UserExchangeServices.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 app\dao\user\UserExchangeDao;
  14. use app\model\user\User;
  15. use app\model\user\UserExchange;
  16. use app\model\user\UserExtract;
  17. use app\services\BaseServices;
  18. use app\dao\user\UserExtractDao;
  19. use app\services\wechat\WechatUserServices;
  20. use crmeb\exceptions\AdminException;
  21. use crmeb\services\FormBuilder as Form;
  22. use crmeb\traits\ServicesTrait;
  23. use FormBuilder\Factory\Iview;
  24. use think\exception\ValidateException;
  25. use think\facade\Route as Url;
  26. /**
  27. *
  28. * Class UserExtractServices
  29. * @package app\services\user
  30. * @mixin UserExtractDao
  31. */
  32. class UserExchangeServices extends BaseServices
  33. {
  34. use ServicesTrait;
  35. /**
  36. * UserExtractServices constructor.
  37. * @param UserExchangeDao $dao
  38. */
  39. public function __construct(UserExchangeDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. /**
  44. * 获取一条提现记录
  45. * @param int $id
  46. * @param array $field
  47. * @return array|\think\Model|null
  48. */
  49. public function getExtract(int $id, array $field = [])
  50. {
  51. return $this->dao->get($id, $field);
  52. }
  53. /**
  54. * 获取某个用户提现总数
  55. * @param int $uid
  56. * @return float
  57. */
  58. public function getUserExtract(int $uid)
  59. {
  60. return $this->dao->getWhereSum(['uid' => $uid, 'status' => [0, 1]]);
  61. }
  62. /**
  63. * 获取某些用户的提现总数列表
  64. * @param array $uids
  65. */
  66. public function getUsersSumList(array $uids)
  67. {
  68. return $this->dao->getWhereSumList(['uid' => $uids, 'status' => [0, 1]]);
  69. }
  70. public function getCount(array $where = [])
  71. {
  72. return $this->dao->getCount($where);
  73. }
  74. /**
  75. * 获取提现列表
  76. * @param array $where
  77. * @param string $field
  78. * @return array
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function getUserExtractList(array $where, string $field = '*')
  84. {
  85. [$page, $limit] = $this->getPageValue();
  86. $list = $this->dao->getExtractList($where, $field, $page, $limit);
  87. foreach ($list as &$item) {
  88. $item['nickname'] = $item['user']['nickname'] ?? '';
  89. }
  90. $count = $this->dao->count($where);
  91. return compact('list', 'count');
  92. }
  93. /**
  94. * 获取提现总数
  95. * @param array $where
  96. */
  97. public function getExtractSum(array $where)
  98. {
  99. return $this->dao->getExtractMoneyByWhere($where, 'extract_num');
  100. }
  101. /**
  102. * 拒绝提现申请
  103. * @param $id
  104. * @param $fail_msg
  105. * @return bool
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. * @throws \think\exception\DbException
  109. */
  110. public function changeFail(int $id, $userExtract, $message)
  111. {
  112. $fail_time = time();
  113. $extract_number = bcadd((string)$userExtract['extract_num'], (string)$userExtract['extract_fee'], 2);
  114. $mark = '换股份失败,退回能量值' . $extract_number;
  115. $uid = $userExtract['uid'];
  116. $status = -1;
  117. /** @var UserServices $userServices */
  118. $userServices = app()->make(UserServices::class);
  119. $user = $userServices->getUserInfo($uid);
  120. if (!$user) {
  121. throw new ValidateException('用户不存在');
  122. }
  123. /** @var UserBillServices $userBrokerageServices */
  124. $userBrokerageServices = app()->make(UserBillServices::class);
  125. $this->transaction(function () use ($user, $userBrokerageServices, $uid, $id, $extract_number, $message, $userServices, $status, $fail_time) {
  126. $now_brokerage = bcadd((string)$user['energy'], (string)$extract_number, 2);
  127. //增加佣金记录
  128. $userBrokerageServices->income('exchange_fail', $uid, $extract_number, $now_brokerage, $id);
  129. //修改用户佣金
  130. if (!$userServices->update($uid, ['energy' => $now_brokerage], 'uid'))
  131. throw new AdminException('增加用户能量失败');
  132. if (!$this->dao->update($id, ['fail_time' => $fail_time, 'fail_msg' => $message, 'status' => $status])) {
  133. throw new AdminException('修改失败');
  134. }
  135. });
  136. //消息推送
  137. return true;
  138. }
  139. /**
  140. * 通过提现申请
  141. * @param $id
  142. * @return bool
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. * @throws \think\exception\DbException
  146. */
  147. public function changeSuccess(int $id, $userExtract)
  148. {
  149. $this->transaction(function () use ($id, $userExtract) {
  150. if (!$this->dao->update($id, ['status' => 1])) {
  151. throw new AdminException('修改失败');
  152. }
  153. });
  154. return true;
  155. }
  156. /**
  157. * 显示资源列表
  158. * @param array $where
  159. * @return array
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\DbException
  162. * @throws \think\db\exception\ModelNotFoundException
  163. */
  164. public function index(array $where)
  165. {
  166. $list = $this->getUserExtractList($where);
  167. //待提现金额
  168. $where['status'] = 0;
  169. $extract_statistics['price'] = $this->getExtractSum($where);
  170. //已提现金额
  171. $where['status'] = 1;
  172. $extract_statistics['priced'] = $this->getExtractSum($where);
  173. //佣金总金额
  174. return compact('extract_statistics', 'list');
  175. }
  176. /**
  177. * 显示资源列表
  178. * @param array $where
  179. * @return array
  180. * @throws \think\db\exception\DataNotFoundException
  181. * @throws \think\db\exception\DbException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. */
  184. public function getExportList(array $where)
  185. {
  186. $list = $this->dao->getExtractAll($where);
  187. return $list;
  188. }
  189. /**
  190. * 显示编辑资源表单页.
  191. *
  192. * @param int $id
  193. * @return \think\Response
  194. */
  195. public function edit(int $id)
  196. {
  197. $UserExtract = $this->getExtract($id);
  198. if (!$UserExtract) {
  199. throw new AdminException('数据不存在!');
  200. }
  201. $f = array();
  202. $f[] = Form::input('real_name', '姓名', $UserExtract['real_name']);
  203. $f[] = Form::number('extract_num', '转换数量', (float)$UserExtract['extract_num'])->precision(2)->disabled(true);
  204. $f[] = Form::input('bank_code', '银行卡号', $UserExtract['bank_code']);
  205. $f[] = Form::input('bank_address', '开户行', $UserExtract['bank_address']);
  206. $f[] = Form::input('mark', '备注', $UserExtract['mark'])->type('textarea');
  207. return create_form('编辑', $f, Url::buildUrl('/finance/exchange/' . $id), 'PUT');
  208. }
  209. public function update(int $id, array $data)
  210. {
  211. if (!$this->dao->update($id, $data))
  212. throw new AdminException('修改失败');
  213. else
  214. return true;
  215. }
  216. /**
  217. * 拒绝
  218. * @param $id
  219. * @return mixed
  220. */
  221. public function refuse(int $id, string $message)
  222. {
  223. $extract = $this->getExtract($id);
  224. if (!$extract) {
  225. throw new AdminException('操作记录不存在!');
  226. }
  227. if ($extract->status == 1) {
  228. throw new AdminException('已经提现,错误操作');
  229. }
  230. if ($extract->status == -1) {
  231. throw new AdminException('您的提现申请已被拒绝,请勿重复操作!');
  232. }
  233. $res = $this->changeFail($id, $extract, $message);
  234. if ($res) {
  235. return true;
  236. } else {
  237. throw new AdminException('操作失败!');
  238. }
  239. }
  240. /**
  241. * 通过
  242. * @param $id
  243. * @return mixed
  244. */
  245. public function adopt(int $id)
  246. {
  247. $extract = $this->getExtract($id);
  248. if (!$extract) {
  249. throw new AdminException('操作记录不存!');
  250. }
  251. if ($extract->status == 1 || $extract->status == 2) {
  252. throw new AdminException('您已提现,请勿重复提现!');
  253. }
  254. if ($extract->status == -1) {
  255. throw new AdminException('您的提现申请已被拒绝!');
  256. }
  257. if ($this->changeSuccess($id, $extract)) {
  258. return true;
  259. } else {
  260. throw new AdminException('操作失败!');
  261. }
  262. }
  263. /**待提现的数量
  264. * @return int
  265. */
  266. public function userExtractCount()
  267. {
  268. return $this->dao->count(['status' => 0]);
  269. }
  270. /**
  271. * 银行卡提现
  272. * @param int $uid
  273. * @return mixed
  274. */
  275. public function bank(int $uid)
  276. {
  277. /** @var UserServices $userService */
  278. $userService = app()->make(UserServices::class);
  279. $user = $userService->getUserInfo($uid);
  280. if (!$user) {
  281. throw new ValidateException('数据不存在');
  282. }
  283. $data['energy'] = $user['energy'];
  284. //可提现佣金
  285. $data['minEnergy'] = sys_config('user_exchange_min_energy');//提现最低金额
  286. $data['exchange_fee'] = sys_config('exchange_fee');//提现手续费
  287. $data['stock_price'] = sys_config('stock_price');//提现手续费
  288. return $data;
  289. }
  290. /**
  291. * 提现申请
  292. * @param int $uid
  293. * @param array $data
  294. */
  295. public function cash(int $uid, array $data)
  296. {
  297. /** @var UserServices $userService */
  298. $userService = app()->make(UserServices::class);
  299. $user = $userService->getUserInfo($uid);
  300. if (!$user) {
  301. throw new ValidateException('数据不存在');
  302. }
  303. if ($user['is_auth'] != 2) throw new ValidateException('请先完成实名认证');
  304. if ($data['name'] != $user['real_name']) throw new ValidateException('转换用户与实名认证不符');
  305. $data['energy'] = $user['energy'];
  306. if ($data['money'] > $data['energy']) {
  307. throw new ValidateException('可转换能量值不足');
  308. }
  309. $extractPrice = $user['energy'];
  310. $userExtractMinPrice = sys_config('user_exchange_min_energy');
  311. if ($data['money'] < $userExtractMinPrice) {
  312. throw new ValidateException('转换能量值不能小于' . $userExtractMinPrice);
  313. }
  314. if ($extractPrice < 0) {
  315. throw new ValidateException('转换能量值不足' . $data['money']);
  316. }
  317. if ($data['money'] > $extractPrice) {
  318. throw new ValidateException('转换能量值不足' . $data['money']);
  319. }
  320. if ($data['money'] <= 0) {
  321. throw new ValidateException('转换能量值大于0');
  322. }
  323. $fee = sys_config('exchange_fee');
  324. $stock_price = sys_config('stock_price');
  325. if ($stock_price <= 0) throw new ValidateException('暂不支持转换股份');
  326. $extract_fee = bcdiv(bcmul($fee, $data['money'], 2), '100', 2);
  327. if ($extract_fee < 0) $extract_fee = 0;
  328. $insertData = [
  329. 'uid' => $user['uid'],
  330. 'extract_num' => bcsub((string)$data['money'], (string)$extract_fee, 2),
  331. 'extract_fee' => $extract_fee,
  332. 'extract_price' => $stock_price,
  333. 'exchange_num' => bcdiv(bcsub((string)$data['money'], (string)$extract_fee, 2), (string)$stock_price, 2),
  334. 'add_time' => time(),
  335. 'balance' => $user['energy'],
  336. 'status' => 0,
  337. ];
  338. $insertData['real_name'] = $data['name'];
  339. $insertData['bank_code'] = $data['cardnum'];
  340. $insertData['bank_address'] = $data['bankname'];
  341. $mark = '转换' . $data['money'] . '能量值,扣除手续费后实际转换' . $insertData['extract_num'] . ',转换时股份价格' . $stock_price . ',共转换' . $insertData['exchange_num'] . '股';
  342. /** @var UserBillServices $userBrokerageServices */
  343. $userBrokerageServices = app()->make(UserBillServices::class);
  344. $res1 = $this->transaction(function () use ($insertData, $data, $uid, $userService, $user, $userBrokerageServices, $mark) {
  345. if (!$res1 = $this->dao->save($insertData)) {
  346. throw new ValidateException('转换失败');
  347. }
  348. //修改用户佣金
  349. $balance = bcsub((string)$user['energy'], (string)$data['money'], 2) ?? 0;
  350. if (!$userService->update($uid, ['energy' => $balance], 'uid')) {
  351. throw new ValidateException('修改用户信息失败');
  352. }
  353. //保存佣金记录
  354. $userBrokerageServices->income('exchange', $uid, ['mark' => $mark, 'number' => $data['money']], $balance, $res1['id']);
  355. return $res1;
  356. });
  357. return true;
  358. }
  359. /**
  360. * @param array $where
  361. * @param string $SumField
  362. * @param string $selectType
  363. * @param string $group
  364. * @return float|mixed
  365. */
  366. public function getOutMoneyByWhere(array $where, string $SumField, string $selectType, string $group = "")
  367. {
  368. switch ($selectType) {
  369. case "sum" :
  370. return $this->dao->getWhereSumField($where, $SumField);
  371. case "group" :
  372. return $this->dao->getGroupField($where, $SumField, $group);
  373. }
  374. }
  375. }