yangys
2025-09-29 4c7296d45efe849dc70a3b2e2240c905481a91c9
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package org.springblade.mdm.program.service.programannotation;
 
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.mdm.program.service.ProgramAnnotationService;
import org.springblade.mdm.utils.FileContentUtil;
import org.springblade.system.pojo.entity.DictBiz;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
 
public abstract class AbstractProcessor implements AnnotationProcessor{
 
    @Autowired
    private ProgramAnnotationService programAnnotationService;
 
    protected AnnotationProperties annotationProperties;
 
    protected List<DictBiz> getAnnotationDictList(){
        return programAnnotationService.getAnnotionDictList();
    }
 
    @Override
    public InputStream putSendPathAnnotation(String sendPath, InputStream inputStream,List<DictBiz> annoDicts) throws IOException{
        String sendPathRepl = StringUtils.replace(sendPath,"\\","/");
        InputStream finishedStream;
        try(inputStream){
            ByteArrayInputStream byteInputStream =  new ByteArrayInputStream(IOUtils.toByteArray(inputStream));
 
            //1加入发送路径的注释
            String sendPathAnnotation = AnnotationUtil.generateAnnotation(sendPathRepl,getControlSystem(),annoDicts);//加了注释之后的文本
 
            String sendPathLine = FileContentUtil.readLineAt(byteInputStream,annotationProperties.getSendPathLineIndex());//第2行是发送路径
            byteInputStream.reset();
 
            if(AnnotationUtil.isAnnotation(sendPathLine,getControlSystem(),annoDicts)){
                String planText = AnnotationUtil.removeAnnotation(getControlSystem(),sendPathLine,annoDicts);
                //String planTextRepl = StringUtils.replace(planText,"\\","/");
                if(!planText.equals(sendPath) && !planText.equals(sendPathRepl)) {
                    //非路径的注释,插入
                    finishedStream = FileContentUtil.insertLine(byteInputStream,annotationProperties.getSendPathLineIndex(),sendPathAnnotation);
                }else{
                    //是路径,不处理直接返回原输入流
                    finishedStream = inputStream;
                }
            }else{
                finishedStream = FileContentUtil.insertLine(byteInputStream,annotationProperties.getSendPathLineIndex(),sendPathAnnotation);
            }
            finishedStream.reset();
        }
 
        return finishedStream;
 
    }
    @Override
    public InputStream putFilenameAnnotation(String fileName, InputStream inputStream) throws IOException {
        List<DictBiz> annoDicts = programAnnotationService.getAnnotionDictList();//TODO 这里应该不用加载,参数传过来就行
        ByteArrayInputStream byteStream = new ByteArrayInputStream(IOUtils.toByteArray(inputStream));
 
        String programNameLine = FileContentUtil.readLineAt(byteStream, annotationProperties.getProgramNameLineIndex());
        byteStream.reset();
        boolean isAnnotation = AnnotationUtil.isAnnotation(programNameLine,this.getControlSystem(),annoDicts);
        InputStream insAfterProgramName;
        //加入程序名注释行
        String proNameAnnotation =  AnnotationUtil.generateAnnotation(FilenameUtils.removeExtension(fileName),getControlSystem(),annoDicts);
 
        if(isAnnotation){//是注释
            //提取注释内文字
            //String planText = AnnotationUtil.removeAnnotation(getControlSystem(),programNameline,annoDicts);
            if(!StringUtils.equals(programNameLine,proNameAnnotation)) {
                //注释不是程序名(工艺员自己写的注释),插入
                insAfterProgramName = FileContentUtil.insertLine(byteStream,annotationProperties.getProgramNameLineIndex(),proNameAnnotation);
            }else{
                //是程序名注释,不处理,返回原stream
                insAfterProgramName =  byteStream;
            }
        }else{
            //非注释,插入状态
            insAfterProgramName = FileContentUtil.insertLine(byteStream,annotationProperties.getProgramNameLineIndex(),proNameAnnotation);
        }
 
        return insAfterProgramName;
    }
 
    @Override
    public InputStream putAnnotation(AnnotationData annoData, InputStream inputStream) throws IOException {
        List<DictBiz> annoDicts = programAnnotationService.getAnnotionDictList();
 
        InputStream finishedStream;
        try(inputStream){
            ByteArrayInputStream byteInputStream =  new ByteArrayInputStream(IOUtils.toByteArray(inputStream));
 
            //1加入发送路径的注释
            InputStream insAfterSetSendDir = putSendPathAnnotation(annoData.getSendPath(),byteInputStream,annoDicts);
 
            InputStream insAfterStatus = putStatusAnnotation(annoData.getProgramStatus(),insAfterSetSendDir,annoDicts);
 
            if(StringUtils.isNotBlank(annoData.getDeviation())){
                finishedStream = putDeviationAnnotation(annoData.getDeviation(),insAfterStatus,annoDicts);
            }else{
                //是更改单号,不处理直接返回原输入流
                finishedStream = insAfterStatus;
            }
        }
 
        return finishedStream;
    }
 
