李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
/*
 *    Copyright (c) 2018-2025, ztt All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: ztt
 */
package com.chinaztt.mes.basic.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.mes.basic.entity.IfsLog;
import com.chinaztt.mes.basic.excel.MaterialIssueStdData;
import com.chinaztt.mes.basic.excel.MaterialUnIssueStdData;
import com.chinaztt.mes.basic.excel.ReceiveStdData;
import com.chinaztt.mes.basic.mapper.IfsLogMapper;
import com.chinaztt.mes.basic.service.IfsLogService;
import com.chinaztt.mes.common.wrapper.QueryWrapperUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * ifs请求日志
 *
 * @author shz
 * @date 2022-08-25 16:49:17
 */
@Service
public class IfsLogServiceImpl extends ServiceImpl<IfsLogMapper, IfsLog> implements IfsLogService {
 
    @Resource
    private IfsLogMapper ifsLogMapper;
 
    private final List<String> orderInterfaceNames = Arrays.asList("IMPORT_SHOP_ORDER_STD", "IMPORT_SO_SMA_SOPER_STD",
            "IMPORT_SHOP_MATERIAL_ALLOC_STD", "MODIFY_SHOP_MATERIAL_ALLOC_STD", "REMOVE_SHOP_MATERIAL_ALLOC_STD",
            "IMPORT_SHOP_OPERATION_STD", "MODIFY_SHOP_OPERATION_STD", "REMOVE_SHOP_OPERATION_STD",
            "MODIFY_SO_PRE_CLOSE_STD", "IMPORT_SO_MATERIAL_ISSUE_STD", "IMPORT_SO_MATERIAL_UNISSUE_STD",
            "IMPORT_SO_OPER_REPORT_STD", "IMPORT_SO_RECEIVE_STD", "IMPORT_SO_UNRECEIVE_STD",
            "IMPORT_SO_OPER_SCRAP_STD", "IMPORT_SO_OPER_UNSCRAP_STD", "MODIFY_SO_STATE_STD");
 
