UserBillServices.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. /**
  81. * UserBillServices constructor.
  82. * @param UserBill $model
  83. */
  84. public function __construct(UserBill $model)
  85. {
  86. $this->model = $model;
  87. $this->categorys = $model->CategoryLang;
  88. }
  89. /**
  90. * 获取积分列表
  91. * @param int $uid
  92. * @param string $category
  93. * @param array $where_time
  94. * @param string $field
  95. * @return array
  96. * @throws DbException
  97. */
  98. public function getBillList(int $uid = 0, $category = 'integral', $where_time = [], string $field = '*')
  99. {
  100. [$page, $limit] = $this->getPageValue();
  101. $where = ['category' => $category];
  102. if ($uid) $where['uid'] = $uid;
  103. if ($where_time) $where['add_time'] = $where_time;
  104. $list = $this->getList($where, $field, $page, $limit);
  105. foreach ($list as &$item) {
  106. $item['number'] = intval($item['number']);
  107. $item['balance'] = intval($item['balance']);
  108. }
  109. $count = $this->getCount($where);
  110. return compact('list', 'count');
  111. }
  112. /**
  113. * 获取资金列表
  114. * @param array $where
  115. * @param string $field
  116. * @param int $limit
  117. * @return array
  118. * @throws \think\db\exception\DbException
  119. */
  120. public function getUserBillList($category, array $where, string $field = '*', int $limit = 0)
  121. {
  122. $where_data = ['category' => $category];
  123. if (isset($where['uid']) && $where['uid'] != '') {
  124. $where_data['uid'] = $where['uid'];
  125. }
  126. if ($where['start_time'] != '' && $where['end_time'] != '') {
  127. $where_data['time'] = str_replace('-', '/', $where['start_time']) . ' - ' . str_replace('-', '/', $where['end_time']);
  128. }
  129. if (isset($where['type']) && $where['type'] != '') {
  130. $where_data['type'] = $where['type'];
  131. }
  132. if (isset($where['nickname']) && $where['nickname'] != '') {
  133. $where_data['like'] = $where['nickname'];
  134. }
  135. if (isset($where['pm']) && $where['pm'] != '') {
  136. $where_data['pm'] = $where['pm'];
  137. }
  138. if ($limit) {
  139. [$page] = $this->getPageValue();
  140. } else {
  141. [$page, $limit] = $this->getPageValue();
  142. }
  143. $data = $this->getList($where_data, $field, $page, $limit, [
  144. 'user' => function ($query) {
  145. $query->field('uid,nickname');
  146. }
  147. ]);
  148. foreach ($data as &$item) {
  149. $item['nickname'] = $item['user']['nickname'] ?? '';
  150. unset($item['user']);
  151. }
  152. $count = $this->getCount($where_data);
  153. return compact('data', 'count');
  154. }
  155. /**
  156. * 某个用户佣金总和
  157. * @param int $uid
  158. * @param string $time
  159. * @param int $pm
  160. * @return float
  161. */
  162. public function getUserBillSum(int $uid, $category, string $time = '', int $pm = 1)
  163. {
  164. $where = ['uid' => $uid];
  165. $where['pm'] = $pm;
  166. $where['status'] = 1;
  167. $where['category'] = $category;
  168. if ($time) $where['time'] = $time;
  169. return $this->getSum($where, 'number');
  170. }
  171. /**
  172. * 写入用户记录
  173. * @param string $type 写入类型
  174. * @param int $uid
  175. * @param int|string|array $number
  176. * @param int|string $link_id
  177. * @return bool|mixed
  178. */
  179. public function income(string $type, int $uid, $number, $link_id = 0)
  180. {
  181. $data = $this->incomeData[$type] ?? null;
  182. if (!$data) {
  183. throw new ValidateException('方法未完成');
  184. }
  185. $data['uid'] = $uid;
  186. $data['balance'] = $balance ?? 0;
  187. $data['link_id'] = $link_id;
  188. if (is_array($number)) {
  189. $key = array_keys($number);
  190. $key = array_map(function ($item) {
  191. return '{%' . $item . '%}';
  192. }, $key);
  193. $value = array_values($number);
  194. $data['number'] = (string)($number['number'] ?? 0);
  195. $data['mark'] = str_replace($key, $value, $data['mark']);
  196. } else {
  197. $data['number'] = (string)$number;
  198. $data['mark'] = str_replace(['{%number%}'], $number, $data['mark']);
  199. }
  200. $data['add_time'] = time();
  201. /** @var UserServices $userService */
  202. $userService = app()->make(UserServices::class);
  203. if ((float)$data['number'] > 0) {
  204. if ($data['status'] == 1) {
  205. $user = $userService->getUserInfo($uid);
  206. if ($data['pm'] == 1) {
  207. $data['balance'] = bcadd((string)$user[$data['category']], $data['number'], 2);
  208. $userService->bcInc($uid, $data['category'], $data['number'], 'uid');
  209. } else {
  210. if ($data['number'] > $user[$data['category']]) {
  211. throw new ValidateException($this->categorys[$data['category']] . '余额不足');
  212. }
  213. $data['balance'] = bcsub((string)$user[$data['category']], $data['number'], 2);
  214. $userService->bcDec($uid, $data['category'], $data['number'], 'uid');
  215. }
  216. }
  217. return $this->create($data);
  218. }
  219. return true;
  220. }
  221. /**
  222. * 获取type
  223. * @param string $category
  224. * @return array
  225. */
  226. public function getBillType(string $category = 'integral')
  227. {
  228. $getMoneyType = function () use ($category) {
  229. $array = $this->incomeData;
  230. $list = [];
  231. foreach ($array as $v) {
  232. if ($v['category'] == $category)
  233. $list[] = ['type' => $v['type'], 'title' => $v['title'], 'pm' => $v['pm']];
  234. }
  235. return $list;
  236. };
  237. return $getMoneyType();
  238. }
  239. /**
  240. * 资金类型
  241. */
  242. public function bill_type(string $category = 'integral')
  243. {
  244. return CacheService::get('user_type_list', function () use ($category) {
  245. return ['list' => $this->getBillType($category)];
  246. }, 600);
  247. }
  248. public function trade(int $uid, int $to_uid, $category, $num)
  249. {
  250. /** @var UserServices $userService */
  251. $userService = app()->make(UserServices::class);
  252. $user = $userService->getUserInfo($uid);
  253. $to_user = $userService->getUserInfo($to_uid);
  254. if (!$user || !$to_user) {
  255. throw new ValidateException('数据不存在');
  256. }
  257. if ($to_uid == $uid) throw new ValidateException('不能自己转给自己');
  258. if (!isset($this->categorys[$category])) throw new ValidateException('代币不存在');
  259. $extractPrice = $user[$category] ?? 0;
  260. if ($num > $extractPrice) {
  261. throw new ValidateException('转账' . $this->categorys[$category] . '不足' . $num);
  262. }
  263. if ($num <= 0) {
  264. throw new ValidateException('转账' . $this->categorys[$category] . '大于0');
  265. }
  266. if (!isset($this->incomeData['trade_out_' . $category]) || !isset($this->incomeData['trade_in_' . $category])) {
  267. throw new ValidateException('暂不支持转账');
  268. }
  269. return $this->transaction(function () use ($num, $user, $to_user, $userService, $category) {
  270. $this->income('trade_out_' . $category, $user['uid'], ['mark' => '转账给' . $to_user['nickname'] . '(' . $to_user['uid'] . ')', 'number' => $num], $to_user['uid']);
  271. $this->income('trade_in_' . $category, $to_user['uid'], ['mark' => '转账自' . $user['nickname'] . '(' . $user['uid'] . ')', 'number' => $num], $user['uid']);
  272. return true;
  273. });
  274. }
  275. }