| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace addons\DoubleCopy\common\logic;
- use addons\DoubleCopy\common\enums\DoubleCopyEnum;
- use addons\DoubleCopy\common\models\DoubleCopyAchievement;
- use addons\DoubleCopy\common\models\DoubleCopyLevel;
- use addons\DoubleCopy\common\models\DoubleCopyPoolLevel;
- use addons\DoubleCopy\common\models\DoubleCopyPoolSource;
- use addons\DoubleCopy\common\models\DoubleCopyPoolUser;
- use addons\DoubleCopy\common\models\DoubleCopyUser;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\helpers\ArrayHelper;
- use common\helpers\FormatHelper;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\user\UserPartitionRelationship;
- use common\traits\ErrorTrait;
- use Yii;
- use yii\db\Exception;
- use yii\helpers\Json;
- /**
- * 分红池逻辑处理
- * @Author: lun
- * @DateTime: 2022/9/8 0008 16:10
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @package addons\DoubleCopy\common\logic
- */
- class PoolLogic
- {
- use ErrorTrait;
- public $mall_id;
- public $user_id;
- public $order_id;
- public $config;
- public $order_detail;
- public $order_status;
- public $task_counter_arr;
- public function __construct($order, $order_status, $task_counter_arr = [])
- {
- $this->mall_id = $order['mall_id'];
- $this->user_id = $order['user_id'];
- $this->order_id = $order['id'];
- $this->order_detail = $order['detail'];
- $this->order_status = $order_status;
- $this->task_counter_arr = $task_counter_arr;
- // 获取配置
- $this->config = Yii::$app->services->setting->addons->get($order['mall_id'], DoubleCopyEnum::ADDONS_NAME, 'basic');
- }
- /**
- * 执行
- * @Author: lun
- * @DateTime: 2022/9/16 0016 14:54
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return bool
- */
- public function exec()
- {
- try {
- $this->placePool();
- $this->upgrade();
- return true;
- } catch (\Exception $e) {
- $this->setError($e->getMessage());
- return false;
- }
- }
- /**
- * 计入分红池金额
- * @Author: lun
- * @DateTime: 2022/9/16 0016 14:45
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return bool
- * @throws Exception
- */
- public function placePool()
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- // 检查是否可以插入分红池
- if (empty($this->config['deposit_type']) && $this->order_status == OrderStatusEnum::PAY) {
- $trans->commit();
- return true;
- }
- if (!empty($this->config['deposit_type']) && $this->config['deposit_type'] == 1 && $this->order_status == OrderStatusEnum::ACCOMPLISH) {
- $trans->commit();
- return true;
- }
- // 获取等级中的商品设置
- $pool_level = DoubleCopyPoolLevel::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->asArray()->all();
- if (empty($pool_level)) {
- $trans->commit();
- return true;
- }
- $time = time();
- $commission_arr = [];
- $reward_currency = $this->config['reward_currency'] ?? 'commission';
- foreach ($pool_level as $pool) {
- $goods_setting = json_decode($pool['goods_setting'], true);
- $goods_ids = array_column($goods_setting, 'goods_id');// 获取该等级中的商品id
- $goods = array_column($goods_setting, null, 'goods_id');// 将商品id作为key
- foreach ($this->order_detail as $detail) {
- // 检查是否已插入过,防止多次插入增加分红池额度
- $check = DoubleCopyPoolSource::find()->where(['mall_id' => $this->mall_id, 'level' => $pool['level'], 'order_detail_id' => $detail['id'], 'status' => StatusEnum::ENABLED])->scalar();
- if ($check) continue;
- // 获取插入分红池金额
- if (!empty($goods[$detail['goods_id']]['is_percent'])) {
- // 按实付金额的百分比插入
- $percent = $goods[$detail['goods_id']]['money'] ?? 0;// 获取比例
- $total_place_money = $percent == 0 ? 0 : bcdiv($detail['goods_price'] * $percent , 100, 2);
- } else {
- // 按照具体金额插入
- $place_money = $goods[$detail['goods_id']]['money'] ?? 0;// 插入分红池金额
- $total_place_money = empty($place_money) ? 0 : $place_money * $detail['num'];// 需要加上商品数量
- }
- if (empty($total_place_money)) continue;
- // 订单商品在设置中
- if (in_array($detail['goods_id'], $goods_ids)) {
- //判断当前奖励类型
- if($reward_currency == 'commission'){
- // 增加分红池金额
- DoubleCopyPoolLevel::savePoolMoney($this->mall_id, $pool['level'], [
- 'current_money' => $total_place_money,
- 'total_money' => $total_place_money,
- ]);
- }else{
- DoubleCopyPoolLevel::savePoolMoney($this->mall_id, $pool['level'], [
- 'current_score' => $total_place_money,
- 'total_score' => $total_place_money,
- ]);
- }
- // 生成记录
- $rows[] = [
- 'mall_id' => $this->mall_id,
- 'level' => $pool['level'],
- 'order_detail_id' => $detail['id'],
- 'money' => $total_place_money,
- 'created_at' => $time,
- 'updated_at' => $time,
- 'reward_currency' => $reward_currency
- ];
- // 放进云库存计数器
- if ($this->order_status == OrderStatusEnum::PAY && $reward_currency == 'commission') {
- if(isset($this->task_counter_arr[$detail['id']][DoubleCopyEnum::ADDONS_NAME]['commission'])){
- $this->task_counter_arr[$detail['id']][DoubleCopyEnum::ADDONS_NAME]['commission'] += $total_place_money;
- }else{
- $this->task_counter_arr[$detail['id']][DoubleCopyEnum::ADDONS_NAME]['commission'] = $total_place_money;
- }
- }
- }
- }
- }
- if (!empty($rows)) {
- $field = ['mall_id','level','order_detail_id','money', 'created_at','updated_at','reward_currency'];
- $res = Yii::$app->db->createCommand()->batchInsert(DoubleCopyPoolSource::tableName(), $field, $rows)->execute();
- if ($res == false) throw new Exception("批量插入计入分红池商品记录失败");
- }
- // 插入云库存计数器
- if (!empty($commission_arr)) {
- Yii::$app->services->taskCounter->set($this->order_id)->callBack($this->mall_id, $this->order_id, $commission_arr);
- }
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- $exception = FormatHelper::exception($e, "插入分红池");
- echo $exception;
- $this->setError($exception);
- return false;
- }
- }
- /**
- * 升级成为分红池等级用户
- * @Author: lun
- * @DateTime: 2022/9/16 0016 10:51
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return bool
- */
- public function upgrade()
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- // 获取用户所有上级
- $parent_ids = UserPartitionRelationship::getParentIdArr($this->user_id);
- if (empty($parent_ids)) {
- $trans->commit();
- return true;
- }
- // 查询二二复制用户的所有上级
- $copy_parent_ids = DoubleCopyUser::find()->select('user_id')->where(['user_id' => $parent_ids, 'status' => StatusEnum::ENABLED])->column();
- if (empty($copy_parent_ids)) {
- $trans->commit();
- return true;
- }
- // 获取分红池用户
- $pool_parent_users = DoubleCopyPoolUser::find()->select('user_id,level')->where(['user_id' => $parent_ids, 'status' => StatusEnum::ENABLED])->indexBy('user_id')->asArray()->all();
- // 用户升级处理
- $this->check($copy_parent_ids, $pool_parent_users);
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- $exception = FormatHelper::exception($e, "分红池用户升级");
- $this->setError($exception);
- return true;
- }
- }
- /**
- * 检查用户升级
- * @Author: lun
- * @DateTime: 2022/9/16 0016 11:28
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $copy_parent_ids
- * @param $pool_parent_users
- * @throws \Exception
- */
- public function check($copy_parent_ids, $pool_parent_users)
- {
- // 获取等级
- $pool_level = DoubleCopyPoolLevel::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->orderBy('level desc')->asArray()->all();
- foreach ($copy_parent_ids as $user_id) {
- // 统计用户12市场的业绩
- $team = DoubleCopyUser::find()->select('achievement')->where(['pid' => $user_id, 'status' => StatusEnum::ENABLED])->andWhere(['<=', 'pos', 2])->asArray()->all();
- if (empty($team)) continue;
- if (count($team) < 2) continue;// 小于两个市场不能升级
- $level = 0;
- foreach ($pool_level as $item) {
- $is_single_pass = true;
- $achievement = 0;// 总业绩
- $team_num = 0;
- foreach ($team as $info) {
- // 判断单个市场是否及格
- if ($info['achievement'] < $item['single_market']) {
- $is_single_pass = false;
- break;
- }else{
- $team_num++;
- }
- $achievement += $info['achievement'];
- }
- // 单个市场业绩符合并且团队业绩也符合才可升级
- // 所有助力会员需满足
- if ($is_single_pass == true && $achievement >= $item['achievement'] && $team_num >= 2) {
- $level = $item['level'];
- break;
- }
- }
- if ($level == 0) continue;// 无升级直接跳过
- // 判断当前等级用户是否符合升级条件
- if (!empty($pool_parent_users[$user_id])) {
- // 已存在的用户升级
- if (!empty($pool_parent_users[$user_id]['level']) && $level > $pool_parent_users[$user_id]['level']) {
- $res = DoubleCopyPoolUser::create(['user_id' => $user_id, 'level' => $level, 'upgrade_status' => DoubleCopyEnum::UPGRADE_STATUS_CONDITION, 'upgrade_level_at' => time()]);
- if ($res == false) throw new \Exception(DoubleCopyPoolUser::getStaticError());
- }
- } else {
- // 新用户升级
- if ($level) {
- $res = DoubleCopyPoolUser::create(['mall_id' => $this->mall_id, 'user_id' => $user_id, 'level' => $level, 'upgrade_status' => DoubleCopyEnum::UPGRADE_STATUS_CONDITION, 'upgrade_level_at' => time()]);
- if ($res == false) throw new \Exception(DoubleCopyPoolUser::getStaticError());
- }
- }
- }
- }
- }
|