ProfitsRepository.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace app\common\repositories\profits;
  3. use app\common\dao\profit\ZeroUserProfitDao;
  4. use app\common\dao\user\UserDao;
  5. use app\common\dao\user\ZeroUserLineDao;
  6. use app\common\enum\user\ZeroUserEnum;
  7. use app\common\model\profit\ZeroUserProfitsDetail;
  8. use app\common\utils\QueueLogger;
  9. use app\jobs\ZeroUserProfits;
  10. use think\db\exception\DataNotFoundException;
  11. use think\db\exception\DbException;
  12. use think\db\exception\ModelNotFoundException;
  13. use think\facade\Db;
  14. class ProfitsRepository
  15. {
  16. // 初级合伙人id
  17. const BASIC = 4;
  18. // 初级合伙人佣金比例
  19. const BASIC_PERCENT = 0.15;
  20. // 中级合伙人id
  21. const MIDDLE = 5;
  22. // 中级合伙人佣金比例
  23. const MIDDLE_PERCENT = 0.15;
  24. // 高级合伙人id
  25. const HIGH = 6;
  26. // 高级合伙人佣金比例
  27. const HIGH_PERCENT = 0.15;
  28. // 合伙人数据映射
  29. private $parents = [
  30. self::BASIC => self::BASIC_PERCENT,
  31. self::MIDDLE => self::MIDDLE_PERCENT,
  32. self::HIGH => self::HIGH_PERCENT,
  33. ];
  34. // 业绩数据映射(pv)
  35. private $pv = [
  36. 1180 => 700,
  37. 11800 => 7000,
  38. 10620 => 6300,
  39. 9440 => 5600,
  40. 2360 => 1400
  41. ];
  42. // 用户
  43. private $userDao;
  44. // 订单(老)
  45. // private $oldUserOfGoodsDao;
  46. // 订单(新)
  47. // private $storeOrder;
  48. public function __construct()
  49. {
  50. $this->userDao = new UserDao();
  51. // $this->oldUserOfGoodsDao = new OldUserOfGoodsDao();
  52. // $this->storeOrder = new StoreOrderDao();
  53. }
  54. /**
  55. * @param $param
  56. * @return string|true
  57. * @throws DataNotFoundException
  58. * @throws DbException
  59. * @throws ModelNotFoundException
  60. * 获取需要分红的0号线用户并加入队列
  61. */
  62. public function getZeroUser($param)
  63. {
  64. $zeroUserId = $param['zeroUserId'];
  65. // 0号线用户id是0的时候,获取全部0号线用户
  66. if ($zeroUserId == 0) {
  67. $zeroUserId = $this->getAllZeroUserIds();
  68. }
  69. if (is_string($zeroUserId)) {
  70. $zeroUserId = explode(',', $zeroUserId);
  71. }
  72. // 拼接队列需要的数据
  73. $queueData = [
  74. 'startTime' => $param['startTime'],
  75. 'endTime' => $param['endTime'],
  76. 'is_test' => $param['isTest'],
  77. 'amount' => $param['amount']
  78. ];
  79. // 记录日志
  80. $insertData = [
  81. 'start_time' => $param['startTime'],
  82. 'end_time' => $param['endTime'],
  83. 'is_test' => $param['isTest'],
  84. 'amount' => $param['amount']
  85. ];
  86. $dao = new ZeroUserProfitDao();
  87. // 加入队列
  88. try {
  89. foreach ($zeroUserId as $value) {
  90. $insertData['user_id'] = $value;
  91. $logId = $dao->insertGetId($insertData);
  92. $queueData['zeroUserId'] = $value;
  93. $queueData['logId'] = $logId;
  94. queue(ZeroUserProfits::class, $queueData, 0, 'zeroProfit');
  95. }
  96. return true;
  97. } catch (\Exception $e) {
  98. return $e->getMessage();
  99. }
  100. }
  101. /**
  102. * @throws DataNotFoundException
  103. * @throws ModelNotFoundException
  104. * @throws DbException
  105. * 执行分红
  106. */
  107. public function profits($param)
  108. {
  109. try {
  110. // 获取0号线用户的所有下级用户
  111. $userIds = $this->getAllUser($param['zeroUserId']);
  112. // 获取分红总额(业绩)
  113. $amount = $param['amount'];
  114. if ($param['amount'] == 0) {
  115. $amount = $this->getAmount($userIds);
  116. }
  117. // 计算分红
  118. $directUids = [];
  119. foreach ($this->parents as $key => $value) {
  120. // 获取初,中,高用户
  121. unset($groupData);
  122. $groupData = $this->getUserByGroup($userIds, $key);
  123. if (empty($groupData)) {
  124. QueueLogger::info('0号线合伙人分红:0号线用户:'.$param['zeroUserId'].',暂无'.$key.'级别用户' );
  125. continue;
  126. }
  127. // 获取初中高用户直推和团队业绩
  128. $profitDetail = $this->getDirect($groupData, $amount * $value);
  129. $directUids[$key] = $profitDetail;
  130. $this->recordLog($param, $amount, $profitDetail);
  131. }
  132. return true;
  133. } catch (\Exception $e) {
  134. return $e->getMessage();
  135. }
  136. }
  137. /**
  138. * @param $param
  139. * @return array
  140. * 获取要分红的用户ids
  141. */
  142. private function getUserIds($param): array
  143. {
  144. $res = $this->getAllZeroUser($param['zeroUserId']);
  145. return $res;
  146. }
  147. /**
  148. * @return array
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\DbException
  151. * @throws \think\db\exception\ModelNotFoundException
  152. * 获取所有下级用户
  153. */
  154. private function getAllUser($zeroUid)
  155. {
  156. $sql = "select getUserLevelId($zeroUid) as teamUids";
  157. $teamUids = Db::query($sql)[0]['teamUids'];
  158. // $resArr[$zeroUid] = $teamUids;
  159. return $teamUids;
  160. }
  161. /**
  162. * @return array
  163. * @throws DataNotFoundException
  164. * @throws DbException
  165. * @throws ModelNotFoundException
  166. * 获取全部0号线用户
  167. */
  168. private function getAllZeroUserIds()
  169. {
  170. $zeroUserDao = new ZeroUserLineDao();
  171. $zeroUserIds = $zeroUserDao->selectWhere([
  172. 'is_remove' => ZeroUserEnum::IS_REMOVE_MAP['YES']['code'],
  173. 'status' => ZeroUserEnum::STATUS_MAP['NORMAL']['code']
  174. ])->column('team_uid');
  175. if (empty($zeroUserIds)) {
  176. return [];
  177. }
  178. return $zeroUserIds;
  179. }
  180. /**
  181. * @param $userIds
  182. * @return int
  183. * 获取分红总额
  184. */
  185. private function getAmount($userIds): int
  186. {
  187. return 10000;
  188. }
  189. /**
  190. * @param $uids
  191. * @param $userGroup
  192. * @return array
  193. * @throws \think\db\exception\DataNotFoundException
  194. * @throws \think\db\exception\DbException
  195. * @throws \think\db\exception\ModelNotFoundException
  196. * 获取符合合伙人等级的用户
  197. */
  198. private function getUserByGroup($uid, $userGroup): array
  199. {
  200. $userDao = new UserDao();
  201. return $userDao->getUsersByGroup($uid, $userGroup);
  202. }
  203. /**
  204. * @param $data
  205. * @return array
  206. * @throws DataNotFoundException
  207. * @throws DbException
  208. * @throws ModelNotFoundException
  209. * 获取直推用户业绩
  210. */
  211. private function getDirect($data, $amount): array
  212. {
  213. $money = $amount / 2;
  214. $res = [];
  215. foreach ($data as $key => $value) {
  216. if (empty($value)) {
  217. unset($data[$key]);
  218. continue;
  219. }
  220. // 获取直推
  221. $directUids = $this->userDao->selectWhere(['level_id' => $value])->column('uid');
  222. // 获取直推用户pv
  223. $pvArr = $this->getPv($directUids);
  224. $all = 0;
  225. $all += array_sum($pvArr);
  226. $max = max($pvArr);
  227. $min = $all - $max;
  228. $res[$value]['all'] = $all;
  229. $res[$value]['min'] = $min;
  230. $res[$value]['max'] = $max;
  231. }
  232. $res = $this->countPrice($res, $money);
  233. return $res;
  234. }
  235. /**
  236. * @param $directUids
  237. * @param $pvConfig
  238. * @return array
  239. * 计算pv
  240. */
  241. private function getPv($directUids)
  242. {
  243. $res = [];
  244. foreach ($directUids as $uid) {
  245. // 获取直推用户的团队uid
  246. $teamUids = $this->getTeamUids($uid);
  247. // 去除自己
  248. $teamUids = array_slice($teamUids, 1);
  249. // 获取团队的pv
  250. $pvArr = $this->userDao->selectWhereIn('uid', $teamUids)->column('pv', 'uid');
  251. if (($pvArr)) {
  252. $all = array_sum($pvArr);
  253. $res[$uid] = $all;
  254. }
  255. }
  256. return $res;
  257. }
  258. /**
  259. * @param $uid
  260. * @return mixed
  261. * 获取团队所有人uids [1,2,3]
  262. */
  263. private function getTeamUids($uid): array
  264. {
  265. $sql = "select getUserLevelId($uid) as uids";
  266. return explode(',', Db::query($sql)[0]['uids']);
  267. }
  268. /**
  269. * @param $pvArr
  270. * @param $money
  271. * @return mixed
  272. * 计算分佣
  273. */
  274. private function countPrice($pvArr, $money)
  275. {
  276. $minAll = array_sum(array_column($pvArr, 'min'));
  277. $maxAll = array_sum(array_column($pvArr, 'all'));
  278. foreach ($pvArr as $k => $v) {
  279. $pvArr[$k]['minCount'] = floor($money * ($v['min'] / $minAll) * 100) / 100;
  280. $pvArr[$k]['maxCount'] = floor($money * ($v['all'] / $maxAll) * 100) / 100;
  281. }
  282. return $pvArr;
  283. }
  284. /**
  285. * @param $param
  286. * @param $amount
  287. * @param $detail
  288. * @return string|true
  289. * 记录日志
  290. */
  291. private function recordLog($param, $amount, $detail)
  292. {
  293. $logDao = new ZeroUserProfitDao();
  294. $detailDao = new ZeroUserProfitsDetail();
  295. // 记录分红明细日志,修改主表数据
  296. $updateData = [
  297. 'amount' => $amount,
  298. 'status' => 1
  299. ];
  300. foreach ($detail as $key => $value) {
  301. $insertData = [
  302. 'zero_user_profit_id' => $param['logId'],
  303. 'zero_user_id' => $param['zeroUserId'],
  304. 'user_id' => $key,
  305. 'amount' => $amount,
  306. 'all' => $value['all'],
  307. 'min' => $value['min'],
  308. 'max' => $value['max'],
  309. 'min_count' => $value['minCount'],
  310. 'max_count' => $value['maxCount'],
  311. ];
  312. try {
  313. Db::startTrans();
  314. $logDao->update($param['logId'], $updateData);
  315. $detailDao->create($insertData);
  316. Db::commit();
  317. } catch (\Exception $e) {
  318. Db::rollback();
  319. return $e->getMessage();
  320. }
  321. }
  322. return true;
  323. }
  324. }