CardService.php 15 KB

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