    @Override
    public void refreshIfsLogOrderNo() {
        List<IfsLog> ifsLogs = ifsLogMapper.selectList(Wrappers.<IfsLog>lambdaQuery().eq(IfsLog::getState,
                Boolean.FALSE).in(IfsLog::getProcedureName, orderInterfaceNames));
        if (CollectionUtil.isEmpty(ifsLogs)) {
            return;
        }
        Map<String, List<IfsLog>> ifsLogMap = ifsLogs.stream().collect(Collectors.groupingBy(IfsLog::getProcedureName));
 
 
    }
 
 
    @Override
    public void export(HttpServletResponse response, String type) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("ifs缓存解析", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        try {
            //新建ExcelWriter
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
            WriteSheet mainSheet = null;
            List<IfsLog> ifsLogList = new ArrayList<>();
            List<String> inAttrList = new ArrayList<>();
 
            switch (type){
                case "IMPORT_SO_RECEIVE_STD": case "IMPORT_SO_UNRECEIVE_STD":
                    //获取sheet0对象
                    mainSheet = EasyExcel.writerSheet(0, "ifs缓存解析").head(ReceiveStdData.class).build();
                    ifsLogList = ifsLogMapper.selectList(Wrappers.<IfsLog>lambdaQuery().eq(IfsLog::getProcedureName, type)
                            .eq(IfsLog::getActive, true)
                            .eq(IfsLog::getState,false));
                    inAttrList = ifsLogList.stream().map(IfsLog::getInAttr).collect(Collectors.toList());
                    List<ReceiveStdData> receiveStdDataList = new ArrayList<>();
                    for (String inAttr: inAttrList) {
                        JSONObject jsonObject = JSONObject.parseObject(inAttr);
                        JSONArray batchInfoArray = jsonObject.getJSONArray("BATCH_INFO");
                        for (int i = 0; i <batchInfoArray.size() ; i++) {
                            ReceiveStdData receiveStdData = new ReceiveStdData();
                            JSONObject batchInfoObject = batchInfoArray.getJSONObject(i);
                            receiveStdData.setPartNo(batchInfoObject.getString("PART_NO"));
                            receiveStdData.setQuantity(batchInfoObject.getString("QUANTITY"));
                            receiveStdData.setOrderNo(batchInfoObject.getString("ORDER_NO"));
                            receiveStdData.setLotBatchNo(batchInfoObject.getString("LOT_BATCH_NO"));
                            receiveStdData.setWaivDevRejNo(batchInfoObject.getString("WAIV_DEV_REJ_NO"));
                            receiveStdData.setLocationNo(batchInfoObject.getString("LOCATION_NO"));
                            receiveStdData.setEngChgLevel(batchInfoObject.getString("ENG_CHG_LEVEL"));
                            String mesParams = jsonObject.getString("mesParams");
                            if(StringUtils.isNotBlank(mesParams)){
                                receiveStdData.setProductMainId(mesParams.substring(mesParams.indexOf(":")+1,mesParams.indexOf("}")));
                            }
                            receiveStdData.setSysModel(jsonObject.getString("SYSMODEL"));
                            receiveStdDataList.add(receiveStdData);
                        }
                    }
                    excelWriter.write(receiveStdDataList, mainSheet);
                    break;
                case "IMPORT_SO_MATERIAL_ISSUE_STD":
                    //获取sheet0对象
                    mainSheet = EasyExcel.writerSheet(0, "ifs缓存解析").head(MaterialIssueStdData.class).build();
                    ifsLogList = ifsLogMapper.selectList(Wrappers.<IfsLog>lambdaQuery().eq(IfsLog::getProcedureName, type)
                            .eq(IfsLog::getActive, true)
                            .eq(IfsLog::getState,false));
                    inAttrList = ifsLogList.stream().map(IfsLog::getInAttr).collect(Collectors.toList());
                    List<MaterialIssueStdData> materialIssueStdDataList = new ArrayList<>();
                    for (String inAttr: inAttrList) {
                        JSONObject jsonObject = JSONObject.parseObject(inAttr);
                        JSONArray batchInfoArray = jsonObject.getJSONArray("BATCH_INFO");
                        for (int i = 0; i <batchInfoArray.size() ; i++) {
                            MaterialIssueStdData materialIssueStdData = new MaterialIssueStdData();
                            JSONObject batchInfoObject = batchInfoArray.getJSONObject(i);
                            materialIssueStdData.setPartNo(batchInfoObject.getString("PART_NO"));
                            materialIssueStdData.setQtyToIssue(batchInfoObject.getString("QTY_TO_ISSUE"));
                            materialIssueStdData.setOrderNo(batchInfoObject.getString("ORDER_NO"));
                            materialIssueStdData.setLotBatchNo(batchInfoObject.getString("LOT_BATCH_NO"));
                            materialIssueStdData.setWaivDevRejNo(batchInfoObject.getString("WAIV_DEV_REJ_NO"));
                            materialIssueStdData.setLocationNo(batchInfoObject.getString("LOCATION_NO"));
                            materialIssueStdData.setEngChgLevel(batchInfoObject.getString("ENG_CHG_LEVEL"));
                            materialIssueStdData.setLineItemNo(batchInfoObject.getString("LINE_ITEM_NO"));
                            String mesParams = jsonObject.getString("mesParams");
                            if(StringUtils.isNotBlank(mesParams)){
                                materialIssueStdData.setProductMainId(mesParams.substring(mesParams.indexOf(":")+1,mesParams.indexOf("}")));
                            }
                            materialIssueStdData.setSysModel(jsonObject.getString("SYSMODEL"));
                            materialIssueStdDataList.add(materialIssueStdData);
                        }
                    }
                    excelWriter.write(materialIssueStdDataList, mainSheet);
                    break;
                case "IMPORT_SO_MATERIAL_UNISSUE_STD":
                    //获取sheet0对象
                    mainSheet = EasyExcel.writerSheet(0, "ifs缓存解析").head(MaterialUnIssueStdData.class).build();
                    ifsLogList = ifsLogMapper.selectList(Wrappers.<IfsLog>lambdaQuery().eq(IfsLog::getProcedureName, type)
                            .eq(IfsLog::getActive, true)
                            .eq(IfsLog::getState,false));
                    inAttrList = ifsLogList.stream().map(IfsLog::getInAttr).collect(Collectors.toList());
                    List<MaterialUnIssueStdData> materialUnIssueStdDataList = new ArrayList<>();
                    for (String inAttr: inAttrList) {
                        JSONObject jsonObject = JSONObject.parseObject(inAttr);
                        JSONArray batchInfoArray = jsonObject.getJSONArray("BATCH_INFO");
                        for (int i = 0; i <batchInfoArray.size() ; i++) {
                            MaterialUnIssueStdData materialUnIssueStdData = new MaterialUnIssueStdData();
                            JSONObject batchInfoObject = batchInfoArray.getJSONObject(i);
                            materialUnIssueStdData.setPartNo(batchInfoObject.getString("PART_NO"));
                            materialUnIssueStdData.setQtyToUnIssue(batchInfoObject.getString("QTY_TO_UNISSUE"));
                            materialUnIssueStdData.setOrderNo(batchInfoObject.getString("ORDER_NO"));
                            materialUnIssueStdData.setLotBatchNo(batchInfoObject.getString("LOT_BATCH_NO"));
                            materialUnIssueStdData.setWaivDevRejNo(batchInfoObject.getString("WAIV_DEV_REJ_NO"));
                            materialUnIssueStdData.setLocationNo(batchInfoObject.getString("LOCATION_NO"));
                            materialUnIssueStdData.setEngChgLevel(batchInfoObject.getString("ENG_CHG_LEVEL"));
                            materialUnIssueStdData.setLineItemNo(batchInfoObject.getString("LINE_ITEM_NO"));
                            String mesParams = jsonObject.getString("mesParams");
                            if(StringUtils.isNotBlank(mesParams)){
                                materialUnIssueStdData.setProductMainId(mesParams.substring(mesParams.indexOf(":")+1,mesParams.indexOf("}")));
                            }
                            materialUnIssueStdData.setSysModel(jsonObject.getString("SYSMODEL"));
                            materialUnIssueStdDataList.add(materialUnIssueStdData);
                        }
                    }
                    excelWriter.write(materialUnIssueStdDataList, mainSheet);
                    break;
            }
            //关闭流
            excelWriter.finish();
        } catch (IOException e) {
            throw new RuntimeException("导出失败");
        }
    }
}