package com.bs.cm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.bs.jxc.domain.CmPort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.lang.Validator;
import com.bs.cm.domain.CmVehicle;
import com.bs.cm.service.ICmVehicleService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 车辆信息Controller
 *
 * @author bs
 * @date 2025-01-12
 */
@Api(tags = "车辆信息")
@RestController
@RequestMapping("/cm/vehicle")
public class CmVehicleController extends BaseController {
    @Resource
    private ICmVehicleService cmVehicleService;

    /**
     * 分页查询车辆信息列表
     */
    @ApiOperation("分页查询车辆信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CmVehicle cmVehicle) {
        startPage();
        LambdaQueryWrapper<CmVehicle> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmVehicle);
        List<CmVehicle> list = cmVehicleService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询车辆信息列表
     */
    @ApiOperation("查询车辆信息列表")
    @GetMapping("/list")
    public AjaxResult list(CmVehicle cmVehicle) {
        LambdaQueryWrapper<CmVehicle> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmVehicle);
        List<CmVehicle> list = cmVehicleService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出车辆信息列表
     */
    @ApiOperation("导出车辆信息列表")
    @Log(title = "车辆信息导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmVehicle cmVehicle) {
        LambdaQueryWrapper<CmVehicle> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmVehicle);
        List<CmVehicle> list = cmVehicleService.list(queryWrapper);
        ExcelUtil<CmVehicle> util = new ExcelUtil<CmVehicle>(CmVehicle. class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    @Log(title = "车辆信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) {
        try {
            ExcelUtil<CmVehicle> util = new ExcelUtil<>(CmVehicle.class);
            List<CmVehicle> portList = util.importExcel(file.getInputStream());
            cmVehicleService.saveBatch(portList);
            return AjaxResult.success("导入成功");
        } catch (Exception e) {
            return AjaxResult.error("导入失败：" + e.getMessage());
        }
    }

    /**
     * 获取车辆信息详细信息
     */
    @ApiOperation("获取车辆信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cmVehicleService.getById(id));
    }

    /**
     * 新增车辆信息
     */
    @ApiOperation("新增车辆信息")
    @Log(title = "车辆信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmVehicle cmVehicle) {
        return toAjax(cmVehicleService.save(cmVehicle));
    }

    /**
     * 修改车辆信息
     */
    @ApiOperation("修改车辆信息")
    @Log(title = "车辆信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmVehicle cmVehicle) {
        return toAjax(cmVehicleService.updateById(cmVehicle));
    }

    /**
     * 删除车辆信息
     */
    @ApiOperation("删除车辆信息")
    @Log(title = "车辆信息删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(cmVehicleService.removeBatchByIds(ids));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<CmVehicle> queryWrapper,CmVehicle cmVehicle){


        //车辆ID
        if(Validator.isNotEmpty(cmVehicle.getId())){
            queryWrapper.eq(CmVehicle::getId,cmVehicle.getId());
        }

        //车牌号
        if(Validator.isNotEmpty(cmVehicle.getVehicleNo())){
            queryWrapper.eq(CmVehicle::getVehicleNo,cmVehicle.getVehicleNo());
        }

        //车辆编号
        if(Validator.isNotEmpty(cmVehicle.getVehicleNumber())){
            queryWrapper.eq(CmVehicle::getVehicleNumber,cmVehicle.getVehicleNumber());
        }

        //车辆类型
        if(Validator.isNotEmpty(cmVehicle.getVehicleType())){
            queryWrapper.eq(CmVehicle::getVehicleType,cmVehicle.getVehicleType());
        }

        //设备型号
        if(Validator.isNotEmpty(cmVehicle.getVehicleModel())){
            queryWrapper.eq(CmVehicle::getVehicleModel,cmVehicle.getVehicleModel());
        }

        //发动机号
        if(Validator.isNotEmpty(cmVehicle.getEngineNo())){
            queryWrapper.eq(CmVehicle::getEngineNo,cmVehicle.getEngineNo());
        }

        //车辆识别代码
        if(Validator.isNotEmpty(cmVehicle.getVinNo())){
            queryWrapper.eq(CmVehicle::getVinNo,cmVehicle.getVinNo());
        }

        //发动机型号
        if(Validator.isNotEmpty(cmVehicle.getEngineModel())){
            queryWrapper.eq(CmVehicle::getEngineModel,cmVehicle.getEngineModel());
        }

        //设备马力
        if(Validator.isNotEmpty(cmVehicle.getHorsePower())){
            queryWrapper.eq(CmVehicle::getHorsePower,cmVehicle.getHorsePower());
        }

        //车辆轴数
        if(Validator.isNotEmpty(cmVehicle.getAxlesNumber())){
            queryWrapper.eq(CmVehicle::getAxlesNumber,cmVehicle.getAxlesNumber());
        }

        //装卸能力（T）
        if(Validator.isNotEmpty(cmVehicle.getLoadingCapacity())){
            queryWrapper.eq(CmVehicle::getLoadingCapacity,cmVehicle.getLoadingCapacity());
        }

        //发动机厂家
        if(Validator.isNotEmpty(cmVehicle.getEngineManufacturer())){
            queryWrapper.eq(CmVehicle::getEngineManufacturer,cmVehicle.getEngineManufacturer());
        }

        //备注
        if(Validator.isNotEmpty(cmVehicle.getRemark())){
            queryWrapper.eq(CmVehicle::getRemark,cmVehicle.getRemark());
        }

        //删除标志（0代表存在 2代表删除）
        if(Validator.isNotEmpty(cmVehicle.getDelFlag())){
            queryWrapper.eq(CmVehicle::getDelFlag,cmVehicle.getDelFlag());
        }

        //创建部门
        if(Validator.isNotEmpty(cmVehicle.getCreateDept())){
            queryWrapper.eq(CmVehicle::getCreateDept,cmVehicle.getCreateDept());
        }

        //创建人
        if(Validator.isNotEmpty(cmVehicle.getCreateBy())){
            queryWrapper.eq(CmVehicle::getCreateBy,cmVehicle.getCreateBy());
        }

        //创建时间
        if(Validator.isNotEmpty(cmVehicle.getCreateTime())){
            queryWrapper.eq(CmVehicle::getCreateTime,cmVehicle.getCreateTime());
        }

        //修改人
        if(Validator.isNotEmpty(cmVehicle.getUpdateBy())){
            queryWrapper.eq(CmVehicle::getUpdateBy,cmVehicle.getUpdateBy());
        }

        //修改时间
        if(Validator.isNotEmpty(cmVehicle.getUpdateTime())){
            queryWrapper.eq(CmVehicle::getUpdateTime,cmVehicle.getUpdateTime());
        }

        //租户ID
        if(Validator.isNotEmpty(cmVehicle.getTenantId())){
            queryWrapper.eq(CmVehicle::getTenantId,cmVehicle.getTenantId());
        }

        }

}
