diff --git a/hw-modules/hw-mes/src/main/java/com/hw/mes/controller/MesImportController.java b/hw-modules/hw-mes/src/main/java/com/hw/mes/controller/MesImportController.java index 24da116..df6909a 100644 --- a/hw-modules/hw-mes/src/main/java/com/hw/mes/controller/MesImportController.java +++ b/hw-modules/hw-mes/src/main/java/com/hw/mes/controller/MesImportController.java @@ -45,5 +45,19 @@ public class MesImportController extends BaseController { return success(message); } + /** + * 物料BOM信息excel导入 + * @param file + * @param updateSupport + * @return + */ + @Log(title = "物料BOM信息", businessType = BusinessType.IMPORT) + @PostMapping(("/BOMImportData")) + public AjaxResult BOMImportData(MultipartFile file, boolean updateSupport) + { + String operName = SecurityUtils.getUsername(); + String message = mesImportService.BOMImportData(file, updateSupport, operName); + return success(message); + } } diff --git a/hw-modules/hw-mes/src/main/java/com/hw/mes/mapper/MesBaseMaterialInfoMapper.java b/hw-modules/hw-mes/src/main/java/com/hw/mes/mapper/MesBaseMaterialInfoMapper.java index 6ebcd0b..d35be6a 100644 --- a/hw-modules/hw-mes/src/main/java/com/hw/mes/mapper/MesBaseMaterialInfoMapper.java +++ b/hw-modules/hw-mes/src/main/java/com/hw/mes/mapper/MesBaseMaterialInfoMapper.java @@ -79,8 +79,13 @@ public interface MesBaseMaterialInfoMapper */ public List selectMaterialInfos4AllocationWarehouse(MesBaseMaterialInfo mesBaseMaterialInfo); - - + /** + * 通过物料编码查询物料信息 + * + * @param materialCode 物料编码 + * @return 物料信息 + */ + public MesBaseMaterialInfo selectMesBaseMaterialInfoByMaterialCode(String materialCode); /** * 查询物料信息 diff --git a/hw-modules/hw-mes/src/main/java/com/hw/mes/service/IMesImportService.java b/hw-modules/hw-mes/src/main/java/com/hw/mes/service/IMesImportService.java index b93e660..8ec3f35 100644 --- a/hw-modules/hw-mes/src/main/java/com/hw/mes/service/IMesImportService.java +++ b/hw-modules/hw-mes/src/main/java/com/hw/mes/service/IMesImportService.java @@ -1,6 +1,7 @@ package com.hw.mes.service; import com.hw.mes.domain.MesPurchaseRequisitionTemplate; +import org.springframework.web.multipart.MultipartFile; import java.util.List; @@ -21,4 +22,13 @@ public interface IMesImportService * @return */ String importPurchaseRequisition(List purchaseRequisitionTemplateList, String operName); + + /** + * 物料BOM信息excel导入 + * @param file + * @param updateSupport + * @param operName + * @return + */ + String BOMImportData(MultipartFile file, boolean updateSupport, String operName); } diff --git a/hw-modules/hw-mes/src/main/java/com/hw/mes/service/impl/MesImportImpl.java b/hw-modules/hw-mes/src/main/java/com/hw/mes/service/impl/MesImportImpl.java index d21b619..0676ed1 100644 --- a/hw-modules/hw-mes/src/main/java/com/hw/mes/service/impl/MesImportImpl.java +++ b/hw-modules/hw-mes/src/main/java/com/hw/mes/service/impl/MesImportImpl.java @@ -13,18 +13,34 @@ import com.hw.jindie.api.RemoteJindieService; import com.hw.mes.api.RemoteMesService; import com.hw.mes.api.domain.MesBaseMaterialInfo; import com.hw.mes.domain.MesBaseUnitInfo; +import com.hw.mes.domain.MesMaterialBom; import com.hw.mes.domain.MesPurchaseRequisitionTemplate; +import com.hw.mes.mapper.MesBaseMaterialInfoMapper; import com.hw.mes.mapper.MesBaseUnitInfoMapper; +import com.hw.mes.mapper.MesMaterialBomMapper; import com.hw.mes.service.IMesBaseMaterialInfoService; import com.hw.mes.service.IMesImportService; +import com.hw.mes.service.IMesMaterialBomService; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.util.IOUtils; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigDecimal; +import java.util.ArrayList; import java.util.List; +import static org.apache.poi.ss.usermodel.CellType.NUMERIC; +import static org.apache.poi.ss.usermodel.CellType.STRING; + /** * excel导入Service业务层处理 * @@ -34,15 +50,17 @@ import java.util.List; @Service public class MesImportImpl implements IMesImportService { - @Autowired - private IMesBaseMaterialInfoService materialInfoService; - @Autowired private MesBaseUnitInfoMapper mesBaseUnitInfoMapper; @Autowired private RemoteJindieService remoteJindieService; + @Autowired + private IMesMaterialBomService mesMaterialBomService; + + @Autowired + private MesBaseMaterialInfoMapper mesBaseMaterialInfoMapper; private static final Logger log = LoggerFactory.getLogger(MesImportImpl.class); @@ -54,6 +72,7 @@ public class MesImportImpl implements IMesImportService { * @return */ @Override + @Transactional(rollbackFor = ServiceException.class) public String importPurchaseRequisition(List purchaseRequisitionTemplateList, String operName) { if (StringUtils.isNull(purchaseRequisitionTemplateList) || purchaseRequisitionTemplateList.size() == 0) { throw new ServiceException("导入采购申请单数据不能为空!"); @@ -71,7 +90,7 @@ public class MesImportImpl implements IMesImportService { productModel = productModel.replaceAll("[^\\w.]", ""); MesBaseMaterialInfo materialInfo = new MesBaseMaterialInfo(); materialInfo.setMaterialName(materialName); - List materialInfoList = materialInfoService.selectMesBaseMaterialInfoList(materialInfo); + List materialInfoList = mesBaseMaterialInfoMapper.selectMesBaseMaterialInfoList(materialInfo); int successFlag = 0; MesBaseMaterialInfo findMaterialInfo = new MesBaseMaterialInfo(); @@ -111,11 +130,11 @@ public class MesImportImpl implements IMesImportService { } } - if (jsonArray.size() > 0){ + if (jsonArray.size() > 0) { String params = jsonArray.toJSONString(); R ajaxResult = remoteJindieService.singleSavePurchaseRequisition(params, SecurityConstants.INNER); successMsg.append("
").append("采购申请单全部信息导入成功"); - log.info("++++++采购申请单导入++++++",purchaseRequisitionTemplateList, ajaxResult); + log.info("++++++采购申请单导入++++++", purchaseRequisitionTemplateList, ajaxResult); } if (failureNum > 0) { @@ -126,4 +145,115 @@ public class MesImportImpl implements IMesImportService { return successMsg.toString(); } + /** + * 物料BOM信息excel导入 + * + * @param file + * @param updateSupport + * @param operName + * @return + */ + @Override + @Transactional(rollbackFor = ServiceException.class) + public String BOMImportData(MultipartFile file, boolean updateSupport, String operName) { + InputStream is = null; + try { + is = file.getInputStream(); + Workbook workbook = new XSSFWorkbook(is); + Sheet sheet = workbook.getSheetAt(0); + int lastRowNum = sheet.getLastRowNum(); + for (int i = 0; i < lastRowNum; i++) { + Row rowT = sheet.getRow(i); + if (rowT == null) { + continue; // 如果行为空,跳过 + } + Cell cellT = rowT.getCell(2); + if (StringUtils.isNull(cellT)) { + throw new ServiceException("Excel格式错误!"); + } + String productBom = cellT.getStringCellValue(); + productBom = productBom.replaceAll("(.*?)", ""); + MesBaseMaterialInfo productMaterial = mesBaseMaterialInfoMapper.selectMesBaseMaterialInfoByMaterialCode(productBom); + if (productMaterial == null) { + throw new ServiceException("Excel格式错误:找不到父级BOM信息!"); + } + + i = i + 3;//标题下一行 + MesMaterialBom selectBom = new MesMaterialBom(); + selectBom.setMaterialId(productMaterial.getMaterialId()); + selectBom.setMaterialNameDesc(productMaterial.getMaterialName()); + List productBomList = mesMaterialBomService.selectMesMaterialBomList(selectBom); + Long parentBomId = 0L; + String parentBomDesc = productMaterial.getMaterialName(); + if (productBomList.size() > 0) { + MesMaterialBom mesMaterialBom = productBomList.get(0); + parentBomId = mesMaterialBom.getMaterialBomId(); + MesMaterialBom materialBom = new MesMaterialBom(); + materialBom.setParentId(mesMaterialBom.getMaterialBomId()); + List mesMaterialBoms = mesMaterialBomService.selectMesMaterialBomList(materialBom); + for (MesMaterialBom bom : mesMaterialBoms) { + mesMaterialBomService.deleteMesMaterialBomByMaterialBomId(bom.getMaterialBomId()); + } + } else { + MesMaterialBom materialBom = new MesMaterialBom(); + materialBom.setMaterialId(productMaterial.getMaterialId()); + materialBom.setMaterialName(productMaterial.getMaterialName()); + materialBom.setMaterialBomDesc(productMaterial.getMaterialName()); + materialBom.setParentId(0L); + materialBom.setActiveFlag("1"); + materialBom.setStandardAmount(new BigDecimal(1)); + materialBom.setCreateBy(operName); + mesMaterialBomService.insertMesMaterialBom(materialBom); + parentBomId = materialBom.getMaterialBomId(); + } + + ArrayList bomList = new ArrayList<>(); + while (true) { + if (i > lastRowNum) { + break; + } + Row rowC = sheet.getRow(i); + Cell numberCell = rowC.getCell(0); + if (StringUtils.isNull(numberCell) || numberCell.getCellType() != NUMERIC) { + break; + } + Cell materialCodeCell = rowC.getCell(1); + String materialCode = null; + if (materialCodeCell.getCellType() == NUMERIC) { + double numericCellValue = materialCodeCell.getNumericCellValue(); + materialCode = new BigDecimal(numericCellValue).toString(); + } else if (materialCodeCell.getCellType() == STRING) { + materialCode = materialCodeCell.getStringCellValue(); + } + Cell amountCell = rowC.getCell(5); + if (StringUtils.isNotEmpty(materialCode)) { + MesBaseMaterialInfo materialInfo = mesBaseMaterialInfoMapper.selectMesBaseMaterialInfoByMaterialCode(materialCode); + if (StringUtils.isNotNull(materialInfo)) { + MesMaterialBom materialBom = new MesMaterialBom(); + materialBom.setMaterialId(materialInfo.getMaterialId()); + materialBom.setMaterialName(materialInfo.getMaterialName()); + materialBom.setMaterialCode(materialInfo.getMaterialName()); + materialBom.setMaterialBomDesc(parentBomDesc); + materialBom.setParentId(parentBomId); + materialBom.setActiveFlag("1"); + materialBom.setStandardAmount(BigDecimal.valueOf(amountCell.getNumericCellValue())); + materialBom.setCreateBy(operName); + bomList.add(materialBom); + } + } + i++; + } + for (MesMaterialBom materialBom : bomList) { + mesMaterialBomService.insertMesMaterialBom(materialBom); + } + } + } catch (IOException e) { + throw new ServiceException("文件处理错误:" + e); + } finally { + IOUtils.closeQuietly(is); + } + + return "BOM导入成功"; + } + } diff --git a/hw-modules/hw-mes/src/main/resources/mapper/mes/MesBaseMaterialInfoMapper.xml b/hw-modules/hw-mes/src/main/resources/mapper/mes/MesBaseMaterialInfoMapper.xml index f1a727f..383637f 100644 --- a/hw-modules/hw-mes/src/main/resources/mapper/mes/MesBaseMaterialInfoMapper.xml +++ b/hw-modules/hw-mes/src/main/resources/mapper/mes/MesBaseMaterialInfoMapper.xml @@ -334,4 +334,8 @@ where bmi.erp_id = #{erpId} limit 1 + diff --git a/hw-ui/src/views/mes/materialBom/index.vue b/hw-ui/src/views/mes/materialBom/index.vue index e645c5c..2a97a5b 100644 --- a/hw-ui/src/views/mes/materialBom/index.vue +++ b/hw-ui/src/views/mes/materialBom/index.vue @@ -83,6 +83,16 @@ >展开/折叠 + + BOM导入 + + @@ -231,6 +241,35 @@ + + + +
将文件拖到此处,或点击上传
+
+ + + + 仅允许导入xls、xlsx格式文件。 + +
+
+ +
+ @@ -246,6 +285,7 @@ import Treeselect from "@riophae/vue-treeselect"; import "@riophae/vue-treeselect/dist/vue-treeselect.css"; import addBom from '@//views/mes/materialBom/addBom.vue'; import {verifyBOMIsProduction} from "@//api/mes/productOrder"; +import {getToken} from "@/utils/auth"; export default { name: "MaterialBom", @@ -312,6 +352,20 @@ export default { {required: true, message: "激活标识不能为空", trigger: "change"} ], }, + upload: { + // 是否显示弹出层(用户导入) + open: false, + // 弹出层标题(用户导入) + title: "", + // 是否禁用上传 + isUploading: false, + // 是否更新已经存在的用户数据 + updateSupport: 0, + // 设置上传的请求头部 + headers: { Authorization: "Bearer " + getToken() }, + // 上传的地址 + url: process.env.VUE_APP_BASE_API + "/mes/import/BOMImportData" + }, columns: [ {key: 0, label: `主键标识`, visible: false}, {key: 1, label: `父级标识`, visible: false}, @@ -554,6 +608,27 @@ export default { this.form.materialName = selectedRow.materialName; this.materialOpen = false; }, + /** 导入按钮操作 */ + handleImport() { + this.upload.title = "BOM导入"; + this.upload.open = true; + }, + // 文件上传中处理 + handleFileUploadProgress(event, file, fileList) { + this.upload.isUploading = true; + }, + // 文件上传成功处理 + handleFileSuccess(response, file, fileList) { + this.upload.open = false; + this.upload.isUploading = false; + this.$refs.upload.clearFiles(); + this.$alert("
" + response.msg + "
", "导入结果", { dangerouslyUseHTMLString: true }); + this.getList(); + }, + // 提交上传文件 + submitFileForm() { + this.$refs.upload.submit(); + }, /** 删除按钮操作 */ handleDelete(row) { verifyBOMIsProduction(row.materialBomId).then(res => {