CardService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <?php
  2. /**
  3. * CardService.php
  4. * User: ZhouBenXu
  5. * Date: 2024/6/27
  6. * Time: 上午9:46
  7. * Notes: 卡管理
  8. */
  9. namespace app\admin\service\card;
  10. use app\admin\service\sys_manage\CategoryService;
  11. use app\model\CardBatch;
  12. use support\Db;
  13. use support\exception\BusinessException;
  14. use app\model\Card;
  15. use support\Request;
  16. use support\Response;
  17. class CardService
  18. {
  19. /**
  20. * Notes: 添加卡信息
  21. * User: ZhouBenXu
  22. * DateTime: 2024/6/27 下午2:58
  23. * @param $join_card_batch_id 卡批次id
  24. * @param $card_info 卡信息
  25. * @return \support\Response
  26. */
  27. public static function insert($join_card_batch_id, $card_info)
  28. {
  29. Db::beginTransaction();
  30. try {
  31. $model = new Card();
  32. // 生成包含100到200的数组
  33. $card_sort = range($card_info['card_batch_serial_begin'], $card_info['card_batch_serial_end']);
  34. $insertData = [];
  35. if ($card_info['card_batch_validtimes'] != 0) { // 卡批次有效期 0=永久 天
  36. $card_info['card_batch_validtimes'] = (strtotime(date('Y-m-d 00:00:00')) + $card_info['card_batch_validtimes'] * 86400) - 1;
  37. }
  38. foreach ($card_sort as $value) {
  39. $info['join_card_batch_id'] = $join_card_batch_id;
  40. $info['join_card_category_id'] = $card_info['join_card_batch_category_id'];
  41. $info['card_status'] = $model::CARD_STATUS[1];
  42. $info['card_category'] = $card_info['card_batch_category'];
  43. $info['card_name'] = $card_info['card_batch_name'];
  44. $info['card_prefix'] = $card_info['card_batch_prefix'];
  45. $info['card_suffix'] = $card_info['card_batch_suffix'];
  46. $info['card_pass'] = mt_rand(10000000, 99999999);// 卡密
  47. $info['card_amount'] = $card_info['card_batch_amount'];
  48. $info['card_validtimes'] = $card_info['card_batch_validtimes']; // 卡批次有效期 0=永久 天
  49. $info['card_json'] = "{}";
  50. $info['card_assign_json'] = "{}";
  51. $info['card_process_json'] = "{}";
  52. $info['card_sort'] = $value;
  53. $info['card_remark'] = "";
  54. $info['card_extend_json'] = "{}";
  55. $info['card_addtimes'] = time();
  56. $insertData[] = $info;
  57. }
  58. $model->insert($insertData);
  59. Db::commit();
  60. } catch (\PDOException $e) {
  61. Db::rollBack();
  62. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  63. return json_fail('数据写入失败~');
  64. } catch (BusinessException $e) {
  65. Db::rollBack();
  66. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  67. return json_fail($e->getMessage());
  68. } catch (\Exception $e) {
  69. Db::rollBack();
  70. dump($e->getTrace());
  71. return json_fail('数据写入失败~');
  72. }
  73. _syslog("添加储值卡", "卡批次名称【" . $card_info['card_batch_name'] . "】", $insertData);
  74. return json_success('success');
  75. }
  76. /**
  77. * Notes: 根据卡批次修改卡信息
  78. * User: ZhouBenXu
  79. * DateTime: 2024/6/27 下午5:03
  80. * @param $join_card_batch_id
  81. * @param $save
  82. * @return string|\support\Response|true
  83. */
  84. public static function saveCardByCardBatchId($join_card_batch_id, $save)
  85. {
  86. Db::beginTransaction();
  87. try {
  88. $model = new Card();
  89. $data['card_name'] = $save['card_batch_name'];
  90. $card_status = array_values($model::CARD_STATUS);
  91. if (in_array($save['card_batch_status'], $card_status)) {
  92. $data['card_status'] = $save['card_batch_status']; // 卡批次状态
  93. }
  94. if ($save['validtimes_status'] == 0) { // 卡批次有效期 0 永久天 1 增加 2减少
  95. $data['card_validtimes'] = 0;
  96. } elseif ($save['validtimes_status'] == 1) {
  97. $data['card_validtimes'] = Db::raw('card_validtimes+' . $save['card_batch_validtimes'] * 86400);
  98. } elseif ($save['validtimes_status'] == 2) {
  99. $data['card_validtimes'] = Db::raw('card_validtimes-' . $save['card_batch_validtimes'] * 86400);;
  100. }
  101. $model->where('join_card_batch_id', $join_card_batch_id)->update($data);
  102. Db::commit();
  103. } catch (\PDOException $e) {
  104. Db::rollBack();
  105. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  106. return false;
  107. } catch (BusinessException $e) {
  108. Db::rollBack();
  109. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  110. return $e->getMessage();
  111. } catch (\Exception $e) {
  112. Db::rollBack();
  113. dump($e->getTrace());
  114. return false;
  115. }
  116. _syslog("修改储值卡", "卡批次名称【" . $save['card_batch_name'] . "】");
  117. return true;
  118. }
  119. /**
  120. * Notes: 储值卡列表
  121. * User: ZhouBenXu
  122. * DateTime: 2024/6/28 上午11:36
  123. * @param $params
  124. * @return \support\Response
  125. */
  126. public static function getList($params)
  127. {
  128. $cardModel = new Card();
  129. $page = $params['page'];
  130. $page_size = $params['pageSize'];
  131. $join_card_batch_id = $params['join_card_batch_id'] ?? "";
  132. $join_card_category_id = $params['join_card_category_id'] ?? "";
  133. $join_card_member_id = $params['join_card_member_id'] ?? "";
  134. $card_status = !empty($params['card_status']) ? $params['card_status'] : "";
  135. $card_category = $params['card_category'] ?? "";
  136. $card_name = $params['card_name'] ?? "";
  137. $rows = $cardModel::select('*')
  138. ->when(!empty($card_name) && $card_name != '', function ($query) use ($card_name) {
  139. $query->where('card_name', 'like', '%' . $card_name . '%');
  140. })->when(!empty($card_status), function ($query) use ($card_status) {
  141. $query->where('card_status', $card_status);
  142. })->when(!empty($join_card_batch_id), function ($query) use ($join_card_batch_id) {
  143. $query->where('join_card_batch_id', $join_card_batch_id);
  144. })->when(!empty($join_card_category_id), function ($query) use ($join_card_category_id) {
  145. $query->where('join_card_category_id', $join_card_category_id);
  146. })->when(!empty($join_card_member_id), function ($query) use ($join_card_member_id) {
  147. $query->where('join_card_member_id', $join_card_member_id);
  148. })->when(!empty($card_category), function ($query) use ($card_category) {
  149. $query->where('card_category', $card_category);
  150. })
  151. ->orderBy('card_id', 'DESC')
  152. ->forPage($page, $page_size)
  153. ->get()
  154. ->toArray();
  155. $total = $cardModel::select('card_id')
  156. ->when(!empty($card_name) && $card_name != '', function ($query) use ($card_name) {
  157. $query->where('card_name', 'like', '%' . $card_name . '%');
  158. })->when(!empty($card_status), function ($query) use ($card_status) {
  159. $query->where('card_status', $card_status);
  160. })->when(!empty($join_card_batch_id), function ($query) use ($join_card_batch_id) {
  161. $query->where('join_card_batch_id', $join_card_batch_id);
  162. })->when(!empty($join_card_category_id), function ($query) use ($join_card_category_id) {
  163. $query->where('join_card_category_id', $join_card_category_id);
  164. })->when(!empty($join_card_member_id), function ($query) use ($join_card_member_id) {
  165. $query->where('join_card_member_id', $join_card_member_id);
  166. })->when(!empty($card_category), function ($query) use ($card_category) {
  167. $query->where('card_category', $card_category);
  168. })
  169. ->count();
  170. if (!empty($rows)) {
  171. // 查询所有分类信息
  172. $category = array_column($rows, 'join_card_category_id');
  173. $categoryInfos = CategoryService::getCategoryInfos($category) ?? [];
  174. $categoryInfos && $categoryInfos = array_column($categoryInfos, null, 'category_id');
  175. // 查询会员信息
  176. foreach ($rows as $key => &$value) {
  177. $value['category_name'] = $categoryInfos[$value['join_card_category_id']]['category_name'] ?? "";
  178. $value['card_status_value'] = Card::CARD_STATUS[$value['card_status']];
  179. !empty($value['card_json']) && $value['card_json'] = json_decode($value['card_json'], true);
  180. !empty($value['card_assign_json']) && $value['card_assign_json'] = json_decode($value['card_assign_json'], true);
  181. !empty($value['card_process_json']) && $value['card_process_json'] = json_decode($value['card_process_json'], true);
  182. !empty($value['card_extend_json']) && $value['card_extend_json'] = json_decode($value['card_extend_json'], true);
  183. $value['card_no'] = $value['card_prefix'] . $value['card_sort'] . $value['card_suffix'];
  184. if ($value['card_validtimes'] == 0) {
  185. $value['card_validtimes'] = "永久";
  186. } else {
  187. $value['card_validtimes'] = date("Y-m-d H:i:s", $value['card_validtimes']);
  188. }
  189. }
  190. }
  191. return json_success('', compact('rows', 'page', 'page_size', 'total'));
  192. }
  193. /**
  194. * Notes: 储值卡详情
  195. * User: ZhouBenXu
  196. * DateTime: 2024/6/28 上午11:41
  197. * @param $card_id
  198. * @return \support\Response
  199. */
  200. public static function getInfo($card_id)
  201. {
  202. $cardInfo = Card::where('card_id', $card_id)->first() ?? [];
  203. if (!empty($cardInfo)) {
  204. $cardInfo['card_status_value'] = $cardInfo['card_status'];
  205. $cardInfo['card_status'] = array_flip(Card::CARD_STATUS)[$cardInfo['card_status']];
  206. !empty($cardInfo['card_json']) && $cardInfo['card_json'] = json_decode($cardInfo['card_json'], true);
  207. !empty($cardInfo['card_assign_json']) && $cardInfo['card_assign_json'] = json_decode($cardInfo['card_assign_json'], true);
  208. !empty($cardInfo['card_process_json']) && $cardInfo['card_process_json'] = json_decode($cardInfo['card_process_json'], true);
  209. !empty($cardInfo['card_extend_json']) && $cardInfo['card_extend_json'] = json_decode($cardInfo['card_extend_json'], true);
  210. }
  211. return json_success('', $cardInfo);
  212. }
  213. /**
  214. * Notes: 储值卡导出数据
  215. * User: ZhouBenXu
  216. * DateTime: 2024/6/28 下午2:44
  217. * @param Request $request
  218. * @return \support\Response
  219. */
  220. public static function exportCard(Request $request)
  221. {
  222. $params = $request->get();
  223. $join_card_batch_id = $params['join_card_batch_id'] ?? "";
  224. $join_card_category_id = $params['join_card_category_id'] ?? "";
  225. $join_card_member_id = $params['join_card_member_id'] ?? "";
  226. $card_status = !empty($params['card_status']) ? $params['card_status'] : "";
  227. $card_category = $params['card_category'] ?? "";
  228. $card_name = $params['card_name'] ?? "";
  229. $card_ids = $params['card_id'] ?? "";
  230. $cardModel = new Card();
  231. $list = $cardModel::select('*')
  232. ->when(!empty($card_name) && $card_name != '', function ($query) use ($card_name) {
  233. $query->where('card_name', 'like', '%' . $card_name . '%');
  234. })->when(!empty($card_status), function ($query) use ($card_status) {
  235. $query->where('card_status', $card_status);
  236. })->when(!empty($join_card_batch_id), function ($query) use ($join_card_batch_id) {
  237. $query->where('join_card_batch_id', $join_card_batch_id);
  238. })->when(!empty($join_card_category_id), function ($query) use ($join_card_category_id) {
  239. $query->where('join_card_category_id', $join_card_category_id);
  240. })->when(!empty($join_card_member_id), function ($query) use ($join_card_member_id) {
  241. $query->where('join_card_member_id', $join_card_member_id);
  242. })->when(!empty($card_category), function ($query) use ($card_category) {
  243. $query->where('card_category', $card_category);
  244. })
  245. ->whereIn('card_id', $card_ids)
  246. ->get()
  247. ->toArray();
  248. $exportData = [];
  249. // 增加签名
  250. if (!empty($list)) {
  251. foreach ($list as $key => $value) {
  252. $info['card_name'] = $value['card_name'];
  253. $info['card_pass'] = $value['card_pass'];
  254. $info['card_prefix'] = $value['card_prefix'];
  255. $info['card_suffix'] = $value['card_suffix'];
  256. $info['card_sort'] = $value['card_sort'];
  257. if ($value['card_validtimes'] == 0) {
  258. $info['card_validtimes'] = "永久";
  259. } else {
  260. $info['card_validtimes'] = date("Y-m-d H:i:s", $value['card_validtimes']);
  261. }
  262. // 生成签名
  263. $card_no = $value['card_prefix'] . $value['card_sort'] . $value['card_suffix'];
  264. $card_sign = self::generateSignature($info, $info['card_pass']);
  265. $card_extend_json['action'] = "bind-card";
  266. $card_extend_json['data'] = ["card_id" => $card_no, "card_sign" => $card_sign];
  267. $info['card_no'] = $card_no;
  268. $info['card_extend_json'] = json_encode($card_extend_json);
  269. $exportData[] = $info;
  270. }
  271. }
  272. return json_success('', $exportData);
  273. }
  274. /**
  275. * Notes: 生成签名
  276. * User: ZhouBenXu
  277. * DateTime: 2024/6/28 下午2:32
  278. * @param $data
  279. * @param $secret
  280. * @return string
  281. */
  282. public static function generateSignature($data, $secret)
  283. {
  284. // 将数组转换为字符串,并按照字母排序
  285. $dataString = http_build_query($data, '', '&', PHP_QUERY_RFC3986);
  286. // 使用HMAC-SHA256算法生成签名
  287. $signature = hash_hmac('sha256', $dataString, $secret);
  288. return $signature;
  289. }
  290. /**
  291. * Notes: 验证签名
  292. * User: ZhouBenXu
  293. * DateTime: 2024/6/28 下午2:33
  294. * @param $data
  295. * @param $providedSignature
  296. * @param $secret
  297. * @return bool
  298. */
  299. public static function verifySignature($data, $providedSignature, $secret)
  300. {
  301. // 生成数据的签名
  302. $generatedSignature = self::generateSignature($data, $secret);
  303. // 比较生成的签名与提供的签名是否一致
  304. return hash_equals($generatedSignature, $providedSignature);
  305. }
  306. /**
  307. * Notes: 储值卡删除
  308. * User: ZhouBenXu
  309. * DateTime: 2024/6/28 下午2:58
  310. * @param $card_ids
  311. * @return \support\Response
  312. */
  313. public static function delete($card_ids)
  314. {
  315. if (!is_array($card_ids)) {
  316. $card_ids = [$card_ids];
  317. }
  318. $card = Card::whereIn('card_id', $card_ids)->get()->toArray();
  319. if (!$card) {
  320. return json_fail("数据错误~");
  321. }
  322. // 是否有激活的卡片
  323. if (Card::whereIn('card_id', $card_ids)->where('card_status', 'ACTIVED')->exists()) {
  324. return json_fail('存在已激活卡片不可以删除');
  325. }
  326. Db::beginTransaction();
  327. try {
  328. Card::whereIn('card_id', $card_ids)->delete();
  329. Db::commit();
  330. // _syslog("删除储值卡", "删除的储值卡ID【" . implode(',', $card_ids) . '】');
  331. return json_success("储值卡删除成功");
  332. } catch (\Exception $e) {
  333. Db::rollBack();
  334. return json_fail("储值卡删除失败~");
  335. }
  336. }
  337. /**
  338. * Notes: 储值卡修改
  339. * User: ZhouBenXu
  340. * DateTime: 2024/6/28 下午3:17
  341. * @param $params
  342. * @return \support\Response
  343. */
  344. public static function save($params)
  345. {
  346. Db::beginTransaction();
  347. try {
  348. $cardBatchModel = new Card();
  349. $model = $cardBatchModel::find($params['card_id']);
  350. $model->card_name = $params['card_name'];
  351. $model->card_remark = $params['card_remark'] ?? "";
  352. if ($model->card_status == "ACTIVED") {
  353. return json_fail('已激活卡片不可以修改状态');
  354. }
  355. $params['join_card_member_id'] && $model->join_card_member_id = $params['join_card_member_id'] ?? "";
  356. $params['card_status'] && $model->card_status = $params['card_status']; // 储值卡状态
  357. if ($params['validtimes_status'] == 0) { // 卡批次有效期 0 永久天 1 增加 2减少
  358. $model->card_validtimes = 0;
  359. } elseif ($params['validtimes_status'] == 1) {
  360. $model->card_validtimes = bcadd($model->card_validtimes, $params['card_validtimes'] * 86400);
  361. } elseif ($params['validtimes_status'] == 2) {
  362. $card_validtimes = bcsub($model->card_validtimes, $params['card_validtimes'] * 86400);
  363. // 修改后的有效期不允许小于当前时间
  364. if ($card_validtimes <= time()) {
  365. return json_fail('储值卡有效期必须大于当前时间~');
  366. }
  367. $model->card_validtimes = $card_validtimes;
  368. }
  369. $model->save();
  370. Db::commit();
  371. } catch (\PDOException $e) {
  372. Db::rollBack();
  373. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  374. return json_fail('数据写入失败~');
  375. } catch (BusinessException $e) {
  376. Db::rollBack();
  377. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  378. return json_fail($e->getMessage());
  379. } catch (\Exception $e) {
  380. Db::rollBack();
  381. dump($e->getTrace());
  382. return json_fail('数据写入失败~');
  383. }
  384. _syslog("修改储值卡", "储值卡id【" . $params['card_id'] . "】");
  385. return json_success('success');
  386. }
  387. /**
  388. * Notes: 卡状态下拉列表
  389. * User: ZhouBenXu
  390. * DateTime: 2024/7/1 上午9:27
  391. * @return Response
  392. */
  393. public static function getStatusList()
  394. {
  395. $data = [];
  396. $statusList = Card::CARD_STATUS;
  397. foreach ($statusList as $key => $value) {
  398. $info = [];
  399. $info['status'] = $key;
  400. $info['status_value'] = $value;
  401. $data[] = $info;
  402. }
  403. return json_success('', $data);
  404. }
  405. /**
  406. * Notes: 储值卡绑定
  407. * User: ZhouBenXu
  408. * DateTime: 2024/7/2 下午6:08
  409. * @param $params
  410. * @return Response
  411. */
  412. public static function binding($params)
  413. {
  414. Db::beginTransaction();
  415. try {
  416. $card_ids = $params['card_id'];
  417. if (!is_array($card_ids)) {
  418. $card_ids = [$card_ids];
  419. }
  420. Card::whereIn('card_id', $card_ids)->update(['join_card_member_id'=>$params['join_card_member_id']]);
  421. Db::commit();
  422. } catch (\PDOException $e) {
  423. Db::rollBack();
  424. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  425. return json_fail('数据写入失败~');
  426. } catch (BusinessException $e) {
  427. Db::rollBack();
  428. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  429. return json_fail($e->getMessage());
  430. } catch (\Exception $e) {
  431. Db::rollBack();
  432. dump($e->getTrace());
  433. return json_fail('数据写入失败~');
  434. }
  435. _syslog("绑定储值卡", "储值卡ID【" . implode(',', $card_ids) . '】');
  436. return json_success('success');
  437. }
  438. }