浏览代码

Merge branch 'master' of http://git.qiniu1314.com/Kirin/shenying

Kirin 10 月之前
父节点
当前提交
56dacf3531

+ 73 - 0
app/controller/api/v1/user/UserExchangeController.php

@@ -0,0 +1,73 @@
+<?php
+// +----------------------------------------------------------------------
+// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
+// +----------------------------------------------------------------------
+// | Author: CRMEB Team <admin@crmeb.com>
+// +----------------------------------------------------------------------
+namespace app\controller\api\v1\user;
+
+use app\model\user\User;
+use app\Request;
+use app\services\user\UserExchangeServices;
+use think\facade\Config;
+
+/**
+ * 提现类
+ * Class UserExtractController
+ * @package app\controller\api\user
+ */
+class UserExchangeController
+{
+    protected $services = NUll;
+
+    /**
+     * UserExtractController constructor.
+     * @param UserExchangeServices $services
+     */
+    public function __construct(UserExchangeServices $services)
+    {
+        $this->services = $services;
+    }
+
+    /**
+     * 提现银行
+     * @param Request $request
+     * @return mixed
+     */
+    public function bank(Request $request)
+    {
+        $uid = (int)$request->uid();
+        return app('json')->successful($this->services->bank($uid));
+    }
+
+
+    /**
+     * 提现申请
+     * @param Request $request
+     * @return mixed
+     */
+    public function exchange_energy(Request $request)
+    {
+        $extractInfo = $request->postMore([
+            ['energy', 0],
+            ['name', ''],
+            ['bankname', ''],
+            ['cardnum', ''],
+        ]);
+        if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', (float)$extractInfo['money'])) return app('json')->fail('转换能量输入有误');
+        if (!$extractInfo['cardnum'] == '')
+            if (!preg_match('/^([1-9]{1})(\d{8}|\d{11}|\d{14}|\d{15}|\d{16}|\d{18})$/', $extractInfo['cardnum']))
+                return app('json')->fail('银行卡号输入有误');
+        if (!$extractInfo['cardnum']) return app('json')->fail('请输入银行卡账号');
+        if (!$extractInfo['bankname']) return app('json')->fail('请输入开户行信息');
+        $uid = (int)$request->uid();
+        if ($this->services->cash($uid, $extractInfo))
+            return app('json')->successful('申请转换成功!');
+        else
+            return app('json')->fail('转换失败');
+    }
+}

+ 1 - 25
app/controller/api/v1/user/UserExtractController.php

@@ -142,29 +142,5 @@ class UserExtractController
     }
 
 
-    /**
-     * 提现申请
-     * @param Request $request
-     * @return mixed
-     */
-    public function exchange_energy(Request $request)
-    {
-        $extractInfo = $request->postMore([
-            ['energy', 0],
-            ['name', ''],
-            ['bankname', ''],
-            ['cardnum', ''],
-        ]);
-        if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', (float)$extractInfo['money'])) return app('json')->fail('转换能量输入有误');
-        if (!$extractInfo['cardnum'] == '')
-            if (!preg_match('/^([1-9]{1})(\d{8}|\d{11}|\d{14}|\d{15}|\d{16}|\d{18})$/', $extractInfo['cardnum']))
-                return app('json')->fail('银行卡号输入有误');
-        if (!$extractInfo['cardnum']) return app('json')->fail('请输入银行卡账号');
-        if (!$extractInfo['bankname']) return app('json')->fail('请输入开户行信息');
-        $uid = (int)$request->uid();
-        if ($this->services->cash($uid, $extractInfo))
-            return app('json')->successful('申请转换成功!');
-        else
-            return app('json')->fail('转换失败');
-    }
+
 }

+ 158 - 0
app/dao/user/UserExchangeDao.php

