package com.bs.tm.controller;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.bs.tm.domain.*;
import com.bs.tm.service.*;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
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 org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 运输装载Controller
 *
 * @author bs
 * @date 2024-02-07
 */
@Api(tags = "运输装载")
@RestController
@RequestMapping("/trans/cargo")
public class TmOrderTransCargoController extends BaseController {
    @Resource
    private ITmOrderTransCargoService tmOrderTransCargoService;
    @Resource
    private ITmOrderService tmOrderService;
    @Resource
    private ITmOrderTransService tmOrderTransService;
    @Resource
    private ITmVehicleService tmVehicleService;
    @Resource
    private ITmDriverService tmDriverService;
    /**
     * 分页查询运输装载列表
     */
    @ApiOperation("分页查询运输装载列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmOrderTransCargo tmOrderTransCargo) {
        startPage();
        MPJLambdaWrapper<TmOrderTransCargo> queryWrapper = new MPJLambdaWrapper();
        queryWrapper.selectAll(TmOrderTransCargo.class)
                .select(TmOrder::getStatus)
                .disableSubLogicDel()
                .leftJoin(TmOrder.class, TmOrder::getId, TmOrderTransCargo::getOrderId);
        conditionByMPJ(queryWrapper,tmOrderTransCargo);
        List<TmOrderTransCargo> list = tmOrderTransCargoService.list(queryWrapper);
        if (null != list && list.size() > 0) {
            List<Long> ids = new ArrayList<>();
            List<Long> transIds = new ArrayList<>();
            for (TmOrderTransCargo trans : list) {
                ids.add(trans.getOrderId());
                transIds.add(trans.getTransId());
            }
            List<TmOrder> lsOrder = tmOrderService.listByIds(ids);
            List<TmOrderTrans> lsTrans = tmOrderTransService.listByIds(transIds);
            Map<Long, TmOrder> orderMap = new HashMap<>();
            Map<Long, TmOrderTrans> transMap = new HashMap<>();
            lsOrder.forEach(ele -> {
                orderMap.put(ele.getId(), ele);
            });
            lsTrans.forEach(ele -> {
                transMap.put(ele.getId(), ele);
            });
            list.forEach(ele -> {
                ele.setOrder(orderMap.get(ele.getOrderId()));
                if (null != orderMap.get(ele.getOrderId())) {
                    ele.setOrderNo(orderMap.get(ele.getOrderId()).getOrderNo());
                }
                if (null != transMap.get(ele.getTransId())) {
                    ele.setDriverName(transMap.get(ele.getTransId()).getDriverName());
                    ele.setVehicleNumber(transMap.get(ele.getTransId()).getVehicleNumber());
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 分页查询运输装载列表
     */
    @ApiOperation("分页查询运输装载列表")
    @GetMapping("/pageListBySearch")
    public TableDataInfo pageListBySearch(TmOrderTransCargo tmOrderTransCargo) {
        startPage();
        MPJLambdaWrapper<TmOrderTransCargo> queryWrapper = new MPJLambdaWrapper();
        queryWrapper.selectAll(TmOrderTransCargo.class)
                    .select(TmOrderTrans::getDriverName, TmOrderTrans::getVehicleNumber)
                    .select(TmOrder::getOrderNo,TmOrder::getStatus,TmOrder::getCustName)
                    .disableSubLogicDel()
                    .leftJoin(TmOrderTrans.class, TmOrderTrans::getId, TmOrderTransCargo::getTransId)
                    .leftJoin(TmOrder.class, TmOrder::getId, TmOrderTransCargo::getOrderId);
        conditionByMPJ(queryWrapper,tmOrderTransCargo);
        List<TmOrderTransCargo> list = tmOrderTransCargoService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 分页查询运输装载列表
     * @return
     */
    @ApiOperation("分页查询运输装载列表")
    @GetMapping("/getSum")
    public Map<String, BigDecimal> getSum(TmOrderTransCargo tmOrderTransCargo) {
        MPJLambdaWrapper<TmOrderTransCargo> queryWrapper = new MPJLambdaWrapper();
        queryWrapper.selectAll(TmOrderTransCargo.class)
                .select(TmOrderTrans::getDriverName, TmOrderTrans::getVehicleNumber)
                .select(TmOrder::getOrderNo,TmOrder::getStatus,TmOrder::getCustName)
                .disableSubLogicDel()
                .leftJoin(TmOrderTrans.class, TmOrderTrans::getId, TmOrderTransCargo::getTransId)
                .leftJoin(TmOrder.class, TmOrder::getId, TmOrderTransCargo::getOrderId);
        conditionByMPJ(queryWrapper,tmOrderTransCargo);
        List<TmOrderTransCargo> list = tmOrderTransCargoService.list(queryWrapper);
        BigDecimal totalGrossWeight = BigDecimal.ZERO;
        BigDecimal totalPierCarWeight = BigDecimal.ZERO;
        BigDecimal totalRealLoadWt = BigDecimal.ZERO;
        BigDecimal totalGrossWeightUnloading = BigDecimal.ZERO;
        BigDecimal totalPlantCarWeight = BigDecimal.ZERO;
        BigDecimal totalRealTransWt = BigDecimal.ZERO;
        BigDecimal totalPoundDifference = BigDecimal.ZERO;
        for (TmOrderTransCargo cargo : list) {
            if (cargo.getGrossWeightLoading() != null) {
                totalGrossWeight = totalGrossWeight.add(cargo.getGrossWeightLoading());
            }
            if (cargo.getPierCarWeight() != null) {
                totalPierCarWeight = totalPierCarWeight.add(cargo.getPierCarWeight());
            }
            if (cargo.getRealLoadWt() != null) {
                totalRealLoadWt = totalRealLoadWt.add(cargo.getRealLoadWt());
            }
            if (cargo.getGrossWeightUnloading() != null) {
                totalGrossWeightUnloading = totalGrossWeightUnloading.add(cargo.getGrossWeightUnloading());
            }
            if (cargo.getPlantCarWeight() != null) {
                totalPlantCarWeight = totalPlantCarWeight.add(cargo.getPlantCarWeight());
            }
            if (cargo.getRealTransWt() != null) {
                totalRealTransWt = totalRealTransWt.add(cargo.getRealTransWt());
            }
            if (cargo.getPoundDifference() != null) {
                totalPoundDifference = totalPoundDifference.add(cargo.getPoundDifference());
            }
        }
        Map<String, BigDecimal> totalsMap = new HashMap<>();
        totalsMap.put("totalGrossWeight", totalGrossWeight);
        totalsMap.put("totalPierCarWeight", totalPierCarWeight);
        totalsMap.put("totalRealLoadWt", totalRealLoadWt);
        totalsMap.put("totalGrossWeightUnloading", totalGrossWeightUnloading);
        totalsMap.put("totalPlantCarWeight", totalPlantCarWeight);
        totalsMap.put("totalRealTransWt", totalRealTransWt);
        totalsMap.put("totalPoundDifference", totalPoundDifference);
        return totalsMap;
    }


    /**
     * 导出运输装载列表
     */
    @ApiOperation("导出运输装载列表")
    @Log(title = "运输装载导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportBySearch")
    public void exportBySearch(HttpServletResponse response, TmOrderTransCargo tmOrderTransCargo) {
        MPJLambdaWrapper<TmOrderTransCargo> queryWrapper = new MPJLambdaWrapper();
        queryWrapper.selectAll(TmOrderTransCargo.class)
                .select(TmOrderTrans::getDriverName, TmOrderTrans::getVehicleNumber)
                .select(TmOrder::getOrderNo,TmOrder::getStatus,TmOrder::getCustName)
                .disableSubLogicDel()
                .leftJoin(TmOrderTrans.class, TmOrderTrans::getId, TmOrderTransCargo::getTransId)
                .leftJoin(TmOrder.class, TmOrder::getId, TmOrderTransCargo::getOrderId);
        conditionByMPJ(queryWrapper,tmOrderTransCargo);
        List<TmOrderTransCargo> list = tmOrderTransCargoService.list(queryWrapper);
        for (TmOrderTransCargo ele : list) {
            if ("1".equals(ele.getStatus())) {
                ele.setStatus("已完成");
            } else if ("0".equals(ele.getStatus())) {
                ele.setStatus("未完成");
            }
        }
        ExcelUtil<TmOrderTransCargo> util = new ExcelUtil<TmOrderTransCargo>(TmOrderTransCargo. class);
        util.exportExcel(response, list, "运输装载数据");
    }

    /**
     * 查询运输装载列表
     */
    @ApiOperation("查询运输装载列表")
    @GetMapping("/list")
    public AjaxResult list(TmOrderTransCargo tmOrderTransCargo) {
        LambdaQueryWrapper<TmOrderTransCargo> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmOrderTransCargo);
        List<TmOrderTransCargo> list = tmOrderTransCargoService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出运输装载列表
     */
    @ApiOperation("导出运输装载列表")
    @Log(title = "运输装载导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TmOrderTransCargo tmOrderTransCargo) {
        LambdaQueryWrapper<TmOrderTransCargo> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmOrderTransCargo);
        List<TmOrderTransCargo> list = tmOrderTransCargoService.list(queryWrapper);
        if (null != list && list.size() > 0) {
            List<Long> ids = new ArrayList<>();
            List<Long> transIds = new ArrayList<>();
            for (TmOrderTransCargo trans : list) {
                ids.add(trans.getOrderId());
                transIds.add(trans.getTransId());
            }

            List<TmOrder> lsOrder = tmOrderService.listByIds(ids);
            List<TmOrderTrans> lsTrans = tmOrderTransService.listByIds(transIds);
            Map<Long, TmOrder> orderMap = new HashMap<>();
            Map<Long, TmOrderTrans> transMap = new HashMap<>();
            lsOrder.forEach(ele -> {
                orderMap.put(ele.getId(), ele);
            });
            lsTrans.forEach(ele -> {
                transMap.put(ele.getId(), ele);
            });
            list.forEach(ele -> {
                ele.setOrder(orderMap.get(ele.getOrderId()));
                if (null != orderMap.get(ele.getOrderId())) {
                    ele.setOrderNo(orderMap.get(ele.getOrderId()).getOrderNo());
                    ele.setCustName(orderMap.get(ele.getOrderId()).getCustName());
                }
                if (null != transMap.get(ele.getTransId())) {
                    ele.setDriverName(transMap.get(ele.getTransId()).getDriverName());
                    ele.setDriverMobile(transMap.get(ele.getTransId()).getDriverMobile());
                    ele.setVehicleNumber(transMap.get(ele.getTransId()).getVehicleNumber());
                    ele.setReceiver(transMap.get(ele.getTransId()).getReceiver());
                }
            });
        }
        List<TmOrderByImportData> tmOrderByImportDataList = new ArrayList<>();
        for (TmOrderTransCargo cargo : list) {
            TmOrderByImportData tmOrderByImportData =  new TmOrderByImportData();
            tmOrderByImportData.setReceiver(cargo.getReceiver());
            tmOrderByImportData.setOrderNo(cargo.getOrderNo());
            tmOrderByImportData.setInputDate(cargo.getInputDate());
            tmOrderByImportData.setCargoName(cargo.getCargoName());
            tmOrderByImportData.setVehicleNumber(cargo.getVehicleNumber());
            tmOrderByImportData.setDriverName(cargo.getDriverName());
            tmOrderByImportData.setDepartTime(cargo.getDepartTime());
            tmOrderByImportData.setArrivalTime(cargo.getArrivalTime());
            tmOrderByImportData.setPlantCarWeight(cargo.getPlantCarWeight());
            tmOrderByImportData.setPlantWeight(cargo.getPlantWeight());
            tmOrderByImportData.setPierCarWeight(cargo.getPierCarWeight());
            tmOrderByImportData.setPierWeight(cargo.getPierWeight());
            tmOrderByImportData.setNetWeightLoading(cargo.getRealLoadWt());
            tmOrderByImportData.setNetWeightUnloading(cargo.getRealTransWt());
            tmOrderByImportData.setGrossWeightLoading(cargo.getGrossWeightLoading());
            tmOrderByImportData.setGrossWeightUnloading(cargo.getGrossWeightUnloading());
            tmOrderByImportData.setPoundDifference(cargo.getPoundDifference());
            tmOrderByImportData.setDriverMobile(cargo.getDriverMobile());
            tmOrderByImportData.setReceiver(cargo.getReceiver());
            tmOrderByImportDataList.add(tmOrderByImportData);
        }
        ExcelUtil<TmOrderByImportData> util = new ExcelUtil<TmOrderByImportData>(TmOrderByImportData. class);
        util.exportExcel(response, tmOrderByImportDataList, "运输装载数据");
    }

    @Log(title = "导入运输装载列表", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(Long updateSupport,Long orderId,MultipartFile file) {
        try {
            ExcelUtil<TmOrderByImportData> util = new ExcelUtil<>(TmOrderByImportData.class);
            List<TmOrderByImportData> transCargoByImportList = util.importExcel(file.getInputStream());
            TmOrder tmOrder = tmOrderService.getOne(new LambdaQueryWrapper<TmOrder>().eq(TmOrder::getId, orderId)
                    .last ("limit 1"));
            List<TmOrderTrans> lsTrans = new ArrayList<TmOrderTrans>();
            List<TmOrderTransCargo> lsTransCargo = new ArrayList<TmOrderTransCargo>();
            if (transCargoByImportList.size() == 0) {
                return AjaxResult.error("请填写数据");
            }
            for (TmOrderByImportData ele : transCargoByImportList) {
                if (Validator.isEmpty(ele.getVehicleNumber())) {
                    return AjaxResult.error("请填写车牌号码");
                }
                if (Validator.isEmpty(ele.getCargoName())) {
                    return AjaxResult.error("请填写货物");
                }
                if (Validator.isEmpty(ele.getGrossWeightUnloading())) {
                    return AjaxResult.error("请填写卸货毛重");
                }
                if (Validator.isEmpty(ele.getGrossWeightLoading())) {
                    return AjaxResult.error("请填写装货毛重");
                }
                TmOrderTrans tmOrderTrans = new TmOrderTrans();
                TmOrderTransCargo tmOrderTransCargo = new TmOrderTransCargo();
                TmVehicle tmVehicle = tmVehicleService.getOne(new LambdaQueryWrapper<TmVehicle>().eq(TmVehicle::getVehicleNumber, ele.getVehicleNumber())
                        .last("limit 1"));
                if (null != tmVehicle) {
                    tmOrderTrans.setVehicleId(tmVehicle.getId());
                    tmOrderTrans.setVehicleLoad(tmVehicle.getVehicleLoad());
                    tmOrderTrans.setVehicleVol(tmVehicle.getVehicleVol());
                    tmOrderTrans.setVehicleBrand(tmVehicle.getVehicleBrand());
                    tmOrderTrans.setVehicleNumber(ele.getVehicleNumber());
                    if (Validator.isEmpty(ele.getPierCarWeight())) {
                        ele.setPierCarWeight(tmVehicle.getWeight());
                    }
                    if (Validator.isEmpty(ele.getPlantCarWeight())) {
                        ele.setPlantCarWeight(tmVehicle.getWeight());
                    }
                } else {
                    TmVehicle tmVehicleNew = new TmVehicle();
                    tmVehicleNew.setVehicleNumber(ele.getVehicleNumber());
                    tmVehicleNew.setCustId(tmOrder.getCustId());
                    tmVehicleNew.setCustName(tmOrder.getCustName());
                    tmVehicleNew.setWeight(ele.getPierWeight());
                    tmVehicleService.save(tmVehicleNew);
                    TmVehicle tmVehicleTwo = tmVehicleService.getOne(new LambdaQueryWrapper<TmVehicle>().eq(TmVehicle::getVehicleNumber, ele.getVehicleNumber())
                            .last("limit 1"));
                    tmOrderTrans.setVehicleId(tmVehicleTwo.getId());
                    tmOrderTrans.setVehicleNumber(ele.getVehicleNumber());
                }
                tmOrderTrans.setOrderId(tmOrder.getId());
                if (Validator.isEmpty(ele.getInputDate())) {
                    tmOrderTrans.setInputDate(new Date());
                } else {
                    tmOrderTrans.setInputDate(ele.getInputDate());
                }
                if (!Validator.isEmpty(ele.getDriverMobile())) {
                    TmDriver tmDriver = tmDriverService.getOne(new LambdaQueryWrapper<TmDriver>().eq(TmDriver::getMobileNumber, ele.getDriverMobile())
                            .last("limit 1"));
                    if (null != tmDriver){
                        tmOrderTrans.setDriverId(tmDriver.getDriverId());
                        tmOrderTrans.setDriverName(tmDriver.getDriverName());
                        tmOrderTrans.setDriverMobile(tmDriver.getMobileNumber());
                    } else {
                        TmDriver tmDriverNew = new TmDriver();
                        tmDriverNew.setDriverName(ele.getDriverName());
                        tmDriverNew.setMobileNumber(ele.getDriverMobile());
                        tmDriverNew.setCustId(tmOrder.getCustId());
                        tmDriverNew.setCustName(tmOrder.getCustName());
                        tmDriverService.save(tmDriverNew);
                        TmDriver tmDriverTwo = tmDriverService.getOne(new LambdaQueryWrapper<TmDriver>().eq(TmDriver::getMobileNumber, ele.getDriverMobile())
                                .last("limit 1"));
                        tmOrderTrans.setDriverId(tmDriverTwo.getDriverId());
                        tmOrderTrans.setDriverName(tmDriverTwo.getDriverName());
                        tmOrderTrans.setDriverMobile(tmDriverTwo.getMobileNumber());
                    }
                }
                lsTrans.add(tmOrderTrans);
                if (Validator.isEmpty(ele.getInputDate())) {
                    tmOrderTransCargo.setInputDate(new Date());
                } else {
                    tmOrderTransCargo.setInputDate(ele.getInputDate());
                }
                tmOrderTransCargo.setVehicleNumber(ele.getVehicleNumber());
                tmOrderTransCargo.setOrderId(tmOrder.getId());
                tmOrderTransCargo.setCargoName(ele.getCargoName());
                tmOrderTransCargo.setDepartTime(ele.getDepartTime());
                tmOrderTransCargo.setArrivalTime(ele.getArrivalTime());
//                tmOrderTransCargo.setRealLoadWt(ele.getNetWeightUnloading());
                BigDecimal grossWeightLoading = ele.getGrossWeightLoading().setScale(4, BigDecimal.ROUND_HALF_UP);
                BigDecimal pierCarWeight = BigDecimal.ZERO;
                if (Validator.isNotEmpty(ele.getPierCarWeight())) {
                    pierCarWeight = ele.getPierCarWeight().setScale(4, BigDecimal.ROUND_HALF_UP);
                }
                BigDecimal grossWeightUnloading = ele.getGrossWeightUnloading().setScale(4, BigDecimal.ROUND_HALF_UP);
                BigDecimal plantCarWeight = BigDecimal.ZERO;
                if (Validator.isNotEmpty(ele.getPlantCarWeight())) {
                    plantCarWeight = ele.getPlantCarWeight().setScale(4, BigDecimal.ROUND_HALF_UP);
                }
                tmOrderTransCargo.setGrossWeightLoading(grossWeightLoading);
                tmOrderTransCargo.setGrossWeightUnloading(grossWeightUnloading);
                tmOrderTransCargo.setPoundDifference(ele.getPoundDifference());
                tmOrderTransCargo.setPlantWeight(ele.getPlantWeight());
                tmOrderTransCargo.setPierWeight(ele.getPierWeight());
                tmOrderTransCargo.setPlantCarWeight(plantCarWeight);
                tmOrderTransCargo.setPierCarWeight(pierCarWeight);
//                tmOrderTransCargo.setCarWeight(ele.getCarWeight());
                if (Validator.isEmpty(ele.getNetWeightLoading())) {
                    tmOrderTransCargo.setRealLoadWt(grossWeightLoading.subtract(pierCarWeight));
                } else {
                    tmOrderTransCargo.setRealLoadWt(ele.getNetWeightLoading().setScale(4, BigDecimal.ROUND_HALF_UP));
                }
                if (Validator.isEmpty(ele.getNetWeightUnloading())) {
                    tmOrderTransCargo.setRealTransWt(grossWeightUnloading.subtract(plantCarWeight));
                } else {
                    tmOrderTransCargo.setRealTransWt(ele.getNetWeightUnloading().setScale(4, BigDecimal.ROUND_HALF_UP));
                }
                if (Validator.isEmpty(ele.getPoundDifference()) && Validator.isNotEmpty(tmOrderTransCargo.getRealLoadWt()) && Validator.isNotEmpty(tmOrderTransCargo.getRealTransWt())) {
                    BigDecimal netWeightLoading = tmOrderTransCargo.getRealLoadWt();
                    BigDecimal netWeightUnloading = tmOrderTransCargo.getRealTransWt();
                    BigDecimal poundDifference = netWeightLoading.subtract(netWeightUnloading);
                    tmOrderTransCargo.setPoundDifference(poundDifference);
                }
                lsTransCargo.add(tmOrderTransCargo);
            }
            Map<String, List<TmOrderTransCargo>> cargoMap = lsTransCargo.stream()
                    .collect(Collectors.groupingBy(TmOrderTransCargo::getVehicleNumber));
            lsTrans.forEach(trans -> trans.setLsCargos(cargoMap.getOrDefault(trans.getVehicleNumber(), Collections.emptyList())));
            List<TmOrderTrans> uniqueList = lsTrans.stream()
                    .collect(Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TmOrderTrans::getVehicleNumber))),
                            ArrayList::new
                    ));
            TmOrder tmOrderNew = new TmOrder();
            tmOrderNew.setLsTrans(uniqueList);
            return success(tmOrderNew);
        } catch (Exception e) {
            return AjaxResult.error("导入失败：" + e.getMessage());
        }
    }

    @ApiOperation("导入车辆运输计划模板")
    @Log(title = "导入车辆运输计划模板", businessType = BusinessType.EXPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response, TmOrderByImportData tmOrderByImportData) {
        // 创建一个空的客户信息列表
        List<TmOrderByImportData> emptyList = new ArrayList<>();
        // 导出空列表为 Excel 文件
        ExcelUtil<TmOrderByImportData> util = new ExcelUtil<>(TmOrderByImportData.class);
        util.exportExcel(response, emptyList, "车辆运输计划模板");
    }

    /**
     * 获取运输装载详细信息
     */
    @ApiOperation("获取运输装载详细信息")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(tmOrderTransCargoService.getById(ID));
    }

    /**
     * 新增运输装载
     */
    @ApiOperation("新增运输装载")
    @Log(title = "运输装载新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmOrderTransCargo tmOrderTransCargo) {
        return toAjax(tmOrderTransCargoService.save(tmOrderTransCargo));
    }

    /**
     * 修改运输装载
     */
    @ApiOperation("修改运输装载")
    @Log(title = "运输装载修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmOrderTransCargo tmOrderTransCargo) {
        return toAjax(tmOrderTransCargoService.updateById(tmOrderTransCargo));
    }

    @ApiOperation("保存车辆货物运输")
    @Log(title = "保存车辆货物运输", businessType = BusinessType.INSERT)
    @PostMapping("/saveTransCargo")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveTransCargo(@RequestBody TmOrderTrans tmOrderTrans) {
        if (null != tmOrderTrans) {
            List<TmOrderTransCargo> lsCargos = tmOrderTrans.getLsCargos();
            List<TmOrderTransCargo> lsInsert = new ArrayList<>();
            List<TmOrderTransCargo> lsUpdate = new ArrayList<>();
            if (null != lsCargos && lsCargos.size() > 0) {
                for (TmOrderTransCargo cargo : lsCargos) {
                    cargo.setOrderId(tmOrderTrans.getOrderId());
                    if (null == cargo.getId()) {
                        cargo.setTransId(tmOrderTrans.getTransId());
                        lsInsert.add(cargo);
                    } else {
                        lsUpdate.add(cargo);
                    }
                }
                if (lsInsert.size() > 0) {
                    tmOrderTransCargoService.saveBatch(lsInsert);
                }
                if (lsUpdate.size() > 0) {
                    tmOrderTransCargoService.updateBatchById(lsUpdate);
                }
            }
            return success();
        } else {
            return warn("请选择运输货物保存");
        }
    }

    /**
     * 删除运输装载
     */
    @ApiOperation("删除运输装载")
    @Log(title = "运输装载删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable List<Long> IDs) {
        return toAjax(tmOrderTransCargoService.removeBatchByIds(IDs));
    }



    /**
     * 条件设置
     */
    private void conditionByMPJ (MPJLambdaWrapper<TmOrderTransCargo> queryWrapper,TmOrderTransCargo tmOrderTransCargo){


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

        if(Validator.isNotEmpty(tmOrderTransCargo.getOrderId())){
            queryWrapper.eq(TmOrderTransCargo::getOrderId,tmOrderTransCargo.getOrderId());
        }

        //司机
        if(Validator.isNotEmpty(tmOrderTransCargo.getDriverName())){
            queryWrapper.like(TmOrderTrans::getDriverName,tmOrderTransCargo.getDriverName());
        }

        //车牌号
        if(Validator.isNotEmpty(tmOrderTransCargo.getVehicleNumber())){
            queryWrapper.like(TmOrderTrans::getVehicleNumber,tmOrderTransCargo.getVehicleNumber());
        }

        //订单号
        if(Validator.isNotEmpty(tmOrderTransCargo.getOrderNo())){
            queryWrapper.like(TmOrder::getOrderNo,tmOrderTransCargo.getOrderNo());
        }

        //	承运企业
        if(Validator.isNotEmpty(tmOrderTransCargo.getCustName())){
            queryWrapper.like(TmOrder::getCustName,tmOrderTransCargo.getCustName());
        }

        //订单状态
        if(Validator.isNotEmpty(tmOrderTransCargo.getStatus())){
            queryWrapper.eq(TmOrder::getStatus,tmOrderTransCargo.getStatus());
        }

        //录入日期
        if (Validator.isNotEmpty(tmOrderTransCargo.getBeginDate())) {
            queryWrapper.ge(TmOrderTransCargo::getInputDate, tmOrderTransCargo.getBeginDate());
        }

        //录入日期
        if (Validator.isNotEmpty(tmOrderTransCargo.getEndDate())) {
            queryWrapper.le(TmOrderTransCargo::getInputDate, tmOrderTransCargo.getEndDate());
        }

        //运输计划ID
        if(Validator.isNotEmpty(tmOrderTransCargo.getTransId())){
            queryWrapper.eq(TmOrderTransCargo::getTransId,tmOrderTransCargo.getTransId());
        }

        //货物ID
        if(Validator.isNotEmpty(tmOrderTransCargo.getOrderCargoId())){
            queryWrapper.eq(TmOrderTransCargo::getOrderCargoId,tmOrderTransCargo.getOrderCargoId());
        }

        //运输货物
        if(Validator.isNotEmpty(tmOrderTransCargo.getCargoName())){
            queryWrapper.like(TmOrderTransCargo::getCargoName,tmOrderTransCargo.getCargoName());
        }

        //工厂总重
        if(Validator.isNotEmpty(tmOrderTransCargo.getPlantWeight())){
            queryWrapper.eq(TmOrderTransCargo::getPlantWeight,tmOrderTransCargo.getPlantWeight());
        }

        //码头总重
        if(Validator.isNotEmpty(tmOrderTransCargo.getPierWeight())){
            queryWrapper.eq(TmOrderTransCargo::getPierWeight,tmOrderTransCargo.getPierWeight());
        }

        //装货实际毛重
        if(Validator.isNotEmpty(tmOrderTransCargo.getGrossWeightLoading())){
            queryWrapper.eq(TmOrderTransCargo::getGrossWeightLoading,tmOrderTransCargo.getGrossWeightLoading());
        }

        //卸货实际毛重
        if(Validator.isNotEmpty(tmOrderTransCargo.getGrossWeightUnloading())){
            queryWrapper.eq(TmOrderTransCargo::getGrossWeightUnloading,tmOrderTransCargo.getGrossWeightUnloading());
        }


        //磅差
        if(Validator.isNotEmpty(tmOrderTransCargo.getPoundDifference())){
            queryWrapper.eq(TmOrderTransCargo::getPoundDifference,tmOrderTransCargo.getPoundDifference());
        }

        //预计装货重量
        if(Validator.isNotEmpty(tmOrderTransCargo.getLoadWt())){
            queryWrapper.eq(TmOrderTransCargo::getLoadWt,tmOrderTransCargo.getLoadWt());
        }

        //实际装货重量
        if(Validator.isNotEmpty(tmOrderTransCargo.getRealLoadWt())){
            queryWrapper.eq(TmOrderTransCargo::getRealLoadWt,tmOrderTransCargo.getRealLoadWt());
        }

        //实际到货重量
        if(Validator.isNotEmpty(tmOrderTransCargo.getRealTransWt())){
            queryWrapper.eq(TmOrderTransCargo::getRealTransWt,tmOrderTransCargo.getRealTransWt());
        }

        //预计装货体积
        if(Validator.isNotEmpty(tmOrderTransCargo.getLoadVol())){
            queryWrapper.eq(TmOrderTransCargo::getLoadVol,tmOrderTransCargo.getLoadVol());
        }

        //实际装货体积
        if(Validator.isNotEmpty(tmOrderTransCargo.getRealLoadVol())){
            queryWrapper.eq(TmOrderTransCargo::getRealLoadVol,tmOrderTransCargo.getRealLoadVol());
        }

        //实际到货体积
        if(Validator.isNotEmpty(tmOrderTransCargo.getRealTransVol())){
            queryWrapper.eq(TmOrderTransCargo::getRealTransVol,tmOrderTransCargo.getRealTransVol());
        }

        //预计装货件数
        if(Validator.isNotEmpty(tmOrderTransCargo.getLoadNum())){
            queryWrapper.eq(TmOrderTransCargo::getLoadNum,tmOrderTransCargo.getLoadNum());
        }

        //实际装货件数
        if(Validator.isNotEmpty(tmOrderTransCargo.getRealLoadNum())){
            queryWrapper.eq(TmOrderTransCargo::getRealLoadNum,tmOrderTransCargo.getRealLoadNum());
        }

        //实际到货件数
        if(Validator.isNotEmpty(tmOrderTransCargo.getRealTransNum())){
            queryWrapper.eq(TmOrderTransCargo::getRealTransNum,tmOrderTransCargo.getRealTransNum());
        }

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

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

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

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

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

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

    }

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


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

        if(Validator.isNotEmpty(tmOrderTransCargo.getOrderId())){
            queryWrapper.eq(TmOrderTransCargo::getOrderId,tmOrderTransCargo.getOrderId());
        }

        if (Validator.isNotEmpty(tmOrderTransCargo.getBeginDate())) {
            queryWrapper.ge(TmOrderTransCargo::getInputDate, tmOrderTransCargo.getBeginDate());
        }

        if (Validator.isNotEmpty(tmOrderTransCargo.getEndDate())) {
            queryWrapper.le(TmOrderTransCargo::getInputDate, tmOrderTransCargo.getEndDate());
        }

        //运输计划ID
        if(Validator.isNotEmpty(tmOrderTransCargo.getTransId())){
            queryWrapper.eq(TmOrderTransCargo::getTransId,tmOrderTransCargo.getTransId());
        }

        //货物ID
        if(Validator.isNotEmpty(tmOrderTransCargo.getOrderCargoId())){
            queryWrapper.eq(TmOrderTransCargo::getOrderCargoId,tmOrderTransCargo.getOrderCargoId());
        }

        //运输货物
        if(Validator.isNotEmpty(tmOrderTransCargo.getCargoName())){
            queryWrapper.like(TmOrderTransCargo::getCargoName,tmOrderTransCargo.getCargoName());
        }

        //工厂总重
        if(Validator.isNotEmpty(tmOrderTransCargo.getPlantWeight())){
            queryWrapper.eq(TmOrderTransCargo::getPlantWeight,tmOrderTransCargo.getPlantWeight());
        }

        //码头总重
        if(Validator.isNotEmpty(tmOrderTransCargo.getPierWeight())){
            queryWrapper.eq(TmOrderTransCargo::getPierWeight,tmOrderTransCargo.getPierWeight());
        }

        //装货实际毛重
        if(Validator.isNotEmpty(tmOrderTransCargo.getGrossWeightLoading())){
            queryWrapper.eq(TmOrderTransCargo::getGrossWeightLoading,tmOrderTransCargo.getGrossWeightLoading());
        }

        //卸货实际毛重
        if(Validator.isNotEmpty(tmOrderTransCargo.getGrossWeightUnloading())){
            queryWrapper.eq(TmOrderTransCargo::getGrossWeightUnloading,tmOrderTransCargo.getGrossWeightUnloading());
        }


        //磅差
        if(Validator.isNotEmpty(tmOrderTransCargo.getPoundDifference())){
            queryWrapper.eq(TmOrderTransCargo::getPoundDifference,tmOrderTransCargo.getPoundDifference());
        }

        //预计装货重量
        if(Validator.isNotEmpty(tmOrderTransCargo.getLoadWt())){
            queryWrapper.eq(TmOrderTransCargo::getLoadWt,tmOrderTransCargo.getLoadWt());
        }

        //实际装货重量
        if(Validator.isNotEmpty(tmOrderTransCargo.getRealLoadWt())){
            queryWrapper.eq(TmOrderTransCargo::getRealLoadWt,tmOrderTransCargo.getRealLoadWt());
        }

        //实际到货重量
        if(Validator.isNotEmpty(tmOrderTransCargo.getRealTransWt())){
            queryWrapper.eq(TmOrderTransCargo::getRealTransWt,tmOrderTransCargo.getRealTransWt());
        }

        //预计装货体积
        if(Validator.isNotEmpty(tmOrderTransCargo.getLoadVol())){
            queryWrapper.eq(TmOrderTransCargo::getLoadVol,tmOrderTransCargo.getLoadVol());
        }

        //实际装货体积
        if(Validator.isNotEmpty(tmOrderTransCargo.getRealLoadVol())){
            queryWrapper.eq(TmOrderTransCargo::getRealLoadVol,tmOrderTransCargo.getRealLoadVol());
        }

        //实际到货体积
        if(Validator.isNotEmpty(tmOrderTransCargo.getRealTransVol())){
            queryWrapper.eq(TmOrderTransCargo::getRealTransVol,tmOrderTransCargo.getRealTransVol());
        }

        //预计装货件数
        if(Validator.isNotEmpty(tmOrderTransCargo.getLoadNum())){
            queryWrapper.eq(TmOrderTransCargo::getLoadNum,tmOrderTransCargo.getLoadNum());
        }

        //实际装货件数
        if(Validator.isNotEmpty(tmOrderTransCargo.getRealLoadNum())){
            queryWrapper.eq(TmOrderTransCargo::getRealLoadNum,tmOrderTransCargo.getRealLoadNum());
        }

        //实际到货件数
        if(Validator.isNotEmpty(tmOrderTransCargo.getRealTransNum())){
            queryWrapper.eq(TmOrderTransCargo::getRealTransNum,tmOrderTransCargo.getRealTransNum());
        }

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

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

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

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

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

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

        }

}
