UserMoneyServices.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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\model\user\UserMoney;
  14. use qiniu\basic\BaseServices;
  15. use qiniu\services\CacheService;
  16. use qiniu\exceptions\AdminException;
  17. use think\db\exception\DbException;
  18. use think\exception\ValidateException;
  19. /**
  20. * 用户余额
  21. * Class UserMoneyServices
  22. * @package app\services\user
  23. * @mixin UserMoney
  24. */
  25. class UserMoneyServices extends BaseServices
  26. {
  27. /**
  28. * 用户记录模板
  29. * @var array[]
  30. */
  31. protected $incomeData = [
  32. 'system_add' => [
  33. 'title' => '系统增加余额',
  34. 'type' => 'system_add',
  35. 'mark' => '系统增加{%number%}余额{%mark%}',
  36. 'status' => 1,
  37. 'pm' => 1
  38. ],
  39. 'system_sub' => [
  40. 'title' => '系统减少余额',
  41. 'type' => 'system_sub',
  42. 'mark' => '系统扣除了{%number%}余额{%mark%}',
  43. 'status' => 1,
  44. 'pm' => 0
  45. ],
  46. 'user_recharge' => [
  47. 'title' => '用户充值余额',
  48. 'type' => 'recharge',
  49. 'mark' => '成功充值余额{%number%}元',
  50. 'status' => 1,
  51. 'pm' => 1
  52. ],
  53. 'trade_in' => [
  54. 'title' => '余额转入',
  55. 'type' => 'trade_in',
  56. 'mark' => '{%mark%}{%number%}元',
  57. 'status' => 1,
  58. 'pm' => 1
  59. ],
  60. 'trade_out' => [
  61. 'title' => '余额转出',
  62. 'type' => 'trade_out',
  63. 'mark' => '{%mark%}{%number%}元',
  64. 'status' => 1,
  65. 'pm' => 0
  66. ],
  67. ];
  68. /**
  69. * UserMoneyServices constructor.
  70. * @param UserMoney $model
  71. */
  72. public function __construct(UserMoney $model)
  73. {
  74. $this->model = $model;
  75. }
  76. /**
  77. * 某个用户佣金总和
  78. * @param int $uid
  79. * @param string $time
  80. * @param int $pm
  81. * @return float
  82. */
  83. public function getUserMoneySum(int $uid, string $time = '', int $pm = 1)
  84. {
  85. $where = ['uid' => $uid];
  86. $where['pm'] = $pm;
  87. $where['status'] = 1;
  88. if ($time) $where['time'] = $time;
  89. return $this->getMoneySum($where);
  90. }
  91. /**
  92. * 获取某个条件总数
  93. * @param array $where
  94. * @return float
  95. */
  96. public function getMoneySum(array $where)
  97. {
  98. return $this->search($where)->sum('number');
  99. }
  100. /**
  101. * 获取资金列表
  102. * @param array $where
  103. * @param string $field
  104. * @param int $limit
  105. * @return array
  106. * @throws DbException
  107. */
  108. public function getMoneyList(array $where, string $field = '*', int $limit = 0)
  109. {
  110. $where_data = [];
  111. if (isset($where['uid']) && $where['uid'] != '') {
  112. $where_data['uid'] = $where['uid'];
  113. }
  114. if ($where['start_time'] != '' && $where['end_time'] != '') {
  115. $where_data['time'] = str_replace('-', '/', $where['start_time']) . ' - ' . str_replace('-', '/', $where['end_time']);
  116. }
  117. if (isset($where['type']) && $where['type'] != '') {
  118. $where_data['type'] = $where['type'];
  119. }
  120. if (isset($where['nickname']) && $where['nickname'] != '') {
  121. $where_data['like'] = $where['nickname'];
  122. }
  123. if (isset($where['pm']) && $where['pm'] != '') {
  124. $where_data['pm'] = $where['pm'];
  125. }
  126. if ($limit) {
  127. [$page] = $this->getPageValue();
  128. } else {
  129. [$page, $limit] = $this->getPageValue();
  130. }
  131. $data = $this->getList($where_data, $field, $page, $limit, [
  132. 'user' => function ($query) {
  133. $query->field('uid,nickname');
  134. }]);
  135. foreach ($data as &$item) {
  136. $item['nickname'] = $item['user']['nickname'] ?? '';
  137. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  138. unset($item['user']);
  139. }
  140. $count = $this->getCount($where_data);
  141. return compact('data', 'count');
  142. }
  143. /**
  144. * 写入用户记录
  145. * @param string $type 写入类型
  146. * @param int $uid
  147. * @param int|string|array $number
  148. * @param int|string $link_id
  149. * @return bool|\qiniu\basic\BaseModel|\think\Model
  150. * @throws DbException
  151. * @throws \think\db\exception\DataNotFoundException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. public function income(string $type, int $uid, $number, $link_id = 0)
  155. {
  156. $data = $this->incomeData[$type] ?? null;
  157. if (!$data) {
  158. throw new ValidateException('方法未完成');
  159. }
  160. $data['uid'] = $uid;
  161. $data['link_id'] = $link_id;
  162. if (is_array($number)) {
  163. $key = array_keys($number);
  164. $key = array_map(function ($item) {
  165. return '{%' . $item . '%}';
  166. }, $key);
  167. $value = array_values($number);
  168. $data['number'] = (string)($number['number'] ?? 0);
  169. $data['mark'] = str_replace($key, $value, $data['mark']);
  170. } else {
  171. $data['number'] = (string)$number;
  172. $data['mark'] = str_replace(['{%number%}'], $number, $data['mark']);
  173. }
  174. $data['add_time'] = time();
  175. /** @var UserServices $userService */
  176. $userService = app()->make(UserServices::class);
  177. if ((float)$data['number'] > 0) {
  178. if ($data['status'] == 1) {
  179. $user = $userService->getUserInfo($uid);
  180. if ($data['pm'] == 1) {
  181. $data['balance'] = bcadd((string)$user['now_money'], (string)$data['number'], 2);
  182. $userService->bcInc($uid, 'now_money', (string)$data['number'], 'uid');
  183. } else {
  184. if ($data['number'] > $user['now_money']) {
  185. throw new ValidateException('用户余额不足');
  186. }
  187. $data['balance'] = bcsub((string)$user['now_money'], (string)$data['number'], 2);
  188. $userService->bcDec($uid, 'now_money', (string)$data['number'], 'uid');
  189. }
  190. }
  191. return $this->create($data);
  192. }
  193. return true;
  194. }
  195. /**
  196. * 资金类型
  197. * @return bool|mixed|null
  198. */
  199. public function bill_type()
  200. {
  201. $getMoneyType = function () {
  202. $array = $this->incomeData;
  203. $list = [];
  204. foreach ($array as $v) {
  205. $list[] = ['type' => $v['type'], 'title' => $v['title'], 'pm' => $v['pm']];
  206. }
  207. return compact('list');
  208. };
  209. return CacheService::get('user_money_type_list', $getMoneyType, 600);
  210. }
  211. public function trade(int $uid, int $to_uid, $num)
  212. {
  213. /** @var UserServices $userService */
  214. $userService = app()->make(UserServices::class);
  215. $user = $userService->getUserInfo($uid);
  216. $to_user = $userService->getUserInfo($to_uid);
  217. if (!$user || !$to_user) {
  218. throw new ValidateException('数据不存在');
  219. }
  220. if ($to_uid == $uid) throw new ValidateException('不能自己转给自己');
  221. $extractPrice = $user['now_money'];
  222. if ($num > $extractPrice) {
  223. throw new ValidateException('转账余额不足' . $num);
  224. }
  225. if ($num <= 0) {
  226. throw new ValidateException('转账余额大于0');
  227. }
  228. return $this->transaction(function () use ($num, $user, $to_user, $userService) {
  229. //保存佣金记录
  230. $this->income('trade_out', $user['uid'], ['mark' => '转账给' . $to_user['nickname'] . '(' . $to_user['uid'] . ')', 'number' => $num], $to_user['uid']);
  231. $this->income('trade_in', $to_user['uid'], ['mark' => '转账自' . $user['nickname'] . '(' . $user['uid'] . ')', 'number' => $num], $user['uid']);
  232. return true;
  233. });
  234. }
  235. }