1
0

2 Коммиты 49c547efaf ... 4912d21fa3

Автор SHA1 Сообщение Дата
  PV-12\Admin 4912d21fa3 Merge remote-tracking branch 'refs/remotes/origin/master' 5 месяцев назад
  PV-12\Admin 068ff554a3 卡批次列表接口 5 месяцев назад

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

@@ -65,9 +65,36 @@ class CardBatchController
      */
     public function delete(Request $request)
     {
+        // 验证器
+        $validate = new CardBatchValidate();
+        if (!$validate->scene('delete')->check($request->post())) {
+            return json_fail($validate->getError());
+        }
         $card_batch_ids = $request->post('card_batch_ids');
         return CardBatchService::delete($card_batch_ids);
     }
 
 
+    /**
+     * Notes: 批次列表
+     * User: ZhouBenXu
+     * DateTime: 2024/6/28 上午9:51
+     * @param Request $request
+     * @return Response
+     */
+    public function list(Request $request)
+    {
+        $params = $request->get();
+        $params['page'] = $params['page'] ?? 1;
+        $params['pageSize'] = $params['pageSize'] ?? 20;
+        $params['join_card_batch_user_id'] = JwtToken::getCurrentId();
+        // 验证器
+        $validate = new CardBatchValidate();
+        if (!$validate->scene('list')->check($params)) {
+            return json_fail($validate->getError());
+        }
+        return CardBatchService::getList($params);
+    }
+
+
 }

+ 108 - 9
app/admin/service/card/CardBatchService.php

@@ -89,12 +89,12 @@ class CardBatchService
             $params['card_batch_status'] = $model->card_batch_status = $cardBatchModel::CARD_BATCH_STATUS[$params['card_batch_status']]; // 批次状态
 
 
-            if($params['validtimes_status'] == 0) {
+            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=永久 天
+            } 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()) {
                 // 修改卡批次下面的卡信息
@@ -128,9 +128,6 @@ class CardBatchService
      */
     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];
         }
@@ -147,7 +144,7 @@ class CardBatchService
             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). '】');
+            _syslog("删除储值卡批次", "删除的卡批次ID【" . implode(',', $card_batch_ids) . '】');
             return json_success("卡批次删除成功");
         } catch (\Exception $e) {
             Db::rollBack();
@@ -155,4 +152,106 @@ class CardBatchService
         }
     }
 
