WithdrawalListController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. $query = $this->doSelect($where, $field, $order);
  29. return $this->doFormat($query, $format, $limit);
  30. }
  31. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  32. {
  33. $model = $this->model->with([
  34. 'member' => function ($query) {
  35. $query->select('member_id', 'member_mobile', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer');
  36. },
  37. 'memberInfo' => function ($query) {
  38. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  39. },
  40. 'memberCert' => function ($query) {
  41. $query->select('join_cert_member_id', 'member_cert_name');
  42. }
  43. ]);
  44. foreach ($where as $column => $value) {
  45. if (is_array($value)) {
  46. if ($value[0] === 'like' || $value[0] === 'not like') {
  47. $model = $model->where($column, $value[0], "%$value[1]%");
  48. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  49. $model = $model->where($column, $value[0], $value[1]);
  50. } elseif ($value[0] == 'in' && !empty($value[1])) {
  51. $valArr = $value[1];
  52. if (is_string($value[1])) {
  53. $valArr = explode(",", trim($value[1]));
  54. }
  55. $model = $model->whereIn($column, $valArr);
  56. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  57. $valArr = $value[1];
  58. if (is_string($value[1])) {
  59. $valArr = explode(",", trim($value[1]));
  60. }
  61. $model = $model->whereNotIn($column, $valArr);
  62. } elseif ($value[0] == 'null') {
  63. $model = $model->whereNull($column);
  64. } elseif ($value[0] == 'not null') {
  65. $model = $model->whereNotNull($column);
  66. } elseif ($value[0] !== '' || $value[1] !== '') {
  67. $model = $model->whereBetween($column, $value);
  68. }
  69. } else {
  70. $model = $model->where($column, $value);
  71. }
  72. }
  73. if ($field) {
  74. $model = $model->orderBy($field, $order);
  75. }
  76. return $model;
  77. }
  78. public function afterQuery($items)
  79. {
  80. foreach ($items as &$item) {
  81. $item['member_mobile'] = isset($item['member']) ? $item['member']['member_mobile'] : '';
  82. $item['member_info_nickname'] = isset($item['memberInfo']) && !empty($item['memberInfo']['member_info_nickname']) ? $item['memberInfo']['member_info_nickname'] : '';
  83. $item['member_cert_name'] = isset($item['memberCert']) && !empty($item['memberCert']['member_cert_name']) ? $item['memberCert']['member_cert_name'] : '';
  84. $item['member_name'] = MemberService::getMemberName($item['member_mobile'], $item['member_cert_name'], $item['member_info_nickname']);
  85. $item['member_info_headimg'] = isset($item['memberInfo']) && !empty($item['memberInfo']['member_info_headimg']) ? $item['memberInfo']['member_info_headimg'] : "";
  86. $item['member_is_owner'] = isset($item['member']) ? $item['member']['member_is_owner'] : '';
  87. $item['member_is_vip'] = isset($item['member']) ? $item['member']['member_is_vip'] : '';
  88. $item['member_is_partner'] = isset($item['member']) ? $item['member']['member_is_partner'] : '';
  89. $item['member_is_referrer'] = isset($item['member']) ? $item['member']['member_is_referrer'] : '';
  90. unset($item['member'], $item['memberInfo'], $item['memberCert']);
  91. }
  92. return $items;
  93. }
  94. /**
  95. * @Desc 记录详情
  96. * @Author Gorden
  97. * @Date 2024/9/28 17:15
  98. *
  99. * @param Request $request
  100. * @return Response
  101. */
  102. public function info(Request $request): Response
  103. {
  104. $id = $request->get('member_account_list_id');
  105. if (!$id) {
  106. return json_fail("参数异常");
  107. }
  108. $info = MemberAccountList::where('member_account_list_id', $id)->first();
  109. $infoMember = Member::with([
  110. 'role' => function ($query) {
  111. $query->select('member_role_id', 'member_role_name');
  112. }
  113. ])->where('member_id', $info->join_member_account_list_member_id)
  114. ->first();
  115. $info->member_role_name = !empty($infoMember->role) ? $infoMember->role->member_role_name : '';
  116. // 获取银行卡信息
  117. if (!empty($info->member_account_list_json)) {
  118. $accountListJson = json_decode($info->member_account_list_json, true);
  119. $info->banks = [
  120. [
  121. 'bank_name' => $accountListJson['bank_name'] ?? '',
  122. 'username' => $accountListJson['bank_acount'] ?? '',
  123. 'cardno' => $accountListJson['bank_cardno'] ?? ''
  124. ]
  125. ];
  126. }
  127. // $info->banks = ClientConfig::where('join_client_config_member_id',$info->join_member_account_list_member_id)->where('client_config_key','client-bank')->get()->toArray();
  128. return json_success('success', $info);
  129. }
  130. public function changeStatus(Request $request)
  131. {
  132. $memberAccountListId = $request->post('member_account_list_id');
  133. $status = $request->post('status');
  134. $password = $request->post('pay_pwd');
  135. if ($status == 'ACTIVED' && $password != '888666') {
  136. return json_fail('密码错误');
  137. }
  138. Db::beginTransaction();
  139. try {
  140. $withdraw = MemberAccountList::where('member_account_list_id', $memberAccountListId)->first();
  141. $withdraw->member_account_list_status = $status;
  142. $memberAccountListJson = [];
  143. if (!empty($withdraw->member_account_list_json)) {
  144. $memberAccountListJson = json_decode($withdraw->member_account_list_json, true);
  145. }
  146. if ($status == 'ACTIVED') {
  147. $memberAccountListJson['resulttime'] = date('Y-m-d H:i:s');
  148. // 扣账户余额
  149. $account = MemberAccount::where('join_account_member_id', $withdraw->join_member_account_list_member_id)
  150. ->where('member_account_classify', 'CASH')
  151. ->where('member_account_status', 'ACTIVED')
  152. ->first();
  153. if (empty($account)) {
  154. throw new BusinessException("账户异常");
  155. }
  156. if ($account->member_account_surplus < $withdraw->member_account_list_amount) {
  157. throw new BusinessException("账户余额不足");
  158. }
  159. $account->member_account_surplus = $account->member_account_surplus - $withdraw->member_account_list_amount;
  160. $account->member_account_expend = $account->member_account_expend + $withdraw->member_account_list_amount;
  161. $account->save();
  162. }
  163. $withdraw->save();
  164. _syslog('提现', '修改状态成功');
  165. Db::commit();
  166. if ($status == 'ACTIVED') {
  167. // 入收支记录
  168. $params['account_list_id'] = $memberAccountListId;
  169. $params['type'] = 'withdraw';
  170. Event::dispatch('statistics.inout.out',$params);
  171. }
  172. return json_success('success');
  173. } catch (BusinessException $e) {
  174. Db::rollBack();
  175. return json_fail($e->getMessage());
  176. } catch (\Exception $e) {
  177. Db::rollBack();
  178. return json_fail("修改状态失败");
  179. }
  180. }
  181. }