UserBillServices.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  152. unset($item['user']);
  153. }
  154. $count = $this->getCount($where_data);
  155. return compact('data', 'count');
  156. }
  157. /**
  158. * 某个用户佣金总和
  159. * @param int $uid
  160. * @param string $time
  161. * @param int $pm
  162. * @return float
  163. */
  164. public function getUserBillSum(int $uid, $category, string $time = '', int $pm = 1)
  165. {
  166. $where = ['uid' => $uid];
  167. $where['pm'] = $pm;
  168. $where['status'] = 1;
  169. $where['category'] = $category;
  170. if ($time) $where['time'] = $time;
  171. return $this->getSum($where, 'number');
  172. }
  173. /**
  174. * 写入用户记录
  175. * @param string $type 写入类型
  176. * @param int $uid
  177. * @param int|string|array $number
  178. * @param int|string $link_id
  179. * @return bool|mixed
  180. */
  181. public function income(string $type, int $uid, $number, $link_id = 0)
  182. {
  183. $data = $this->incomeData[$type] ?? null;
  184. if (!$data) {
  185. throw new ValidateException('方法未完成');
  186. }
  187. $data['uid'] = $uid;
  188. $data['balance'] = $balance ?? 0;
  189. $data['link_id'] = $link_id;
  190. if (is_array($number)) {
  191. $key = array_keys($number);
  192. $key = array_map(function ($item) {
  193. return '{%' . $item . '%}';
  194. }, $key);
  195. $value = array_values($number);
  196. $data['number'] = $number['number'] ?? 0;
  197. $data['mark'] = str_replace($key, $value, $data['mark']);
  198. } else {
  199. $data['number'] = $number;
  200. $data['mark'] = str_replace(['{%number%}'], $number, $data['mark']);
  201. }
  202. $data['add_time'] = time();
  203. /** @var UserServices $userService */
  204. $userService = app()->make(UserServices::class);
  205. if ((float)$data['number'] > 0) {
  206. if ($data['status'] == 1) {
  207. $user = $userService->getUserInfo($uid);
  208. if ($data['pm'] == 1) {
  209. $data['balance'] = bcadd((string)$user[$data['category']], (string)$data['number'], 2);
  210. $userService->bcInc($uid, $data['category'], $data['number'], 'uid');
  211. } else {
  212. if ($data['number'] > $user[$data['category']]) {
  213. throw new ValidateException($this->categorys[$data['category']] . '余额不足');
  214. }
  215. $data['balance'] = bcsub((string)$user[$data['category']], (string)$data['number'], 2);
  216. $userService->bcDec($uid, $data['category'], $data['number'], 'uid');
  217. }
  218. }
  219. return $this->create($data);
  220. }
  221. return true;
  222. }
  223. /**
  224. * 获取type
  225. * @param string $category
  226. * @return array
  227. */
  228. public function getBillType(string $category = 'integral')
  229. {
  230. $getMoneyType = function () use ($category) {
  231. $array = $this->incomeData;
  232. $list = [];
  233. foreach ($array as $v) {
  234. if ($v['category'] == $category)
  235. $list[] = ['type' => $v['type'], 'title' => $v['title'], 'pm' => $v['pm']];
  236. }
  237. return $list;
  238. };
  239. return $getMoneyType();
  240. }
  241. /**
  242. * 资金类型
  243. */
  244. public function bill_type(string $category = 'integral')
  245. {
  246. return CacheService::get('user_type_list', function () use ($category) {
  247. return ['list' => $this->getBillType($category)];
  248. }, 600);
  249. }
  250. public function trade(int $uid, int $to_uid, $category, $num)
  251. {
  252. /** @var UserServices $userService */
  253. $userService = app()->make(UserServices::class);
  254. $user = $userService->getUserInfo($uid);
  255. $to_user = $userService->getUserInfo($to_uid);
  256. if (!$user || !$to_user) {
  257. throw new ValidateException('数据不存在');
  258. }
  259. if ($to_uid == $uid) throw new ValidateException('不能自己转给自己');
  260. if (!isset($this->categorys[$category])) throw new ValidateException('代币不存在');
  261. $extractPrice = $user[$category] ?? 0;
  262. if ($num > $extractPrice) {
  263. throw new ValidateException('转账' . $this->categorys[$category] . '不足' . $num);
  264. }
  265. if ($num <= 0) {
  266. throw new ValidateException('转账' . $this->categorys[$category] . '大于0');
  267. }
  268. if (!isset($this->incomeData['trade_out_' . $category]) || !isset($this->incomeData['trade_in_' . $category])) {
  269. throw new ValidateException('暂不支持转账');
  270. }
  271. return $this->transaction(function () use ($num, $user, $to_user, $userService, $category) {
  272. $this->income('trade_out_' . $category, $user['uid'], ['mark' => '转账给' . $to_user['nickname'] . '(' . $to_user['uid'] . ')', 'number' => $num], $to_user['uid']);
  273. $this->income('trade_in_' . $category, $to_user['uid'], ['mark' => '转账自' . $user['nickname'] . '(' . $user['uid'] . ')', 'number' => $num], $user['uid']);
  274. return true;
  275. });
  276. }
  277. }