whyclxw
2025-02-13 f4739452add5718a1e4d55172ba85c4d779747d7
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whyc.mapper.LockInfMapper;
import com.whyc.pojo.db_area.LockInf;
import com.whyc.pojo.db_lock_his.LockHis;
import com.whyc.util.ActionUtil;
import com.whyc.util.SubTablePageInfoUtil;
import com.whyc.util.ThreadLocalUtil;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
@Service
public class ExportService {
    @Autowired
    private SubTablePageInfoUtil util;
 
    @Autowired(required = false)
    private LockInfMapper linfMapper;
 
    //历史数据导出
    public void exportHis(int lockId, String startTime, String endTime, HttpServletRequest req, HttpServletResponse resp) throws ParseException {
        LockHis his=new LockHis();
        his.setLockId(lockId);
        List<LockHis> list=util.getLockHisWithReal(ThreadLocalUtil.parse(startTime,1),ThreadLocalUtil.parse(endTime,1)
                ,"db_lock_his","tb_lock_his_"+lockId,his);
        //获取锁的信息
        LockInf linf=linfMapper.getlinfBylockId(lockId);
 
        //当前日期
        String nowFormat = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String excelName = "bl-"+nowFormat;
 
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("锁的基础信息");
 
        XSSFDrawing patriarch = sheet.createDrawingPatriarch();
        int rowNumSheet = 0;
        sheet.createRow(rowNumSheet);
        sheet.getRow(rowNumSheet).createCell(0).setCellValue("所属区域:"+linf.getAreaName());
        sheet.getRow(rowNumSheet).createCell(1).setCellValue("锁的名称:"+linf.getLockName());
        sheet.getRow(rowNumSheet).createCell(2).setCellValue("锁的类型:实体锁");
        rowNumSheet++;
 
        sheet.createRow(rowNumSheet);
        switch (linf.getLockState()){
            case -1:sheet.getRow(rowNumSheet).createCell(0).setCellValue("锁的状态:未安装");break;
            case 0:sheet.getRow(rowNumSheet).createCell(0).setCellValue("锁的状态:闭锁");break;
            case 1:sheet.getRow(rowNumSheet).createCell(0).setCellValue("锁的状态:开锁");break;
        }
        switch (linf.getLockOnline()){
            case 0:sheet.getRow(rowNumSheet).createCell(1).setCellValue("锁的状态:离线");break;
            case 1:sheet.getRow(rowNumSheet).createCell(1).setCellValue("锁的状态:在线");break;
        }
        rowNumSheet++;
        sheet.createRow(rowNumSheet);
        sheet.getRow(rowNumSheet).createCell(0).setCellValue("锁具开启次数:"+linf.getLockOpenCount());
        sheet.getRow(rowNumSheet).createCell(1).setCellValue("上一次开锁时间:"+ThreadLocalUtil.format(linf.getLastUpdateTime(),1));
 
        rowNumSheet++;
        sheet.createRow(rowNumSheet);
        sheet.getRow(rowNumSheet).createCell(0).setCellValue("屏柜类型:"+linf.getScreenBoxType());
        sheet.getRow(rowNumSheet).createCell(1).setCellValue("屏柜品牌:"+linf.getScreenBoxProduct());
 
        rowNumSheet++;
        sheet.createRow(rowNumSheet);
        sheet.getRow(rowNumSheet).createCell(0).setCellValue("开始时间:"+startTime);
        sheet.getRow(rowNumSheet).createCell(1).setCellValue("结束时间:"+endTime);
 
        XSSFSheet sheet1 = wb.createSheet("历史数据");
        //抬头
        int rowNum = 0;
        XSSFRow row = sheet1.createRow(rowNum);
        row.createCell(0).setCellValue("记录时间(yyyy-MM-dd HH:mm:ss)");
        row.createCell(1).setCellValue("锁具状态");
        row.createCell(2).setCellValue("开锁方式");
        rowNum++;
        //数据栏
        for (int i = 0; i < list.size(); i++) {
            sheet1.createRow(rowNum);  //创建行
            LockHis lock=list.get(i);
            sheet1.getRow(rowNum).createCell(0).setCellValue(ThreadLocalUtil.format(lock.getRecordTime(),1));
            sheet1.getRow(rowNum).createCell(1).setCellValue(lock.getLockState()==0?"闭锁":lock.getLockState()==1?"开锁":"未知");
            switch (lock.getUnlockType()){
                case 0:sheet1.getRow(rowNum).createCell(2).setCellValue("485开锁");break;
                case 1:sheet1.getRow(rowNum).createCell(2).setCellValue("刷卡开锁");break;
                case 2:sheet1.getRow(rowNum).createCell(2).setCellValue("蓝牙开锁");break;
                case 3:sheet1.getRow(rowNum).createCell(2).setCellValue("DI开锁");break;
            }
            rowNum++;
        }
 
        rowNum++;
 
        try {
            // 转码防止乱码
            resp.addHeader("Content-Disposition", "attachment;filename="
                    + new String(excelName.getBytes("UTF-8"), "ISO8859-1")
                    + ".xlsx");
            OutputStream out = resp.getOutputStream();
            wb.write(out);
            out.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}