package com.estate.expert.admin.initdata; import com.estate.expert.admin.domain.standard.hierachy.Hierarchy; import com.estate.expert.admin.domain.standard.measure.MeasureRule; import com.estate.expert.admin.initdata.common.ImportUtil; import com.estate.expert.admin.service.HierarchyService; import com.estate.expert.admin.service.MeasureRuleService; import com.estate.expert.admin.service.UserService; import lombok.Builder; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.junit.platform.commons.util.StringUtils; import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import java.util.List; import java.util.UUID; @Builder @Slf4j public class ImportMeasureRuleJob { private UserService userService; private HierarchyService hierarchyService; private MeasureRuleService measureRuleService; public void importMeasureRules() { var hierarchy = hierarchyService.get(6); loadDataFiles(hierarchy,"/实测实量-标准-规则管理"); } private void loadDataFiles(Hierarchy hierarchy, String path){ var systemUser = userService.getSystemUser(); FileUtils.listFiles(new File(ImportUtil.docsRoot(path)), new String[]{"xlsx"}, true).forEach(f -> { if (!f.getName().equals("data.xlsx")) return; var filePath = f.getAbsolutePath(); var hierarchyNamePath = filePath.substring(filePath.indexOf(path) + path.length(), filePath.lastIndexOf("/")); var hierarchyId = hierarchy.getId(); var hierarchyPath = hierarchy.getContent().getByNamePath(hierarchyNamePath).getPath(); var measureRules = loadMeasureRules(hierarchyId, hierarchyPath, filePath); measureRuleService.addEntities(systemUser, measureRules); }); } private List loadMeasureRules(Integer hierarchyId, String hierarchyPath, String filePath){ var measureRules = new ArrayList(); try{ FileInputStream fis = new FileInputStream(filePath); var workbook = WorkbookFactory.create(fis); var sheet = workbook.getSheetAt(0); int rowCount = sheet.getLastRowNum(); for (int i = 0; i <= rowCount; i++) { var row = sheet.getRow(i); var name = row.getCell(0).getStringCellValue(); if(StringUtils.isBlank(name)){ continue; } var measureRule = new MeasureRule(); measureRule.setHierarchyId(hierarchyId); measureRule.setHierarchyPath(hierarchyPath); measureRule.setName(UUID.randomUUID().toString()); measureRule.setDescription(row.getCell(0).getStringCellValue()); measureRule.setMeasurePoint(Integer.parseInt(row.getCell(1).getStringCellValue())); measureRule.setMeasureType(row.getCell(2).getStringCellValue()); measureRule.setMeasureExpr1(row.getCell(3).getStringCellValue()); measureRule.setMeasureExpr2(row.getCell(4).getStringCellValue()); measureRules.add(measureRule); } }catch (Exception e){ log.error("导入实测实量规则数据失败", e); } return measureRules; } }