CardController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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\MemberRole;
  10. use app\model\SysCategory;
  11. use app\model\SysUser;
  12. use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
  13. use Illuminate\Database\Query\Builder as QueryBuilder;
  14. use support\Db;
  15. use support\exception\BusinessException;
  16. use support\Log;
  17. use support\Model;
  18. use support\Request;
  19. use support\Response;
  20. class CardController extends Curd
  21. {
  22. public function __construct()
  23. {
  24. $this->model = new Card();
  25. }
  26. /**
  27. * 查询
  28. * @param Request $request
  29. * @return Response
  30. * @throws BusinessException
  31. */
  32. public function select(Request $request): Response
  33. {
  34. $cardMainName = $request->get('card_main_name');
  35. $cardIssueName = $request->get('card_issue_name');
  36. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  37. $order = $request->get('order', 'desc');
  38. $field = $field ?? 'card_addtimes';
  39. if (!empty($where['card_addtimes'])) {
  40. $where['card_addtimes'][0] = strtotime($where['card_addtimes'][0]);
  41. $where['card_addtimes'][1] = strtotime($where['card_addtimes'][1]);
  42. }
  43. if (!empty($cardMainName)) {
  44. $cardMainIds = CardMain::where('card_main_name', 'like', '%' . $cardMainName . '%')->pluck('card_main_id')->toArray();
  45. $where['join_card_main_id'] = ['in', $cardMainIds];
  46. }
  47. if (!empty($cardIssueName)) {
  48. $cardIssueIds = CardIssue::where('card_issue_name', 'like', '%' . $cardIssueName . '%')->pluck('card_issue_id')->toArray();
  49. $where['join_card_issue_id'] = ['in', $cardIssueIds];
  50. }
  51. $query = $this->doSelect($where, $field, $order);
  52. return $this->doFormat($query, $format, $limit);
  53. }
  54. /**
  55. * 指定查询where条件,并没有真正的查询数据库操作
  56. * @param array $where
  57. * @param string|null $field
  58. * @param string $order
  59. * @return EloquentBuilder|QueryBuilder|Model
  60. */
  61. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  62. {
  63. $model = $this->model->with([
  64. 'member' => function ($query) {
  65. $query->select('member_id', 'member_mobile');
  66. },
  67. 'cert' => function ($query) {
  68. $query->select('join_cert_member_id', 'member_cert_name');
  69. },
  70. 'dept',
  71. 'user',
  72. 'main' => function ($query) {
  73. $query->select('card_main_id', 'card_main_name', 'card_main_amount', 'card_main_valid_mode');
  74. },
  75. 'issue' => function ($query) {
  76. $query->select('card_issue_id', 'card_issue_name');
  77. }
  78. ]);
  79. foreach ($where as $column => $value) {
  80. if (is_array($value)) {
  81. if ($value[0] === 'like' || $value[0] === 'not like') {
  82. $model = $model->where($column, $value[0], "%$value[1]%");
  83. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  84. $model = $model->where($column, $value[0], $value[1]);
  85. } elseif ($value[0] == 'in') {
  86. $valArr = $value[1];
  87. if (is_string($value[1])) {
  88. $valArr = explode(",", trim($value[1]));
  89. }
  90. $model = $model->whereIn($column, $valArr);
  91. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  92. $valArr = $value[1];
  93. if (is_string($value[1])) {
  94. $valArr = explode(",", trim($value[1]));
  95. }
  96. $model = $model->whereNotIn($column, $valArr);
  97. } elseif ($value[0] == 'null') {
  98. $model = $model->whereNull($column);
  99. } elseif ($value[0] == 'not null') {
  100. $model = $model->whereNotNull($column);
  101. } elseif ($value[0] !== '' || $value[1] !== '') {
  102. $model = $model->whereBetween($column, $value);
  103. }
  104. } else {
  105. $model = $model->where($column, $value);
  106. }
  107. }
  108. if ($field) {
  109. $model = $model->orderBy($field, $order)->orderByDesc('card_id');
  110. }
  111. return $model;
  112. }
  113. public function afterQuery($items)
  114. {
  115. foreach ($items as &$item) {
  116. $mobile = $item['member']['member_mobile'] ?? '';
  117. $certName = $item['cert']['member_cert_name'] ?? '';
  118. $item['member_name'] = MemberService::getMemberCertName($mobile, $certName, '');
  119. if (!empty($item['main']['card_main_valid_mode'])) {
  120. if ($item['main']['card_main_valid_mode'] == 'LONG') {
  121. $item['valid_datetime'] = '长期有效';
  122. } else if (!empty($item['card_deadline_datetime'])) {
  123. $item['valid_datetime'] = date('Y/m/d', strtotime($item['card_assign_datetime'])) . '-' . date('Y/m/d', strtotime($item['card_deadline_datetime']));
  124. }
  125. }
  126. $cardExtendJson = [];
  127. if (!empty($item['card_extend_json'])) {
  128. $cardExtendJson = json_decode($item['card_extend_json'], true);
  129. }
  130. if (!empty($cardExtendJson['issue_datetime'])) {
  131. $item['issue_datetime'] = $cardExtendJson['issue_datetime'];
  132. }
  133. }
  134. return $items;
  135. }
  136. public function info(Request $request): Response
  137. {
  138. $cardId = $request->get('card_id');
  139. if (!$cardId) {
  140. return json_fail("参数异常");
  141. }
  142. $info = Card::with([
  143. 'member' => function ($query) {
  144. $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');
  145. },
  146. 'cert',
  147. 'info',
  148. 'dept',
  149. 'user',
  150. 'main',
  151. 'issue'
  152. ])->where('card_id', $cardId)
  153. ->first();
  154. $info = $info ? $info->toArray() : [];
  155. if (!empty($info['member']) && !empty($info['member']['join_member_role_id'])) {
  156. $info['role_name'] = MemberRole::where('member_role_id', $info['member']['join_member_role_id'])->value('member_role_name');
  157. }
  158. // 头像
  159. $memberAvatar = !empty($info['info']) && !empty($info['info']['member_info_headimg']) ? $info['info']['member_info_headimg'] : '';
  160. $info['info']['member_info_headimg'] = MemberService::getAvatarUrl($memberAvatar);
  161. // 昵称
  162. $memberNickname = !empty($info['info']) && !empty($info['info']['member_info_nickname']) ? $info['info']['member_info_nickname'] : '';
  163. $memberMobile = !empty($info['member']) && !empty($info['member']['member_mobile']) ? $info['member']['member_mobile'] : '';
  164. $info['info']['member_info_nickname'] = MemberService::getNickname($memberNickname, $memberMobile);
  165. if (!empty($info['main'])) {
  166. if (!empty($info['main']['join_card_main_category_id'])) {
  167. $info['main_category_name'] = SysCategory::where('category_id', $info['main']['join_card_main_category_id'])->value('category_name');
  168. }
  169. if (!empty($info['main']['card_main_valid_mode']) && $info['main']['card_main_valid_mode'] == 'LONG') {
  170. $info['valid_term'] = '长期有效';
  171. } elseif (!empty($info['main']['card_main_valid_mode']) && $info['main']['card_main_valid_mode'] == 'DAYS') {
  172. $info['valid_term'] = $info['main']['card_main_valid_days'] . '天';
  173. } elseif (!empty($info['main']['card_main_valid_mode']) && $info['main']['card_main_valid_mode'] == 'DATE') {
  174. $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']));
  175. }
  176. if (!empty($info['main']['card_main_json'])) {
  177. $cardMainJson = json_decode($info['main']['card_main_json'], true);
  178. $info['is_recharge'] = $cardMainJson['is_recharge'];
  179. $info['issue_mode'] = $cardMainJson['issue'];
  180. }
  181. if (!empty($info['main']['card_main_valid_mode'])) {
  182. if ($info['main']['card_main_valid_mode'] == 'LONG') {
  183. $info['valid_datetime'] = '长期有效';
  184. } else if (!empty($info['card_deadline_datetime'])) {
  185. $info['valid_datetime'] = date('Y/m/d', strtotime($info['card_assign_datetime'])) . '-' . date('Y/m/d', strtotime($info['card_deadline_datetime']));
  186. }
  187. }
  188. }
  189. if (!empty($info['issue']) && !empty($info['issue']['card_issue_creator_user_id'])) {
  190. $info['issue_username'] = SysUser::where('user_id', $info['issue']['card_issue_creator_user_id'])->value('user_name');
  191. }
  192. return json_success('', $info);
  193. }
  194. /**
  195. * 更新前置方法
  196. * @param Request $request
  197. * @return array
  198. * @throws BusinessException
  199. */
  200. protected function updateInput(Request $request): array
  201. {
  202. $primary_key = $this->model->getKeyName();
  203. $id = $request->post($primary_key);
  204. $data = $this->inputFilter($request->post());
  205. $model = $this->model->find($id);
  206. if (!$model) {
  207. throw new BusinessException('记录不存在', 2);
  208. }
  209. $cardExtendJson = [];
  210. if (!empty($model->card_extend_json)) {
  211. $cardExtendJson = json_decode($model->card_extend_json, true);
  212. }
  213. if ($data['is_issue'] == 'N') {
  214. unset($cardExtendJson['issue_datetime']);
  215. } elseif ($data['is_issue'] == 'Y') {
  216. $cardExtendJson['issue_datetime'] = date('Y-m-d H:i:s');
  217. }
  218. $data['card_extend_json'] = json_encode($cardExtendJson);
  219. if ($model->join_card_dept_id != $data['join_card_dept_id'] && empty($model->join_card_member_id)) {
  220. $data['card_assign_datetime'] = date('Y-m-d H:i:s');
  221. }
  222. if (!empty($data['join_card_dept_id']) && $model->card_status == 'INIT'){
  223. $data['card_status'] = 'WAITING';
  224. }
  225. _syslog('储值卡', '编辑', $data, $request->all());
  226. unset($data[$primary_key]);
  227. return [$id, $data];
  228. }
  229. /**
  230. * @Desc 获取指定状态的 card_id
  231. * @Author Gorden
  232. * @Date 2024/11/15 17:21
  233. *
  234. * @param Request $request
  235. * @return Response
  236. */
  237. public function getCardIdsByMainId(Request $request)
  238. {
  239. $mainId = $request->get('main_id');
  240. if (!$mainId) {
  241. return json_fail('参数异常');
  242. }
  243. $status = $request->get('status');
  244. $isIssue = $request->get('is_issue');
  245. $allocation = $request->get('allocation');
  246. $cardIds = Card::where('join_card_main_id', $mainId)
  247. ->when(!empty($status), function ($query) use ($status) {
  248. $query->where('card_status', $status);
  249. })->when(!empty($isIssue), function ($query) use ($isIssue) {
  250. $query->where('is_issue', $isIssue);
  251. })->when(!empty($allocation), function ($query) use ($allocation) {
  252. if ($allocation == 'N') { // 未分配
  253. $query->where('join_card_dept_id', '');
  254. } elseif ($allocation == 'Y') { // 已分配
  255. $query->where('join_card_dept_id', '<>', '');
  256. }
  257. })->pluck('card_id')
  258. ->toArray();
  259. return json_success('success', $cardIds);
  260. }
  261. /**
  262. * @Desc 储值卡发行
  263. * @Author Gorden
  264. * @Date 2024/11/18 15:13
  265. *
  266. * @param Request $request
  267. * @return Response
  268. */
  269. public function issue(Request $request)
  270. {
  271. $params = $request->post();
  272. Db::beginTransaction();
  273. try {
  274. $issueMode = $params['issue'];
  275. // 1.按卡号
  276. if ($issueMode == 'CARDID' && !empty($params['card_id'])) {
  277. foreach ($params['card_id'] as $card_id) {
  278. $card = Card::where('card_id', $card_id)->select('card_id', 'is_issue', 'card_extend_json')->first();
  279. if (!empty($card)) {
  280. $cardExtendJson = json_decode($card->card_extend_json, true);
  281. $cardExtendJson['issue_datetime'] = date('Y-m-d H:i:s');
  282. $card->card_extend_json = json_encode($cardExtendJson);
  283. $card->is_issue = 'Y';
  284. $card->save();
  285. } else {
  286. throw new BusinessException($card_id . "不存在");
  287. }
  288. }
  289. } elseif ($issueMode == 'ISSUE' && !empty($params['card_issue_id'])) {
  290. // 2.按批次
  291. $cards = Card::where('join_card_issue_id', $params['card_issue_id'])
  292. ->where('is_issue', 'N')
  293. ->select('card_id', 'card_extend_json')
  294. ->get()
  295. ->toArray();
  296. if (!empty($cards)) {
  297. foreach ($cards as $cardItem) {
  298. $cardExtendJson = json_decode($cardItem['card_extend_json'], true);
  299. $cardExtendJson['issue_datetime'] = date('Y-m-d H:i:s');
  300. Card::where('card_id', $cardItem['card_id'])->update([
  301. 'is_issue' => 'Y',
  302. 'card_extend_json' => json_encode($cardExtendJson)
  303. ]);
  304. }
  305. } else {
  306. throw new BusinessException("批次不存在");
  307. }
  308. }
  309. Db::commit();
  310. _syslog("储值卡", '发行成功');
  311. return json_success('success');
  312. } catch (BusinessException $e) {
  313. Db::rollBack();
  314. _syslog("储值卡", "发行失败:" . $e->getMessage());
  315. Log::error('储值卡发行失败', ['msg' => $e->getMessage()]);
  316. return json_fail("发行失败:" . $e->getMessage());
  317. } catch (\Exception $e) {
  318. Db::rollBack();
  319. _syslog("储值卡", '发行失败');
  320. Log::error('储值卡发行失败', ['msg' => $e->getMessage()]);
  321. return json_fail("发行失败");
  322. }
  323. }
  324. /**
  325. * @Desc 储值卡分配
  326. * @Author Gorden
  327. * @Date 2024/11/18 16:02
  328. *
  329. * @param Request $request
  330. * @return Response
  331. */
  332. public function allocation(Request $request)
  333. {
  334. $params = $request->post();
  335. try {
  336. $allocation = $params['allocation'];
  337. // 1.按卡号分配
  338. if ($allocation == 'CARDID' && !empty($params['card_id'])) {
  339. Card::whereIn('card_id', $params['card_id'])->where('join_card_dept_id', 0)->update([
  340. 'join_card_dept_id' => $params['join_card_dept_id'],
  341. 'card_status'=>'WAITING',
  342. 'card_assign_datetime' => date('Y-m-d H:i:s')
  343. ]);
  344. } elseif ($allocation == 'ISSUE' && !empty($params['card_issue_id'])) {
  345. Card::where('join_card_issue_id', $params['card_issue_id'])->where('join_card_dept_id', 0)->update([
  346. 'join_card_dept_id' => $params['join_card_dept_id'],
  347. 'card_status'=>'WAITING',
  348. 'card_assign_datetime' => date('Y-m-d H:i:s')
  349. ]);
  350. }
  351. _syslog("储值卡", '分配成功');
  352. return json_success('success');
  353. } catch (\Exception $e) {
  354. _syslog("储值卡", '分配失败');
  355. Log::error('储值卡分配失败', ['msg' => $e->getMessage()]);
  356. return json_fail('分配失败');
  357. }
  358. }
  359. /**
  360. * @Desc 导出储值卡
  361. * @Author Gorden
  362. * @Date 2024/11/21 13:03
  363. *
  364. * @param Request $request
  365. * @return Response
  366. */
  367. public function exportCard(Request $request)
  368. {
  369. $cardStatus = $request->get('card_status');
  370. $isIssue = $request->get('is_issue');
  371. $cardMainName = $request->get('card_main_name');
  372. $cardIssueName = $request->get('card_issue_name');
  373. $deptId = $request->get('join_card_dept_id');
  374. $cardAddtimes = $request->get('card_addtimes');
  375. $cardIds = $request->get('card_ids');
  376. $cards = Card::with([
  377. 'member' => function ($query) {
  378. $query->select('member_id', 'member_mobile');
  379. },
  380. 'cert' => function ($query) {
  381. $query->select('join_cert_member_id', 'member_cert_name');
  382. },
  383. 'dept',
  384. 'user',
  385. 'main' => function ($query) {
  386. $query->select('card_main_id', 'card_main_name', 'card_main_amount', 'card_main_valid_mode');
  387. },
  388. 'issue' => function ($query) {
  389. $query->select('card_issue_id', 'card_issue_name');
  390. }
  391. ])->when(!empty($cardStatus), function ($query) use ($cardStatus) {
  392. $query->where('card_status', $cardStatus);
  393. })->when(!empty($isIssue), function ($query) use ($isIssue) {
  394. $query->where('is_issue', $isIssue);
  395. })->when(!empty($deptId), function ($query) use ($deptId) {
  396. $query->where('join_card_dept_id', $deptId);
  397. })->when(!empty($cardAddtimes), function ($query) use ($cardAddtimes) {
  398. $cardAddtimes[0] = strtotime($cardAddtimes[0]);
  399. $cardAddtimes[1] = strtotime($cardAddtimes[1]);
  400. $query->whereBetween('card_addtimes', $cardAddtimes);
  401. })->when(!empty($cardIds), function ($query) use ($cardIds) {
  402. $query->whereIn('card_id', $cardIds);
  403. })->when(!empty($cardMainName), function ($query) use ($cardMainName) {
  404. $cardMainIds = CardMain::where('card_main_name', 'like', '%' . $cardMainName . '%')->pluck('card_main_id')->toArray();
  405. $query->whereIn('join_card_main_id', $cardMainIds);
  406. })->when(!empty($cardIssueName), function ($query) use ($cardIssueName) {
  407. $cardIssueIds = CardIssue::where('card_issue_name', 'like', '%' . $cardIssueName . '%')->pluck('card_issue_id')->toArray();
  408. $query->whereIn('join_card_issue_id', $cardIssueIds);
  409. })->orderByDesc('card_addtimes')
  410. ->get()
  411. ->toArray();
  412. $data = [];
  413. foreach ($cards as $item) {
  414. $mobile = $item['member']['member_mobile'] ?? '';
  415. $certName = $item['cert']['member_cert_name'] ?? '';
  416. $item['member_name'] = MemberService::getMemberCertName($mobile, $certName, '');
  417. if (!empty($item['main']['card_main_valid_mode'])) {
  418. if ($item['main']['card_main_valid_mode'] == 'LONG') {
  419. $item['valid_datetime'] = '长期有效';
  420. } else if (!empty($item['card_deadline_datetime'])) {
  421. $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']));
  422. }
  423. }
  424. $cardExtendJson = [];
  425. if (!empty($item['card_extend_json'])) {
  426. $cardExtendJson = json_decode($item['card_extend_json'], true);
  427. }
  428. if (!empty($cardExtendJson['issue_datetime'])) {
  429. $item['issue_datetime'] = $cardExtendJson['issue_datetime'];
  430. }
  431. $data[] = [
  432. 'card_id' => $item['card_id'],
  433. 'main_name' => !empty($item['main']['card_main_name']) ? $item['main']['card_main_name'] : '--',
  434. 'issue_name' => !empty($item['issue']['card_issue_name']) ? $item['issue']['card_issue_name'] : '--',
  435. 'main_amount' => !empty($item['main']['card_main_amount']) ? $item['main']['card_main_amount'] : '--',
  436. 'dept_name' => !empty($item['dept']['dept_name']) ? $item['dept']['dept_name'] : '--',
  437. 'member_name' => $item['member_name'] ?? '--',
  438. 'valid_datetime' => $item['valid_datetime'] ?? '--',
  439. 'status' => CardService::$status[$item['card_status']],
  440. 'is_issue' => $item['is_issue'] == 'Y' ? '已发行' : '未发行',
  441. 'issue_datetime' => $item['issue_datetime'] ?? '--',
  442. 'card_addtimes' => $item['card_addtimes']
  443. ];
  444. }
  445. return json_success("success", $data);
  446. }
  447. }