自动化立体仓库 - WCS系统
Junjie
2023-05-18 5ac625f324a7bb1656b319348cabfd13467467ef
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.zy.common.utils;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.asrs.entity.DataResource;
import com.zy.asrs.service.DataResourceService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
 
@Component
public class YamlUtils {
 
    @Autowired
    private DataResourceService dataResourceService;
 
    private static final String src = "src/main/resources/application-prod.yml";
 
    private final static String C = "#";
    private final static String CHARSET = "UTF-8";
 
    //读取后的每行数据
    private static List<String> LINES = null;
 
    public Map<String, Object> read() {
        Yaml yaml = new Yaml();
        try {
            LINES = FileUtils.readLines(new File(src), CHARSET);//读取后的每行数据
            Map<String, Object> resultMap = (Map<String, Object>) yaml.load(new FileInputStream(new File(src)));
            Map<String, Object> wcs = (Map<String, Object>) resultMap.get("wcs-slave");
            recursion(wcs, null);
            return wcs;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public void recursion(Map<String, Object> resultMap, DataResource resource) {
        Iterator<String> iterator = resultMap.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            Object value = resultMap.get(key);
 
            DataResource dataResource = new DataResource();
            if (value == null) {
                dataResource.setData(null);
            } else if (value instanceof Map) {
                dataResource.setData(JSON.toJSONString(value));
            }else {
                dataResource.setData(value.toString());
            }
            dataResource.setCreateTime(new Date());
            dataResource.setName(key);
            if (resource != null) {
                dataResource.setResourceId(resource.getId());
            }
 
            //获取备注
            String memo = getMemoFromLines(key);
            dataResource.setMemo(memo);
 
            dataResourceService.insert(dataResource);
            if (value instanceof Map) {
                Map<String, Object> map = (Map<String, Object>) resultMap.get(key);
                recursion(map, dataResource);
            }
 
        }
    }
 
    /**
     * 从读取的数据中获取备注信息
     */
    public String getMemoFromLines(String name) {
        for (int i = 0; i < LINES.size(); i++) {
            String line = LINES.get(i);
            String trim = line.trim();
            String[] split1 = trim.split(":");
            String lineName = split1[0];
            if (!lineName.equals(name)) {
                continue;
            }
 
            if (line.contains(C)) {
                String[] split = trim.split(C);
                String memo = split[1];//获取注释
                return memo;
            }
        }
 
        return null;
    }
 
    /**
     * 设置备注到字符串list
     */
    public String setMemoToLines(String[] lines) {
        List<DataResource> dataResources = dataResourceService.selectList(new EntityWrapper<DataResource>().ne("memo", ""));
        for (DataResource dataResource : dataResources) {
            String name = dataResource.getName();
            int idx = 0;
            for (String line : lines) {
                String trim = line.trim();
                String[] split = trim.split(":");
                String lineName = split[0];
                if (lineName.equals(name)) {
                    lines[idx] = line + " #" + dataResource.getMemo();
                }
                idx++;
            }
        }
 
        StringBuffer sb = new StringBuffer();
        for (String line : lines) {
            sb.append(line).append("\n");
        }
        return sb.toString();
    }
 
    public void update() {
        Map<String, Object> ymlData = getYmlData();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("wcs-slave", ymlData);
        save(hashMap);
    }
 
    /**
     * 将配置信息保存至文件中
     */
    public void save(Map<String, Object> resultMap) {
        try {
            Yaml yaml = new Yaml();
            FileWriter fileWriter = new FileWriter(new File(src));
            String dumpAsMap = yaml.dumpAsMap(resultMap);
            dumpAsMap = dumpAsMap.replaceAll("'", "");
            String[] lines = dumpAsMap.split("\n");
            dumpAsMap = setMemoToLines(lines);
            fileWriter.write(dumpAsMap);
            fileWriter.flush();
            fileWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 遍历数据库数据,转换成yml数据
     */
    public Map<String, Object> getYmlData() {
        HashMap<String, Object> hashMap = new HashMap<>();
        List<DataResource> dataResources = dataResourceService.selectRootData();
        for (DataResource dataResource : dataResources) {
            getYmlDataRecursion(dataResource, hashMap);
        }
        return hashMap;
    }
 
    public void getYmlDataRecursion(DataResource dataResource, Map<String, Object> map) {
        List<DataResource> resources = dataResourceService.selectByResourceId(dataResource.getId());
        if (resources.size() > 0) {
            HashMap<String, Object> hashMap = new HashMap<>();
            map.put(dataResource.getName(), hashMap);
            for (DataResource resource : resources) {
                getYmlDataRecursion(resource, hashMap);
            }
        } else {
            String data = dataResource.getData();
            map.put(dataResource.getName(), data);
        }
    }
 
    public static void main(String[] args) {
        Map<String, Integer> keyCountMap = new HashMap<>();
        try {
            List<String> lines = FileUtils.readLines(new File(src), CHARSET);
            for (int i = 0; i < lines.size(); i++) {
                String line = lines.get(i);
                if (line.contains(C)) {
                    String[] split = line.split(C);
                    String tmp = split[0];
                    String memo = split[1];//获取注释
 
                    String[] split1 = tmp.split(":");
                    String name = split1[0];
                    String data = split1[1];
                    System.out.println(memo);
                    System.out.println(name);
                    System.out.println(data);
                }
            }
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
}