UserExtractServices.php 20 KB

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