package cn.iocoder.yudao.module.asset.controller.admin.vehiclemodel;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.asset.controller.admin.vehiclemodel.vo.*;
import cn.iocoder.yudao.module.asset.dal.dataobject.vehiclemodel.VehicleModelDO;
import cn.iocoder.yudao.module.asset.dal.dataobject.vehiclemodel.VehicleModelMaintainItemDO;
import cn.iocoder.yudao.module.asset.service.vehiclemodel.VehicleModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 车型参数 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 车型参数")
@RestController
@RequestMapping("/asset/vehicle-model")
@Validated
public class VehicleModelController {

    @Resource
    private VehicleModelService vehicleModelService;

    @PostMapping("/create")
    @Operation(summary = "创建车型参数")
    @PreAuthorize("@ss.hasPermission('asset:vehicle-model:create')")
    public CommonResult<Long> createVehicleModel(@Valid @RequestBody VehicleModelSaveReqVO createReqVO) {
        return success(vehicleModelService.createVehicleModel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车型参数")
    @PreAuthorize("@ss.hasPermission('asset:vehicle-model:update')")
    public CommonResult<Boolean> updateVehicleModel(@Valid @RequestBody VehicleModelSaveReqVO updateReqVO) {
        vehicleModelService.updateVehicleModel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车型参数")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asset:vehicle-model:delete')")
    public CommonResult<Boolean> deleteVehicleModel(@RequestParam("id") Long id) {
        vehicleModelService.deleteVehicleModel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车型参数")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('asset:vehicle-model:query')")
    public CommonResult<VehicleModelRespVO> getVehicleModel(@RequestParam("id") Long id) {
        VehicleModelDO vehicleModel = vehicleModelService.getVehicleModel(id);
        VehicleModelRespVO respVO = BeanUtils.toBean(vehicleModel, VehicleModelRespVO.class);
        
        // 获取维保项目列表
        List<VehicleModelMaintainItemDO> maintainItems = vehicleModelService.getVehicleModelMaintainItems(id);
        respVO.setMaintainItems(BeanUtils.toBean(maintainItems, VehicleModelMaintainItemVO.class));
        
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得车型参数分页")
    @PreAuthorize("@ss.hasPermission('asset:vehicle-model:query')")
    public CommonResult<PageResult<VehicleModelRespVO>> getVehicleModelPage(@Valid VehicleModelPageReqVO pageReqVO) {
        PageResult<VehicleModelDO> pageResult = vehicleModelService.getVehicleModelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VehicleModelRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得车型参数精简列表", description = "用于下拉选择")
    public CommonResult<List<VehicleModelSimpleRespVO>> getVehicleModelSimpleList() {
        List<VehicleModelDO> list = vehicleModelService.getVehicleModelSimpleList();
        return success(BeanUtils.toBean(list, VehicleModelSimpleRespVO.class));
    }

    @GetMapping("/list-by-brand")
    @Operation(summary = "根据品牌获取车型参数列表")
    @Parameter(name = "brand", description = "品牌", required = true)
    public CommonResult<List<VehicleModelRespVO>> getVehicleModelListByBrand(@RequestParam("brand") String brand) {
        List<VehicleModelDO> list = vehicleModelService.getVehicleModelListByBrand(brand);
        return success(BeanUtils.toBean(list, VehicleModelRespVO.class));
    }

    @GetMapping("/list-by-model")
    @Operation(summary = "根据车型获取车型参数列表")
    @Parameter(name = "model", description = "车型", required = true)
    public CommonResult<List<VehicleModelRespVO>> getVehicleModelListByModel(@RequestParam("model") String model) {
        List<VehicleModelDO> list = vehicleModelService.getVehicleModelListByModel(model);
        return success(BeanUtils.toBean(list, VehicleModelRespVO.class));
    }

    @GetMapping("/maintain-item/used-list")
    @Operation(summary = "获取已使用的保养项目列表", description = "用于前端下拉提示，返回去重后的保养项目名称")
    public CommonResult<List<String>> getUsedMaintainItems() {
        List<String> list = vehicleModelService.getUsedMaintainItems();
        return success(list);
    }

}
