UserExchangeServices.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. $item['real_name'] = $item['user']['real_name'] ?? '';
  90. $item['card_id'] = $item['user']['card_id'] ?? '';
  91. $item['phone'] = $item['user']['phone'] ?? '';
  92. }
  93. $count = $this->dao->count($where);
  94. return compact('list', 'count');
  95. }
  96. /**
  97. * 获取提现总数
  98. * @param array $where
  99. */
  100. public function getExtractSum(array $where)
  101. {
  102. return $this->dao->getExtractMoneyByWhere($where, 'extract_num');
  103. }
  104. /**
  105. * 拒绝提现申请
  106. * @param $id
  107. * @param $fail_msg
  108. * @return bool
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. * @throws \think\exception\DbException
  112. */
  113. public function changeFail(int $id, $userExtract, $message)
  114. {
  115. $fail_time = time();
  116. $extract_number = bcadd((string)$userExtract['extract_num'], (string)$userExtract['extract_fee'], 2);
  117. $mark = '换股份失败,退回能量值' . $extract_number;
  118. $uid = $userExtract['uid'];
  119. $status = -1;
  120. /** @var UserServices $userServices */
  121. $userServices = app()->make(UserServices::class);
  122. $user = $userServices->getUserInfo($uid);
  123. if (!$user) {
  124. throw new ValidateException('用户不存在');
  125. }
  126. /** @var UserBillServices $userBrokerageServices */
  127. $userBrokerageServices = app()->make(UserBillServices::class);
  128. $this->transaction(function () use ($user, $userBrokerageServices, $uid, $id, $extract_number, $message, $userServices, $status, $fail_time) {
  129. $now_brokerage = bcadd((string)$user['energy'], (string)$extract_number, 2);
  130. //增加佣金记录
  131. $userBrokerageServices->income('exchange_fail', $uid, $extract_number, $now_brokerage, $id);
  132. //修改用户佣金
  133. if (!$userServices->update($uid, ['energy' => $now_brokerage], 'uid'))
  134. throw new AdminException('增加用户能量失败');
  135. if (!$this->dao->update($id, ['fail_time' => $fail_time, 'fail_msg' => $message, 'status' => $status])) {
  136. throw new AdminException('修改失败');
  137. }
  138. });
  139. //消息推送
  140. return true;
  141. }
  142. /**
  143. * 通过提现申请
  144. * @param $id
  145. * @return bool
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. * @throws \think\exception\DbException
  149. */
  150. public function changeSuccess(int $id, $userExtract)
  151. {
  152. $this->transaction(function () use ($id, $userExtract) {
  153. if (!$this->dao->update($id, ['status' => 1])) {
  154. throw new AdminException('修改失败');
  155. }
  156. });
  157. return true;
  158. }
  159. /**
  160. * 显示资源列表
  161. * @param array $where
  162. * @return array
  163. * @throws \think\db\exception\DataNotFoundException
  164. * @throws \think\db\exception\DbException
  165. * @throws \think\db\exception\ModelNotFoundException
  166. */
  167. public function index(array $where)
  168. {
  169. $list = $this->getUserExtractList($where);
  170. //待提现金额
  171. $where['status'] = 0;
  172. $extract_statistics['price'] = $this->getExtractSum($where);
  173. //已提现金额
  174. $where['status'] = 1;
  175. $extract_statistics['priced'] = $this->getExtractSum($where);
  176. //佣金总金额
  177. return compact('extract_statistics', 'list');
  178. }
  179. /**
  180. * 显示资源列表
  181. * @param array $where
  182. * @return array
  183. * @throws \think\db\exception\DataNotFoundException
  184. * @throws \think\db\exception\DbException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. */
  187. public function getExportList(array $where)
  188. {
  189. $list = $this->dao->getExtractAll($where);
  190. return $list;
  191. }
  192. /**
  193. * 显示编辑资源表单页.
  194. *
  195. * @param int $id
  196. * @return \think\Response
  197. */
  198. public function edit(int $id)
  199. {
  200. $UserExtract = $this->getExtract($id);
  201. if (!$UserExtract) {
  202. throw new AdminException('数据不存在!');
  203. }
  204. $f = array();
  205. $f[] = Form::input('real_name', '姓名', $UserExtract['real_name']);
  206. $f[] = Form::number('extract_num', '转换能量', (float)$UserExtract['extract_num'])->precision(2)->disabled(true);
  207. $f[] = Form::number('extract_price', '股份价格', (float)$UserExtract['extract_price'])->precision(2)->disabled(true);
  208. $f[] = Form::number('exchange_num', '转换股份', (float)$UserExtract['exchange_num'])->precision(2)->disabled(true);
  209. $f[] = Form::input('account', '账号', $UserExtract['account']);
  210. $f[] = Form::input('bank_code', '银行卡号', $UserExtract['bank_code']);
  211. $f[] = Form::input('bank_address', '开户行', $UserExtract['bank_address']);
  212. $f[] = Form::input('mark', '备注', $UserExtract['mark'])->type('textarea');
  213. return create_form('编辑', $f, Url::buildUrl('/finance/exchange/' . $id), 'PUT');
  214. }
  215. public function update(int $id, array $data)
  216. {
  217. if (!$this->dao->update($id, $data))
  218. throw new AdminException('修改失败');
  219. else
  220. return true;
  221. }
  222. /**
  223. * 拒绝
  224. * @param $id
  225. * @return mixed
  226. */
  227. public function refuse(int $id, string $message)
  228. {
  229. $extract = $this->getExtract($id);
  230. if (!$extract) {
  231. throw new AdminException('操作记录不存在!');
  232. }
  233. if ($extract->status == 1) {
  234. throw new AdminException('已经提现,错误操作');
  235. }
  236. if ($extract->status == -1) {
  237. throw new AdminException('您的提现申请已被拒绝,请勿重复操作!');
  238. }
  239. $res = $this->changeFail($id, $extract, $message);
  240. if ($res) {
  241. return true;
  242. } else {
  243. throw new AdminException('操作失败!');
  244. }
  245. }
  246. /**
  247. * 通过
  248. * @param $id
  249. * @return mixed
  250. */
  251. public function adopt(int $id)
  252. {
  253. $extract = $this->getExtract($id);
  254. if (!$extract) {
  255. throw new AdminException('操作记录不存!');
  256. }
  257. if ($extract->status == 1 || $extract->status == 2) {
  258. throw new AdminException('您已提现,请勿重复提现!');
  259. }
  260. if ($extract->status == -1) {
  261. throw new AdminException('您的提现申请已被拒绝!');
  262. }
  263. if ($this->changeSuccess($id, $extract)) {
  264. return true;
  265. } else {
  266. throw new AdminException('操作失败!');
  267. }
  268. }
  269. /**待提现的数量
  270. * @return int
  271. */
  272. public function userExtractCount()
  273. {
  274. return $this->dao->count(['status' => 0]);
  275. }
  276. /**
  277. * 银行卡提现
  278. * @param int $uid
  279. * @return mixed
  280. */
  281. public function bank(int $uid)
  282. {
  283. /** @var UserServices $userService */
  284. $userService = app()->make(UserServices::class);
  285. $user = $userService->getUserInfo($uid);
  286. if (!$user) {
  287. throw new ValidateException('数据不存在');
  288. }
  289. $data['energy'] = $user['energy'];
  290. //可提现佣金
  291. $data['minEnergy'] = sys_config('user_exchange_min_energy');//提现最低金额
  292. $data['exchange_fee'] = sys_config('exchange_fee');//提现手续费
  293. $data['stock_price'] = sys_config('stock_price');//提现手续费
  294. $data['bank'] = $this->dao->getLastOne($uid);
  295. return $data;
  296. }
  297. /**
  298. * 提现申请
  299. * @param int $uid
  300. * @param array $data
  301. */
  302. public function cash(int $uid, array $data)
  303. {
  304. /** @var UserServices $userService */
  305. $userService = app()->make(UserServices::class);
  306. $user = $userService->getUserInfo($uid);
  307. if (date('w') != 6 && date('w') != 0 && date('w') != 7) {
  308. throw new ValidateException('兑换只能在周末申请');
  309. }
  310. if (!$user) {
  311. throw new ValidateException('数据不存在');
  312. }
  313. if ($user['is_auth'] != 2) throw new ValidateException('请先完成实名认证');
  314. if ($data['name'] != $user['real_name']) throw new ValidateException('转换用户与实名认证不符');
  315. $data['money'] = $data['energy'];
  316. if ($data['money'] > $user['energy']) {
  317. throw new ValidateException('可转换能量值不足');
  318. }
  319. $extractPrice = $user['energy'];
  320. $userExtractMinPrice = sys_config('user_exchange_min_energy');
  321. if ($data['money'] < $userExtractMinPrice) {
  322. throw new ValidateException('转换能量值不能小于' . $userExtractMinPrice);
  323. }
  324. if ($extractPrice < 0) {
  325. throw new ValidateException('转换能量值不足' . $data['money']);
  326. }
  327. if ($data['money'] > $extractPrice) {
  328. throw new ValidateException('转换能量值不足' . $data['money']);
  329. }
  330. if ($data['money'] <= 0) {
  331. throw new ValidateException('转换能量值大于0');
  332. }
  333. $fee = sys_config('exchange_fee');
  334. $stock_price = sys_config('stock_price');
  335. if ($stock_price <= 0) throw new ValidateException('暂不支持转换股份');
  336. $extract_fee = bcdiv(bcmul($fee, $data['money'], 2), '100', 2);
  337. if ($extract_fee < 0) $extract_fee = 0;
  338. $insertData = [
  339. 'uid' => $user['uid'],
  340. 'account' => $data['account'],
  341. 'extract_num' => bcsub((string)$data['money'], (string)$extract_fee, 2),
  342. 'extract_fee' => $extract_fee,
  343. 'extract_price' => $stock_price,
  344. 'exchange_num' => bcdiv(bcsub((string)$data['money'], (string)$extract_fee, 2), (string)$stock_price, 2),
  345. 'add_time' => time(),
  346. 'balance' => $user['energy'],
  347. 'status' => 0,
  348. ];
  349. $insertData['real_name'] = $data['name'];
  350. $insertData['bank_code'] = $data['cardnum'];
  351. $insertData['bank_address'] = $data['bankname'];
  352. $mark = '转换' . $data['money'] . '能量值,扣除手续费后实际转换' . $insertData['extract_num'] . ',转换时份额价格' . $stock_price . ',共转换' . $insertData['exchange_num'] . '份额';
  353. /** @var UserBillServices $userBrokerageServices */
  354. $userBrokerageServices = app()->make(UserBillServices::class);
  355. $res1 = $this->transaction(function () use ($insertData, $data, $uid, $userService, $user, $userBrokerageServices, $mark) {
  356. if (!$res1 = $this->dao->save($insertData)) {
  357. throw new ValidateException('转换失败');
  358. }
  359. //修改用户佣金
  360. $balance = bcsub((string)$user['energy'], (string)$data['money'], 2) ?? 0;
  361. if (!$userService->update($uid, ['energy' => $balance], 'uid')) {
  362. throw new ValidateException('修改用户信息失败');
  363. }
  364. //保存佣金记录
  365. $userBrokerageServices->income('exchange', $uid, ['mark' => $mark, 'number' => $data['money']], $balance, $res1['id']);
  366. return $res1;
  367. });
  368. return true;
  369. }
  370. /**
  371. * @param array $where
  372. * @param string $SumField
  373. * @param string $selectType
  374. * @param string $group
  375. * @return float|mixed
  376. */
  377. public function getOutMoneyByWhere(array $where, string $SumField, string $selectType, string $group = "")
  378. {
  379. switch ($selectType) {
  380. case "sum" :
  381. return $this->dao->getWhereSumField($where, $SumField);
  382. case "group" :
  383. return $this->dao->getGroupField($where, $SumField, $group);
  384. }
  385. }
  386. /**
  387. * 导入佣金到余额
  388. * @param int $uid
  389. * @param $price
  390. * @return bool
  391. */
  392. public function importEnergy(int $uid, $price)
  393. {
  394. $switch = sys_config('brokerage_to_energy_switch', 1);
  395. if (!$switch) throw new ValidateException('暂不支持');
  396. /** @var UserServices $userServices */
  397. $userServices = app()->make(UserServices::class);
  398. $user = $userServices->getUserInfo($uid);
  399. if (!$user) {
  400. throw new ValidateException('数据不存在');
  401. }
  402. /** @var UserBrokerageServices $userBrokerageServices */
  403. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  404. $broken_commission = $userBrokerageServices->getUserFrozenPrice($uid);
  405. $commissionCount = bcsub((string)$user['brokerage_price'], (string)$broken_commission, 2);
  406. if ($price > $commissionCount) {
  407. throw new ValidateException('转入金额不能大于可提现佣金!');
  408. }
  409. return $this->transaction(function () use ($uid, $user, $price, $userServices) {
  410. $edit_data = [];
  411. $edit_data['energy'] = bcadd((string)$user['energy'], (string)$price, 2);
  412. $edit_data['brokerage_price'] = $user['brokerage_price'] > $price ? bcsub((string)$user['brokerage_price'], (string)$price, 2) : 0;
  413. //修改用户佣金、余额信息
  414. $res1 = $userServices->update($uid, $edit_data, 'uid');
  415. /** @var UserBillServices $userMoneyServices */
  416. $userMoneyServices = app()->make(UserBillServices::class);
  417. //余额记录
  418. $res2 = $userMoneyServices->income('brokerage_to_energy', $uid, $price, $edit_data['energy'], 0);
  419. //佣金提现记录
  420. /** @var UserBrokerageServices $userBrokerageServices */
  421. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  422. $res3 = $userBrokerageServices->income('brokerage_to_energy', $uid, $price, $edit_data['brokerage_price'], 0);
  423. return $res1 && $res2 && $res3;
  424. });
  425. }
  426. }