AccountController.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. }
  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. }