UserExtractServices.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\UserExtractDao;
  14. use app\services\BaseServices;
  15. use app\services\order\StoreOrderCreateServices;
  16. use app\services\statistic\CapitalFlowServices;
  17. use app\services\system\admin\SystemAdminServices;
  18. use app\services\wechat\WechatUserServices;
  19. use crmeb\exceptions\AdminException;
  20. use crmeb\exceptions\ApiException;
  21. use crmeb\services\FormBuilder as Form;
  22. use crmeb\services\app\WechatService;
  23. use crmeb\services\workerman\ChannelService;
  24. use think\facade\Route as Url;
  25. /**
  26. *
  27. * Class UserExtractServices
  28. * @package app\services\user
  29. */
  30. class UserExtractServices extends BaseServices
  31. {
  32. /**
  33. * UserExtractServices constructor.
  34. * @param UserExtractDao $dao
  35. */
  36. public function __construct(UserExtractDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**
  41. * 获取一条提现记录
  42. * @param int $id
  43. * @param array $field
  44. * @return array|\think\Model|null
  45. */
  46. public function getExtract(int $id, array $field = [])
  47. {
  48. return $this->dao->get($id, $field);
  49. }
  50. /**
  51. * 获取某个用户提现总数
  52. * @param int $uid
  53. * @return float
  54. */
  55. public function getUserExtract(int $uid)
  56. {
  57. return $this->dao->getWhereSum(['uid' => $uid, 'status' => 1]);
  58. }
  59. /**
  60. * 获取某些用户的提现总数列表
  61. * @param array $uids
  62. */
  63. public function getUsersSumList(array $uids)
  64. {
  65. return $this->dao->getWhereSumList(['uid' => $uids, 'status' => 1]);
  66. }
  67. public function getCount(array $where = [])
  68. {
  69. return $this->dao->getCount($where);
  70. }
  71. /**
  72. * 获取提现列表
  73. * @param array $where
  74. * @param string $field
  75. * @return array
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function getUserExtractList(array $where, string $field = '*')
  81. {
  82. [$page, $limit] = $this->getPageValue();
  83. $list = $this->dao->getExtractList($where, $field, $page, $limit);
  84. foreach ($list as &$item) {
  85. $item['nickname'] = $item['user']['nickname'] ?? '';
  86. }
  87. $count = $this->dao->count($where);
  88. return compact('list', 'count');
  89. }
  90. /**
  91. * 获取提现总数
  92. * @param array $where
  93. */
  94. public function getExtractSum(array $where)
  95. {
  96. return $this->dao->getExtractMoneyByWhere($where, 'extract_price');
  97. }
  98. /**
  99. * 拒绝提现申请
  100. * @param $id
  101. * @param $fail_msg
  102. * @return bool
  103. * @throws \think\db\exception\DataNotFoundException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. * @throws \think\exception\DbException
  106. */
  107. public function changeFail(int $id, $userExtract, $message)
  108. {
  109. $fail_time = time();
  110. $extract_number = $userExtract['extract_price'];
  111. $mark = '提现失败,退回佣金' . $extract_number . '元';
  112. $uid = $userExtract['uid'];
  113. $status = -1;
  114. /** @var UserServices $userServices */
  115. $userServices = app()->make(UserServices::class);
  116. $user = $userServices->getUserInfo($uid);
  117. $this->transaction(function () use ($user, $uid, $id, $extract_number, $message, $userServices, $status, $fail_time) {
  118. //增加佣金记录
  119. /** @var UserBrokerageServices $userBrokerageServices */
  120. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  121. $now_brokerage = bcadd((string)$user['brokerage_price'], (string)$extract_number, 2);
  122. $userBrokerageServices->income('extract_fail', $uid, $extract_number, $now_brokerage, $id);
  123. if (!$userServices->update($uid, ['brokerage_price' => bcadd((string)$user['brokerage_price'], (string)$extract_number, 2)], 'uid'))
  124. throw new AdminException(400657);
  125. if (!$this->dao->update($id, ['fail_time' => $fail_time, 'fail_msg' => $message, 'status' => $status])) {
  126. throw new AdminException(100007);
  127. }
  128. });
  129. event('notice.notice', [['uid' => $uid, 'userType' => strtolower($user['user_type']), 'extract_number' => $extract_number, 'nickname' => $user['nickname'], 'message' => $message], 'user_balance_change']);
  130. return true;
  131. }
  132. /**
  133. * 通过提现申请
  134. * @param $id
  135. * @return bool
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. * @throws \think\exception\DbException
  139. */
  140. public function changeSuccess(int $id, $userExtract)
  141. {
  142. $extractNumber = $userExtract['extract_price'];
  143. /** @var WechatUserServices $wechatServices */
  144. $wechatServices = app()->make(WechatUserServices::class);
  145. /** @var UserServices $userServices */
  146. $userServices = app()->make(UserServices::class);
  147. $userType = $userServices->value(['uid' => $userExtract['uid']], 'user_type');
  148. $nickname = $userServices->value(['uid' => $userExtract['uid']], 'nickname');
  149. $phone = $userServices->value(['uid' => $userExtract['uid']], 'phone');
  150. event('notice.notice', [['uid' => $userExtract['uid'], 'userType' => strtolower($userType), 'extractNumber' => $extractNumber, 'nickname' => $nickname], 'user_extract']);
  151. if (!$this->dao->update($id, ['status' => 1])) {
  152. throw new AdminException(100007);
  153. }
  154. switch ($userExtract['extract_type']) {
  155. case 'bank':
  156. $order_id = $userExtract['bank_code'];
  157. break;
  158. case 'weixin':
  159. $order_id = $userExtract['wechat'];
  160. break;
  161. case 'alipay':
  162. $order_id = $userExtract['alipay_code'];
  163. break;
  164. default:
  165. $order_id = '';
  166. break;
  167. }
  168. $insertData = ['order_id' => $order_id, 'nickname' => $nickname, 'phone' => $phone];
  169. $openid = $wechatServices->getWechatOpenid($userExtract['uid'], 'wechat');
  170. //自动提现到零钱
  171. if ($userExtract['extract_type'] == 'weixin' && sys_config('brokerage_type', 0) && $openid) {
  172. /** @var StoreOrderCreateServices $services */
  173. $services = app()->make(StoreOrderCreateServices::class);
  174. $insertData['order_id'] = $services->getNewOrderId();
  175. // 微信提现
  176. $res = WechatService::merchantPay($openid, $insertData['order_id'], $userExtract['extract_price'], '提现佣金到零钱');
  177. if (!$res) {
  178. throw new ApiException(400658);
  179. }
  180. // 更新 提现申请记录 wechat_order_id
  181. $this->dao->update($id, ['wechat_order_id' => $insertData['order_id']]);
  182. /** @var UserServices $userService */
  183. $userService = app()->make(UserServices::class);
  184. $user = $userService->getUserInfo($userExtract['uid']);
  185. $insertData['nickname'] = $user['nickname'];
  186. $insertData['phone'] = $user['phone'];
  187. }
  188. /** @var CapitalFlowServices $capitalFlowServices */
  189. $capitalFlowServices = app()->make(CapitalFlowServices::class);
  190. $capitalFlowServices->setFlow([
  191. 'order_id' => $insertData['order_id'],
  192. 'uid' => $userExtract['uid'],
  193. 'price' => bcmul('-1', $extractNumber, 2),
  194. 'pay_type' => $userExtract['extract_type'],
  195. 'nickname' => $insertData['nickname'],
  196. 'phone' => $insertData['phone']
  197. ], 'extract');
  198. return true;
  199. }
  200. /**
  201. * 显示资源列表
  202. * @param array $where
  203. * @return array
  204. * @throws \think\db\exception\DataNotFoundException
  205. * @throws \think\db\exception\DbException
  206. * @throws \think\db\exception\ModelNotFoundException
  207. */
  208. public function index(array $where)
  209. {
  210. $list = $this->getUserExtractList($where);
  211. /** @var UserServices $userServices */
  212. $userServices = app()->make(UserServices::class);
  213. //待提现金额
  214. $where['status'] = 0;
  215. $extract_statistics['price'] = $this->getExtractSum($where);
  216. //已提现金额
  217. $where['status'] = 1;
  218. $extract_statistics['priced'] = $this->getExtractSum($where);
  219. /** @var UserBrokerageServices $userBrokerageServices */
  220. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  221. $extract_statistics['brokerage_count'] = $userBrokerageServices->getUsersBokerageSum($where);
  222. //未提现金额
  223. $extract_statistics['brokerage_not'] = $extract_statistics['brokerage_count'] > $extract_statistics['priced'] ? bcsub((string)$extract_statistics['brokerage_count'], (string)$extract_statistics['priced'], 2) : 0.00;
  224. return compact('extract_statistics', 'list');
  225. }
  226. /**
  227. * 显示编辑资源表单页.
  228. *
  229. * @param int $id
  230. * @return \think\Response
  231. */
  232. public function edit(int $id)
  233. {
  234. $UserExtract = $this->getExtract($id);
  235. if (!$UserExtract) {
  236. throw new AdminException(100026);
  237. }
  238. $f = array();
  239. $f[] = Form::input('real_name', '姓名', $UserExtract['real_name']);
  240. $f[] = Form::number('extract_price', '提现金额', (float)$UserExtract['extract_price'])->precision(2)->disabled(true);
  241. if ($UserExtract['extract_type'] == 'alipay') {
  242. $f[] = Form::input('alipay_code', '支付宝账号', $UserExtract['alipay_code']);
  243. } else if ($UserExtract['extract_type'] == 'weixin') {
  244. $f[] = Form::input('wechat', '微信号', $UserExtract['wechat']);
  245. } else {
  246. $f[] = Form::input('bank_code', '银行卡号', $UserExtract['bank_code']);
  247. $f[] = Form::input('bank_address', '开户行', $UserExtract['bank_address']);
  248. }
  249. $f[] = Form::input('mark', '备注', $UserExtract['mark'])->type('textarea');
  250. return create_form('编辑', $f, Url::buildUrl('/finance/extract/' . $id), 'PUT');
  251. }
  252. public function update(int $id, array $data)
  253. {
  254. if (!$this->dao->update($id, $data))
  255. throw new AdminException(100007);
  256. else
  257. return true;
  258. }
  259. /**
  260. * 拒绝
  261. * @param $id
  262. * @return mixed
  263. */
  264. public function refuse(int $id, string $message)
  265. {
  266. $extract = $this->getExtract($id);
  267. if (!$extract) {
  268. throw new AdminException(100026);
  269. }
  270. if ($extract->status == 1) {
  271. throw new AdminException(400659);
  272. }
  273. if ($extract->status == -1) {
  274. throw new AdminException(400660);
  275. }
  276. $res = $this->changeFail($id, $extract, $message);
  277. if ($res) {
  278. return true;
  279. } else {
  280. throw new AdminException(100005);
  281. }
  282. }
  283. /**
  284. * 通过
  285. * @param $id
  286. * @return mixed
  287. */
  288. public function adopt(int $id)
  289. {
  290. $extract = $this->getExtract($id);
  291. if (!$extract) {
  292. throw new AdminException(100026);
  293. }
  294. if ($extract->status == 1) {
  295. throw new AdminException(400659);
  296. }
  297. if ($extract->status == -1) {
  298. throw new AdminException(400660);
  299. }
  300. if ($this->changeSuccess($id, $extract)) {
  301. return true;
  302. } else {
  303. throw new AdminException(100005);
  304. }
  305. }
  306. /**待提现的数量
  307. * @return int
  308. */
  309. public function userExtractCount()
  310. {
  311. return $this->dao->count(['status' => 0]);
  312. }
  313. /**
  314. * 银行卡提现
  315. * @param int $uid
  316. * @return mixed
  317. */
  318. public function bank(int $uid)
  319. {
  320. /** @var UserServices $userService */
  321. $userService = app()->make(UserServices::class);
  322. $user = $userService->getUserInfo($uid);
  323. if (!$user) {
  324. throw new ApiException(100026);
  325. }
  326. /** @var UserBrokerageServices $services */
  327. $services = app()->make(UserBrokerageServices::class);
  328. $data['broken_commission'] = $services->getUserFrozenPrice($uid);
  329. if ($data['broken_commission'] < 0)
  330. $data['broken_commission'] = '0';
  331. $data['brokerage_price'] = $user['brokerage_price'];
  332. //可提现佣金
  333. $data['commissionCount'] = bcsub((string)$data['brokerage_price'], (string)$data['broken_commission'], 2);
  334. $extractBank = sys_config('user_extract_bank') ?? []; //提现银行
  335. $extractBank = str_replace("\r\n", "\n", $extractBank);//防止不兼容
  336. $data['extractBank'] = explode("\n", is_array($extractBank) ? ($extractBank[0] ?? $extractBank) : $extractBank);
  337. $data['minPrice'] = sys_config('user_extract_min_price');//提现最低金额
  338. $data['brokerageType'] = sys_config('brokerage_type', 0);//到账方式
  339. return $data;
  340. }
  341. /**
  342. * 提现申请
  343. * @param int $uid
  344. * @param array $data
  345. */
  346. public function cash(int $uid, array $data)
  347. {
  348. /** @var UserServices $userService */
  349. $userService = app()->make(UserServices::class);
  350. $user = $userService->getUserInfo($uid);
  351. if (!$user) {
  352. throw new ApiException(100026);
  353. }
  354. if ($data['extract_type'] == 'weixin' && !sys_config('brokerage_type', 0) && !$data['weixin']) {
  355. throw new ApiException(400110);
  356. }
  357. if ($data['extract_type'] == 'weixin' && bccomp($data['money'], '1', 2) < 0) {
  358. throw new ApiException(410112);
  359. }
  360. /** @var WechatUserServices $wechatServices */
  361. $wechatServices = app()->make(WechatUserServices::class);
  362. if ($data['extract_type'] == 'weixin' && sys_config('brokerage_type', 0) && !$wechatServices->uidToOpenid($uid, 'wechat')) {
  363. throw new ApiException(410024);
  364. }
  365. /** @var UserBrokerageServices $services */
  366. $services = app()->make(UserBrokerageServices::class);
  367. $data['broken_commission'] = $services->getUserFrozenPrice($uid);
  368. if ($data['broken_commission'] < 0)
  369. $data['broken_commission'] = 0;
  370. $data['brokerage_price'] = $user['brokerage_price'];
  371. //可提现佣金
  372. $commissionCount = bcsub((string)$data['brokerage_price'], (string)$data['broken_commission'], 2);
  373. if ($data['money'] > $commissionCount) {
  374. throw new ApiException(400661);
  375. }
  376. $extractPrice = $user['brokerage_price'];
  377. $userExtractMinPrice = sys_config('user_extract_min_price');
  378. if ($data['money'] < $userExtractMinPrice) {
  379. throw new ApiException(400662, ['money' => $userExtractMinPrice]);
  380. }
  381. if ($extractPrice < 0) {
  382. throw new ApiException(400663, ['money' => $data['money']]);
  383. }
  384. if ($data['money'] > $extractPrice) {
  385. throw new ApiException(400663, ['money' => $data['money']]);
  386. }
  387. if ($data['money'] <= 0) {
  388. throw new ApiException(400664);
  389. }
  390. $openid = '';
  391. $insertData = [
  392. 'uid' => $user['uid'],
  393. 'extract_type' => $data['extract_type'],
  394. 'extract_price' => $data['money'],
  395. 'add_time' => time(),
  396. 'balance' => $user['brokerage_price'],
  397. 'status' => 0
  398. ];
  399. if (isset($data['name']) && strlen(trim($data['name']))) $insertData['real_name'] = $data['name'];
  400. else $insertData['real_name'] = $user['nickname'];
  401. if (isset($data['cardnum'])) $insertData['bank_code'] = $data['cardnum'];
  402. else $insertData['bank_code'] = '';
  403. if (isset($data['bankname'])) $insertData['bank_address'] = $data['bankname'];
  404. else $insertData['bank_address'] = '';
  405. if (isset($data['weixin'])) $insertData['wechat'] = $data['weixin'];
  406. else $insertData['wechat'] = $user['nickname'];
  407. $mark = '';
  408. if ($data['extract_type'] == 'alipay') {
  409. $insertData['alipay_code'] = $data['alipay_code'];
  410. $insertData['qrcode_url'] = $data['qrcode_url'];
  411. $mark = '使用支付宝提现' . $insertData['extract_price'] . '元';
  412. } else if ($data['extract_type'] == 'bank') {
  413. $mark = '使用银联卡' . $insertData['bank_code'] . '提现' . $insertData['extract_price'] . '元';
  414. } else if ($data['extract_type'] == 'weixin') {
  415. $insertData['qrcode_url'] = $data['qrcode_url'];
  416. $mark = '使用微信提现' . $insertData['extract_price'] . '元';
  417. /** @var WechatUserServices $wechatServices */
  418. $wechatServices = app()->make(WechatUserServices::class);
  419. $openid = $wechatServices->getWechatOpenid($uid, 'wechat');
  420. if (sys_config('brokerage_type', 0) && $openid) {
  421. if ($data['money'] < 1) {
  422. throw new ApiException(400665);
  423. }
  424. }
  425. }
  426. $res1 = $this->transaction(function () use ($insertData, $data, $uid, $userService, $user, $mark) {
  427. if (!$res1 = $this->dao->save($insertData)) {
  428. throw new ApiException(410121);
  429. }
  430. $balance = bcsub((string)$user['brokerage_price'], (string)$data['money'], 2) ?? 0;
  431. if (!$userService->update($uid, ['brokerage_price' => $balance], 'uid')) {
  432. throw new ApiException(410121);
  433. }
  434. //保存佣金记录
  435. /** @var UserBrokerageServices $userBrokerageServices */
  436. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  437. $userBrokerageServices->income('extract', $uid, ['mark' => $mark, 'number' => $data['money']], $balance, $res1['id']);
  438. /** @var WechatUserServices $wechatServices */
  439. $wechatServices = app()->make(WechatUserServices::class);
  440. $openid = $wechatServices->uidToOpenid($uid, 'wechat');
  441. //自动提现到零钱
  442. if ($insertData['extract_type'] == 'weixin' && $insertData['status'] == 1 && isset($insertData['wechat_order_id'])) {
  443. $res = WechatService::merchantPay($openid, $insertData['wechat_order_id'], $data['money'], '提现佣金到零钱');
  444. if (!$res) {
  445. throw new ApiException(400658);
  446. }
  447. /** @var CapitalFlowServices $capitalFlowServices */
  448. $capitalFlowServices = app()->make(CapitalFlowServices::class);
  449. $capitalFlowServices->setFlow([
  450. 'order_id' => $insertData['wechat_order_id'],
  451. 'uid' => $uid,
  452. 'price' => bcmul('-1', $data['money'], 2),
  453. 'pay_type' => 'weixin',
  454. 'nickname' => $user['nickname'],
  455. 'phone' => $user['phone']
  456. ], 'extract');
  457. }
  458. return $res1;
  459. });
  460. try {
  461. ChannelService::instance()->send('WITHDRAW', ['id' => $res1->id]);
  462. } catch (\Exception $e) {
  463. }
  464. /** @var SystemAdminServices $systemAdmin */
  465. $systemAdmin = app()->make(SystemAdminServices::class);
  466. $systemAdmin->adminNewPush();
  467. //消息
  468. event('notice.notice', [['nickname' => $user['nickname'], 'money' => $data['money']], 'kefu_send_extract_application']);
  469. return true;
  470. }
  471. /**
  472. * @param array $where
  473. * @param string $SumField
  474. * @param string $selectType
  475. * @param string $group
  476. * @return float|mixed
  477. */
  478. public function getOutMoneyByWhere(array $where, string $SumField, string $selectType, string $group = "")
  479. {
  480. switch ($selectType) {
  481. case "sum" :
  482. return $this->dao->getWhereSumField($where, $SumField);
  483. case "group" :
  484. return $this->dao->getGroupField($where, $SumField, $group);
  485. }
  486. }
  487. }