AccountController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\controller\Curd;
  4. use app\model\MemberAccount;
  5. use app\model\MemberBenefit;
  6. use support\exception\BusinessException;
  7. use support\Request;
  8. use Tinywan\Jwt\JwtToken;
  9. class AccountController extends Curd
  10. {
  11. public function __construct()
  12. {
  13. $this->model = new MemberAccount();
  14. }
  15. public function my($id)
  16. {
  17. $account = MemberAccount::where('join_account_member_id', $id)->get()->toArray();
  18. if (!$account) {
  19. // 账户表
  20. $accountData = [
  21. 'join_account_member_id' => $id,
  22. 'member_account_classify' => 'POINTS',
  23. 'member_account_status' => 'ACTIVED',
  24. 'member_account_category' => 'NORMAL',
  25. 'member_account_nbr' => $id . '-POINTS',
  26. 'member_account_name' => '积分账户',
  27. 'member_account_addtimes' => time()
  28. ];
  29. // 积分账户
  30. MemberAccount::insert($accountData);
  31. $accountData['member_account_classify'] = 'CASH';
  32. $accountData['member_account_nbr'] = $id . '-CASH';
  33. $accountData['member_account_name'] = '余额账户';
  34. // 现金/余额账户
  35. MemberAccount::insert($accountData);
  36. $account = MemberAccount::where('join_account_member_id', $id)->get()->toArray();
  37. }
  38. if ($account[0]['member_account_classify'] == 'POINTS') {
  39. $account = array_reverse($account);
  40. }
  41. foreach ($account as &$item) {
  42. $item['member_account_duedate'] = !empty($item['member_account_duedate']) ? date('Y-m-d H:i:s', $item['member_account_duedate']) : '长期有效';
  43. $item['member_account_surplus'] = round($item['member_account_surplus'] + $item['member_account_added'], 2);
  44. }
  45. return json_success('', $account);
  46. }
  47. public function incomeExpend(Request $request)
  48. {
  49. $params = $request->post();
  50. $account = MemberAccount::where('member_account_id', $params['member_account_id'])->first();
  51. if (!$account) {
  52. return json_fail('账户不存在');
  53. }
  54. try {
  55. if (!empty($params['type']) && $params['type'] == 'income') {
  56. $account->member_account_income = $account->member_account_income + $params['income'];
  57. $account->member_account_surplus = $account->member_account_surplus + $params['income'];
  58. $account->member_account_update_user_id = JwtToken::getCurrentId();
  59. $account->member_account_updatetimes = time();
  60. $account->save();
  61. } elseif (!empty($params['type']) && $params['type'] == 'expend') {
  62. $account->member_account_expend = $account->member_account_expend + $params['expend'];
  63. $account->member_account_surplus = $account->member_account_surplus - $params['expend'];
  64. $account->member_account_update_user_id = JwtToken::getCurrentId();
  65. $account->member_account_updatetimes = time();
  66. $account->save();
  67. }
  68. _syslog("账户出入账", $params['type'] == 'income' ? '入账' : '出账');
  69. return json_success("操作成功");
  70. } catch (\Exception $e) {
  71. return json_fail('操作失败');
  72. }
  73. }
  74. /**
  75. * 用户的账户
  76. */
  77. public function selectMemberAccount(Request $request)
  78. {
  79. $memberId = $request->get('member_id', '');
  80. if (!$memberId) {
  81. return json_fail('参数异常');
  82. }
  83. $account = MemberAccount::where('join_account_member_id', $memberId)->get()->toArray();
  84. $data = [];
  85. foreach ($account as $item) {
  86. $item['member_account_duedate'] = !empty($item['member_account_duedate']) ? date('Y-m-d H:i:s', $item['member_account_duedate']) : '长期有效';
  87. if ($item['member_account_classify'] == 'POINTS') {
  88. $data['points'] = $item;
  89. } elseif ($item['member_account_classify'] == 'CASH') {
  90. $data['cash'] = $item;
  91. } elseif ($item['member_account_classify'] == 'CARD') {
  92. $data['card'][] = $item;
  93. } elseif ($item['member_account_classify'] == 'WELFARE') {
  94. $data['welfare'] = $item;
  95. } elseif ($item['member_account_classify'] == 'VIP') {
  96. $data['vip'] = $item;
  97. }
  98. }
  99. return json_success('', $data);
  100. }
  101. /**
  102. * @Desc 修改账户状态
  103. * @Author Gorden
  104. * @Date 2024/9/12 16:28
  105. *
  106. * @param Request $request
  107. * @return \support\Response
  108. */
  109. public function updateStatus(Request $request)
  110. {
  111. $accountId = $request->post('account_id');
  112. $status = $request->post('status');
  113. if (!$accountId || !$status || !in_array($status,['DISABLED','ACTIVED','EXPIRED'])){
  114. return json_fail('参数异常');
  115. }
  116. try {
  117. $account = MemberAccount::where('member_account_id',$accountId)->first();
  118. $account->member_account_status = $status;
  119. $account->save();
  120. _syslog("修改账户状态",'修改账户状态成功');
  121. return json_success('账户状态修改成功');
  122. }catch (\Exception $e){
  123. _syslog("修改账户状态",'账户状态修改失败');
  124. return json_fail('账户状态修改失败');
  125. }
  126. }
  127. }