AccountController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. return json_success('', $account);
  41. }
  42. public function incomeExpend(Request $request)
  43. {
  44. $params = $request->post();
  45. $account = MemberAccount::where('member_account_id', $params['member_account_id'])->first();
  46. if (!$account) {
  47. return json_fail('账户不存在');
  48. }
  49. try {
  50. if (!empty($params['type']) && $params['type'] == 'income') {
  51. $account->member_account_income = $account->member_account_income + $params['income'];
  52. $account->member_account_surplus = $account->member_account_surplus + $params['income'];
  53. $account->save();
  54. } elseif (!empty($params['type']) && $params['type'] == 'expend') {
  55. $account->member_account_expend = $account->member_account_expend + $params['expend'];
  56. $account->member_account_surplus = $account->member_account_surplus - $params['expend'];
  57. $account->save();
  58. }
  59. _syslog("账户出入账", $params['type'] == 'income' ? '入账' : '出账');
  60. return json_success("操作成功");
  61. } catch (\Exception $e) {
  62. return json_fail('操作失败');
  63. }
  64. }
  65. }