CardController.php 38 KB

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