WithdrawalListController.php 18 KB

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