package com.bs.jxc.controller;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.bs.jxc.domain.*;
import com.bs.jxc.service.*;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.hutool.core.lang.Validator;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 船舶载货清单Controller
 *
 * @author bs
 * @date 2023-07-10
 */
@Api(tags = "船舶载货清单")
@RestController
@RequestMapping("/jxc/bmcargo")
public class BmVesselCargoController extends BaseController {
    @Resource
    private IBmVesselCargoService bmVesselCargoService;
    @Resource
    private IBmVesselVoyageService bmVesselVoyageService;
    @Resource
    private ICkBillService ckBillService;

    @Resource
    private ICkBillCargoService ckBillCargoService;

    @Resource
    private ICkBillStockService ckBillStockService;
    @Resource
    private ICkCargoStockService ckCargoStockService;


    /**
     * 分页查询船舶载货清单列表
     */
    @ApiOperation("分页查询船舶载货清单列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(BmVesselCargo bmVesselCargo) {
        startPage();
        LambdaQueryWrapper<BmVesselCargo> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,bmVesselCargo);
        List<BmVesselCargo> list = bmVesselCargoService.list(queryWrapper);
        if(!list.isEmpty()&&list.size()>0){
            getStockData(list);
        }
        return getDataTable(list);
    }

    /**
     * 查询船舶载货清单列表
     */
    @ApiOperation("查询船舶载货清单列表")
    @GetMapping("/list")
    public AjaxResult list(BmVesselCargo bmVesselCargo) {
        LambdaQueryWrapper<BmVesselCargo> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,bmVesselCargo);
        List<BmVesselCargo> list = bmVesselCargoService.list(queryWrapper);
        if(!list.isEmpty()&&list.size()>0){
            getStockData(list);
        }
        return success(list);
    }

    private List<BmVesselCargo> getStockData1(List<BmVesselCargo> list) {
        if (null != list && list.size() > 0) {
            List<Long> ids = new ArrayList<>();
            for (BmVesselCargo cargo : list) {
                ids.add(cargo.getId());
            }
            List<CkBillCargo> lsCargo = ckBillCargoService.list(new LambdaQueryWrapper<CkBillCargo>().in(CkBillCargo::getVesselCargoId, ids));
            List<Long> cargoIds = new ArrayList<>();
            Map<Long, CkBillCargo> cargoMap = new HashMap<>();
            Map<Long, CkBillCargo> vesselCargoMap = new HashMap<>();
            for (CkBillCargo cargo : lsCargo) {
                cargoIds.add(cargo.getId());
                cargoMap.put(cargo.getId(), cargo);
                vesselCargoMap.put(cargo.getVesselCargoId(), cargo);
            }
            LambdaQueryWrapper<CkBillStock> wrapper = new LambdaQueryWrapper<>();
            if(cargoIds.size()>0){
                wrapper.in(CkBillStock::getBillCargoId, cargoIds);
                List<CkBillStock> lsSotck = ckBillStockService.list(wrapper);
                if (null != lsSotck && lsSotck.size() > 0) {
                    Map<Long, List<CkBillStock>> stockMap = new HashMap<>();
                    for (CkBillStock stock : lsSotck) {
                        CkBillCargo cc = cargoMap.get(stock.getBillCargoId());
                        List<CkBillStock> tmp = stockMap.get(cc.getVesselCargoId());
                        if (null == tmp) {
                            tmp = new ArrayList<>();
                        }
                        tmp.add(stock);
                        stockMap.put(cc.getVesselCargoId(), tmp);
                    }
                    for (BmVesselCargo cargo : list) {
                        cargo.setBillStockList(stockMap.get(cargo.getId()));
                        CkBillCargo cbc = vesselCargoMap.get(cargo.getId());
                        if (cbc != null) {
                            cargo.setBillNumber(cbc.getBillNumber());
                            cargo.setBillDate(cbc.getBillDate());
                            cargo.setStockNum(cbc.getCargoNum());
                            cargo.setStockVol(cbc.getCargoVol());
                            cargo.setStockWt(cbc.getCargoWt());
                        }
                    }
                }
            }
        }
        return list;
    }


    private void getStockData(List<BmVesselCargo> list) {
        Map<Long,BmVesselCargo> vesselMap = list.stream().collect(Collectors.toMap(o-> o.getId(), o-> o));
        MPJLambdaWrapper<CkBill> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(CkBill.class)
                .selectCollection(CkBillCargo.class,CkBill::getCkBillCargos,billCargo->billCargo.collection(
                        CkBillStock.class,CkBillCargo::getCkBillStocks)).
                leftJoin(CkBillCargo.class,CkBillCargo::getBillId,CkBill::getId).
                leftJoin(CkBillStock.class,CkBillStock::getBillCargoId,CkBillCargo::getId);
        queryWrapper.in(CkBill::getVoyageCargoId,vesselMap.keySet());
        List<CkBill> ckBills = ckBillService.selectJoinList(CkBill.class,queryWrapper);
        ckBills.forEach(ckBill->{
            final BigDecimal[] countNum = {new BigDecimal("0")};
            final BigDecimal[] countVol = {new BigDecimal("0")};
            final BigDecimal[] countWt = {new BigDecimal("0")};
            List<CkBillCargo> ckBillCargos = ckBill.getCkBillCargos();
            List<CkBillStock> stocks = new ArrayList<>();
            ckBillCargos.forEach(ele->{
                List<CkBillStock> ckBillStocks = ele.getCkBillStocks();
                ckBillStocks.forEach(stock->{
                    countNum[0] = countNum[0].add(stock.getCargoNum());
                    countVol[0] = countVol[0].add(stock.getCargoVol());
                    countWt[0] = countWt[0].add(stock.getCargoWt());
                    LambdaQueryWrapper<CkCargoStock> wrapper = new LambdaQueryWrapper();
                    wrapper.eq(CkCargoStock::getCargoId,ele.getCargoId());
                    wrapper.eq(CkCargoStock::getCustId,ele.getCustId());
                    wrapper.eq(CkCargoStock::getStockId,stock.getStockId());
                    //if(ele.getInoutType().equals("2")){
                    wrapper.eq(CkCargoStock::getBillNumber,ele.getBillNumber());
                    //}
                    CkCargoStock ckCargoStock = ckCargoStockService.getOne(wrapper);
                    if(ckCargoStock!=null){
                        stock.setNowStockWt(ckCargoStock.getCargoWt());
                        stock.setNowStockVol(ckCargoStock.getCargoVol());
                        stock.setNowStockNum(ckCargoStock.getCargoNum());
                        stock.setNowCargoValue(ckCargoStock.getCargoValue());
                    }else{
                        stock.setNowStockWt(new BigDecimal(0));
                        stock.setNowStockVol(new BigDecimal(0));
                        stock.setNowStockNum(new BigDecimal(0));
                        stock.setNowCargoValue(new BigDecimal(0));
                    }
                    stock.setCargoId(ele.getCargoId());
                    stocks.add(stock);
                });
            });
            BmVesselCargo bmVesselCargo = vesselMap.get(ckBill.getVoyageCargoId());
            bmVesselCargo.setBillStockList(stocks);
            bmVesselCargo.setBillNumber(ckBill.getBillNumber());
            bmVesselCargo.setBillDate(ckBill.getBillDate());
            bmVesselCargo.setStockNum(countNum[0]);
            bmVesselCargo.setStockVol(countVol[0]);
            bmVesselCargo.setStockWt(countWt[0]);
        });
    }


    /**
     * 导出船舶载货清单列表
     */
    @ApiOperation("导出船舶载货清单列表")
    @Log(title = "船舶载货清单导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmVesselCargo bmVesselCargo) {
        LambdaQueryWrapper<BmVesselCargo> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,bmVesselCargo);
        List<BmVesselCargo> list = bmVesselCargoService.list(queryWrapper);
        ExcelUtil<BmVesselCargo> util = new ExcelUtil<BmVesselCargo>(BmVesselCargo. class);
        util.exportExcel(response, list, "船舶载货清单数据");
    }

    /**
     * 获取船舶载货清单详细信息
     */
    @ApiOperation("获取船舶载货清单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bmVesselCargoService.getById(id));
    }

    /**
     * 新增船舶载货清单（载货清单页面进行保存）
     */
    @ApiOperation("新增船舶载货清单")
    @Log(title = "船舶载货清单新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmVesselCargo bmVesselCargo) {
        bmVesselCargoService.save(bmVesselCargo);
        List<CkBillStock> lsStock = bmVesselCargo.getBillStockList();
        //if (null != lsStock && lsStock.size() > 0) {
        //新增不需要单据信息（还是载货清单）
        //saveBillStock(bmVesselCargo, lsStock);
        //}
        return toAjax(true);
    }

    /**
     * 修改船舶载货清单
     */
    @ApiOperation("修改船舶载货清单")
    @Log(title = "船舶载货清单修改", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult edit(@RequestBody BmVesselCargo bmVesselCargo) {
        bmVesselCargoService.updateById(bmVesselCargo);
        //数量大于0，生成单据，不然还是只做单纯的保存操作
        if((bmVesselCargo.getStockNum()!=null&&bmVesselCargo.getStockNum().compareTo(new BigDecimal("0"))>0)
                ||(bmVesselCargo.getStockWt()!=null&&bmVesselCargo.getStockWt().compareTo(new BigDecimal("0"))>0)
                ||(bmVesselCargo.getStockVol()!=null&&bmVesselCargo.getStockVol().compareTo(new BigDecimal("0"))>0)){
            AjaxResult ret = verifyOrder(bmVesselCargo);
            if(ret.isFail()){
                return ret;
            }
            saveBillStock(bmVesselCargo);
        }else{
            //二次修改的时候，把数量修改成0后，删除单据信息
            LambdaQueryWrapper<CkBill> query =  new LambdaQueryWrapper<>();
            query.eq(CkBill::getVoyageCargoId, bmVesselCargo.getId());
            CkBill ckBill = ckBillService.getOne(query);
            if(ckBill!=null){
                LambdaQueryWrapper<CkBillCargo> wrapper = new LambdaQueryWrapper<>();
                wrapper.select(CkBillCargo::getId);
                wrapper.eq(CkBillCargo::getBillId, ckBill.getId());
                wrapper.eq(CkBillCargo::getVesselCargoId, bmVesselCargo.getId());
                List<Object> ids = ckBillCargoService.listObjs(wrapper);
                ckBillCargoService.remove(wrapper);
                // 先删除billStock数据
                if(ids.size()>0){
                    LambdaQueryWrapper<CkBillStock> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.in(CkBillStock::getBillCargoId, ids);
                    ckBillStockService.remove(lambdaQueryWrapper);
                }
            }
        }
        return toAjax(true);
    }

    private void saveBillStock(BmVesselCargo bmVesselCargo) {
        List<CkBillStock> lsStock = bmVesselCargo.getBillStockList();
        LambdaQueryWrapper<CkBill> query =  new LambdaQueryWrapper<>();
        query.eq(CkBill::getVoyageCargoId, bmVesselCargo.getId());
        CkBill ckBill = ckBillService.getOne(query);
        //BmVesselVoyage voyage = bmVesselVoyageService.getById(bmVesselCargo.getVoyageId());
        // 判断业务表是否有生成，第一次未生成，则生成业务表
        if (null == ckBill) {
            ckBill = new CkBill();
            //ckBill.setBillStatus("0");
            ckBill.setStatus("0");
            ckBill.setSourceType("0");
        }
        ckBill.setBillType(bmVesselCargo.getOpType());
        ckBill.setBillDate(bmVesselCargo.getBillDate());
        ckBill.setBillNumber(bmVesselCargo.getBillNumber());
        ckBill.setBillNo(bmVesselCargo.getBillNo());
        ckBill.setVesselId(bmVesselCargo.getVesselId());
        ckBill.setVesselName(bmVesselCargo.getVesselName());
        ckBill.setVoyageId(bmVesselCargo.getVoyageId());
        ckBill.setVoyageCargoId(bmVesselCargo.getId());
        ckBill.setVoyageNo(bmVesselCargo.getVoyageNo());
        ckBillService.saveOrUpdate(ckBill);

        LambdaQueryWrapper<CkBillCargo> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(CkBillCargo::getId);
        wrapper.eq(CkBillCargo::getBillId, ckBill.getId());
        wrapper.eq(CkBillCargo::getVesselCargoId, bmVesselCargo.getId());
        List<Object> ids = ckBillCargoService.listObjs(wrapper);
        ckBillCargoService.remove(wrapper);
        // 先删除billStock数据
        if(ids.size()>0){
            LambdaQueryWrapper<CkBillStock> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(CkBillStock::getBillCargoId, ids);
            ckBillStockService.remove(lambdaQueryWrapper);
        }
        for (CkBillStock stock : lsStock) {
            CkBillCargo ckBillCargo = new CkBillCargo();
            BeanUtils.copyProperties(bmVesselCargo, ckBillCargo, "id");
            ckBillCargo.setBillId(ckBill.getId());
            ckBillCargo.setBillNo(ckBill.getBillNo());
            ckBillCargo.setInoutType(ckBill.getBillType());
            ckBillCargo.setVoyageId(ckBill.getVoyageId());
            ckBillCargo.setVoyageNo(ckBill.getVoyageNo());
            ckBillCargo.setVesselId(ckBill.getVesselId());
            ckBillCargo.setVesselName(ckBill.getVesselName());
            ckBillCargo.setVesselCargoId(bmVesselCargo.getId());
            ckBillCargo.setUnit(bmVesselCargo.getUnit());
            ckBillCargo.setFirstUnit(bmVesselCargo.getFirstUnit());
            ckBillCargo.setCurrType(bmVesselCargo.getCurrType());
            ckBillCargo.setUnitPrice(bmVesselCargo.getUnitPrice());
            ckBillCargoService.save(ckBillCargo);
            stock.setBillCargoId(ckBillCargo.getId());
            stock.setInoutType(ckBillCargo.getInoutType());
            if (Validator.isEmpty(stock.getBillNumber())) {
                stock.setBillNumber(ckBillCargo.getBillNumber());
            }
            stock.setId(null);
        }
        ckBillStockService.saveBatch(lsStock);
    }

   /** 修改船舶载货清单（确认订单）
     * 当再确认订单页面进行确认的时候，判断实际交接量参数
     * 进行生成订单
     */
    @ApiOperation("修改船舶载货清单")
    @Log(title = "船舶载货清单修改", businessType = BusinessType.UPDATE)
    @PutMapping("/confirmOrder")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult confirmOrder(@RequestBody BmVesselCargo bmVesselCargo) {
        AjaxResult ajaxResult =verifyOrder(bmVesselCargo);
        if(ajaxResult.isFail()){
            return ajaxResult;
        }
        if(bmVesselCargo.getStockNum().compareTo(new BigDecimal("0"))>0
        ||bmVesselCargo.getStockWt().compareTo(new BigDecimal("0"))>0
        ||bmVesselCargo.getStockVol().compareTo(new BigDecimal("0"))>0){
            bmVesselCargoService.outPutWarehouse(bmVesselCargo);
        }
        return toAjax(bmVesselCargoService.updateById(bmVesselCargo));
    }

    /**
     * 获取船舶载货清单详细信息
     */
    @ApiOperation("获取船舶载货清单详细信息")
    @GetMapping(value = "/confirmOrder/{id}")
    public AjaxResult getConfirmOrder(@PathVariable("id") Long id) {
        BmVesselCargo vesselCargo = bmVesselCargoService.getById(id);
        List<CkBillCargo> ckBillCargos= ckBillCargoService.list(new LambdaQueryWrapper<CkBillCargo>().eq(CkBillCargo::getBillNo,vesselCargo.getBillNo()));
        if(!ckBillCargos.isEmpty()&&ckBillCargos.size()>0){
            List<CkBillStock> ckBillStocks = new ArrayList<>();
            ckBillCargos.forEach(ckBillCargo->{
                CkBillStock ckBillStock = ckBillStockService.getOne(new LambdaQueryWrapper<CkBillStock>().eq(CkBillStock::getBillCargoId,ckBillCargo.getId()));
                QueryWrapper<CkCargoStock> wrapper=new QueryWrapper<>();
                wrapper.select("sum(cargo_num) as cargo_num",
                        "sum(cargo_wt) as cargo_wt",
                        "sum(cargo_value) as cargo_value",
                        "sum(cargo_vol) as cargo_vol");
                wrapper.lambda().eq(Validator.isNotEmpty(ckBillStock.getBillNumber()),CkCargoStock::getBillNumber,ckBillStock.getBillNumber())
                        .eq(CkCargoStock::getStockId,ckBillStock.getStockId())
                        .eq(CkCargoStock::getCargoId,ckBillCargo.getCargoId())
                        .eq(CkCargoStock::getCustId,ckBillCargo.getCustId());
//                .groupBy(CkCargoStock::getCargoId);
                CkCargoStock ckCargoStock = ckCargoStockService.getOne(wrapper);
                ckBillStock.setNowStockNum(ckCargoStock.getCargoNum());
                ckBillStock.setNowStockWt(ckCargoStock.getCargoWt());
                ckBillStock.setNowStockVol(ckCargoStock.getCargoVol());
                ckBillStock.setNowCargoValue(ckCargoStock.getCargoValue());
                ckBillStocks.add(ckBillStock);
            });
            vesselCargo.setBillStockList(ckBillStocks);
            vesselCargo.setBillNumber(ckBillCargos.get(0).getBillNumber());
        }
        return success(vesselCargo);
    }




    /**
     * 删除船舶载货清单
     */
    @ApiOperation("删除船舶载货清单")
    @Log(title = "船舶载货清单删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        LambdaQueryWrapper<BmVesselCargo> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(BmVesselCargo::getVoyageId);
        wrapper.in(BmVesselCargo::getId, ids);
        List<Object> voyageIds = bmVesselCargoService.listObjs(wrapper);
        LambdaQueryWrapper<BmVesselVoyage> voyageWrapper = new LambdaQueryWrapper<>();
        voyageWrapper.select(BmVesselVoyage::getId);
        voyageWrapper.in(BmVesselVoyage::getId, voyageIds);
        voyageWrapper.eq(BmVesselVoyage::getStatus, "1");
        List<BmVesselVoyage> bmVesselVoyages =  bmVesselVoyageService.list(voyageWrapper);
        if(bmVesselVoyages.size()>0){
            return error("载货清单已经被确认，生成出入库信息，不允许删除");
        }
        return toAjax(bmVesselCargoService.removeBatchByIds(ids));
    }



    public AjaxResult verifyOrder(BmVesselCargo bmVesselCargo){
        if(bmVesselCargo.getActNum()==null){
            return AjaxResult.error("实际交接件数不能为空");
        }
        if(bmVesselCargo.getActVol()==null){
            return AjaxResult.error("实际交接体积不能为空");
        }
        if(bmVesselCargo.getActWt()==null){
            return AjaxResult.error("实际交接重量不能为空");
        }
        if(bmVesselCargo.getZtNum()==null){
            return AjaxResult.error("直提件数不能为空");
        }
        if(bmVesselCargo.getZtVol()==null){
            return AjaxResult.error("直提体积不能为空");
        }
        if(bmVesselCargo.getZtWt()==null){
            return AjaxResult.error("直提重量不能为空");
        }
        if(bmVesselCargo.getStockNum()==null){
            return AjaxResult.error("出入库件数不能为空");
        }
        if(bmVesselCargo.getStockWt()==null){
            return AjaxResult.error("出入库重量不能为空");
        }
        if(bmVesselCargo.getStockVol()==null){
            return AjaxResult.error("出入库体积不能为空");
        }
        CkBill tmpCkBill = ckBillService.getOne(new LambdaQueryWrapper<CkBill>().eq(CkBill::getBillNumber, bmVesselCargo.getBillNumber()));
        CkBill ckBill = ckBillService.getOne(new LambdaQueryWrapper<CkBill>().eq(CkBill::getVoyageCargoId, bmVesselCargo.getId()));
        if(tmpCkBill!=null){
            if(ckBill != null){
                if(!tmpCkBill.getId().equals(ckBill.getId())){
                    return error("单据编号已经存在，请重新输入");
                }
            }else{
                return error("单据编号已经存在，请重新输入");
            }
        }

        if(bmVesselCargo.getBillStockList()==null||bmVesselCargo.getBillStockList().size()==0){
            return AjaxResult.error("请添加出（入）库信息");
        }
        for (int i = 0; i < bmVesselCargo.getBillStockList().size(); i++) {
            CkBillStock stock = bmVesselCargo.getBillStockList().get(i);
            if(stock.getStockId()==null){
                return AjaxResult.error("出入库信息，仓库必填");
            }
            //出口
            if(bmVesselCargo.getOpType().equals("2")){
                if(Validator.isEmpty(stock.getBillNumber())){
                    return AjaxResult.error("出库信息，出库单据必选");
                }
            }
            if(stock.getCargoNum().compareTo(new BigDecimal("0"))==0&&
                    stock.getCargoWt().compareTo(new BigDecimal("0"))==0&&
                    stock.getCargoVol().compareTo(new BigDecimal("0"))==0){
                return AjaxResult.error("出入库信息，出入库量必需大于0");
            }
        }


        return success();
    }

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


        if (null != bmVesselCargo.getBeginDateTwo()) {
            queryWrapper.between(BmVesselCargo::getExpiryDate, bmVesselCargo.getBeginDateTwo(), bmVesselCargo.getEndDateTwo());
        }

        //船舶航次ID
        if(bmVesselCargo.getVoyageId() != null){
            queryWrapper.eq(BmVesselCargo::getVoyageId,bmVesselCargo.getVoyageId());
        }

        //报关单号
        if (bmVesselCargo.getCustomsNo() != null) {
            queryWrapper.like(BmVesselCargo::getCustomsNo, bmVesselCargo.getCustomsNo());
        }

        //核注清单号
        if (Validator.isNotEmpty(bmVesselCargo.getListNumber())) {
            queryWrapper.like(BmVesselCargo::getListNumber, bmVesselCargo.getListNumber());
        }


        //航次号
        if(Validator.isNotEmpty(bmVesselCargo.getVoyageNo())){
            queryWrapper.like(BmVesselCargo::getVoyageNo,bmVesselCargo.getVoyageNo());
        }

        //船舶ID
        if(bmVesselCargo.getVesselId() != null){
            queryWrapper.eq(BmVesselCargo::getVesselId,bmVesselCargo.getVesselId());
        }

        //船舶名称
        if(Validator.isNotEmpty(bmVesselCargo.getVesselName())){
            queryWrapper.like(BmVesselCargo::getVesselName,bmVesselCargo.getVesselName());
        }

        //货物ID
        if(bmVesselCargo.getCargoId() != null){
            queryWrapper.eq(BmVesselCargo::getCargoId,bmVesselCargo.getCargoId());
        }

        //货物自然序号
        if(Validator.isNotEmpty(bmVesselCargo.getCargoNumber())){
            queryWrapper.like(BmVesselCargo::getCargoNumber,bmVesselCargo.getCargoNumber());
        }

        //商品料号
        if(Validator.isNotEmpty(bmVesselCargo.getItemNumber())){
            queryWrapper.like(BmVesselCargo::getItemNumber,bmVesselCargo.getItemNumber());
        }

        //电子账册项号
        if(Validator.isNotEmpty(bmVesselCargo.getBookNumber())){
            queryWrapper.like(BmVesselCargo::getBookNumber,bmVesselCargo.getBookNumber());
        }


        //queryWrapper.inSql(BmVesselCargo::getCargoId, "select cargo_id from cm_cargo where name like '%" +bmVesselCargo.getCargoName() + "%'");

        //HS编号
        if(Validator.isNotEmpty(bmVesselCargo.getHsCode())){
            queryWrapper.like(BmVesselCargo::getHsCode,bmVesselCargo.getHsCode());
        }

        //货物名称
        if(Validator.isNotEmpty(bmVesselCargo.getCargoName())){
            queryWrapper.like(BmVesselCargo::getCargoName,bmVesselCargo.getCargoName());
        }

        //提运单号
        if(Validator.isNotEmpty(bmVesselCargo.getBillNo())){
            queryWrapper.like(BmVesselCargo::getBillNo,bmVesselCargo.getBillNo());
        }

        //货主ID
        if(bmVesselCargo.getCustId() != null){
            queryWrapper.eq(BmVesselCargo::getCustId,bmVesselCargo.getCustId());
        }

        //货主名称
        if(Validator.isNotEmpty(bmVesselCargo.getCustName())){
            queryWrapper.like(BmVesselCargo::getCustName,bmVesselCargo.getCustName());
        }

        //业务性质
        if(Validator.isNotEmpty(bmVesselCargo.getOpType())){
            queryWrapper.eq(BmVesselCargo::getOpType,bmVesselCargo.getOpType());
        }

        //贸易性质
        if(Validator.isNotEmpty(bmVesselCargo.getTradType())){
            queryWrapper.eq(BmVesselCargo::getTradType,bmVesselCargo.getTradType());
        }

        //货物件数
        if(bmVesselCargo.getCargoNum() != null){
            queryWrapper.eq(BmVesselCargo::getCargoNum,bmVesselCargo.getCargoNum());
        }

        //货物重量
        if(bmVesselCargo.getCargoWt() != null){
            queryWrapper.eq(BmVesselCargo::getCargoWt,bmVesselCargo.getCargoWt());
        }

        //货物体积
        if(bmVesselCargo.getCargoVol() != null){
            queryWrapper.eq(BmVesselCargo::getCargoVol,bmVesselCargo.getCargoVol());
        }

        //唛头
        if(Validator.isNotEmpty(bmVesselCargo.getMark())){
            queryWrapper.eq(BmVesselCargo::getMark,bmVesselCargo.getMark());
        }

        //包装类型
        if(Validator.isNotEmpty(bmVesselCargo.getPackType())){
            queryWrapper.like(BmVesselCargo::getPackType,bmVesselCargo.getPackType());
        }

        //计量单位
        if(Validator.isNotEmpty(bmVesselCargo.getUnit())){
            queryWrapper.eq(BmVesselCargo::getUnit,bmVesselCargo.getUnit());
        }

        //舱口类型
        if(Validator.isNotEmpty(bmVesselCargo.getHatchTpe())){
            queryWrapper.like(BmVesselCargo::getHatchTpe,bmVesselCargo.getHatchTpe());
        }

        //船港货机械
        if(Validator.isNotEmpty(bmVesselCargo.getCargoMachine())){
            queryWrapper.like(BmVesselCargo::getCargoMachine,bmVesselCargo.getCargoMachine());
        }

        //单件重量
        if(bmVesselCargo.getPieceWeight() != null){
            queryWrapper.eq(BmVesselCargo::getPieceWeight,bmVesselCargo.getPieceWeight());
        }

        //实际交接件数
        if(bmVesselCargo.getActNum() != null){
            queryWrapper.eq(BmVesselCargo::getActNum,bmVesselCargo.getActNum());
        }

        //实际交接重量
        if(bmVesselCargo.getActWt() != null){
            queryWrapper.eq(BmVesselCargo::getActWt,bmVesselCargo.getActWt());
        }

        //实际交接体积
        if(bmVesselCargo.getActVol() != null){
            queryWrapper.eq(BmVesselCargo::getActVol,bmVesselCargo.getActVol());
        }

        //其中直提件数
        if(bmVesselCargo.getZtNum() != null){
            queryWrapper.eq(BmVesselCargo::getZtNum,bmVesselCargo.getZtNum());
        }

        //其中直提重量
        if(bmVesselCargo.getZtWt() != null){
            queryWrapper.eq(BmVesselCargo::getZtWt,bmVesselCargo.getZtWt());
        }

        //其中直提体积
        if(bmVesselCargo.getZtVol() != null){
            queryWrapper.eq(BmVesselCargo::getZtVol,bmVesselCargo.getZtVol());
        }

        //其中出入库件数
        if(bmVesselCargo.getStockNum()!= null){
            queryWrapper.eq(BmVesselCargo::getStockNum,bmVesselCargo.getStockNum());
        }

        //其中出入库重量
        if(bmVesselCargo.getStockWt()!= null){
            queryWrapper.eq(BmVesselCargo::getStockWt,bmVesselCargo.getStockWt());
        }

        //其中出入库体积
        if(bmVesselCargo.getStockVol() != null){
            queryWrapper.eq(BmVesselCargo::getStockVol,bmVesselCargo.getStockVol());
        }

        //所属公司
        if(bmVesselCargo.getDeptId()!= null){
            queryWrapper.eq(BmVesselCargo::getDeptId,bmVesselCargo.getDeptId());
        }

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

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


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

        }

}
