| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- <?php
- namespace app\common\repositories\profits;
- use app\common\dao\profit\ZeroUserProfitDao;
- use app\common\dao\store\order\OldUserOfGoodsDao;
- use app\common\dao\store\order\StoreOrderDao;
- use app\common\dao\user\UserDao;
- use app\common\dao\user\ZeroUserLineDao;
- use app\common\enum\user\ZeroUserEnum;
- use app\common\model\profit\ZeroUserProfitsDetail;
- use app\jobs\ZeroUserProfits;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\facade\Db;
- class ProfitsRepository
- {
- // 初级合伙人id
- const BASIC = 4;
- // 初级合伙人佣金比例
- const BASIC_PERCENT = 0.15;
- // 中级合伙人id
- const MIDDLE = 5;
- // 中级合伙人佣金比例
- const MIDDLE_PERCENT = 0.15;
- // 高级合伙人id
- const HIGH = 6;
- // 高级合伙人佣金比例
- const HIGH_PERCENT = 0.15;
- // 合伙人数据映射
- private $parents = [
- self::BASIC => 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;
- }
- }
|