UserBillServices.php 25 KB

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