UserExchangeServices.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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('bank_code', '银行卡号', $UserExtract['bank_code']);
  210. $f[] = Form::input('bank_address', '开户行', $UserExtract['bank_address']);
  211. $f[] = Form::input('mark', '备注', $UserExtract['mark'])->type('textarea');
  212. return create_form('编辑', $f, Url::buildUrl('/finance/exchange/' . $id), 'PUT');
  213. }
  214. public function update(int $id, array $data)
  215. {
  216. if (!$this->dao->update($id, $data))
  217. throw new AdminException('修改失败');
  218. else
  219. return true;
  220. }
  221. /**
  222. * 拒绝
  223. * @param $id
  224. * @return mixed
  225. */
  226. public function refuse(int $id, string $message)
  227. {
  228. $extract = $this->getExtract($id);
  229. if (!$extract) {
  230. throw new AdminException('操作记录不存在!');
  231. }
  232. if ($extract->status == 1) {
  233. throw new AdminException('已经提现,错误操作');
  234. }
  235. if ($extract->status == -1) {
  236. throw new AdminException('您的提现申请已被拒绝,请勿重复操作!');
  237. }
  238. $res = $this->changeFail($id, $extract, $message);
  239. if ($res) {
  240. return true;
  241. } else {
  242. throw new AdminException('操作失败!');
  243. }
  244. }
  245. /**
  246. * 通过
  247. * @param $id
  248. * @return mixed
  249. */
  250. public function adopt(int $id)
  251. {
  252. $extract = $this->getExtract($id);
  253. if (!$extract) {
  254. throw new AdminException('操作记录不存!');
  255. }
  256. if ($extract->status == 1 || $extract->status == 2) {
  257. throw new AdminException('您已提现,请勿重复提现!');
  258. }
  259. if ($extract->status == -1) {
  260. throw new AdminException('您的提现申请已被拒绝!');
  261. }
  262. if ($this->changeSuccess($id, $extract)) {
  263. return true;
  264. } else {
  265. throw new AdminException('操作失败!');
  266. }
  267. }
  268. /**待提现的数量
  269. * @return int
  270. */
  271. public function userExtractCount()
  272. {
  273. return $this->dao->count(['status' => 0]);
  274. }
  275. /**
  276. * 银行卡提现
  277. * @param int $uid
  278. * @return mixed
  279. */
  280. public function bank(int $uid)
  281. {
  282. /** @var UserServices $userService */
  283. $userService = app()->make(UserServices::class);
  284. $user = $userService->getUserInfo($uid);
  285. if (!$user) {
  286. throw new ValidateException('数据不存在');
  287. }
  288. $data['energy'] = $user['energy'];
  289. //可提现佣金
  290. $data['minEnergy'] = sys_config('user_exchange_min_energy');//提现最低金额
  291. $data['exchange_fee'] = sys_config('exchange_fee');//提现手续费
  292. $data['stock_price'] = sys_config('stock_price');//提现手续费
  293. $data['bank'] = $this->dao->getLastOne($uid);
  294. return $data;
  295. }
  296. /**
  297. * 提现申请
  298. * @param int $uid
  299. * @param array $data
  300. */
  301. public function cash(int $uid, array $data)
  302. {
  303. /** @var UserServices $userService */
  304. $userService = app()->make(UserServices::class);
  305. $user = $userService->getUserInfo($uid);
  306. if (date('w') != 6 && date('w') != 0 && date('w') != 7) {
  307. throw new ValidateException('兑换只能在周末申请');
  308. }
  309. if (!$user) {
  310. throw new ValidateException('数据不存在');
  311. }
  312. if ($user['is_auth'] != 2) throw new ValidateException('请先完成实名认证');
  313. if ($data['name'] != $user['real_name']) throw new ValidateException('转换用户与实名认证不符');
  314. $data['money'] = $data['energy'];
  315. if ($data['money'] > $user['energy']) {
  316. throw new ValidateException('可转换能量值不足');
  317. }
  318. $extractPrice = $user['energy'];
  319. $userExtractMinPrice = sys_config('user_exchange_min_energy');
  320. if ($data['money'] < $userExtractMinPrice) {
  321. throw new ValidateException('转换能量值不能小于' . $userExtractMinPrice);
  322. }
  323. if ($extractPrice < 0) {
  324. throw new ValidateException('转换能量值不足' . $data['money']);
  325. }
  326. if ($data['money'] > $extractPrice) {
  327. throw new ValidateException('转换能量值不足' . $data['money']);
  328. }
  329. if ($data['money'] <= 0) {
  330. throw new ValidateException('转换能量值大于0');
  331. }
  332. $fee = sys_config('exchange_fee');
  333. $stock_price = sys_config('stock_price');
  334. if ($stock_price <= 0) throw new ValidateException('暂不支持转换股份');
  335. $extract_fee = bcdiv(bcmul($fee, $data['money'], 2), '100', 2);
  336. if ($extract_fee < 0) $extract_fee = 0;
  337. $insertData = [
  338. 'uid' => $user['uid'],
  339. 'extract_num' => bcsub((string)$data['money'], (string)$extract_fee, 2),
  340. 'extract_fee' => $extract_fee,
  341. 'extract_price' => $stock_price,
  342. 'exchange_num' => bcdiv(bcsub((string)$data['money'], (string)$extract_fee, 2), (string)$stock_price, 2),
  343. 'add_time' => time(),
  344. 'balance' => $user['energy'],
  345. 'status' => 0,
  346. ];
  347. $insertData['real_name'] = $data['name'];
  348. $insertData['bank_code'] = $data['cardnum'];
  349. $insertData['bank_address'] = $data['bankname'];
  350. $mark = '转换' . $data['money'] . '能量值,扣除手续费后实际转换' . $insertData['extract_num'] . ',转换时份额价格' . $stock_price . ',共转换' . $insertData['exchange_num'] . '份额';
  351. /** @var UserBillServices $userBrokerageServices */
  352. $userBrokerageServices = app()->make(UserBillServices::class);
  353. $res1 = $this->transaction(function () use ($insertData, $data, $uid, $userService, $user, $userBrokerageServices, $mark) {
  354. if (!$res1 = $this->dao->save($insertData)) {
  355. throw new ValidateException('转换失败');
  356. }
  357. //修改用户佣金
  358. $balance = bcsub((string)$user['energy'], (string)$data['money'], 2) ?? 0;
  359. if (!$userService->update($uid, ['energy' => $balance], 'uid')) {
  360. throw new ValidateException('修改用户信息失败');
  361. }
  362. //保存佣金记录
  363. $userBrokerageServices->income('exchange', $uid, ['mark' => $mark, 'number' => $data['money']], $balance, $res1['id']);
  364. return $res1;
  365. });
  366. return true;
  367. }
  368. /**
  369. * @param array $where
  370. * @param string $SumField
  371. * @param string $selectType
  372. * @param string $group
  373. * @return float|mixed
  374. */
  375. public function getOutMoneyByWhere(array $where, string $SumField, string $selectType, string $group = "")
  376. {
  377. switch ($selectType) {
  378. case "sum" :
  379. return $this->dao->getWhereSumField($where, $SumField);
  380. case "group" :
  381. return $this->dao->getGroupField($where, $SumField, $group);
  382. }
  383. }
  384. /**
  385. * 导入佣金到余额
  386. * @param int $uid
  387. * @param $price
  388. * @return bool
  389. */
  390. public function importEnergy(int $uid, $price)
  391. {
  392. $switch = sys_config('brokerage_to_energy_switch', 1);
  393. if (!$switch) throw new ValidateException('暂不支持');
  394. /** @var UserServices $userServices */
  395. $userServices = app()->make(UserServices::class);
  396. $user = $userServices->getUserInfo($uid);
  397. if (!$user) {
  398. throw new ValidateException('数据不存在');
  399. }
  400. /** @var UserBrokerageServices $userBrokerageServices */
  401. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  402. $broken_commission = $userBrokerageServices->getUserFrozenPrice($uid);
  403. $commissionCount = bcsub((string)$user['brokerage_price'], (string)$broken_commission, 2);
  404. if ($price > $commissionCount) {
  405. throw new ValidateException('转入金额不能大于可提现佣金!');
  406. }
  407. return $this->transaction(function () use ($uid, $user, $price, $userServices) {
  408. $edit_data = [];
  409. $edit_data['energy'] = bcadd((string)$user['energy'], (string)$price, 2);
  410. $edit_data['brokerage_price'] = $user['brokerage_price'] > $price ? bcsub((string)$user['brokerage_price'], (string)$price, 2) : 0;
  411. //修改用户佣金、余额信息
  412. $res1 = $userServices->update($uid, $edit_data, 'uid');
  413. /** @var UserBillServices $userMoneyServices */
  414. $userMoneyServices = app()->make(UserBillServices::class);
  415. //余额记录
  416. $res2 = $userMoneyServices->income('brokerage_to_energy', $uid, $price, $edit_data['energy'], 0);
  417. //佣金提现记录
  418. /** @var UserBrokerageServices $userBrokerageServices */
  419. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  420. $res3 = $userBrokerageServices->income('brokerage_to_energy', $uid, $price, $edit_data['brokerage_price'], 0);
  421. return $res1 && $res2 && $res3;
  422. });
  423. }
  424. }