+
+
+    /**
+     * Notes: 卡批次列表
+     * User: ZhouBenXu
+     * DateTime: 2024/6/28 上午9:43
+     * @param $params
+     * @return Response
+     */
+    public static function getList($params)
+    {
+        $cardBatchModel = new CardBatch();
+        $page = $params['page'];
+        $page_size = $params['pageSize'];
+        $card_batch_name = $params['card_batch_name'] ?? "";
+        $card_batch_status = !empty($params['card_batch_status']) ? $cardBatchModel::CARD_BATCH_STATUS[$params['card_batch_status']] : "";
+        $join_card_batch_category_id = $params['join_card_batch_category_id'] ?? "";
+        $rows = $cardBatchModel::select('*')
+            ->when(!empty($card_batch_name) && $card_batch_name != '', function ($query) use ($card_batch_name) {
+                $query->where('card_batch_name', 'like', '%' . $card_batch_name . '%');
+            })->when(!empty($card_batch_status), function ($query) use ($card_batch_status) {
+                $query->where('card_batch_status', $card_batch_status);
+            })->when(!empty($join_card_batch_category_id), function ($query) use ($join_card_batch_category_id) {
+                $query->where('join_card_batch_category_id', $join_card_batch_category_id);
+            })
+            ->orderBy('card_batch_id', 'DESC')
+            ->forPage($page, $page_size)
+            ->get()
+            ->toArray();
+
+        $total = $cardBatchModel::select('*')
+            ->when(!empty($card_batch_name) && $card_batch_name != '', function ($query) use ($card_batch_name) {
+                $query->where('card_batch_name', 'like', '%' . $card_batch_name . '%');
+            })->when(!empty($card_batch_status), function ($query) use ($card_batch_status) {
+                $query->where('card_batch_status', $card_batch_status);
+            })->when(!empty($join_card_batch_category_id), function ($query) use ($join_card_batch_category_id) {
+                $query->where('join_card_batch_category_id', $join_card_batch_category_id);
+            })->count();
+
+
+        if (!empty($rows)) {
+            // 查询求和数据信息
+            $card_batch_id_string = implode(',', array_column($rows, 'card_batch_id'));
+            $sql = "select join_card_batch_id,
+                    -- INIT
+                    IFNULL(SUM(CASE WHEN card_status = 'INIT' THEN counts ELSE 0 END),0) as status_INIT_counts,
+                    IFNULL(SUM(CASE WHEN card_status = 'INIT' THEN amounts ELSE 0 END),0) as status_INIT_amounts,
+                    IFNULL(SUM(CASE WHEN card_status = 'INIT' THEN addeds ELSE 0 END),0) as status_INIT_addeds,
+                    -- PRODUCTED
+                    IFNULL(SUM(CASE WHEN card_status = 'PRODUCTED' THEN counts ELSE 0 END),0) as status_PRODUCTED_counts,
+                    IFNULL(SUM(CASE WHEN card_status = 'PRODUCTED' THEN amounts ELSE 0 END),0) as status_PRODUCTED_amounts,
+                    IFNULL(SUM(CASE WHEN card_status = 'PRODUCTED' THEN addeds ELSE 0 END),0) as status_PRODUCTED_addeds,
+                    -- ENABLED
+                    IFNULL(SUM(CASE WHEN card_status = 'ENABLED' THEN counts ELSE 0 END),0) as status_ENABLED_counts,
+                    IFNULL(SUM(CASE WHEN card_status = 'ENABLED' THEN amounts ELSE 0 END),0) as status_ENABLED_amounts,
+                    IFNULL(SUM(CASE WHEN card_status = 'ENABLED' THEN addeds ELSE 0 END),0) as status_ENABLED_addeds,
+                    -- WAITING
+                    IFNULL(SUM(CASE WHEN card_status = 'WAITING' THEN counts ELSE 0 END),0) as status_WAITING_counts,
+                    IFNULL(SUM(CASE WHEN card_status = 'WAITING' THEN amounts ELSE 0 END),0) as status_WAITING_amounts,
+                    IFNULL(SUM(CASE WHEN card_status = 'WAITING' THEN addeds ELSE 0 END),0) as status_WAITING_addeds,
+                    -- PENDING
+                    IFNULL(SUM(CASE WHEN card_status = 'PENDING' THEN counts ELSE 0 END),0) as status_PENDING_counts,
+                    IFNULL(SUM(CASE WHEN card_status = 'PENDING' THEN amounts ELSE 0 END),0) as status_PENDING_amounts,
+                    IFNULL(SUM(CASE WHEN card_status = 'PENDING' THEN addeds ELSE 0 END),0) as status_PENDING_addeds,
+                    -- ACTIVED
+                    IFNULL(SUM(CASE WHEN card_status = 'ACTIVED' THEN counts ELSE 0 END),0) as status_ACTIVED_counts,
+                    IFNULL(SUM(CASE WHEN card_status = 'ACTIVED' THEN amounts ELSE 0 END),0) as status_ACTIVED_amounts,
+                    IFNULL(SUM(CASE WHEN card_status = 'ACTIVED' THEN addeds ELSE 0 END),0) as status_ACTIVED_addeds,
+                    -- DISABLED
+                    IFNULL(SUM(CASE WHEN card_status = 'DISABLED' THEN counts ELSE 0 END),0) as status_DISABLED_counts,
+                    IFNULL(SUM(CASE WHEN card_status = 'DISABLED' THEN amounts ELSE 0 END),0) as status_DISABLED_amounts,
+                    IFNULL(SUM(CASE WHEN card_status = 'DISABLED' THEN addeds ELSE 0 END),0) as status_DISABLED_addeds,
+                    -- EXPIRED
+                    IFNULL(SUM(CASE WHEN card_status = 'EXPIRED' THEN counts ELSE 0 END),0) as status_EXPIRED_counts,
+                    IFNULL(SUM(CASE WHEN card_status = 'EXPIRED' THEN amounts ELSE 0 END),0) as status_EXPIRED_amounts,
+                    IFNULL(SUM(CASE WHEN card_status = 'EXPIRED' THEN addeds ELSE 0 END),0) as status_EXPIRED_addeds,
+                    -- PAUSED
+                    IFNULL(SUM(CASE WHEN card_status = 'PAUSED' THEN counts ELSE 0 END),0) as status_PAUSED_counts,
+                    IFNULL(SUM(CASE WHEN card_status = 'PAUSED' THEN amounts ELSE 0 END),0) as status_PAUSED_amounts,
+                    IFNULL(SUM(CASE WHEN card_status = 'PAUSED' THEN addeds ELSE 0 END),0) as status_PAUSED_addeds
+                    from (select join_card_batch_id, card_status, COUNT(*) as counts, SUM(card_amount) as amounts, SUM(card_added) as addeds from app_card where join_card_batch_id in (" . $card_batch_id_string . ") group by join_card_batch_id, card_status) group_data
+                    group by join_card_batch_id";
+
+            $card_status_total = Db::select($sql);
+            $card_status_total = array_column($card_status_total, null, 'join_card_batch_id');
+            foreach ($rows as $key => &$value) {
+                $value['card_batch_status'] = array_flip($cardBatchModel::CARD_BATCH_STATUS)[$value['card_batch_status']];
+                !empty($value['card_batch_json']) && $value['card_batch_json'] = json_decode($value['card_batch_json'], true);
+                !empty($value['card_batch_extend_json']) && $value['card_batch_extend_json'] = json_decode($value['card_batch_extend_json'], true);
+                // 合并储值卡面值以及状态数量
+                if (!empty($card_status_total[$value['card_batch_id']])) {
+                    if (is_array($card_status_total[$value['card_batch_id']])) {
+                        $value = array_merge($value, $card_status_total[$value['card_batch_id']]);
+                    } else {
+                        $value = array_merge($value, json_decode(json_encode($card_status_total[$value['card_batch_id']]), true));
+                    }
+                }
+            }
+        }
+        return json_success('', compact('rows', 'page', 'page_size', 'total'));
+    }
+
 }

