zrj
2024-07-03 52ef8b68a7b49d3f596266109c43dc9dfade2a9a
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package cn.gistack.sm.intelligentCall.util;
 
import cn.gistack.sm.intelligentCall.vo.CallTaskResultVO;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
 
/**
 * excel工具类
 */
public class ExcelUtil {
 
    /**
     * 导出数据
     * @param response
     * @param callTaskResultVOS
     * @param callTaskResult
     * @throws Exception
     */
    public static void generateExcel(HttpServletResponse response, List<CallTaskResultVO> callTaskResultVOS,CallTaskResultVO callTaskResult) throws Exception {
        String fileName =  callTaskResult.getStartTime() + "~" + callTaskResult.getEndTime() + "智能外呼统计情况表" + System.currentTimeMillis() + ".xlsx";
        HashMap<String, Object> info = formatInfo(callTaskResultVOS,callTaskResult);
        //获取模板(带公式的模板需要文件编辑之后才会显示正常数据)
        InputStream inputStream = ExcelUtil.class.getClassLoader().getResourceAsStream("excel/xlsx/callStatisticTemplate.xlsx");
        ExcelWriter excelWriter = EasyExcel.write(getOutputStream(fileName, response)).withTemplate(inputStream).excelType(ExcelTypeEnum.XLSX).build();
        //设置公式生效
        WriteSheet writeSheet = EasyExcel.writerSheet(0,"sheet").build();
        // 这个传进去的 map 就是非表格数据,详见我定义的模板中图片位置上方那部分
        excelWriter.fill(info, writeSheet);
        excelWriter.fill(new FillWrapper("data", callTaskResultVOS), writeSheet);
        excelWriter.finish();
    }
 
