AccountController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\admin\service\goods\GoodsService;
  4. use app\admin\service\order\OrderService;
  5. use app\controller\Curd;
  6. use app\model\Card;
  7. use app\model\Goods;
  8. use app\model\Member;
  9. use app\model\MemberAccount;
  10. use app\model\MemberAccountList;
  11. use app\model\MemberBenefit;
  12. use app\model\Order;
  13. use app\model\OrderSheet;
  14. use app\model\PayDetail;
  15. use app\model\SysCategory;
  16. use support\Db;
  17. use support\exception\BusinessException;
  18. use support\Log;
  19. use support\Redis;
  20. use support\Request;
  21. use Tinywan\Jwt\JwtToken;
  22. class AccountController extends Curd
  23. {
  24. public function __construct()
  25. {
  26. $this->model = new MemberAccount();
  27. }
  28. public function my($id)
  29. {
  30. $account = MemberAccount::where('join_account_member_id', $id)->get()->toArray();
  31. if (!$account) {
  32. // 账户表
  33. $accountData = [
  34. 'join_account_member_id' => $id,
  35. 'member_account_classify' => 'POINTS',
  36. 'member_account_status' => 'ACTIVED',
  37. 'member_account_category' => 'NORMAL',
  38. 'member_account_nbr' => $id . '-POINTS',
  39. 'member_account_name' => '积分账户',
  40. 'member_account_addtimes' => time()
  41. ];
  42. // 积分账户
  43. MemberAccount::insert($accountData);
  44. $accountData['member_account_classify'] = 'CASH';
  45. $accountData['member_account_nbr'] = $id . '-CASH';
  46. $accountData['member_account_name'] = '余额账户';
  47. // 现金/余额账户
  48. MemberAccount::insert($accountData);
  49. $account = MemberAccount::where('join_account_member_id', $id)->get()->toArray();
  50. }
  51. if ($account[0]['member_account_classify'] == 'POINTS') {
  52. $account = array_reverse($account);
  53. }
  54. foreach ($account as &$item) {
  55. $item['member_account_duedate'] = !empty($item['member_account_duedate']) ? date('Y-m-d H:i:s', $item['member_account_duedate']) : '长期有效';
  56. $item['member_account_surplus'] = round($item['member_account_surplus'] + $item['member_account_added'], 2);
  57. }
  58. return json_success('', $account);
  59. }
  60. public function incomeExpend(Request $request)
  61. {
  62. $params = $request->post();
  63. $account = MemberAccount::where('member_account_id', $params['member_account_id'])->first();
  64. if (!$account) {
  65. return json_fail('账户不存在');
  66. }
  67. try {
  68. if (!empty($params['type']) && $params['type'] == 'income') {
  69. $account->member_account_income = $account->member_account_income + $params['income'];
  70. $account->member_account_surplus = $account->member_account_surplus + $params['income'];
  71. $account->member_account_update_user_id = JwtToken::getCurrentId();
  72. $account->member_account_updatetimes = time();
  73. $account->save();
  74. } elseif (!empty($params['type']) && $params['type'] == 'expend') {
  75. $account->member_account_expend = $account->member_account_expend + $params['expend'];
  76. $account->member_account_surplus = $account->member_account_surplus - $params['expend'];
  77. $account->member_account_update_user_id = JwtToken::getCurrentId();
  78. $account->member_account_updatetimes = time();
  79. $account->save();
  80. }
  81. _syslog("账户出入账", $params['type'] == 'income' ? '入账' : '出账');
  82. return json_success("操作成功");
  83. } catch (\Exception $e) {
  84. return json_fail('操作失败');
  85. }
  86. }
  87. /**
  88. * 用户的账户
  89. */
  90. public function selectMemberAccount(Request $request)
  91. {
  92. $memberId = $request->get('member_id', '');
  93. if (!$memberId) {
  94. return json_fail('参数异常');
  95. }
  96. $account = MemberAccount::where('join_account_member_id', $memberId)->get()->toArray();
  97. $data = [];
  98. foreach ($account as $item) {
  99. $item['member_account_duedate'] = !empty($item['member_account_duedate']) ? date('Y-m-d H:i:s', $item['member_account_duedate']) : '长期有效';
  100. if ($item['member_account_classify'] == 'POINTS') {
  101. $data['points'] = $item;
  102. } elseif ($item['member_account_classify'] == 'CASH') {
  103. $data['cash'] = $item;
  104. } elseif ($item['member_account_classify'] == 'CARD') {
  105. if (intval($item['member_account_surplus']) == 0 && intval($item['member_account_added']) == 0) {
  106. continue;
  107. }
  108. $data['card'][] = $item;
  109. } elseif ($item['member_account_classify'] == 'WELFARE') {
  110. $data['welfare'] = $item;
  111. } elseif ($item['member_account_classify'] == 'VIP') {
  112. $data['vip'] = $item;
  113. }
  114. }
  115. return json_success('', $data);
  116. }
  117. /**
  118. * @Desc 修改账户状态
  119. * @Author Gorden
  120. * @Date 2024/9/12 16:28
  121. *
  122. * @param Request $request
  123. * @return \support\Response
  124. */
  125. public function updateStatus(Request $request)
  126. {
  127. $accountId = $request->post('account_id');
  128. $status = $request->post('status');
  129. if (!$accountId || !$status || !in_array($status, ['DISABLED', 'ACTIVED', 'EXPIRED'])) {
  130. return json_fail('参数异常');
  131. }
  132. try {
  133. $account = MemberAccount::where('member_account_id', $accountId)->first();
  134. $account->member_account_status = $status;
  135. $account->save();
  136. // 储值卡账户,把卡冻结掉
  137. if ($account->member_account_classify == 'CARD') {
  138. $card = Card::where('card_id', $account->member_account_nbr)->first();
  139. $cardExtendJson = [];
  140. if (!empty($card->card_extend_json)) {
  141. $cardExtendJson = json_decode($card->card_extend_json, true);
  142. }
  143. if ($status == 'DISABLED') {
  144. $card->card_status = 'PAUSED';
  145. $cardExtendJson['paused_datetime'] = date('Y-m-d H:i:s');
  146. $cardExtendJson['paused_user_id'] = JwtToken::getCurrentId();
  147. $card->card_extend_json = json_encode($cardExtendJson);
  148. } elseif ($status == 'ACTIVED') {
  149. $card->card_status = 'USED';
  150. }
  151. $card->save();
  152. }
  153. _syslog("修改账户状态", '修改账户状态成功');
  154. return json_success('账户状态修改成功');
  155. } catch (\Exception $e) {
  156. _syslog("修改账户状态", '账户状态修改失败');
  157. return json_fail('账户状态修改失败');
  158. }
  159. }
  160. /**
  161. * @Desc 会员绑定储值卡
  162. * @Author Gorden
  163. * @Date 2024/11/19 11:12
  164. *
  165. * @param Request $request
  166. * @return \support\Response
  167. */
  168. public function bindCard(Request $request)
  169. {
  170. $cardId = $request->post('card_id');
  171. $cardKey = $request->post('card_cdkey');
  172. $memberId = $request->post('member_id');
  173. if (!Member::where('member_id', $memberId)->where('member_status', 'ACTIVED')->exists()) {
  174. return json_fail('会员不存在');
  175. }
  176. // 防刷
  177. $redisKey = $memberId . '_' . $cardId;
  178. $num = 0;
  179. if (Redis::exists($redisKey)) {
  180. $num = Redis::get($redisKey);
  181. }
  182. if ($num >= 3) {
  183. return json_fail("请30分钟后重试");
  184. }
  185. Redis::set($redisKey, $num + 1, 'EX', 1800);
  186. $card = Card::with([
  187. 'main' => function ($query) {
  188. $query->select('card_main_id', 'card_main_name', 'card_main_amount', 'card_main_added');
  189. }
  190. ])->where('card_id', $cardId)
  191. ->first();
  192. if (!$card || empty($card->main)) {
  193. return json_fail("卡号不存在");
  194. }
  195. if ($card->card_cdkey != $cardKey) {
  196. return json_fail("秘钥错误,请重新输入");
  197. }
  198. if ($card->is_issue == 'N') {
  199. return json_fail("卡号未发行");
  200. }
  201. if (!in_array($card->card_status, ['INIT', 'WAITING', 'PENDING'])) {
  202. return json_fail("卡号不是可绑定状态");
  203. }
  204. // 清除防刷
  205. Redis::del($redisKey);
  206. Db::beginTransaction();
  207. try {
  208. $duedate = 0;
  209. if ($card->main->card_main_valid_mode == 'DAYS') {
  210. $duedate = date('Y-m-d H:i:s', strtotime("+ " . $card->main->card_main_valid_days . "days") - 1);
  211. } elseif ($card->main->card_main_valid_mode == 'DATE') {
  212. $duedate = $card->main->card_main_valid_end;
  213. }
  214. // 创建储值卡账户
  215. $accountId = MemberAccount::insertGetId([
  216. 'join_account_member_id' => $memberId,
  217. 'member_account_classify' => 'CARD',
  218. 'member_account_status' => 'ACTIVED',
  219. 'member_account_category' => 'NORMAL',
  220. 'member_account_nbr' => $card->card_id,
  221. 'member_account_name' => $card->main->card_main_name,
  222. 'member_account_income' => $card->main->card_main_amount,
  223. 'member_account_surplus' => $card->main->card_main_amount,
  224. 'member_account_added' => $card->main->card_main_added,
  225. 'member_account_duedate' => $duedate,
  226. 'member_account_addtimes' => time(),
  227. 'member_account_update_user_id' => JwtToken::getCurrentId(),
  228. 'member_account_updatetimes' => time()
  229. ]);
  230. // 会员绑定卡
  231. $card->join_card_member_id = $memberId;
  232. $card->join_card_member_account_id = $accountId;
  233. // $card->card_assign_datetime = date('Y-m-d H:i:s');
  234. $card->card_deadline_datetime = $duedate;
  235. $card->card_status = 'USED';
  236. // 激活时间
  237. $cardExtendJson = [];
  238. if (!empty($card->card_extend_json)) {
  239. $cardExtendJson = json_decode($card->card_extend_json, true);
  240. }
  241. $cardExtendJson['used_datetime'] = date('Y-m-d H:i:s');
  242. $cardExtendJson['used_user_id'] = JwtToken::getCurrentId();
  243. $card->card_extend_json = json_encode($cardExtendJson);
  244. $card->save();
  245. // member_account_list 加卡充值记录
  246. MemberAccountList::insert([
  247. 'join_list_member_account_nbr' => $cardId,
  248. 'join_member_account_list_member_id' => $memberId,
  249. 'member_account_list_status' => 'ACTIVED',
  250. 'member_account_list_attr' => 'IN',
  251. 'member_account_list_classify' => 'CARD',
  252. 'member_account_list_category' => '绑卡',
  253. 'member_account_list_datetime' => date('Y-m-d H:i:s'),
  254. 'member_account_list_amount' => $card->main->card_main_amount,
  255. 'member_account_list_addtimes' => time()
  256. ]);
  257. Db::commit();
  258. return json_success('success');
  259. } catch (\Exception $e) {
  260. Db::rollBack();
  261. Log::error("储值卡绑定失败", ['msg' => $e->getMessage()]);
  262. return json_fail("绑定失败");
  263. }
  264. }
  265. /**
  266. * @Desc 导出储值卡账户消费记录
  267. * @Author Gorden
  268. * @Date 2024/12/3 10:07
  269. *
  270. * @param Request $request
  271. * @return \support\Response
  272. */
  273. public function exportCardConsumeList(Request $request)
  274. {
  275. $memberId = $request->get('member_id', '');
  276. $accountNbr = $request->get('account_nbr', '');
  277. $account = MemberAccount::where('join_account_member_id', $memberId)
  278. ->where('member_account_nbr', $accountNbr)
  279. ->where('member_account_classify', 'CARD')
  280. ->first();
  281. if (!$account) {
  282. return json_fail('暂无数据');
  283. }
  284. $balance = $account->member_account_surplus + $account->member_account_added;
  285. $info = PayDetail::where('pay_prepayid', $accountNbr)
  286. ->where('pay_status', 'SUCCESS')
  287. ->orderByDesc('pay_addtimes')
  288. ->get()
  289. ->toArray();
  290. $data = [];
  291. foreach ($info as $key => $item) {
  292. unset($item['pay_json_request']);
  293. $categoryIds = [];
  294. if (!empty($item['join_pay_order_id'])) {
  295. $orderIds = Order::where('order_groupby', $item['join_pay_order_id'])->pluck('order_id')->toArray();
  296. $goodsIds = OrderSheet::whereIn('join_sheet_order_id', $orderIds)->pluck('join_sheet_goods_id')->toArray();
  297. $categoryIds = Goods::whereIn('goods_id', $goodsIds)->pluck('join_goods_category_id')->toArray();
  298. }
  299. $categoryName = [];
  300. foreach ($categoryIds as $id) {
  301. $category = SysCategory::where('category_id', $id)->first();
  302. if (!empty($category->category_super_path)) {
  303. $path = explode('#', $category->category_super_path);
  304. $ids = array_filter($path);
  305. $categoryName[] = SysCategory::where('category_id', current($ids))->value('category_name');
  306. } else {
  307. $categoryName[] = $category->category_name;
  308. }
  309. }
  310. $item['goods_category'] = implode(',', array_unique($categoryName));
  311. if (in_array($item['pay_category'], ['RECHARGE', 'REFUND'])) {
  312. $item['balance_type'] = 'add';
  313. } else {
  314. $item['balance_type'] = 'cut';
  315. }
  316. $balance = $balance + $item['pay_amount'];
  317. $payCategory = '消费';
  318. if ($item['pay_category'] == 'REFUND') {
  319. $payCategory = '退款';
  320. }
  321. $data[] = [
  322. 'goods_category' => $item['goods_category'],
  323. 'join_pay_order_id' => $item['join_pay_order_id'],
  324. 'balance' => number_format($balance, 2, '.', ''),
  325. 'balance_type' => $item['balance_type'],
  326. 'pay_amount' => ($item['balance_type'] == 'add' ? '+' : '-') . $item['pay_amount'],
  327. 'pay_category' => $payCategory,
  328. 'pay_addtimes' => $item['pay_addtimes']
  329. ];
  330. }
  331. // 绑定记录
  332. $memberAccountLists = MemberAccountList::where('join_list_member_account_nbr', $accountNbr)
  333. ->where('member_account_list_attr', 'IN')
  334. ->select('member_account_list_amount', 'member_account_list_datetime', 'member_account_list_attr', 'join_list_member_account_nbr', 'member_account_list_addtimes')
  335. ->get()
  336. ->toArray();
  337. foreach ($memberAccountLists as $item) {
  338. $data[] = [
  339. 'goods_category' => '--',
  340. 'join_pay_order_id' => '--',
  341. 'balance' => $item['member_account_list_amount'],
  342. 'pay_amount' => '+' . $item['member_account_list_amount'],
  343. 'pay_category' => '绑定',
  344. 'pay_addtimes' => $item['member_account_list_addtimes'],
  345. ];
  346. }
  347. return json_success('success', $data);
  348. }
  349. }