self::BASIC_PERCENT, self::MIDDLE => self::MIDDLE_PERCENT, self::HIGH => self::HIGH_PERCENT, ]; // 业绩数据映射(pv) private $pv = [ 1180 => 700, 11800 => 7000, 10620 => 6300, 9440 => 5600, 2360 => 1400 ]; // 用户 private $userDao; // 订单(老) // private $oldUserOfGoodsDao; // 订单(新) // private $storeOrder; public function __construct() { $this->userDao = new UserDao(); // $this->oldUserOfGoodsDao = new OldUserOfGoodsDao(); // $this->storeOrder = new StoreOrderDao(); } /** * @param $param * @return string|true * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * 获取需要分红的0号线用户并加入队列 */ public function getZeroUser($param) { $zeroUserId = $param['zeroUserId']; // 0号线用户id是0的时候,获取全部0号线用户 if ($zeroUserId == 0) { $zeroUserId = $this->getAllZeroUserIds(); } if (is_string($zeroUserId)) { $zeroUserId = explode(',', $zeroUserId); } // 拼接队列需要的数据 $queueData = [ 'startTime' => $param['startTime'], 'endTime' => $param['endTime'], 'is_test' => $param['isTest'], 'amount' => $param['amount'] ]; // 记录日志 $insertData = [ 'start_time' => $param['startTime'], 'end_time' => $param['endTime'], 'is_test' => $param['isTest'], 'amount' => $param['amount'] ]; $dao = new ZeroUserProfitDao(); // 加入队列 try { foreach ($zeroUserId as $value) { $insertData['user_id'] = $value; $logId = $dao->insertGetId($insertData); $queueData['zeroUserId'] = $value; $queueData['logId'] = $logId; queue(ZeroUserProfits::class, $queueData, 0, 'zeroProfit'); } return true; } catch (\Exception $e) { return $e->getMessage(); } } /** * @throws DataNotFoundException * @throws ModelNotFoundException * @throws DbException * 执行分红 */ public function profits($param) { try { // 获取0号线用户的所有下级用户 $userIds = $this->getAllUser($param['zeroUserId']); // 获取分红总额(业绩) $amount = $param['amount']; if ($param['amount'] == 0) { $amount = $this->getAmount($userIds); } // 计算分红 $directUids = []; foreach ($this->parents as $key => $value) { // 获取初,中,高用户 unset($groupData); $groupData = $this->getUserByGroup($userIds, $key); if (empty($groupData)) { QueueLogger::info('0号线合伙人分红:0号线用户:' . $param['zeroUserId'] . ',暂无' . $key . '级别用户'); continue; } // 获取初中高用户直推和团队业绩 $profitDetail = $this->getDirect($groupData, $amount * $value); $directUids[$key] = $profitDetail; $this->recordLog($param, $amount, $profitDetail); } return true; } catch (\Exception $e) { return $e->getMessage(); } } /** * @param $param * @return array * 获取要分红的用户ids */ private function getUserIds($param): array { $res = $this->getAllZeroUser($param['zeroUserId']); return $res; } /** * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * 获取所有下级用户 */ private function getAllUser($zeroUid) { $sql = "select getUserLevelId($zeroUid) as teamUids"; $teamUids = Db::query($sql)[0]['teamUids']; // $resArr[$zeroUid] = $teamUids; return $teamUids; } /** * @return array * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * 获取全部0号线用户 */ private function getAllZeroUserIds() { $zeroUserDao = new ZeroUserLineDao(); $zeroUserIds = $zeroUserDao->selectWhere([ 'is_remove' => ZeroUserEnum::IS_REMOVE_MAP['YES']['code'], 'status' => ZeroUserEnum::STATUS_MAP['NORMAL']['code'] ])->column('team_uid'); if (empty($zeroUserIds)) { return []; } return $zeroUserIds; } /** * @param $userIds * @return int * 获取分红总额 */ private function getAmount($userIds): int { return 10000; } /** * @param $uids * @param $userGroup * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * 获取符合合伙人等级的用户 */ private function getUserByGroup($uid, $userGroup): array { $userDao = new UserDao(); return $userDao->getUsersByGroup($uid, $userGroup); } /** * @param $data * @return array * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * 获取直推用户业绩 */ private function getDirect($data, $amount): array { $money = $amount / 2; $res = []; foreach ($data as $key => $value) { if (empty($value)) { unset($data[$key]); continue; } // 获取直推 $directUids = $this->userDao->selectWhere(['level_id' => $value])->column('uid'); // 获取直推用户pv $pvArr = $this->getPv($directUids); $all = 0; $all += array_sum($pvArr); $max = max($pvArr); $min = $all - $max; $res[$value]['all'] = $all; $res[$value]['min'] = $min; $res[$value]['max'] = $max; } $res = $this->countPrice($res, $money); return $res; } /** * @param $directUids * @param $pvConfig * @return array * 计算pv */ private function getPv($directUids) { $res = []; foreach ($directUids as $uid) { // 获取直推用户的团队uid $teamUids = $this->getTeamUids($uid); // 去除自己 $teamUids = array_slice($teamUids, 1); // 获取团队的pv $pvArr = $this->userDao->selectWhereIn('uid', $teamUids)->column('pv', 'uid'); if (($pvArr)) { $all = array_sum($pvArr); $res[$uid] = $all; } } return $res; } /** * @param $uid * @return mixed * 获取团队所有人uids [1,2,3] */ private function getTeamUids($uid): array { $sql = "select getUserLevelId($uid) as uids"; return explode(',', Db::query($sql)[0]['uids']); } /** * @param $pvArr * @param $money * @return mixed * 计算分佣 */ private function countPrice($pvArr, $money) { $minAll = array_sum(array_column($pvArr, 'min')); $maxAll = array_sum(array_column($pvArr, 'all')); foreach ($pvArr as $k => $v) { $pvArr[$k]['minCount'] = floor($money * ($v['min'] / $minAll) * 100) / 100; $pvArr[$k]['maxCount'] = floor($money * ($v['all'] / $maxAll) * 100) / 100; } return $pvArr; } /** * @param $param * @param $amount * @param $detail * @return string|true * 记录日志 */ private function recordLog($param, $amount, $detail) { $logDao = new ZeroUserProfitDao(); $detailDao = new ZeroUserProfitsDetail(); // 记录分红明细日志,修改主表数据 $updateData = [ 'amount' => $amount, 'status' => 1 ]; foreach ($detail as $key => $value) { $insertData = [ 'zero_user_profit_id' => $param['logId'], 'zero_user_id' => $param['zeroUserId'], 'user_id' => $key, 'amount' => $amount, 'all' => $value['all'], 'min' => $value['min'], 'max' => $value['max'], 'min_count' => $value['minCount'], 'max_count' => $value['maxCount'], ]; try { Db::startTrans(); $logDao->update($param['logId'], $updateData); $detailDao->create($insertData); Db::commit(); } catch (\Exception $e) { Db::rollback(); return $e->getMessage(); } } return true; } // 执行分红 public function executeProfit($param) { $detailDao = new ZeroUserProfitsDetail(); // 获取分红信息 $list = $detailDao->where('zero_user_profit_id', $param['zeroUserProfitId'])->select()->toArray(); if (empty($list)) { return []; } // $userIds = array_column($list, 'user_id'); // // $userList = User::select($userIds); // dd($list); foreach ($list as $key => $value) { $allAward = bcadd($value['min_count'], $value['max_count']); //90%给佣金,10给复购金 $yj_90 = floor($allAward * 0.9 * 100) / 100; $fg_10 = floor($allAward * 0.1 * 100) / 100; $userModel = $this->userDao->get($value['user_id']); $userModel->total_amount_old = $userModel->total_amount_old + $yj_90; $userModel->amount_old = $userModel->amount_old + $yj_90; $userModel->brokerage_price = $userModel->brokerage_price + $yj_90; $userModel->fugou = $userModel->fugou + $fg_10; $userModel->save(); } die; // 插入执行分红队列 // foreach ($userList as $value) { // $value->amount_old = Db::raw('amount_old + 1'); // $value->save(); // } $update = []; foreach ($userList as $value) { $amountOld = $value->amount_old + 1; $update[] = [ 'uid' => $value->uid, 'amount_old' => $amountOld ]; } $this->userDao->saveAll($update); die; } /** * @param $userId * @param $type * @param $changeStatus * @param $money * @param $routineLogOfKeyId * @param $remark * @param $isAdmin * @param $createTime * @param $alterMoney * @return int|string * @author 史晨 * @date 2025/9/18 14:16 * 记录用户余额日志 */ private function recordOldUserLog($userId, $type, $changeStatus, $money, $routineLogOfKeyId, $remark = "", $isAdmin = 1, $alterMoney = 0) { $insertData = [ 'user_id' => $userId, 'type' => $type, 'change_status' => $changeStatus, 'money' => $money, 'routine_log_of_key_id' => $routineLogOfKeyId, 'remark' => $remark, 'is_admin' => $isAdmin, 'create_time' => time(), 'alter_money' => $alterMoney ]; $dao = new OldUserLog(); return $dao->insert($insertData); } private function recordUserBillLog() { $bill = [ 'uid' => $uid, 'link_id' => 0,//关联订单id 'pm' => 1,//0:支出,1:获得 'title' => $name,//账单标题 'category' => 'now_money',//明细种类 'type' => 'commission',//明细类型 'number' => $num,//明细数字 'balance' => 0,//剩余 'mark' => $mark,//备注 'create_time' => date('Y-m-d H:i:s'),//添加时间 'status' => 1,//0待确定,1有效,-1无效 'commission_type' => $comm,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费 //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金 //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’ 'order_sn' => 0,//订单号 'tripartite' => 0,// 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上 'mer_id' => 0,//商户id 'source' => 0,//1线下 0线上 'order_type' => 1,//1自营 2 第三方 订单类型 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金 'gzc' => 3,//养老金是否已进入公证处log表0:未进入。1:已进入 'take_time' => time(),//结算时间 'district_id' => '',// 'street_id' => '',// 'month' => '',//月份 ]; } private function fugouUserLog() { $bill = [ 'uid' => $uid, 'number' => $num,//数量 'status' => $status,//复购金状态1-有效 0-失效 -1待确认 'mark' => "复购金",//备注 'desc' => "10%复购金",//描述 'order_id' => $order_id,//订单id 'surplus' => 0,//剩余 'order_type' => 11,//关联订单ID 'type' => $type_inc_des,//复购金类型 :1赠送,2消耗 'settlement_time' => date('Y-m-d H:i:s'),//添加时间, 'addtime' => time(), ]; } }