UserBillServices.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\services\BaseServices;
  14. use app\dao\user\UserBillDao;
  15. use app\services\user\level\UserLevelServices;
  16. use think\Exception;
  17. use think\exception\ValidateException;
  18. use crmeb\services\CacheService;
  19. use think\facade\Log;
  20. /**
  21. *
  22. * Class UserBillServices
  23. * @package app\services\user
  24. * @mixin UserBillDao
  25. */
  26. class UserBillServices extends BaseServices
  27. {
  28. /**
  29. * 用户记录模板
  30. * @var array[]
  31. */
  32. protected $incomeData = [
  33. 'pay_give_integral' => [
  34. 'title' => '购买商品赠送消费分',
  35. 'category' => 'integral',
  36. 'type' => 'gain',
  37. 'mark' => '购买商品赠送{%num%}消费分',
  38. 'status' => 1,
  39. 'pm' => 1
  40. ],
  41. 'order_give_integral' => [
  42. 'title' => '下单赠送消费分',
  43. 'category' => 'integral',
  44. 'type' => 'gain',
  45. 'mark' => '下单赠送{%num%}消费分',
  46. 'status' => 1,
  47. 'pm' => 1
  48. ],
  49. 'order_promotions_give_integral' => [
  50. 'title' => '下单优惠活动赠送消费分',
  51. 'category' => 'integral',
  52. 'type' => 'gain',
  53. 'mark' => '下单优惠活动赠送{%num%}消费分',
  54. 'status' => 1,
  55. 'pm' => 1
  56. ],
  57. 'order_give_exp' => [
  58. 'title' => '下单赠送经验',
  59. 'category' => 'exp',
  60. 'type' => 'gain',
  61. 'mark' => '下单赠送{%num%}经验',
  62. 'status' => 1,
  63. 'pm' => 1
  64. ],
  65. 'integral_refund' => [
  66. 'title' => '消费分回退',
  67. 'category' => 'integral',
  68. 'type' => 'deduction',
  69. 'mark' => '购买商品失败,回退{%num%}消费分',
  70. 'status' => 1,
  71. 'pm' => 0
  72. ],
  73. 'order_integral_refund' => [
  74. 'title' => '返还下单使用消费分',
  75. 'category' => 'integral',
  76. 'type' => 'integral_refund',
  77. 'mark' => '购买商品失败,回退{%num%}消费分',
  78. 'status' => 1,
  79. 'pm' => 1
  80. ],
  81. 'pay_product_integral_back' => [
  82. 'title' => '商品退消费分',
  83. 'category' => 'integral',
  84. 'type' => 'pay_product_integral_back',
  85. 'mark' => '订单返还{%num%}消费分',
  86. 'status' => 1,
  87. 'pm' => 1
  88. ],
  89. 'deduction' => [
  90. 'title' => '消费分抵扣',
  91. 'category' => 'integral',
  92. 'type' => 'deduction',
  93. 'mark' => '购买商品使用{%number%}消费分抵扣{%deductionPrice%}元',
  94. 'status' => 1,
  95. 'pm' => 0
  96. ],
  97. 'lottery_use_integral' => [
  98. 'title' => '参与抽奖使用消费分',
  99. 'category' => 'integral',
  100. 'type' => 'lottery_use',
  101. 'mark' => '参与抽奖使用{%num%}消费分',
  102. 'status' => 1,
  103. 'pm' => 0
  104. ],
  105. 'lottery_give_integral' => [
  106. 'title' => '抽奖中奖赠送消费分',
  107. 'category' => 'integral',
  108. 'type' => 'lottery_add',
  109. 'mark' => '抽奖中奖赠送{%num%}消费分',
  110. 'status' => 1,
  111. 'pm' => 1
  112. ],
  113. 'storeIntegral_use_integral' => [
  114. 'title' => '消费分兑换商品',
  115. 'category' => 'integral',
  116. 'type' => 'storeIntegral_use',
  117. 'mark' => '消费分商城兑换商品使用{%num%}消费分',
  118. 'status' => 1,
  119. 'pm' => 0
  120. ],
  121. 'system_clear_integral' => [
  122. 'title' => '到期自动清除消费分',
  123. 'category' => 'integral',
  124. 'type' => 'system_clear',
  125. 'mark' => '到期自动清除{%num%}消费分',
  126. 'status' => 1,
  127. 'pm' => 0
  128. ],
  129. 'newcomer_give_integral' => [
  130. 'title' => '新人礼赠送消费分',
  131. 'category' => 'integral',
  132. 'type' => 'newcomer_add',
  133. 'mark' => '新人礼赠送{%num%}消费分',
  134. 'status' => 1,
  135. 'pm' => 1
  136. ],
  137. 'level_give_integral' => [
  138. 'title' => '会员卡激活赠送消费分',
  139. 'category' => 'integral',
  140. 'type' => 'level_add',
  141. 'mark' => '会员卡激活赠送{%num%}消费分',
  142. 'status' => 1,
  143. 'pm' => 1
  144. ],
  145. 'system_add_integral' => [
  146. 'title' => '系统增加消费分',
  147. 'category' => 'integral',
  148. 'type' => 'system_add',
  149. 'mark' => '系统增加了{%num%}消费分',
  150. 'status' => 1,
  151. 'pm' => 1
  152. ],
  153. 'system_sub_integral' => [
  154. 'title' => '系统减少消费分',
  155. 'category' => 'integral',
  156. 'type' => 'system_sub',
  157. 'mark' => '系统扣除了{%num%}消费分',
  158. 'status' => 1,
  159. 'pm' => 0
  160. ],
  161. 'extract_integral' => [
  162. 'title' => '积分出局',
  163. 'category' => 'energy',
  164. 'type' => 'extract_integral',
  165. 'mark' => '用户{%type%}出局,出局时积分价格{%price%},共出局{%integral_num%}积分,获得{%number%}能量',
  166. 'status' => 1,
  167. 'pm' => 1
  168. ],
  169. 'extract_integral_new' => [
  170. 'title' => '贡献分能量',
  171. 'category' => 'energy',
  172. 'type' => 'activity_energy',
  173. 'mark' => '{mark},获得{%number%}能量',
  174. 'status' => 1,
  175. 'pm' => 1
  176. ],
  177. 'extract_integral_static' => [
  178. 'title' => '参考分提取能量',
  179. 'category' => 'energy',
  180. 'type' => 'static_energy',
  181. 'mark' => '{mark},获得{%number%}能量',
  182. 'status' => 1,
  183. 'pm' => 1
  184. ],
  185. 'extract_integral_shop' => [
  186. 'title' => '参考分提取消费分',
  187. 'category' => 'integral',
  188. 'type' => 'static_integral',
  189. 'mark' => '{mark},获得{%number%}消费分',
  190. 'status' => 1,
  191. 'pm' => 1
  192. ],
  193. 'exchange' => [
  194. 'title' => '能量转换',
  195. 'category' => 'energy',
  196. 'type' => 'exchange',
  197. 'mark' => '{%mark%},{%number%}能量转换份额',
  198. 'status' => 1,
  199. 'pm' => 0
  200. ],
  201. 'exchange_fail' => [
  202. 'title' => '转换失败',
  203. 'category' => 'energy',
  204. 'type' => 'exchange_fail',
  205. 'mark' => '转换失败,退回能量{%num%}',
  206. 'status' => 1,
  207. 'pm' => 1
  208. ],
  209. 'brokerage_to_energy' => [
  210. 'title' => '佣金转换到能量',
  211. 'category' => 'energy',
  212. 'type' => 'exchange_to_energy',
  213. 'mark' => '佣金转换到能量{%num%}',
  214. 'status' => 1,
  215. 'pm' => 1
  216. ],
  217. 'trade_in_shop_integral' => [
  218. 'title' => '消费分转入',
  219. 'category' => 'integral',
  220. 'type' => 'trade_in',
  221. 'mark' => '{%mark%}{%number%},扣除手续费后实际到账{%real_get%}',
  222. 'status' => 1,
  223. 'pm' => 1
  224. ],
  225. 'trade_out_shop_integral' => [
  226. 'title' => '消费分转出',
  227. 'category' => 'integral',
  228. 'type' => 'trade_out',
  229. 'mark' => '{%mark%}{%number%}',
  230. 'status' => 1,
  231. 'pm' => 0
  232. ],
  233. ];
  234. /**
  235. * UserBillServices constructor.
  236. * @param UserBillDao $dao
  237. */
  238. public function __construct(UserBillDao $dao)
  239. {
  240. $this->dao = $dao;
  241. }
  242. /**
  243. * 获取用户记录总和
  244. * @param $uid
  245. * @param string $category
  246. * @param array $type
  247. * @return mixed
  248. */
  249. public function getRecordCount(int $uid, $category = 'now_money', $type = [], $time = '', $pm = false)
  250. {
  251. $where = [];
  252. $where['uid'] = $uid;
  253. $where['category'] = $category;
  254. $where['status'] = 1;
  255. if (is_string($type) && strlen(trim($type))) {
  256. $where['type'] = explode(',', $type);
  257. }
  258. if ($time) {
  259. $where['time'] = $time;
  260. }
  261. $where['pm'] = $pm ? 1 : 0;
  262. return $this->dao->getBillSumColumn($where);
  263. }
  264. /**
  265. * 获取积分列表
  266. * @param int $uid
  267. * @param array $where_time
  268. * @param string $field
  269. * @return array
  270. */
  271. public function getIntegralList(int $uid = 0, $where_time = [], string $field = '*')
  272. {
  273. [$page, $limit] = $this->getPageValue();
  274. $where = ['category' => 'integral'];
  275. if ($uid) $where['uid'] = $uid;
  276. if ($where_time) $where['add_time'] = $where_time;
  277. $list = $this->dao->getList($where, $field, $page, $limit);
  278. foreach ($list as &$item) {
  279. $item['number'] = intval($item['number']);
  280. $item['balance'] = intval($item['balance']);
  281. }
  282. $count = $this->dao->count($where);
  283. return compact('list', 'count');
  284. }
  285. /**
  286. * 获取积分列表
  287. * @param int $uid
  288. * @param array $where_time
  289. * @param string $field
  290. * @return array
  291. */
  292. public function getEnergyList(int $uid = 0, $where_time = [], string $field = '*')
  293. {
  294. [$page, $limit] = $this->getPageValue();
  295. $where = ['category' => 'energy'];
  296. if ($uid) $where['uid'] = $uid;
  297. if ($where_time) $where['add_time'] = $where_time;
  298. $list = $this->dao->getList($where, $field, $page, $limit);
  299. foreach ($list as &$item) {
  300. $item['number'] = intval($item['number']);
  301. $item['balance'] = intval($item['balance']);
  302. }
  303. $count = $this->dao->count($where);
  304. return compact('list', 'count');
  305. }
  306. /**
  307. * 获取签到列表
  308. * @param int $uid
  309. * @param array $where_time
  310. * @param string $field
  311. * @return array
  312. */
  313. public function getSignList(int $uid = 0, $where_time = [], string $field = '*')
  314. {
  315. [$page, $limit] = $this->getPageValue();
  316. $where = ['category' => 'integral', 'type' => 'sign'];
  317. if ($uid) $where['uid'] = $uid;
  318. if ($where_time) $where['add_time'] = $where_time;
  319. $list = $this->dao->getList($where, $field, $page, $limit);
  320. $count = $this->dao->count($where);
  321. return compact('list', 'count');
  322. }
  323. /**
  324. * 经验总数
  325. * @param int $uid
  326. * @param array $where_time
  327. * @return float
  328. */
  329. public function getExpSum(int $uid = 0, $where_time = [])
  330. {
  331. $where = ['category' => ['exp'], 'pm' => 1, 'status' => 1];
  332. if ($uid) $where['uid'] = $uid;
  333. if ($where_time) $where['time'] = $where_time;
  334. return $this->dao->getBillSum($where);
  335. }
  336. /**
  337. * 获取所有经验列表
  338. * @param int $uid
  339. * @param array $where_time
  340. * @param string $field
  341. * @return array
  342. */
  343. public function getExpList(int $uid = 0, $where_time = [], string $field = '*')
  344. {
  345. [$page, $limit] = $this->getPageValue();
  346. $where = ['category' => ['exp']];
  347. $where['status'] = 1;
  348. if ($uid) $where['uid'] = $uid;
  349. if ($where_time) $where['time'] = $where_time;
  350. $list = $this->dao->getList($where, $field, $page, $limit);
  351. $count = $this->dao->count($where);
  352. return compact('list', 'count');
  353. }
  354. /**
  355. * 记录积分变化
  356. * @param int $uid
  357. * @param string $type
  358. * @param array $data
  359. * @param int $pm
  360. * @return bool
  361. * @throws Exception
  362. */
  363. public function incomeIntegral(int $uid, string $type, array $data, int $pm = 1)
  364. {
  365. $data['uid'] = $uid;
  366. $data['category'] = 'integral';
  367. $data['type'] = $type;
  368. $data['pm'] = $pm;
  369. $data['status'] = 1;
  370. $data['add_time'] = time();
  371. if (!$this->dao->save($data))
  372. throw new Exception('增加记录失败');
  373. return true;
  374. }
  375. /**
  376. * 写入用户记录
  377. * @param string $type 写入类型
  378. * @param int $uid
  379. * @param int|string|array $number
  380. * @param int|string $balance
  381. * @param int $link_id
  382. * @return bool|mixed
  383. */
  384. public function income(string $type, int $uid, $number, $balance, $link_id = 0)
  385. {
  386. $data = $this->incomeData[$type] ?? null;
  387. if (!$data) {
  388. return true;
  389. }
  390. $data['uid'] = $uid;
  391. $data['balance'] = $balance ?? 0;
  392. $data['link_id'] = $link_id;
  393. if (is_array($number)) {
  394. $key = array_keys($number);
  395. $key = array_map(function ($item) {
  396. return '{%' . $item . '%}';
  397. }, $key);
  398. $value = array_values($number);
  399. $data['number'] = $number['number'] ?? 0;
  400. $data['mark'] = str_replace($key, $value, $data['mark']);
  401. } else {
  402. $data['number'] = $number;
  403. $data['mark'] = str_replace(['{%num%}'], $number, $data['mark']);
  404. }
  405. $data['add_time'] = time();
  406. if ((float)$data['number']) {
  407. return $this->dao->save($data);
  408. }
  409. return true;
  410. }
  411. /**
  412. * 邀请新用户增加经验
  413. * @param int $spreadUid
  414. */
  415. public function inviteUserIncExp(int $spreadUid)
  416. {
  417. if (!$spreadUid) {
  418. return false;
  419. }
  420. //用户等级是否开启
  421. if (!sys_config('member_func_status', 1)) {
  422. return false;
  423. }
  424. /** @var UserServices $userService */
  425. $userService = app()->make(UserServices::class);
  426. $spread_user = $userService->getUserInfo($spreadUid);
  427. if (!$spread_user) {
  428. return false;
  429. }
  430. $exp_num = sys_config('invite_user_exp', 0);
  431. if ($exp_num) {
  432. $userService->incField($spreadUid, 'exp', (int)$exp_num);
  433. $data = [];
  434. $data['uid'] = $spreadUid;
  435. $data['number'] = $exp_num;
  436. $data['category'] = 'exp';
  437. $data['type'] = 'invite_user';
  438. $data['title'] = $data['mark'] = '邀新奖励';
  439. $data['balance'] = (int)$spread_user['exp'];
  440. $data['pm'] = 1;
  441. $data['status'] = 1;
  442. $this->dao->save($data);
  443. }
  444. //检测会员等级
  445. try {
  446. /** @var UserLevelServices $levelServices */
  447. $levelServices = app()->make(UserLevelServices::class);
  448. //检测会员升级
  449. $levelServices->detection($spreadUid);
  450. } catch (\Throwable $e) {
  451. Log::error('会员等级升级失败,失败原因:' . $e->getMessage());
  452. }
  453. return true;
  454. }
  455. /**
  456. * 获取type
  457. * @param array $where
  458. * @param string $filed
  459. */
  460. public function getBillType(array $where)
  461. {
  462. return $this->dao->getType($where);
  463. }
  464. /**
  465. * 资金类型
  466. */
  467. public function bill_type()
  468. {
  469. $where = [];
  470. $where['not_type'] = ['gain', 'system_sub', 'deduction', 'sign'];
  471. $where['not_category'] = ['exp', 'integral'];
  472. return CacheService::get('user_type_list', function () use ($where) {
  473. return ['list' => $this->dao->getType($where)];
  474. }, 600);
  475. }
  476. /**
  477. * 记录分享次数
  478. * @param int $uid 用户uid
  479. * @param int $cd 冷却时间
  480. * @return Boolean
  481. * */
  482. public function setUserShare(int $uid, $cd = 300)
  483. {
  484. /** @var UserServices $userServices */
  485. $userServices = app()->make(UserServices::class);
  486. $user = $userServices->getUserInfo($uid);
  487. if (!$user) {
  488. throw new ValidateException('用户不存在!');
  489. }
  490. $cachename = 'Share_' . $uid;
  491. if (CacheService::get($cachename)) {
  492. return false;
  493. }
  494. $data = ['title' => '用户分享记录', 'uid' => $uid, 'category' => 'share', 'type' => 'share', 'number' => 0, 'link_id' => 0, 'balance' => 0, 'mark' => date('Y-m-d H:i:s', time()) . ':用户分享'];
  495. if (!$this->dao->save($data)) {
  496. throw new ValidateException('记录分享记录失败');
  497. }
  498. CacheService::set($cachename, 1, $cd);
  499. return true;
  500. }
  501. /**
  502. * 获取积分列表
  503. * @param array $where
  504. * @param string $field
  505. * @param int $limit
  506. * @return array
  507. */
  508. public function getPointList(array $where, string $field = '*', int $limit = 0)
  509. {
  510. $where_data = [];
  511. $where_data['category'] = 'integral';
  512. if (isset($where['uid']) && $where['uid'] != '') {
  513. $where_data['uid'] = $where['uid'];
  514. }
  515. if ($where['start_time'] != '' && $where['end_time'] != '') {
  516. $where_data['time'] = $where['start_time'] . ' - ' . $where['end_time'];
  517. }
  518. if (isset($where['type']) && $where['type'] != '') {
  519. $where_data['type'] = $where['type'];
  520. }
  521. if (isset($where['nickname']) && $where['nickname'] != '') {
  522. $where_data['like'] = $where['nickname'];
  523. }
  524. if (isset($where['excel']) && $where['excel'] != '') {
  525. $where_data['excel'] = $where['excel'];
  526. } else {
  527. $where_data['excel'] = 0;
  528. }
  529. if ($limit) {
  530. [$page] = $this->getPageValue();
  531. } else {
  532. [$page, $limit] = $this->getPageValue();
  533. }
  534. $list = $this->dao->getBillList($where_data, $field, $page, $limit);
  535. foreach ($list as &$item) {
  536. $item['nickname'] = $item['user']['nickname'] ?? '';
  537. $item['number'] = intval($item['number']);
  538. $item['balance'] = intval($item['balance']);
  539. unset($item['user']);
  540. }
  541. $count = $this->dao->count($where_data);
  542. return compact('list', 'count');
  543. }
  544. /**
  545. * 积分头部信息
  546. * @param array $where
  547. * @return array[]
  548. */
  549. public function getUserPointBadgelist(array $where)
  550. {
  551. $data = [];
  552. $where_data = [];
  553. $where_data['category'] = 'integral';
  554. if ($where['start_time'] != '' && $where['end_time'] != '') {
  555. $where_data['time'] = $where['start_time'] . ' - ' . $where['end_time'];
  556. }
  557. if (isset($where['nickname']) && $where['nickname'] != '') {
  558. $where_data['like'] = $where['nickname'];
  559. }
  560. $data['SumIntegral'] = intval($this->dao->getBillSumColumn($where_data + ['pm' => 1]));
  561. $where_data['type'] = 'sign';
  562. $data['CountSign'] = $this->dao->getUserSignPoint($where_data);
  563. $data['SumSign'] = intval($this->dao->getBillSumColumn($where_data));
  564. unset($where_data['type']);
  565. $data['SumDeductionIntegral'] = intval($this->dao->getBillSumColumn($where_data + ['pm' => 0]));
  566. return [
  567. [
  568. 'col' => 6,
  569. 'count' => $data['SumIntegral'],
  570. 'name' => '总积分(个)',
  571. ],
  572. [
  573. 'col' => 6,
  574. 'count' => $data['CountSign'],
  575. 'name' => '客户签到次数(次)',
  576. ],
  577. [
  578. 'col' => 6,
  579. 'count' => $data['SumSign'],
  580. 'name' => '签到送出积分(个)',
  581. ],
  582. [
  583. 'col' => 6,
  584. 'count' => $data['SumDeductionIntegral'],
  585. 'name' => '使用积分(个)',
  586. ],
  587. ];
  588. }
  589. /**
  590. * @param $uid
  591. * @param $type
  592. * @return array
  593. */
  594. public function getUserBillList(int $uid, int $type)
  595. {
  596. $where = [];
  597. $where['uid'] = $uid;
  598. $where['category'] = 'now_money';
  599. switch ((int)$type) {
  600. case 0:
  601. $where['type'] = ['recharge', 'pay_money', 'system_add', 'pay_product_refund', 'system_sub', 'pay_member', 'offline_scan', 'lottery_use', 'lottery_add'];
  602. break;
  603. case 1:
  604. $where['type'] = ['pay_money', 'pay_member', 'offline_scan', 'user_recharge_refund', 'lottery_use'];
  605. break;
  606. case 2:
  607. $where['type'] = ['recharge', 'system_add', 'lottery_add'];
  608. break;
  609. case 3:
  610. $where['type'] = ['brokerage', 'brokerage_user'];
  611. break;
  612. case 4:
  613. $where['type'] = ['extract'];
  614. break;
  615. }
  616. $field = 'FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids';
  617. [$page, $limit] = $this->getPageValue();
  618. $list = $this->dao->getUserBillListByGroup($where, $field, 'time', $page, $limit);
  619. $data = [];
  620. if ($list) {
  621. $listIds = array_column($list, 'ids');
  622. $ids = [];
  623. foreach ($listIds as $id) {
  624. $ids = array_merge($ids, explode(',', $id));
  625. }
  626. $info = $this->dao->getColumn([['id', 'in', $ids]], 'FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i") as add_time,title,number,pm', 'id');
  627. foreach ($list as $item) {
  628. $value['time'] = $item['time'];
  629. $id = explode(',', $item['ids']);
  630. array_multisort($id, SORT_DESC);
  631. $value['list'] = [];
  632. foreach ($id as $v) {
  633. if (isset($info[$v])) {
  634. $value['list'][] = $info[$v];
  635. }
  636. }
  637. array_push($data, $value);
  638. }
  639. }
  640. return $data;
  641. }
  642. public function shop_integral_trade(int $uid, int $to_uid, $num)
  643. {
  644. $switch = sys_config('shop_integral_trade_switch', 1);
  645. $commission = sys_config('shop_integral_trade_commission', 8);
  646. if (!$switch) throw new ValidateException('暂不支持');
  647. if ($commission >= 100 || $commission < 0) throw new ValidateException('暂不支持');
  648. /** @var UserServices $userService */
  649. $userService = app()->make(UserServices::class);
  650. $user = $userService->getUserInfo($uid);
  651. $to_user = $userService->getUserInfo($to_uid);
  652. if (!$user || !$to_user) {
  653. throw new ValidateException('数据不存在');
  654. }
  655. if ($to_uid == $uid) throw new ValidateException('不能自己转给自己');
  656. if ($user['is_auth'] != 2) throw new ValidateException('请先完成实名认证');
  657. $extractPrice = $user['integral'];
  658. if ($num > $extractPrice) {
  659. throw new ValidateException('转账消费分不足' . $num);
  660. }
  661. if ($num <= 0) {
  662. throw new ValidateException('转账消费分大于0');
  663. }
  664. return $this->transaction(function () use ($num, $user, $to_user, $userService, $commission) {
  665. //修改用户佣金
  666. $balance = bcsub((string)$user['integral'], (string)$num, 2) ?? 0;
  667. $real_get = bcdiv(bcmul(bcsub('100', $commission, 2), (string)$num, 2), '100', 2);
  668. $balance2 = bcadd((string)$to_user['integral'], (string)$real_get, 2) ?? 0;
  669. if (!$userService->update($user['uid'], ['integral' => $balance], 'uid')) {
  670. throw new ValidateException('修改用户信息失败');
  671. }
  672. if (!$userService->update($to_user['uid'], ['integral' => $balance2], 'uid')) {
  673. throw new ValidateException('修改用户信息失败');
  674. }
  675. //保存佣金记录
  676. $this->income('trade_out_shop_integral', $user['uid'], ['mark' => '转账给' . $to_user['nickname'] . '(' . $to_user['uid'] . ')', 'number' => $num], $balance, 0);
  677. $this->income('trade_in_shop_integral', $to_user['uid'], ['mark' => '转账自' . $user['nickname'] . '(' . $user['uid'] . ')', 'number' => $num, 'real_get' => $real_get], $balance2, 0);
  678. return true;
  679. });
  680. }
  681. }