添加:下单赠送权重值

This commit is contained in:
wuhui_zzw 2023-12-27 10:34:27 +08:00
parent 80859f8b22
commit ef516f06a8
11 changed files with 371 additions and 2 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace app\common\dao\store\platformCommission;
use app\common\dao\BaseDao;
use app\common\model\store\platformCommission\WeightValue;
class WeightValueDao extends BaseDao{
protected function getModel():string{
return WeightValue::class;
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace app\common\dao\store\platformCommission;
use app\common\dao\BaseDao;
use app\common\model\store\platformCommission\WeightValueLog;
class WeightValueLogDao extends BaseDao{
protected function getModel():string{
return WeightValueLog::class;
}
}

View File

@ -7,6 +7,7 @@ namespace app\common\model;
use think\db\BaseQuery;
use think\facade\Db;
use think\Model;
/**
@ -63,4 +64,40 @@ abstract class BaseModel extends Model
return self::getInstance()->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);
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace app\common\model\store\platformCommission;
use app\common\model\BaseModel;
use app\common\model\user\User;
class WeightValue extends BaseModel{
public static function tablePk():string{
return 'id';
}
public static function tableName():string{
return 'platform_commission_weight_value';
}
/**
* Common: 关联用户表
* Author: wu-hui
* Time: 2023/12/26 17:12
* @return \think\model\relation\HasOne
*/
public function user(){
return $this->hasOne(User::class, 'uid', 'uid');
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace app\common\model\store\platformCommission;
use app\common\model\BaseModel;
use app\common\model\user\User;
class WeightValueLog extends BaseModel{
public static function tablePk():string{
return 'id';
}
public static function tableName():string{
return 'platform_commission_weight_value_log';
}
/**
* Common: 关联用户表
* Author: wu-hui
* Time: 2023/12/26 17:13
* @return \think\model\relation\HasOne
*/
public function user(){
return $this->hasOne(User::class, 'uid', 'uid');
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace app\common\repositories\store\platformCommission;
use app\common\dao\store\platformCommission\WeightValueLogDao;
use app\common\repositories\BaseRepository;
class WeightValueLogRepository extends BaseRepository{
protected $dao;
public function __construct(WeightValueLogDao $dao){
$this->dao = $dao;
}
}

View File

@ -0,0 +1,78 @@
<?php
namespace app\common\repositories\store\platformCommission;
use app\common\dao\store\platformCommission\WeightValueDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\user\UserRepository;
class WeightValueRepository extends BaseRepository{
protected $dao;
public function __construct(WeightValueDao $dao){
$this->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;
}
}

View File

@ -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);
}
}

View File

@ -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
// ]);
}

View File

@ -0,0 +1,77 @@
<?php
namespace app\jobs\store\platformCommission;
use app\common\model\store\order\StoreOrder;
use app\common\model\store\order\StoreOrderProduct;
use app\common\model\store\platformCommission\WeightValue;
use app\common\model\store\platformCommission\WeightValueLog;
use app\common\repositories\store\platformCommission\WeightValueRepository;
use crmeb\interfaces\JobInterface;
use think\facade\Log;
/**
* Common: 下单赠送权重值
* Author: wu-hui
* Time: 2023/12/27 10:30
* Class GiveWeightValueJob
* @package app\jobs\store\platformCommission
*/
class GiveWeightValueJob implements JobInterface{
public function fire($job,$data){
try{
Log::info('下单赠送权重值 - 开始处理: '.var_export($data,1));
// 获取所有用户的持有信息
$userList = app()->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));
}
}

View File

@ -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: 支付成功 - 平台抽成处理