WithdrawalListController.php 18 KB

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