    /**
     * 数据格式化处理
     * @param callTaskResultVOS
     * @param callTaskResult
     * @return
     */
    private static HashMap<String,Object> formatInfo(List<CallTaskResultVO> callTaskResultVOS,CallTaskResultVO callTaskResult) {
        HashMap<String,Object> info = new HashMap<>();
        info.put("startTime",callTaskResult.getStartTime());
        info.put("endTime",callTaskResult.getEndTime());
        int total = callTaskResultVOS.size();
        int jtTotal = 0;
        int wtTotal = 0;
        int isNotMan = 0;
        int isMan = 0;
        int patrol = 0;
        int noOver = 0;
        int noDanger = 0;
        String bl = "0";
 
        //给总览加序号
        for (int i = 0; i < callTaskResultVOS.size(); i++) {
            CallTaskResultVO callResult = callTaskResultVOS.get(i);
            callResult.setIndex(String.valueOf(i+1));
 
            // 通话时长计算
            if (!callResult.getCallDuration().equals("0")) {
                int result = Integer.parseInt(callResult.getCallDuration());
                // double h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600)
                double m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
                double s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
                StringBuilder builder = new StringBuilder();
                builder.append(m).append(":").append(s);
                callResult.setCallDuration(builder.toString());
            }
 
            // 是否巡查责任人 0:未知或空 1:不是巡查责任人 2:是巡查责任人
            if (callResult.getIsMan().equals("0")) {
                callResult.setIsMan("");
            }
            if (callResult.getIsMan().equals("1")) {
                callResult.setIsMan("否");
            }
            if (callResult.getIsMan().equals("2")) {
                callResult.setIsMan("是");
            }
 
            // 是否已巡查 0:未知或空 1:未巡查 2:已巡查
            if (callResult.getIsPatrol().equals("0")) {
                callResult.setIsPatrol("");
            }
            if (callResult.getIsPatrol().equals("1")) {
                callResult.setIsPatrol("否");
            }
            if (callResult.getIsPatrol().equals("2")) {
                callResult.setIsPatrol("是");
            }
 
            // 是否超汛限  0:未知或空 1:未超汛限 2:超汛限
            if (callResult.getIsOver().equals("0")) {
                callResult.setIsOver("");
            }
            if (callResult.getIsOver().equals("1")) {
                callResult.setIsOver("否");
            }
            if (callResult.getIsOver().equals("2")) {
                callResult.setIsOver("是");
            }
 
            // 是否发现险情 0:未知或空 1:未发现险情 2:发现险情
            if (callResult.getIsDange().equals("0")) {
                callResult.setIsDange("");
            }
            if (callResult.getIsDange().equals("1")) {
                callResult.setIsDange("否");
            }
            if (callResult.getIsDange().equals("2")) {
                callResult.setIsDange("是");
            }
 
            // 挂断方向 A机器 B用户
            if (callResult.getCallHangupDirection().equals("A")) {
                callResult.setCallHangupDirection("机器");
            }
            if (callResult.getCallHangupDirection().equals("B")) {
                callResult.setCallHangupDirection("用户");
            }
 
            if (callResult.getCallResult().equals("200000") ||
                callResult.getIsSmsFill().equals("已反馈")
            ){
                if (callResult.getIsSmsFill().equals("已反馈")){
                    setCallResult(callResult);
                }else {
                    callResult.setCallResult("接通");
                }
                jtTotal += 1;
            }else {
                wtTotal += 1;
                setCallResult(callResult);
            }
 
            // 接通并是巡查责任人
            if ((callResult.getCallResult().equals("接通") && callResult.getIsMan().equals("是")) ||
                (callResult.getIsSmsFill().equals("已反馈") && callResult.getIsMan().equals("是"))
            ) {
                isMan += 1;
            }
 
            // 接通并不是巡查责任人
            if ((callResult.getCallResult().equals("接通") && callResult.getIsMan().equals("否")) ||
                (callResult.getIsSmsFill().equals("已反馈") && callResult.getIsMan().equals("否"))
            ) {
                isNotMan += 1;
            }
 
            // 接通并已巡查
            if ((callResult.getCallResult().equals("接通") && callResult.getIsPatrol().equals("是") && callResult.getIsMan().equals("是") ) ||
                (callResult.getIsSmsFill().equals("已反馈") && callResult.getIsPatrol().equals("是") && callResult.getIsMan().equals("是"))
            ) {
                patrol += 1;
            }
 
            // 接通并不是超汛限
            if ((callResult.getCallResult().equals("接通") && callResult.getIsOver().equals("否") && callResult.getIsMan().equals("是") ) ||
                (callResult.getIsSmsFill().equals("已反馈") && callResult.getIsOver().equals("否") && callResult.getIsMan().equals("是"))
            ) {
                noOver += 1;
            }
 
            // 接通并没有发现险情
            if ((callResult.getCallResult().equals("接通") && callResult.getIsDange().equals("否") && callResult.getIsMan().equals("是") ) ||
                (callResult.getIsSmsFill().equals("已反馈") && callResult.getIsDange().equals("否") && callResult.getIsMan().equals("是"))
            ) {
                noDanger += 1;
            }
        }
 
         if (total > 0) {
            // 计算百分比
            double number = (double) jtTotal/total;
            // 格式化
            String result = String.format("%.2f", number*100);
            // 设置
            bl = result;
            String[] split = bl.split("\\.");
            if (split.length==2){
                if (split[1].equals("00")){
                    bl = split[0];
                }
            }
        }
        // 备注说明拼接
        StringBuilder builder = new StringBuilder();
        builder.append("统计:智能外呼系统于 ")
            .append(callTaskResult.getStartTime()).append(" 至 ")
            .append(callTaskResult.getEndTime()).append(" 以来对湖北省超汛限水库进行巡查责任人电话抽查其中湖北省进行了")
            .append(total).append("人次的电话抽查,接通")
            .append(jtTotal).append("个,未接通")
            .append(wtTotal).append("个,实际触达率为")
            .append(bl).append("%;有效通话中的")
            .append(jtTotal).append("个水库,其中")
            .append(isNotMan).append("人反馈不是巡查责任人;在巡查问答环节的")
            .append(isMan).append("个责任人中,")
            .append(patrol).append("人均已表示巡查,")
            .append(noOver).append("人表示水位未超汛限水位,")
            .append(noDanger).append("人表示未发现险情。");
        // 设置说明
        info.put("remark", builder.toString());
        // 返回
        return info;
    }
 
    /**
     * 设置通话结果
     * @param callResult
     */
    private static void setCallResult(CallTaskResultVO callResult) {
        if (callResult.getCallResult().equals("200001")) {
            callResult.setCallResult("用户提前挂机未完整收听");
        }
        if (callResult.getCallResult().equals("200002")) {
            callResult.setCallResult("占线");
        }
        if (callResult.getCallResult().equals("200003")) {
            callResult.setCallResult("未接通");
        }
        if (callResult.getCallResult().equals("200004")) {
            callResult.setCallResult("空号");
        }
        if (callResult.getCallResult().equals("200005")) {
            callResult.setCallResult("拒接");
        }
        if (callResult.getCallResult().equals("200007")) {
            callResult.setCallResult("无法接通/不在服务区");
        }
        if (callResult.getCallResult().equals("200010")) {
            callResult.setCallResult("关机");
        }
        if (callResult.getCallResult().equals("200011")) {
            callResult.setCallResult("停机");
        }
        if (callResult.getCallResult().equals("200100")) {
            callResult.setCallResult("转接");
        }
        if (callResult.getCallResult().equals("200130")) {
            callResult.setCallResult("其他(无法识别)");
        }
    }
 
    /**
     * 导出文件时为Writer生成OutputStream.
     *
     * @param fileName 文件名
     * @param response response
     * @return ""
     */
    public static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf8");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            return response.getOutputStream();
        } catch (IOException e) {
            throw new Exception("导出excel表格失败!", e);
        }
    }
}