| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- <?php
- namespace addons\CloudStockTwo\common\logic\level_up;
- use addons\CloudStockTwo\common\enums\CloudStockTwoEnum;
- use addons\CloudStockTwo\common\logic\order\FillOrderLogic;
- use addons\CloudStockTwo\common\models\CloudStockTwoAgent;
- use addons\CloudStockTwo\common\models\CloudStockTwoAgentGoods;
- use addons\CloudStockTwo\common\models\CloudStockTwoAgentQuota;
- use addons\CloudStockTwo\common\models\CloudStockTwoFillOrder;
- use addons\CloudStockTwo\common\models\CloudStockTwoLevel;
- use addons\CloudStockTwo\common\models\CloudStockTwoUpgradeBag;
- use addons\CloudStockTwo\common\sms\LevelUpgradeMessage;
- use addons\CloudStockTwo\common\sms\SmsConfig;
- use addons\CloudStockTwo\common\logic\reward\LevelRewardLogic;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\helpers\sms\Sms;
- use common\helpers\StringHelper;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use common\models\user\UserPartitionRelationship;
- use Overtrue\EasySms\EasySms;
- use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
- use yii\db\Exception;
- class LevelUp
- {
- public static $reward_unit_price = 0;
- public static function handle($mall_id, $user_id, $goods_id_arr, $order_id,$order_type)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK_TWO, 'basic');
- if (empty($configs['is_enable'])) throw new Exception('没开启云库存');
- $upgrade_bag_list = CloudStockTwoUpgradeBag::lists(['where' => [['mall_id' => $mall_id], ['goods_id' => $goods_id_arr], ['status' => StatusEnum::ENABLED]], 'order' => 'level asc']);
- $cloud_stock_agent = CloudStockTwoAgent::findOne(['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if (empty($cloud_stock_agent)) {
- $cloud_stock_agent = new CloudStockTwoAgent();
- $cloud_stock_agent->user_id = $user_id;
- $cloud_stock_agent->mall_id = $mall_id;
- }
- $before_level = $cloud_stock_agent->level ?? 0;
- //购买升级礼包升级
- $time = time();
- if (!empty($upgrade_bag_list)) {
- $level_up_give_stock_by_parent = $configs['level_up_give_stock_by_parent'] ?? 0;
- self::upgradeBagUp($mall_id, $user_id, $order_id, $upgrade_bag_list, $cloud_stock_agent, $time, $before_level, $level_up_give_stock_by_parent,$order_type);
- }
- $level = 0;
- if (!empty($cloud_stock_agent->level)) $level = $cloud_stock_agent->level;
- $level_list = CloudStockTwoLevel::lists(['where' => [
- ['mall_id' => $mall_id],
- ['>', 'level', $level],
- ['status' => StatusEnum::ENABLED]
- ], 'order' => 'level desc'
- ]);
- //购买商品升级
- if (!empty($level_list)) {
- self::conditionUp($user_id, $goods_id_arr, $cloud_stock_agent, $time, $before_level, $level_list,$order_id,$order_type);
- }
- $t->commit();
- //触发升级上级代理商是否满足升级
- $parent_agent_list = self::getUserParentAgentList($mall_id,$user_id);
- foreach ($parent_agent_list as $agent) {
- $level = 0;
- if (!empty($agent->level)) $level = $agent['level'];
- $level_list = CloudStockTwoLevel::lists(['where' => [
- ['mall_id' => $mall_id],
- ['>', 'level', $level],
- ['status' => StatusEnum::ENABLED]
- ], 'order' => 'level desc'
- ]);
- self::conditionUp($agent['user_id'], [], $agent, $time, $agent['before_level'], $level_list,$order_id,$order_type);
- }
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- $exception = FormatHelper::exception($e, $e->getMessage() . $e->getFile() . $e->getLine());
- //echo $exception;
- \Yii::error($exception);
- return false;
- }
- }
- public static function getUserParentAgentList($mall_id,$agent_user_id)
- {
- $parent_id_arr = UserPartitionRelationship::getParentIdArr($agent_user_id);
- $parent_id_arr = array_reverse($parent_id_arr);
- $parent_agent_list = CloudStockTwoAgent::lists(['where' => [['user_id' => $parent_id_arr],['status'=>StatusEnum::ENABLED]], 'index' => 'user_id'], false);
- $list = [];
- foreach ($parent_id_arr as $user_id) {
- if (isset($parent_agent_list[$user_id])) {
- $list[] = $parent_agent_list[$user_id];
- }else{
- $model = new CloudStockTwoAgent();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- $model->user_id = $user_id;
- $model->level = 0;
- $model->before_level = 0;
- $list[] = $model;
- }
- }
- return $list;
- }
- public static function upgradeBagUp($mall_id, $user_id, $order_id, $upgrade_bag_list, $cloud_stock_agent, $time, $before_level, $level_up_give_stock_by_parent,$order_type)
- {
- $level = 0;
- $give_arr = [];
- foreach ($upgrade_bag_list as $item) {
- $level = $item['level'];
- // if (!empty($item['give_goods_id'])) {
- // $give_arr[$item['give_goods_id']] = $item['give_goods_num'];
- // }
- if (!empty($item['give_goods_info'])) {
- $item['give_goods_info'] = json_decode($item['give_goods_info'],true) ?? [];
- foreach($item['give_goods_info'] as $info){
- if(!empty($info['num'])){
- $give_arr[$info['goods_id']] = $info['num'];
- }
- }
- }
- }
- $cloud_stock_level_model = CloudStockTwoLevel::findOne(['mall_id' => $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 = CloudStockTwoEnum::UPGRADE_STATUS_GOODS;
- $cloud_stock_agent->upgrade_at = $time;
- $cloud_stock_agent->before_level = $before_level;
- $res = $cloud_stock_agent->save();
- if ($res) {
- self::updateQuota($cloud_stock_agent);
- self::sendMessage($mall_id, $cloud_stock_level_model['name'], $cloud_stock_agent['user_id']);
- //触发直推上级奖励
- //支付金额
- if($order_type == 'fill'){
- $order = CloudStockTwoFillOrder::findOne(['id'=>$order_id]);
- $price = $order->pay_money ?? 0;
- }else{
- $order = OrderDetail::findOne(['order_id'=>$order_id]);
- $price = $order->goods_price;
- $goods_id = $order->goods_id;
- }
- if($order){
- $LevelRewardLogicObj = new LevelRewardLogic($mall_id,$cloud_stock_agent,$order_type,$order_id,$cloud_stock_level_model['name'],$price,$order->goods_id,$order->num);
- $LevelRewardLogicObj->addRewardLog();
- //返回直推奖励
- self::$reward_unit_price += $LevelRewardLogicObj->reward_unit_price;
- }
- }
- //赠送库存
- foreach ($give_arr as $goods_id => $num) {
- if (!empty($level_up_give_stock_by_parent)) {
- //走补货的逻辑
- $order_no = StringHelper::generateOrderNo('CSFL');
- $params = [
- 'user_id' => $user_id,
- 'pay_money' => 0,
- 'order_no' => $order_no,
- 'is_pay' => CloudStockTwoEnum::FILL_STATUS_PAY,
- 'goods_id' => $goods_id,
- 'num' => $num,
- ];
- $order = CloudStockTwoFillOrder::setData($mall_id, $params);
- if ($order) {
- $obj = new FillOrderLogic();
- $obj->handle($order);
- }
- } else {
- //最初的方案
- //生成代理商商品库存
- $params = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'goods_id' => $goods_id,
- 'num' => $num,
- 'order_id' => $order_id,
- 'changes_type' => CloudStockTwoEnum::STOCK_CHANGE_TYPE105,
- 'changes_dir' => CloudStockTwoEnum::STOCK_CHANGE_ADD,
- 'order_type' => CloudStockTwoEnum::STOCK_ORDER_TYPE_USER,
- ];
- $res = CloudStockTwoAgentGoods::addData($params);
- if (!$res) throw new \Exception(CloudStockTwoAgentGoods::getStaticError());
- }
- }
- }
- public static function conditionUp($user_id, $goods_id_arr, $cloud_stock_agent, $time, $before_level, $level_list,$order_id,$order_type)
- {
- $can_to_level = 0;
- $type = 1;
- $is_can_up = false;
- $mall_id = 0;
- $params['user_id'] = $user_id;
- $params['goods_id_arr'] = $goods_id_arr;
- $params['order_type'] = $order_type;
- $params['order_id'] = $order_id;
- $is_can_up = false;
- $level_name = '';
- foreach ($level_list as $level_item) {
- $can_to_level = $level_item['level'];
- $level_name = $level_item['name'];
- $mall_id = $level_item['mall_id'];
- if (empty($level_item['upgrade_type_condition'])) continue;
- if (!empty($level_item['condition_type'])) {
- $condition_type = $level_item['condition_type'];
- $checked_condition_values = json_decode($level_item['checked_condition_values'], true);
- $checked_condition_keys = json_decode($level_item['checked_condition_keys'], true);
- foreach ($checked_condition_keys as $key) {
- $method = 'condition' . $key;
- $is_bool = self::$method($checked_condition_values, $key, $params);
- //满足其一
- if ($condition_type == 1) {
- if ($is_bool) {
- $is_can_up = true;
- break;
- } else {
- $is_can_up = false;
- }
- }
- //满足所有
- if ($condition_type == 2) {
- if ($is_bool) {
- $is_can_up = true;
- } else {
- $is_can_up = false;
- break;
- }
- }
- }
- if($is_can_up) break;
- }
- }
- if ($is_can_up && $can_to_level > $cloud_stock_agent->level) {
- $cloud_stock_agent->level = $can_to_level;
- $cloud_stock_agent->upgrade_status = CloudStockTwoEnum::UPGRADE_STATUS_CONDITION;
- $cloud_stock_agent->upgrade_at = $time;
- $cloud_stock_agent->before_level = $before_level;
- $res = $cloud_stock_agent->save();
- if ($res) {
- self::updateQuota($cloud_stock_agent);
- self::sendMessage($mall_id, $level_name, $cloud_stock_agent['user_id']);
- //触发直推上级奖励
- //支付金额
- if($order_type == 'fill'){
- $order = CloudStockTwoFillOrder::findOne(['id'=>$order_id]);
- $price = $order->pay_money ?? 0;
- }else{
- $order = OrderDetail::findOne(['order_id'=>$order_id]);
- $price = $order->goods_price;
- $goods_id = $order->goods_id;
- }
- if($order){
- $LevelRewardLogicObj = new LevelRewardLogic($mall_id,$cloud_stock_agent,$order_type,$order_id,$level_name,$price,$order->goods_id,$order->num);
- $LevelRewardLogicObj->addRewardLog();
- //返回直推奖励
- self::$reward_unit_price += $LevelRewardLogicObj->reward_unit_price;
- }
- }
- }
- }
- public static function updateQuota($cloud_stock_agent)
- {
- $level = CloudStockTwoLevel::findOne(['mall_id' => $cloud_stock_agent['mall_id'], 'level' => $cloud_stock_agent['level'], 'status' => [StatusEnum::ENABLED]]);
- if (!empty($level)) {
- $give_quota = json_decode($level['give_quota'], true);
- foreach ($give_quota as $item) {
- if (!empty($item['is_use']) && !empty($item['num'])) {
- $params = [];
- $params['mall_id'] = $cloud_stock_agent['mall_id'];
- $params['user_id'] = $cloud_stock_agent['user_id'];
- $params['num'] = $item['num'];
- $params['level'] = $item['level'];
- $res = CloudStockTwoAgentQuota::setData($params);
- if ($res == false) throw new Exception(CloudStockTwoAgentQuota::getStaticError());
- }
- }
- }
- }
- public static function sendMessage($mall_id, $level_name, $user_id)
- {
- try {
- $SmsConfig = new Sms($mall_id);
- $easy_sms = $SmsConfig->easySms;
- $message = new LevelUpgradeMessage($mall_id, $level_name);
- $user = User::findOne(['id' => $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())) {
- \Yii::error('云库存升级短信发送失败:' . $exception->getMessage());
- }
- }
- }
- } catch (\Exception $e) {
- \Yii::error('云库存升级短信发送失败:' . $e->getMessage());
- }
- }
- protected static function condition0($checked_condition_values, $key, $params)
- {
- //购买指定商品
- $upgrade = false;
- //取出等级升级商品id
- $value_goods_ids = [];
- if (isset($checked_condition_values[$key]['value'])) $value_goods_ids = array_column($checked_condition_values[$key]['value'], 'goods_id');
- if (empty($value_goods_ids)) $upgrade = false;
- $goods_id_arr = $params['goods_id_arr'];
- foreach ($goods_id_arr as $goods_id) {
- if (in_array($goods_id, $value_goods_ids)) {
- if($params['order_type'] == 'fill'){
- $order = CloudStockTwoFillOrder::findOne([['id' => $params['order_id']]]);
- $order->is_first = 1;
- $order->save();
- }
- $upgrade = true;
- break;
- }
- }
- return $upgrade;
- }
- protected static function condition1($checked_condition_values, $key, $params)
- {
- $user_id = $params['user_id'];
- $child_user_ids = User::find()->where(['parent_id'=>$user_id])->asArray()->all();
- $level_arr = [];
- $use_level_arr = [];
- foreach ($checked_condition_values[$key]['value'] as $val) {
- if ($val['is_use']) $use_level_arr[] = $val['level'];
- }
- $user_id_arr = array_column($child_user_ids, 'id');
- $agent_list = CloudStockTwoAgent::lists(['where' => [['user_id' => $user_id_arr], ['level' => $use_level_arr], ['status' => StatusEnum::ENABLED]]]);
- foreach ($agent_list as $val) {
- if (isset($level_arr[$val['level']])) {
- $level_arr[$val['level']] += 1;
- } else {
- $level_arr[$val['level']] = 1;
- }
- }
- $upgrade = true;
- foreach ($checked_condition_values[$key]['value'] as $val) {
- if ($val['is_use'] && !empty($val['num'])) {
- if (empty($level_arr[$val['level']]) || $level_arr[$val['level']] < $val['num']) {
- $upgrade = false;
- continue;
- }
- }
- }
- return $upgrade;
- }
- // protected static function condition2($checked_condition_values, $key, $params)
- // {
- // $user_id = $params['user_id'];
- // $value_goods_ids = array_column($checked_condition_values[$key]['value'], 'id');
- // //条件 radio_type=1 表示或(满足其一);radio_type=2 表示与(同时满足)
- // $radio_type = isset($checked_condition_values[$key]["radio_type"]) ? $checked_condition_values[$key]["radio_type"] : 1;
- // $fill_order_list = CloudStockTwoFillOrder::lists(['where' => [['user_id' => $user_id], ['goods_id' => $value_goods_ids], ['is_pay' => 1]],
- // 'select' => 'sum(num) as num,goods_id',
- // 'group' => 'goods_id',
- // 'index' => 'goods_id'
- // ]);
- // //0待付款 1待发货 2 已发货 3已收货 4已完成 -1申请售后 -2售后中 -3售后完成 -4 已关闭 -5撤销售后】
- // $order_detail_list = OrderDetail::lists(['where' => [['user_id' => $user_id], ['goods_id' => $value_goods_ids], ['order_status' => [1, 2, 3, 4, -5]]],
- // 'select' => 'sum(num) as num,goods_id',
- // 'group' => 'goods_id',
- // 'index' => 'goods_id'
- // ]);
- // if ($radio_type == 1) {
- // $upgrade = false;
- // foreach ($checked_condition_values[$key]['value'] as $row) {
- // $num = 0;
- // if (!empty($fill_order_list[$row['id']]['num'])) $num += $fill_order_list[$row['id']]['num'];
- // if (!empty($order_detail_list[$row['id']]['num'])) $num += $order_detail_list[$row['id']]['num'];
- // if ($num >= $row['num']) {
- // $upgrade = true;
- // break;
- // }
- // }
- // } else {
- // $upgrade = true;
- // if (empty($checked_condition_values[$key]['value'])) {
- // $upgrade = false;
- // } else {
- // foreach ($checked_condition_values[$key]['value'] as $row) {
- // $num = 0;
- // if (!empty($fill_order_list[$row['goods_id']])) $num += $fill_order_list[$row['goods_id']];
- // if (!empty($order_detail_list[$row['goods_id']])) $num += $order_detail_list[$row['goods_id']];
- // if ($num < $row['num']) {
- // $upgrade = false;
- // break;
- // }
- // }
- // }
- // }
- // return $upgrade;
- // }
- protected static function condition2($checked_condition_values, $key, $params)
- {
- $user_id = $params['user_id'];
- $child_user_ids = User::find()->where(['parent_id'=>$user_id])->asArray()->all();
- $level_arr = [];
- $use_level_arr = [];
- foreach ($checked_condition_values[$key]['value'] as $val) {
- if ($val['is_use']) $use_level_arr[] = $val['level'];
- }
- $user_id_arr = array_column($child_user_ids, 'id');
- $agent_list = CloudStockTwoAgent::lists(['where' => [['user_id' => $user_id_arr], ['level' => $use_level_arr], ['status' => StatusEnum::ENABLED]]]);
- $user_id_arr = array_column($agent_list, 'user_id');
- $order_list = CloudStockTwoFillOrder::find()
- ->where(['user_id'=>$user_id_arr,'is_pay'=>1])
- ->select('sum(pay_money) as pay_money,user_id')
- ->groupBy('user_id')
- ->indexBy('user_id')
- ->asArray()
- ->all();
- foreach ($agent_list as $val) {
- $price = $order_list[$val['user_id']]['pay_money'] ?? 0;
- if (isset($level_arr[$val['level']])) {
- $level_arr[$val['level']] += $price;
- } else {
- $level_arr[$val['level']] = $price;
- }
- }
- $upgrade = true;
- foreach ($checked_condition_values[$key]['value'] as $val) {
- if ($val['is_use'] && !empty($val['num'])) {
- if (!isset($level_arr[$val['level']]) || empty($level_arr[$val['level']]) || $level_arr[$val['level']] < $val['num']) {
- $upgrade = false;
- continue;
- }
- }
- }
- return $upgrade;
- }
- protected static function condition3($checked_condition_values, $key, $params)
- {
- $user_id = $params['user_id'];
- $upgrade = false;
- if($params['order_type'] == 'fill'){
- $nums = $checked_condition_values[$key]['nums'] ?? 0;
- $order = CloudStockTwoFillOrder::findOne([['id' => $params['order_id']]]);
- if ($order['pay_money']>=$nums && $user_id == $order['user_id']) $upgrade = true;
- $order->is_first = 1;
- $order->save();
- }
- return $upgrade;
- }
- protected static function condition4($checked_condition_values, $key, $params)
- {
- $user_id = $params['user_id'];
- $first_child_user_ids = User::find()->where(['parent_id'=>$user_id])->asArray()->all();
- $child_id_arr = UserPartitionRelationship::getChildIdArr($user_id);
- $level_arr = [];
- $direct_level = [];
- $use_level_arr = [];
- foreach ($checked_condition_values[$key]['value'] as $val) {
- if ($val['is_use']) $use_level_arr[] = $val['level'];
- }
- $user_id_arr = array_column($first_child_user_ids, 'id');
- $agent_list = CloudStockTwoAgent::lists(['where' => [['user_id' => $child_id_arr], ['level' => $use_level_arr], ['status' => StatusEnum::ENABLED]]]);
- foreach ($agent_list as $val) {
- if (isset($level_arr[$val['level']])) {
- $level_arr[$val['level']] += 1;
- } else {
- $level_arr[$val['level']] = 1;
- }
- if(in_array($val['user_id'],$user_id_arr)){
- if (isset($direct_level[$val['level']])) {
- $direct_level[$val['level']] += 1;
- } else {
- $direct_level[$val['level']] = 1;
- }
- }
- }
- $upgrade = true;
- foreach ($checked_condition_values[$key]['value'] as $val) {
- if ($val['is_use'] && !empty($val['num'])) {
- if (!isset($level_arr[$val['level']]) || empty($level_arr[$val['level']]) || $level_arr[$val['level']] < $val['num']) {
- $upgrade = false;
- continue;
- }
- }
- if ($val['is_use'] && !empty($val['direct_num'])) {
- if (!isset($direct_level[$val['level']]) || empty($direct_level[$val['level']]) || $direct_level[$val['level']] < $val['direct_num']) {
- $upgrade = false;
- continue;
- }
- }
- }
- return $upgrade;
- }
- }
|