UserBillServices.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. 'energy_to_money' => [
  242. 'title' => '能量转换到余额',
  243. 'category' => 'energy',
  244. 'type' => 'energy_to_money',
  245. 'mark' => '能量转换到余额{%num%}',
  246. 'status' => 1,
  247. 'pm' => 0
  248. ],
  249. 'trade_in_shop_integral' => [
  250. 'title' => '消费分转入',
  251. 'category' => 'integral',
  252. 'type' => 'trade_in',
  253. 'mark' => '{%mark%},到账{%number%}',
  254. 'status' => 1,
  255. 'pm' => 1
  256. ],
  257. 'trade_out_shop_integral' => [
  258. 'title' => '消费分转出',
  259. 'category' => 'integral',
  260. 'type' => 'trade_out',
  261. 'mark' => '{%mark%}{%origin%},转账手续费{%fee%},合计{%number%}',
  262. 'status' => 1,
  263. 'pm' => 0
  264. ],
  265. ];
  266. /**
  267. * UserBillServices constructor.
  268. * @param UserBillDao $dao
  269. */
  270. public function __construct(UserBillDao $dao)
  271. {
  272. $this->dao = $dao;
  273. }
  274. /**
  275. * 获取用户记录总和
  276. * @param $uid
  277. * @param string $category
  278. * @param array $type
  279. * @return mixed
  280. */
  281. public function getRecordCount(int $uid, $category = 'now_money', $type = [], $time = '', $pm = false)
  282. {
  283. $where = [];
  284. $where['uid'] = $uid;
  285. $where['category'] = $category;
  286. $where['status'] = 1;
  287. if (is_string($type) && strlen(trim($type))) {
  288. $where['type'] = explode(',', $type);
  289. }
  290. if ($time) {
  291. $where['time'] = $time;
  292. }
  293. $where['pm'] = $pm ? 1 : 0;
  294. return $this->dao->getBillSumColumn($where);
  295. }
  296. /**
  297. * 获取积分列表
  298. * @param int $uid
  299. * @param array $where_time
  300. * @param string $field
  301. * @return array
  302. */
  303. public function getIntegralList(int $uid = 0, $where_time = [], string $field = '*')
  304. {
  305. [$page, $limit] = $this->getPageValue();
  306. $where = ['category' => 'integral'];
  307. if ($uid) $where['uid'] = $uid;
  308. if ($where_time) $where['add_time'] = $where_time;
  309. $list = $this->dao->getList($where, $field, $page, $limit);
  310. foreach ($list as &$item) {
  311. $item['number'] = intval($item['number']);
  312. $item['balance'] = intval($item['balance']);
  313. }
  314. $count = $this->dao->count($where);
  315. return compact('list', 'count');
  316. }
  317. /**
  318. * 获取积分列表
  319. * @param int $uid
  320. * @param array $where_time
  321. * @param string $field
  322. * @return array
  323. */
  324. public function getEnergyList(int $uid = 0, $where_time = [], string $field = '*')
  325. {
  326. [$page, $limit] = $this->getPageValue();
  327. $where = ['category' => 'energy'];
  328. if ($uid) $where['uid'] = $uid;
  329. if ($where_time) $where['add_time'] = $where_time;
  330. $list = $this->dao->getList($where, $field, $page, $limit);
  331. foreach ($list as &$item) {
  332. $item['number'] = intval($item['number']);
  333. $item['balance'] = intval($item['balance']);
  334. }
  335. $count = $this->dao->count($where);
  336. return compact('list', 'count');
  337. }
  338. /**
  339. * 获取签到列表
  340. * @param int $uid
  341. * @param array $where_time
  342. * @param string $field
  343. * @return array
  344. */
  345. public function getSignList(int $uid = 0, $where_time = [], string $field = '*')
  346. {
  347. [$page, $limit] = $this->getPageValue();
  348. $where = ['category' => 'integral', 'type' => 'sign'];
  349. if ($uid) $where['uid'] = $uid;
  350. if ($where_time) $where['add_time'] = $where_time;
  351. $list = $this->dao->getList($where, $field, $page, $limit);
  352. $count = $this->dao->count($where);
  353. return compact('list', 'count');
  354. }
  355. /**
  356. * 经验总数
  357. * @param int $uid
  358. * @param array $where_time
  359. * @return float
  360. */
  361. public function getExpSum(int $uid = 0, $where_time = [])
  362. {
  363. $where = ['category' => ['exp'], 'pm' => 1, 'status' => 1];
  364. if ($uid) $where['uid'] = $uid;
  365. if ($where_time) $where['time'] = $where_time;
  366. return $this->dao->getBillSum($where);
  367. }
  368. /**
  369. * 获取所有经验列表
  370. * @param int $uid
  371. * @param array $where_time
  372. * @param string $field
  373. * @return array
  374. */
  375. public function getExpList(int $uid = 0, $where_time = [], string $field = '*')
  376. {
  377. [$page, $limit] = $this->getPageValue();
  378. $where = ['category' => ['exp']];
  379. $where['status'] = 1;
  380. if ($uid) $where['uid'] = $uid;
  381. if ($where_time) $where['time'] = $where_time;
  382. $list = $this->dao->getList($where, $field, $page, $limit);
  383. $count = $this->dao->count($where);
  384. return compact('list', 'count');
  385. }
  386. /**
  387. * 记录积分变化
  388. * @param int $uid
  389. * @param string $type
  390. * @param array $data
  391. * @param int $pm
  392. * @return bool
  393. * @throws Exception
  394. */
  395. public function incomeIntegral(int $uid, string $type, array $data, int $pm = 1)
  396. {
  397. $data['uid'] = $uid;
  398. $data['category'] = 'integral';
  399. $data['type'] = $type;
  400. $data['pm'] = $pm;
  401. $data['status'] = 1;
  402. $data['add_time'] = time();
  403. if (!$this->dao->save($data))
  404. throw new Exception('增加记录失败');
  405. return true;
  406. }
  407. /**
  408. * 写入用户记录
  409. * @param string $type 写入类型
  410. * @param int $uid
  411. * @param int|string|array $number
  412. * @param int|string $balance
  413. * @param int $link_id
  414. * @return bool|mixed
  415. */
  416. public function income(string $type, int $uid, $number, $balance, $link_id = 0)
  417. {
  418. $data = $this->incomeData[$type] ?? null;
  419. if (!$data) {
  420. return true;
  421. }
  422. $data['uid'] = $uid;
  423. $data['balance'] = $balance ?? 0;
  424. $data['link_id'] = $link_id;
  425. if (is_array($number)) {
  426. $key = array_keys($number);
  427. $key = array_map(function ($item) {
  428. return '{%' . $item . '%}';
  429. }, $key);
  430. $value = array_values($number);
  431. $data['number'] = $number['number'] ?? 0;
  432. $data['mark'] = str_replace($key, $value, $data['mark']);
  433. } else {
  434. $data['number'] = $number;
  435. $data['mark'] = str_replace(['{%num%}'], $number, $data['mark']);
  436. }
  437. $data['add_time'] = time();
  438. if ((float)$data['number']) {
  439. return $this->dao->save($data);
  440. }
  441. return true;
  442. }
  443. /**
  444. * 邀请新用户增加经验
  445. * @param int $spreadUid
  446. */
  447. public function inviteUserIncExp(int $spreadUid)
  448. {
  449. if (!$spreadUid) {
  450. return false;
  451. }
  452. //用户等级是否开启
  453. if (!sys_config('member_func_status', 1)) {
  454. return false;
  455. }
  456. /** @var UserServices $userService */
  457. $userService = app()->make(UserServices::class);
  458. $spread_user = $userService->getUserInfo($spreadUid);
  459. if (!$spread_user) {
  460. return false;
  461. }
  462. $exp_num = sys_config('invite_user_exp', 0);
  463. if ($exp_num) {
  464. $userService->incField($spreadUid, 'exp', (int)$exp_num);
  465. $data = [];
  466. $data['uid'] = $spreadUid;
  467. $data['number'] = $exp_num;
  468. $data['category'] = 'exp';
  469. $data['type'] = 'invite_user';
  470. $data['title'] = $data['mark'] = '邀新奖励';
  471. $data['balance'] = (int)$spread_user['exp'];
  472. $data['pm'] = 1;
  473. $data['status'] = 1;
  474. $this->dao->save($data);
  475. }
  476. //检测会员等级
  477. try {
  478. /** @var UserLevelServices $levelServices */
  479. $levelServices = app()->make(UserLevelServices::class);
  480. //检测会员升级
  481. $levelServices->detection($spreadUid);
  482. } catch (\Throwable $e) {
  483. Log::error('会员等级升级失败,失败原因:' . $e->getMessage());
  484. }
  485. return true;
  486. }
  487. /**
  488. * 获取type
  489. * @param array $where
  490. * @param string $filed
  491. */
  492. public function getBillType(array $where)
  493. {
  494. return $this->dao->getType($where);
  495. }
  496. /**
  497. * 资金类型
  498. */
  499. public function bill_type()
  500. {
  501. $where = [];
  502. $where['not_type'] = ['gain', 'system_sub', 'deduction', 'sign'];
  503. $where['not_category'] = ['exp', 'integral'];
  504. return CacheService::get('user_type_list', function () use ($where) {
  505. return ['list' => $this->dao->getType($where)];
  506. }, 600);
  507. }
  508. /**
  509. * 记录分享次数
  510. * @param int $uid 用户uid
  511. * @param int $cd 冷却时间
  512. * @return Boolean
  513. * */
  514. public function setUserShare(int $uid, $cd = 300)
  515. {
  516. /** @var UserServices $userServices */
  517. $userServices = app()->make(UserServices::class);
  518. $user = $userServices->getUserInfo($uid);
  519. if (!$user) {
  520. throw new ValidateException('用户不存在!');
  521. }
  522. $cachename = 'Share_' . $uid;
  523. if (CacheService::get($cachename)) {
  524. return false;
  525. }
  526. $data = ['title' => '用户分享记录', 'uid' => $uid, 'category' => 'share', 'type' => 'share', 'number' => 0, 'link_id' => 0, 'balance' => 0, 'mark' => date('Y-m-d H:i:s', time()) . ':用户分享'];
  527. if (!$this->dao->save($data)) {
  528. throw new ValidateException('记录分享记录失败');
  529. }
  530. CacheService::set($cachename, 1, $cd);
  531. return true;
  532. }
  533. /**
  534. * 获取积分列表
  535. * @param array $where
  536. * @param string $field
  537. * @param int $limit
  538. * @return array
  539. */
  540. public function getPointList(array $where, string $field = '*', int $limit = 0)
  541. {
  542. $where_data = [];
  543. $where_data['category'] = 'integral';
  544. if (isset($where['uid']) && $where['uid'] != '') {
  545. $where_data['uid'] = $where['uid'];
  546. }
  547. if ($where['start_time'] != '' && $where['end_time'] != '') {
  548. $where_data['time'] = $where['start_time'] . ' - ' . $where['end_time'];
  549. }
  550. if (isset($where['type']) && $where['type'] != '') {
  551. $where_data['type'] = $where['type'];
  552. }
  553. if (isset($where['nickname']) && $where['nickname'] != '') {
  554. $where_data['like'] = $where['nickname'];
  555. }
  556. if (isset($where['excel']) && $where['excel'] != '') {
  557. $where_data['excel'] = $where['excel'];
  558. } else {
  559. $where_data['excel'] = 0;
  560. }
  561. if ($limit) {
  562. [$page] = $this->getPageValue();
  563. } else {
  564. [$page, $limit] = $this->getPageValue();
  565. }
  566. $list = $this->dao->getBillList($where_data, $field, $page, $limit);
  567. foreach ($list as &$item) {
  568. $item['nickname'] = $item['user']['nickname'] ?? '';
  569. $item['number'] = intval($item['number']);
  570. $item['balance'] = intval($item['balance']);
  571. unset($item['user']);
  572. }
  573. $count = $this->dao->count($where_data);
  574. return compact('list', 'count');
  575. }
  576. /**
  577. * 积分头部信息
  578. * @param array $where
  579. * @return array[]
  580. */
  581. public function getUserPointBadgelist(array $where)
  582. {
  583. $data = [];
  584. $where_data = [];
  585. $where_data['category'] = 'integral';
  586. if ($where['start_time'] != '' && $where['end_time'] != '') {
  587. $where_data['time'] = $where['start_time'] . ' - ' . $where['end_time'];
  588. }
  589. if (isset($where['nickname']) && $where['nickname'] != '') {
  590. $where_data['like'] = $where['nickname'];
  591. }
  592. $data['SumIntegral'] = intval($this->dao->getBillSumColumn($where_data + ['pm' => 1]));
  593. $where_data['type'] = 'sign';
  594. $data['CountSign'] = $this->dao->getUserSignPoint($where_data);
  595. $data['SumSign'] = intval($this->dao->getBillSumColumn($where_data));
  596. unset($where_data['type']);
  597. $data['SumDeductionIntegral'] = intval($this->dao->getBillSumColumn($where_data + ['pm' => 0]));
  598. return [
  599. [
  600. 'col' => 6,
  601. 'count' => $data['SumIntegral'],
  602. 'name' => '总积分(个)',
  603. ],
  604. [
  605. 'col' => 6,
  606. 'count' => $data['CountSign'],
  607. 'name' => '客户签到次数(次)',
  608. ],
  609. [
  610. 'col' => 6,
  611. 'count' => $data['SumSign'],
  612. 'name' => '签到送出积分(个)',
  613. ],
  614. [
  615. 'col' => 6,
  616. 'count' => $data['SumDeductionIntegral'],
  617. 'name' => '使用积分(个)',
  618. ],
  619. ];
  620. }
  621. /**
  622. * @param $uid
  623. * @param $type
  624. * @return array
  625. */
  626. public function getUserBillList(int $uid, int $type)
  627. {
  628. $where = [];
  629. $where['uid'] = $uid;
  630. $where['category'] = 'now_money';
  631. switch ((int)$type) {
  632. case 0:
  633. $where['type'] = ['recharge', 'pay_money', 'system_add', 'pay_product_refund', 'system_sub', 'pay_member', 'offline_scan', 'lottery_use', 'lottery_add'];
  634. break;
  635. case 1:
  636. $where['type'] = ['pay_money', 'pay_member', 'offline_scan', 'user_recharge_refund', 'lottery_use'];
  637. break;
  638. case 2:
  639. $where['type'] = ['recharge', 'system_add', 'lottery_add'];
  640. break;
  641. case 3:
  642. $where['type'] = ['brokerage', 'brokerage_user'];
  643. break;
  644. case 4:
  645. $where['type'] = ['extract'];
  646. break;
  647. }
  648. $field = 'FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids';
  649. [$page, $limit] = $this->getPageValue();
  650. $list = $this->dao->getUserBillListByGroup($where, $field, 'time', $page, $limit);
  651. $data = [];
  652. if ($list) {
  653. $listIds = array_column($list, 'ids');
  654. $ids = [];
  655. foreach ($listIds as $id) {
  656. $ids = array_merge($ids, explode(',', $id));
  657. }
  658. $info = $this->dao->getColumn([['id', 'in', $ids]], 'FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i") as add_time,title,number,pm', 'id');
  659. foreach ($list as $item) {
  660. $value['time'] = $item['time'];
  661. $id = explode(',', $item['ids']);
  662. array_multisort($id, SORT_DESC);
  663. $value['list'] = [];
  664. foreach ($id as $v) {
  665. if (isset($info[$v])) {
  666. $value['list'][] = $info[$v];
  667. }
  668. }
  669. array_push($data, $value);
  670. }
  671. }
  672. return $data;
  673. }
  674. public function shop_integral_trade(int $uid, int $to_uid, $num)
  675. {
  676. /** @var UserServices $userService */
  677. $userService = app()->make(UserServices::class);
  678. $user = $userService->getUserInfo($uid);
  679. $to_user = $userService->getUserInfo($to_uid);
  680. if ($user['is_shop'] && $to_user['is_shop']) throw new ValidateException('商家只可转给用户');
  681. if (!$user['is_shop'] && !$to_user['is_shop']) throw new ValidateException('用户只可转给商家');
  682. $switch = sys_config('shop_integral_trade_switch', 1);
  683. $commission = sys_config('shop_integral_trade_commission', 8);
  684. if ($user['is_shop']) $commission = sys_config('shop_integral_trade_commission_shop', 3);
  685. if (!$switch) throw new ValidateException('暂不支持');
  686. if ($commission >= 100 || $commission < 0) throw new ValidateException('暂不支持');
  687. if (!$user || !$to_user) {
  688. throw new ValidateException('数据不存在');
  689. }
  690. $fee = bcdiv(bcmul($commission, (string)$num, 2), '100', 2);
  691. if ($to_uid == $uid) throw new ValidateException('不能自己转给自己');
  692. if ($user['is_auth'] != 2) throw new ValidateException('请先完成实名认证');
  693. $extractPrice = $user['integral'];
  694. if ($num + $fee > $extractPrice) {
  695. throw new ValidateException('转账消费分不足' . $num);
  696. }
  697. if ($num <= 0) {
  698. throw new ValidateException('转账消费分大于0');
  699. }
  700. return $this->transaction(function () use ($num, $user, $to_user, $userService, $commission, $fee) {
  701. //修改用户佣金
  702. $balance = bcsub((string)$user['integral'], (string)($num + $fee), 2) ?? 0;
  703. $balance2 = bcadd((string)$to_user['integral'], (string)$num, 2);
  704. if ($user['is_shop']) $balance2 = bcadd((string)$to_user['now_money'], (string)$num, 2);
  705. if (!$userService->update($user['uid'], ['integral' => $balance], 'uid')) {
  706. throw new ValidateException('修改用户信息失败');
  707. }
  708. $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']);
  709. if (!$user['is_shop']) {
  710. if (!$userService->update($to_user['uid'], ['integral' => $balance2], 'uid')) {
  711. throw new ValidateException('修改用户信息失败');
  712. }
  713. $this->income('trade_in_shop_integral', $to_user['uid'], ['mark' => '转账自' . $user['nickname'] . '(' . $user['uid'] . ')', 'number' => $num], $balance2, $user['uid']);
  714. } else {
  715. $server = app()->make(UserMoneyServices::class);
  716. if (!$userService->update($to_user['uid'], ['now_money' => $balance2], 'uid')) {
  717. throw new ValidateException('修改用户信息失败');
  718. }
  719. $server->income('trade_in_shop_integral', $to_user['uid'], ['mark' => '转账自' . $user['nickname'] . '(' . $user['uid'] . ')', 'number' => $num], $balance2, $user['uid']);
  720. }
  721. //保存佣金记录
  722. return true;
  723. });
  724. }
  725. }