UserExtractServices.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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\jobs\system\CapitalFlowJob;
  14. use app\model\user\User;
  15. use app\model\user\UserExtract;
  16. use app\services\BaseServices;
  17. use app\dao\user\UserExtractDao;
  18. use app\services\order\StoreOrderCreateServices;
  19. use app\services\wechat\WechatUserServices;
  20. use crmeb\exceptions\AdminException;
  21. use crmeb\services\wechat\Payment;
  22. use crmeb\services\FormBuilder as Form;
  23. use crmeb\services\WithdrawService;
  24. use crmeb\traits\ServicesTrait;
  25. use FormBuilder\Factory\Iview;
  26. use think\exception\ValidateException;
  27. use think\facade\Route as Url;
  28. /**
  29. *
  30. * Class UserExtractServices
  31. * @package app\services\user
  32. * @mixin UserExtractDao
  33. */
  34. class UserExtractServices extends BaseServices
  35. {
  36. use ServicesTrait;
  37. /**
  38. * UserExtractServices constructor.
  39. * @param UserExtractDao $dao
  40. */
  41. public function __construct(UserExtractDao $dao)
  42. {
  43. $this->dao = $dao;
  44. }
  45. /**
  46. * 获取一条提现记录
  47. * @param int $id
  48. * @param array $field
  49. * @return array|\think\Model|null
  50. */
  51. public function getExtract(int $id, array $field = [])
  52. {
  53. return $this->dao->get($id, $field);
  54. }
  55. /**
  56. * 获取某个用户提现总数
  57. * @param int $uid
  58. * @return float
  59. */
  60. public function getUserExtract(int $uid)
  61. {
  62. return $this->dao->getWhereSum(['uid' => $uid, 'status' => [0, 1]]);
  63. }
  64. /**
  65. * 获取某些用户的提现总数列表
  66. * @param array $uids
  67. */
  68. public function getUsersSumList(array $uids)
  69. {
  70. return $this->dao->getWhereSumList(['uid' => $uids, 'status' => [0, 1]]);
  71. }
  72. public function getCount(array $where = [])
  73. {
  74. return $this->dao->getCount($where);
  75. }
  76. /**
  77. * 获取提现列表
  78. * @param array $where
  79. * @param string $field
  80. * @return array
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. public function getUserExtractList(array $where, string $field = '*')
  86. {
  87. [$page, $limit] = $this->getPageValue();
  88. $list = $this->dao->getExtractList($where, $field, $page, $limit);
  89. foreach ($list as &$item) {
  90. $item['nickname'] = $item['user']['nickname'] ?? '';
  91. }
  92. $count = $this->dao->count($where);
  93. return compact('list', 'count');
  94. }
  95. /**
  96. * 获取提现总数
  97. * @param array $where
  98. */
  99. public function getExtractSum(array $where)
  100. {
  101. return $this->dao->getExtractMoneyByWhere($where, 'extract_price');
  102. }
  103. /**
  104. * 拒绝提现申请
  105. * @param $id
  106. * @param $fail_msg
  107. * @return bool
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. * @throws \think\exception\DbException
  111. */
  112. public function changeFail(int $id, $userExtract, $message)
  113. {
  114. $fail_time = time();
  115. $extract_number = bcadd((string)$userExtract['extract_price'], (string)$userExtract['extract_fee'], 2);
  116. $mark = '提现失败,退回佣金' . $extract_number . '元';
  117. $uid = $userExtract['uid'];
  118. $status = -1;
  119. /** @var UserServices $userServices */
  120. $userServices = app()->make(UserServices::class);
  121. $user = $userServices->getUserInfo($uid);
  122. if (!$user) {
  123. throw new ValidateException('用户不存在');
  124. }
  125. /** @var UserBrokerageServices $userBrokerageServices */
  126. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  127. $this->transaction(function () use ($user, $userBrokerageServices, $uid, $id, $extract_number, $message, $userServices, $status, $fail_time) {
  128. $now_brokerage = bcadd((string)$user['brokerage_price'], (string)$extract_number, 2);
  129. //增加佣金记录
  130. $userBrokerageServices->income('extract_fail', $uid, $extract_number, $now_brokerage, $id);
  131. //修改用户佣金
  132. if (!$userServices->update($uid, ['brokerage_price' => $now_brokerage], 'uid'))
  133. throw new AdminException('增加用户佣金失败');
  134. if (!$this->dao->update($id, ['fail_time' => $fail_time, 'fail_msg' => $message, 'status' => $status])) {
  135. throw new AdminException('修改失败');
  136. }
  137. });
  138. //消息推送
  139. event('notice.notice', [['uid' => $uid, 'userType' => strtolower($user['user_type']), 'extract_number' => $extract_number, 'nickname' => $user['nickname'], 'message' => $message], 'user_balance_change']);
  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. $extractNumber = $userExtract['extract_price'];
  153. $this->transaction(function () use ($id, $userExtract, $extractNumber) {
  154. // if ($userExtract['extract_type'] != 'bank') {
  155. if (!$this->dao->update($id, ['status' => 1])) {
  156. throw new AdminException('修改失败');
  157. }
  158. //配置开启自动到零钱
  159. if (sys_config('brokerage_type', 0)) {
  160. /** @var WechatUserServices $wechatServices */
  161. $wechatServices = app()->make(WechatUserServices::class);
  162. $openid = $wechatServices->getWechatOpenid((int)$userExtract['uid'], 'wechat');
  163. if ($openid) {//公众号用户
  164. $type = Payment::WEB;
  165. } else {//小程序用户
  166. $openid = $wechatServices->getWechatOpenid((int)$userExtract['uid'], 'routine');
  167. $type = Payment::MINI;
  168. }
  169. //app微信用户
  170. if (!$openid) {
  171. $openid = $wechatServices->getWechatOpenid((int)$userExtract['uid'], 'app');
  172. $type = Payment::APP;
  173. }
  174. if ($openid) {
  175. /** @var StoreOrderCreateServices $services */
  176. $services = app()->make(StoreOrderCreateServices::class);
  177. $wechat_order_id = $services->getNewOrderId();
  178. $res = Payment::merchantPay($openid, $wechat_order_id, $extractNumber, '提现佣金到零钱', $type);
  179. if (!$res) {
  180. throw new ValidateException('企业付款到零钱失败,请稍后再试');
  181. }
  182. } else {
  183. throw new ValidateException('该用户暂不支持企业付款到零钱,请手动转账');
  184. }
  185. }
  186. /** @var UserServices $userServices */
  187. $userServices = app()->make(UserServices::class);
  188. $userType = $userServices->value(['uid' => $userExtract['uid']], 'user_type');
  189. $nickname = $userServices->value(['uid' => $userExtract['uid']], 'nickname');
  190. $phone = $userServices->value(['uid' => $userExtract['uid']], 'phone');
  191. switch ($userExtract['extract_type']) {
  192. case 'bank':
  193. $order_id = $userExtract['bank_code'];
  194. break;
  195. case 'weixin':
  196. $order_id = $userExtract['wechat'];
  197. break;
  198. case 'alipay':
  199. $order_id = $userExtract['alipay_code'];
  200. break;
  201. default:
  202. $order_id = '';
  203. break;
  204. }
  205. //记录资金流水队列
  206. CapitalFlowJob::dispatch([['order_id' => $order_id, 'store_id' => 0, 'uid' => $userExtract['uid'], 'nickname' => $nickname, 'phone' => $phone, 'price' => $extractNumber, 'pay_type' => $userExtract['extract_type']], 'extract']);
  207. //消息推送
  208. event('notice.notice', [['uid' => $userExtract['uid'], 'userType' => strtolower($userType), 'extractNumber' => $extractNumber, 'nickname' => $nickname], 'user_extract']);
  209. // } else {
  210. // if (!$this->dao->update($id, ['status' => 1])) {
  211. // throw new AdminException('修改失败');
  212. // }
  213. // $userServices = app()->make(UserServices::class);
  214. // $userInfo = $userServices->get($userExtract['uid']);
  215. // if (!$userInfo['professional_id']) {
  216. // throw new ValidateException('用户未认证签约');
  217. // }
  218. // $bankInfo = WithdrawService::init()::contractInfo($userInfo['enterprise_professional_facilitator_id']);
  219. // if (!$bankInfo['sign_img']) throw new ValidateException('用户未签约');
  220. // $res = WithdrawService::init()::fastIssuing(
  221. // $userInfo['professional_id'],
  222. // $userExtract['id'],
  223. // $bankInfo['name'],
  224. // $bankInfo['cer_code'],
  225. // $bankInfo['mobile'],
  226. // $bankInfo['bank_code'],
  227. // $extractNumber,
  228. // '佣金提现'
  229. // );
  230. // if ($res) {
  231. // $this->dao->update($id, ['trade_number' => $res['trade_number']]);
  232. // }
  233. // }
  234. });
  235. return true;
  236. }
  237. /**
  238. * 显示资源列表
  239. * @param array $where
  240. * @return array
  241. * @throws \think\db\exception\DataNotFoundException
  242. * @throws \think\db\exception\DbException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. */
  245. public function index(array $where)
  246. {
  247. $list = $this->getUserExtractList($where);
  248. //待提现金额
  249. $where['status'] = 0;
  250. $extract_statistics['price'] = $this->getExtractSum($where);
  251. //已提现金额
  252. $where['status'] = 1;
  253. $extract_statistics['priced'] = $this->getExtractSum($where);
  254. //佣金总金额
  255. /** @var UserBrokerageServices $userBrokerageServices */
  256. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  257. $extract_statistics['brokerage_count'] = $userBrokerageServices->getUsersBokerageSum(array_merge($where, ['pm' => 1, 'not_type' => ['extract_fail', 'refund']]));
  258. //未提现金额
  259. $extract_statistics['brokerage_not'] = $extract_statistics['brokerage_count'] > $extract_statistics['priced'] ? bcsub((string)$extract_statistics['brokerage_count'], (string)$extract_statistics['priced'], 2) : 0.00;
  260. return compact('extract_statistics', 'list');
  261. }
  262. /**
  263. * 显示资源列表
  264. * @param array $where
  265. * @return array
  266. * @throws \think\db\exception\DataNotFoundException
  267. * @throws \think\db\exception\DbException
  268. * @throws \think\db\exception\ModelNotFoundException
  269. */
  270. public function getExportList(array $where)
  271. {
  272. $list = $this->dao->getExtractAll($where);
  273. return $list;
  274. }
  275. /**
  276. * 显示资源列表
  277. * @return array
  278. * @throws \think\db\exception\DataNotFoundException
  279. * @throws \think\db\exception\DbException
  280. * @throws \think\db\exception\ModelNotFoundException
  281. */
  282. public function getExportInfoList($start, $end)
  283. {
  284. $uids = UserExtract::whereBetween('add_time', [$start, $end])->where('extract_type', '<>', 'balance')->where('user_type', 0)->where('status', 1)->column('uid');
  285. $idcards = User::where('uid', 'in', $uids)->where('is_auth', 2)->column('card_id,real_name', 'card_id');
  286. $list = [];
  287. foreach ($idcards as $v) {
  288. $uids = User::where('card_id', $v['card_id'])->where('is_auth', 2)->column('uid');
  289. $sum_money = UserExtract::where('uid', 'in', $uids)
  290. ->whereBetween('add_time', [$start, $end])
  291. ->where('extract_type', '<>', 'balance')
  292. ->where('user_type', 0)
  293. ->where('status', 1)
  294. ->sum('extract_price');
  295. $fee = sys_data('withdraw_fee');
  296. $max_range = 0;
  297. $range = [];
  298. foreach ($fee as $vv) {
  299. if ($sum_money > $vv['month_number']) {
  300. if ($max_range < $vv['month_number']) {
  301. $range = $vv;
  302. $max_range = $vv['month_number'];
  303. }
  304. }
  305. }
  306. if ($range != []) {
  307. if ($range['free_type'] == 1) {
  308. $free = bcdiv(bcmul((string)$sum_money, (string)$range['free']), '100', 2);
  309. } else {
  310. $free = $range['free'];
  311. }
  312. $the_fee = bcdiv(bcmul(bcsub((string)$sum_money, (string)$free, 2), (string)$range['fee']), '100', 2);
  313. $the_fee = bcsub((string)$the_fee, (string)$range['dec'], 2);
  314. }
  315. $row['xingming'] = $v['card_id'];
  316. $row['zhengjianhaoma'] = $v['real_name'];
  317. $row['suodexiangmu'] = '一般劳务报酬所得';
  318. $row['shouru'] = $sum_money;
  319. $row['feiyong'] = $free ?? 0;
  320. $row['mianshuishouru'] = 0;
  321. $row['kouchuxiangmuheji'] = 0;
  322. $row['yingnashuisuodee'] = bcsub((string)$sum_money, (string)($free ?? 0), 2);
  323. $row['shuilv'] = $range['fee'];
  324. $row['susuankouchushu'] = $range['dec'];
  325. $row['yingnashuie'] = $the_fee ?? 0;
  326. $row['jianmianshuie'] = 0;
  327. $row['yingkoujiaoshuie'] = $the_fee ?? 0;
  328. $row['yijiaoshuie'] = 0;
  329. $row['yingbushuie'] = $the_fee ?? 0;
  330. $list[] = $row;
  331. }
  332. return $list;
  333. }
  334. /**
  335. * 显示编辑资源表单页.
  336. *
  337. * @param int $id
  338. * @return \think\Response
  339. */
  340. public function edit(int $id)
  341. {
  342. $UserExtract = $this->getExtract($id);
  343. if (!$UserExtract) {
  344. throw new AdminException('数据不存在!');
  345. }
  346. $f = array();
  347. $f[] = Form::input('real_name', '姓名', $UserExtract['real_name']);
  348. $f[] = Form::number('extract_price', '提现金额', (float)$UserExtract['extract_price'])->precision(2)->disabled(true);
  349. if ($UserExtract['extract_type'] == 'alipay') {
  350. $f[] = Form::input('alipay_code', '支付宝账号', $UserExtract['alipay_code']);
  351. } else if ($UserExtract['extract_type'] == 'weixin') {
  352. $f[] = Form::input('wechat', '微信号', $UserExtract['wechat']);
  353. } else {
  354. $f[] = Form::input('bank_code', '银行卡号', $UserExtract['bank_code']);
  355. $f[] = Form::input('bank_address', '开户行', $UserExtract['bank_address']);
  356. }
  357. $f[] = Form::input('mark', '备注', $UserExtract['mark'])->type('textarea');
  358. return create_form('编辑', $f, Url::buildUrl('/finance/extract/' . $id), 'PUT');
  359. }
  360. public function changeOrderStatusForm(int $id)
  361. {
  362. $f = array();
  363. $f[] = Form::radio('status', '审核意见', '1')->setOptions([['label' => '通过', 'value' => '1'], ['label' => '拒绝', 'value' => '5'],]);
  364. $f[] = Form::frameImage('icon', '具体结算清单', Url::buildUrl(config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'icon')))->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])->appendValidate(Iview::validateStr()->required()->message('请选择具体结算清单'));
  365. $f[] = Form::frameImage('image', '公章图片', Url::buildUrl(config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'image')))->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])->appendValidate(Iview::validateStr()->required()->message('请选择公章图片'));
  366. $f[] = Form::input('remarks', '备注')->type('textarea');
  367. return create_form('编辑', $f, Url::buildUrl('/finance/extract/order/' . $id), 'PUT');
  368. }
  369. public function update(int $id, array $data)
  370. {
  371. if (!$this->dao->update($id, $data))
  372. throw new AdminException('修改失败');
  373. else
  374. return true;
  375. }
  376. /**
  377. * 拒绝
  378. * @param $id
  379. * @return mixed
  380. */
  381. public function refuse(int $id, string $message)
  382. {
  383. $extract = $this->getExtract($id);
  384. if (!$extract) {
  385. throw new AdminException('操作记录不存在!');
  386. }
  387. if ($extract->status == 1) {
  388. throw new AdminException('已经提现,错误操作');
  389. }
  390. if ($extract->status == -1) {
  391. throw new AdminException('您的提现申请已被拒绝,请勿重复操作!');
  392. }
  393. $res = $this->changeFail($id, $extract, $message);
  394. if ($res) {
  395. return true;
  396. } else {
  397. throw new AdminException('操作失败!');
  398. }
  399. }
  400. /**
  401. * 通过
  402. * @param $id
  403. * @return mixed
  404. */
  405. public function adopt(int $id)
  406. {
  407. $extract = $this->getExtract($id);
  408. if (!$extract) {
  409. throw new AdminException('操作记录不存!');
  410. }
  411. if ($extract->status == 1 || $extract->status == 2) {
  412. throw new AdminException('您已提现,请勿重复提现!');
  413. }
  414. if ($extract->status == -1) {
  415. throw new AdminException('您的提现申请已被拒绝!');
  416. }
  417. if ($this->changeSuccess($id, $extract)) {
  418. return true;
  419. } else {
  420. throw new AdminException('操作失败!');
  421. }
  422. }
  423. /**待提现的数量
  424. * @return int
  425. */
  426. public function userExtractCount()
  427. {
  428. return $this->dao->count(['status' => 0]);
  429. }
  430. /**
  431. * 银行卡提现
  432. * @param int $uid
  433. * @return mixed
  434. */
  435. public function bank(int $uid)
  436. {
  437. /** @var UserServices $userService */
  438. $userService = app()->make(UserServices::class);
  439. $user = $userService->getUserInfo($uid);
  440. if (!$user) {
  441. throw new ValidateException('数据不存在');
  442. }
  443. /** @var UserBrokerageServices $userBrokerageServices */
  444. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  445. $data['broken_commission'] = $userBrokerageServices->getUserFrozenPrice($uid);
  446. if ($data['broken_commission'] < 0)
  447. $data['broken_commission'] = '0';
  448. $data['brokerage_price'] = $user['brokerage_price'];
  449. //可提现佣金
  450. $data['commissionCount'] = bcsub((string)$data['brokerage_price'], (string)$data['broken_commission'], 2);
  451. $extractBank = sys_config('user_extract_bank') ?? []; //提现银行
  452. $extractBank = str_replace("\r\n", "\n", $extractBank);//防止不兼容
  453. $data['extractBank'] = explode("\n", is_array($extractBank) ? (isset($extractBank[0]) ? $extractBank[0] : $extractBank) : $extractBank);
  454. $data['minPrice'] = sys_config('user_extract_min_price');//提现最低金额
  455. $data['withdraw_fee'] = sys_config('withdraw_fee');//提现手续费
  456. $data['extract_wechat_type'] = sys_config('brokerage_type');//微信提现到账方式
  457. $data['brokerage_to_money_switch'] = sys_config('brokerage_to_money_switch');//微信提现到账方式
  458. $data['brokerage_to_energy_switch'] = sys_config('brokerage_to_energy_switch');//微信提现到账方式
  459. $data['brokerage_trade_switch'] = sys_config('brokerage_trade_switch');//微信提现到账方式
  460. return $data;
  461. }
  462. /**
  463. * 提现申请
  464. * @param int $uid
  465. * @param array $data
  466. */
  467. public function cash(int $uid, array $data)
  468. {
  469. /** @var UserServices $userService */
  470. $userService = app()->make(UserServices::class);
  471. $user = $userService->getUserInfo($uid);
  472. if (!$user) {
  473. throw new ValidateException('数据不存在');
  474. }
  475. if ($user['is_auth'] != 2) throw new ValidateException('请先完成实名认证');
  476. $uids = $userService->search()->where('card_id', $user['card_id'])->column('uid');
  477. if (UserExtract::where('uid', 'in', $uids)
  478. ->whereTime('add_time', 'month')
  479. ->where('status', 0)->find()) {
  480. throw new ValidateException('请等待身份证下上次提现申请通过');
  481. }
  482. // if (!$user['enterprise_professional_facilitator_id']) {
  483. // throw new ValidateException('签约未完成');
  484. // }
  485. // $bank_info = WithdrawService::init()::contractInfo($user['enterprise_professional_facilitator_id']);
  486. // if (!$bank_info['sign_img']) {
  487. // throw new ValidateException('签约未完成');
  488. // }
  489. // $data['cardnum'] = $bank_info['bank_code'];
  490. // $data['bankname'] = '灵活用工提现';
  491. /** @var UserBrokerageServices $userBrokerageServices */
  492. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  493. $data['broken_commission'] = $userBrokerageServices->getUserFrozenPrice($uid);
  494. if ($data['broken_commission'] < 0)
  495. $data['broken_commission'] = 0;
  496. $data['brokerage_price'] = $user['brokerage_price'];
  497. //可提现佣金
  498. $commissionCount = (float)bcsub((string)$data['brokerage_price'], (string)$data['broken_commission'], 2);
  499. if ($data['money'] > $commissionCount) {
  500. throw new ValidateException('可提现佣金不足');
  501. }
  502. $extractPrice = $user['brokerage_price'];
  503. $userExtractMinPrice = sys_config('user_extract_min_price');
  504. if ($data['money'] < $userExtractMinPrice) {
  505. throw new ValidateException('提现金额不能小于' . $userExtractMinPrice . '元');
  506. }
  507. if ($extractPrice < 0) {
  508. throw new ValidateException('提现佣金不足' . $data['money']);
  509. }
  510. if ($data['money'] > $extractPrice) {
  511. throw new ValidateException('提现佣金不足' . $data['money']);
  512. }
  513. if ($data['money'] <= 0) {
  514. throw new ValidateException('提现佣金大于0');
  515. }
  516. $fee = sys_data('withdraw_fee');
  517. $fees = [];
  518. foreach ($fee as $v) {
  519. $fees[$v['month_number']] = (string)$v['fee'];
  520. }
  521. krsort($fees, SORT_DESC);
  522. $sum_money = UserExtract::where('uid', 'in', $uids)
  523. ->whereTime('add_time', 'month')
  524. ->where('user_type', 0)
  525. ->where('extract_type', '<>', 'balance')
  526. ->where('status', 1)->sum('extract_price');
  527. $sum_fee = UserExtract::where('uid', 'in', $uids)
  528. ->whereTime('add_time', 'month')
  529. ->where('user_type', 0)
  530. ->where('extract_type', '<>', 'balance')
  531. ->where('status', 1)->sum('extract_fee');
  532. $money = $data['money'];
  533. $extract_fee = 0;
  534. if ($data['user_type'] == 0) {
  535. if ($data['name'] != $user['real_name']) throw new ValidateException('提现用户与实名认证不符');
  536. $money_sum = bcadd(bcadd((string)$sum_money, (string)$sum_fee, 2), (string)$money, 2);
  537. $max_range = 0;
  538. $range = [];
  539. foreach ($fee as $v) {
  540. if ($money_sum > $v['month_number']) {
  541. if ($max_range < $v['month_number']) {
  542. $range = $v;
  543. $max_range = $v['month_number'];
  544. }
  545. }
  546. }
  547. if ($range != []) {
  548. if ($range['free_type'] == 1) {
  549. $free = bcdiv(bcmul((string)$money_sum, (string)$range['free']), '100', 2);
  550. } else {
  551. $free = $range['free'];
  552. }
  553. $the_fee = bcdiv(bcmul(bcsub((string)$money_sum, (string)$free, 2), (string)$range['fee']), '100', 2);
  554. $the_fee = bcsub((string)$the_fee, (string)$range['dec'], 2);
  555. $extract_fee = bcsub((string)$the_fee, (string)$sum_fee, 2);
  556. }
  557. }
  558. if ($extract_fee < 0) $extract_fee = 0;
  559. $insertData = [
  560. 'uid' => $user['uid'],
  561. 'extract_type' => $data['extract_type'],
  562. 'extract_price' => bcsub((string)$data['money'], (string)$extract_fee, 2),
  563. 'extract_fee' => $extract_fee,
  564. 'add_time' => time(),
  565. 'balance' => $user['brokerage_price'],
  566. 'image' => $data['image'],
  567. 'status' => 0,
  568. 'user_type' => $data['user_type']
  569. ];
  570. if (isset($data['name']) && strlen(trim($data['name']))) $insertData['real_name'] = $data['name'];
  571. else $insertData['real_name'] = $user['nickname'];
  572. if (isset($data['cardnum'])) $insertData['bank_code'] = $data['cardnum'];
  573. else $insertData['bank_code'] = '';
  574. if (isset($data['bankname'])) $insertData['bank_address'] = $data['bankname'];
  575. else $insertData['bank_address'] = '';
  576. if (isset($data['weixin'])) $insertData['wechat'] = $data['weixin'];
  577. else $insertData['wechat'] = $user['nickname'];
  578. if ($data['extract_type'] == 'alipay') {
  579. if (!sys_config('user_extract_alipay_status', 0)) {
  580. throw new ValidateException('支付宝通道暂时关闭');
  581. }
  582. $insertData['alipay_code'] = $data['alipay_code'];
  583. $insertData['qrcode_url'] = $data['qrcode_url'];
  584. $mark = '使用支付宝';
  585. } else if ($data['extract_type'] == 'bank') {
  586. if (!sys_config('user_extract_bank_status', 0)) {
  587. throw new ValidateException('银行卡通道暂时关闭');
  588. }
  589. $mark = '使用银联卡' . $insertData['bank_code'];
  590. } else if ($data['extract_type'] == 'weixin') {
  591. if (!sys_config('user_extract_wechat_status', 0)) {
  592. throw new ValidateException('微信通道暂时关闭');
  593. }
  594. $insertData['qrcode_url'] = $data['qrcode_url'];
  595. $mark = '使用微信提现';
  596. /** @var WechatUserServices $wechatServices */
  597. $wechatServices = app()->make(WechatUserServices::class);
  598. $openid = $wechatServices->getWechatOpenid($uid, 'wechat');
  599. if (sys_config('brokerage_type', 0) && $openid) {
  600. if ($insertData['extract_price'] < 1) {
  601. throw new ValidateException('扣除手续费后,提现金额不足1元;而企业微信付款到零钱最低金额为1元');
  602. }
  603. }
  604. }
  605. /** @var UserBrokerageServices $userBrokerageServices */
  606. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  607. $res1 = $this->transaction(function () use ($insertData, $data, $uid, $userService, $user, $userBrokerageServices, $mark) {
  608. if (!$res1 = $this->dao->save($insertData)) {
  609. throw new ValidateException('提现失败');
  610. }
  611. //修改用户佣金
  612. $balance = bcsub((string)$user['brokerage_price'], (string)$data['money'], 2) ?? 0;
  613. if (!$userService->update($uid, ['brokerage_price' => $balance], 'uid')) {
  614. throw new ValidateException('修改用户信息失败');
  615. }
  616. //保存佣金记录
  617. $userBrokerageServices->income('extract', $uid, ['mark' => $mark, 'number' => $data['money']], $balance, $res1['id']);
  618. return $res1;
  619. });
  620. //用户申请体现事件
  621. event('user.extract', [$user, $data, $res1]);
  622. return true;
  623. }
  624. /**
  625. * @param array $where
  626. * @param string $SumField
  627. * @param string $selectType
  628. * @param string $group
  629. * @return float|mixed
  630. */
  631. public function getOutMoneyByWhere(array $where, string $SumField, string $selectType, string $group = "")
  632. {
  633. switch ($selectType) {
  634. case "sum" :
  635. return $this->dao->getWhereSumField($where, $SumField);
  636. case "group" :
  637. return $this->dao->getGroupField($where, $SumField, $group);
  638. }
  639. }
  640. }