UserBillServices.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 qiniu\basic\BaseServices;
  14. use app\model\user\UserBill;
  15. use think\db\exception\DbException;
  16. use think\Exception;
  17. use think\exception\ValidateException;
  18. use qiniu\services\CacheService;
  19. use think\facade\Log;
  20. /**
  21. *
  22. * Class UserBillServices
  23. * @package app\services\user
  24. * @mixin UserBill
  25. */
  26. class UserBillServices extends BaseServices
  27. {
  28. protected $exportHeader = [
  29. 'id' => 'ID',
  30. 'uid' => '用户id',
  31. 'link_id' => '关联ID',
  32. 'category_chs' => '变更代币',
  33. 'title' => '变动标题',
  34. 'pm_chs' => '变动类型',
  35. 'number' => '变动金额',
  36. 'balance' => '变动后金额',
  37. 'mark' => '变动详情',
  38. 'add_time' => '变动时间',
  39. 'status_chs' => '变动状态'
  40. ];
  41. /**
  42. * 用户记录模板
  43. * @var array[]
  44. */
  45. protected $incomeData = [
  46. 'system_add_integral' => [
  47. 'title' => '系统增加积分',
  48. 'category' => 'integral',
  49. 'type' => 'system_add',
  50. 'mark' => '系统增加了{%number%}积分{%mark%}',
  51. 'status' => 1,
  52. 'pm' => 1
  53. ],
  54. 'system_sub_integral' => [
  55. 'title' => '系统减少积分',
  56. 'category' => 'integral',
  57. 'type' => 'system_sub',
  58. 'mark' => '系统扣除了{%number%}积分{%mark%}',
  59. 'status' => 1,
  60. 'pm' => 0
  61. ],
  62. 'trade_in_integral' => [
  63. 'title' => '积分转入',
  64. 'category' => 'integral',
  65. 'type' => 'trade_in',
  66. 'mark' => '{%mark%}{%number%}积分',
  67. 'status' => 1,
  68. 'pm' => 1
  69. ],
  70. 'trade_out_integral' => [
  71. 'title' => '积分转出',
  72. 'category' => 'integral',
  73. 'type' => 'trade_out',
  74. 'mark' => '{%mark%}{%number%}积分',
  75. 'status' => 1,
  76. 'pm' => 0
  77. ],
  78. ];
  79. protected $categorys = [
  80. 'integral' => '积分'
  81. ];
  82. /**
  83. * UserBillServices constructor.
  84. * @param UserBill $model
  85. */
  86. public function __construct(UserBill $model)
  87. {
  88. $this->model = $model;
  89. }
  90. /**
  91. * 获取积分列表
  92. * @param int $uid
  93. * @param string $category
  94. * @param array $where_time
  95. * @param string $field
  96. * @return array
  97. * @throws DbException
  98. */
  99. public function getBillList(int $uid = 0, $category = 'integral', $where_time = [], string $field = '*')
  100. {
  101. [$page, $limit] = $this->getPageValue();
  102. $where = ['category' => $category];
  103. if ($uid) $where['uid'] = $uid;
  104. if ($where_time) $where['add_time'] = $where_time;
  105. $list = $this->getList($where, $field, $page, $limit);
  106. foreach ($list as &$item) {
  107. $item['number'] = intval($item['number']);
  108. $item['balance'] = intval($item['balance']);
  109. }
  110. $count = $this->getCount($where);
  111. return compact('list', 'count');
  112. }
  113. /**
  114. * 获取资金列表
  115. * @param array $where
  116. * @param string $field
  117. * @param int $limit
  118. * @return array
  119. * @throws \think\db\exception\DbException
  120. */
  121. public function getUserBillList($category, array $where, string $field = '*', int $limit = 0)
  122. {
  123. $where_data = ['category' => $category];
  124. if (isset($where['uid']) && $where['uid'] != '') {
  125. $where_data['uid'] = $where['uid'];
  126. }
  127. if ($where['start_time'] != '' && $where['end_time'] != '') {
  128. $where_data['time'] = str_replace('-', '/', $where['start_time']) . ' - ' . str_replace('-', '/', $where['end_time']);
  129. }
  130. if (isset($where['type']) && $where['type'] != '') {
  131. $where_data['type'] = $where['type'];
  132. }
  133. if (isset($where['nickname']) && $where['nickname'] != '') {
  134. $where_data['like'] = $where['nickname'];
  135. }
  136. if (isset($where['pm']) && $where['pm'] != '') {
  137. $where_data['pm'] = $where['pm'];
  138. }
  139. if ($limit) {
  140. [$page] = $this->getPageValue();
  141. } else {
  142. [$page, $limit] = $this->getPageValue();
  143. }
  144. $data = $this->getList($where_data, $field, $page, $limit, [
  145. 'user' => function ($query) {
  146. $query->field('uid,nickname');
  147. }
  148. ]);
  149. foreach ($data as &$item) {
  150. $item['nickname'] = $item['user']['nickname'] ?? '';
  151. unset($item['user']);
  152. }
  153. $count = $this->getCount($where_data);
  154. return compact('data', 'count');
  155. }
  156. /**
  157. * 某个用户佣金总和
  158. * @param int $uid
  159. * @param string $time
  160. * @param int $pm
  161. * @return float
  162. */
  163. public function getUserBillSum(int $uid, $category, string $time = '', int $pm = 1)
  164. {
  165. $where = ['uid' => $uid];
  166. $where['pm'] = $pm;
  167. $where['status'] = 1;
  168. $where['category'] = $category;
  169. if ($time) $where['time'] = $time;
  170. return $this->getSum($where, 'number');
  171. }
  172. /**
  173. * 写入用户记录
  174. * @param string $type 写入类型
  175. * @param int $uid
  176. * @param int|string|array $number
  177. * @param int|string $link_id
  178. * @return bool|mixed
  179. */
  180. public function income(string $type, int $uid, $number, $link_id = 0)
  181. {
  182. $data = $this->incomeData[$type] ?? null;
  183. if (!$data) {
  184. throw new ValidateException('方法未完成');
  185. }
  186. $data['uid'] = $uid;
  187. $data['balance'] = $balance ?? 0;
  188. $data['link_id'] = $link_id;
  189. if (is_array($number)) {
  190. $key = array_keys($number);
  191. $key = array_map(function ($item) {
  192. return '{%' . $item . '%}';
  193. }, $key);
  194. $value = array_values($number);
  195. $data['number'] = (string)($number['number'] ?? 0);
  196. $data['mark'] = str_replace($key, $value, $data['mark']);
  197. } else {
  198. $data['number'] = (string)$number;
  199. $data['mark'] = str_replace(['{%number%}'], $number, $data['mark']);
  200. }
  201. $data['add_time'] = time();
  202. /** @var UserServices $userService */
  203. $userService = app()->make(UserServices::class);
  204. if ((float)$data['number'] > 0) {
  205. if ($data['status'] == 1) {
  206. $user = $userService->getUserInfo($uid);
  207. if ($data['pm'] == 1) {
  208. $data['balance'] = bcadd((string)$user[$data['category']], $data['number'], 2);
  209. $userService->bcInc($uid, $data['category'], $data['number'], 'uid');
  210. } else {
  211. if ($data['number'] > $user[$data['category']]) {
  212. throw new ValidateException($this->categorys[$data['category']] . '余额不足');
  213. }
  214. $data['balance'] = bcsub((string)$user[$data['category']], $data['number'], 2);
  215. $userService->bcDec($uid, $data['category'], $data['number'], 'uid');
  216. }
  217. }
  218. return $this->create($data);
  219. }
  220. return true;
  221. }
  222. /**
  223. * 获取type
  224. * @param string $category
  225. * @return array
  226. */
  227. public function getBillType(string $category = 'integral')
  228. {
  229. $getMoneyType = function () use ($category) {
  230. $array = $this->incomeData;
  231. $list = [];
  232. foreach ($array as $v) {
  233. if ($v['category'] == $category)
  234. $list[] = ['type' => $v['type'], 'title' => $v['title'], 'pm' => $v['pm']];
  235. }
  236. return $list;
  237. };
  238. return $getMoneyType();
  239. }
  240. /**
  241. * 资金类型
  242. */
  243. public function bill_type(string $category = 'integral')
  244. {
  245. return CacheService::get('user_type_list', function () use ($category) {
  246. return ['list' => $this->getBillType($category)];
  247. }, 600);
  248. }
  249. public function trade(int $uid, int $to_uid, $category, $num)
  250. {
  251. /** @var UserServices $userService */
  252. $userService = app()->make(UserServices::class);
  253. $user = $userService->getUserInfo($uid);
  254. $to_user = $userService->getUserInfo($to_uid);
  255. if (!$user || !$to_user) {
  256. throw new ValidateException('数据不存在');
  257. }
  258. if ($to_uid == $uid) throw new ValidateException('不能自己转给自己');
  259. if (!isset($this->categorys[$category])) throw new ValidateException('代币不存在');
  260. $extractPrice = $user[$category] ?? 0;
  261. if ($num > $extractPrice) {
  262. throw new ValidateException('转账' . $this->categorys[$category] . '不足' . $num);
  263. }
  264. if ($num <= 0) {
  265. throw new ValidateException('转账' . $this->categorys[$category] . '大于0');
  266. }
  267. if (!isset($this->incomeData['trade_out_' . $category]) || !isset($this->incomeData['trade_in_' . $category])) {
  268. throw new ValidateException('暂不支持转账');
  269. }
  270. return $this->transaction(function () use ($num, $user, $to_user, $userService, $category) {
  271. $this->income('trade_out_' . $category, $user['uid'], ['mark' => '转账给' . $to_user['nickname'] . '(' . $to_user['uid'] . ')', 'number' => $num], $to_user['uid']);
  272. $this->income('trade_in_' . $category, $to_user['uid'], ['mark' => '转账自' . $user['nickname'] . '(' . $user['uid'] . ')', 'number' => $num], $user['uid']);
  273. return true;
  274. });
  275. }
  276. }