package com.bs.tm.controller;

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

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.tm.domain.TmOrderVehicle;
import com.bs.tm.service.ITmOrderVehicleService;
import javax.annotation.Resource;

/**
 * 订单车辆Controller
 *
 * @author bs
 * @date 2024-02-07
 */
@Api(tags = "订单车辆")
@RestController
@RequestMapping("/order/vehicle")
public class TmOrderVehicleController extends BaseController {
    @Resource
    private ITmOrderVehicleService tmOrderVehicleService;

    /**
     * 分页查询订单车辆列表
     */
    @ApiOperation("分页查询订单车辆列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmOrderVehicle tmOrderVehicle) {
        startPage();
        LambdaQueryWrapper<TmOrderVehicle> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmOrderVehicle);
        List<TmOrderVehicle> list = tmOrderVehicleService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询订单车辆列表
     */
    @ApiOperation("查询订单车辆列表")
    @GetMapping("/list")
    public AjaxResult list(TmOrderVehicle tmOrderVehicle) {
        LambdaQueryWrapper<TmOrderVehicle> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmOrderVehicle);
        List<TmOrderVehicle> list = tmOrderVehicleService.list(queryWrapper);
        return success(list);
    }

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

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

    /**
     * 新增订单车辆
     */
    @ApiOperation("新增订单车辆")
    @Log(title = "订单车辆新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmOrderVehicle tmOrderVehicle) {
        return toAjax(tmOrderVehicleService.save(tmOrderVehicle));
    }

    /**
     * 修改订单车辆
     */
    @ApiOperation("修改订单车辆")
    @Log(title = "订单车辆修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmOrderVehicle tmOrderVehicle) {
        return toAjax(tmOrderVehicleService.updateById(tmOrderVehicle));
    }

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

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


        //id
        if(Validator.isNotEmpty(tmOrderVehicle.getId())){
            queryWrapper.eq(TmOrderVehicle::getId,tmOrderVehicle.getId());
        }

        //订单ID
        if(Validator.isNotEmpty(tmOrderVehicle.getOrderId())){
            queryWrapper.eq(TmOrderVehicle::getOrderId,tmOrderVehicle.getOrderId());
        }

        //车牌ID
        if(Validator.isNotEmpty(tmOrderVehicle.getVehicleId())){
            queryWrapper.eq(TmOrderVehicle::getVehicleId,tmOrderVehicle.getVehicleId());
        }

        //挂车ID
        if(Validator.isNotEmpty(tmOrderVehicle.getTrailerId())){
            queryWrapper.eq(TmOrderVehicle::getTrailerId,tmOrderVehicle.getTrailerId());
        }

        //GPS代码
        if(Validator.isNotEmpty(tmOrderVehicle.getGpsCode())){
            queryWrapper.eq(TmOrderVehicle::getGpsCode,tmOrderVehicle.getGpsCode());
        }

        //联系人
        if(Validator.isNotEmpty(tmOrderVehicle.getLinkMan())){
            queryWrapper.eq(TmOrderVehicle::getLinkMan,tmOrderVehicle.getLinkMan());
        }

        //联系电话
        if(Validator.isNotEmpty(tmOrderVehicle.getMobileNumber())){
            queryWrapper.like(TmOrderVehicle::getMobileNumber,tmOrderVehicle.getMobileNumber());
        }


        //车辆结构
        if(Validator.isNotEmpty(tmOrderVehicle.getVehicleStucture())){
            queryWrapper.eq(TmOrderVehicle::getVehicleStucture,tmOrderVehicle.getVehicleStucture());
        }

        //车型
        if(Validator.isNotEmpty(tmOrderVehicle.getVehilceModel())){
            queryWrapper.eq(TmOrderVehicle::getVehilceModel,tmOrderVehicle.getVehilceModel());
        }

        //车长
        if(Validator.isNotEmpty(tmOrderVehicle.getVehicleLength())){
            queryWrapper.eq(TmOrderVehicle::getVehicleLength,tmOrderVehicle.getVehicleLength());
        }

        //载重
        if(Validator.isNotEmpty(tmOrderVehicle.getVehicleLoad())){
            queryWrapper.eq(TmOrderVehicle::getVehicleLoad,tmOrderVehicle.getVehicleLoad());
        }

        //装载体积
        if(Validator.isNotEmpty(tmOrderVehicle.getVehicleVol())){
            queryWrapper.eq(TmOrderVehicle::getVehicleVol,tmOrderVehicle.getVehicleVol());
        }

        //车辆情况
        if(Validator.isNotEmpty(tmOrderVehicle.getVehicleCondition())){
            queryWrapper.eq(TmOrderVehicle::getVehicleCondition,tmOrderVehicle.getVehicleCondition());
        }

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

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

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

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

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

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

        }

}