+ 6 - 2
app/admin/validate/card/CardBatchValidate.php

@@ -14,7 +14,7 @@ use support\Validate;
 class CardBatchValidate extends Validate
 {
     protected $rule = [
-        'card_batch_id' => 'require', // 批次创建用户 user_id
+        'card_batch_id' => 'require', // 批次id
         'join_card_batch_user_id' => 'require', // 批次创建用户 user_id
         'join_card_batch_category_id' => 'require', // 批次卡分类ID
         'card_batch_status' => 'require', // 批次状态 CREATE=已创建|PRODUCTED=制作完成|ENABLED=启用|DISABLED=禁用
@@ -27,6 +27,9 @@ class CardBatchValidate extends Validate
         'card_batch_added' => 'require', // 卡批次增值赠送
         'card_batch_validtimes' => 'require', // 卡批次有效期时间戳 0=永久 天
         'validtimes_status' => 'require', // 修改时有效期状态  0 永久 1增加 2减少
+        'page' => 'require', // 页码
+        'pageSize' => 'require', // 分页条数
+        'card_batch_ids' => 'require', // 批次ids
     ];
 
     protected $message = [];
@@ -34,7 +37,8 @@ class CardBatchValidate extends Validate
     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' => [],
+        'delete' => ['card_batch_ids'],
+        'list' => ['page','pageSize'],
         'changeStatus' => []
     ];
 

+ 1 - 0
route/admin.php

@@ -909,6 +909,7 @@ Route::group('/admin', 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']);
+            Route::get('/list', [\app\admin\controller\card\CardBatchController::class, 'list']);
         })->middleware([
             \app\middleware\AdminAuthCheck::class
         ]);