package com.huaheng.api.general.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.general.domain.ContainerTypeDomain;
import com.huaheng.api.general.domain.InventoryDomain;
import com.huaheng.api.general.domain.InventoryQueryDomain;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.containerCapacity.domain.ContainerCapacity;
import com.huaheng.pc.config.containerCapacity.service.ContainerCapacityService;
import com.huaheng.pc.config.containerType.domain.ContainerType;
import com.huaheng.pc.config.containerType.service.ContainerTypeService;
import com.huaheng.pc.config.customer.domain.Customer;
import com.huaheng.pc.config.customer.service.CustomerServiceImpl;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.supplier.domain.Supplier;
import com.huaheng.pc.config.supplier.service.SupplierService;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.system.dept.domain.Dept;
import com.huaheng.pc.system.dept.service.IDeptService;
import com.huaheng.pc.system.dict.domain.DictData;
import com.huaheng.pc.system.dict.domain.DictType;
import com.huaheng.pc.system.dict.mapper.DictDataMapper;
import com.huaheng.pc.system.dict.mapper.DictTypeMapper;
import com.huaheng.pc.system.dict.service.IDictDataService;
import com.huaheng.pc.system.dict.service.IDictTypeService;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class BasicDataApiService {
    @Autowired
    IDictTypeService dictTypeService;
    @Autowired
    IDictDataService dictDataService;
    @Autowired
    MaterialService materialService;
    @Autowired
    ContainerTypeService containerTypeService;
    @Autowired
    InventoryDetailService inventoryDetailService;
    @Autowired
    InventoryHeaderService inventoryHeaderService;
    @Resource
    private DictTypeMapper dictTypeMapper;
    @Resource
    private DictDataMapper dictDataMapper;

    @Autowired
    IUserService iUserService;

    @Autowired
    IDeptService iDeptService;

    @Autowired
    CustomerServiceImpl iCustomerService;

    @Autowired
    WarehouseService iWarehouseService;

    @Autowired
    SupplierService iSupplierService;

    @Autowired
    CompanyService companyService;

    @Autowired
    private ContainerCapacityService containerCapacityService;

    private static final Logger logger = LoggerFactory.getLogger("base-api");


    //检查仓库
    public Warehouse checkWarehouse(String code) {
        LambdaQueryWrapper<Warehouse> warehouseLam = Wrappers.lambdaQuery();
        warehouseLam.eq(Warehouse::getCode, code);
        Warehouse warehouse = iWarehouseService.getOne(warehouseLam);
        if (warehouse == null) {
            throw new ServiceException("数据出现问题,系统没有该仓库");
        }
        return warehouse;
    }

    /**
     * 字典通用接口
     * 1、判断必填字段是否为空
     * 2、仓库是否正确
     * 3、检查字典头表,如果没有就添加
     * 4、增新一条字典明细表的记录
     * @param dictData
     * @return
     */
    public AjaxResult dict(DictData dictData) {
        //1、判断必填字段是否为空
        if(StringUtils.isEmpty(dictData.getWarehouseCode())){
            return AjaxResult.error("没有仓库编码");
        }
        if(StringUtils.isEmpty(dictData.getDictLabel())){
            return AjaxResult.error("没有字典标签");
        }
        if(StringUtils.isEmpty(dictData.getDictType())){
            return AjaxResult.error("没有字典类型");
        }
        if(StringUtils.isEmpty(dictData.getDictValue())){
            return AjaxResult.error("没有字典键值");
        }

        //2、仓库是否正确
        int result = 0;
        this.checkWarehouse(dictData.getWarehouseCode());

        //3、检查字典头表,如果没有就添加
        DictData condition = new DictData();
        try {
            condition.setDictType(dictData.getDictType());
            condition.setWarehouseCode(dictData.getWarehouseCode());
            condition.setDictValue(dictData.getDictValue());
            condition.setEnable(true);
            List<DictData> dictDatas = dictDataService.selectDictDataList(dictData);
            if (dictDatas.size() < 1) {
                //找出字典头表的id
                DictType dictType = new DictType();
                dictType.setWarehouseCode(dictData.getWarehouseCode());
                dictType.setDictType(dictData.getDictType());
                List<DictType> dictList = dictTypeService.selectDictTypeList(dictType);
                if (dictList.size() < 1) {
                    dictType.setWarehouseId(dictData.getWarehouseId());
                    dictType.setCreateBy(dictData.getCreateBy());
                    dictType.setCreateTime(dictData.getCreateTime());
                    if(StringUtils.isEmpty(dictType.getCreateBy())) {
                        result = dictTypeService.insertDictType(dictType);
                    }else {
                        result=dictTypeMapper.insertDictType(dictType);
                    }
                    dictList.add(dictType);
                    if (result < 1) {
                        throw new ServiceException("新增字典类型失败!");
                    }
                }
                //4、增新一条字典明细表的记录
                dictData.setHeaderId(dictList.get(0).getId());
                if(StringUtils.isEmpty(dictData.getCreateBy())) {
                    result = dictDataService.insertDictData(dictData);
                }else {
                    result=dictDataMapper.insertDictData(dictData);
                }
                if (result < 1) {
                    throw new ServiceException("新增字典数据失败!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("字典数据有误!!");
        }
        return AjaxResult.success("新增字典成功");
    }

    /**
     * 检查是否存在物料,如果存在就修改,不存在就新增
     * 1、判断必填字段是否为空
     * 2、检查仓库和货主
     * 3、查看此物料在系统是否存在
     * @param material
     * @return
     */
    @Transactional
    public AjaxResult material(Material material)    {

        //1、判断必填字段是否为空
        if(StringUtils.isEmpty(material.getCode())){
            return AjaxResult.error("物料编码不能为空!!");
        }
        if(StringUtils.isEmpty(material.getWarehouseCode())){
            return AjaxResult.error("仓库编码不能为空!!");
        }
        if(StringUtils.isEmpty(material.getCompanyCode())){
            return AjaxResult.error("货主编码不能为空!!");
        }
        if(StringUtils.isEmpty(material.getType())){
            return AjaxResult.error("物料类型不能为空!!");
        }
        if(StringUtils.isEmpty(material.getName())){
            return AjaxResult.error("物料名称不能为空!!");
        }
        if(StringUtils.isEmpty(material.getSpec())){
            return AjaxResult.error("物料规格不能为空!!");
        }
        if(StringUtils.isEmpty(material.getUnit())){
            return AjaxResult.error("物料单位不能为空!!");
        }

        //2、检查仓库和货主
        this.checkWarehouse(material.getWarehouseCode());
        LambdaQueryWrapper<Company> companyLam = Wrappers.lambdaQuery();
        companyLam.eq(Company::getCode,material.getCompanyCode());
        Company company=companyService.getOne(companyLam);
        if(company==null){
            return AjaxResult.error("没有该货主!!");
        }

        Boolean flag = false;
        try {
            //3、查看此物料在系统是否存在
            LambdaQueryWrapper<Material> materialLam = Wrappers.lambdaQuery();
            materialLam.eq(Material::getCode,material.getCode())
                    .eq(Material::getWarehouseCode,material.getWarehouseCode());
            Material entity = materialService.getOne(materialLam);
            if (entity == null) {
                flag = materialService.save(material);
                if (flag == false) {
                    throw new ServiceException("新增物料失败!");
                }
            } else {
//                return AjaxResult.error("已有该物料,无法进行修改!!");
                material.setId(entity.getId());
                flag = materialService.updateById(material);
                if (flag == false) {
                    throw new ServiceException("更新物料失败!");
                }else {
                    return AjaxResult.success("更新物流成功!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("物料数据问题");
        }
        return AjaxResult.success("新增物料成功");
    }

    /**
    1,物料同步
    2,条件查询
     * @param material
     * @return
     */
    @Transactional
    public AjaxResult queryMaterial(Material material)    {
        if (StringUtils.isEmpty(material.getWarehouseCode())){
            return AjaxResult.error("仓库编码不能为空!!");
        }
        if (StringUtils.isEmpty(material.getCompanyCode())){
            return AjaxResult.error("货主编码不能为空!!");
        }
        LambdaQueryWrapper<Material> lambdaQueryWrapper =Wrappers.lambdaQuery(material);
        materialService.list(lambdaQueryWrapper);
        return AjaxResult.success(materialService.list(lambdaQueryWrapper));
    }



    /**
     * 人员档案通用接口
     * 1、判断必填字段是否为空
     * 2、判断系统中是否有该用户,如果有则更新,如果没有则新增
     *新增: (1)、默认密码为123456
     *       (2)默认为普通用户
     *       (3)默认为长沙仓库
     * @param user
     * @return
     */
    public AjaxResult user(User user){
        //1、判断必填字段是否为空
        int result = 0;
        User user1 = new User();
        if(user.getLoginName()==null || user.getLoginName()=="") {
            return AjaxResult.error("没有人员编码!!");
        }
        if (user.getUserName()==null || user.getUserName()==""){
            return AjaxResult.error("没有人员名称!!");
        }
        if(user.getDeptId()==null){
            return AjaxResult.error("没有部门ID!!");
        }
        if(iDeptService.selectDeptById(user.getDeptId())==null){
            return AjaxResult.error("系统没有此部门!!");
        }
        try {
            user1.setLoginName(user.getLoginName());
            //2、判断系统中是否有该用户,如果有则更新,如果没有则新增
                if (iUserService.selectmen(user.getLoginName()) == null) {
                    //(1)默认密码为123456
                    if(user.getPassword()==null) {
                        user.setPassword("123456");
                    }
                    //(2)默认为普通用户
                    if(StringUtils.isEmpty(user.getRoleIds())) {
                        List<Integer> roleIds=new ArrayList<>();
                        roleIds.add(2);
                        user.setRoleIds(roleIds);
                    }
                    //(3)默认为长沙仓库
                    if(StringUtils.isEmpty(user.getCompanyIdList())) {
                        List<Integer> companyIdList = new ArrayList<>();
                        companyIdList.add(2);
                        user.setCompanyIdList(companyIdList);
                    }
                result = iUserService.insertUser(user);
                if (result < 1) {
                    throw new ServiceException("新增人员档案失败!");
                } else {
                    return AjaxResult.success("新增人员档案成功!");
                }
            } else {
                    return AjaxResult.error("已有该人员档案,无法进行修改!");
//                result = iUserService.updateUser(user);
//                if (result < 1) {
//                    throw new ServiceException("更新人员档案失败!");
//                } else {
//                    return AjaxResult.success("更新人员档案成功!");
//                }
            }
        }catch (Exception e){
            throw new ServiceException("数据问题。。。");
        }
    }



    /**
     1,用户同步
     2,条件查询
     * @param user
     * @return
     */
    @Transactional
    public AjaxResult queryUser(User user)    {
        iUserService.selectUserList(user);
        return AjaxResult.success(iUserService.selectUserList(user));
    }



    /**
     * 部门档案通用接口
     * 1、判断必填字段是否为空
     * 2、部门编码长度应是双数
     *
     * @param dept
     * @return
     */
    @Transactional
    public AjaxResult dept(Dept dept) {

        //1、判断必填字段是否为空
        int result = 0;
        String code = dept.getCode();
        if (code == null || code == "") {
            return AjaxResult.error("部门编码不能为空!!");
        }
        try {
            Dept rs = iDeptService.selectDepts(code);

            //2、部门编码长度应是双数
            if (rs == null) {
                int x = code.length() % 2;
                if (x != 0) {
                    return AjaxResult.error("部门编码长度应是双数");
                } else {
                    int y = code.length() / 2;
                    if (y >= 1) {
                        String scode = code.substring(0, 2);
                        if (iDeptService.selectDepts(scode) == null) {
                            dept.setCode(scode);
                            dept.setParentId(100);
                            dept.setAncestors("0,100");
                            dept.setOrderNum("1");
                            result = iDeptService.insertDept(dept);
                            if (result < 1) {
                                throw new ServiceException("新增部门档案失败!");
                            }
                        }
                    }
                    for (int z = 1; z <=y; z++) {

                        //找到上级部门
                        String sqcode = code.substring(0, 2 * (z - 1));
                        Dept sdept = iDeptService.selectDepts(sqcode);
                        String sscode = code.substring(0, 2 * z);
                        if (iDeptService.selectDepts(sscode) == null) {
                            dept.setCode(sscode);
                            dept.setParentId(sdept.getId());
                            dept.setAncestors(sdept.getAncestors() + "," + sdept.getId());
                            dept.setOrderNum(String.valueOf(z));
                            result = iDeptService.insertDept(dept);
                            if (result < 1) {
                                throw new ServiceException("新增部门档案失败!");
                            }
                        }
                    }
                }
                return AjaxResult.success("新增部门成功");
            } else {
                dept.setId(rs.getId());
                int num = iDeptService.updatesDept(dept);
                if (num < 1) {
                    throw new ServiceException("部门修改失败");
                } else {
                    return AjaxResult.success("部门修改成功");
                }
            }
        } catch (Exception e) {
            throw new ServiceException("数据问题。。。");
        }
    }

    /**
     * 客户档案通用接口
     * 1、判断必填字段是否为空
     * 2、检查仓库
     * 3、查看此客户在系统是否存在
     * @param customer
     * @return
     */
    public AjaxResult customer(Customer customer){
        Boolean flag = true;
        //1、判断必填字段是否为空
        if(customer.getCode()==null||customer.getCode()=="") {
            return AjaxResult.error("客户代码不能为空!!");
        }
        if(StringUtils.isEmpty(customer.getWarehouseCode())){
            return AjaxResult.error("没有仓库编码");
        }
        if(StringUtils.isEmpty(customer.getName())){
            return AjaxResult.error("没有客户名称");
        }
        if(StringUtils.isEmpty(customer.getCompanyCode())){
            return AjaxResult.error("没有货主编码");
        }
        try {
            //2、检查仓库
            this.checkWarehouse(customer.getWarehouseCode());

            //3、查看此客户在系统是否存在
            LambdaQueryWrapper<Customer> customerLam = Wrappers.lambdaQuery();
            customerLam.eq(Customer::getCode,customer.getCode())
                    .eq(Customer::getWarehouseCode,customer.getWarehouseCode());
            Customer ctr = iCustomerService.getOne(customerLam);

            //不存在添加
            if ( ctr == null) {
                flag = iCustomerService.save(customer);
                if (flag == false) {
                    throw new ServiceException("新增客户档案失败!");
                } else {
                    return AjaxResult.success("新增客户档案成功!");
                }
            } else {
                return AjaxResult.error("已有该客户,无法进行修改!!");
//                customer.setId(rs.getId());
//                result = iCustomerService.updateByModel(customer);
//                if (result < 1) {
//                    throw new ServiceException("更新客户档案失败!");
//                } else {
//                    return AjaxResult.success("更新客户档案成功!");
//                }
            }
        }catch (Exception e){
            throw new ServiceException("数据问题。。。");
        }
    }

    /**
     * 仓库档案通用接口
     * 1、判断必填字段是否为空
     * 2、判断系统中是否有该仓库,若有则更新,若无则新增
     * @param warehouse
     * @return
     */
    public AjaxResult warehouse(Warehouse warehouse){
        if(warehouse.getCode()==null||warehouse.getCode()=="") {
            return AjaxResult.error("仓库编码不能为空!!");
        }
        if(warehouse.getName()==null||warehouse.getName()=="") {
            return AjaxResult.error("仓库名称不能为空!!");
        }
        try {
            LambdaQueryWrapper<Warehouse> warehouseLam = Wrappers.lambdaQuery();
            warehouseLam.eq(Warehouse::getCode,warehouse.getCode());
            Warehouse whs = iWarehouseService.getOne(warehouseLam);

            //2、判断系统中是否有该仓库,若有则更新,若无则新增
            if (whs == null) {
                Boolean flag = iWarehouseService.save(warehouse);
                if (flag == false) {
                    throw new ServiceException("新增仓库档案失败!");
                } else {
                    return AjaxResult.success("新增仓库档案成功!");
                }
            } else {
                return AjaxResult.error("已有该仓库,无法进行修改!");
//                warehouse.setId(rs.getId());
//                result = iWarehouseService.updateByModel(warehouse);
//                if (result < 1) {
//                    throw new ServiceException("更新仓库档案失败!");
//                } else {
//                    return AjaxResult.success("更新仓库档案成功!");
//                }
            }
        }catch (Exception e){
            throw new ServiceException("仓库数据问题。。。");
        }
    }

    /**
     * 供应商档案通用接口
     * 1、判断必填字段是否为空
     *  2、检查仓库
     *  3、查看此供应商在系统是否存在
     * @param supplier
     * @return
     */
    public AjaxResult supplier(Supplier supplier){

        //1、判断必填字段是否为空
        if(StringUtils.isEmpty(supplier.getCode())){
            return AjaxResult.error("没有供应商代码");
        }
        if(StringUtils.isEmpty(supplier.getName())){
            return AjaxResult.error("没有供应商名称");
        }
        if(StringUtils.isEmpty(supplier.getWarehouseCode())){
            return AjaxResult.error("没有仓库编码");
        }

        //2、检查仓库
        this.checkWarehouse(supplier.getCode());

        //3、查看此供应商在系统是否存在
        try {
            LambdaQueryWrapper<Supplier> supplierLam = Wrappers.lambdaQuery();
            supplierLam.eq(Supplier::getCode,supplier.getCode())
                    .eq(Supplier::getWarehouseCode,supplier.getWarehouseCode());
            Supplier spl=iSupplierService.getOne(supplierLam);

            if (spl== null) {
                Boolean flag = iSupplierService.save(supplier);
                if (flag == false) {
                    throw new ServiceException("新增供应商失败!");
                } else {
                    return AjaxResult.success("新增供应商成功!");
                }
            } else {
                return AjaxResult.error("已有该供应商,无法修改!!");
            }
        }catch (Exception e){
            throw new ServiceException("供应商数据问题。。。");
        }
    }


    /**
     * 容器容量通用接口
     * 1、判断必填字段是否为空
     * 2、检查仓库、物料和容器类型
     * 3、检查此容器容量是否存在,不存在则添加
     * @param containerCapacity
     * @return
     */
    public AjaxResult containerCapacity(ContainerCapacity containerCapacity){
        Boolean flag = false;
        //1、判断必填字段是否为空
        if(StringUtils.isEmpty(containerCapacity.getContainerType())){
            return AjaxResult.error("没有容器类型");
        }
        if(StringUtils.isEmpty(containerCapacity.getWarehouseCode())){
            return AjaxResult.error("没有仓库编号");
        }
        if(StringUtils.isEmpty(containerCapacity.getMaterialCode())){
            return AjaxResult.error("没有物料编号");
        }
        if(StringUtils.isNull(containerCapacity.getQty())){
            return AjaxResult.error("没有容量上限");
        }

        // 2、检查仓库、物料和容器类型
        this.checkWarehouse(containerCapacity.getWarehouseCode());

        //物料检查
        LambdaQueryWrapper<Material> materialLam = Wrappers.lambdaQuery();
        materialLam.eq(Material::getCode,containerCapacity.getMaterialCode())
                .eq(Material::getWarehouseCode,containerCapacity.getWarehouseCode());
        Material material = materialService.getOne(materialLam);
        if(material == null){
            return AjaxResult.error("系统没有此物料编号");
        }

        //容器类型检查
        LambdaQueryWrapper<ContainerType> containerTypeLamb = Wrappers.lambdaQuery();
        containerTypeLamb.eq(ContainerType::getCode,containerCapacity.getContainerType())
                .eq(ContainerType::getWarehouseCode,containerCapacity.getWarehouseCode());
        ContainerType containerType = containerTypeService.getOne(containerTypeLamb);
        if(containerType == null){
            return AjaxResult.error("系统没有此容器类型");
        }

        //3、检查此容器容量是否存在,不存在则添加
        LambdaQueryWrapper<ContainerCapacity> containerCapacityLambd = Wrappers.lambdaQuery();
        containerCapacityLambd.eq(ContainerCapacity::getContainerType,containerCapacity.getContainerType())
                .eq(ContainerCapacity::getWarehouseCode,containerCapacity.getWarehouseCode())
                .eq(ContainerCapacity::getMaterialCode,containerCapacity.getMaterialCode());
        ContainerCapacity capacity = containerCapacityService.getOne(containerCapacityLambd);
        if(capacity == null){
            flag = containerCapacityService.save(containerCapacity);
            if(flag == false){
                return AjaxResult.error("新增容器容量失败");
            }
        }else {
            if(capacity.getQty().compareTo(containerCapacity.getQty()) != 0){
                capacity.setQty(containerCapacity.getQty());
                flag = containerCapacityService.updateById(capacity);
                if(flag == false){
                    return AjaxResult.error("修改容器容量失败");
                }
            }
        }
        return AjaxResult.success("新增容器容量成功");
    }

    public AjaxResult containerType(ContainerTypeDomain containerTypeDomain){
        if ("0".equals(containerTypeDomain.getCode())){
            if (!containerTypeService.saveBatch(containerTypeDomain.getContainerTypeList())){
                logger.error("erp初始化容器类型失败,失败原因:无法插入数据");
                return AjaxResult.error("初始化容器类型失败");
            }
            return AjaxResult.success("容器类型初始化成功");
        }else {
            LambdaQueryWrapper<ContainerType> lambdaQuery;
            for (ContainerType containerType : containerTypeDomain.getContainerTypeList()) {
                lambdaQuery = Wrappers.lambdaQuery();
                lambdaQuery.eq(ContainerType::getCode,containerType.getCode());
                ContainerType srcContainerType = containerTypeService.getOne(lambdaQuery);
                if (srcContainerType ==null){
                    if (!containerTypeService.save(containerType)){
                        logger.error("erp更新容器类型失败,失败原因:无法插入数据");
                        return AjaxResult.error("插入容器类型失败");
                    }
                }else {
                    containerType.setId(srcContainerType.getId());
                    if (!containerTypeService.updateById(containerType)){
                        logger.error("erp更新容器类型失败,失败原因:无法更新数据");
                        return AjaxResult.error("更新容器类型失败");
                    }
                }
            }
            return AjaxResult.success("容器类型更新成功");
        }
    }



    /**
     1,库存同步
     2,条件查询
     * @param inventoryDetail
     * @return
     */
    @Transactional
    public AjaxResult queryInventoryApi(InventoryDetail inventoryDetail)    {
        if (StringUtils.isEmpty(inventoryDetail.getWarehouseCode())){
            return AjaxResult.error("仓库编码不能为空!!");
        }
        if (StringUtils.isEmpty(inventoryDetail.getCompanyCode())){
            return AjaxResult.error("货主编码不能为空!!");
        }
        //查询库存明细
        LambdaQueryWrapper<InventoryDetail> lambdaQueryWrapper =Wrappers.lambdaQuery(inventoryDetail);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(lambdaQueryWrapper);

        //查询库存Header表
        Map<Integer,InventoryHeader> inventoryHeaderList = new HashMap<>();//map防止出现数据重复
        for (InventoryDetail inventoryDetail1:inventoryDetailList) {
            LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper =Wrappers.lambdaQuery();
            inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getId,inventoryDetail1.getInventoryHeaderId());
            inventoryHeaderList.put(inventoryDetail1.getInventoryHeaderId(),inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper));
        }

        InventoryDomain inventoryDomain =new InventoryDomain();
        inventoryDomain.setInventoryHeader(inventoryHeaderList);
        inventoryDomain.setInventoryDetails(inventoryDetailList);
        return AjaxResult.success(inventoryDomain);
    }


    public AjaxResult queryInventory(InventoryQueryDomain queryDomain){
        if (StringUtils.isEmpty(queryDomain.getWarehouseCode())){
            return AjaxResult.error("仓库编码不能为空");
        }
        if (StringUtils.isEmpty(queryDomain.getCompanyCode())){
            return AjaxResult.error("货主编码不能为空");
        }
        if (StringUtils.isEmpty(queryDomain.getContainerCode())&&StringUtils.isEmpty(queryDomain.getLocationCode())&&StringUtils.isEmpty(queryDomain.getMaterialCode())){
            return AjaxResult.error("容器、库位与物料至少一样不为空");
        }
        LambdaQueryWrapper<InventoryDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(InventoryDetail::getWarehouseCode,queryDomain.getWarehouseCode())
                .eq(InventoryDetail::getCompanyCode,queryDomain.getCompanyCode());
        ArrayList<InventoryDetail> inventoryDetails = new ArrayList<>();
        if (StringUtils.isNotEmpty(queryDomain.getContainerCode())){
            queryWrapper.eq(InventoryDetail::getContainerCode,queryDomain.getContainerCode());
        }
        if (StringUtils.isNotEmpty(queryDomain.getLocationCode())){
            queryWrapper.eq(InventoryDetail::getLocationCode,queryDomain.getLocationCode());
        }
        if (StringUtils.isNotEmpty(queryDomain.getMaterialCode())){
            queryWrapper.eq(InventoryDetail::getMaterialCode,queryDomain.getMaterialCode());
        }
        List<InventoryDetail> list = inventoryDetailService.list(queryWrapper);
        if (list==null){
            return AjaxResult.error("没有符合条件的库存");
        }
        return AjaxResult.success("获取成功",list);
    }




}