WithdrawalListController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. namespace app\admin\controller\finance;
  3. use app\admin\service\goods\GoodsSkuService;
  4. use app\admin\service\member\MemberService;
  5. use app\admin\service\order\CommissionService;
  6. use app\controller\Curd;
  7. use app\model\ClientConfig;
  8. use app\model\Member;
  9. use app\model\MemberAccount;
  10. use app\model\MemberAccountList;
  11. use app\model\Order;
  12. use app\model\OrderSheet;
  13. use support\Db;
  14. use support\exception\BusinessException;
  15. use support\Request;
  16. use support\Response;
  17. use Webman\Event\Event;
  18. class WithdrawalListController extends Curd
  19. {
  20. public function __construct()
  21. {
  22. $this->model = new MemberAccountList();
  23. }
  24. public function select(Request $request): Response
  25. {
  26. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  27. $order = $request->get('order', 'desc');
  28. $field = $field ?? 'member_account_list_addtimes';
  29. if (!empty($where['member_account_list_addtimes'])) {
  30. $where['member_account_list_addtimes'][0] = strtotime($where['member_account_list_addtimes'][0]);
  31. $where['member_account_list_addtimes'][1] = strtotime($where['member_account_list_addtimes'][1]);
  32. }
  33. $query = $this->doSelect($where, $field, $order);
  34. return $this->doFormat($query, $format, $limit);
  35. }
  36. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  37. {
  38. $model = $this->model->with([
  39. 'member' => function ($query) {
  40. $query->select('member_id', 'member_mobile', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer', 'member_is_franchisee');
  41. },
  42. 'memberInfo' => function ($query) {
  43. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  44. },
  45. 'memberCert' => function ($query) {
  46. $query->select('join_cert_member_id', 'member_cert_name');
  47. }
  48. ]);
  49. foreach ($where as $column => $value) {
  50. if (is_array($value)) {
  51. if ($value[0] === 'like' || $value[0] === 'not like') {
  52. $model = $model->where($column, $value[0], "%$value[1]%");
  53. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  54. $model = $model->where($column, $value[0], $value[1]);
  55. } elseif ($value[0] == 'in' && !empty($value[1])) {
  56. $valArr = $value[1];
  57. if (is_string($value[1])) {
  58. $valArr = explode(",", trim($value[1]));
  59. }
  60. $model = $model->whereIn($column, $valArr);
  61. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  62. $valArr = $value[1];
  63. if (is_string($value[1])) {
  64. $valArr = explode(",", trim($value[1]));
  65. }
  66. $model = $model->whereNotIn($column, $valArr);
  67. } elseif ($value[0] == 'null') {
  68. $model = $model->whereNull($column);
  69. } elseif ($value[0] == 'not null') {
  70. $model = $model->whereNotNull($column);
  71. } elseif ($value[0] !== '' || $value[1] !== '') {
  72. $model = $model->whereBetween($column, $value);
  73. }
  74. } else {
  75. $model = $model->where($column, $value);
  76. }
  77. }
  78. if ($field) {
  79. $model = $model->orderBy($field, $order);
  80. }
  81. if (!empty($where['member_account_list_status']) && $where['member_account_list_status'] == 'CANCEL') {
  82. $model = $model->whereJsonDoesntContain('member_account_list_extend_json->reason', '');
  83. }
  84. return $model;
  85. }
  86. public function afterQuery($items)
  87. {
  88. foreach ($items as &$item) {
  89. $item['member_mobile'] = isset($item['member']) ? $item['member']['member_mobile'] : '';
  90. $item['member_info_nickname'] = isset($item['memberInfo']) && !empty($item['memberInfo']['member_info_nickname']) ? $item['memberInfo']['member_info_nickname'] : '';
  91. $item['member_cert_name'] = isset($item['memberCert']) && !empty($item['memberCert']['member_cert_name']) ? $item['memberCert']['member_cert_name'] : '';
  92. $item['member_name'] = MemberService::getMemberName($item['member_mobile'], $item['member_cert_name'], $item['member_info_nickname']);
  93. $item['member_info_headimg'] = !empty($item['memberInfo']) ? MemberService::getAvatarUrl($item['memberInfo']['member_info_headimg']) : "";
  94. $item['member_is_owner'] = isset($item['member']) ? $item['member']['member_is_owner'] : '';
  95. $item['member_is_vip'] = isset($item['member']) ? $item['member']['member_is_vip'] : '';
  96. $item['member_is_partner'] = isset($item['member']) ? $item['member']['member_is_partner'] : '';
  97. $item['member_is_referrer'] = isset($item['member']) ? $item['member']['member_is_referrer'] : '';
  98. $item['member_is_franchisee'] = isset($item['member']) ? $item['member']['member_is_franchisee'] : '';
  99. unset($item['member'], $item['memberInfo'], $item['memberCert']);
  100. if (!empty($item['member_account_list_extend_json'])) {
  101. $memberAccountListExtendJson = json_decode($item['member_account_list_extend_json'], true);
  102. if (isset($memberAccountListExtendJson['reason'])) {
  103. $item['reason'] = $memberAccountListExtendJson['reason'];
  104. }
  105. }
  106. if (!empty($item['member_account_list_json'])) {
  107. $accountListJson = json_decode($item['member_account_list_json'], true);
  108. $item['out_rate'] = !empty($accountListJson['out_rate']) ? 0.6 : 0;
  109. $item['out_rate_amount'] = $accountListJson['out_rate'] ?? 0;
  110. $item['to_account'] = sprintf('%.2f', $item['member_account_list_amount'] - $item['out_rate_amount']);
  111. }
  112. }
  113. return $items;
  114. }
  115. /**
  116. * @Desc 提现顶部统计
  117. * @Author Gorden
  118. * @Date 2024/10/31 10:49
  119. *
  120. * @param Request $request
  121. * @return Response
  122. */
  123. public function statistics(Request $request)
  124. {
  125. $memberAccountListAddtimes = $request->get('member_account_list_addtimes');
  126. $memberId = $request->get('join_member_account_list_member_id');
  127. $status = $request->get('member_account_list_status');
  128. if (!empty($memberAccountListAddtimes)) {
  129. $memberAccountListAddtimes[0] = strtotime($memberAccountListAddtimes[0]);
  130. $memberAccountListAddtimes[1] = strtotime($memberAccountListAddtimes[1]);
  131. }
  132. $withdrawalModel = MemberAccountList::where('member_account_list_attr', 'OUT')
  133. ->when(!empty($memberAccountListAddtimes), function ($query) use ($memberAccountListAddtimes) {
  134. $query->whereBetween('member_account_list_addtimes', $memberAccountListAddtimes);
  135. })->when(!empty($memberId), function ($query) use ($memberId) {
  136. $query->where('join_member_account_list_member_id', $memberId);
  137. })->when(!empty($status), function ($query) use ($status) {
  138. $query->where('member_account_list_status', $status);
  139. });
  140. $totalModel = clone $withdrawalModel;
  141. $total = $totalModel->selectRaw("SUM(member_account_list_amount) as amount,count(member_account_list_id) as total")
  142. ->first()
  143. ->toArray();
  144. $pendingTotalModel = clone $withdrawalModel;
  145. $pendingTotal = $pendingTotalModel->where('member_account_list_status', 'PENDING')
  146. ->selectRaw("SUM(member_account_list_amount) as amount,count(member_account_list_id) as total")
  147. ->first()
  148. ->toArray();
  149. $waitingTotalModel = clone $withdrawalModel;
  150. $waitingTotal = $waitingTotalModel->where('member_account_list_status', 'WAITING')
  151. ->selectRaw("SUM(member_account_list_amount) as amount,count(member_account_list_id) as total")
  152. ->first()
  153. ->toArray();
  154. $activedTotalModel = clone $withdrawalModel;
  155. $activedTotal = $activedTotalModel->where('member_account_list_status', 'ACTIVED')
  156. ->selectRaw("SUM(member_account_list_amount) as amount,count(member_account_list_id) as total")
  157. // ->selectRaw("SUM(member_account_list_amount) as amount,SUM(JSON_UNQUOTE(JSON_EXTRACT(member_account_list_json,'$.surplus')) * JSON_UNQUOTE(JSON_EXTRACT(member_account_list_json,'$.out_rate')) / 1000) as rate_amount,count(member_account_list_id) as total")
  158. ->first()
  159. ->toArray();
  160. // 实际打款是总额减去手续费
  161. if (!empty($activedTotal['amount']) && !empty($activedTotal['rate_amount'])) {
  162. $activedTotal['amount'] = sprintf('%.2f', $activedTotal['amount'] - $activedTotal['rate_amount']);
  163. }
  164. $rejectTotalModel = clone $withdrawalModel;
  165. $rejectTotal = $rejectTotalModel->where('member_account_list_status', 'CANCEL')
  166. ->whereJsonDoesntContain('member_account_list_extend_json->reason', '')
  167. ->selectRaw("SUM(member_account_list_amount) as amount,count(member_account_list_id) as total")
  168. ->first()
  169. ->toArray();
  170. return json_success('success', [
  171. 'total' => $total,
  172. 'pendingTotal' => $pendingTotal,
  173. 'waitingTotal' => $waitingTotal,
  174. 'activedTotal' => $activedTotal,
  175. 'rejectTotal' => $rejectTotal
  176. ]);
  177. }
  178. /**
  179. * @Desc 记录详情
  180. * @Author Gorden
  181. * @Date 2024/9/28 17:15
  182. *
  183. * @param Request $request
  184. * @return Response
  185. */
  186. public function info(Request $request): Response
  187. {
  188. $id = $request->get('member_account_list_id');
  189. if (!$id) {
  190. return json_fail("参数异常");
  191. }
  192. $info = MemberAccountList::where('member_account_list_id', $id)->first();
  193. $infoMember = Member::with([
  194. 'role' => function ($query) {
  195. $query->select('member_role_id', 'member_role_name');
  196. }
  197. ])->where('member_id', $info->join_member_account_list_member_id)
  198. ->first();
  199. $info->member_role_name = !empty($infoMember->role) ? $infoMember->role->member_role_name : '';
  200. // 获取银行卡信息
  201. if (!empty($info->member_account_list_json)) {
  202. $accountListJson = json_decode($info->member_account_list_json, true);
  203. $info->out_rate = !empty($accountListJson['out_rate']) ? 0.6 : 0;
  204. $info->out_rate_amount = $accountListJson['out_rate'] ?? 0;
  205. $info->to_account = sprintf('%.2f', $info->member_account_list_amount - $info->out_rate_amount);
  206. $info->banks = [
  207. [
  208. 'bank_name' => $accountListJson['bank_name'] ?? '',
  209. 'username' => $accountListJson['bank_acount'] ?? '',
  210. 'cardno' => $accountListJson['bank_cardno'] ?? ''
  211. ]
  212. ];
  213. }
  214. if (!empty($info->member_account_list_extend_json)) {
  215. $memberAccountListExtendJson = json_decode($info->member_account_list_extend_json, true);
  216. if (isset($memberAccountListExtendJson['reason'])) {
  217. $info->reason = $memberAccountListExtendJson['reason'];
  218. }
  219. if (isset($memberAccountListExtendJson['resulttime'])) {
  220. $info->resulttime = $memberAccountListExtendJson['resulttime'];
  221. }
  222. }
  223. // $info->banks = ClientConfig::where('join_client_config_member_id',$info->join_member_account_list_member_id)->where('client_config_key','client-bank')->get()->toArray();
  224. return json_success('success', $info);
  225. }
  226. public function changeStatus(Request $request)
  227. {
  228. $memberAccountListId = $request->post('member_account_list_id');
  229. $status = $request->post('status');
  230. $password = $request->post('pay_pwd');
  231. if ($status == 'ACTIVED' && $password != '888666') {
  232. return json_fail('密码错误');
  233. }
  234. Db::beginTransaction();
  235. try {
  236. $withdraw = MemberAccountList::where('member_account_list_id', $memberAccountListId)->first();
  237. $withdraw->member_account_list_status = $status;
  238. $memberAccountListJson = [];
  239. if (!empty($withdraw->member_account_list_json)) {
  240. $memberAccountListJson = json_decode($withdraw->member_account_list_json, true);
  241. }
  242. $memberAccountListExtendJson = [];
  243. if (!empty($withdraw->member_account_list_extend_json)) {
  244. $memberAccountListExtendJson = json_decode($withdraw->member_account_list_extend_json, true);
  245. }
  246. if ($status == 'ACTIVED') {
  247. $memberAccountListJson['resulttime'] = date('Y-m-d H:i:s');
  248. $memberAccountListExtendJson['resulttime'] = date('Y-m-d H:i:s');
  249. // 扣账户余额
  250. $account = MemberAccount::where('join_account_member_id', $withdraw->join_member_account_list_member_id)
  251. ->where('member_account_classify', 'CASH')
  252. ->where('member_account_status', 'ACTIVED')
  253. ->first();
  254. if (empty($account)) {
  255. throw new BusinessException("账户异常");
  256. }
  257. if ($account->member_account_surplus < $withdraw->member_account_list_amount) {
  258. throw new BusinessException("账户余额不足");
  259. }
  260. $account->member_account_surplus = $account->member_account_surplus - $withdraw->member_account_list_amount;
  261. $account->member_account_expend = $account->member_account_expend + $withdraw->member_account_list_amount;
  262. $account->save();
  263. } else if ($status == 'CANCEL') {
  264. $memberAccountListExtendJson['resulttime'] = date('Y-m-d H:i:s');
  265. $memberAccountListExtendJson['reason'] = $request->post('reason', '');
  266. if ($request->post('is_return', false)) {
  267. CommissionService::withdrawReject($withdraw->join_member_account_list_member_id, $withdraw->member_account_list_amount, json_encode($memberAccountListExtendJson));
  268. }
  269. }
  270. $withdraw->member_account_list_extend_json = json_encode($memberAccountListExtendJson);
  271. $withdraw->member_account_list_outed = 'Y';
  272. $withdraw->save();
  273. _syslog('提现', '修改状态成功');
  274. Db::commit();
  275. if ($status == 'ACTIVED') {
  276. // 入收支记录
  277. $params['account_list_id'] = $memberAccountListId;
  278. $params['type'] = 'withdraw';
  279. Event::dispatch('statistics.inout.out', $params);
  280. }
  281. return json_success('success');
  282. } catch (BusinessException $e) {
  283. Db::rollBack();
  284. return json_fail($e->getMessage());
  285. } catch (\Exception $e) {
  286. Db::rollBack();
  287. return json_fail("修改状态失败");
  288. }
  289. }
  290. /**
  291. * @Desc 提现导出
  292. * @Author Gorden
  293. * @Date 2024/11/1 17:12
  294. *
  295. * @param Request $request
  296. * @return Response
  297. */
  298. public function export(Request $request)
  299. {
  300. $memberAccountListAddtimes = $request->get('member_account_list_addtimes');
  301. $memberId = $request->get('join_member_account_list_member_id');
  302. $memberAccountListStatus = $request->get('member_account_list_status');
  303. $memberAccountListIds = $request->get('member_account_list_ids');
  304. $withdrawal = MemberAccountList::with([
  305. 'member' => function ($query) {
  306. $query->select('member_id', 'member_mobile');
  307. },
  308. 'memberCert' => function ($query) {
  309. $query->select('join_cert_member_id', 'member_cert_name');
  310. }
  311. ])->where('member_account_list_attr', 'OUT');
  312. if (!empty($memberId)) {
  313. $withdrawal = $withdrawal->where('join_member_account_list_member_id', $memberId);
  314. }
  315. if (!empty($memberAccountListStatus)) {
  316. $withdrawal = $withdrawal->where('member_account_list_status', $memberAccountListStatus);
  317. }
  318. if (!empty($memberAccountListAddtimes)) {
  319. $memberAccountListAddtimes[0] = strtotime($memberAccountListAddtimes[0]);
  320. $memberAccountListAddtimes[1] = strtotime($memberAccountListAddtimes[1]);
  321. $withdrawal = $withdrawal->whereBetween('member_account_list_addtimes', $memberAccountListAddtimes);
  322. }
  323. if (!empty($memberAccountListIds)) {
  324. $withdrawal = $withdrawal->whereIn('member_account_list_id', $memberAccountListIds);
  325. }
  326. $withdrawal = $withdrawal->orderBy("member_account_list_addtimes", 'DESC')->get()->toArray();
  327. $data = [];
  328. foreach ($withdrawal as $item) {
  329. $mobile = !empty($item['member']) ? $item['member']['member_mobile'] : '';
  330. $certName = !empty($item['member_cert']) ? $item['member_cert']['member_cert_name'] : '';
  331. $rateAmount = 0;
  332. if (!empty($item['member_account_list_json'])) {
  333. $memberAccountListJson = json_decode($item['member_account_list_json'], true);
  334. if (isset($memberAccountListJson['out_rate'])) {
  335. $item['out_rate'] = ($memberAccountListJson['out_rate'] / 10);
  336. $rateAmount = round($item['member_account_list_amount'] * $item['out_rate'] / 100, 2);
  337. $item['out_rate'] = $item['out_rate'] . '%';
  338. }
  339. }
  340. $item['to_account'] = sprintf('%.2f', $item['member_account_list_amount'] - $rateAmount);
  341. if (!empty($item['member_account_list_extend_json']) && $item['member_account_list_status'] == 'CANCEL') {
  342. $extendJson = json_decode($item['member_account_list_extend_json'], true);
  343. if (!empty($extendJson['reason'])) {
  344. $item['member_account_list_status'] = 'REJECT';
  345. }
  346. }
  347. $data[] = [
  348. 'member_account_list_addtimes' => $item['member_account_list_addtimes'],
  349. 'member_name' => MemberService::getMemberCertName($mobile, $certName, ''),
  350. 'member_account_list_category' => $item['member_account_list_category'],
  351. 'member_account_list_amount' => $item['member_account_list_amount'],
  352. 'out_rate' => $item['out_rate'] ?? '0',
  353. 'to_account' => $item['to_account'],
  354. 'member_account_list_status' => self::$status[$item['member_account_list_status']]
  355. ];
  356. }
  357. return json_success('success', $data);
  358. }
  359. public static $status = [
  360. 'PENDING' => '待处理',
  361. 'WAITING' => '处理中',
  362. 'ACTIVED' => '已完成',
  363. 'REJECT' => '已驳回',
  364. 'CANCEL' => '已取消'
  365. ];
  366. }