UserBrokerageServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\user;
  12. use app\dao\user\UserBrokerageDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\ApiException;
  15. /**
  16. * 用户佣金
  17. * Class UserBrokerageServices
  18. * @package app\services\user
  19. * @method getUserFrozenPrice(int $uid) 获取某个用户冻结的佣金
  20. */
  21. class UserBrokerageServices extends BaseServices
  22. {
  23. /**
  24. * 用户记录模板
  25. * @var array[]
  26. */
  27. protected $incomeData = [
  28. 'get_self_member_brokerage' => [
  29. 'title' => '获得自购付费会员佣金',
  30. 'type' => 'self_member_brokerage',
  31. 'mark' => '您成功消费{%pay_price%}元,奖励自购佣金{%number%}',
  32. 'status' => 1,
  33. 'pm' => 1
  34. ],
  35. 'get_member_brokerage' => [
  36. 'title' => '获得下级购买付费会员佣金',
  37. 'type' => 'one_member_brokerage',
  38. 'mark' => '{%nickname%}成功消费{%pay_price%}元,奖励推广佣金{%number%}',
  39. 'status' => 1,
  40. 'pm' => 1
  41. ],
  42. 'get_two_member_brokerage' => [
  43. 'title' => '获得二级购买付费会员佣金',
  44. 'type' => 'two_member_brokerage',
  45. 'mark' => '二级推广人{%nickname%}成功消费{%pay_price%}元,奖励推广佣金{%number%}',
  46. 'status' => 1,
  47. 'pm' => 1
  48. ],
  49. 'get_self_brokerage' => [
  50. 'title' => '获得自购订单佣金',
  51. 'type' => 'self_brokerage',
  52. 'mark' => '您成功消费{%pay_price%}元,奖励自购佣金{%number%}',
  53. 'status' => 1,
  54. 'pm' => 1
  55. ],
  56. 'get_brokerage' => [
  57. 'title' => '获得下级推广订单佣金',
  58. 'type' => 'one_brokerage',
  59. 'mark' => '{%nickname%}成功消费{%pay_price%}元,奖励推广佣金{%number%}',
  60. 'status' => 1,
  61. 'pm' => 1
  62. ],
  63. 'get_two_brokerage' => [
  64. 'title' => '获得二级推广订单佣金',
  65. 'type' => 'two_brokerage',
  66. 'mark' => '二级推广人{%nickname%}成功消费{%pay_price%}元,奖励推广佣金{%number%}',
  67. 'status' => 1,
  68. 'pm' => 1
  69. ],
  70. 'get_user_brokerage' => [
  71. 'title' => '获得推广用户佣金',
  72. 'type' => 'brokerage_user',
  73. 'mark' => '成功推广用户:{%nickname%},奖励推广佣金{%number%}',
  74. 'status' => 1,
  75. 'pm' => 1
  76. ],
  77. 'extract' => [
  78. 'title' => '佣金提现',
  79. 'type' => 'extract',
  80. 'mark' => '{%mark%}',
  81. 'status' => 1,
  82. 'pm' => 0
  83. ],
  84. 'extract_fail' => [
  85. 'title' => '提现失败',
  86. 'type' => 'extract_fail',
  87. 'mark' => '提现失败,退回佣金{%number%}元',
  88. 'status' => 1,
  89. 'pm' => 1
  90. ],
  91. 'brokerage_to_nowMoney' => [
  92. 'title' => '佣金提现到余额',
  93. 'type' => 'extract_money',
  94. 'mark' => '佣金提现到余额{%number%}元',
  95. 'status' => 1,
  96. 'pm' => 0
  97. ],
  98. 'brokerage_refund' => [
  99. 'title' => '退款退佣金',
  100. 'type' => 'refund',
  101. 'mark' => '订单退款扣除佣金{%number%}元',
  102. 'status' => 1,
  103. 'pm' => 0
  104. ],
  105. 'get_staff_brokerage' => [
  106. 'title' => '获得员工推广订单佣金',
  107. 'type' => 'staff_brokerage',
  108. 'mark' => '{%nickname%}成功消费{%pay_price%}元,奖励推广佣金{%number%}',
  109. 'status' => 1,
  110. 'pm' => 1
  111. ],
  112. 'get_agent_brokerage' => [
  113. 'title' => '获得代理推广订单佣金',
  114. 'type' => 'agent_brokerage',
  115. 'mark' => '{%nickname%}成功消费{%pay_price%}元,奖励推广佣金{%number%}',
  116. 'status' => 1,
  117. 'pm' => 1
  118. ],
  119. 'get_division_brokerage' => [
  120. 'title' => '获得事业部推广订单佣金',
  121. 'type' => 'division_brokerage',
  122. 'mark' => '{%nickname%}成功消费{%pay_price%}元,奖励推广佣金{%number%}',
  123. 'status' => 1,
  124. 'pm' => 1
  125. ],
  126. 'get_pink_master_brokerage' => [
  127. 'title' => '获得拼团团长佣金',
  128. 'type' => 'pink_master_brokerage',
  129. 'mark' => '开团成功,奖励团长佣金{%number%}',
  130. 'status' => 1,
  131. 'pm' => 1
  132. ],
  133. ];
  134. /**
  135. * UserBrokerageServices constructor.
  136. * @param UserBrokerageDao $dao
  137. */
  138. public function __construct(UserBrokerageDao $dao)
  139. {
  140. $this->dao = $dao;
  141. }
  142. /**
  143. * 写入佣金记录
  144. * @param string $type 写入类型
  145. * @param int $uid
  146. * @param int|string|array $number
  147. * @param int|string $balance
  148. * @param $linkId
  149. * @return bool|mixed
  150. */
  151. public function income(string $type, int $uid, $number, $balance, $linkId)
  152. {
  153. $data = $this->incomeData[$type] ?? null;
  154. if (!$data) {
  155. return true;
  156. }
  157. $data['uid'] = $uid;
  158. $data['balance'] = $balance ?? 0;
  159. $data['link_id'] = $linkId;
  160. if (is_array($number)) {
  161. $key = array_keys($number);
  162. $key = array_map(function ($item) {
  163. return '{%' . $item . '%}';
  164. }, $key);
  165. $value = array_values($number);
  166. $data['number'] = $number['number'] ?? 0;
  167. $data['frozen_time'] = $number['frozen_time'] ?? 0;
  168. $data['mark'] = str_replace($key, $value, $data['mark']);
  169. } else {
  170. $data['number'] = $number;
  171. $data['mark'] = str_replace(['{%number%}'], $number, $data['mark']);
  172. }
  173. $data['add_time'] = time();
  174. return $this->dao->save($data);
  175. }
  176. /**
  177. * 某个用户佣金总和
  178. * @param int $uid
  179. * @param array|string[] $type
  180. * @param string $time
  181. * @return float
  182. * @throws \ReflectionException
  183. */
  184. public function getUserBrokerageSum(int $uid, array $type = ['one_brokerage', 'two_brokerage', 'brokerage_user'], $time = '')
  185. {
  186. $where = ['uid' => $uid];
  187. if ($type) $where['type'] = $type;
  188. if ($time) $where['time'] = $time;
  189. return $this->dao->sum($where, 'number', true);
  190. }
  191. /**
  192. * 退佣金
  193. * @param $order
  194. * @return bool
  195. * @throws \think\db\exception\DataNotFoundException
  196. * @throws \think\db\exception\DbException
  197. * @throws \think\db\exception\ModelNotFoundException
  198. */
  199. public function orderRefundBrokerageBack($order)
  200. {
  201. $id = (int)$order['id'];
  202. $where = [
  203. 'uid' => [$order['spread_uid'], $order['spread_two_uid'], $order['staff_id'], $order['agent_id'], $order['division_id']],
  204. 'type' => ['self_brokerage', 'one_brokerage', 'two_brokerage', 'staff_brokerage', 'agent_brokerage', 'division_brokerage', 'pink_master_brokerage'],
  205. 'link_id' => $id,
  206. 'pm' => 1
  207. ];
  208. $brokerageList = $this->dao->getUserBrokerageList($where);
  209. //子订单
  210. if (!$brokerageList && $order['pid']) {
  211. $where['link_id'] = $order['pid'];
  212. $p_brokerageList = $this->dao->getUserBrokerageList($where);
  213. //主订单已分佣 子订单按订单拆分后计算结果回退
  214. if ($p_brokerageList) {
  215. $brokerageList = [
  216. ['uid' => $order['spread_uid'], 'number' => $order['one_brokerage']],
  217. ['uid' => $order['spread_two_uid'], 'number' => $order['two_brokerage']],
  218. ];
  219. }
  220. }
  221. $res = true;
  222. if ($brokerageList) {
  223. /** @var UserServices $userServices */
  224. $userServices = app()->make(UserServices::class);
  225. $brokerages = $userServices->getColumn([['uid', 'in', array_column($brokerageList, 'uid')]], 'brokerage_price', 'uid');
  226. $brokerageData = [];
  227. foreach ($brokerageList as $item) {
  228. if (!$item['uid']) continue;
  229. $usermoney = $brokerages[$item['uid']] ?? 0;
  230. if ($item['number'] > $usermoney) {
  231. $item['number'] = $usermoney;
  232. }
  233. $res = $res && $userServices->bcDec($item['uid'], 'brokerage_price', (string)$item['number'], 'uid');
  234. $brokerageData[] = [
  235. 'title' => '退款退佣金',
  236. 'uid' => $item['uid'],
  237. 'pm' => 0,
  238. 'add_time' => time(),
  239. 'type' => 'refund',
  240. 'number' => $item['number'],
  241. 'link_id' => $id,
  242. 'balance' => bcsub((string)$usermoney, (string)$item['number'], 2),
  243. 'mark' => '订单退款扣除佣金' . floatval($item['number']) . '元'
  244. ];
  245. }
  246. if ($brokerageData) {
  247. $res = $res && $this->dao->saveAll($brokerageData);
  248. }
  249. //修改佣金冻结时间
  250. $this->dao->update($where, ['frozen_time' => 0]);
  251. }
  252. return $res;
  253. }
  254. /**
  255. * 佣金排行
  256. * @param string $time
  257. * @return array
  258. * @throws \think\db\exception\DataNotFoundException
  259. * @throws \think\db\exception\DbException
  260. * @throws \think\db\exception\ModelNotFoundException
  261. */
  262. public function brokerageRankList(string $time = 'week')
  263. {
  264. $where = ['pm' => 1];
  265. if ($time) {
  266. $where['time'] = $time;
  267. }
  268. [$page, $limit] = $this->getPageValue();
  269. $list = $this->dao->brokerageRankList($where, $page, $limit);
  270. foreach ($list as $key => &$item) {
  271. if (!isset($item['user']) || !$item['user'] || $item['brokerage_price'] <= 0) {
  272. unset($list[$key]);
  273. continue;
  274. }
  275. $item['nickname'] = $item['user']['nickname'] ?? '';
  276. $item['avatar'] = $item['user']['avatar'] ?? '';
  277. if ($item['brokerage_price'] == '0.00' || $item['brokerage_price'] == 0 || !$item['brokerage_price']) {
  278. unset($list[$key]);
  279. }
  280. unset($item['user']);
  281. }
  282. return array_merge($list);
  283. }
  284. /**
  285. * 获取用户排名
  286. * @param int $uid
  287. * @param string $time
  288. * @return false|int|string
  289. * @throws \think\db\exception\DataNotFoundException
  290. * @throws \think\db\exception\DbException
  291. * @throws \think\db\exception\ModelNotFoundException
  292. */
  293. public function getUserBrokerageRank(int $uid, string $time = 'week')
  294. {
  295. $where = ['pm' => 1];
  296. if ($time) {
  297. $where['time'] = $time;
  298. }
  299. $list = $this->dao->brokerageRankList($where);
  300. foreach ($list as $key => &$item) {
  301. if (!isset($item['user']) || !$item['user'] || $item['brokerage_price'] <= 0) {
  302. unset($list[$key]);
  303. }
  304. }
  305. $position_tmp_one = array_column($list, 'uid');
  306. $position_tmp_two = array_column($list, 'brokerage_price', 'uid');
  307. if (!in_array($uid, $position_tmp_one)) {
  308. $position = 0;
  309. } else {
  310. if ($position_tmp_two[$uid] == 0.00) {
  311. $position = 0;
  312. } else {
  313. $position = array_search($uid, $position_tmp_one) + 1;
  314. }
  315. }
  316. return $position;
  317. }
  318. /**
  319. * 推广数据 昨天的佣金 累计提现金额 当前佣金
  320. * @param int $uid
  321. * @return mixed
  322. * @throws \think\db\exception\DataNotFoundException
  323. * @throws \think\db\exception\DbException
  324. * @throws \think\db\exception\ModelNotFoundException
  325. */
  326. public function commission(int $uid)
  327. {
  328. /** @var UserServices $userServices */
  329. $userServices = app()->make(UserServices::class);
  330. if (!$userServices->getUserInfo($uid)) {
  331. throw new ApiException(100026);
  332. }
  333. /** @var UserExtractServices $userExtract */
  334. $userExtract = app()->make(UserExtractServices::class);
  335. $data = [];
  336. $data['uid'] = $uid;
  337. $data['pm'] = 1;
  338. $data['commissionSum'] = $this->getUsersBokerageSum($data);
  339. $extract_fail = $this->dao->sum(['uid' => $uid, 'pm' => 1, 'type' => 'extract_fail'], 'number');
  340. $data['commissionSum'] = bcadd($data['commissionSum'], $extract_fail, 2);
  341. $data['pm'] = 0;
  342. $data['commissionRefund'] = $this->getUsersBokerageSum($data);
  343. $data['commissionCount'] = $data['commissionSum'] > $data['commissionRefund'] ? bcsub((string)$data['commissionSum'], (string)$data['commissionRefund'], 2) : 0.00;
  344. $data['lastDayCount'] = $this->getUsersBokerageSum($data, 'yesterday');//昨天的佣金
  345. $data['extractCount'] = $userExtract->getUserExtract($uid);//累计提现金额
  346. return $data;
  347. }
  348. /**
  349. * 计算佣金
  350. * @param array $where
  351. * @param int $time
  352. * @return mixed
  353. * @throws \ReflectionException
  354. */
  355. public function getUsersBokerageSum(array $where, $time = 0)
  356. {
  357. $where_data = [
  358. 'status' => 1,
  359. 'pm' => $where['pm'] ?? '',
  360. 'uid' => $where['uid'] ?? '',
  361. 'time' => $where['time'] ?? 0
  362. ];
  363. if ($time) $where_data['time'] = $time;
  364. return $this->dao->getBrokerageSumColumn($where_data);
  365. }
  366. /**
  367. * 佣金明细
  368. * @param $uid
  369. * @param $type
  370. * @return array
  371. * @throws \ReflectionException
  372. * @throws \think\db\exception\DataNotFoundException
  373. * @throws \think\db\exception\DbException
  374. * @throws \think\db\exception\ModelNotFoundException
  375. */
  376. public function getBrokerageList($uid, $type)
  377. {
  378. $where = [];
  379. $where['uid'] = $uid;
  380. [$page, $limit] = $this->getPageValue();
  381. if ($type == 4) {
  382. $where['type'] = ['extract', 'extract_money', 'extract_fail'];
  383. }
  384. /** @var UserExtractServices $userExtractService */
  385. $userExtractService = app()->make(UserExtractServices::class);
  386. $userExtract = $userExtractService->getColumn(['uid' => $uid], 'fail_msg,extract_type', 'id');
  387. $list = $this->dao->getList($where, '*', $page, $limit);
  388. $count = $this->dao->count($where);
  389. $times = [];
  390. if ($list) {
  391. foreach ($list as &$item) {
  392. $item['time'] = $item['time_key'] = $item['add_time'] ? date('Y-m', (int)$item['add_time']) : '';
  393. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  394. $item['fail_msg'] = $item['type'] == 'extract_fail' ? $userExtract[$item['link_id']]['fail_msg'] : '';
  395. if ($type == 4) {
  396. $extract_type = $userExtract[$item['link_id']]['extract_type'] ?? '';
  397. if ($extract_type == 'alipay') {
  398. $item['extract_type'] = '支付宝';
  399. } elseif ($extract_type == 'weixin') {
  400. $item['extract_type'] = '微信';
  401. } elseif ($extract_type == 'bank') {
  402. $item['extract_type'] = '银行卡';
  403. } else {
  404. $item['extract_type'] = '余额';
  405. }
  406. } else {
  407. $item['extract_type'] = '';
  408. }
  409. }
  410. $times = array_merge(array_unique(array_column($list, 'time_key')));
  411. }
  412. return ['list' => $list, 'time' => $times, 'count' => $count];
  413. }
  414. /**
  415. * 前端佣金排行页面数据
  416. * @param int $uid
  417. * @param $type
  418. * @return array
  419. * @throws \think\db\exception\DataNotFoundException
  420. * @throws \think\db\exception\DbException
  421. * @throws \think\db\exception\ModelNotFoundException
  422. */
  423. public function brokerageRank(int $uid, $type)
  424. {
  425. /** @var UserServices $userService */
  426. $userService = app()->make(UserServices::class);
  427. if (!$userService->getUserInfo($uid)) {
  428. throw new ApiException(100026);
  429. }
  430. return [
  431. 'rank' => $this->brokerageRankList($type),
  432. 'position' => $this->getUserBrokerageRank($uid, $type)
  433. ];
  434. }
  435. }