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)) { // 获取初中高用户直推和团队业绩 $profitDetail = $this->getDirect($groupData, $amount * $value); $directUids[$key] = $profitDetail; $this->recordLog($param, $amount, $profitDetail); } } return true; } catch (\Exception $e) { return false; } } /** * @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 []; } // // 循环获取0号线用户团队 // $resArr = []; // foreach ($zeroUserIds as $zeroUserId) { // $zeroUid = $zeroUserId['team_uid']; // $sql = "select getUserLevelId($zeroUid) as teamUids"; // $teamUids = Db::query($sql)[0]['teamUids']; // $resArr[$zeroUid] = $teamUids; // } 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; } }