CardController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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);
  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. _syslog('储值卡', '编辑', $data, $request->all());
  223. unset($data[$primary_key]);
  224. return [$id, $data];
  225. }
  226. /**
  227. * @Desc 获取指定状态的 card_id
  228. * @Author Gorden
  229. * @Date 2024/11/15 17:21
  230. *
  231. * @param Request $request
  232. * @return Response
  233. */
  234. public function getCardIdsByMainId(Request $request)
  235. {
  236. $mainId = $request->get('main_id');
  237. if (!$mainId) {
  238. return json_fail('参数异常');
  239. }
  240. $status = $request->get('status');
  241. $isIssue = $request->get('is_issue');
  242. $allocation = $request->get('allocation');
  243. $cardIds = Card::where('join_card_main_id', $mainId)
  244. ->when(!empty($status), function ($query) use ($status) {
  245. $query->where('card_status', $status);
  246. })->when(!empty($isIssue), function ($query) use ($isIssue) {
  247. $query->where('is_issue', $isIssue);
  248. })->when(!empty($allocation), function ($query) use ($allocation) {
  249. if ($allocation == 'N') { // 未分配
  250. $query->where('join_card_dept_id', '');
  251. } elseif ($allocation == 'Y') { // 已分配
  252. $query->where('join_card_dept_id', '<>', '');
  253. }
  254. })->pluck('card_id')
  255. ->toArray();
  256. return json_success('success', $cardIds);
  257. }
  258. /**
  259. * @Desc 储值卡发行
  260. * @Author Gorden
  261. * @Date 2024/11/18 15:13
  262. *
  263. * @param Request $request
  264. * @return Response
  265. */
  266. public function issue(Request $request)
  267. {
  268. $params = $request->post();
  269. Db::beginTransaction();
  270. try {
  271. $issueMode = $params['issue'];
  272. // 1.按卡号
  273. if ($issueMode == 'CARDID' && !empty($params['card_id'])) {
  274. foreach ($params['card_id'] as $card_id) {
  275. $card = Card::where('card_id', $card_id)->select('card_id', 'is_issue', 'card_extend_json')->first();
  276. if (!empty($card)) {
  277. $cardExtendJson = json_decode($card->card_extend_json, true);
  278. $cardExtendJson['issue_datetime'] = date('Y-m-d H:i:s');
  279. $card->card_extend_json = json_encode($cardExtendJson);
  280. $card->is_issue = 'Y';
  281. $card->save();
  282. } else {
  283. throw new BusinessException($card_id . "不存在");
  284. }
  285. }
  286. } elseif ($issueMode == 'ISSUE' && !empty($params['card_issue_id'])) {
  287. // 2.按批次
  288. $cards = Card::where('join_card_issue_id', $params['card_issue_id'])
  289. ->where('is_issue', 'N')
  290. ->select('card_id', 'card_extend_json')
  291. ->get()
  292. ->toArray();
  293. if (!empty($cards)) {
  294. foreach ($cards as $cardItem) {
  295. $cardExtendJson = json_decode($cardItem['card_extend_json'], true);
  296. $cardExtendJson['issue_datetime'] = date('Y-m-d H:i:s');
  297. Card::where('card_id', $cardItem['card_id'])->update([
  298. 'is_issue' => 'Y',
  299. 'card_extend_json' => json_encode($cardExtendJson)
  300. ]);
  301. }
  302. } else {
  303. throw new BusinessException("批次不存在");
  304. }
  305. }
  306. Db::commit();
  307. _syslog("储值卡", '发行成功');
  308. return json_success('success');
  309. } catch (BusinessException $e) {
  310. Db::rollBack();
  311. _syslog("储值卡", "发行失败:" . $e->getMessage());
  312. Log::error('储值卡发行失败', ['msg' => $e->getMessage()]);
  313. return json_fail("发行失败:" . $e->getMessage());
  314. } catch (\Exception $e) {
  315. Db::rollBack();
  316. _syslog("储值卡", '发行失败');
  317. Log::error('储值卡发行失败', ['msg' => $e->getMessage()]);
  318. return json_fail("发行失败");
  319. }
  320. }
  321. /**
  322. * @Desc 储值卡分配
  323. * @Author Gorden
  324. * @Date 2024/11/18 16:02
  325. *
  326. * @param Request $request
  327. * @return Response
  328. */
  329. public function allocation(Request $request)
  330. {
  331. $params = $request->post();
  332. try {
  333. $allocation = $params['allocation'];
  334. // 1.按卡号分配
  335. if ($allocation == 'CARDID' && !empty($params['card_id'])) {
  336. Card::whereIn('card_id', $params['card_id'])->where('join_card_dept_id', 0)->update([
  337. 'join_card_dept_id' => $params['join_card_dept_id'],
  338. 'card_assign_datetime' => date('Y-m-d H:i:s')
  339. ]);
  340. } elseif ($allocation == 'ISSUE' && !empty($params['card_issue_id'])) {
  341. Card::where('join_card_issue_id', $params['card_issue_id'])->where('join_card_dept_id', 0)->update([
  342. 'join_card_dept_id' => $params['join_card_dept_id'],
  343. 'card_assign_datetime' => date('Y-m-d H:i:s')
  344. ]);
  345. }
  346. _syslog("储值卡", '分配成功');
  347. return json_success('success');
  348. } catch (\Exception $e) {
  349. _syslog("储值卡", '分配失败');
  350. Log::error('储值卡分配失败', ['msg' => $e->getMessage()]);
  351. return json_fail('分配失败');
  352. }
  353. }
  354. /**
  355. * @Desc 导出储值卡
  356. * @Author Gorden
  357. * @Date 2024/11/21 13:03
  358. *
  359. * @param Request $request
  360. * @return Response
  361. */
  362. public function exportCard(Request $request)
  363. {
  364. $cardStatus = $request->get('card_status');
  365. $isIssue = $request->get('is_issue');
  366. $cardMainName = $request->get('card_main_name');
  367. $cardIssueName = $request->get('card_issue_name');
  368. $deptId = $request->get('join_card_dept_id');
  369. $cardAddtimes = $request->get('card_addtimes');
  370. $cardIds = $request->get('card_ids');
  371. $cards = Card::with([
  372. 'member' => function ($query) {
  373. $query->select('member_id', 'member_mobile');
  374. },
  375. 'cert' => function ($query) {
  376. $query->select('join_cert_member_id', 'member_cert_name');
  377. },
  378. 'dept',
  379. 'user',
  380. 'main' => function ($query) {
  381. $query->select('card_main_id', 'card_main_name', 'card_main_amount', 'card_main_valid_mode');
  382. },
  383. 'issue' => function ($query) {
  384. $query->select('card_issue_id', 'card_issue_name');
  385. }
  386. ])->when(!empty($cardStatus), function ($query) use ($cardStatus) {
  387. $query->where('card_status', $cardStatus);
  388. })->when(!empty($isIssue), function ($query) use ($isIssue) {
  389. $query->where('is_issue', $isIssue);
  390. })->when(!empty($deptId), function ($query) use ($deptId) {
  391. $query->where('join_card_dept_id', $deptId);
  392. })->when(!empty($cardAddtimes), function ($query) use ($cardAddtimes) {
  393. $cardAddtimes[0] = strtotime($cardAddtimes[0]);
  394. $cardAddtimes[1] = strtotime($cardAddtimes[1]);
  395. $query->whereBetween('card_addtimes', $cardAddtimes);
  396. })->when(!empty($cardIds), function ($query) use ($cardIds) {
  397. $query->whereIn('card_id', $cardIds);
  398. })->when(!empty($cardMainName), function ($query) use ($cardMainName) {
  399. $cardMainIds = CardMain::where('card_main_name', 'like', '%' . $cardMainName . '%')->pluck('card_main_id')->toArray();
  400. $query->whereIn('join_card_main_id', $cardMainIds);
  401. })->when(!empty($cardIssueName), function ($query) use ($cardIssueName) {
  402. $cardIssueIds = CardIssue::where('card_issue_name', 'like', '%' . $cardIssueName . '%')->pluck('card_issue_id')->toArray();
  403. $query->whereIn('join_card_issue_id', $cardIssueIds);
  404. })->orderByDesc('card_addtimes')
  405. ->get()
  406. ->toArray();
  407. $data = [];
  408. foreach ($cards as $item) {
  409. $mobile = $item['member']['member_mobile'] ?? '';
  410. $certName = $item['cert']['member_cert_name'] ?? '';
  411. $item['member_name'] = MemberService::getMemberCertName($mobile, $certName, '');
  412. if (!empty($item['main']['card_main_valid_mode'])) {
  413. if ($item['main']['card_main_valid_mode'] == 'LONG') {
  414. $item['valid_datetime'] = '长期有效';
  415. } else if (!empty($item['card_deadline_datetime'])) {
  416. $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']));
  417. }
  418. }
  419. $cardExtendJson = [];
  420. if (!empty($item['card_extend_json'])) {
  421. $cardExtendJson = json_decode($item['card_extend_json'], true);
  422. }
  423. if (!empty($cardExtendJson['issue_datetime'])) {
  424. $item['issue_datetime'] = $cardExtendJson['issue_datetime'];
  425. }
  426. $data[] = [
  427. 'card_id' => $item['card_id'],
  428. 'main_name' => !empty($item['main']['card_main_name']) ? $item['main']['card_main_name'] : '--',
  429. 'issue_name' => !empty($item['issue']['card_issue_name']) ? $item['issue']['card_issue_name'] : '--',
  430. 'main_amount' => !empty($item['main']['card_main_amount']) ? $item['main']['card_main_amount'] : '--',
  431. 'dept_name' => !empty($item['dept']['dept_name']) ? $item['dept']['dept_name'] : '--',
  432. 'member_name' => $item['member_name'] ?? '--',
  433. 'valid_datetime' => $item['valid_datetime'] ?? '--',
  434. 'status' => CardService::$status[$item['card_status']],
  435. 'is_issue' => $item['is_issue'] == 'Y' ? '已发行' : '未发行',
  436. 'issue_datetime' => $item['issue_datetime'] ?? '--',
  437. 'card_addtimes' => $item['card_addtimes']
  438. ];
  439. }
  440. return json_success("success", $data);
  441. }
  442. }