123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\services\user;
- use app\model\user\UserMoney;
- use qiniu\basic\BaseServices;
- use qiniu\services\CacheService;
- use qiniu\exceptions\AdminException;
- use think\db\exception\DbException;
- use think\exception\ValidateException;
- /**
- * 用户余额
- * Class UserMoneyServices
- * @package app\services\user
- * @mixin UserMoney
- */
- class UserMoneyServices extends BaseServices
- {
- /**
- * 用户记录模板
- * @var array[]
- */
- protected $incomeData = [
- 'system_add' => [
- 'title' => '系统增加余额',
- 'type' => 'system_add',
- 'mark' => '系统增加{%number%}余额{%mark%}',
- 'status' => 1,
- 'pm' => 1
- ],
- 'system_sub' => [
- 'title' => '系统减少余额',
- 'type' => 'system_sub',
- 'mark' => '系统扣除了{%number%}余额{%mark%}',
- 'status' => 1,
- 'pm' => 0
- ],
- 'user_recharge' => [
- 'title' => '用户充值余额',
- 'type' => 'recharge',
- 'mark' => '成功充值余额{%number%}元',
- 'status' => 1,
- 'pm' => 1
- ],
- 'trade_in' => [
- 'title' => '余额转入',
- 'type' => 'trade_in',
- 'mark' => '{%mark%}{%number%}元',
- 'status' => 1,
- 'pm' => 1
- ],
- 'trade_out' => [
- 'title' => '余额转出',
- 'type' => 'trade_out',
- 'mark' => '{%mark%}{%number%}元',
- 'status' => 1,
- 'pm' => 0
- ],
- ];
- /**
- * UserMoneyServices constructor.
- * @param UserMoney $model
- */
- public function __construct(UserMoney $model)
- {
- $this->model = $model;
- }
- /**
- * 获取资金列表
- * @param array $where
- * @param string $field
- * @param int $limit
- * @return array
- * @throws DbException
- */
- public function getMoneyList(array $where, string $field = '*', int $limit = 0)
- {
- $where_data = [];
- if (isset($where['uid']) && $where['uid'] != '') {
- $where_data['uid'] = $where['uid'];
- }
- if ($where['start_time'] != '' && $where['end_time'] != '') {
- $where_data['time'] = str_replace('-', '/', $where['start_time']) . ' - ' . str_replace('-', '/', $where['end_time']);
- }
- if (isset($where['type']) && $where['type'] != '') {
- $where_data['type'] = $where['type'];
- }
- if (isset($where['nickname']) && $where['nickname'] != '') {
- $where_data['like'] = $where['nickname'];
- }
- if (isset($where['pm']) && $where['pm'] != '') {
- $where_data['pm'] = $where['pm'];
- }
- if ($limit) {
- [$page] = $this->getPageValue();
- } else {
- [$page, $limit] = $this->getPageValue();
- }
- $data = $this->getList($where_data, $field, $page, $limit, [
- 'user' => function ($query) {
- $query->field('uid,nickname');
- }]);
- foreach ($data as &$item) {
- $item['nickname'] = $item['user']['nickname'] ?? '';
- $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
- unset($item['user']);
- }
- $count = $this->getCount($where_data);
- return compact('data', 'count');
- }
- /**
- * 写入用户记录
- * @param string $type 写入类型
- * @param int $uid
- * @param int|string|array $number
- * @param int|string $link_id
- * @return bool
- * @throws DbException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function income(string $type, int $uid, $number, $link_id = 0)
- {
- $data = $this->incomeData[$type] ?? null;
- if (!$data) {
- return true;
- }
- $data['uid'] = $uid;
- $data['link_id'] = $link_id;
- if (is_array($number)) {
- $key = array_keys($number);
- $key = array_map(function ($item) {
- return '{%' . $item . '%}';
- }, $key);
- $value = array_values($number);
- $data['number'] = $number['number'] ?? 0;
- $data['mark'] = str_replace($key, $value, $data['mark']);
- } else {
- $data['number'] = $number;
- $data['mark'] = str_replace(['{%number%}'], $number, $data['mark']);
- }
- $data['add_time'] = time();
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- if ((float)$data['number'] > 0) {
- if ($data['status'] == 1) {
- $user = $userService->getUserInfo($uid);
- if ($data['pm'] == 1) {
- $data['balance'] = bcadd($user['now_money'], $data['number'], 2);
- $userService->bcInc($uid, 'now_money', $data['number'], 'uid');
- } else {
- if ($data['number'] > $user['now_money']) {
- throw new ValidateException('用户余额不足');
- }
- $data['balance'] = bcsub($user['now_money'], $data['number'], 2);
- $userService->bcDec($uid, 'now_money', $data['number'], 'uid');
- }
- }
- return $this->save($data);
- }
- return true;
- }
- /**
- * 资金类型
- * @return bool|mixed|null
- */
- public function bill_type()
- {
- $getMoneyType = function () {
- $array = $this->incomeData;
- $list = [];
- foreach ($array as $v) {
- $list[] = ['type' => $v['type'], 'title' => $v['title'], 'pm' => $v['pm']];
- }
- return compact('list');
- };
- return CacheService::get('user_money_type_list', $getMoneyType, 600);
- }
- public function trade(int $uid, int $to_uid, $num)
- {
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- $user = $userService->getUserInfo($uid);
- $to_user = $userService->getUserInfo($to_uid);
- if (!$user || !$to_user) {
- throw new ValidateException('数据不存在');
- }
- if ($to_uid == $uid) throw new ValidateException('不能自己转给自己');
- $extractPrice = $user['now_money'];
- if ($num > $extractPrice) {
- throw new ValidateException('转账余额不足' . $num);
- }
- if ($num <= 0) {
- throw new ValidateException('转账余额大于0');
- }
- return $this->transaction(function () use ($num, $user, $to_user, $userService) {
- //保存佣金记录
- $this->income('trade_out', $user['uid'], ['mark' => '转账给' . $to_user['nickname'] . '(' . $to_user['uid'] . ')', 'number' => $num], $to_user['uid']);
- $this->income('trade_in', $to_user['uid'], ['mark' => '转账自' . $user['nickname'] . '(' . $user['uid'] . ')', 'number' => $num], $user['uid']);
- return true;
- });
- }
- }
|