@@ -0,0 +1,158 @@
+<?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\dao\user;
+
+use app\dao\BaseDao;
+use app\model\user\UserExchange;
+use app\model\user\UserExtract;
+
+/**
+ *
+ * Class UserExtractDao
+ * @package app\dao\user
+ */
+class UserExchangeDao extends BaseDao
+{
+
+    /**
+     * 设置模型
+     * @return string
+     */
+    protected function setModel(): string
+    {
+        return UserExchange::class;
+    }
+
+    /**
+     * 获取列表
+     * @param array $where
+     * @param string $field
+     * @param int $page
+     * @param int $limit
+     * @param array $typeWhere
+     * @return array
+     */
+    public function getList(array $where, string $field = '*', array $with = [], int $page = 0, int $limit = 0)
+    {
+        return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
+            $query->with($with);
+        })->when($page && $limit, function ($query) use ($page, $limit) {
+            $query->page($page, $limit);
+        })->order('id desc')->select()->toArray();
+    }
+
+    /**
+     * 获取某个条件的提现总和
+     * @param array $where
+     * @return float
+     */
+    public function getWhereSum(array $where)
+    {
+        return $this->search($where)->field('(extract_num + extract_fee) as extract_num')->sum('extract_num');
+    }
+
+    /**
+     * 获取某些条件总数组合列表
+     * @param array $where
+     * @param string $field
+     * @param string $key
+     * @return mixed
+     */
+    public function getWhereSumList(array $where, string $field = 'extract_num', string $key = 'uid')
+    {
+        return $this->search($where)->group($key)->column('(sum(extract_num) + sum(extract_num)) as extract_num', $key);
+    }
+
+    /**
+     * 获取提现列表
+     * @param array $where
+     * @param string $field
+     * @param int $page
+     * @param int $limit
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getExtractList(array $where, string $field = '*', int $page = 0, int $limit = 0)
+    {
+        return $this->search($where)->field($field)->with([
+            'user' => function ($query) {
+                $query->field('uid,nickname');
+            }])->page($page, $limit)->order('id desc')->select()->toArray();
+    }
+
+    public function getExtractAll(array $where, string $field = '*')
+    {
+        return $this->search($where)->field($field)->with([
+            'user' => function ($query) {
+                $query->field('uid,nickname');
+            }])->order('id desc')->select()->toArray();
+    }
+
+    /**
+     * 获取某个字段总和
+     * @param array $where
+     * @param string $field
+     * @return float
+     */
+    public function getWhereSumField(array $where, string $field)
+    {
+        return $this->search($where)
+            ->when(isset($where['timeKey']), function ($query) use ($where) {
+                $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
+            })
+            ->sum($field);
+    }
+
+    /**
+     * 根据某字段分组查询
+     * @param array $where
+     * @param string $field
+     * @param string $group
+     * @return mixed
+     */
+    public function getGroupField(array $where, string $field, string $group)
+    {
+        return $this->search($where)
+            ->when(isset($where['timeKey']), function ($query) use ($where, $field, $group) {
+                $query->whereBetweenTime('add_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
+                if ($where['timeKey']['days'] == 1) {
+                    $timeUinx = "%H";
+                } elseif ($where['timeKey']['days'] == 30) {
+                    $timeUinx = "%Y-%m-%d";
+                } elseif ($where['timeKey']['days'] == 365) {
+                    $timeUinx = "%Y-%m";
+                } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
+                    $timeUinx = "%Y-%m-%d";
+                } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
+                    $timeUinx = "%Y-%m";
+                } else {
+					$timeUinx = "%Y-%m";
+                }
+                $query->field("sum($field) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
+                $query->group("FROM_UNIXTIME($group, '$timeUinx')");
+            })
+            ->order('add_time ASC')->select()->toArray();
+    }
+
+    /**
+     * @param array $where
+     * @param string $field
+     * @return float
+     */
+    public function getExtractMoneyByWhere(array $where, string $field)
+    {
+        return $this->search($where)->sum($field);
+    }
+}

+ 111 - 0
app/model/user/UserExchange.php

