WithdrawLogic.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. <?php
  2. namespace app\admin\logic\finance;
  3. use app\admin\logic\WechatMerchantTransferLogic;
  4. use app\common\basics\Logic;
  5. use app\common\model\RechargeOrder;
  6. use app\common\model\WithdrawApply;
  7. use app\common\enum\WithdrawEnum;
  8. use app\common\server\ConfigServer;
  9. use app\common\server\ExportExcelServer;
  10. use app\common\server\UrlServer;
  11. use app\common\model\user\User;
  12. use app\common\logic\AccountLogLogic;
  13. use app\common\model\AccountLog;
  14. use app\admin\logic\WechatCorporatePaymentLogic;
  15. use think\facade\Db;
  16. use think\Exception;
  17. /**
  18. * Class WithdrawLogic
  19. * @package app\admin\logic\finance
  20. */
  21. class WithdrawLogic extends Logic
  22. {
  23. /**
  24. * @notes 会员佣金提现列表
  25. * @param $get
  26. * @return array
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\DbException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. * @author suny
  31. * @date 2021/7/14 10:00 上午
  32. */
  33. public static function lists($get, $is_export = false)
  34. {
  35. $where = [];
  36. // 会员信息
  37. if (!empty($get['search_key']) && !empty($get['keyword'])) {
  38. $keyword = $get['keyword'];
  39. if ($get['search_key'] == 'user_sn') {
  40. $where[] = ['u.sn', '=', $keyword];
  41. } elseif ($get['search_key'] == 'nickname') {
  42. $where[] = ['u.nickname', 'like', '%' . $keyword . '%'];
  43. }
  44. }
  45. //提现单号
  46. if (isset($get['withdraw_sn']) && $get['withdraw_sn'] != '') {
  47. $where[] = ['w.sn', '=', $get['withdraw_sn']];
  48. }
  49. //提现方式
  50. if (isset($get['type']) && $get['type'] != '') {
  51. $where[] = ['w.type', '=', $get['type']];
  52. }
  53. //提现状态
  54. if (isset($get['status']) && $get['status'] != '') {
  55. $where[] = ['status', '=', $get['status']];
  56. }
  57. if (empty($get['start_time']) && empty($get['end_time'])) {
  58. $where[] = ['w.create_time', '>=', strtotime(date("Y-m-d", time()))];
  59. $where[] = ['w.create_time', '<=', strtotime(date("Y-m-d", time())) + 86399];
  60. }
  61. // 提现时间
  62. if (isset($get['start_time']) && $get['start_time'] && isset($get['end_time']) && $get['end_time']) {
  63. $where[] = ['w.create_time', 'between', [strtotime($get['start_time']), strtotime($get['end_time'])]];
  64. // }else{
  65. //// $where[] = ['w.create_time', 'between', Time::today()];
  66. }
  67. // 导出
  68. if (true === $is_export) {
  69. return self::withdrawExport($where);
  70. }
  71. $lists = WithdrawApply::alias('w')
  72. ->field('w.*, u.nickname,u.avatar, u.sn as user_sn, u.mobile, ul.name as user_level_name')
  73. ->with('user')
  74. ->leftJoin('user u', 'u.id = w.user_id')
  75. ->leftJoin('user_level ul', 'ul.id = u.level')
  76. ->where($where)
  77. ->page($get['page'], $get['limit'])
  78. ->order('w.id desc')
  79. ->select();
  80. $count = WithdrawApply::alias('w')
  81. ->field('w.*, u.nickname,u.avatar, u.sn as user_sn, u.mobile, ul.name as user_level_name')
  82. ->leftJoin('user u', 'u.id = w.user_id')
  83. ->leftJoin('user_level ul', 'ul.id = u.level')
  84. ->where($where)
  85. ->order('w.id desc')
  86. ->count();
  87. foreach ($lists as &$item) {
  88. if (empty($item['user'])) {
  89. // 用户不存在
  90. $user = [
  91. 'avatar' => '',
  92. 'sn' => '-',
  93. 'nickname' => '-',
  94. ];
  95. } else {
  96. $user = $item['user'];
  97. }
  98. $item['type_text'] = WithdrawEnum::getTypeDesc($item['type']);
  99. $item['status_text'] = WithdrawEnum::getStatusDesc($item['status']);
  100. $item['avatar'] = UrlServer::getFileUrl($item['avatar']);
  101. $user['avatar'] = UrlServer::getFileUrl($user['avatar']);
  102. $item['user_level_name'] = $item['user_level_name'] ? $item['user_level_name'] : '无等级';
  103. $user['user_level_name'] = $item['user_level_name'];
  104. // 通过中间变量$user解决Indirect modification of overloaded element报错
  105. $item['user'] = $user;
  106. }
  107. return ['count' => $count, 'lists' => $lists];
  108. }
  109. /**
  110. * @notes 数据汇总
  111. * @return array
  112. * @author suny
  113. * @date 2021/7/14 10:01 上午
  114. */
  115. public static function summary()
  116. {
  117. $model = new WithdrawApply();
  118. $successWithdraw = $model->where(['status' => WithdrawEnum::STATUS_SUCCESS])->sum('money');
  119. $handleWithdraw = $model->where(['status' => WithdrawEnum::STATUS_ING])->sum('money');
  120. $totalEarnings = (new User())->where(['del' => 0])->sum('earnings');
  121. return ['successWithdraw' => $successWithdraw, 'handleWithdraw' => $handleWithdraw, 'totalEarnings' => $totalEarnings];
  122. }
  123. /**
  124. * @notes 佣金明细
  125. * @param $get
  126. * @return array
  127. * @throws \think\db\exception\DataNotFoundException
  128. * @throws \think\db\exception\DbException
  129. * @throws \think\db\exception\ModelNotFoundException
  130. * @author suny
  131. * @date 2021/7/14 10:01 上午
  132. */
  133. public static function commission($get, $is_export = false)
  134. {
  135. $where = [];
  136. // 明细类型
  137. $source_type = AccountLog::earnings_change;
  138. if (isset($get['source_type']) && !empty($get['source_type'])) {
  139. $where[] = ['a.source_type', '=', $get['source_type']];
  140. } else {
  141. $where[] = ['a.source_type', 'in', $source_type];
  142. }
  143. //明细搜索
  144. if (!empty($get['search_key']) && !empty($get['keyword'])) {
  145. $keyword = $get['keyword'];
  146. switch ($get['search_key']) {
  147. case 'user_sn' :
  148. $where[] = ['u.sn', '=', $keyword];
  149. break;
  150. case 'nickname' :
  151. $where[] = ['u.nickname', 'like', '%' . $keyword . '%'];
  152. break;
  153. }
  154. }
  155. if (empty($get['start_time']) && empty($get['end_time'])) {
  156. $where[] = ['a.create_time', '>=', strtotime(date("Y-m-d", time()))];
  157. $where[] = ['a.create_time', '<=', strtotime(date("Y-m-d", time())) + 86399];
  158. }
  159. //明细时间
  160. if (isset($get['start_time']) && $get['start_time'] != '') {
  161. $where[] = ['a.create_time', '>=', strtotime($get['start_time'])];
  162. }
  163. if (isset($get['end_time']) && $get['end_time'] != '') {
  164. $where[] = ['a.create_time', '<=', strtotime($get['end_time'])];
  165. }
  166. // 导出
  167. if (true === $is_export) {
  168. return self::commissionExport($where);
  169. }
  170. $lists = AccountLog::alias('a')
  171. ->field('a.*,u.nickname,u.sn as user_sn,u.mobile,w.sn as withdraw_sn')
  172. ->join('user u', 'u.id = a.user_id')
  173. ->leftjoin('withdraw_apply w', 'w.sn = a.source_sn')
  174. ->where($where)
  175. ->page($get['page'], $get['limit'])
  176. ->order('a.id desc')
  177. ->select();
  178. $count = AccountLog::alias('a')
  179. ->field('a.*,u.nickname,u.sn as user_sn,u.mobile,w.sn as withdraw_sn')
  180. ->join('user u', 'u.id = a.user_id')
  181. ->leftjoin('withdraw_apply w', 'w.sn = a.source_sn')
  182. ->where($where)
  183. ->order('a.id desc')
  184. ->count();
  185. return ['count' => $count, 'lists' => $lists];
  186. }
  187. /**
  188. * @notes 账户明细
  189. * @param $get
  190. * @return array
  191. * @throws \think\db\exception\DataNotFoundException
  192. * @throws \think\db\exception\DbException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. * @author suny
  195. * @date 2021/7/14 10:01 上午
  196. */
  197. public static function account($get, $is_export = false)
  198. {
  199. $where = [];
  200. // 明细类型
  201. $source_type = AccountLog::money_change;
  202. if (isset($get['type']) && !empty($get['type'])) {
  203. switch ($get['type']) {
  204. case 'admin_add_money' :
  205. $type = AccountLog::admin_add_money;
  206. break;
  207. case 'admin_reduce_money' :
  208. $type = AccountLog::admin_reduce_money;
  209. break;
  210. case 'recharge_money' :
  211. $type = AccountLog::recharge_money;
  212. break;
  213. case 'balance_pay_order' :
  214. $type = AccountLog::balance_pay_order;
  215. break;
  216. case 'cancel_order_refund' :
  217. $type = AccountLog::cancel_order_refund;
  218. break;
  219. case 'after_sale_refund' :
  220. $type = AccountLog::after_sale_refund;
  221. break;
  222. case 'withdraw_to_balance' :
  223. $type = AccountLog::withdraw_to_balance;
  224. break;
  225. case 'user_transfer_inc_balance' :
  226. $type = AccountLog::user_transfer_inc_balance;
  227. break;
  228. case 'user_transfer_dec_balance' :
  229. $type = AccountLog::user_transfer_dec_balance;
  230. break;
  231. case 'integral_order_inc_balance' :
  232. $type = AccountLog::integral_order_inc_balance;
  233. break;
  234. case 'integral_order_dec_balance' :
  235. $type = AccountLog::integral_order_dec_balance;
  236. break;
  237. }
  238. $where[] = ['a.source_type', '=', $type];
  239. } else {
  240. $where[] = ['a.source_type', 'in', $source_type];
  241. }
  242. //明细搜索
  243. if (!empty($get['search_key']) && !empty($get['keyword'])) {
  244. $keyword = $get['keyword'];
  245. switch ($get['search_key']) {
  246. case 'user_sn' :
  247. $where[] = ['u.sn', '=', $keyword];
  248. break;
  249. case 'nickname' :
  250. $where[] = ['u.nickname', 'like', '%' . $keyword . '%'];
  251. break;
  252. }
  253. }
  254. if (empty($get['start_time']) && empty($get['end_time'])) {
  255. $where[] = ['a.create_time', '>=', strtotime(date("Y-m-d", time()))];
  256. $where[] = ['a.create_time', '<=', strtotime(date("Y-m-d", time())) + 86399];
  257. }
  258. //明细时间
  259. if (isset($get['start_time']) && $get['start_time'] != '') {
  260. $where[] = ['a.create_time', '>=', strtotime($get['start_time'])];
  261. }
  262. if (isset($get['end_time']) && $get['end_time'] != '') {
  263. $where[] = ['a.create_time', '<=', strtotime($get['end_time'])];
  264. }
  265. // 导出
  266. if (true === $is_export) {
  267. return self::accountExport($where);
  268. }
  269. $lists = AccountLog::alias('a')
  270. ->field('a.*,u.nickname,u.sn as user_sn,u.mobile')
  271. ->join('user u', 'u.id = a.user_id')
  272. ->where($where)
  273. ->page($get['page'], $get['limit'])
  274. ->order('a.id desc')
  275. ->select();
  276. $count = AccountLog::alias('a')
  277. ->field('a.*,u.nickname,u.sn as user_sn,u.mobile')
  278. ->join('user u', 'u.id = a.user_id')
  279. ->where($where)
  280. ->order('a.id desc')
  281. ->count();
  282. return ['count' => $count, 'lists' => $lists];
  283. }
  284. /**
  285. * @notes 充值明细
  286. * @param $get
  287. * @return array
  288. * @throws \think\db\exception\DataNotFoundException
  289. * @throws \think\db\exception\DbException
  290. * @throws \think\db\exception\ModelNotFoundException
  291. * @author suny
  292. * @date 2021/7/14 10:01 上午
  293. */
  294. public static function recharge($get, $is_export = false)
  295. {
  296. $where = [];
  297. //明细搜索
  298. if (isset($get['search_key']) && !empty($get['search_key'])) {
  299. $keyword = $get['keyword'];
  300. switch ($get['search_key']) {
  301. case 'nickname' :
  302. $where[] = ['u.nickname', 'like', '%' . $keyword . '%'];
  303. break;
  304. case 'order_sn' :
  305. $where[] = ['order_sn', '=', $keyword];
  306. break;
  307. case 'user_mobile' :
  308. $where[] = ['u.mobile', '=', $keyword];
  309. break;
  310. }
  311. }
  312. //订单来源
  313. if (isset($get['order_source']) && $get['order_source'] != '') {
  314. $where[] = ['r.order_source', '=', $get['order_source']];
  315. }
  316. //订单状态
  317. if (isset($get['pay_status']) && $get['pay_status'] != '') {
  318. $where[] = ['r.pay_status', '=', $get['pay_status']];
  319. }
  320. //支付方式
  321. if (isset($get['pay_way']) && $get['pay_way'] != '') {
  322. $where[] = ['r.pay_way', '=', $get['pay_way']];
  323. }
  324. if (! empty($get['start_time']) && ! empty($get['end_time'])) {
  325. $where[] = ['r.create_time', '>=', strtotime($get['start_time']) ];
  326. $where[] = ['r.create_time', '<=', strtotime($get['end_time']) ];
  327. }
  328. // 导出
  329. if (true === $is_export) {
  330. return self::rechargeExport($where);
  331. }
  332. $lists = RechargeOrder::alias('r')
  333. ->field('r.*,u.id,u.nickname,u.mobile')
  334. ->join('user u', 'u.id = r.user_id')
  335. ->where($where)
  336. ->page($get['page'], $get['limit'])
  337. ->order('r.id desc')
  338. ->select();
  339. foreach ($lists as $list) {
  340. if (!empty($list['pay_time'])) {
  341. $list['pay_time'] = date('Y-m-d H:i:s', $list['pay_time']);
  342. }
  343. }
  344. $count = RechargeOrder::alias('r')
  345. ->field('r.*,u.id,u.nickname,u.mobile')
  346. ->join('user u', 'u.id = r.user_id')
  347. ->where($where)
  348. ->order('r.id desc')
  349. ->count();
  350. return ['count' => $count, 'lists' => $lists];
  351. }
  352. /**
  353. * @notes 会员佣金提现详情
  354. * @param $id
  355. * @return array|\think\Model|null
  356. * @throws \think\db\exception\DataNotFoundException
  357. * @throws \think\db\exception\DbException
  358. * @throws \think\db\exception\ModelNotFoundException
  359. * @author suny
  360. * @date 2021/7/14 10:01 上午
  361. */
  362. public static function detail($id)
  363. {
  364. $detail = WithdrawApply::alias('w')
  365. ->field('w.*,u.sn as user_sn, u.nickname, u.mobile')
  366. ->leftJoin('user u', 'u.id=w.user_id')
  367. ->where('w.id', $id)
  368. ->find();
  369. $detail['money_qr_code'] = UrlServer::getFileUrl($detail['money_qr_code'] ?? '');
  370. $detail['typeDesc'] = WithdrawEnum::getTypeDesc($detail['type']);
  371. $detail['statusDesc'] = WithdrawEnum::getStatusDesc($detail['status']);
  372. $detail['transfer_time'] = $detail['transfer_time'] ? date('Y-m-d H:i:s', $detail['transfer_time']) : '';
  373. $detail['payment_time'] = $detail['payment_time'] ? date('Y-m-d H:i:s', $detail['payment_time']) : '';
  374. return $detail;
  375. }
  376. /**
  377. * @notes 审核通过
  378. * @param $post
  379. * @return array
  380. * @throws \think\db\exception\DataNotFoundException
  381. * @throws \think\db\exception\DbException
  382. * @throws \think\db\exception\ModelNotFoundException
  383. * @throws \think\exception\DbException
  384. * @author suny
  385. * @date 2021/7/14 10:02 上午
  386. */
  387. public static function confirm($post)
  388. {
  389. try {
  390. $id = $post['id'];
  391. $withdraw = WithdrawApply::where('id', $id)
  392. ->find();
  393. // 判断提现单是否为待提现状态 1
  394. if ($withdraw['status'] != 1) {
  395. return [
  396. 'code' => 0,
  397. 'msg' => '不是待提现申请单'
  398. ];
  399. }
  400. //提现到钱包余额
  401. if ($withdraw['type'] == WithdrawEnum::TYPE_BALANCE) {
  402. $user = User::find($withdraw['user_id']);
  403. $user->user_money = ['inc', $withdraw['left_money']];
  404. $user->save();
  405. AccountLogLogic::AccountRecord(
  406. $withdraw['user_id'],
  407. $withdraw['left_money'],
  408. 1,
  409. AccountLog::withdraw_to_balance,
  410. '',
  411. $withdraw['id'],
  412. $withdraw['sn']
  413. );
  414. //更新提现申请单状态为提现成功
  415. WithdrawApply::where('id', $id)
  416. ->update(['status' => WithdrawEnum::STATUS_SUCCESS, 'update_time' => time(), 'description' => $post['description']]);
  417. return [
  418. 'code' => 1,
  419. 'msg' => '提现至钱包余额成功'
  420. ];
  421. }
  422. //提现到微信零钱
  423. if ($withdraw['type'] == WithdrawEnum::TYPE_WECHAT_CHANGE) {
  424. // 先更新审核备注
  425. WithdrawApply::where('id', $id)
  426. ->update(['update_time' => time(), 'description' => $post['description']]);
  427. //微信零钱接口:1-企业付款到零钱;2-商家转账到零钱
  428. $transfer_way = ConfigServer::get('withdraw', 'transfer_way',1);
  429. if ($transfer_way == 1) {
  430. return WechatCorporatePaymentLogic::pay($withdraw);
  431. }
  432. if ($transfer_way == 2) {
  433. return WechatMerchantTransferLogic::transfer($withdraw);
  434. }
  435. }
  436. //提现到微信收款码、支付收款码
  437. if ($withdraw['type'] == WithdrawEnum::TYPE_WECHAT_CODE || $withdraw['type'] == WithdrawEnum::TYPE_ALI_CODE || WithdrawEnum::TYPE_BANK) {
  438. // 直接标识为提现中状态
  439. WithdrawApply::where('id', $id)
  440. ->update(['status' => WithdrawEnum::STATUS_ING, 'update_time' => time(), 'description' => $post['description']]);
  441. return [
  442. 'code' => 1,
  443. 'msg' => '审核通过,提现中'
  444. ];
  445. }
  446. return [
  447. 'code' => 1,
  448. 'msg' => '审核通过,提现中'
  449. ];
  450. } catch(\Throwable $e) {
  451. return [
  452. 'code' => 0,
  453. 'msg' => $e->getMessage(),
  454. ];
  455. }
  456. }
  457. /**
  458. * @notes 审核拒绝
  459. * @param $post
  460. * @throws \think\exception\PDOException
  461. * @author suny
  462. * @date 2021/7/14 10:03 上午
  463. */
  464. public static function refuse($post)
  465. {
  466. Db::startTrans();
  467. try {
  468. $withdraw_apply = WithdrawApply::where('id', $post['id'])->find();
  469. $withdraw_apply->status = WithdrawEnum::STATUS_FAIL; // 提现失败
  470. $withdraw_apply->description = $post['description'];
  471. $withdraw_apply->update_time = time();
  472. $withdraw_apply->save();
  473. //拒绝提现,回退佣金
  474. $user = User::find($withdraw_apply['user_id']);
  475. $user->earnings = ['inc', $withdraw_apply['money']];
  476. $user->save();
  477. //增加佣金变动记录
  478. AccountLogLogic::AccountRecord(
  479. $withdraw_apply['user_id'],
  480. $withdraw_apply['money'],
  481. 1,
  482. AccountLog::withdraw_back_earnings,
  483. '',
  484. $withdraw_apply['id'],
  485. $withdraw_apply['sn']
  486. );
  487. Db::commit();
  488. } catch (Exception $e) {
  489. Db::rollback();
  490. }
  491. }
  492. /**
  493. * @notes 审核拒绝
  494. * @param $post
  495. * @return array
  496. * @throws \think\db\exception\DataNotFoundException
  497. * @throws \think\db\exception\DbException
  498. * @throws \think\db\exception\ModelNotFoundException
  499. * @throws \think\exception\DbException
  500. * @author suny
  501. * @date 2021/7/14 10:03 上午
  502. */
  503. public static function transferFail($post)
  504. {
  505. if (empty($post['transfer_description'])) {
  506. return [
  507. 'code' => 0,
  508. 'msg' => '请填写转账说明'
  509. ];
  510. }
  511. // 标识提现失败
  512. WithdrawApply::where('id', $post['id'])->update([
  513. 'status' => 4, // 提现失败
  514. 'transfer_voucher' => $post['transfer_voucher'] ? $post['transfer_voucher'] : '',
  515. 'transfer_description' => $post['transfer_description'],
  516. 'update_time' => time()
  517. ]);
  518. $withdraw_apply = WithdrawApply::where('id', $post['id'])->find();
  519. // 退回佣金
  520. $user = User::find($withdraw_apply['user_id']);
  521. $user->earnings = ['inc', $withdraw_apply['money']];
  522. $user->save();
  523. //增加佣金变动记录
  524. AccountLogLogic::AccountRecord(
  525. $withdraw_apply['user_id'],
  526. $withdraw_apply['money'],
  527. 1,
  528. AccountLog::withdraw_back_earnings,
  529. '',
  530. $withdraw_apply['id'],
  531. $withdraw_apply['sn']
  532. );
  533. return [
  534. 'code' => 1,
  535. 'msg' => '转账失败,提现金额已退回佣金账户'
  536. ];
  537. }
  538. /**
  539. * @notes 转账成功
  540. * @param $post
  541. * @return array
  542. * @author suny
  543. * @date 2021/7/14 10:03 上午
  544. */
  545. public static function transferSuccess($post)
  546. {
  547. if (empty($post['transfer_voucher'])) {
  548. return [
  549. 'code' => 0,
  550. 'msg' => '请上传转账凭证'
  551. ];
  552. }
  553. $post['transfer_voucher'] = UrlServer::getFileUrl($post['transfer_voucher']);
  554. if (empty($post['transfer_description'])) {
  555. return [
  556. 'code' => 0,
  557. 'msg' => '请填写转账说明'
  558. ];
  559. }
  560. // 标识提现成功
  561. WithdrawApply::where('id', $post['id'])->update([
  562. 'status' => 3, // 提现成功
  563. 'transfer_voucher' => $post['transfer_voucher'],
  564. 'transfer_description' => $post['transfer_description'],
  565. 'update_time' => time(),
  566. 'transfer_time' => time()
  567. ]);
  568. return [
  569. 'code' => 1,
  570. 'msg' => '转账成功'
  571. ];
  572. }
  573. /**
  574. * @notes 提现失败
  575. * @param $id
  576. * @throws \think\db\exception\DataNotFoundException
  577. * @throws \think\db\exception\DbException
  578. * @throws \think\db\exception\ModelNotFoundException
  579. * @throws \think\exception\DbException
  580. * @author suny
  581. * @date 2021/7/14 10:03 上午
  582. */
  583. public static function withdrawFailed($id)
  584. {
  585. $withdraw_apply = WithdrawApply::where('id', $id)->find();
  586. $withdraw_apply->status = WithdrawEnum::STATUS_FAIL; // 提现失败
  587. $withdraw_apply->update_time = time();
  588. $withdraw_apply->save();
  589. //拒绝提现,回退佣金
  590. $user = User::find($withdraw_apply['user_id']);
  591. $user->earnings = ['inc', $withdraw_apply['money']];
  592. $user->save();
  593. //增加佣金变动记录
  594. AccountLogLogic::AccountRecord(
  595. $withdraw_apply['user_id'],
  596. $withdraw_apply['money'],
  597. 1,
  598. AccountLog::withdraw_back_earnings,
  599. '',
  600. $withdraw_apply['id'],
  601. $withdraw_apply['sn']
  602. );
  603. }
  604. /**
  605. * @notes 搜索
  606. * @param $id
  607. * @return array
  608. * @throws \think\db\exception\DataNotFoundException
  609. * @throws \think\db\exception\DbException
  610. * @throws \think\db\exception\ModelNotFoundException
  611. * @author suny
  612. * @date 2021/7/14 10:03 上午
  613. */
  614. public static function search($id)
  615. {
  616. $withdraw = WithdrawApply::where('id', $id)
  617. ->find();
  618. // 判断提现单是否为提现中状态 2 且 提现方式为 微信零钱 2
  619. if ($withdraw['status'] == 2 && $withdraw['type'] == 2) {
  620. //微信零钱接口:1-企业付款到零钱;2-商家转账到零钱
  621. $transfer_way = ConfigServer::get('withdraw', 'transfer_way',1);
  622. if ($transfer_way == 1) {
  623. return WechatCorporatePaymentLogic::search($withdraw);
  624. }
  625. if ($transfer_way == 2) {
  626. $result = WechatMerchantTransferLogic::details($withdraw);
  627. // 记录查询结果
  628. WithdrawApply::update(['update_time'=>time(),'pay_search_desc'=>json_encode($result, JSON_UNESCAPED_UNICODE)],['id'=>$withdraw['id']]);
  629. if(isset($result['state'])) {
  630. if ($result['state'] == 'SUCCESS') {
  631. // 转账成功,标记提现申请单为提现成功,记录支付信息
  632. WithdrawApply::update(['status'=>3,'payment_no'=>$result['transfer_bill_no'],'payment_time'=>strtotime($result['update_time'])],['id'=>$withdraw['id']]);
  633. return ['code' => 1, 'msg' => '提现成功'];
  634. }
  635. if ($result['state'] == 'FAIL') {
  636. // 转账失败
  637. WithdrawApply::update(['status'=>4],['id'=>$withdraw['id']]);
  638. //回退佣金
  639. $user = User::find($withdraw['user_id']);
  640. $user->earnings = ['inc', $withdraw['money']];
  641. $user->save();
  642. //增加佣金变动记录
  643. AccountLogLogic::AccountRecord(
  644. $withdraw['user_id'],
  645. $withdraw['money'],
  646. 1,
  647. AccountLog::withdraw_back_earnings,
  648. '',
  649. $withdraw['id'],
  650. $withdraw['sn']
  651. );
  652. return ['code' => 1, 'msg' => '提现至微信零钱失败'];
  653. }
  654. if ($result['state'] == 'PROCESSING') {
  655. return ['code' => 0, 'msg' => '正在处理中'];
  656. }
  657. }else{
  658. return ['code' => 0, 'msg' => $result['message'] ?? '商家转账到零钱查询失败'];
  659. }
  660. }
  661. } else {
  662. return [
  663. 'code' => 0,
  664. 'msg' => '不是提现中的微信零钱申请单,无法查询'
  665. ];
  666. }
  667. }
  668. /**
  669. * @notes 导出Excel
  670. * @param array $where
  671. * @return array|false
  672. * @author 段誉
  673. * @date 2022/4/24 10:10
  674. */
  675. public static function rechargeExport($where)
  676. {
  677. try {
  678. $lists = RechargeOrder::alias('r')
  679. ->field('r.*,u.id,u.nickname,u.mobile')
  680. ->join('user u', 'u.id = r.user_id')
  681. ->where($where)
  682. ->order('r.id desc')
  683. ->select()->toArray();
  684. foreach ($lists as &$list) {
  685. if (!empty($list['pay_time'])) {
  686. $list['pay_time'] = date('Y-m-d H:i:s', $list['pay_time']);
  687. }
  688. }
  689. $excelFields = [
  690. 'order_sn' => '订单编号',
  691. 'nickname' => '用户昵称',
  692. 'mobile' => '用户手机号',
  693. 'order_amount' => '充值金额',
  694. 'give_money' => '赠送金额',
  695. 'give_growth' => '赠送成长值',
  696. 'pay_way' => '支付方式',
  697. 'pay_time' => '支付时间',
  698. 'pay_status' => '订单状态',
  699. 'create_time' => '下单时间',
  700. ];
  701. $export = new ExportExcelServer();
  702. $export->setFileName('充值明细');
  703. $result = $export->createExcel($excelFields, $lists);
  704. return ['url' => $result];
  705. } catch (\Exception $e) {
  706. self::$error = $e->getMessage();
  707. return false;
  708. }
  709. }
  710. /**
  711. * @notes 导出Excel
  712. * @param array $where
  713. * @return array|false
  714. * @author 段誉
  715. * @date 2022/4/24 10:10
  716. */
  717. public static function accountExport($where)
  718. {
  719. try {
  720. $lists = AccountLog::alias('a')
  721. ->field('a.*,u.nickname,u.sn as user_sn,u.mobile')
  722. ->join('user u', 'u.id = a.user_id')
  723. ->where($where)
  724. ->order('a.id desc')
  725. ->select();
  726. $excelFields = [
  727. 'nickname' => '会员昵称',
  728. 'user_sn' => '会员编号',
  729. 'mobile' => '手机号码',
  730. 'change_amount' => '变动金额',
  731. 'left_amount' => '剩余金额',
  732. 'source_type' => '明细类型',
  733. 'source_sn' => '来源单号',
  734. 'create_time' => '记录时间',
  735. ];
  736. $export = new ExportExcelServer();
  737. $export->setFileName('账户明细');
  738. $result = $export->createExcel($excelFields, $lists);
  739. return ['url' => $result];
  740. } catch (\Exception $e) {
  741. self::$error = $e->getMessage();
  742. return false;
  743. }
  744. }
  745. /**
  746. * @notes 导出Excel
  747. * @param array $condition
  748. * @return array|false
  749. * @author 段誉
  750. * @date 2022/4/24 10:10
  751. */
  752. public static function commissionExport($where)
  753. {
  754. try {
  755. $lists = AccountLog::alias('a')
  756. ->field('a.*,u.nickname,u.sn as user_sn,u.mobile,w.sn as withdraw_sn')
  757. ->join('user u', 'u.id = a.user_id')
  758. ->leftjoin('withdraw_apply w', 'w.sn = a.source_sn')
  759. ->where($where)
  760. ->order('a.id desc')
  761. ->select();
  762. $excelFields = [
  763. 'nickname' => '会员昵称',
  764. 'user_sn' => '会员编号',
  765. 'mobile' => '手机号码',
  766. 'change_amount' => '变动金额',
  767. 'left_amount' => '剩余佣金',
  768. 'source_type' => '明细类型',
  769. 'withdraw_sn' => '来源单号',
  770. 'create_time' => '记录时间',
  771. ];
  772. $export = new ExportExcelServer();
  773. $export->setFileName('佣金明细');
  774. $result = $export->createExcel($excelFields, $lists);
  775. return ['url' => $result];
  776. } catch (\Exception $e) {
  777. self::$error = $e->getMessage();
  778. return false;
  779. }
  780. }
  781. /**
  782. * @notes 导出Excel
  783. * @param array $condition
  784. * @return array|false
  785. * @author 段誉
  786. * @date 2022/4/24 10:10
  787. */
  788. public static function withdrawExport($where)
  789. {
  790. try {
  791. $lists = WithdrawApply::alias('w')
  792. ->field('w.*, u.nickname,u.avatar, u.sn as user_sn, u.mobile, ul.name as user_level_name')
  793. ->with('user')
  794. ->leftJoin('user u', 'u.id = w.user_id')
  795. ->leftJoin('user_level ul', 'ul.id = u.level')
  796. ->where($where)
  797. ->order('w.id desc')
  798. ->select();
  799. foreach ($lists as &$item) {
  800. $item['type_text'] = WithdrawEnum::getTypeDesc($item['type']);
  801. $item['status_text'] = WithdrawEnum::getStatusDesc($item['status']);
  802. }
  803. $excelFields = [
  804. 'sn' => '提现单号',
  805. 'nickname' => '会员昵称',
  806. 'user_sn' => '会员编号',
  807. 'mobile' => '手机号码',
  808. 'left_money' => '提现金额',
  809. 'type_text' => '提现方式',
  810. 'status_text' => '提现状态',
  811. 'remark' => '提现说明',
  812. 'create_time' => '提现时间',
  813. ];
  814. $export = new ExportExcelServer();
  815. $export->setFileName('佣金提现');
  816. $result = $export->createExcel($excelFields, $lists);
  817. return ['url' => $result];
  818. } catch (\Exception $e) {
  819. self::$error = $e->getMessage();
  820. return false;
  821. }
  822. }
  823. }