package com.bohuikeji.delivery.backstage.controller.api.courier;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TDrivingLicence;
import com.bohuikeji.delivery.backstage.entity.TLicense;
import com.bohuikeji.delivery.backstage.service.TLicenseService;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Api(tags = {"驾照管理接口"})
@RestController
@RequestMapping("/api/license")
public class TLicenseApi extends ApiController {
    @Autowired
    TLicenseService tLicenseService;
    @PostMapping("/save")
    @ApiOperation("驾驶证录入")
    public APIEntity save(@RequestBody TLicense tLicense){

        TLicense res = tLicenseService.getOne(new QueryWrapper<TLicense>().eq("license_Type",tLicense.getLicenseType()));
        if (res != null){
            return APIEntity.fail("改类型驾照已存在!");
        }
        tLicenseService.save(tLicense);
        return APIEntity.success("操作成功");
    }

    @PostMapping("/update")
    @ApiOperation("驾驶证修改")
    public APIEntity update(@RequestBody TLicense tLicense){
        TLicense res = tLicenseService.getOne(new QueryWrapper<TLicense>().eq("license_Type",tLicense.getLicenseType()));
        if (res == null){
            return APIEntity.fail("该类型驾照不存在");
        }
        assert false;
        tLicense.setId(res.getId());
        tLicenseService.updateById(tLicense);
        return APIEntity.success("操作成功");
    }
    @GetMapping("/verification")
    @ApiOperation("根据驾照证验证车辆")
    @ApiImplicitParams({
            @ApiImplicitParam(name="type",value="驾照类型（如C1...）", paramType = "query",required=true),
            @ApiImplicitParam(name="vehicle",value="车辆信心（如皮卡车）", paramType = "query",required=true)
    })
    public APIEntity update(String type,String vehicle){
        TLicense tLicense = tLicenseService.getOne(new QueryWrapper<TLicense>().eq("license_type",type));
        String driving = tLicense.getDrivingList();
        // 将查找的车辆信息已逗号拆分成数组
        String[] list = driving.split(",");
        for (String i : list){
            if (i.equals(vehicle)){
                return APIEntity.success("您的车辆符合你驾照信息");
            }
        }
        return APIEntity.fail("你所选的车辆不符合驾照要求");
    }
}
