UserExtractServices.php 21 KB

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