@@ -0,0 +1,111 @@
+<?php
+// +----------------------------------------------------------------------
+// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
+// +----------------------------------------------------------------------
+// | Author: CRMEB Team <admin@crmeb.com>
+// +----------------------------------------------------------------------
+
+namespace app\model\user;
+
+use crmeb\basic\BaseModel;
+use crmeb\traits\ModelTrait;
+use think\model;
+
+/**
+ * Class UserExtract
+ * @package app\model\user
+ */
+class UserExchange extends BaseModel
+{
+    use ModelTrait;
+
+    /**
+     * 数据表主键
+     * @var string
+     */
+    protected $pk = 'id';
+
+    /**
+     * 模型名称
+     * @var string
+     */
+    protected $name = 'user_exchange';
+
+    //审核中
+    const AUDIT_STATUS = 0;
+    //未通过
+    const FAIL_STATUS = -1;
+    //已提现
+    const SUCCESS_STATUS = 1;
+
+    /**
+     * 状态
+     * @var string[]
+     */
+    protected static $status = [
+        -1 => '未通过',
+        0 => '审核中',
+        1 => '已提现',
+    ];
+
+    /**
+     * 关联user
+     * @return model\relation\HasOne
+     */
+    public function user()
+    {
+        return $this->hasOne(User::class, 'uid', 'uid');
+    }
+
+    /**
+     * 用户uid
+     * @param Model $query
+     * @param $value
+     */
+    public function searchUidAttr($query, $value)
+    {
+        if (is_array($value))
+            $query->whereIn('uid', $value);
+        else
+            $query->where('uid', $value);
+    }
+
+    /**
+     * 审核状态
+     * @param Model $query
+     * @param $value
+     */
+    public function searchStatusAttr($query, $value)
+    {
+		if (is_array($value)) {
+			if ($value) {
+				$query->whereIn('status', $value);
+			}
+		}else {
+			if ($value !== '') {
+				$query->where('status', $value);
+			}
+		}
+    }
+
+    /**
+     * 模糊搜索
+     * @param Model $query
+     * @param $value
+     */
+    public function searchLikeAttr($query, $value)
+    {
+        if ($value) {
+            $query->where(function ($query) use ($value) {
+                $query->where('real_name|id|bank_code', 'LIKE', "%$value%")->whereOr('uid', 'in', function ($query) use ($value) {
+                    $query->name('user')->whereLike('nickname', '%' . $value . '%')->field('uid')->select();
+                });
+            });
+        }
+    }
+
+}

+ 16 - 0
app/services/user/UserBillServices.php

@@ -170,6 +170,22 @@ class UserBillServices extends BaseServices
             'status' => 1,
             'pm' => 1
         ],
