WithdrawalListController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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']) ? $accountListJson['out_rate'] / 10 : 0;
  107. $item['to_account'] = sprintf('%.2f', $item['member_account_list_amount'] - (round($item['member_account_list_amount'] * $item['out_rate'] / 100, 2)));
  108. }
  109. }
  110. return $items;
  111. }
  112. /**
  113. * @Desc 提现顶部统计
  114. * @Author Gorden
  115. * @Date 2024/10/31 10:49
  116. *
  117. * @param Request $request
  118. * @return Response
  119. */
  120. public function statistics(Request $request)
  121. {
  122. $memberAccountListAddtimes = $request->get('member_account_list_addtimes');
  123. $memberId = $request->get('join_member_account_list_member_id');
  124. $status = $request->get('member_account_list_status');
  125. if (!empty($memberAccountListAddtimes)) {
  126. $memberAccountListAddtimes[0] = strtotime($memberAccountListAddtimes[0]);
  127. $memberAccountListAddtimes[1] = strtotime($memberAccountListAddtimes[1]);
  128. }
  129. $withdrawalModel = MemberAccountList::where('member_account_list_attr', 'OUT')
  130. ->when(!empty($memberAccountListAddtimes), function ($query) use ($memberAccountListAddtimes) {
  131. $query->whereBetween('member_account_list_addtimes', $memberAccountListAddtimes);
  132. })->when(!empty($memberId), function ($query) use ($memberId) {
  133. $query->where('join_member_account_list_member_id', $memberId);
  134. })->when(!empty($status), function ($query) use ($status) {
  135. $query->where('member_account_list_status', $status);
  136. });
  137. $totalModel = clone $withdrawalModel;
  138. $total = $totalModel->selectRaw("SUM(member_account_list_amount) as amount,count(member_account_list_id) as total")
  139. ->first()
  140. ->toArray();
  141. $pendingTotalModel = clone $withdrawalModel;
  142. $pendingTotal = $pendingTotalModel->where('member_account_list_status', 'PENDING')
  143. ->selectRaw("SUM(member_account_list_amount) as amount,count(member_account_list_id) as total")
  144. ->first()
  145. ->toArray();
  146. $waitingTotalModel = clone $withdrawalModel;
  147. $waitingTotal = $waitingTotalModel->where('member_account_list_status', 'WAITING')
  148. ->selectRaw("SUM(member_account_list_amount) as amount,count(member_account_list_id) as total")
  149. ->first()
  150. ->toArray();
  151. $activedTotalModel = clone $withdrawalModel;
  152. $activedTotal = $activedTotalModel->where('member_account_list_status', 'ACTIVED')
  153. ->selectRaw("SUM(member_account_list_amount) as amount,count(member_account_list_id) as total")
  154. // ->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")
  155. ->first()
  156. ->toArray();
  157. // 实际打款是总额减去手续费
  158. if (!empty($activedTotal['amount']) && !empty($activedTotal['rate_amount'])) {
  159. $activedTotal['amount'] = sprintf('%.2f', $activedTotal['amount'] - $activedTotal['rate_amount']);
  160. }
  161. $rejectTotalModel = clone $withdrawalModel;
  162. $rejectTotal = $rejectTotalModel->where('member_account_list_status', 'CANCEL')
  163. ->whereJsonDoesntContain('member_account_list_extend_json->reason', '')
  164. ->selectRaw("SUM(member_account_list_amount) as amount,count(member_account_list_id) as total")
  165. ->first()
  166. ->toArray();
  167. return json_success('success', [
  168. 'total' => $total,
  169. 'pendingTotal' => $pendingTotal,
  170. 'waitingTotal' => $waitingTotal,
  171. 'activedTotal' => $activedTotal,
  172. 'rejectTotal' => $rejectTotal
  173. ]);
  174. }
  175. /**
  176. * @Desc 记录详情
  177. * @Author Gorden
  178. * @Date 2024/9/28 17:15
  179. *
  180. * @param Request $request
  181. * @return Response
  182. */
  183. public function info(Request $request): Response
  184. {
  185. $id = $request->get('member_account_list_id');
  186. if (!$id) {
  187. return json_fail("参数异常");
  188. }
  189. $info = MemberAccountList::where('member_account_list_id', $id)->first();
  190. $infoMember = Member::with([
  191. 'role' => function ($query) {
  192. $query->select('member_role_id', 'member_role_name');
  193. }
  194. ])->where('member_id', $info->join_member_account_list_member_id)
  195. ->first();
  196. $info->member_role_name = !empty($infoMember->role) ? $infoMember->role->member_role_name : '';
  197. // 获取银行卡信息
  198. if (!empty($info->member_account_list_json)) {
  199. $accountListJson = json_decode($info->member_account_list_json, true);
  200. $info->out_rate = !empty($accountListJson['out_rate']) ? $accountListJson['out_rate'] / 10 : 0;
  201. $info->to_account = sprintf('%.2f', $info->member_account_list_amount - (round($info->member_account_list_amount * $info->out_rate / 100, 2)));
  202. $info->banks = [
  203. [
  204. 'bank_name' => $accountListJson['bank_name'] ?? '',
  205. 'username' => $accountListJson['bank_acount'] ?? '',
  206. 'cardno' => $accountListJson['bank_cardno'] ?? ''
  207. ]
  208. ];
  209. }
  210. if (!empty($info->member_account_list_extend_json)) {
  211. $memberAccountListExtendJson = json_decode($info->member_account_list_extend_json, true);
  212. if (isset($memberAccountListExtendJson['reason'])) {
  213. $info->reason = $memberAccountListExtendJson['reason'];
  214. }
  215. if (isset($memberAccountListExtendJson['resulttime'])) {
  216. $info->resulttime = $memberAccountListExtendJson['resulttime'];
  217. }
  218. }
  219. // $info->banks = ClientConfig::where('join_client_config_member_id',$info->join_member_account_list_member_id)->where('client_config_key','client-bank')->get()->toArray();
  220. return json_success('success', $info);
  221. }
  222. public function changeStatus(Request $request)
  223. {
  224. $memberAccountListId = $request->post('member_account_list_id');
  225. $status = $request->post('status');
  226. $password = $request->post('pay_pwd');
  227. if ($status == 'ACTIVED' && $password != '888666') {
  228. return json_fail('密码错误');
  229. }
  230. Db::beginTransaction();
  231. try {
  232. $withdraw = MemberAccountList::where('member_account_list_id', $memberAccountListId)->first();
  233. $withdraw->member_account_list_status = $status;
  234. $memberAccountListJson = [];
  235. if (!empty($withdraw->member_account_list_json)) {
  236. $memberAccountListJson = json_decode($withdraw->member_account_list_json, true);
  237. }
  238. $memberAccountListExtendJson = [];
  239. if (!empty($withdraw->member_account_list_extend_json)) {
  240. $memberAccountListExtendJson = json_decode($withdraw->member_account_list_extend_json, true);
  241. }
  242. if ($status == 'ACTIVED') {
  243. $memberAccountListJson['resulttime'] = date('Y-m-d H:i:s');
  244. $memberAccountListExtendJson['resulttime'] = date('Y-m-d H:i:s');
  245. // 扣账户余额
  246. $account = MemberAccount::where('join_account_member_id', $withdraw->join_member_account_list_member_id)
  247. ->where('member_account_classify', 'CASH')
  248. ->where('member_account_status', 'ACTIVED')
  249. ->first();
  250. if (empty($account)) {
  251. throw new BusinessException("账户异常");
  252. }
  253. if ($account->member_account_surplus < $withdraw->member_account_list_amount) {
  254. throw new BusinessException("账户余额不足");
  255. }
  256. $account->member_account_surplus = $account->member_account_surplus - $withdraw->member_account_list_amount;
  257. $account->member_account_expend = $account->member_account_expend + $withdraw->member_account_list_amount;
  258. $account->save();
  259. } else if ($status == 'CANCEL') {
  260. $memberAccountListExtendJson['resulttime'] = date('Y-m-d H:i:s');
  261. $memberAccountListExtendJson['reason'] = $request->post('reason', '');
  262. }
  263. $withdraw->member_account_list_extend_json = json_encode($memberAccountListExtendJson);
  264. $withdraw->member_account_list_outed = 'Y';
  265. $withdraw->save();
  266. _syslog('提现', '修改状态成功');
  267. Db::commit();
  268. if ($status == 'ACTIVED') {
  269. // 入收支记录
  270. $params['account_list_id'] = $memberAccountListId;
  271. $params['type'] = 'withdraw';
  272. Event::dispatch('statistics.inout.out', $params);
  273. }
  274. return json_success('success');
  275. } catch (BusinessException $e) {
  276. Db::rollBack();
  277. return json_fail($e->getMessage());
  278. } catch (\Exception $e) {
  279. Db::rollBack();
  280. return json_fail("修改状态失败");
  281. }
  282. }
  283. }