AccountController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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)->where('member_account_status','ACTIVED')->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. }
  43. return json_success('', $account);
  44. }
  45. public function incomeExpend(Request $request)
  46. {
  47. $params = $request->post();
  48. $account = MemberAccount::where('member_account_id', $params['member_account_id'])->first();
  49. if (!$account) {
  50. return json_fail('账户不存在');
  51. }
  52. try {
  53. if (!empty($params['type']) && $params['type'] == 'income') {
  54. $account->member_account_income = $account->member_account_income + $params['income'];
  55. $account->member_account_surplus = $account->member_account_surplus + $params['income'];
  56. $account->save();
  57. } elseif (!empty($params['type']) && $params['type'] == 'expend') {
  58. $account->member_account_expend = $account->member_account_expend + $params['expend'];
  59. $account->member_account_surplus = $account->member_account_surplus - $params['expend'];
  60. $account->save();
  61. }
  62. _syslog("账户出入账", $params['type'] == 'income' ? '入账' : '出账');
  63. return json_success("操作成功");
  64. } catch (\Exception $e) {
  65. return json_fail('操作失败');
  66. }
  67. }
  68. /**
  69. * 用户的账户
  70. */
  71. public function selectMemberAccount(Request $request)
  72. {
  73. $memberId = $request->get('member_id','');
  74. if (!$memberId){
  75. return json_fail('参数异常');
  76. }
  77. $account = MemberAccount::where('join_account_member_id', $memberId)->get()->toArray();
  78. $data = [];
  79. foreach ($account as $item){
  80. $item['member_account_duedate'] = !empty($item['member_account_duedate']) ? date('Y-m-d H:i:s',$item['member_account_duedate']) : '长期有效';
  81. if ($item['member_account_classify'] == 'POINTS'){
  82. $data['points'] = $item;
  83. }elseif ($item['member_account_classify'] == 'CASH'){
  84. $data['cash'] = $item;
  85. }elseif ($item['member_account_classify'] == 'CARD'){
  86. $data['card'][] = $item;
  87. }elseif ($item['member_account_classify'] == 'WELFARE'){
  88. $data['welfare'] = $item;
  89. }
  90. }
  91. return json_success('', $data);
  92. }
  93. }