+        'exchange' => [
+            'title' => '能量转换',
+            'category' => 'energy',
+            'type' => 'exchange',
+            'mark' => '{%mark%},{%number%}能量转换股权',
+            'status' => 1,
+            'pm' => 0
+        ],
+        'exchange_fail' => [
+            'title' => '转换失败',
+            'category' => 'energy',
+            'type' => 'exchange_fail',
+            'mark' => '转换失败,退回能量{%num%}',
+            'status' => 1,
+            'pm' => 1
+        ],
     ];
 
     /**

+ 402 - 0
app/services/user/UserExchangeServices.php

@@ -0,0 +1,402 @@
+<?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\dao\user\UserExchangeDao;
+use app\model\user\User;
+use app\model\user\UserExchange;
+use app\model\user\UserExtract;
+use app\services\BaseServices;
+use app\dao\user\UserExtractDao;
+use app\services\wechat\WechatUserServices;
+use crmeb\exceptions\AdminException;
+use crmeb\services\FormBuilder as Form;
+use crmeb\traits\ServicesTrait;
+use FormBuilder\Factory\Iview;
+use think\exception\ValidateException;
+use think\facade\Route as Url;
+
+/**
+ *
+ * Class UserExtractServices
+ * @package app\services\user
+ * @mixin UserExtractDao
+ */
+class UserExchangeServices extends BaseServices
+{
+
+    use ServicesTrait;
+
+    /**
+     * UserExtractServices constructor.
+     * @param UserExchangeDao $dao
+     */
+    public function __construct(UserExchangeDao $dao)
+    {
+        $this->dao = $dao;
+    }
+
+    /**
+     * 获取一条提现记录
+     * @param int $id
+     * @param array $field
+     * @return array|\think\Model|null
+     */
+    public function getExtract(int $id, array $field = [])
+    {
+        return $this->dao->get($id, $field);
+    }
+
+    /**
+     * 获取某个用户提现总数
+     * @param int $uid
+     * @return float
+     */
+    public function getUserExtract(int $uid)
+    {
+        return $this->dao->getWhereSum(['uid' => $uid, 'status' => [0, 1]]);
+    }
+
+    /**
+     * 获取某些用户的提现总数列表
+     * @param array $uids
+     */
+    public function getUsersSumList(array $uids)
+    {
+        return $this->dao->getWhereSumList(['uid' => $uids, 'status' => [0, 1]]);
+    }
+
+    public function getCount(array $where = [])
+    {
+        return $this->dao->getCount($where);
+    }
+
+    /**
+     * 获取提现列表
+     * @param array $where
+     * @param string $field
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getUserExtractList(array $where, string $field = '*')
+    {
+        [$page, $limit] = $this->getPageValue();
+        $list = $this->dao->getExtractList($where, $field, $page, $limit);
+        foreach ($list as &$item) {
+            $item['nickname'] = $item['user']['nickname'] ?? '';
+        }
+        $count = $this->dao->count($where);
+        return compact('list', 'count');
+    }
+
+    /**
+     * 获取提现总数
+     * @param array $where
+     */
+    public function getExtractSum(array $where)
+    {
+        return $this->dao->getExtractMoneyByWhere($where, 'extract_num');
+    }
+
+    /**
+     * 拒绝提现申请
+     * @param $id
+     * @param $fail_msg
+     * @return bool
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function changeFail(int $id, $userExtract, $message)
+    {
+        $fail_time = time();
+        $extract_number = bcadd((string)$userExtract['extract_num'], (string)$userExtract['extract_fee'], 2);
+        $mark = '换股份失败,退回能量值' . $extract_number;
+        $uid = $userExtract['uid'];
+        $status = -1;
+        /** @var UserServices $userServices */
+        $userServices = app()->make(UserServices::class);
+        $user = $userServices->getUserInfo($uid);
+        if (!$user) {
+            throw new ValidateException('用户不存在');
+        }
+        /** @var UserBillServices $userBrokerageServices */
+        $userBrokerageServices = app()->make(UserBillServices::class);
+        $this->transaction(function () use ($user, $userBrokerageServices, $uid, $id, $extract_number, $message, $userServices, $status, $fail_time) {
+            $now_brokerage = bcadd((string)$user['energy'], (string)$extract_number, 2);
+            //增加佣金记录
+            $userBrokerageServices->income('exchange_fail', $uid, $extract_number, $now_brokerage, $id);
+            //修改用户佣金
+            if (!$userServices->update($uid, ['energy' => $now_brokerage], 'uid'))
+                throw new AdminException('增加用户能量失败');
+            if (!$this->dao->update($id, ['fail_time' => $fail_time, 'fail_msg' => $message, 'status' => $status])) {
+                throw new AdminException('修改失败');
+            }
+        });
+        //消息推送
+        return true;
+    }
+
+    /**
+     * 通过提现申请
+     * @param $id
+     * @return bool
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function changeSuccess(int $id, $userExtract)
+    {
+        $this->transaction(function () use ($id, $userExtract) {
+            if (!$this->dao->update($id, ['status' => 1])) {
+                throw new AdminException('修改失败');
+            }
+        });
+        return true;
+    }
+
+    /**
+     * 显示资源列表
+     * @param array $where
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function index(array $where)
+    {
+        $list = $this->getUserExtractList($where);
+        //待提现金额
+        $where['status'] = 0;
+        $extract_statistics['price'] = $this->getExtractSum($where);
+        //已提现金额
+        $where['status'] = 1;
+        $extract_statistics['priced'] = $this->getExtractSum($where);
+        //佣金总金额
+        return compact('extract_statistics', 'list');
+    }
+
+
+    /**
+     * 显示资源列表
+     * @param array $where
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getExportList(array $where)
+    {
+        $list = $this->dao->getExtractAll($where);
+        return $list;
+    }
+
+
+    /**
+     * 显示编辑资源表单页.
+     *
+     * @param int $id
+     * @return \think\Response
+     */
+    public function edit(int $id)
+    {
+        $UserExtract = $this->getExtract($id);
+        if (!$UserExtract) {
+            throw new AdminException('数据不存在!');
+        }
+        $f = array();
+        $f[] = Form::input('real_name', '姓名', $UserExtract['real_name']);
+        $f[] = Form::number('extract_num', '转换数量', (float)$UserExtract['extract_num'])->precision(2)->disabled(true);
+        $f[] = Form::input('bank_code', '银行卡号', $UserExtract['bank_code']);
+        $f[] = Form::input('bank_address', '开户行', $UserExtract['bank_address']);
+        $f[] = Form::input('mark', '备注', $UserExtract['mark'])->type('textarea');
+        return create_form('编辑', $f, Url::buildUrl('/finance/exchange/' . $id), 'PUT');
+    }
+
+
+    public function update(int $id, array $data)
+    {
+        if (!$this->dao->update($id, $data))
+            throw new AdminException('修改失败');
+        else
+            return true;
+    }
+
+    /**
+     * 拒绝
+     * @param $id
+     * @return mixed
+     */
+    public function refuse(int $id, string $message)
+    {
+        $extract = $this->getExtract($id);
+        if (!$extract) {
+            throw new AdminException('操作记录不存在!');
+        }
+        if ($extract->status == 1) {
+            throw new AdminException('已经提现,错误操作');
+        }
+        if ($extract->status == -1) {
+            throw new AdminException('您的提现申请已被拒绝,请勿重复操作!');
+        }
+        $res = $this->changeFail($id, $extract, $message);
+        if ($res) {
+            return true;
+        } else {
+            throw new AdminException('操作失败!');
+        }
+    }
+
+    /**
+     * 通过
+     * @param $id
+     * @return mixed
+     */
+    public function adopt(int $id)
+    {
+        $extract = $this->getExtract($id);
+        if (!$extract) {
+            throw new AdminException('操作记录不存!');
+        }
+        if ($extract->status == 1 || $extract->status == 2) {
+            throw new AdminException('您已提现,请勿重复提现!');
+        }
+        if ($extract->status == -1) {
+            throw new AdminException('您的提现申请已被拒绝!');
+        }
+        if ($this->changeSuccess($id, $extract)) {
+            return true;
+        } else {
+            throw new AdminException('操作失败!');
+        }
+    }
+
+    /**待提现的数量
+     * @return int
+     */
+    public function userExtractCount()
+    {
+        return $this->dao->count(['status' => 0]);
+    }
+
+    /**
+     * 银行卡提现
+     * @param int $uid
+     * @return mixed
+     */
+    public function bank(int $uid)
+    {
+        /** @var UserServices $userService */
+        $userService = app()->make(UserServices::class);
+        $user = $userService->getUserInfo($uid);
+        if (!$user) {
+            throw new ValidateException('数据不存在');
+        }
+        $data['energy'] = $user['energy'];
+        //可提现佣金
+        $data['minEnergy'] = sys_config('user_exchange_min_energy');//提现最低金额
+        $data['exchange_fee'] = sys_config('exchange_fee');//提现手续费
+        $data['stock_price'] = sys_config('stock_price');//提现手续费
+        return $data;
+    }
+
+    /**
+     * 提现申请
+     * @param int $uid
+     * @param array $data
+     */
+    public function cash(int $uid, array $data)
+    {
+        /** @var UserServices $userService */
+        $userService = app()->make(UserServices::class);
+        $user = $userService->getUserInfo($uid);
+        if (!$user) {
+            throw new ValidateException('数据不存在');
+        }
+        if ($user['is_auth'] != 2) throw new ValidateException('请先完成实名认证');
+        if ($data['name'] != $user['real_name']) throw new ValidateException('转换用户与实名认证不符');
+        $data['energy'] = $user['energy'];
+        if ($data['money'] > $data['energy']) {
+            throw new ValidateException('可转换能量值不足');
+        }
+
+        $extractPrice = $user['energy'];
+        $userExtractMinPrice = sys_config('user_exchange_min_energy');
+        if ($data['money'] < $userExtractMinPrice) {
+            throw new ValidateException('转换能量值不能小于' . $userExtractMinPrice);
+        }
+        if ($extractPrice < 0) {
+            throw new ValidateException('转换能量值不足' . $data['money']);
+        }
+        if ($data['money'] > $extractPrice) {
+            throw new ValidateException('转换能量值不足' . $data['money']);
+        }
+        if ($data['money'] <= 0) {
+            throw new ValidateException('转换能量值大于0');
+        }
+        $fee = sys_config('exchange_fee');
+        $stock_price = sys_config('stock_price');
+        if ($stock_price <= 0) throw new ValidateException('暂不支持转换股份');
+        $extract_fee = bcdiv(bcmul($fee, $data['money'], 2), '100', 2);
+        if ($extract_fee < 0) $extract_fee = 0;
+        $insertData = [
+            'uid' => $user['uid'],
+            'extract_num' => bcsub((string)$data['money'], (string)$extract_fee, 2),
+            'extract_fee' => $extract_fee,
+            'extract_price' => $stock_price,
+            'exchange_num' => bcdiv(bcsub((string)$data['money'], (string)$extract_fee, 2), (string)$stock_price, 2),
+            'add_time' => time(),
+            'balance' => $user['energy'],
+            'status' => 0,
+        ];
+        $insertData['real_name'] = $data['name'];
+        $insertData['bank_code'] = $data['cardnum'];
+        $insertData['bank_address'] = $data['bankname'];
+        $mark = '转换' . $data['money'] . '能量值,扣除手续费后实际转换' . $insertData['extract_num'] . ',转换时股份价格' . $stock_price . ',共转换' . $insertData['exchange_num'] . '股';
+        /** @var UserBillServices $userBrokerageServices */
+        $userBrokerageServices = app()->make(UserBillServices::class);
+        $res1 = $this->transaction(function () use ($insertData, $data, $uid, $userService, $user, $userBrokerageServices, $mark) {
+            if (!$res1 = $this->dao->save($insertData)) {
+                throw new ValidateException('转换失败');
+            }
+            //修改用户佣金
+            $balance = bcsub((string)$user['energy'], (string)$data['money'], 2) ?? 0;
+            if (!$userService->update($uid, ['energy' => $balance], 'uid')) {
+                throw new ValidateException('修改用户信息失败');
+            }
+            //保存佣金记录
+            $userBrokerageServices->income('exchange', $uid, ['mark' => $mark, 'number' => $data['money']], $balance, $res1['id']);
+            return $res1;
+        });
+        return true;
+    }
+
+    /**
+     * @param array $where
+     * @param string $SumField
+     * @param string $selectType
+     * @param string $group
+     * @return float|mixed
+     */
+    public function getOutMoneyByWhere(array $where, string $SumField, string $selectType, string $group = "")
+    {
+        switch ($selectType) {
+            case "sum" :
+                return $this->dao->getWhereSumField($where, $SumField);
+            case "group" :
+                return $this->dao->getGroupField($where, $SumField, $group);
+        }
+    }
+}

+ 6 - 0
route/api.php

@@ -376,6 +376,12 @@ Route::group('api', function () {
         //提现类
         Route::get('extract/bank', 'v1.user.UserExtractController/bank')->name('extractBank');//提现银行/提现最低金额
         Route::post('extract/cash', 'v1.user.UserExtractController/cash')->name('extractCash');//提现申请
+
+
+        Route::get('exchange/bank', 'v1.user.UserExchangeController/bank')->name('exchangeEnergyBank');//能量值提取
+        Route::post('exchange/energy', 'v1.user.UserExchangeController/exchange_energy')->name('exchangeEnergy');//能量值提取
+
+
         Route::post('extract/calculator', 'v1.user.UserExtractController/cash_calculator')->name('extractCalculator');//提现申请
         Route::post('extract/employee', 'v1.user.UserController/addEmployee')->name('addEmployee');//提现申请
         Route::get('extract/sign_url', 'v1.user.UserController/applySignUrl')->name('applySignUrl');//提现申请