$mall_id, 'level' => $level, 'status' => StatusEnum::ENABLED]); if (empty($cloud_stock_level_model)) return false; if ($cloud_stock_agent->level >= $level) return true; $cloud_stock_agent->level = $level; $cloud_stock_agent->upgrade_status = CloudStockEnum::UPGRADE_STATUS_GOODS; $cloud_stock_agent->upgrade_at = $time; $cloud_stock_agent->before_level = $before_level; $res = $cloud_stock_agent->save(); if ($res) { LevelUp::updateQuota($cloud_stock_agent); LevelUp::sendMessage($mall_id, $cloud_stock_level_model['name'], $cloud_stock_agent['user_id']); LevelUp::addUpgradeAgentToArr($mall_id, $user_id); } foreach ($upgrade_bag_list as $item) { $goods_ids = []; if (!empty(intval($item['goods_id']))) { $goods_ids[] = intval($item['goods_id']); } else { $goods_ids = json_decode($item['goods_id'], true); } if (array_intersect($buy_goods_id_arr, $goods_ids)) { if(!empty($item['give_goods_num'])){ if (!empty(intval($item['give_goods_num']))) { $give_goods_num[] = intval($item['give_goods_num']); if (!empty($item['give_goods_id'])) { if (isset($give_arr[$item['give_goods_id']])) { $give_arr[$item['give_goods_id']] += $item['give_goods_num']; } else { $give_arr[$item['give_goods_id']] = $item['give_goods_num']; } } } else { $give_goods_num = json_decode($item['give_goods_num'], true); foreach ($give_goods_num as $give_goods) { if (isset($give_arr[$give_goods['goods_id']])) { $give_arr[$give_goods['goods_id']] += $give_goods['num']; } else { $give_arr[$give_goods['goods_id']] = $give_goods['num']; } } } } break; } } //赠送库存 try { self::giveCloudStockGoods($mall_id, $user_id, $order_id, $level, $upgrade_bag_list, $buy_goods_id_arr, $goods_price_arr,$level_up_give_stock_by_parent,$bag_commission); }catch (\Exception $e){ var_dump('addons\CloudStock\common\logic\level_up:upgradeBagUpNew:'.$e->getMessage().$e->getFile().$e->getLine()); } } //送库存,送奖励 public static function giveCloudStockGoods($mall_id, $buy_user_id, $order_id, $level, $upgrade_bag_list, $buy_goods_id_arr, $goods_price_arr,$level_up_give_stock_by_parent,$bag_commission) { $user = User::findOne(['id' => $buy_user_id]); $parent_id = $user['parent_id']; $second_parent_id = $user['second_parent_id']; $direct_reward = 0; $indirect_reward = 0; $is_percent = 0;//默认固定金额 $total_goods_price = 0; $direct_reward_capital_type = ''; $indirect_reward_capital_type = ''; $p_cloud_stock_agent_list = CloudStockAgent::lists([ 'where' => [ ['user_id' => [$parent_id, $second_parent_id]], ['status' => StatusEnum::ENABLED], ['<=', 'level', $level], ], 'index' => 'user_id', ]); foreach ($upgrade_bag_list as $item) { $goods_ids = []; if(!empty($item['goods_id'])){ if (!empty(intval($item['goods_id']))) { $goods_ids[] = intval($item['goods_id']); } else { $goods_ids = json_decode($item['goods_id'], true); } } $intersect_goods_ids = array_intersect($buy_goods_id_arr, $goods_ids);//升级商品id if (!empty($intersect_goods_ids)) { $level_reward = json_decode($item['level_reward'], true); if(is_array($level_reward)){ $level_reward = array_column($level_reward,null,'level'); } if(!empty($item['give_goods_num'])){ if (!empty(intval($item['give_goods_num']))) { $give_goods_num[] = intval($item['give_goods_num']); if (!empty($item['give_goods_id'])) { if (isset($give_arr[$item['give_goods_id']])) { $give_arr[$item['give_goods_id']] += $item['give_goods_num']; } else { $give_arr[$item['give_goods_id']] = $item['give_goods_num']; } } } else { $give_goods_num = json_decode($item['give_goods_num'], true); foreach ($give_goods_num as $give_goods) { if (isset($give_arr[$give_goods['goods_id']])) { $give_arr[$give_goods['goods_id']] += $give_goods['num']; } else { $give_arr[$give_goods['goods_id']] = $give_goods['num']; } } } } if (!isset($p_cloud_stock_agent_list[$parent_id])) { $parent_id = 0; } else { if ($p_cloud_stock_agent_list[$parent_id]['level'] == $level) { $direct_reward = $level_reward[$p_cloud_stock_agent_list[$parent_id]['level']]['direct_equal_reward'] ?? 0; $direct_reward_capital_type = 'first_equal'; } else { $direct_reward = $level_reward[$p_cloud_stock_agent_list[$parent_id]['level']]['direct_over_reward'] ?? 0; $direct_reward_capital_type = 'first_over'; } } if (!isset($p_cloud_stock_agent_list[$second_parent_id])) { $second_parent_id = 0; } else { if ($p_cloud_stock_agent_list[$second_parent_id]['level'] == $level) { $indirect_reward = $level_reward[$p_cloud_stock_agent_list[$second_parent_id]['level']]['indirect_equal_reward'] ?? 0; $indirect_reward_capital_type = 'second_equal'; } else { $indirect_reward = $level_reward[$p_cloud_stock_agent_list[$second_parent_id]['level']]['indirect_over_reward'] ?? 0; $indirect_reward_capital_type = 'second_over'; } } $is_percent = $item['is_percent'] ?? 0; break; } } if (!empty($intersect_goods_ids)) { foreach ($intersect_goods_ids as $goods_id) { if (isset($goods_price_arr[$goods_id])) { $total_goods_price = bcadd($total_goods_price, $goods_price_arr[$goods_id], 2); } } } $payment_for_goods = $total_goods_price; if ($is_percent == 1) { //百分比 if ($total_goods_price > 0) { $direct_reward = bcdiv($direct_reward * $total_goods_price, 100, 2); $indirect_reward = bcdiv($indirect_reward * $total_goods_price, 100, 2); } else { $direct_reward = 0; $indirect_reward = 0; } } if ($payment_for_goods > $direct_reward) { $payment_for_goods = bcsub($payment_for_goods, $direct_reward, 2); } else { $payment_for_goods = 0; $direct_reward = $payment_for_goods; } if ($payment_for_goods > $indirect_reward) { $payment_for_goods = bcsub($payment_for_goods, $indirect_reward, 2); } else { $payment_for_goods = 0; $indirect_reward = $payment_for_goods; } //扣除分佣 if(!empty($bag_commission)){ $payment_for_goods = bcsub($payment_for_goods, $bag_commission, 2); } if ($parent_id > 0 && $direct_reward > 0) { $insert_wallet[] = [ 'mall_id' => $mall_id, 'user_id' => $parent_id, 'is_frozen' => false, 'wallet_type' => 'commission', 'money' => $direct_reward, 'desc' => '云库存直推奖励', 'source_table' => Order::tableName(), 'source_table_id' => $order_id, 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'capital_type' => $direct_reward_capital_type, ]; } if ($second_parent_id > 0 && $indirect_reward > 0) { $insert_wallet[] = [ 'mall_id' => $mall_id, 'user_id' => $second_parent_id, 'is_frozen' => false, 'wallet_type' => 'commission', 'money' => $indirect_reward, 'desc' => '云库存间推奖励', 'source_table' => Order::tableName(), 'source_table_id' => $order_id, 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'capital_type' => $indirect_reward_capital_type, ]; } if (!empty($insert_wallet)) CloudStockAgent::sendUserWalletQueue($insert_wallet); if (!empty($give_arr)) { //库存变动表 $stock_log_arr = []; foreach ($give_arr as $goods_id => $num) { if (empty($num)) continue; $params = [ 'mall_id' => $mall_id, 'user_id' => $buy_user_id, 'goods_id' => $goods_id, 'num' => $num, 'order_id' => $order_id, 'changes_type' => CloudStockEnum::STOCK_CHANGE_TYPE105, 'changes_dir' => CloudStockEnum::STOCK_CHANGE_ADD, 'order_type' => CloudStockEnum::STOCK_ORDER_TYPE_USER, ]; $res = CloudStockAgentGoods::addData($params, $increase_detail); if (!$res) throw new \Exception(CloudStockAgentGoods::getStaticError()); CloudStockAgent::fixStock($mall_id,$buy_user_id); //库存变化记录 $stock_log_params = [ 'mall_id' => $mall_id, 'goods_id' => $goods_id, 'user_id' => 0, 'changes_dir' => CloudStockEnum::STOCK_CHANGE_SUB, 'order_id' => $order_id, 'num' => $num, 'order_type' => CloudStockEnum::STOCK_ORDER_TYPE_USER, 'changes_type' => CloudStockEnum::STOCK_CHANGE_TYPE105, ]; $stock_log = CloudStockGoodsStockLog::setData($mall_id,$stock_log_params); if (!$stock_log) throw new \Exception(CloudStockGoodsStockLog::getStaticError()); $stock_log_arr[$goods_id] = $stock_log->id; } if(!empty($level_up_give_stock_by_parent)){ $res = self::checkDeductionStock($mall_id,$order_id, $buy_user_id, $level, $give_arr, $payment_for_goods,$stock_log_arr); var_dump($res); if(!empty($res['is_sub_stock'])){ self::deductionStock($mall_id, $res['sub_user_id']??0, $order_id, $give_arr, $payment_for_goods,$stock_log_arr,$bag_commission); } }else{ self::deductionStock($mall_id, 0, $order_id, $give_arr, $payment_for_goods,$stock_log_arr); } } } public static function checkDeductionStock($mall_id,$order_id, $buy_user_id, $level, $give_arr, $payment_for_goods,$stock_log_arr = []) { $give_goods_ids = array_keys($give_arr); $p_cloud_stock_agent_arr = self::getParentCloudStockAgent($mall_id,$level,$buy_user_id); $is_sub_stock = 1; $sub_user_id = 0; if (!empty($p_cloud_stock_agent_arr)) { $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic'); if (!empty($configs['is_time_limit_fill'])) { //开启延时补货 $first_parent = $p_cloud_stock_agent_arr[0]; $agent_goods_list = CloudStockAgentGoods::lists([ 'where' => [ ['mall_id' => $mall_id], ['user_id' => $first_parent['user_id']], ['goods_id' => $give_goods_ids], ], 'index' => 'goods_id', ]); $goods_ids = ''; $num_s = ''; foreach ($give_arr as $g_id => $num) { if (!isset($agent_goods_list[$g_id]) || $agent_goods_list[$g_id]['num'] < $num) { //延时补货 $goods_ids.=$g_id.','; $num_s.=$num.','; $is_sub_stock = 0; $send_user_id = $first_parent['user_id']; } } if ($is_sub_stock == 1) { $sub_user_id = $first_parent['user_id']; } if($is_sub_stock==0){ //投入延时队列 $post_data = [ 'mall_id' => $mall_id, 'order_id' => $order_id, 'give_arr' => $give_arr, 'payment_for_goods' => $payment_for_goods, 'buy_user_id' => $buy_user_id, 'level' => $level, 'stock_log_arr' => $stock_log_arr, ]; var_dump('投入延时队列数据:'.json_encode($post_data)); $fill_hour = $configs['fill_hour']??0; \Yii::$app->services->rabbitMq->delay($fill_hour * 3600, $post_data, DeductionJob::class, 'delayDeductionStock'); if(!empty($send_user_id)){ //补货通知 try{ $SmsConfig = new Sms($mall_id); $easy_sms = $SmsConfig->easySms; $message = new TimeLimitFillMessage($mall_id, $fill_hour, trim($goods_ids,','), trim($num_s,',')); $user = User::findOne(['id' => $send_user_id]); $easy_sms->send($user['mobile'], $message); }catch (NoGatewayAvailableException $e){ $res = $e->getExceptions(); if(!empty($res)){ foreach ($res as $exception){ if(is_string($exception->getMessage())){ var_dump('云库存购买升级礼包补货通知短信发送失败:' .$exception->getMessage()); } } } }catch (\Exception $e){ var_dump('云库存购买升级礼包补货通知短信发送失败:' .$e->getMessage()); } } } } else { $sub_user_id = self::checkDeductionStockNoDelay($mall_id,$buy_user_id,$level,$give_arr); } } return ['is_sub_stock' => $is_sub_stock, 'sub_user_id' => $sub_user_id]; } public static function checkDeductionStockNoDelay($mall_id,$buy_user_id,$level,$give_arr){ $p_cloud_stock_agent_arr = self::getParentCloudStockAgent($mall_id,$level,$buy_user_id); $give_goods_ids = array_keys($give_arr); $u_pids = array_column($p_cloud_stock_agent_arr, 'user_id'); $agent_goods_list = CloudStockAgentGoods::lists([ 'where' => [ ['mall_id' => $mall_id], ['user_id' => $u_pids], ['goods_id' => $give_goods_ids], ] ]); $agent_goods_arr = []; foreach ($agent_goods_list as $val) { $agent_goods_arr[$val['user_id']][$val['goods_id']] = $val; } $sub_user_id = 0; if (!empty($agent_goods_arr)) { foreach ($p_cloud_stock_agent_arr as $agent){ if(isset($agent_goods_arr[$agent['user_id']])){ $agent_goods = $agent_goods_arr[$agent['user_id']]; $sub_user_id = $agent['user_id']; foreach ($give_arr as $g_id => $num) { if (!isset($agent_goods[$g_id]) || $agent_goods[$g_id]['num'] < $num) { $sub_user_id = 0; break; } } if ($sub_user_id > 0) { break; } } } } return $sub_user_id; } //获取上级代理 public static function getParentCloudStockAgent($mall_id,$level,$buy_user_id){ $parent_id_arr = UserPartitionRelationship::getParentIdArr($buy_user_id); $parent_id_arr = array_reverse($parent_id_arr); $p_cloud_stock_agent_list = CloudStockAgent::lists([ 'where' => [ ['mall_id' => $mall_id], ['user_id' => $parent_id_arr], ['status' => StatusEnum::ENABLED], ['>', 'level', $level], ], 'index' => 'user_id', ]); $p_cloud_stock_agent_arr = []; foreach ($parent_id_arr as $u_id) { if (isset($p_cloud_stock_agent_list[$u_id])) { $p_cloud_stock_agent_arr[] = $p_cloud_stock_agent_list[$u_id]; } } return $p_cloud_stock_agent_arr; } //扣库存 public static function deductionStock($mall_id, $sub_user_id, $order_id, $give_arr, $payment_for_goods,$stock_log_arr = []) { if (!empty($give_arr)) { if($sub_user_id>0){ foreach ($give_arr as $goods_id => $num) { if (empty($num)) continue; $params = [ 'mall_id' => $mall_id, 'user_id' => $sub_user_id, 'goods_id' => $goods_id, 'num' => $num, 'order_id' => $order_id, 'changes_type' => CloudStockEnum::STOCK_CHANGE_TYPE105, 'changes_dir' => CloudStockEnum::STOCK_CHANGE_SUB, 'order_type' => CloudStockEnum::STOCK_ORDER_TYPE_USER, 'stock_log_id' => !empty($stock_log_arr[$goods_id]) ? $stock_log_arr[$goods_id] : 0, ]; $res = CloudStockAgentGoods::subData($params); if (!$res) throw new \Exception(CloudStockAgentGoods::getStaticError()); CloudStockAgent::fixStock($mall_id,$sub_user_id); var_dump('扣减库存成功'); } var_dump('货款'.$payment_for_goods); if($payment_for_goods>0){ $source_table = Order::tableName(); $source_table_id = $order_id; $insert_wallet[] = [ 'mall_id' => $mall_id, 'user_id' => $sub_user_id, 'is_frozen' => false, 'wallet_type' => 'commission', 'money' => $payment_for_goods, 'desc' => '会员购买升级礼包赠送库存货款', 'source_table' => $source_table, 'source_table_id' => $source_table_id, 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'capital_type' => 'payment_for_level_goods_buy', ]; if (!empty($insert_wallet)) CloudStockAgent::sendUserWalletQueue($insert_wallet); } }else{ foreach ($give_arr as $goods_id => $num) { $params = [ 'stock_log_id' => !empty($stock_log_arr[$goods_id]) ? $stock_log_arr[$goods_id] : 0, 'user_id' => 0, 'num' =>$num, 'not_yet_num' => 0, 'changes_type' => CloudStockEnum::STOCK_CHANGE_TYPE105, 'order_id' => $order_id, 'goods_id' => $goods_id, 'mall_id' => $mall_id, ]; $res = CloudStockDeductionDetail::setData($params); if($res){ //触发库存变动事件 $event = new CloudStockChangeEvent($mall_id); $event_data = [ 'id' => $res->id, 'type' => CloudStockEnum::STOCK_CHANGE_SUB, 'mall_id' => $mall_id, ]; \Yii::$app->services->events->dispatch($event, $event_data, $event->listeners); } } } } } }