|
@@ -0,0 +1,158 @@
|
|
|
|
+<?php
|
|
|
|
+/**
|
|
|
|
+ * CardBatchService.php
|
|
|
|
+ * User: ZhouBenXu
|
|
|
|
+ * Date: 2024/6/27
|
|
|
|
+ * Time: 上午9:46
|
|
|
|
+ * Notes:
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+namespace app\admin\service\card;
|
|
|
|
+
|
|
|
|
+use support\Db;
|
|
|
|
+use support\exception\BusinessException;
|
|
|
|
+use support\Response;
|
|
|
|
+use app\model\CardBatch;
|
|
|
|
+use app\model\Card;
|
|
|
|
+use app\admin\service\card\CardService;
|
|
|
|
+
|
|
|
|
+class CardBatchService
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Notes: 添加卡批次
|
|
|
|
+ * User: ZhouBenXu
|
|
|
|
+ * DateTime: 2024/6/27 上午10:04
|
|
|
|
+ * @param $params
|
|
|
|
+ */
|
|
|
|
+ public static function insert($params)
|
|
|
|
+ {
|
|
|
|
+ Db::beginTransaction();
|
|
|
|
+ try {
|
|
|
|
+ // 添加卡批次
|
|
|
|
+ $model = new CardBatch();
|
|
|
|
+ $model->join_card_batch_user_id = $params['join_card_batch_user_id']; // 批次创建用户 user_id
|
|
|
|
+ $model->join_card_batch_category_id = $params['join_card_batch_category_id']; // 批次卡分类ID
|
|
|
|
+ $model->card_batch_status = $model::CARD_BATCH_STATUS[$params['card_batch_status']]; // 批次状态
|
|
|
|
+ $model->card_batch_category = $params['card_batch_category'] ?? "NORMAL"; // 批次类型 NORMAL
|
|
|
|
+ $model->card_batch_name = $params['card_batch_name']; // 卡批次名称
|
|
|
|
+ $model->card_batch_prefix = $params['card_batch_prefix']; // 卡批次前缀
|
|
|
|
+ $model->card_batch_suffix = $params['card_batch_suffix']; // 卡批次后缀
|
|
|
|
+ $model->card_batch_serial_begin = $params['card_batch_serial_begin']; // 卡批次序号起始
|
|
|
|
+ $model->card_batch_serial_end = $params['card_batch_serial_end']; // 卡批次序号结束
|
|
|
|
+ $model->card_batch_amount = $params['card_batch_amount']; // 卡批次面额
|
|
|
|
+ $model->card_batch_added = $params['card_batch_added']; // 卡批次增值赠送
|
|
|
|
+ $model->card_batch_validtimes = $params['card_batch_validtimes']; // 卡批次有效期 0=永久 天
|
|
|
|
+ $model->card_batch_json = $params['card_batch_json'] ?? "{}"; // 卡JSON {}
|
|
|
|
+ $model->card_batch_remark = $params['card_batch_remark'] ?? ""; // 备注
|
|
|
|
+ $model->card_batch_extend_json = $params['card_batch_extend_json'] ?? "{}"; // 扩展 {}
|
|
|
|
+
|
|
|
|
+ if ($model->save()) {
|
|
|
|
+ $join_card_batch_id = $model->card_batch_id;
|
|
|
|
+ // 添加卡信息
|
|
|
|
+ CardService::insert($join_card_batch_id, $params);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Db::commit();
|
|
|
|
+ } catch (\PDOException $e) {
|
|
|
|
+ Db::rollBack();
|
|
|
|
+ dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
|
|
|
|
+ return json_fail('数据写入失败~');
|
|
|
|
+ } catch (BusinessException $e) {
|
|
|
|
+ Db::rollBack();
|
|
|
|
+ dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
|
|
|
|
+ return json_fail($e->getMessage());
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
+ Db::rollBack();
|
|
|
|
+ dump($e->getTrace());
|
|
|
|
+ return json_fail('数据写入失败~');
|
|
|
|
+ }
|
|
|
|
+ _syslog("添加储值卡批次", "卡批次名称【" . $params['card_batch_name'] . "】");
|
|
|
|
+ return json_success('success');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Notes: 修改卡批次
|
|
|
|
+ * User: ZhouBenXu
|
|
|
|
+ * DateTime: 2024/6/27 下午4:32
|
|
|
|
+ * @param $params
|
|
|
|
+ * @return Response
|
|
|
|
+ */
|
|
|
|
+ public static function save($params)
|
|
|
|
+ {
|
|
|
|
+ Db::beginTransaction();
|
|
|
|
+ try {
|
|
|
|
+ $cardBatchModel = new CardBatch();
|
|
|
|
+ $model = $cardBatchModel::find($params['card_batch_id']);
|
|
|
|
+ $model->card_batch_name = $params['card_batch_name'];
|
|
|
|
+ $params['card_batch_status'] = $model->card_batch_status = $cardBatchModel::CARD_BATCH_STATUS[$params['card_batch_status']]; // 批次状态
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if($params['validtimes_status'] == 0) {
|
|
|
|
+ $model->card_batch_validtimes = 0; // 卡批次有效期 0=永久 天
|
|
|
|
+ } elseif($params['validtimes_status'] == 1) {
|
|
|
|
+ $model->card_batch_validtimes = bcadd($model->card_batch_validtimes , $params['card_batch_validtimes']); // 卡批次有效期 0=永久 天
|
|
|
|
+ } elseif($params['validtimes_status'] == 2) {
|
|
|
|
+ $model->card_batch_validtimes = bcsub($model->card_batch_validtimes , $params['card_batch_validtimes']); // 卡批次有效期 0=永久 天
|
|
|
|
+ }
|
|
|
|
+ if ($model->save()) {
|
|
|
|
+ // 修改卡批次下面的卡信息
|
|
|
|
+ CardService::saveCardByCardBatchId($params['card_batch_id'], $params);
|
|
|
|
+ }
|
|
|
|
+ Db::commit();
|
|
|
|
+ } catch (\PDOException $e) {
|
|
|
|
+ Db::rollBack();
|
|
|
|
+ dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
|
|
|
|
+ return json_fail('数据写入失败~');
|
|
|
|
+ } catch (BusinessException $e) {
|
|
|
|
+ Db::rollBack();
|
|
|
|
+ dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
|
|
|
|
+ return json_fail($e->getMessage());
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
+ Db::rollBack();
|
|
|
|
+ dump($e->getTrace());
|
|
|
|
+ return json_fail('数据写入失败~');
|
|
|
|
+ }
|
|
|
|
+ _syslog("修改储值卡批次", "卡批次名称【" . $params['card_batch_name'] . "】");
|
|
|
|
+ return json_success('success');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Notes: 删除卡批次
|
|
|
|
+ * User: ZhouBenXu
|
|
|
|
+ * DateTime: 2024/6/27 下午5:53
|
|
|
|
+ * @param $card_batch_ids
|
|
|
|
+ * @return Response
|
|
|
|
+ */
|
|
|
|
+ public static function delete($card_batch_ids)
|
|
|
|
+ {
|
|
|
|
+ if (!$card_batch_ids) {
|
|
|
|
+ return json_fail("数据错误~");
|
|
|
|
+ }
|
|
|
|
+ if (!is_array($card_batch_ids)) {
|
|
|
|
+ $card_batch_ids = [$card_batch_ids];
|
|
|
|
+ }
|
|
|
|
+ $cardBatch = CardBatch::whereIn('card_batch_id', $card_batch_ids)->get()->toArray();
|
|
|
|
+ if (!$cardBatch) {
|
|
|
|
+ return json_fail("数据错误~");
|
|
|
|
+ }
|
|
|
|
+ // 是否有激活的卡片
|
|
|
|
+ if (Card::whereIn('join_card_batch_id', $card_batch_ids)->where('card_status', 'ACTIVED')->exists()) {
|
|
|
|
+ return json_fail('存在已激活卡片不可以删除');
|
|
|
|
+ }
|
|
|
|
+ Db::beginTransaction();
|
|
|
|
+ try {
|
|
|
|
+ CardBatch::whereIn('card_batch_id', $card_batch_ids)->delete();
|
|
|
|
+ Card::whereIn('join_card_batch_id', $card_batch_ids)->delete();
|
|
|
|
+ Db::commit();
|
|
|
|
+ _syslog("删除储值卡批次", "删除的卡批次ID【". implode(',', $card_batch_ids). '】');
|
|
|
|
+ return json_success("卡批次删除成功");
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
+ Db::rollBack();
|
|
|
|
+ return json_fail("卡批次删除失败~");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|