UserExtractServices.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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\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\services\FormBuilder as Form;
  21. use crmeb\services\WechatService;
  22. use crmeb\services\workerman\ChannelService;
  23. use think\exception\ValidateException;
  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('增加用户佣金失败');
  125. if (!$this->dao->update($id, ['fail_time' => $fail_time, 'fail_msg' => $message, 'status' => $status])) {
  126. throw new AdminException('修改失败');
  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('修改失败');
  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. /** @var CapitalFlowServices $capitalFlowServices */
  169. $capitalFlowServices = app()->make(CapitalFlowServices::class);
  170. $capitalFlowServices->setFlow([
  171. 'order_id' => $order_id,
  172. 'uid' => $userExtract['uid'],
  173. 'price' => bcmul('-1', $extractNumber, 2),
  174. 'pay_type' => $userExtract['extract_type'],
  175. 'nickname' => $nickname,
  176. 'phone' => $phone
  177. ], 'extract');
  178. return true;
  179. }
  180. /**
  181. * 显示资源列表
  182. * @param array $where
  183. * @return array
  184. * @throws \think\db\exception\DataNotFoundException
  185. * @throws \think\db\exception\DbException
  186. * @throws \think\db\exception\ModelNotFoundException
  187. */
  188. public function index(array $where)
  189. {
  190. $list = $this->getUserExtractList($where);
  191. /** @var UserServices $userServices */
  192. $userServices = app()->make(UserServices::class);
  193. //待提现金额
  194. $where['status'] = 0;
  195. $extract_statistics['price'] = $this->getExtractSum($where);
  196. //已提现金额
  197. $where['status'] = 1;
  198. $extract_statistics['priced'] = $this->getExtractSum($where);
  199. /** @var UserBrokerageServices $userBrokerageServices */
  200. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  201. $extract_statistics['brokerage_count'] = $userBrokerageServices->getUsersBokerageSum($where);
  202. //未提现金额
  203. $extract_statistics['brokerage_not'] = $extract_statistics['brokerage_count'] > $extract_statistics['priced'] ? bcsub((string)$extract_statistics['brokerage_count'], (string)$extract_statistics['priced'], 2) : 0.00;
  204. return compact('extract_statistics', 'list');
  205. }
  206. /**
  207. * 显示编辑资源表单页.
  208. *
  209. * @param int $id
  210. * @return \think\Response
  211. */
  212. public function edit(int $id)
  213. {
  214. $UserExtract = $this->getExtract($id);
  215. if (!$UserExtract) {
  216. throw new AdminException('数据不存在!');
  217. }
  218. $f = array();
  219. $f[] = Form::input('real_name', '姓名', $UserExtract['real_name']);
  220. $f[] = Form::number('extract_price', '提现金额', (float)$UserExtract['extract_price'])->precision(2)->disabled(true);
  221. if ($UserExtract['extract_type'] == 'alipay') {
  222. $f[] = Form::input('alipay_code', '支付宝账号', $UserExtract['alipay_code']);
  223. } else if ($UserExtract['extract_type'] == 'weixin') {
  224. $f[] = Form::input('wechat', '微信号', $UserExtract['wechat']);
  225. } else {
  226. $f[] = Form::input('bank_code', '银行卡号', $UserExtract['bank_code']);
  227. $f[] = Form::input('bank_address', '开户行', $UserExtract['bank_address']);
  228. }
  229. $f[] = Form::input('mark', '备注', $UserExtract['mark'])->type('textarea');
  230. return create_form('编辑', $f, Url::buildUrl('/finance/extract/' . $id), 'PUT');
  231. }
  232. public function update(int $id, array $data)
  233. {
  234. if (!$this->dao->update($id, $data))
  235. throw new AdminException('修改失败');
  236. else
  237. return true;
  238. }
  239. /**
  240. * 拒绝
  241. * @param $id
  242. * @return mixed
  243. */
  244. public function refuse(int $id, string $message)
  245. {
  246. $extract = $this->getExtract($id);
  247. if (!$extract) {
  248. throw new AdminException('操作记录不存在!');
  249. }
  250. if ($extract->status == 1) {
  251. throw new AdminException('已经提现,错误操作');
  252. }
  253. if ($extract->status == -1) {
  254. throw new AdminException('您的提现申请已被拒绝,请勿重复操作!');
  255. }
  256. $res = $this->changeFail($id, $extract, $message);
  257. if ($res) {
  258. return true;
  259. } else {
  260. throw new AdminException('操作失败!');
  261. }
  262. }
  263. /**
  264. * 通过
  265. * @param $id
  266. * @return mixed
  267. */
  268. public function adopt(int $id)
  269. {
  270. $extract = $this->getExtract($id);
  271. if (!$extract) {
  272. throw new AdminException('操作记录不存!');
  273. }
  274. if ($extract->status == 1) {
  275. throw new AdminException('您已提现,请勿重复提现!');
  276. }
  277. if ($extract->status == -1) {
  278. throw new AdminException('您的提现申请已被拒绝!');
  279. }
  280. if ($this->changeSuccess($id, $extract)) {
  281. return true;
  282. } else {
  283. throw new AdminException('操作失败!');
  284. }
  285. }
  286. /**待提现的数量
  287. * @return int
  288. */
  289. public function userExtractCount()
  290. {
  291. return $this->dao->count(['status' => 0]);
  292. }
  293. /**
  294. * 银行卡提现
  295. * @param int $uid
  296. * @return mixed
  297. */
  298. public function bank(int $uid)
  299. {
  300. /** @var UserServices $userService */
  301. $userService = app()->make(UserServices::class);
  302. $user = $userService->getUserInfo($uid);
  303. if (!$user) {
  304. throw new ValidateException('数据不存在');
  305. }
  306. /** @var UserBrokerageServices $services */
  307. $services = app()->make(UserBrokerageServices::class);
  308. $data['broken_commission'] = $services->getUserFrozenPrice($uid);
  309. if ($data['broken_commission'] < 0)
  310. $data['broken_commission'] = '0';
  311. $data['brokerage_price'] = $user['brokerage_price'];
  312. //可提现佣金
  313. $data['commissionCount'] = bcsub((string)$data['brokerage_price'], (string)$data['broken_commission'], 2);
  314. $extractBank = sys_config('user_extract_bank') ?? []; //提现银行
  315. $extractBank = str_replace("\r\n", "\n", $extractBank);//防止不兼容
  316. $data['extractBank'] = explode("\n", is_array($extractBank) ? ($extractBank[0] ?? $extractBank) : $extractBank);
  317. $data['minPrice'] = sys_config('user_extract_min_price');//提现最低金额
  318. return $data;
  319. }
  320. /**
  321. * 提现申请
  322. * @param int $uid
  323. * @param array $data
  324. */
  325. public function cash(int $uid, array $data)
  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 UserBillServices $userBill */
  334. $userBill = app()->make(UserBillServices::class);
  335. /** @var UserBrokerageServices $services */
  336. $services = app()->make(UserBrokerageServices::class);
  337. $data['broken_commission'] = $services->getUserFrozenPrice($uid);
  338. if ($data['broken_commission'] < 0)
  339. $data['broken_commission'] = 0;
  340. $data['brokerage_price'] = $user['brokerage_price'];
  341. //可提现佣金
  342. $commissionCount = bcsub((string)$data['brokerage_price'], (string)$data['broken_commission'], 2);
  343. if ($data['money'] > $commissionCount) {
  344. throw new ValidateException('可提现佣金不足');
  345. }
  346. $extractPrice = $user['brokerage_price'];
  347. $userExtractMinPrice = sys_config('user_extract_min_price');
  348. if ($data['money'] < $userExtractMinPrice) {
  349. throw new ValidateException('提现金额不能小于' . $userExtractMinPrice . '元');
  350. }
  351. if ($extractPrice < 0) {
  352. throw new ValidateException('提现佣金不足' . $data['money']);
  353. }
  354. if ($data['money'] > $extractPrice) {
  355. throw new ValidateException('提现佣金不足' . $data['money']);
  356. }
  357. if ($data['money'] <= 0) {
  358. throw new ValidateException('提现佣金大于0');
  359. }
  360. $openid = '';
  361. $insertData = [
  362. 'uid' => $user['uid'],
  363. 'extract_type' => $data['extract_type'],
  364. 'extract_price' => $data['money'],
  365. 'add_time' => time(),
  366. 'balance' => $user['brokerage_price'],
  367. 'status' => 0
  368. ];
  369. if (isset($data['name']) && strlen(trim($data['name']))) $insertData['real_name'] = $data['name'];
  370. else $insertData['real_name'] = $user['nickname'];
  371. if (isset($data['cardnum'])) $insertData['bank_code'] = $data['cardnum'];
  372. else $insertData['bank_code'] = '';
  373. if (isset($data['bankname'])) $insertData['bank_address'] = $data['bankname'];
  374. else $insertData['bank_address'] = '';
  375. if (isset($data['weixin'])) $insertData['wechat'] = $data['weixin'];
  376. else $insertData['wechat'] = $user['nickname'];
  377. $mark = '';
  378. if ($data['extract_type'] == 'alipay') {
  379. $insertData['alipay_code'] = $data['alipay_code'];
  380. $insertData['qrcode_url'] = $data['qrcode_url'];
  381. $mark = '使用支付宝提现' . $insertData['extract_price'] . '元';
  382. } else if ($data['extract_type'] == 'bank') {
  383. $mark = '使用银联卡' . $insertData['bank_code'] . '提现' . $insertData['extract_price'] . '元';
  384. } else if ($data['extract_type'] == 'weixin') {
  385. $insertData['qrcode_url'] = $data['qrcode_url'];
  386. $mark = '使用微信提现' . $insertData['extract_price'] . '元';
  387. /** @var WechatUserServices $wechatServices */
  388. $wechatServices = app()->make(WechatUserServices::class);
  389. $openid = $wechatServices->getWechatOpenid($uid, 'wechat');
  390. if (sys_config('brokerage_type', 0) && $openid) {
  391. $insertData['status'] = 1;
  392. /** @var StoreOrderCreateServices $services */
  393. $services = app()->make(StoreOrderCreateServices::class);
  394. $insertData['wechat_order_id'] = $services->getNewOrderId();
  395. if ($data['money'] < 1) {
  396. throw new ValidateException('企业微信付款到零钱最低金额为1元');
  397. }
  398. }
  399. }
  400. $res1 = $this->transaction(function () use ($insertData, $data, $uid, $userService, $user, $mark, $openid) {
  401. if (!$res1 = $this->dao->save($insertData)) {
  402. throw new ValidateException('提现失败');
  403. }
  404. $balance = bcsub((string)$user['brokerage_price'], (string)$data['money'], 2) ?? 0;
  405. if (!$userService->update($uid, ['brokerage_price' => $balance], 'uid')) {
  406. throw new ValidateException('修改用户信息失败');
  407. }
  408. //保存佣金记录
  409. /** @var UserBrokerageServices $userBrokerageServices */
  410. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  411. $userBrokerageServices->income('extract', $uid, ['mark' => $mark, 'number' => $data['money']], $balance, $res1['id']);
  412. //自动提现到零钱
  413. if ($insertData['extract_type'] == 'weixin' && $insertData['status'] == 1 && isset($insertData['wechat_order_id'])) {
  414. $res = WechatService::merchantPay($openid, $insertData['wechat_order_id'], $data['money'], '提现佣金到零钱');
  415. if (!$res) {
  416. throw new ValidateException('企业付款到零钱失败,请稍后再试');
  417. }
  418. /** @var CapitalFlowServices $capitalFlowServices */
  419. $capitalFlowServices = app()->make(CapitalFlowServices::class);
  420. $capitalFlowServices->setFlow([
  421. 'order_id' => $insertData['wechat_order_id'],
  422. 'uid' => $uid,
  423. 'price' => bcmul('-1', $data['money'], 2),
  424. 'pay_type' => 'weixin',
  425. 'nickname' => $user['nickname'],
  426. 'phone' => $user['phone']
  427. ], 'extract');
  428. }
  429. return $res1;
  430. });
  431. try {
  432. ChannelService::instance()->send('WITHDRAW', ['id' => $res1->id]);
  433. } catch (\Exception $e) {
  434. }
  435. /** @var SystemAdminServices $systemAdmin */
  436. $systemAdmin = app()->make(SystemAdminServices::class);
  437. $systemAdmin->adminNewPush();
  438. //消息
  439. event('notice.notice', [['nickname' => $user['nickname'], 'money' => $data['money']], 'kefu_send_extract_application']);
  440. return true;
  441. }
  442. /**
  443. * @param array $where
  444. * @param string $SumField
  445. * @param string $selectType
  446. * @param string $group
  447. * @return float|mixed
  448. */
  449. public function getOutMoneyByWhere(array $where, string $SumField, string $selectType, string $group = "")
  450. {
  451. switch ($selectType) {
  452. case "sum" :
  453. return $this->dao->getWhereSumField($where, $SumField);
  454. case "group" :
  455. return $this->dao->getGroupField($where, $SumField, $group);
  456. }
  457. }
  458. }