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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TCourier;
import com.bohuikeji.delivery.backstage.entity.TVehicleInformation;
import com.bohuikeji.delivery.backstage.entity.socket.AdminNotice;
import com.bohuikeji.delivery.backstage.service.ISocketService;
import com.bohuikeji.delivery.backstage.service.ITCourierCardService;
import com.bohuikeji.delivery.backstage.service.ITCourierService;
import com.bohuikeji.delivery.backstage.service.ITVehicleInformationService;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.sys.SysConfig;
import com.bohuikeji.frame.module.admin.service.ISysConfigService;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车辆相关接口
 * @author 10552
 */
@Api(tags = {"车辆相关接口"})
@RestController
@RequestMapping("/api/vehicleInfo")
@RequiredArgsConstructor
@Slf4j
public class VehicleInfoApi extends ApiController {

    final ITVehicleInformationService vehicleInformationService;
    final ISysConfigService configService;
    final ISocketService socketService;
    final ITCourierService courierService;

    /**
     * 完善车辆信息
     * @return
     */
    @ApiOperation("获取车辆信息")
    @GetMapping("/get/id")
    public APIEntity perfect(Integer vehicleId){
        return APIEntity.success(vehicleInformationService.getById(vehicleId));
    }

    /**
     * 完善车辆信息
     * @return
     */
    @ApiOperation("完善车辆信息")
    @PostMapping("/perfect/vehicle")
    public APIEntity perfect(@RequestBody TVehicleInformation vehicleInformation){
        log.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@1111:{}",vehicleInformation);
        TVehicleInformation tVehicleInformation = vehicleInformationService.getOne(new QueryWrapper<TVehicleInformation>().eq("courier_id",vehicleInformation.getCourierId()).eq("vehicle_name",vehicleInformation.getVehicleName()));
        if(vehicleInformation.getVehicleId() == null && tVehicleInformation !=null){
            if(vehicleInformation.getVehicleName().equals(tVehicleInformation.getVehicleName())){
                return APIEntity.fail("您已认证过该类型车辆，请不要重复申请！");
            }
        }
        if(vehicleInformation.getVehicleId() != null){
            if(!vehicleInformation.getVehicleId().equals(tVehicleInformation.getVehicleId()) && vehicleInformation.getVehicleName().equals(tVehicleInformation.getVehicleName())){
                return APIEntity.fail("您已认证过该类型车辆，请不要重复申请！");
            }
        }
        if(vehicleInformationService.list(new QueryWrapper<TVehicleInformation>().eq("courier_id",vehicleInformation.getCourierId()).eq("vehicle_name",vehicleInformation.getVehicleName())).size()>=1){
            if(vehicleInformation.getVehicleId() == null){
                return APIEntity.fail("您已认证过该类型车辆，请不要重复申请！");
            }
        }
        vehicleInformation.setAuthStatus("待认证");
        if(configService.getOneByKey("car_type_ddc").getConfigName().equals(vehicleInformation.getVehicleName())){
            vehicleInformation.setVehicleNumber("无车牌");
        }
        if(StringUtils.isBlank(vehicleInformation.getVehicleNumber())){
            return APIEntity.fail("请填写车牌号！");
        }
        List<SysConfig> carTypes=configService.getListByGroup("car_type");
        //根据车辆名称查找车辆类型ID
        for(SysConfig carType:carTypes){
            if(vehicleInformation.getVehicleName().equals(carType.getConfigName())){
                vehicleInformation.setVehicleTypeId(carType.getConfigId());
            }
        }
        if(vehicleInformation.getVehicleTypeId()==null){
            return APIEntity.fail("车辆类型选择错误，请刷新页面重新选择！");
        }
        TCourier courier=courierService.getById(vehicleInformation.getCourierId());
        //socketService.sendAdminMsg(new AdminNotice(StringUtils.formatStr("配送员{0}正在申请车辆认证，请及时处理",courier.getCourierName())).setFranId(courier.getFranId()));
        return APIEntity.success(vehicleInformationService.saveOrUpdate(vehicleInformation));
    }


    /**
     * 获取派送员车辆
     * @return
     */
    @ApiOperation("获取派送员车辆")
    @GetMapping("/getVehicle")
    @ApiImplicitParams({
            @ApiImplicitParam(name="courierId",value="派送员登陆id",dataType="Integer", paramType = "query")})
    public APIEntity getVehicle(Integer courierId){
        List<TVehicleInformation> tVehicleInformation = vehicleInformationService.list(new QueryWrapper<TVehicleInformation>().eq("courier_id",courierId));
        return APIEntity.success(tVehicleInformation);
    }

    /**
     * 获取所有车辆
     * @return
     */
    @ApiOperation("获取所有车辆")
    @GetMapping("/getVehicleAll")
    public APIEntity getVehicleAll(){
        List<TVehicleInformation> tVehicleInformation = vehicleInformationService.list();
        return APIEntity.success(tVehicleInformation);
    }

}
