UserBillServices.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. 'type' => 'trade_in',
  65. 'mark' => '{%mark%}{%number%}积分',
  66. 'status' => 1,
  67. 'pm' => 1
  68. ],
  69. 'trade_out_integral' => [
  70. 'title' => '积分转出',
  71. 'type' => 'trade_out',
  72. 'mark' => '{%mark%}{%number%}积分',
  73. 'status' => 1,
  74. 'pm' => 0
  75. ],
  76. ];
  77. protected $categorys = [
  78. 'integral' => '积分'
  79. ];
  80. /**
  81. * UserBillServices constructor.
  82. * @param UserBill $model
  83. */
  84. public function __construct(UserBill $model)
  85. {
  86. $this->model = $model;
  87. }
  88. /**
  89. * 获取积分列表
  90. * @param int $uid
  91. * @param string $category
  92. * @param array $where_time
  93. * @param string $field
  94. * @return array
  95. * @throws DbException
  96. */
  97. public function getBillList(int $uid = 0, $category = 'integral', $where_time = [], string $field = '*')
  98. {
  99. [$page, $limit] = $this->getPageValue();
  100. $where = ['category' => $category];
  101. if ($uid) $where['uid'] = $uid;
  102. if ($where_time) $where['add_time'] = $where_time;
  103. $list = $this->getList($where, $field, $page, $limit);
  104. foreach ($list as &$item) {
  105. $item['number'] = intval($item['number']);
  106. $item['balance'] = intval($item['balance']);
  107. }
  108. $count = $this->getCount($where);
  109. return compact('list', 'count');
  110. }
  111. /**
  112. * 写入用户记录
  113. * @param string $type 写入类型
  114. * @param int $uid
  115. * @param int|string|array $number
  116. * @param int|string $link_id
  117. * @return bool|mixed
  118. */
  119. public function income(string $type, int $uid, $number, $link_id = 0)
  120. {
  121. $data = $this->incomeData[$type] ?? null;
  122. if (!$data) {
  123. return true;
  124. }
  125. $data['uid'] = $uid;
  126. $data['balance'] = $balance ?? 0;
  127. $data['link_id'] = $link_id;
  128. if (is_array($number)) {
  129. $key = array_keys($number);
  130. $key = array_map(function ($item) {
  131. return '{%' . $item . '%}';
  132. }, $key);
  133. $value = array_values($number);
  134. $data['number'] = $number['number'] ?? 0;
  135. $data['mark'] = str_replace($key, $value, $data['mark']);
  136. } else {
  137. $data['number'] = $number;
  138. $data['mark'] = str_replace(['{%number%}'], $number, $data['mark']);
  139. }
  140. $data['add_time'] = time();
  141. /** @var UserServices $userService */
  142. $userService = app()->make(UserServices::class);
  143. if ((float)$data['number'] > 0) {
  144. if ($data['status'] == 1) {
  145. $user = $userService->getUserInfo($uid);
  146. if ($data['pm'] == 1) {
  147. $data['balance'] = bcadd($user[$data['category']], $data['number'], 2);
  148. $userService->bcInc($uid, $data['category'], $data['number'], 'uid');
  149. } else {
  150. if ($data['number'] > $user[$data['category']]) {
  151. throw new ValidateException($this->categorys[$data['category']] . '余额不足');
  152. }
  153. $data['balance'] = bcsub($user[$data['category']], $data['number'], 2);
  154. $userService->bcDec($uid, $data['category'], $data['number'], 'uid');
  155. }
  156. }
  157. return $this->save($data);
  158. }
  159. return true;
  160. }
  161. /**
  162. * 获取type
  163. * @param string $category
  164. * @return array
  165. */
  166. public function getBillType(string $category = 'integral')
  167. {
  168. $getMoneyType = function () use ($category) {
  169. $array = $this->incomeData;
  170. $list = [];
  171. foreach ($array as $v) {
  172. if ($v['category'] == $category)
  173. $list[] = ['type' => $v['type'], 'title' => $v['title'], 'pm' => $v['pm']];
  174. }
  175. return $list;
  176. };
  177. return $getMoneyType();
  178. }
  179. /**
  180. * 资金类型
  181. */
  182. public function bill_type(string $category = 'integral')
  183. {
  184. return CacheService::get('user_type_list', function () use ($category) {
  185. return ['list' => $this->getBillType($category)];
  186. }, 600);
  187. }
  188. public function trade(int $uid, int $to_uid, $category, $num)
  189. {
  190. /** @var UserServices $userService */
  191. $userService = app()->make(UserServices::class);
  192. $user = $userService->getUserInfo($uid);
  193. $to_user = $userService->getUserInfo($to_uid);
  194. if (!$user || !$to_user) {
  195. throw new ValidateException('数据不存在');
  196. }
  197. if ($to_uid == $uid) throw new ValidateException('不能自己转给自己');
  198. $extractPrice = $user[$category];
  199. if ($num > $extractPrice) {
  200. throw new ValidateException('转账' . $this->categorys[$category] . '不足' . $num);
  201. }
  202. if ($num <= 0) {
  203. throw new ValidateException('转账' . $this->categorys[$category] . '大于0');
  204. }
  205. return $this->transaction(function () use ($num, $user, $to_user, $userService, $category) {
  206. $this->income('trade_out_' . $category, $user['uid'], ['mark' => '转账给' . $to_user['nickname'] . '(' . $to_user['uid'] . ')', 'number' => $num], $to_user['uid']);
  207. $this->income('trade_in_' . $category, $to_user['uid'], ['mark' => '转账自' . $user['nickname'] . '(' . $user['uid'] . ')', 'number' => $num], $user['uid']);
  208. return true;
  209. });
  210. }
  211. }