package com.bs.jxc.controller;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.ICkBillCargoService;
import com.bs.jxc.service.ICkCargoStockService;
import com.bs.system.service.ISysConfigService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 出入库单据明细Controller
 *
 * @author bs
 * @date 2023-07-10
 */
@Api(tags = "出入库单据明细")
@RestController
@RequestMapping("/jxc/ckcargo")
public class CkBillCargoController extends BaseController {
    @Resource
    private ICkBillCargoService ckBillCargoService;
    @Resource
    private ICkCargoStockService ckCargoStockService;
    @Resource
    private ISysConfigService configService;
    /**
     * 分页查询出入库单据明细列表
     */
    @ApiOperation("分页查询出入库单据明细列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CkBillCargo ckBillCargo) {
        startPage();
        MPJLambdaWrapper<CkBillCargo> ckBillCargoMPJLambdaWrapper = buildQueryWrapper(ckBillCargo);
        List<CkBillCargo> list = ckBillCargoService.selectJoinList(CkBillCargo.class,ckBillCargoMPJLambdaWrapper);
        return getDataTable(list);
    }

    public MPJLambdaWrapper<CkBillCargo> buildQueryWrapper(CkBillCargo ckBillCargo) {
        MPJLambdaWrapper<CkBillCargo> queryWrapper = new MPJLambdaWrapper();
        queryWrapper.select(CkBillCargo::getId, CkBillCargo::getBillStatus, CkBillCargo::getSourceType
                , CkBillCargo::getBillId, CkBillCargo::getBillDate, CkBillCargo::getBillNumber
                ,CkBillCargo::getInoutType,CkBillCargo::getVoyageId,CkBillCargo::getVoyageNo
                ,CkBillCargo::getVesselId,CkBillCargo::getVesselName,CkBillCargo::getVesselCargoId
                ,CkBillCargo::getCargoId,CkBillCargo::getHsCode,CkBillCargo::getCargoName
                ,CkBillCargo::getBillNo,CkBillCargo::getCargoSpec,CkBillCargo::getOriginCountry
                ,CkBillCargo::getCustId,CkBillCargo::getCustName,CkBillCargo::getCargoNumber
                ,CkBillCargo::getItemNumber,CkBillCargo::getBookNumber,CkBillCargo::getExpiryDate
                ,CkBillCargo::getOpType,CkBillCargo::getTradType,CkBillCargo::getCustomsNo
                ,CkBillCargo::getListNumber)
                .select(CkBill::getBillNumber)
                .select(CkBillStock::getStockName,CkBillStock::getCargoNum,CkBillStock::getCargoWt,CkBillStock::getCargoVol,CkBillStock::getCargoValue,CkBillStock::getStockCode)
                .selectAs(CkBillStock::getBillNumber,CkBillCargo::getOutBillNumber)
                .leftJoin(CkBill.class, CkBill::getId, CkBillCargo::getBillId)
                .leftJoin(CkBillStock.class,CkBillStock::getBillCargoId,CkBillCargo::getId);
        if (Validator.isNotEmpty(ckBillCargo.getInoutType())) {
            if ("3".equals(ckBillCargo.getInoutType())) {
                // 查询转场数据
                queryWrapper.inSql(CkBillCargo::getBillId, "select id from ck_bill where bill_type = '3'");
            } else if ("4".equals(ckBillCargo.getInoutType())) {
                // 查询混配数据
                queryWrapper.inSql(CkBillCargo::getBillId, "select id from ck_bill where bill_type = '4'");
            } else {
                queryWrapper.eq(CkBillCargo::getInoutType, ckBillCargo.getInoutType());
            }
        }
        //出入库的页面需要查询所有
        if(StringUtils.isEmpty(ckBillCargo.getInoutType())||(!ckBillCargo.getInoutType().equals("1")&&!ckBillCargo.getInoutType().equals("2"))){
            queryWrapper.eq(CkBillCargo::getBillStatus,"1");
        }
        queryWrapper.eq(ckBillCargo.getBillId()!=null,CkBillCargo::getBillId,ckBillCargo.getBillId());
        queryWrapper.between(null != ckBillCargo.getBeginDate(),CkBillCargo::getBillDate, ckBillCargo.getBeginDate(), adjustEndDate(ckBillCargo.getEndDate()));
        queryWrapper.between(null != ckBillCargo.getBeginDateTwo(),CkBillCargo::getExpiryDate, ckBillCargo.getBeginDateTwo(), adjustEndDate(ckBillCargo.getEndDateTwo()));
        //单据编号
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getBillNumber()),CkBillCargo::getBillNumber,ckBillCargo.getBillNumber());
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getVesselName()),CkBillCargo::getVesselName,ckBillCargo.getVesselName());
        //货物名称
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getCargoName()),CkBillCargo::getCargoName,ckBillCargo.getCargoName());
        //货主名称
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getCustName()),CkBillCargo::getCustName,ckBillCargo.getCustName());
        //航次号
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getVoyageNo()),CkBillCargo::getVoyageNo,ckBillCargo.getVoyageNo());
        //核注清单号和报关单号，电子账册项号
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getCustomsNo()),CkBillCargo::getCustomsNo,ckBillCargo.getCustomsNo());
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getListNumber()),CkBillCargo::getListNumber,ckBillCargo.getListNumber());
        queryWrapper.eq(Validator.isNotEmpty(ckBillCargo.getBookNumber()),CkBillCargo::getBookNumber,ckBillCargo.getBookNumber());
        //业务性质和贸易性质
        queryWrapper.eq(Validator.isNotEmpty(ckBillCargo.getOpType()),CkBillCargo::getOpType,ckBillCargo.getOpType());
        queryWrapper.eq(Validator.isNotEmpty(ckBillCargo.getTradType()),CkBillCargo::getTradType,ckBillCargo.getTradType());
        queryWrapper.eq(Validator.isNotEmpty(ckBillCargo.getStockCode()),CkBillStock::getStockCode,ckBillCargo.getStockCode());
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getStockName()),CkBillStock::getStockName,ckBillCargo.getStockName());
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getOutBillNumber()),CkBillStock::getBillNumber,ckBillCargo.getOutBillNumber());
        queryWrapper.orderByDesc(CkBillCargo::getBillDate);
        return queryWrapper;
    }

    /**
     * 查询总重量
     */
    @ApiOperation("查询总重量")
    @GetMapping("/selectTotalWt")
    public AjaxResult selectTotalWt(CkBillCargo ckBillCargo) {
        MPJLambdaWrapper<CkBillCargo> ckBillCargoMPJLambdaWrapper = buildQueryWrapper(ckBillCargo);
        List<CkBillCargo> list = ckBillCargoService.selectJoinList(CkBillCargo.class,ckBillCargoMPJLambdaWrapper);
        BigDecimal totalWeight = list.stream()
                .map(CkBillCargo::getCargoWt)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        return success(totalWeight);
    }

    public static Date adjustEndDate(Date endDate) {
        if (endDate != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(endDate);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar.getTime();
        }
        return null;
    }

    /**
     * 查询出入库单据明细列表
     */
    @ApiOperation("查询出入库单据明细列表")
    @GetMapping("/list")
    public AjaxResult list(CkBillCargo ckBillCargo) {
        LambdaQueryWrapper<CkBillCargo> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ckBillCargo);
        List<CkBillCargo> list = ckBillCargoService.list(queryWrapper);
        return success(list);
    }

    /**
     * 查询即将过期出库单据明细列表
     */
    @ApiOperation("查询即将过期出库单据明细列表")
    @GetMapping("/pageListByBillCargoWithExpire")
    public TableDataInfo pageListByBillCargoWithExpire(CkBillCargo ckBillCargo) {
        String outBillCargoExpiryDateWarn = configService.selectConfigByKey("outBillCargoExpiryDateWarn");
        if (outBillCargoExpiryDateWarn == null || outBillCargoExpiryDateWarn.isEmpty()) {
            outBillCargoExpiryDateWarn = "30";
        }
        startPage();
        LambdaQueryWrapper<CkBillCargo> queryWrapper = new LambdaQueryWrapper();
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);
        LocalDate expirationLimit = today.plusDays(Long.parseLong(outBillCargoExpiryDateWarn));
        queryWrapper.le(CkBillCargo::getExpiryDate, expirationLimit)
                .gt(CkBillCargo::getExpiryDate, yesterday);
        condition(queryWrapper,ckBillCargo);
        List<CkBillCargo> list = ckBillCargoService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出出入库单据明细列表
     */
    @ApiOperation("导出出入库单据明细列表")
    @Log(title = "出入库单据明细导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CkBillCargo ckBillCargo) {
        MPJLambdaWrapper<CkBillCargo> queryWrapper = new MPJLambdaWrapper();
        queryWrapper.select(CkBillCargo::getId, CkBillCargo::getBillStatus, CkBillCargo::getSourceType
                , CkBillCargo::getBillId, CkBillCargo::getBillDate, CkBillCargo::getBillNumber
                ,CkBillCargo::getInoutType,CkBillCargo::getVoyageId,CkBillCargo::getVoyageNo
                ,CkBillCargo::getVesselId,CkBillCargo::getVesselName,CkBillCargo::getVesselCargoId
                ,CkBillCargo::getCargoId,CkBillCargo::getHsCode,CkBillCargo::getCargoName
                ,CkBillCargo::getBillNo,CkBillCargo::getCargoSpec,CkBillCargo::getOriginCountry
                ,CkBillCargo::getCustId,CkBillCargo::getCustName,CkBillCargo::getCargoNumber
                ,CkBillCargo::getItemNumber,CkBillCargo::getBookNumber,CkBillCargo::getExpiryDate
                ,CkBillCargo::getOpType,CkBillCargo::getTradType,CkBillCargo::getCustomsNo
                ,CkBillCargo::getListNumber)
                .select(CkBill::getBillNumber)
                .select(CkBillStock::getStockName,CkBillStock::getCargoNum,CkBillStock::getCargoWt,CkBillStock::getCargoVol,CkBillStock::getCargoValue,CkBillStock::getStockCode)
                .selectAs(CkBillStock::getBillNumber,CkBillCargo::getOutBillNumber)
                .leftJoin(CkBill.class, CkBill::getId, CkBillCargo::getBillId)
                .leftJoin(CkBillStock.class,CkBillStock::getBillCargoId,CkBillCargo::getId);
        if (Validator.isNotEmpty(ckBillCargo.getInoutType())) {
            if ("3".equals(ckBillCargo.getInoutType())) {
                // 查询转场数据
                queryWrapper.inSql(CkBillCargo::getBillId, "select id from ck_bill where bill_type = '3'");
            } else if ("4".equals(ckBillCargo.getInoutType())) {
                // 查询混配数据
                queryWrapper.inSql(CkBillCargo::getBillId, "select id from ck_bill where bill_type = '4'");
            } else {
                queryWrapper.eq(CkBillCargo::getInoutType, ckBillCargo.getInoutType());
            }
        }
        //出入库的页面需要查询所有
        if(StringUtils.isEmpty(ckBillCargo.getInoutType())||(!ckBillCargo.getInoutType().equals("1")&&!ckBillCargo.getInoutType().equals("2"))){
            queryWrapper.eq(CkBillCargo::getBillStatus,"1");
        }
        queryWrapper.eq(ckBillCargo.getBillId()!=null,CkBillCargo::getBillId,ckBillCargo.getBillId());
        queryWrapper.between(null != ckBillCargo.getBeginDate(),CkBillCargo::getBillDate, ckBillCargo.getBeginDate(), ckBillCargo.getEndDate());
        queryWrapper.between(null != ckBillCargo.getBeginDateTwo(),CkBillCargo::getExpiryDate, ckBillCargo.getBeginDateTwo(), ckBillCargo.getEndDateTwo());
        //单据编号
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getBillNumber()),CkBillCargo::getBillNumber,ckBillCargo.getBillNumber());
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getVesselName()),CkBillCargo::getVesselName,ckBillCargo.getVesselName());
        //货物名称
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getCargoName()),CkBillCargo::getCargoName,ckBillCargo.getCargoName());
        //货主名称
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getCustName()),CkBillCargo::getCustName,ckBillCargo.getCustName());
        //航次号
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getVoyageNo()),CkBillCargo::getVoyageNo,ckBillCargo.getVoyageNo());
        //核注清单号和报关单号，电子账册项号
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getCustomsNo()),CkBillCargo::getCustomsNo,ckBillCargo.getCustomsNo());
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getListNumber()),CkBillCargo::getListNumber,ckBillCargo.getListNumber());
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getBookNumber()),CkBillCargo::getBookNumber,ckBillCargo.getBookNumber());
        //业务性质和贸易性质
        queryWrapper.eq(Validator.isNotEmpty(ckBillCargo.getOpType()),CkBillCargo::getOpType,ckBillCargo.getOpType());
        queryWrapper.eq(Validator.isNotEmpty(ckBillCargo.getTradType()),CkBillCargo::getTradType,ckBillCargo.getTradType());
        queryWrapper.eq(Validator.isNotEmpty(ckBillCargo.getStockCode()),CkBillStock::getStockCode,ckBillCargo.getStockCode());
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getStockName()),CkBillStock::getStockName,ckBillCargo.getStockName());
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getOutBillNumber()),CkBillStock::getBillNumber,ckBillCargo.getOutBillNumber());
        queryWrapper.orderByDesc(CkBillCargo::getBillDate);
        List<CkBillCargo> list = ckBillCargoService.selectJoinList(CkBillCargo.class,queryWrapper);
        ExcelUtil<CkBillCargo> util = new ExcelUtil<CkBillCargo>(CkBillCargo. class);
        util.exportExcel(response, list, "出入库单据明细数据");
    }

    /**
     * 获取出入库单据明细详细信息
     */
    @ApiOperation("获取出入库单据明细详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        MPJLambdaWrapper<CkBillCargo> queryWrapper = new MPJLambdaWrapper();
        queryWrapper.selectAll(CkBillCargo.class)
                .select(CkBillStock::getStockName,CkBillStock::getStockCode)
                .selectAs(CkBillStock::getStockId,CkBillCargo::getInStockId)
                .selectAs(CkBillStock::getBillNumber,CkBillCargo::getOutBillNumber)
                .leftJoin(CkBillStock.class,CkBillStock::getBillCargoId,CkBillCargo::getId);
        queryWrapper.eq(CkBillCargo::getId,id);
        CkBillCargo ckBillCargo = ckBillCargoService.selectJoinOne(CkBillCargo.class,queryWrapper);
        if(ckBillCargo.getInoutType().equals("2")){
            ckBillCargo.setOutStockId(ckBillCargo.getInStockId());
            LambdaQueryWrapper<CkCargoStock> wrapper = new LambdaQueryWrapper();
            wrapper.eq(CkCargoStock::getCargoId,ckBillCargo.getCargoId());
            wrapper.eq(CkCargoStock::getCustId,ckBillCargo.getCustId());
            wrapper.eq(CkCargoStock::getStockId,ckBillCargo.getOutStockId());
            wrapper.eq(CkCargoStock::getBillNumber,ckBillCargo.getOutBillNumber());
            CkCargoStock ckCargoStock = ckCargoStockService.getOne(wrapper);
            if(ckCargoStock!=null) {
                ckBillCargo.setNowStockWt(ckCargoStock.getCargoWt());
                ckBillCargo.setNowStockVol(ckCargoStock.getCargoVol());
                ckBillCargo.setNowStockNum(ckCargoStock.getCargoNum());
                ckBillCargo.setNowCargoValue(ckCargoStock.getCargoValue());
            }else{
                ckBillCargo.setNowStockWt(new BigDecimal(0));
                ckBillCargo.setNowStockVol(new BigDecimal(0));
                ckBillCargo.setNowStockNum(new BigDecimal(0));
                ckBillCargo.setNowCargoValue(new BigDecimal(0));
            }
        }
        return success(ckBillCargo);
    }

    /**
     * 新增出入库单据明细
     */
    @ApiOperation("新增出入库单据明细")
    @Log(title = "出入库单据明细新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CkBillCargo ckBillCargo) {
        return toAjax(ckBillCargoService.save(ckBillCargo));
    }

    /**
     * 修改出入库单据明细
     */
    @ApiOperation("修改出入库单据明细")
    @Log(title = "出入库单据明细修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CkBillCargo ckBillCargo) {
        return toAjax(ckBillCargoService.updateById(ckBillCargo));
    }

    /**
     * 删除出入库单据明细
     */
    @ApiOperation("删除出入库单据明细")
    @Log(title = "出入库单据明细删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(ckBillCargoService.removeBatchByIds(ids));
    }

    @ApiOperation("分页查询出入库单据明细列表")
    @GetMapping("/queryCkcargo")
    public TableDataInfo queryCkcargo(CkBillCargo ckBillCargo) {
        startPage();
        MPJLambdaWrapper<CkBillCargo> queryWrapper = new MPJLambdaWrapper();
        queryWrapper.select(CkBillCargo::getHsCode, CkBillCargo::getCargoName, CkBillCargo::getCargoSpec,
                CkBillCargo::getOriginCountry, CkBillCargo::getBillDate, CkBillCargo::getVesselName,
                CkBillCargo::getVoyageNo, CkBillCargo::getInoutType);
        queryWrapper.select(CkBillStock::getStockCode, CkBillStock::getStockName);
        queryWrapper.selectSum(CkBillStock::getCargoWt, "cargoWt");
        queryWrapper.leftJoin(CkBillStock.class,CkBillStock::getBillCargoId,CkBillCargo::getId);
        List<String> inoutType = new ArrayList<>();
        inoutType.add("1");
        inoutType.add("2");
        queryWrapper.in(CkBillCargo::getInoutType, inoutType);
        queryWrapper.eq(CkBillCargo::getBillStatus,"1");
        queryWrapper.between(null != ckBillCargo.getBeginDate(),CkBillCargo::getBillDate, ckBillCargo.getBeginDate(), ckBillCargo.getEndDate());
        queryWrapper.between(null != ckBillCargo.getBeginDateTwo(),CkBillCargo::getExpiryDate, ckBillCargo.getBeginDateTwo(), ckBillCargo.getEndDateTwo());

        //单据编号
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getBillNumber()),CkBillCargo::getBillNumber,ckBillCargo.getBillNumber());
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getVesselName()),CkBillCargo::getVesselName,ckBillCargo.getVesselName());
        //货物名称
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getCargoName()),CkBillCargo::getCargoName,ckBillCargo.getCargoName());
        //货主名称
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getCustName()),CkBillCargo::getCustName,ckBillCargo.getCustName());
        //航次号
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getVoyageNo()),CkBillCargo::getVoyageNo,ckBillCargo.getVoyageNo());
        //核注清单号和报关单号
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getCustomsNo()),CkBillCargo::getCustomsNo,ckBillCargo.getCustomsNo());
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getListNumber()),CkBillCargo::getListNumber,ckBillCargo.getListNumber());

        //业务性质和贸易性质
        queryWrapper.eq(Validator.isNotEmpty(ckBillCargo.getOpType()),CkBillCargo::getOpType,ckBillCargo.getOpType());
        queryWrapper.eq(Validator.isNotEmpty(ckBillCargo.getTradType()),CkBillCargo::getTradType,ckBillCargo.getTradType());

        queryWrapper.eq(Validator.isNotEmpty(ckBillCargo.getStockCode()),CkBillStock::getStockCode,ckBillCargo.getStockCode());
        queryWrapper.like(Validator.isNotEmpty(ckBillCargo.getStockName()),CkBillStock::getStockName,ckBillCargo.getStockName());
        queryWrapper.groupBy(CkBillCargo::getHsCode, CkBillCargo::getCargoName, CkBillCargo::getCargoSpec,
                CkBillCargo::getOriginCountry, CkBillCargo::getBillDate, CkBillCargo::getVesselName,
                CkBillCargo::getVoyageNo, CkBillCargo::getInoutType);
        queryWrapper.groupBy(CkBillStock::getStockCode, CkBillStock::getStockName);
        queryWrapper.orderByDesc(CkBillCargo::getBillDate);
        List<CkBillCargo> list = ckBillCargoService.selectJoinList(CkBillCargo.class,queryWrapper);

        return getDataTable(list);
    }
    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<CkBillCargo> queryWrapper,CkBillCargo ckBillCargo){


        if(Validator.isNotEmpty(ckBillCargo.getInoutType())){
            queryWrapper.eq(CkBillCargo::getInoutType,ckBillCargo.getInoutType());
        }


        if (null != ckBillCargo.getBeginDate()) {
            queryWrapper.between(CkBillCargo::getBillDate, ckBillCargo.getBeginDate(), ckBillCargo.getEndDate());
        }

        //单据编号
        if(Validator.isNotEmpty(ckBillCargo.getBillNumber())){
            queryWrapper.like(CkBillCargo::getBillNumber,ckBillCargo.getBillNumber());
        }

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

        //仓库名称
        if(ckBillCargo.getStockCode() != null){
            queryWrapper.eq(CkBillCargo::getStockCode,ckBillCargo.getStockCode());
        }

        //航次号
        if(null != ckBillCargo.getVoyageNo()){
            queryWrapper.like(CkBillCargo::getVoyageNo,ckBillCargo.getVoyageNo());
        }

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

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

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

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

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


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

        //有效期至
        if(null != ckBillCargo.getExpiryDate()){
            queryWrapper.eq(CkBillCargo::getExpiryDate,ckBillCargo.getExpiryDate());
        }


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

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

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

        //规格类型品质
        if(Validator.isNotEmpty(ckBillCargo.getCargoSpec())){
            queryWrapper.like(CkBillCargo::getCargoSpec,ckBillCargo.getCargoSpec());
        }

        //原产国
        if(Validator.isNotEmpty(ckBillCargo.getOriginCountry())){
            queryWrapper.like(CkBillCargo::getOriginCountry,ckBillCargo.getOriginCountry());
        }

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

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

        //业务性质
        if(Validator.isNotEmpty(ckBillCargo.getOpType())){
            queryWrapper.like(CkBillCargo::getOpType,ckBillCargo.getOpType());
        }

        //贸易性质
        if(Validator.isNotEmpty(ckBillCargo.getTradType())){
            queryWrapper.like(CkBillCargo::getTradType,ckBillCargo.getTradType());
        }


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

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

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



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

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





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

        }

}
