UserExtractServices.php 19 KB

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