2021与蓝度共同重构项目,服务端
fix
zhanzhiqin
2022-05-27 ce72201c7cd63b8eb02af930b7b8302d5395ae6a
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
package com.sandu.ximon.admin.service;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sandu.common.domain.CommonPage;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.file.config.FileProperties;
import com.sandu.common.file.impl.AliOssFileServiceImpl;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5LightHeartbeatReportInnerFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.enums.A5LightDataEnum;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.RedisUtils;
import com.sandu.ximon.dao.bo.LightReportDataBo;
import com.sandu.ximon.dao.domain.LightReportData;
import com.sandu.ximon.dao.domain.Pole;
import com.sandu.ximon.dao.domain.PoleBinding;
import com.sandu.ximon.dao.mapper.LightReportDataMapper;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author chenjiantian
 * @date 2021/12/13 16:00
 * 灯上报数据 service
 */
@Service
@AllArgsConstructor
public class LightReportDataService extends BaseServiceImpl<LightReportDataMapper, LightReportData> {
 
    private final FileProperties properties;
    private final AliOssFileServiceImpl fileService;
    private final LightReportDataMapper lightReportDataMapper;
 
    /**
     * 保存上报的灯心跳数据
     *
     * @return 是否成功
     */
    public boolean saveReportData(String deviceName, A5LightHeartbeatReportInnerFrame.HeartBeatDataPackage heartBeatDataPackage) {
//        LightReportData lightReportData = RedisUtils.getBean().get(A5LightDataEnum.LIGHT_HEART_BEAT.getCode() + deviceName, LightReportData.class);
 
 
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss"));
        LightReportData lightReportData = new LightReportData();
 
        if (lightReportData == null) {
            lightReportData = new LightReportData();
            lightReportData.setCreateTime(format);
        }
        //todo
        lightReportData.setCreateTime(format);
        BeanUtils.copyProperties(heartBeatDataPackage, lightReportData);
        lightReportData.setDeviceCode(deviceName);
 
        lightReportData.setUpdateTime(format);
        //单灯数据保存到缓存里
        RedisUtils.getBean().set(A5LightDataEnum.LIGHT_HEART_BEAT.getCode() + deviceName, lightReportData);
 
        return save(lightReportData);
    }
 
    /**
     * 获取指定设备码最新的一天上报数据
     *
     * @param deviceCodeList 设备码列表
     * @return 上报数据
     */
    public List<LightReportData> getNewestReportByDeviceCode(List<String> deviceCodeList) {
        return baseMapper.getNewestReportByDeviceCode(deviceCodeList);
    }
 
    /**
     * 获取上报数据
     *
     * @param keyword    关键词
     * @param deviceCode 设备码
     */
    public CommonPage listReportData(int pageNo, int pageSize, String keyword, String deviceCode) {
 
        List<LightReportDataBo> lightReportDataBos = new ArrayList<>(pageSize);
 
        CommonPage<String> stringCommonPage = SpringContextHolder.getBean(LightService.class).listDeviceCode(pageNo, pageSize, keyword, deviceCode);
        List<String> macList = stringCommonPage.getList();
        if (CollUtil.isEmpty(macList)) {
            return new CommonPage();
        }
 
        for (String macCode : macList) {
            try {
                LightReportData lightReportData
                        = JSON.parseObject(RedisUtils.getBean().get(A5LightDataEnum.LIGHT_HEART_BEAT.getCode() + macCode), LightReportData.class);
 
                LightReportDataBo lightReportDataBo = new LightReportDataBo();
                if (lightReportData != null) {
                    BeanUtils.copyProperties(lightReportData, lightReportDataBo);
                }
                lightReportDataBo.setDeviceCode(macCode);
                Pole pole = SpringContextHolder.getBean(PoleService.class).getOne(Wrappers.lambdaQuery(Pole.class).eq(Pole::getDeviceCode, macCode));
                if (pole != null) {
                    lightReportDataBo.setPoleName(pole.getPoleName());
                }
                lightReportDataBos.add(lightReportDataBo);
 
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
        CommonPage commonPage = CommonPage.restPage(lightReportDataBos);
        commonPage.setTotal(stringCommonPage.getTotal());
        commonPage.setTotalPage(stringCommonPage.getTotalPage());
        commonPage.setPageNum(pageNo);
 
        return commonPage;
    }
 
    @SneakyThrows
    public void exportList(HttpServletRequest request, HttpServletResponse response, String deviceCode) {
 
        if (SecurityUtils.getClientId() != null) {
            PoleBinding one = SpringContextHolder.getBean(PoleBindingService.class).getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceCode, deviceCode)
                    .eq(PoleBinding::getDeviceType, 0));
            if (one == null) {
                throw new BusinessException("未找到绑定关系!");
            } else {
                Pole pole = SpringContextHolder.getBean(PoleService.class).getOne(Wrappers.lambdaQuery(Pole.class).eq(Pole::getUserId, SecurityUtils.getUserId()).or(w -> {
                    w.eq(Pole::getClientId, SecurityUtils.getUserId());
                }));
                if (pole == null) {
                    throw new BusinessException("绑定关系不正确!");
                }
            }
        }
 
        List<LightReportDataBo> list = lightReportDataMapper.listReportData(null, deviceCode);
 
        File file = new File("./" + RandomUtil.randomString(12) + ".xlsx");
 
        EasyExcel.write(file, LightReportDataBo.class).sheet("模版").doWrite(list);
 
        //文件流化返回给前端
        findfile(request, response, file);
        //删除文件
       // file.delete();
    }
 
    /**
     * 文件流化
     *
     * @param request
     * @param response
     * @param file
     * @throws IOException
     */
    public void findfile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException {
        ServletOutputStream out = null;
        FileInputStream ips = null;
        try {
            //获取到文字 数据库里对应的附件名字加上老的文件名字:filename 截取到后面的文件类型 例:txt  组成一个新的文件名字:newFileName
            //生成3个随机数,用来生成新的文件名字
            String newFileName = "单灯数据" + RandomUtil.randomString(3) + ".xlsx";
            if (!file.exists()) {
                //如果文件不存在就跳出
                return;
            }
            ips = new FileInputStream(file);
            response.setContentType("application/json;charset=utf-8");
            //为文件重新设置名字,采用数据库内存储的文件名称
            response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(newFileName.getBytes("UTF-8"), "ISO8859-1") + "\"");
            out = response.getOutputStream();
            //读取文件流
            int len = 0;
            byte[] buffer = new byte[1024 * 10];
            while ((len = ips.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                ips.close();
                file.delete();
            } catch (IOException e) {
                System.out.println("关闭流出现异常");
                e.printStackTrace();
            }
        }
    }
}