package com.huaheng.pc.u8.service; import com.huaheng.common.constant.QuantityConstant; import com.huaheng.common.utils.security.ShiroUtils; import com.huaheng.framework.web.domain.AjaxResult; import com.huaheng.pc.config.company.domain.CompanyU8; import com.huaheng.pc.config.company.service.CompanyU8Service; import com.huaheng.pc.config.material.domain.Material; import com.huaheng.pc.config.material.service.MaterialService; import com.huaheng.pc.config.materialType.domain.MaterialType; import com.huaheng.pc.config.materialType.service.MaterialTypeService; import com.huaheng.pc.config.warehouseCompany.domain.WarehouseCompany; import com.huaheng.pc.config.warehouseCompany.service.WarehouseCompanyService; import com.huaheng.pc.u8.domain.ICSMaterialModel; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; @Service public class MaterialAPIService { @Resource private CompanyU8Service companyU8Service; @Resource MaterialService materialService; @Resource private WarehouseCompanyService warehouseCompanyService; @Resource private MaterialTypeService materialTypeService; @Transactional public AjaxResult ICSInventory(List<ICSMaterialModel> iCSInventorys) { WarehouseCompany warehouseCompany = new WarehouseCompany(); AjaxResult ajaxResult = new AjaxResult(); List<Material> materials=new ArrayList<>(); List<MaterialType> materialTypeList=new ArrayList<>(); for(ICSMaterialModel iCSInventory:iCSInventorys) { CompanyU8 companyWu=new CompanyU8(); companyWu.setUCompanyCode(iCSInventory.getCompanyCode()); companyWu=companyU8Service.getCompanyByU8CodeWarehouseCode(iCSInventory.getCompanyCode()); if (companyWu == null) { return AjaxResult.error("货主不存在"); } warehouseCompany.setCompanyCode(companyWu.getCompanyCode()); List<WarehouseCompany> list = warehouseCompanyService.getByDoamin(warehouseCompany); if (list == null || list.size() == 0) { return AjaxResult.error("系统中没有该货主:" + warehouseCompany.toString() + " 信息,请先录入货主信息!"); } //先查询货主对应的仓库,有几个仓库,就循环几次来添加。 for (WarehouseCompany item : list) { Material material = new Material(); material.setCode(iCSInventory.getcInvCode()); material.setBarcode(iCSInventory.getcInvAddCode()); material.setName(iCSInventory.getcInvName()); material.setUserDef1(iCSInventory.getcInvAddCode()); material.setSpec(iCSInventory.getcInvStd()); material.setUnit(iCSInventory.getcComUnitName()); // material.setAssistUnit(iCSInventory.getcAssComUnitCode()); // material.setConvertRate(iCSInventory.getfConvertRate()); material.setType(iCSInventory.getcInvCCode()); // material.setDictLabel(iCSInventory.getcInvCName()); // material.setCompanyId(item.getCompanyId()); material.setCompanyCode(item.getCompanyCode()); // material.setWarehouseId(item.getWarehouseId()); material.setWarehouseCode(item.getWarehouseCode()); material.setLastUpdatedBy(ShiroUtils.getLoginName()); material.setCreatedBy(ShiroUtils.getLoginName()); if(materialService.getMaterialByCode(material.getCode(),material.getWarehouseCode())==null) { materials.add(material); } //查找MaterialType有无该数据 MaterialType materialType=new MaterialType(); materialType.setCode(material.getType()); materialType.setWarehouseCode(material.getWarehouseCode()); List<MaterialType> materialTypelist=materialTypeService.getMaterialTypeByDoamin(materialType); if(materialTypelist==null||materialTypelist.size()==0){ MaterialType materialType1=createMaterialType(material, iCSInventory); materialTypeList.add(materialType1); } } } if(materials.size()>0) { materialService.saveBatch(materials); } if(materialTypeList.size()>0) { materialTypeService.saveBatch(materialTypeList); } return AjaxResult.success("成功"); } public MaterialType createMaterialType(Material material,ICSMaterialModel iCSInventory){ MaterialType materialType=new MaterialType(); materialType.setCode(material.getType()); materialType.setWarehouseCode(material.getWarehouseCode()); materialType.setName(iCSInventory.getcInvCName()); materialType.setCompanyCode(material.getCompanyCode()); materialType.setCreatedBy(ShiroUtils.getUserName()); materialType.setLastUpdatedBy(ShiroUtils.getUserName()); return materialType; } }