UserBillServices.php 24 KB

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