AccountController.php 5.0 KB

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