UserBillServices.php 26 KB

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