AccountController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. $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. }