Browse Source

Merge branch 'master' of http://39.98.194.76:3000/txct/wanyue_app

gorden 5 months ago
parent
commit
fa49406aa2

+ 73 - 0
app/admin/controller/card/CardBatchController.php

@@ -0,0 +1,73 @@
+<?php
+/**
+ * CardBatchController.php
+ * User: ZhouBenXu
+ * Date: 2024/6/27
+ * Time: 上午9:36
+ * Notes: 卡批次管理
+ */
+
+namespace app\admin\controller\card;
+
+
+use support\Request;
+use support\Response;
+use app\admin\validate\card\CardBatchValidate;
+use app\admin\service\card\CardBatchService;
+use Tinywan\Jwt\JwtToken;
+
+class CardBatchController
+{
+
+    /**
+     * Notes: 添加卡批次
+     * User: ZhouBenXu
+     * DateTime: 2024/6/27 上午9:44
+     */
+    public function insert(Request $request)
+    {
+        $params = $request->post();
+        $params['join_card_batch_user_id'] = JwtToken::getCurrentId();
+        // 验证器
+        $validate = new CardBatchValidate();
+        if (!$validate->scene('add')->check($params)) {
+            return json_fail($validate->getError());
+        }
+        return CardBatchService::insert($params);
+    }
+
+    /**
+     * Notes: 修改卡批次 仅修改名称 状态 有效期
+     * User: ZhouBenXu
+     * DateTime: 2024/6/27 上午9:44
+     */
+    public function save(Request $request)
+    {
+        $params = $request->post();
+        $params['join_card_batch_user_id'] = JwtToken::getCurrentId();
+        // 验证器
+        $validate = new CardBatchValidate();
+        if (!$validate->scene('save')->check($params)) {
+            return json_fail($validate->getError());
+        }
+
+        // 校验有效期 0 永久 1增加 2 减少
+        return CardBatchService::save($params);
+    }
+
+
+    /**
+     * Notes: 删除 支持批量
+     * User: ZhouBenXu
+     * DateTime: 2024/6/27 下午5:41
+     * @param Request $request
+     * @return Response
+     */
+    public function delete(Request $request)
+    {
+        $card_batch_ids = $request->post('card_batch_ids');
+        return CardBatchService::delete($card_batch_ids);
+    }
+
+
+}

+ 18 - 0
app/admin/controller/card/CardController.php

@@ -0,0 +1,18 @@
+<?php
+/**
+ * CardController.php
+ * User: ZhouBenxu
+ * Date: 2024/6/27
+ * Time: 上午9:35
+ * Notes: 储值卡管理
+ */
+
+namespace app\admin\controller\card;
+
+class CardController
+{
+
+
+
+
+}

+ 158 - 0
app/admin/service/card/CardBatchService.php

@@ -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("卡批次删除失败~");
+        }
+    }
+
+}

+ 124 - 0
app/admin/service/card/CardService.php

