From ef516f06a8d80ab3662a0ea953d206e454069377 Mon Sep 17 00:00:00 2001 From: wuhui_zzw <1760308791@qq.com> Date: Wed, 27 Dec 2023 10:34:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=9A=E4=B8=8B=E5=8D=95?= =?UTF-8?q?=E8=B5=A0=E9=80=81=E6=9D=83=E9=87=8D=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../platformCommission/WeightValueDao.php | 23 ++++++ .../platformCommission/WeightValueLogDao.php | 23 ++++++ app/common/model/BaseModel.php | 37 +++++++++ .../store/platformCommission/WeightValue.php | 30 +++++++ .../platformCommission/WeightValueLog.php | 33 ++++++++ .../WeightValueLogRepository.php | 23 ++++++ .../WeightValueRepository.php | 78 +++++++++++++++++++ .../repositories/user/UserRepository.php | 29 +++++++ app/controller/api/Auth.php | 13 +++- .../platformCommission/GiveWeightValueJob.php | 77 ++++++++++++++++++ .../OrderPaySuccessEvent.php | 7 ++ 11 files changed, 371 insertions(+), 2 deletions(-) create mode 100644 app/common/dao/store/platformCommission/WeightValueDao.php create mode 100644 app/common/dao/store/platformCommission/WeightValueLogDao.php create mode 100644 app/common/model/store/platformCommission/WeightValue.php create mode 100644 app/common/model/store/platformCommission/WeightValueLog.php create mode 100644 app/common/repositories/store/platformCommission/WeightValueLogRepository.php create mode 100644 app/common/repositories/store/platformCommission/WeightValueRepository.php create mode 100644 app/jobs/store/platformCommission/GiveWeightValueJob.php diff --git a/app/common/dao/store/platformCommission/WeightValueDao.php b/app/common/dao/store/platformCommission/WeightValueDao.php new file mode 100644 index 0000000..755e1ae --- /dev/null +++ b/app/common/dao/store/platformCommission/WeightValueDao.php @@ -0,0 +1,23 @@ +db($scope); } + + public static function batchUpdate(array $update, $whenField = 'id', $whereField = 'id', $raw = false){ + $when = []; + $ids = []; + foreach ($update as $sets) { + # 跳过没有更新主键的数据 + if (!isset($sets[$whenField])) { + continue; + } + $whenValue = $sets[$whenField]; + + foreach ($sets as $fieldName => $value) { + #主键不需要被更新 + if ($fieldName == $whenField) { + array_push($ids, $value); + continue; + }; + if ($raw) { + $when[$fieldName][] = "when {$whenValue} then {$value}"; + } else { + $when[$fieldName][] = "when '{$whenValue}' then '{$value}'"; + } + } + } + + # 没有更新的条件id + if (!$when) return false; + $query = self::whereIn($whereField, $ids); + # 组织sql + foreach ($when as $fieldName => &$item) { + $item = Db::raw("case $whenField " . implode(' ', $item) . ' end '); + } + + return $query->update($when); + } + } diff --git a/app/common/model/store/platformCommission/WeightValue.php b/app/common/model/store/platformCommission/WeightValue.php new file mode 100644 index 0000000..6ef14dc --- /dev/null +++ b/app/common/model/store/platformCommission/WeightValue.php @@ -0,0 +1,30 @@ +hasOne(User::class, 'uid', 'uid'); + } +} diff --git a/app/common/model/store/platformCommission/WeightValueLog.php b/app/common/model/store/platformCommission/WeightValueLog.php new file mode 100644 index 0000000..d6fcef9 --- /dev/null +++ b/app/common/model/store/platformCommission/WeightValueLog.php @@ -0,0 +1,33 @@ +hasOne(User::class, 'uid', 'uid'); + } +} diff --git a/app/common/repositories/store/platformCommission/WeightValueLogRepository.php b/app/common/repositories/store/platformCommission/WeightValueLogRepository.php new file mode 100644 index 0000000..8ece276 --- /dev/null +++ b/app/common/repositories/store/platformCommission/WeightValueLogRepository.php @@ -0,0 +1,23 @@ +dao = $dao; + } + + + + + +} diff --git a/app/common/repositories/store/platformCommission/WeightValueRepository.php b/app/common/repositories/store/platformCommission/WeightValueRepository.php new file mode 100644 index 0000000..9be412d --- /dev/null +++ b/app/common/repositories/store/platformCommission/WeightValueRepository.php @@ -0,0 +1,78 @@ +dao = $dao; + } + /** + * Common: 获取用户所有上级持有权重值信息 + * Author: wu-hui + * Time: 2023/12/26 18:46 + * @param $uid + * @param array $userList + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function getUserHoldList($uid,$userList = []){ + // 获取用户所有上级 + if(count($userList) <= 0){ + $userList = app()->make(UserRepository::class)->getAllSuperiorLv($uid); + unset($userList[$uid]);// 删除本人 + if(count($userList) <= 0) return [];// 不存在上级 + } + // 获取持有信息 + $holdList = $this->dao->getSearch([]) + ->field('id,uid,brokerage_level,quantity,CONCAT(uid, "_", brokerage_level) AS only_key') + ->whereIn('uid',array_column($userList,'uid')) + ->select() + ->toArray(); + // 判断:是否有不存在的用户持有信息 有则先添加 + $holdList = array_column($holdList,null,'only_key'); + $onlyKeyList = array_column($holdList,'only_key'); + $insertData = []; + // 判断:是否存在持有信息 不存在添加 + $currentLevelList = []; + foreach($userList as $userItem){ + $onlyKey = $userItem['uid'].'_'.$userItem['brokerage_level']; + if(!in_array($onlyKey,$onlyKeyList)){ + // 不存在 需要添加 + $defaultData = [ + 'uid' => $userItem['uid'], + 'brokerage_level' => $userItem['brokerage_level'], + 'quantity' => 0, + ]; + $insertData[] = $defaultData; + }else{ + $currentLevelList[] = $holdList[$onlyKey]; + } + } + if(count($insertData) > 0) { + $this->dao->insertAll($insertData); + + return $this->getUserHoldList($uid,$userList); + } + + return $currentLevelList; + } + + + + + + + +} diff --git a/app/common/repositories/user/UserRepository.php b/app/common/repositories/user/UserRepository.php index 1a2a4f0..effe7da 100644 --- a/app/common/repositories/user/UserRepository.php +++ b/app/common/repositories/user/UserRepository.php @@ -1511,4 +1511,33 @@ class UserRepository extends BaseRepository 'uid' => $user_info['uid'] ?? '-1' ] + $append_info; } + + + /** + * Common: 获取全部上级信息 + * Author: wu-hui + * Time: 2023/12/26 18:17 + * @param $uid + * @param array $superiorList + * @param int $level + * @return array|mixed + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + */ + public function getAllSuperiorLv($uid,array $superiorList = [],int $level = 1) { + // 获取用户上级列表 + $userInfo = $this->dao->getSearch([])->where('uid',$uid)->field(['uid','brokerage_level','spread_uid'])->find(); + $superiorList[$uid] = [ + 'level' => $level, + 'uid' => $userInfo['uid'], + 'spread_uid' => $userInfo['spread_uid'], + 'brokerage_level' => $userInfo['brokerage_level'] + ]; + if(empty($userInfo) || (int)$userInfo['spread_uid'] <= 0) return $superiorList; + return $this->getAllSuperiorLv($userInfo['spread_uid'], $superiorList, ++$level); + } + + + } diff --git a/app/controller/api/Auth.php b/app/controller/api/Auth.php index 0f4ef67..4e36b87 100644 --- a/app/controller/api/Auth.php +++ b/app/controller/api/Auth.php @@ -6,15 +6,19 @@ namespace app\controller\api; +use app\common\model\store\platformCommission\WeightValue; +use app\common\model\store\platformCommission\WeightValueLog; use app\common\repositories\store\order\StoreGroupOrderRepository; use app\common\repositories\store\order\StoreOrderRepository; use app\common\repositories\store\order\StoreRefundOrderRepository; +use app\common\repositories\store\platformCommission\WeightValueRepository; use app\common\repositories\system\notice\SystemNoticeConfigRepository; use app\common\repositories\user\UserBrokerageRepository; use app\common\repositories\user\UserRepository; use app\common\repositories\user\UserSignRepository; use app\common\repositories\wechat\RoutineQrcodeRepository; use app\common\repositories\wechat\WechatUserRepository; +use app\jobs\store\platformCommission\GiveWeightValueJob; use app\validate\api\ChangePasswordValidate; use app\validate\api\UserAuthValidate; use crmeb\basic\BaseController; @@ -63,8 +67,6 @@ class Auth extends BaseController // app()->make(WechatTemplateMessageService::class)->subscribeSendTemplate($data); // } - - // 分销商升级模拟 // $user = app()->make(UserRepository::class)->get(327706); // $res = app()->make(UserBrokerageRepository::class)->incV2($user, [ @@ -75,6 +77,13 @@ class Auth extends BaseController + // $userList = app()->make(WeightValueRepository::class)->getUserHoldList(327706); + // debug(['用户列表'=>$userList]); + // 订单支付成功 触发购买商品赠送上级权重值 + // Queue::push(GiveWeightValueJob::class,[ + // 'uid' => 327706, + // 'group_order_id' => 133 + // ]); } diff --git a/app/jobs/store/platformCommission/GiveWeightValueJob.php b/app/jobs/store/platformCommission/GiveWeightValueJob.php new file mode 100644 index 0000000..6a98f11 --- /dev/null +++ b/app/jobs/store/platformCommission/GiveWeightValueJob.php @@ -0,0 +1,77 @@ +make(WeightValueRepository::class)->getUserHoldList($data['uid']); + $orderIds = StoreOrder::where('group_order_id',$data['group_order_id'])->column('order_id');// 获取订单id + $productList = StoreOrderProduct::whereIn('order_id',$orderIds) + ->field('order_product_id,order_id,product_id,product_price') + ->select() + ->toArray();// 获取当前订单中所有商品 + $updateData = []; + $insertLogData = []; + foreach($userList as $userInfo){ + foreach($productList as $productInfo){ + // 判断:当前用户是否存在修改记录中 不存在添加修改信息 + if(empty($updateData[$userInfo['id']])){ + $updateData[$userInfo['id']] = [ + 'id' => $userInfo['id'], + 'quantity' => $userInfo['quantity'], + ]; + } + // 持有数量增加 + $changeFront = (float)$updateData[$userInfo['id']]['quantity']; + $updateData[$userInfo['id']]['quantity'] = (float)$productInfo['product_price'] + $changeFront; + // 记录变更记录 + $insertLogData[] = [ + 'uid' => $userInfo['uid'], + 'brokerage_level' => $userInfo['brokerage_level'], + 'product_id' => $productInfo['product_id'], + 'order_id' => $productInfo['order_id'], + 'order_product_id' => $productInfo['order_product_id'], + 'change_type' => 1, + 'change_quantity' => $productInfo['product_price'], + 'change_front' => $changeFront, + 'change_after' => (float)$updateData[$userInfo['id']]['quantity'], + 'remark' => '下线购买商品赠送', + ]; + } + } + // 数据结果处理 + if(count($insertLogData) > 0){ + $res = WeightValue::batchUpdate(array_values($updateData)); + $res = WeightValueLog::insertAll($insertLogData); + } + } + catch(\Exception $e){ + $data['error_msg'] = $e->getMessage(); + Log::info('下单赠送权重值 - 失败: '.var_export($data,1)); + } + $job->delete(); + } + public function failed($data){ + Log::info('下单赠送权重值 - 失败(failed): '.var_export($data,1)); + } +} diff --git a/app/listener/platformCommission/OrderPaySuccessEvent.php b/app/listener/platformCommission/OrderPaySuccessEvent.php index f710b6c..b999601 100644 --- a/app/listener/platformCommission/OrderPaySuccessEvent.php +++ b/app/listener/platformCommission/OrderPaySuccessEvent.php @@ -5,6 +5,7 @@ namespace app\listener\platformCommission; use app\common\model\store\platformCommission\Record; use app\common\model\system\merchant\Merchant; use app\common\repositories\store\platformCommission\RecordRepository; +use app\jobs\store\platformCommission\GiveWeightValueJob; use crmeb\jobs\UserBrokerageLevelJob; use think\facade\Log; use think\facade\Queue; @@ -23,6 +24,12 @@ class OrderPaySuccessEvent{ 'inc' => 0, 'group_order_id' => $groupOrder->group_order_id ]); + // 订单支付成功 触发购买商品赠送上级权重值 + Queue::push(GiveWeightValueJob::class,[ + 'uid' => $groupOrder->uid, + 'group_order_id' => $groupOrder->group_order_id + ]); + } /** * Common: 支付成功 - 平台抽成处理