package com.muyu.enterprise.controller.car;



import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.log.annotation.Log;
import com.muyu.enterprise.domain.car.Vehicle;
import com.muyu.enterprise.domain.req.car.VehicleAddReq;
import com.muyu.enterprise.domain.req.car.VehicleManageReq;
import com.muyu.enterprise.domain.req.car.VehicleUpdReq;
import com.muyu.enterprise.domain.resp.car.VehicleManageResp;
import com.muyu.enterprise.service.car.VehicleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
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 java.util.List;

/**
 * 车辆管理控制层
 * @Author：李庆帅
 * @Package：com.muyu.car.controller
 * @Project：cloud-server
 * @name：VehicleController
 * @Date：2024/9/26 22:17
 * @Description: 车辆管理控制层
 */
@Log4j2
@RestController
@RequestMapping("/vehicleManage")
@Tag(name = "车辆管理控制层", description = "进行车辆管理操作")
public class VehicleController extends BaseController
{
    @Autowired
    private VehicleService vehicleService;

    /**
     * 查询车辆管理列表
     * @param vehicleManageReq 请求对象
     * @return 返回结果
     */
    @RequestMapping(path = "/list", method = RequestMethod.POST)
    @Operation(summary = "车辆管理列表", description = "根据车牌号、VIN码、车辆状态筛选车辆")
    public Result<TableDataInfo<VehicleManageResp>> getVehicleList(@RequestBody VehicleManageReq vehicleManageReq) {
        startPage();
        List<VehicleManageResp> list = vehicleService.getVehicleList(vehicleManageReq);
        return getDataTable(list);
    }

    /**
     * 车辆录入（添加车辆）
     * @param vehicleAddReq 请求对象
     * @return 返回结果
     */
    @PostMapping("/")
    @Operation(summary = "新增车辆", description = "录入车辆信息")
    public Result<String> save(@RequestBody VehicleAddReq vehicleAddReq) {
        vehicleService.save(Vehicle.addBuild(vehicleAddReq));
        return Result.success("录入成功");
    }

    /**
     * 通过车辆id获取车辆信息
     * @param vehicleId 请求对象
     * @return 返回结果
     */
    @GetMapping("/{vehicleId}")
    @Operation(summary = "通过id查询车辆信息", description = "通过id查询车辆信息")
    public Result<Vehicle> findById(@PathVariable("vehicleId") Long vehicleId) {
        return Result.success(vehicleService.getById(vehicleId));
    }

    /**
     * 修改车辆信息
     * @param vehicleId 请求对象
     * @param vehicleUpdReq 请求对象
     * @return 返回结果
     */
    @PutMapping("/{vehicleId}")
    @Operation(summary = "修改车辆信息", description = "修改车辆信息")
    public Result<String> update(
            @PathVariable("vehicleId") Long vehicleId,
            @RequestBody @Validated VehicleUpdReq vehicleUpdReq) {
        vehicleService.updateById(Vehicle.updBuild(vehicleUpdReq, () -> vehicleId));
        return Result.success("修改成功");
    }

    /**
     * 通过车辆id删除车辆信息
     * @param vehicleId 请求对象
     * @return 返回结果
     */
    @DeleteMapping("/{vehicleId}")
    @Operation(summary = "通过车辆id删除车辆信息", description = "通过车辆id删除车辆信息")
    public Result<String> delete(@PathVariable("vehicleId") Long vehicleId) {
        vehicleService.removeById(vehicleId);
        return Result.success("删除成功");
    }

    /**
     * 批量删除车辆
     * @param vehicleIds 请求对象
     * @return 返回结果
     */
    @DeleteMapping("/batchDelete")
    @Operation(summary = "批量删除车辆")
    public Result<String> batchDelete(@RequestBody List<Long> vehicleIds) {
        vehicleService.removeBatchByIds(vehicleIds);
        return Result.success("批量删除成功");
    }

    /**
     * 导出所有车辆数据
     * @param response 请求对象
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
        List<Vehicle> list = vehicleService.list();
        ExcelUtil<Vehicle> util = new ExcelUtil<>(Vehicle.class);
        util.exportExcel(response, list, "车辆数据");
    }

    /**
     * 通过车辆vin码查询模板id
     * @param vehicleVin 请求对象
     * @return 返回结果
     */
    @GetMapping("/findByVehicleVin/{vehicleVin}")
    @Operation(description = "通过车辆vin码查询模板id")
    public Result<Long> findByVehicleVin(@PathVariable("vehicleVin") String vehicleVin) {
        Long byVehicleVin = vehicleService.findByVehicleVin(vehicleVin);
        return Result.success(byVehicleVin);
    }

}