    protected InputStream putStatusAnnotation(String status,InputStream inputStream,List<DictBiz> annoDicts) throws IOException {
        InputStream insAfterStatus;
        //加入lg单号
 
        String statusLine = FileContentUtil.readLineAt(inputStream,annotationProperties.getStatusLineIndex());//状态注释
        String stationAnnotation = AnnotationUtil.generateAnnotation(status,getControlSystem(),annoDicts);
 
        inputStream.reset();
        if(AnnotationUtil.isAnnotation(statusLine,getControlSystem(),annoDicts) ){
            //是注释
            //提取注释内文字
            String planText = AnnotationUtil.removeAnnotation(getControlSystem(),statusLine,annoDicts);
            if(!StringUtils.equalsAny(planText,AnnotationUtil.SQ,AnnotationUtil.GH,AnnotationUtil.LG)) {
                //注释不是状态(工艺员自己写的注释),插入
                insAfterStatus = FileContentUtil.insertLine(inputStream,annotationProperties.getStatusLineIndex(),stationAnnotation);
            }else{
                //是状态注释,替换
                insAfterStatus =  FileContentUtil.replaceAtLine(inputStream,annotationProperties.getStatusLineIndex(),stationAnnotation);
            }
        }else{
            //非注释,插入状态
            insAfterStatus = FileContentUtil.insertLine(inputStream,annotationProperties.getStatusLineIndex(),stationAnnotation);
        }
        return insAfterStatus;
    }
 
    protected InputStream putDeviationAnnotation(String deviation,InputStream inputStream,List<DictBiz> annoDicts) throws IOException {
        InputStream insAfter1;
        //加入lg单号
        String devLine = FileContentUtil.readLineAt(inputStream,annotationProperties.getDeviationLineIndex());//状态注释
 
        String devAnnotation = AnnotationUtil.generateAnnotation(deviation,getControlSystem(),annoDicts);
 
        inputStream.reset();
        if(AnnotationUtil.isAnnotation(devLine,getControlSystem(),annoDicts) ){
            //是注释
            //提取注释内文字
            String planText = AnnotationUtil.removeAnnotation(getControlSystem(),devLine,annoDicts);
            if(!planText.equals(deviation)) {
                //注释不是临时更改单号(工艺员自己写的注释),插入
                insAfter1 = FileContentUtil.insertLine(inputStream,annotationProperties.getDeviationLineIndex(),devAnnotation);
            }else{
                //是更改单号,不处理直接返回原输入流
                insAfter1 = inputStream;
            }
        }else{
            //非注释,怎插入lg号
            insAfter1 = FileContentUtil.insertLine(inputStream,annotationProperties.getDeviationLineIndex(),devAnnotation);
        }
        return insAfter1;
    }
 
    @Override
    public abstract void setControlSystem(String controlSystemDictVal) ;
 
    @Override
    public void setAnnotationProperties(AnnotationProperties annotationProperties) {
        this.annotationProperties= annotationProperties;
    }
 
    @Override
    public AnnotationProperties getAnnotationProperties(){
        return annotationProperties;
    }
    public abstract String getControlSystem();
 
    @Override
    public AnnotationData readAnnotationData(InputStream inputStream) {
        AnnotationData data = new AnnotationData();
 
        int maxLineIndex = maxAnnotationLineIndex();
        try {
            int lineCount = maxLineIndex+1;
            List<String> lines = FileContentUtil.readFirstNLines(inputStream,lineCount);
            fixLine(lines,lineCount);
 
            String programNameLine = lines.get(this.getAnnotationProperties().getProgramNameLineIndex());
            String sendPathLine = lines.get(this.getAnnotationProperties().getSendPathLineIndex());
            String statusLine = lines.get(this.getAnnotationProperties().getStatusLineIndex());
            String devLine =  lines.get(this.getAnnotationProperties().getDeviationLineIndex());
 
            List<DictBiz> annoDictList= this.programAnnotationService.getAnnotionDictList();
            data.setFilename(AnnotationUtil.removeAnnotation(this.getControlSystem(),programNameLine,annoDictList));
            data.setSendPath(AnnotationUtil.removeAnnotation(this.getControlSystem(),sendPathLine,annoDictList));
 
            String statusText = AnnotationUtil.removeAnnotation(this.getControlSystem(),statusLine,annoDictList);
            if(AnnotationUtil.isStatusContent(statusText)){
                data.setProgramStatus(statusText);
 
                if(AnnotationUtil.LG.equals(statusText)) {
                    data.setDeviation(AnnotationUtil.removeAnnotation(this.getControlSystem(), devLine, annoDictList));
                }
            }
 
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
 
        return data;
    }
 
    /**
     * 没有的行填充未空串
     * @param lines
     * @return
     */
    void fixLine(List<String> lines,int fixLineCount){
        int needFix = fixLineCount - lines.size();
        if(needFix > 0){
            for(int i = 0 ; i < needFix ; i++){
                lines.add(StringUtils.EMPTY);
            }
        }
    }
 
    private int maxAnnotationLineIndex(){
        int max = this.getAnnotationProperties().getDeviationLineIndex();
        if(max < this.getAnnotationProperties().getStatusLineIndex()){
            max = this.getAnnotationProperties().getStatusLineIndex();
        }
 
        if(max < this.getAnnotationProperties().getSendPathLineIndex()){
            max = this.getAnnotationProperties().getSendPathLineIndex();
        }
        if(max < this.getAnnotationProperties().getProgramNameLineIndex()){
            max = this.getAnnotationProperties().getProgramNameLineIndex();
        }
        return max;
    }
}