CardService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. class CardService
  14. {
  15. /**
  16. * Notes: 添加卡信息
  17. * User: ZhouBenXu
  18. * DateTime: 2024/6/27 下午2:58
  19. * @param $join_card_batch_id 卡批次id
  20. * @param $card_info 卡信息
  21. * @return \support\Response
  22. */
  23. public static function insert($join_card_batch_id, $card_info)
  24. {
  25. Db::beginTransaction();
  26. try {
  27. $model = new Card();
  28. // 生成包含100到200的数组
  29. $card_sort = range($card_info['card_batch_serial_begin'], $card_info['card_batch_serial_end']);
  30. $insertData = [];
  31. if ($card_info['card_batch_validtimes'] != 0) { // 卡批次有效期 0=永久 天
  32. $card_info['card_batch_validtimes'] = (strtotime(date('Y-m-d 00:00:00')) + $card_info['card_batch_validtimes'] * 86400) - 1;
  33. }
  34. foreach ($card_sort as $value) {
  35. $info['join_card_batch_id'] = $join_card_batch_id;
  36. $info['join_card_category_id'] = $card_info['join_card_batch_category_id'];
  37. $info['card_status'] = $model::CARD_STATUS[1];
  38. $info['card_category'] = $card_info['card_batch_category'];
  39. $info['card_name'] = $card_info['card_batch_name'];
  40. $info['card_prefix'] = $card_info['card_batch_prefix'];
  41. $info['card_suffix'] = $card_info['card_batch_suffix'];
  42. $info['card_pass'] = mt_rand(10000000, 99999999);// 卡密
  43. $info['card_amount'] = $card_info['card_batch_amount'];
  44. $info['card_added'] = $card_info['card_batch_added'];
  45. $info['card_validtimes'] = $card_info['card_batch_validtimes']; // 卡批次有效期 0=永久 天
  46. $info['card_json'] = "{}";
  47. $info['card_assign_json'] = "{}";
  48. $info['card_process_json'] = "{}";
  49. $info['card_sort'] = $value;
  50. $info['card_remark'] = "";
  51. $info['card_extend_json'] = "{}";
  52. $info['card_addtimes'] = time();
  53. $insertData[] = $info;
  54. }
  55. $model->insert($insertData);
  56. Db::commit();
  57. } catch (\PDOException $e) {
  58. Db::rollBack();
  59. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  60. return json_fail('数据写入失败~');
  61. } catch (BusinessException $e) {
  62. Db::rollBack();
  63. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  64. return json_fail($e->getMessage());
  65. } catch (\Exception $e) {
  66. Db::rollBack();
  67. dump($e->getTrace());
  68. return json_fail('数据写入失败~');
  69. }
  70. _syslog("添加储值卡", "卡批次名称【" . $card_info['card_batch_name'] . "】", $insertData);
  71. return json_success('success');
  72. }
  73. /**
  74. * Notes: 根据卡批次修改卡信息
  75. * User: ZhouBenXu
  76. * DateTime: 2024/6/27 下午5:03
  77. * @param $join_card_batch_id
  78. * @param $save
  79. * @return \support\Response
  80. */
  81. public static function saveCardByCardBatchId($join_card_batch_id, $save)
  82. {
  83. Db::beginTransaction();
  84. try {
  85. $model = new Card();
  86. $data['card_name'] = $save['card_batch_name'];
  87. $card_status = array_values($model::CARD_STATUS);
  88. if (in_array($save['card_batch_status'], $card_status)) {
  89. $data['card_status'] = $save['card_batch_status']; // 卡批次状态
  90. }
  91. if ($save['validtimes_status'] == 0) { // 卡批次有效期 0 永久天 1 增加 2减少
  92. $data['card_validtimes'] = 0;
  93. } elseif ($save['validtimes_status'] == 1) {
  94. $data['card_validtimes'] = Db::raw('card_validtimes+' . $save['card_batch_validtimes'] * 86400);
  95. } elseif ($save['validtimes_status'] == 2) {
  96. $data['card_validtimes'] = Db::raw('card_validtimes-' . $save['card_batch_validtimes'] * 86400);
  97. }
  98. $model->where('join_card_batch_id', $join_card_batch_id)->update($data);
  99. Db::commit();
  100. } catch (\PDOException $e) {
  101. Db::rollBack();
  102. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  103. return json_fail('数据写入失败~');
  104. } catch (BusinessException $e) {
  105. Db::rollBack();
  106. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  107. return json_fail($e->getMessage());
  108. } catch (\Exception $e) {
  109. Db::rollBack();
  110. dump($e->getTrace());
  111. return json_fail('数据写入失败~');
  112. }
  113. _syslog("修改储值卡", "卡批次名称【" . $save['card_batch_name'] . "】");
  114. return json_success('success');
  115. }
  116. }