package com.muyu.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.muyu.common.core.domain.Result;
import com.muyu.domain.CarInformation;
import com.muyu.domain.req.CarInformationAddReq;
import com.muyu.domain.req.CarInformationListReq;
import com.muyu.domain.req.CarInformationUpdReq;
import com.muyu.domain.resp.CarInformationResp;
import com.muyu.enterprise.cache.car.VehicleCacheCarInformationAddService;
import com.muyu.enterprise.cache.car.VehicleCacheCarInformationService;
import com.muyu.enterprise.cache.car.VehicleCacheCarInformationUpdService;
import com.muyu.server.service.CarInformationService;
import com.muyu.server.util.ObtainRootLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 车辆信息管理控制层
 * @author 17353
 */
@Log4j2
@RestController
@RequestMapping("/carinformation")
@Tag(name = "车辆信息管理控制层" ,description = "进行车辆管理基础业务操作")
public class CarInformationController {
    @Resource
    private CarInformationService carInformationService;
    @Autowired
    private VehicleCacheCarInformationService vehicleCacheCarInformationService;
    @Autowired
    private VehicleCacheCarInformationAddService vehicleCacheCarInformationAddService;
    @Autowired
    private VehicleCacheCarInformationUpdService vehicleCacheCarInformationUpdService;


    /**
     * 查询车辆业务信息
     */
    @PostMapping("/selectCarInformation")
    @Operation(summary = "查询数据",description = "查询数据")
    public Result<List<CarInformation>> selectConnect(){
        List<CarInformation> connects = carInformationService.list()
                .stream()
                .map(CarInformation::carInformationBuilder)
                .toList();

        return Result.success(
                connects, "操作成功"
        );
    }

    /**
     * 企业车辆管理列表
     * 联查--> 车辆管理--车辆类型表--车辆电子围栏
     * 搜索-->(车辆唯一VIN carInformationVIN 精确查
     * 车辆类型ID  carTypeId  精确查
     * 车辆电子围栏外键ID carInformationFence 精确查
     * 启用状态(1.在线 2.离线 3.已断开 4.待连接 5.维修中)
     * carInformationState  精确查
     * 车辆电机厂商 carInformationMotorManufacturer 模糊查
     * 车辆电机型号 carInformationMotorModel 精确查
     * 车辆电池厂商 carInformationBatteryManufacturer 模糊查
     * 车辆电池型号 carInformationBatteryModel 精确查
     * )
     * 分页-->  分页页数 pageNum  分页条数 pageSize
     *
     * @param carInformationListReq
     * @return
     */
    @PostMapping("/selectCarInformationList")
    @Operation(summary = "企业车辆管理列表")
    public Result<Page<CarInformationResp>> selectCarInformationList(@Validated @RequestBody CarInformationListReq carInformationListReq) {

        Page<CarInformationResp> pageInfo = carInformationService.selectCarInformationList(carInformationListReq);
        log.info("企业车辆管理列表查询",carInformationListReq,pageInfo);
        List<CarInformationResp> records = pageInfo.getRecords();
        records.forEach(carInformation -> {
            vehicleCacheCarInformationService.put(
                    vehicleCacheCarInformationService.keyPre()+carInformation.getCarInformationVIN()
                    ,records);
        });
        return Result.success(pageInfo);

    }


    /**
     * 企业车辆添加管理
     * @param carInformationAddReq
     * @return
     */
    @PostMapping("/addCarInformation")
    @Operation(summary = "企业车辆添加管理")
    public Result addCarInformation(@Validated @RequestBody CarInformationAddReq carInformationAddReq){

        vehicleCacheCarInformationAddService.put(
                vehicleCacheCarInformationAddService
                        .keyPre()+carInformationAddReq.getCarInformationVIN(), carInformationAddReq);
        return carInformationService.addCarInformation(carInformationAddReq)
                ?Result.success("添加车辆成功")
                :Result.error(402,"添加车辆失败");
    }

    /**
     * 企业车辆删除
     * @param carInformationId
     * @return
     */
    @GetMapping("/delBycarInformationId/{carInformationId}")
    @Operation(summary = "企业车辆删除")
    public Result delBycarInformationId(@Validated @RequestParam(name = "carInformationId") Integer carInformationId){
        boolean delBycarInformationId = carInformationService.delBycarInformationId(carInformationId);
        if (delBycarInformationId){
            return Result.success(delBycarInformationId ,"删除车辆成功");
        }
        return Result.error(402,"删除车辆失败");
    }


    /**
     * 企业车辆修改管理
     * @param carInformationUpdReq
     * @return
     */
    @PostMapping("/updatecarInformation")
    @Operation(summary = "企业车辆修改管理")
    public Result updateCarMessage(@Validated @RequestBody CarInformationUpdReq carInformationUpdReq){
        boolean updatecarInformation = carInformationService.updatecarInformation(carInformationUpdReq);
        log.info(updatecarInformation);
        vehicleCacheCarInformationUpdService.put(
                vehicleCacheCarInformationUpdService
                        .keyPre()+ObtainRootLogin.obtain(), carInformationUpdReq);
        if(updatecarInformation)
        {
            return Result.success(carInformationUpdReq,"修改成功");
        }
        return Result.error( 402,"修改失败");
    }



    /**
     * To 电子围栏负责人
     * 查询企业车辆 carInformationID 和  carInformationLicensePlate
     * 无参
     * @return
     */
    @GetMapping("/selectCarInformationIdAndLicensePlate")
    @Operation(summary = "查询企业车辆 carInformationID 和  carInformationLicensePlate")
    public Result<List<CarInformationResp>> selectCarInformationIdAndLicensePlate(){
        List<CarInformationResp> carInformations = carInformationService.selectBycarInformationIDAndLicensePlate();

        return Result.success(carInformations);
    }



}
