<?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());
        }
        if (!$validate->checkCardSort($params)) {
            return json_fail("卡号重请修改后添加");
        }
        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();
        $params['validtimes_status'] = $params['validtimes_status'] ?? 3;
        // 验证器
        $validate = new CardBatchValidate();
        if (!$validate->scene('save')->check($params)) {
            return json_fail($validate->getError());
        }
        // 校验有效期 0 永久 1增加 2 减少 3不变
        return CardBatchService::save($params);
    }


    /**
     * Notes: 删除 支持批量
     * User: ZhouBenXu
     * DateTime: 2024/6/27 下午5:41
     * @param Request $request
     * @return Response
     */
    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);
    }


    /**
     * Notes: 卡批次详情
     * User: ZhouBenXu
     * DateTime: 2024/6/28 上午10:51
     * @param Request $request
     * @return Response
     */
    public function info(Request $request)
    {
        $params = $request->get();
        // 验证器
        $validate = new CardBatchValidate();
        if (!$validate->scene('info')->check($params)) {
            return json_fail($validate->getError());
        }
        return CardBatchService::getInfo($params['card_batch_id']);
    }

    /**
     * Notes: 卡状态下拉列表
     * User: ZhouBenXu
     * DateTime: 2024/7/1 上午9:27
     * @return Response
     */
    public function getStatusList()
    {
        return CardBatchService::getStatusList();
    }


}