CardController.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. <?php
  2. namespace app\admin\controller\coupon;
  3. use app\admin\service\coupon\CardService;
  4. use app\admin\service\member\MemberService;
  5. use app\controller\Curd;
  6. use app\model\Card;
  7. use app\model\CardIssue;
  8. use app\model\CardMain;
  9. use app\model\Member;
  10. use app\model\MemberRole;
  11. use app\model\SysCategory;
  12. use app\model\SysUser;
  13. use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
  14. use Illuminate\Database\Query\Builder as QueryBuilder;
  15. use support\Db;
  16. use support\exception\BusinessException;
  17. use support\Log;
  18. use support\Model;
  19. use support\Request;
  20. use support\Response;
  21. use Tinywan\Jwt\JwtToken;
  22. class CardController extends Curd
  23. {
  24. public function __construct()
  25. {
  26. $this->model = new Card();
  27. }
  28. /**
  29. * 查询
  30. * @param Request $request
  31. * @return Response
  32. * @throws BusinessException
  33. */
  34. public function select(Request $request): Response
  35. {
  36. $cardMainName = $request->get('card_main_name');
  37. $cardMainAmount = $request->get('card_main_amount');
  38. $cardIssueName = $request->get('card_issue_name');
  39. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  40. $order = $request->get('order', 'desc');
  41. $field = $field ?? 'card_addtimes';
  42. if (!empty($where['card_addtimes'])) {
  43. $where['card_addtimes'][0] = strtotime($where['card_addtimes'][0]);
  44. $where['card_addtimes'][1] = strtotime($where['card_addtimes'][1]);
  45. }
  46. $cardMainIds = [];
  47. if (!empty($cardMainName)) {
  48. $cardMainIds = CardMain::where('card_main_name', 'like', '%' . $cardMainName . '%')->pluck('card_main_id')->toArray();
  49. }
  50. if (!empty($cardMainAmount)) {
  51. $mainIds = CardMain::where('card_main_amount', $cardMainAmount)->pluck('card_main_id')->toArray();
  52. $cardMainIds = array_intersect($cardMainIds, $mainIds);
  53. }
  54. if (!empty($cardMainName) || !empty($cardMainAmount)) {
  55. $where['join_card_main_id'] = ['in', $cardMainIds];
  56. }
  57. if (!empty($cardIssueName)) {
  58. $cardIssueIds = CardIssue::where('card_issue_name', 'like', '%' . $cardIssueName . '%')->pluck('card_issue_id')->toArray();
  59. $where['join_card_issue_id'] = ['in', $cardIssueIds];
  60. }
  61. $query = $this->doSelect($where, $field, $order);
  62. return $this->doFormat($query, $format, $limit);
  63. }
  64. /**
  65. * 指定查询where条件,并没有真正的查询数据库操作
  66. * @param array $where
  67. * @param string|null $field
  68. * @param string $order
  69. * @return EloquentBuilder|QueryBuilder|Model
  70. */
  71. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  72. {
  73. $model = $this->model->with([
  74. 'member' => function ($query) {
  75. $query->select('member_id', 'member_mobile');
  76. },
  77. 'cert' => function ($query) {
  78. $query->select('join_cert_member_id', 'member_cert_name');
  79. },
  80. 'dept',
  81. 'user',
  82. 'main' => function ($query) {
  83. $query->select('card_main_id', 'card_main_name', 'card_main_amount', 'card_main_valid_mode');
  84. },
  85. 'issue' => function ($query) {
  86. $query->select('card_issue_id', 'card_issue_name');
  87. }
  88. ]);
  89. foreach ($where as $column => $value) {
  90. if (is_array($value)) {
  91. if ($value[0] === 'like' || $value[0] === 'not like') {
  92. $model = $model->where($column, $value[0], "%$value[1]%");
  93. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  94. $model = $model->where($column, $value[0], $value[1]);
  95. } elseif ($value[0] == 'in') {
  96. $valArr = $value[1];
  97. if (is_string($value[1])) {
  98. $valArr = explode(",", trim($value[1]));
  99. }
  100. $model = $model->whereIn($column, $valArr);
  101. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  102. $valArr = $value[1];
  103. if (is_string($value[1])) {
  104. $valArr = explode(",", trim($value[1]));
  105. }
  106. $model = $model->whereNotIn($column, $valArr);
  107. } elseif ($value[0] == 'null') {
  108. $model = $model->whereNull($column);
  109. } elseif ($value[0] == 'not null') {
  110. $model = $model->whereNotNull($column);
  111. } elseif ($value[0] !== '' || $value[1] !== '') {
  112. $model = $model->whereBetween($column, $value);
  113. }
  114. } else {
  115. $model = $model->where($column, $value);
  116. }
  117. }
  118. if ($field) {
  119. $model = $model->orderBy($field, $order)->orderByDesc('card_id');
  120. }
  121. return $model;
  122. }
  123. public function afterQuery($items)
  124. {
  125. foreach ($items as &$item) {
  126. $mobile = $item['member']['member_mobile'] ?? '';
  127. $certName = $item['cert']['member_cert_name'] ?? '';
  128. $item['member_name'] = MemberService::getMemberCertName($mobile, $certName, '');
  129. if (!empty($item['main']['card_main_valid_mode'])) {
  130. if ($item['main']['card_main_valid_mode'] == 'LONG') {
  131. $item['valid_datetime'] = '长期有效';
  132. } else if (!empty($item['card_deadline_datetime'])) {
  133. $item['valid_datetime'] = date('Y/m/d', strtotime($item['card_assign_datetime'])) . '-' . date('Y/m/d', strtotime($item['card_deadline_datetime']));
  134. }
  135. }
  136. $cardExtendJson = [];
  137. if (!empty($item['card_extend_json'])) {
  138. $cardExtendJson = json_decode($item['card_extend_json'], true);
  139. }
  140. if (!empty($cardExtendJson['issue_datetime'])) {
  141. $item['issue_datetime'] = $cardExtendJson['issue_datetime'];
  142. }
  143. }
  144. return $items;
  145. }
  146. /**
  147. * @Desc 储值卡统计
  148. * @Author Gorden
  149. * @Date 2024/11/22 13:06
  150. *
  151. * @param Request $request
  152. * @return Response
  153. */
  154. public function statistics(Request $request)
  155. {
  156. $cardStatus = $request->get('card_status');
  157. $isIssue = $request->get('is_issue');
  158. $issueName = $request->get('card_issue_name');
  159. $mainName = $request->get('card_main_name');
  160. $addtimes = $request->get('card_addtimes');
  161. $amount = $request->get('card_main_amount');
  162. $deptId = $request->get('join_card_dept_id');
  163. $cardId = $request->get('card_id');
  164. $memberId = $request->get('join_card_member_id');
  165. $cardMainIds = [];
  166. if (!empty($mainName)) {
  167. $cardMainIds = CardMain::where('card_main_name', 'like', '%' . $mainName . '%')->pluck('card_main_id')->toArray();
  168. }
  169. if (!empty($amount)) {
  170. $mainIds = CardMain::where('card_main_amount', $amount)->pluck('card_main_id')->toArray();
  171. $cardMainIds = array_intersect($cardMainIds, $mainIds);
  172. }
  173. $cardIssueIds = [];
  174. if (!empty($issueName)) {
  175. $cardIssueIds = CardIssue::where('card_issue_name', 'like', '%' . $issueName . '%')->pluck('card_issue_id')->toArray();
  176. }
  177. $cardModel = Card::with([
  178. 'member' => function ($query) {
  179. $query->select('member_id', 'member_mobile');
  180. },
  181. 'cert' => function ($query) {
  182. $query->select('join_cert_member_id', 'member_cert_name');
  183. },
  184. 'dept',
  185. 'user',
  186. 'main' => function ($query) {
  187. $query->select('card_main_id', 'card_main_name', 'card_main_amount', 'card_main_valid_mode');
  188. },
  189. 'issue' => function ($query) {
  190. $query->select('card_issue_id', 'card_issue_name');
  191. }
  192. ])->when(!empty($cardId), function ($query) use ($cardId) {
  193. $query->where('card_id', $cardId);
  194. })->when(!empty($deptId), function ($query) use ($deptId) {
  195. $query->where('join_card_dept_id', $deptId);
  196. })->when(!empty($memberId), function ($query) use ($memberId) {
  197. $query->where('join_card_member_id', $memberId);
  198. })->when(!empty($isIssue), function ($query) use ($isIssue) {
  199. $query->where('is_issue', $isIssue);
  200. })->when(!empty($cardStatus), function ($query) use ($cardStatus) {
  201. $query->where('card_status', $cardStatus);
  202. })->when(!empty($addtimes), function ($query) use ($addtimes) {
  203. $addtimes[0] = strtotime($addtimes[0]);
  204. $addtimes[1] = strtotime($addtimes[1]);
  205. $query->whereBetween('card_addtimes', $addtimes);
  206. })->when(!empty($mainName) && !empty($amount), function ($query) use ($cardMainIds) {
  207. $query->whereIn('join_card_main_id', $cardMainIds);
  208. })->when(!empty($issueName), function ($query) use ($cardIssueIds) {
  209. $query->whereIn('join_card_issue_id', $cardIssueIds);
  210. });
  211. $totalList = CardMain::when(!empty($mainName), function ($query) use ($mainName) {
  212. $query->where('card_main_name', 'like', '%' . $mainName . '%');
  213. })->when(!empty($amount), function ($query) use ($amount) {
  214. $query->where('card_main_amount', $amount);
  215. })->select('card_main_id', 'card_main_amount')
  216. ->get()
  217. ->toArray();
  218. $statistics = [
  219. 'total' => ['total' => 0, 'amount' => 0],
  220. 'is_issue' => ['total' => 0, 'amount' => 0],
  221. 'init' => ['total' => 0, 'amount' => 0],
  222. 'waiting' => ['total' => 0, 'amount' => 0],
  223. 'pending' => ['total' => 0, 'amount' => 0],
  224. 'used' => ['total' => 0, 'amount' => 0],
  225. 'done' => ['total' => 0, 'amount' => 0],
  226. 'expired' => ['total' => 0, 'amount' => 0],
  227. 'paused' => ['total' => 0, 'amount' => 0]
  228. ];
  229. foreach ($totalList as $total) {
  230. // 发行统计
  231. $issueModel = clone $cardModel;
  232. $isIssueTotal = $issueModel->where('is_issue', 'Y')->where('join_card_main_id', $total['card_main_id'])->count();
  233. $statistics['is_issue']['total'] += $isIssueTotal;
  234. $statistics['is_issue']['amount'] = sprintf('%.2f', $isIssueTotal * $total['card_main_amount'] + $statistics['is_issue']['amount']);
  235. // 待分配统计
  236. $initModel = clone $cardModel;
  237. $initTotal = $initModel->where('card_status', 'INIT')->where('join_card_main_id', $total['card_main_id'])->count();
  238. $statistics['init']['total'] += $initTotal;
  239. $statistics['init']['amount'] = sprintf('%.2f', $initTotal * $total['card_main_amount'] + $statistics['init']['amount']);
  240. // 已分配,待售统计
  241. $waitingModel = clone $cardModel;
  242. $waitingTotal = $waitingModel->where('card_status', 'WAITING')->where('join_card_main_id', $total['card_main_id'])->count();
  243. $statistics['waiting']['total'] += $waitingTotal;
  244. $statistics['waiting']['amount'] = sprintf('%.2f', $waitingTotal * $total['card_main_amount'] + $statistics['waiting']['amount']);
  245. // 已售,待激活统计
  246. $pendingModel = clone $cardModel;
  247. $pendingTotal = $pendingModel->where('card_status', 'PENDING')->where('join_card_main_id', $total['card_main_id'])->count();
  248. $statistics['pending']['total'] += $pendingTotal;
  249. $statistics['pending']['amount'] = sprintf('%.2f', $pendingTotal * $total['card_main_amount'] + $statistics['pending']['amount']);
  250. // 已激活统计
  251. $usedModel = clone $cardModel;
  252. $usedTotal = $usedModel->where('card_status', 'USED')->where('join_card_main_id', $total['card_main_id'])->count();
  253. $statistics['used']['total'] += $usedTotal;
  254. $statistics['used']['amount'] = sprintf('%.2f', $usedTotal * $total['card_main_amount'] + $statistics['used']['amount']);
  255. // 使用完成统计
  256. $doneModel = clone $cardModel;
  257. $doneTotal = $doneModel->where('card_status', 'DONE')->where('join_card_main_id', $total['card_main_id'])->count();
  258. $statistics['done']['total'] += $doneTotal;
  259. $statistics['done']['amount'] = sprintf('%.2f', $doneTotal * $total['card_main_amount'] + $statistics['done']['amount']);
  260. // 过期统计
  261. $expiredModel = clone $cardModel;
  262. $doneTotal = $expiredModel->where('card_status', 'EXPIRED')->where('join_card_main_id', $total['card_main_id'])->count();
  263. $statistics['expired']['total'] += $doneTotal;
  264. $statistics['expired']['amount'] = sprintf('%.2f', $doneTotal * $total['card_main_amount'] + $statistics['expired']['amount']);
  265. // 冻结统计
  266. $pausedModel = clone $cardModel;
  267. $pausedTotal = $pausedModel->where('card_status', 'PAUSED')->where('join_card_main_id', $total['card_main_id'])->count();
  268. $statistics['paused']['total'] += $pausedTotal;
  269. $statistics['paused']['amount'] = sprintf('%.2f', $pausedTotal * $total['card_main_amount'] + $statistics['paused']['total']);
  270. }
  271. $statistics['total']['total'] = $statistics['init']['total'] + $statistics['waiting']['total']
  272. + $statistics['pending']['total'] + $statistics['used']['total'] + $statistics['done']['total']
  273. + $statistics['expired']['total'] + $statistics['paused']['total'];
  274. $statistics['total']['amount'] = sprintf('%.2f', $statistics['init']['amount']
  275. + $statistics['waiting']['amount'] + $statistics['pending']['amount'] + $statistics['used']['amount']
  276. + $statistics['expired']['amount'] + $statistics['done']['amount'] + $statistics['paused']['amount']);
  277. return json_success('success', $statistics);
  278. }
  279. public function info(Request $request): Response
  280. {
  281. $cardId = $request->get('card_id');
  282. if (!$cardId) {
  283. return json_fail("参数异常");
  284. }
  285. $info = Card::with([
  286. 'member' => function ($query) {
  287. $query->select('member_id', 'member_mobile', 'join_member_role_id', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer', 'member_is_franchisee');
  288. },
  289. 'cert',
  290. 'info',
  291. 'dept',
  292. 'user',
  293. 'main',
  294. 'issue'
  295. ])->where('card_id', $cardId)
  296. ->first();
  297. $info = $info ? $info->toArray() : [];
  298. if (!empty($info['member']) && !empty($info['member']['join_member_role_id'])) {
  299. $info['role_name'] = MemberRole::where('member_role_id', $info['member']['join_member_role_id'])->value('member_role_name');
  300. }
  301. // 头像
  302. $memberAvatar = !empty($info['info']) && !empty($info['info']['member_info_headimg']) ? $info['info']['member_info_headimg'] : '';
  303. $info['info']['member_info_headimg'] = MemberService::getAvatarUrl($memberAvatar);
  304. // 昵称
  305. $memberNickname = !empty($info['info']) && !empty($info['info']['member_info_nickname']) ? $info['info']['member_info_nickname'] : '';
  306. $memberMobile = !empty($info['member']) && !empty($info['member']['member_mobile']) ? $info['member']['member_mobile'] : '';
  307. $info['info']['member_info_nickname'] = MemberService::getNickname($memberNickname, $memberMobile);
  308. if (!empty($info['main'])) {
  309. if (!empty($info['main']['join_card_main_category_id'])) {
  310. $info['main_category_name'] = SysCategory::where('category_id', $info['main']['join_card_main_category_id'])->value('category_name');
  311. }
  312. if (!empty($info['main']['card_main_valid_mode']) && $info['main']['card_main_valid_mode'] == 'LONG') {
  313. $info['valid_term'] = '长期有效';
  314. } elseif (!empty($info['main']['card_main_valid_mode']) && $info['main']['card_main_valid_mode'] == 'DAYS') {
  315. $info['valid_term'] = $info['main']['card_main_valid_days'] . '天';
  316. } elseif (!empty($info['main']['card_main_valid_mode']) && $info['main']['card_main_valid_mode'] == 'DATE') {
  317. $info['valid_term'] = date('Y/m/d', strtotime($info['main']['card_main_valid_begin'])) . '-' . date('Y/m/d', strtotime($info['main']['card_main_valid_end']));
  318. }
  319. if (!empty($info['main']['card_main_json'])) {
  320. $cardMainJson = json_decode($info['main']['card_main_json'], true);
  321. $info['is_recharge'] = $cardMainJson['is_recharge'];
  322. $info['issue_mode'] = $cardMainJson['issue'];
  323. }
  324. if (!empty($info['main']['card_main_valid_mode'])) {
  325. if ($info['main']['card_main_valid_mode'] == 'LONG') {
  326. $info['valid_datetime'] = '长期有效';
  327. } else if (!empty($info['card_deadline_datetime'])) {
  328. $info['valid_datetime'] = date('Y/m/d', strtotime($info['card_assign_datetime'])) . '-' . date('Y/m/d', strtotime($info['card_deadline_datetime']));
  329. }
  330. }
  331. }
  332. if (!empty($info['issue']) && !empty($info['issue']['card_issue_creator_user_id'])) {
  333. $info['issue_username'] = SysUser::where('user_id', $info['issue']['card_issue_creator_user_id'])->value('user_name');
  334. }
  335. if (!empty($info['card_extend_json'])) {
  336. $cardExtendJson = json_decode($info['card_extend_json'], true);
  337. if (!empty($info['card_assign_datetime'])) {
  338. $info['info_assign']['datetime'] = $info['card_assign_datetime'];
  339. if (!empty($cardExtendJson['assign_user_id'])) {
  340. $info['info_assign']['username'] = SysUser::where('user_id', $cardExtendJson['assign_user_id'])->value('user_name');
  341. }
  342. }
  343. if (!empty($cardExtendJson['buy_datetime'])) {
  344. $info['info_buy']['datetime'] = $cardExtendJson['buy_datetime'];
  345. if (!empty($cardExtendJson['buy_user_id'])) {
  346. $info['info_buy']['username'] = SysUser::where('user_id', $cardExtendJson['buy_user_id'])->value('user_name');
  347. }
  348. if (!empty($cardExtendJson['buy_member'])) {
  349. $member = Member::with([
  350. 'cert' => function ($query) {
  351. $query->select('join_cert_member_id', 'member_cert_name');
  352. }
  353. ])->where('member_id', $cardExtendJson['buy_member'])
  354. ->first();
  355. $mobile = $member->member_mobile ?? '';
  356. $certName = !empty($member->cert) ? $member->cert->member_cert_name : '';
  357. $info['info_buy']['member_name'] = MemberService::getMemberCertName($mobile, $certName, '');
  358. }
  359. if (!empty($cardExtendJson['buy_order_id'])) {
  360. $info['info_buy']['order_id'] = $cardExtendJson['buy_order_id'];
  361. }
  362. }
  363. if (!empty($cardExtendJson['used_datetime'])) {
  364. $info['info_used']['datetime'] = $cardExtendJson['used_datetime'];
  365. $info['info_used']['username'] = '会员';
  366. if (!empty($cardExtendJson['used_user_id'])) {
  367. $info['info_used']['username'] = SysUser::where('user_id', $cardExtendJson['used_user_id'])->value('user_name');
  368. }
  369. }
  370. if (!empty($cardExtendJson['done_time'])) {
  371. $info['info_done']['datetime'] = $cardExtendJson['done_time'];
  372. if (!empty($cardExtendJson['done_user_id'])) {
  373. $info['info_done']['username'] = SysUser::where('user_id', $cardExtendJson['done_user_id'])->value('user_name');
  374. }
  375. }
  376. if (!empty($cardExtendJson['expired_datetime'])) {
  377. $info['info_expired']['datetime'] = $cardExtendJson['expired_datetime'];
  378. }
  379. if (!empty($cardExtendJson['paused_datetime'])) {
  380. $info['info_paused']['datetime'] = $cardExtendJson['paused_datetime'];
  381. if (!empty($cardExtendJson['paused_user_id'])) {
  382. $info['info_paused']['username'] = SysUser::where('user_id', $cardExtendJson['paused_user_id'])->value('user_name');
  383. }
  384. }
  385. if (!empty($cardExtendJson['issue_datetime'])) {
  386. $info['info_issue']['datetime'] = $cardExtendJson['issue_datetime'];
  387. if (!empty($cardExtendJson['issue_user_id'])) {
  388. $info['info_issue']['username'] = SysUser::where('user_id', $cardExtendJson['issue_user_id'])->value('user_name');
  389. }
  390. }
  391. }
  392. return json_success('', $info);
  393. }
  394. /**
  395. * 更新前置方法
  396. * @param Request $request
  397. * @return array
  398. * @throws BusinessException
  399. */
  400. protected function updateInput(Request $request): array
  401. {
  402. $primary_key = $this->model->getKeyName();
  403. $id = $request->post($primary_key);
  404. $data = $this->inputFilter($request->post());
  405. $model = $this->model->find($id);
  406. if (!$model) {
  407. throw new BusinessException('记录不存在', 2);
  408. }
  409. $cardExtendJson = [];
  410. if (!empty($model->card_extend_json)) {
  411. $cardExtendJson = json_decode($model->card_extend_json, true);
  412. }
  413. if ($data['is_issue'] == 'N') {
  414. unset($cardExtendJson['issue_datetime']);
  415. } elseif ($data['is_issue'] == 'Y') {
  416. $cardExtendJson['issue_datetime'] = date('Y-m-d H:i:s');
  417. }
  418. $data['card_extend_json'] = json_encode($cardExtendJson);
  419. if ($model->join_card_dept_id != $data['join_card_dept_id'] && empty($model->join_card_member_id)) {
  420. $data['card_assign_datetime'] = date('Y-m-d H:i:s');
  421. }
  422. if (!empty($data['join_card_dept_id']) && $model->card_status == 'INIT') {
  423. $data['card_status'] = 'WAITING';
  424. }
  425. _syslog('储值卡', '编辑', $data, $request->all());
  426. unset($data[$primary_key]);
  427. return [$id, $data];
  428. }
  429. /**
  430. * @Desc 获取指定状态的 card_id
  431. * @Author Gorden
  432. * @Date 2024/11/15 17:21
  433. *
  434. * @param Request $request
  435. * @return Response
  436. */
  437. public function getCardIdsByMainId(Request $request)
  438. {
  439. $mainId = $request->get('main_id');
  440. if (!$mainId) {
  441. return json_fail('参数异常');
  442. }
  443. $status = $request->get('status');
  444. $isIssue = $request->get('is_issue');
  445. $allocation = $request->get('allocation');
  446. $cardIds = Card::where('join_card_main_id', $mainId)
  447. ->when(!empty($status), function ($query) use ($status) {
  448. $query->where('card_status', $status);
  449. })->when(!empty($isIssue), function ($query) use ($isIssue) {
  450. $query->where('is_issue', $isIssue);
  451. })->when(!empty($allocation), function ($query) use ($allocation) {
  452. if ($allocation == 'N') { // 未分配
  453. $query->where('join_card_dept_id', '');
  454. } elseif ($allocation == 'Y') { // 已分配
  455. $query->where('join_card_dept_id', '<>', '');
  456. }
  457. })->pluck('card_id')
  458. ->toArray();
  459. return json_success('success', $cardIds);
  460. }
  461. /**
  462. * @Desc 储值卡发行
  463. * @Author Gorden
  464. * @Date 2024/11/18 15:13
  465. *
  466. * @param Request $request
  467. * @return Response
  468. */
  469. public function issue(Request $request)
  470. {
  471. $params = $request->post();
  472. Db::beginTransaction();
  473. try {
  474. $issueMode = $params['issue'];
  475. // 1.按卡号
  476. if ($issueMode == 'CARDID' && !empty($params['card_id'])) {
  477. foreach ($params['card_id'] as $card_id) {
  478. $card = Card::where('card_id', $card_id)->select('card_id', 'is_issue', 'card_extend_json')->first();
  479. if (!empty($card)) {
  480. $cardExtendJson = json_decode($card->card_extend_json, true);
  481. $cardExtendJson['issue_datetime'] = date('Y-m-d H:i:s');
  482. $cardExtendJson['issue_user_id'] = JwtToken::getCurrentId();
  483. $card->card_extend_json = json_encode($cardExtendJson);
  484. $card->is_issue = 'Y';
  485. $card->save();
  486. } else {
  487. throw new BusinessException($card_id . "不存在");
  488. }
  489. }
  490. } elseif ($issueMode == 'ISSUE' && !empty($params['card_issue_id'])) {
  491. // 2.按批次
  492. $cards = Card::where('join_card_issue_id', $params['card_issue_id'])
  493. ->where('is_issue', 'N')
  494. ->select('card_id', 'card_extend_json')
  495. ->get()
  496. ->toArray();
  497. if (!empty($cards)) {
  498. foreach ($cards as $cardItem) {
  499. $cardExtendJson = json_decode($cardItem['card_extend_json'], true);
  500. $cardExtendJson['issue_datetime'] = date('Y-m-d H:i:s');
  501. $cardExtendJson['issue_user_id'] = JwtToken::getCurrentId();
  502. Card::where('card_id', $cardItem['card_id'])->update([
  503. 'is_issue' => 'Y',
  504. 'card_extend_json' => json_encode($cardExtendJson)
  505. ]);
  506. }
  507. } else {
  508. throw new BusinessException("批次不存在");
  509. }
  510. }
  511. Db::commit();
  512. _syslog("储值卡", '发行成功');
  513. return json_success('success');
  514. } catch (BusinessException $e) {
  515. Db::rollBack();
  516. _syslog("储值卡", "发行失败:" . $e->getMessage());
  517. Log::error('储值卡发行失败', ['msg' => $e->getMessage()]);
  518. return json_fail("发行失败:" . $e->getMessage());
  519. } catch (\Exception $e) {
  520. Db::rollBack();
  521. _syslog("储值卡", '发行失败');
  522. Log::error('储值卡发行失败', ['msg' => $e->getMessage()]);
  523. return json_fail("发行失败");
  524. }
  525. }
  526. /**
  527. * @Desc 储值卡分配
  528. * @Author Gorden
  529. * @Date 2024/11/18 16:02
  530. *
  531. * @param Request $request
  532. * @return Response
  533. */
  534. public function allocation(Request $request)
  535. {
  536. $params = $request->post();
  537. try {
  538. $allocation = $params['allocation'];
  539. // 1.按卡号分配
  540. if ($allocation == 'CARDID' && !empty($params['card_id'])) {
  541. foreach ($params['card_id'] as $card_id) {
  542. $card = Card::where('card_id', $card_id)->select('card_id', 'card_extend_json')->first();
  543. if (!empty($card)) {
  544. $cardExtendJson = json_decode($card->card_extend_json, true);
  545. $cardExtendJson['assign_user_id'] = JwtToken::getCurrentId();
  546. $card->card_status = 'WAITING';
  547. $card->card_assign_datetime = date('Y-m-d H:i:s');
  548. $card->join_card_dept_id = $params['join_card_dept_id'];
  549. $card->card_extend_json = json_encode($cardExtendJson);
  550. $card->save();
  551. } else {
  552. throw new BusinessException($card_id . "不存在");
  553. }
  554. }
  555. // Card::whereIn('card_id', $params['card_id'])->where('join_card_dept_id', 0)->update([
  556. // 'join_card_dept_id' => $params['join_card_dept_id'],
  557. // 'card_status' => 'WAITING',
  558. // 'card_assign_datetime' => date('Y-m-d H:i:s')
  559. // ]);
  560. } elseif ($allocation == 'ISSUE' && !empty($params['card_issue_id'])) {
  561. $cards = Card::where('join_card_issue_id', $params['card_issue_id'])
  562. ->where('card_status', 'INIT')
  563. ->select('card_id', 'card_extend_json')
  564. ->get()
  565. ->toArray();
  566. if (!empty($cards)) {
  567. foreach ($cards as $cardItem) {
  568. $cardExtendJson = json_decode($cardItem['card_extend_json'], true);
  569. $cardExtendJson['assign_user_id'] = JwtToken::getCurrentId();
  570. Card::where('card_id', $cardItem['card_id'])->update([
  571. 'card_status' => 'WAITING',
  572. 'card_assign_datetime' => date('Y-m-d H:i:s'),
  573. 'join_card_dept_id' => $params['join_card_dept_id'],
  574. 'card_extend_json' => json_encode($cardExtendJson)
  575. ]);
  576. }
  577. } else {
  578. throw new BusinessException("批次不存在");
  579. }
  580. // Card::where('join_card_issue_id', $params['card_issue_id'])->where('join_card_dept_id', 0)->update([
  581. // 'join_card_dept_id' => $params['join_card_dept_id'],
  582. // 'card_status' => 'WAITING',
  583. // 'card_assign_datetime' => date('Y-m-d H:i:s')
  584. // ]);
  585. }
  586. _syslog("储值卡", '分配成功');
  587. return json_success('success');
  588. } catch (\Exception $e) {
  589. _syslog("储值卡", '分配失败');
  590. Log::error('储值卡分配失败', ['msg' => $e->getMessage()]);
  591. return json_fail('分配失败');
  592. }
  593. }
  594. /**
  595. * @Desc 导出储值卡
  596. * @Author Gorden
  597. * @Date 2024/11/21 13:03
  598. *
  599. * @param Request $request
  600. * @return Response
  601. */
  602. public function exportCard(Request $request)
  603. {
  604. $cardStatus = $request->get('card_status');
  605. $isIssue = $request->get('is_issue');
  606. $cardMainName = $request->get('card_main_name');
  607. $cardIssueName = $request->get('card_issue_name');
  608. $deptId = $request->get('join_card_dept_id');
  609. $cardAddtimes = $request->get('card_addtimes');
  610. $cardIds = $request->get('card_ids');
  611. $cards = Card::with([
  612. 'member' => function ($query) {
  613. $query->select('member_id', 'member_mobile');
  614. },
  615. 'cert' => function ($query) {
  616. $query->select('join_cert_member_id', 'member_cert_name');
  617. },
  618. 'dept',
  619. 'user',
  620. 'main' => function ($query) {
  621. $query->select('card_main_id', 'card_main_name', 'card_main_amount', 'card_main_valid_mode');
  622. },
  623. 'issue' => function ($query) {
  624. $query->select('card_issue_id', 'card_issue_name');
  625. }
  626. ])->when(!empty($cardStatus), function ($query) use ($cardStatus) {
  627. $query->where('card_status', $cardStatus);
  628. })->when(!empty($isIssue), function ($query) use ($isIssue) {
  629. $query->where('is_issue', $isIssue);
  630. })->when(!empty($deptId), function ($query) use ($deptId) {
  631. $query->where('join_card_dept_id', $deptId);
  632. })->when(!empty($cardAddtimes), function ($query) use ($cardAddtimes) {
  633. $cardAddtimes[0] = strtotime($cardAddtimes[0]);
  634. $cardAddtimes[1] = strtotime($cardAddtimes[1]);
  635. $query->whereBetween('card_addtimes', $cardAddtimes);
  636. })->when(!empty($cardIds), function ($query) use ($cardIds) {
  637. $query->whereIn('card_id', $cardIds);
  638. })->when(!empty($cardMainName), function ($query) use ($cardMainName) {
  639. $cardMainIds = CardMain::where('card_main_name', 'like', '%' . $cardMainName . '%')->pluck('card_main_id')->toArray();
  640. $query->whereIn('join_card_main_id', $cardMainIds);
  641. })->when(!empty($cardIssueName), function ($query) use ($cardIssueName) {
  642. $cardIssueIds = CardIssue::where('card_issue_name', 'like', '%' . $cardIssueName . '%')->pluck('card_issue_id')->toArray();
  643. $query->whereIn('join_card_issue_id', $cardIssueIds);
  644. })->orderByDesc('card_addtimes')
  645. ->get()
  646. ->toArray();
  647. $data = [];
  648. foreach ($cards as $item) {
  649. $mobile = $item['member']['member_mobile'] ?? '';
  650. $certName = $item['cert']['member_cert_name'] ?? '';
  651. $item['member_name'] = MemberService::getMemberCertName($mobile, $certName, '');
  652. if (!empty($item['main']['card_main_valid_mode'])) {
  653. if ($item['main']['card_main_valid_mode'] == 'LONG') {
  654. $item['valid_datetime'] = '长期有效';
  655. } else if (!empty($item['card_deadline_datetime'])) {
  656. $item['valid_datetime'] = date('Y/m/d H:i:s', strtotime($item['card_assign_datetime'])) . '-' . date('Y/m/d H:i:s', strtotime($item['card_deadline_datetime']));
  657. }
  658. }
  659. $cardExtendJson = [];
  660. if (!empty($item['card_extend_json'])) {
  661. $cardExtendJson = json_decode($item['card_extend_json'], true);
  662. }
  663. if (!empty($cardExtendJson['issue_datetime'])) {
  664. $item['issue_datetime'] = $cardExtendJson['issue_datetime'];
  665. }
  666. $data[] = [
  667. 'card_id' => $item['card_id'],
  668. 'main_name' => !empty($item['main']['card_main_name']) ? $item['main']['card_main_name'] : '--',
  669. 'issue_name' => !empty($item['issue']['card_issue_name']) ? $item['issue']['card_issue_name'] : '--',
  670. 'main_amount' => !empty($item['main']['card_main_amount']) ? $item['main']['card_main_amount'] : '--',
  671. 'dept_name' => !empty($item['dept']['dept_name']) ? $item['dept']['dept_name'] : '--',
  672. 'member_name' => $item['member_name'] ?? '--',
  673. 'valid_datetime' => $item['valid_datetime'] ?? '--',
  674. 'status' => CardService::$status[$item['card_status']],
  675. 'is_issue' => $item['is_issue'] == 'Y' ? '已发行' : '未发行',
  676. 'issue_datetime' => $item['issue_datetime'] ?? '--',
  677. 'card_addtimes' => $item['card_addtimes']
  678. ];
  679. }
  680. return json_success("success", $data);
  681. }
  682. /**
  683. * @Desc 激活记录
  684. * @Author Gorden
  685. * @Date 2024/11/27 16:04
  686. *
  687. * @param Request $request
  688. * @return Response
  689. */
  690. public function usedList(Request $request)
  691. {
  692. $page = $request->get('page', 1);
  693. $pageSize = $request->get('pageSize', 10);
  694. $mainId = $request->get('main_id');
  695. if (!$mainId) {
  696. return json_fail("参数异常");
  697. }
  698. $cards = Card::leftJoin("member", 'member.member_id', '=', 'card.join_card_member_id')
  699. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', '=', 'card.join_card_member_id')
  700. ->leftJoin('sys_dept', 'sys_dept.dept_id', '=', 'card.join_card_dept_id')
  701. ->where('join_card_main_id', $mainId)
  702. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.used_datetime'))) as SIGNED) > 0");
  703. $total = $cards->count();
  704. $rows = $cards->selectRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.used_datetime'))) as SIGNED) as used_time,
  705. member_mobile,member_cert_name,dept_name,card_id,card_status,card_addtimes")
  706. ->orderByDesc('used_time')
  707. ->forPage($page, $pageSize)
  708. ->get()
  709. ->toArray();
  710. foreach ($rows as &$row) {
  711. $mobile = $row['member_mobile'] ?? '';
  712. $certName = $row['member_cert_name'] ?? '';
  713. $row['member_name'] = MemberService::getMemberCertName($mobile, $certName, '');
  714. $row['used_time'] = date('Y-m-d H:i:s', $row['used_time']);
  715. }
  716. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  717. }
  718. /**
  719. * @Desc 发行记录
  720. * @Author Gorden
  721. * @Date 2024/11/27 16:05
  722. *
  723. * @param Request $request
  724. * @return Response
  725. */
  726. public function issueList(Request $request)
  727. {
  728. $page = $request->get('page', 1);
  729. $pageSize = $request->get('pageSize', 10);
  730. $mainId = $request->get('main_id');
  731. if (!$mainId) {
  732. return json_fail("参数异常");
  733. }
  734. $cards = Card::leftJoin("member", 'member.member_id', '=', 'card.join_card_member_id')
  735. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', '=', 'card.join_card_member_id')
  736. ->leftJoin('sys_dept', 'sys_dept.dept_id', '=', 'card.join_card_dept_id')
  737. ->where('join_card_main_id', $mainId)
  738. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.issue_datetime'))) as SIGNED) > 0");
  739. $total = $cards->count();
  740. $rows = $cards->selectRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.issue_datetime'))) as SIGNED) as issue_time,
  741. member_mobile,member_cert_name,dept_name,card_id,card_status,card_addtimes")
  742. ->orderByDesc('issue_time')
  743. ->forPage($page, $pageSize)
  744. ->get()
  745. ->toArray();
  746. foreach ($rows as &$row) {
  747. $mobile = $row['member_mobile'] ?? '';
  748. $certName = $row['member_cert_name'] ?? '';
  749. $row['member_name'] = MemberService::getMemberCertName($mobile, $certName, '');
  750. $row['issue_time'] = date('Y-m-d H:i:s', $row['issue_time']);
  751. }
  752. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  753. }
  754. }