@@ -0,0 +1,124 @@
+<?php
+/**
+ * CardService.php
+ * User: ZhouBenXu
+ * Date: 2024/6/27
+ * Time: 上午9:46
+ * Notes: 卡管理
+ */
+
+namespace app\admin\service\card;
+
+use support\Db;
+use support\exception\BusinessException;
+use app\model\Card;
+
+class CardService
+{
+
+    /**
+     * Notes: 添加卡信息
+     * User: ZhouBenXu
+     * DateTime: 2024/6/27 下午2:58
+     * @param $join_card_batch_id 卡批次id
+     * @param $card_info 卡信息
+     * @return \support\Response
+     */
+    public static function insert($join_card_batch_id, $card_info)
+    {
+        Db::beginTransaction();
+        try {
+            $model = new Card();
+            // 生成包含100到200的数组
+            $card_sort = range($card_info['card_batch_serial_begin'], $card_info['card_batch_serial_end']);
+            $insertData = [];
+            if ($card_info['card_batch_validtimes'] != 0) { // 卡批次有效期 0=永久 天
+                $card_info['card_batch_validtimes'] = (strtotime(date('Y-m-d 00:00:00')) + $card_info['card_batch_validtimes'] * 86400) - 1;
+            }
+            foreach ($card_sort as $value) {
+                $info['join_card_batch_id'] = $join_card_batch_id;
+                $info['join_card_category_id'] = $card_info['join_card_batch_category_id'];
+                $info['card_status'] = $model::CARD_STATUS[1];
+                $info['card_category'] = $card_info['card_batch_category'];
+                $info['card_name'] = $card_info['card_batch_name'];
+                $info['card_prefix'] = $card_info['card_batch_prefix'];
+                $info['card_suffix'] = $card_info['card_batch_suffix'];
+                $info['card_pass'] = mt_rand(10000000, 99999999);// 卡密
+                $info['card_amount'] = $card_info['card_batch_amount'];
+                $info['card_added'] = $card_info['card_batch_added'];
+                $info['card_validtimes'] = $card_info['card_batch_validtimes']; // 卡批次有效期 0=永久 天
+                $info['card_json'] = "{}";
+                $info['card_assign_json'] = "{}";
+                $info['card_process_json'] = "{}";
+                $info['card_sort'] = $value;
+                $info['card_remark'] = "";
+                $info['card_extend_json'] = "{}";
+                $info['card_addtimes'] = time();
+                $insertData[] = $info;
+            }
+            $model->insert($insertData);
+            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("添加储值卡", "卡批次名称【" . $card_info['card_batch_name'] . "】", $insertData);
+        return json_success('success');
+    }
+
+    /**
+     * Notes: 根据卡批次修改卡信息
+     * User: ZhouBenXu
+     * DateTime: 2024/6/27 下午5:03
+     * @param $join_card_batch_id
+     * @param $save
+     * @return \support\Response
+     */
+    public static function saveCardByCardBatchId($join_card_batch_id, $save)
+    {
+
+        Db::beginTransaction();
+        try {
+            $model = new Card();
+            $data['card_name'] = $save['card_batch_name'];
+
+            $card_status = array_values($model::CARD_STATUS);
+            if (in_array($save['card_batch_status'], $card_status)) {
+                $data['card_status'] = $save['card_batch_status']; // 卡批次状态
+            }
+            if ($save['validtimes_status'] == 0) { // 卡批次有效期 0 永久天  1 增加 2减少
+                $data['card_validtimes'] = 0;
+            } elseif ($save['validtimes_status'] == 1) {
+                $data['card_validtimes'] = Db::raw('card_validtimes+' . $save['card_batch_validtimes'] * 86400);
+            } elseif ($save['validtimes_status'] == 2) {
+                $data['card_validtimes'] = Db::raw('card_validtimes-' . $save['card_batch_validtimes'] * 86400);
+            }
+            $model->where('join_card_batch_id', $join_card_batch_id)->update($data);
+            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("修改储值卡", "卡批次名称【" . $save['card_batch_name'] . "】");
+        return json_success('success');
+    }
+
+}

+ 42 - 0
app/admin/validate/card/CardBatchValidate.php

@@ -0,0 +1,42 @@
+<?php
+/**
+ * CardBatchValidate.php
+ * User: ZhouBenXu
+ * Date: 2024/6/27
+ * Time: 上午9:54
+ * Notes:
+ */
+
+namespace app\admin\validate\card;
+
+use support\Validate;
+
+class CardBatchValidate extends Validate
+{
+    protected $rule = [
+        'card_batch_id' => 'require', // 批次创建用户 user_id
+        'join_card_batch_user_id' => 'require', // 批次创建用户 user_id
+        'join_card_batch_category_id' => 'require', // 批次卡分类ID
+        'card_batch_status' => 'require', // 批次状态 CREATE=已创建|PRODUCTED=制作完成|ENABLED=启用|DISABLED=禁用
+        'card_batch_name' => 'require', // 卡批次名称
+        'card_batch_prefix' => 'require', // 卡批次前缀
+        'card_batch_suffix' => 'require', // 卡批次后缀
+        'card_batch_serial_begin' => 'require', // 卡批次序号起始
+        'card_batch_serial_end' => 'require', // 卡批次序号结束
+        'card_batch_amount' => 'require', // 卡批次面额
+        'card_batch_added' => 'require', // 卡批次增值赠送
+        'card_batch_validtimes' => 'require', // 卡批次有效期时间戳 0=永久 天
+        'validtimes_status' => 'require', // 修改时有效期状态  0 永久 1增加 2减少
+    ];
+
+    protected $message = [];
+
+    protected $scene = [
+        'add' => ['join_card_batch_user_id', 'join_card_batch_category_id', 'card_batch_status', 'card_batch_name', 'card_batch_prefix', 'card_batch_suffix', 'card_batch_serial_begin', 'card_batch_serial_end', 'card_batch_amount', 'card_batch_added', 'card_batch_validtimes'],
+        'save' => ['join_card_batch_user_id','card_batch_id','validtimes_status','card_batch_name'],
+        'info' => [],
+        'changeStatus' => []
+    ];
+
+
+}

+ 28 - 0
app/model/Card.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace app\model;
+
+use DateTimeInterface;
+use support\Model;
+
+class Card extends Model
+{
+    public $table = 'card';
+
+    public $primaryKey = 'card_id';
+
+    protected $dateFormat = 'U';
+
+    const CREATED_AT = 'card_addtimes';
+
+    const UPDATED_AT = null;
+
+    //卡状态 INTI=初始化|PRODUCTED=制作完成|ENABLED=已启用|WAITING=已分配|PENDING=待激活|ACTIVED=已激活|DISABLED=已禁用|EXPIRED=已过期|PAUSED=暂停使用
+    public const CARD_STATUS = [1 => 'INIT', 2 => 'PRODUCTED', 3 => 'ENABLED', 4 => 'WAITING', 5 => 'PENDING', 6 => 'ACTIVED', 7 => 'DISABLED', 8 => 'EXPIRED', 9 => 'PAUSED'];
+
+
+    public function serializeDate(DateTimeInterface $date)
+    {
+        return $date->format('Y-m-d H:i:s');
+    }
+}

+ 28 - 0
app/model/CardBatch.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace app\model;
+
+use DateTimeInterface;
+use support\Model;
+
+class CardBatch extends Model
+{
+    public $table = 'card_batch';
+
+    public $primaryKey = 'card_batch_id';
+
+    protected $dateFormat = 'U';
+
+    const CREATED_AT = 'card_batch_addtimes';
+
+    const UPDATED_AT = null;
+
+
+    // 批次状态 CREATE=已创建|PRODUCTED=制作完成|ENABLED=启用|DISABLED=禁用
+    public const CARD_BATCH_STATUS = [1 => 'CREATE', 2 => 'PRODUCTED', 3 => 'ENABLED', 4 => 'DISABLED'];
+
+    public function serializeDate(DateTimeInterface $date)
+    {
+        return $date->format('Y-m-d H:i:s');
+    }
+}

+ 22 - 0
route/admin.php

@@ -891,6 +891,28 @@ Route::group('/admin', function () {
     Route::group('/dashboard', function () {
         Route::get('/', [\app\admin\controller\dashboard\DashboardController::class, 'index']);
     });
+
+    /* 储值卡管理 */
+    Route::group('/card', function () {
+        /* 卡管理 */
+        Route::group('/card', function () {
+            Route::get('/list', [\app\admin\controller\member\FamilyController::class, 'list']);
+            Route::get('/list/{id:\w+}', [\app\admin\controller\member\FamilyController::class, 'selectList']);
+            Route::get('/info/{id:\w+}', [\app\admin\controller\member\FamilyController::class, 'selectInfo']);
+        })->middleware([
+            \app\middleware\AdminAuthCheck::class
+        ]);
+
+        /* 卡批次管理 */
+        Route::group('/cardBatch', function () {
+            Route::post('/add', [\app\admin\controller\card\CardBatchController::class, 'insert']);
+            Route::post('/save', [\app\admin\controller\card\CardBatchController::class, 'save']);
+            Route::delete('/delete', [\app\admin\controller\card\CardBatchController::class, 'delete']);
+        })->middleware([
+            \app\middleware\AdminAuthCheck::class
+        ]);
+    });
+
 });