UserExtractServices.php 18 KB

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