“mitao”
2026-05-16 5b32f811c6bb6ddeb7f3a1cad4df68cf5705b9b0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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<MeasureRule> loadMeasureRules(Integer hierarchyId, String hierarchyPath,  String filePath){
        var measureRules = new ArrayList<MeasureRule>();
 
        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;
    }
}