张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
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
package com.thhy.engineering.modules.biz.device.service.impl;
 
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.thhy.engineering.modules.biz.device.entity.TBigDevice;
import com.thhy.engineering.modules.biz.device.entity.TBigPath;
import com.thhy.engineering.modules.biz.device.entity.dto.BigDeviceExcelDto;
import com.thhy.engineering.modules.biz.device.mapper.TBigDeviceMapper;
import com.thhy.engineering.modules.biz.device.service.TBigDeviceService;
import com.thhy.engineering.modules.biz.file.service.FileService;
import com.thhy.general.common.BasicResult;
import com.thhy.general.config.SysUserInfo;
import com.thhy.general.utils.UserInfoUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class TBigDeviceServiceImpl implements TBigDeviceService {
 
    @Resource
    private TBigDeviceMapper bigDeviceMapper;
 
    @Resource
    private FileService fileService;
 
    @Override
    @Transactional
    public BasicResult bigDeviceInsert(Map<String, Object> values) {
 
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
 
        String bigNumber = values.get("bigNumber").toString();
        String bigDeviceId = bigDeviceMapper.bigNumberByOne(bigNumber,companyId);
        if(!StringUtil.isEmpty(bigDeviceId)){
            return BasicResult.faild("11111","设备编号重复","error");
        }
        values.put("bigDeviceId","");
        values.put("companyId",companyId);
        bigDeviceMapper.bigDeviceInsert(values);
        //大型生产设备id
        String bigDeviceId1 = values.get("bigDeviceId").toString();
        //图片 列表
        List<String> pictures = (List<String>) values.get("pictures");
        if(pictures!=null && pictures.size()>0){
            for (String p:pictures){
                HashMap<String, Object> val = new HashMap<>();
                val.put("bigDeviceId",bigDeviceId1);
                val.put("id","");
                val.put("fileType",1);
                val.put("filePath",p);
                bigDeviceMapper.picturesInsert(val);
            }
        }
        //文件
        List<String> files = (List<String>) values.get("files");
        if(files!=null && files.size()>0){
            for (String f:files){
                HashMap<String, Object> val = new HashMap<>();
                val.put("bigDeviceId",bigDeviceId1);
                val.put("id","");
                val.put("fileType",2);
                val.put("filePath",f);
                bigDeviceMapper.picturesInsert(val);
            }
        }
        return BasicResult.success("添加成功");
    }
 
    @Override
    public BasicResult bigDeviceList(Map<String, Object> values) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        values.put("companyId",companyId);
        Integer pageSize = Integer.valueOf(values.get("pageSize").toString());
        Integer pageNum = Integer.valueOf(values.get("pageNum").toString());
        PageHelper.startPage(pageNum,pageSize);
        List<TBigDevice> tBigDevices = bigDeviceMapper.bigDeviceList(values);
        PageInfo<TBigDevice> tBigDevicePageInfo = new PageInfo<>(tBigDevices);
        return BasicResult.success(tBigDevicePageInfo);
    }
 
    @Override
    public BasicResult bigDeviceInfo(String bigDeviceId) {
        TBigDevice tBigDevices = bigDeviceMapper.bigDeviceInfo(bigDeviceId);
        List<TBigPath> bigPaths = bigDeviceMapper.devicePath(bigDeviceId);
        bigPaths.stream().forEach(fl->{
            JSONObject json = new JSONObject();
            json.put("fullpath",fl.getFilePath());
            BasicResult basicResult = fileService.fileinfo(json.toJSONString());
            if(basicResult.isSuccess()){
                JSONArray jsonArray = JSONArray.parseArray(basicResult.getData().toString());
                for(Object obj : jsonArray){
                    JSONObject j = JSON.parseObject(obj.toString());
                    if(j.containsKey("name")&&"name".equals(j.getString("name"))){
                        fl.setFilePathName(j.getString("value"));
                    }
                }
            }
        });
        tBigDevices.setTBigPaths(bigPaths);
        return BasicResult.success(tBigDevices);
    }
 
    @Override
    @Transactional
    public BasicResult bigDeviceDel(String bigDeviceId) {
        bigDeviceMapper.bigDeviceDel(bigDeviceId);
        bigDeviceMapper.bigDevicePathDel(bigDeviceId);
        return BasicResult.success("删除成功");
    }
 
    @Override
    @Transactional
    public BasicResult bigDeviceUpdate(Map<String, Object> values) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        String bigNumber = values.get("bigNumber").toString();
        String bigDeviceId = bigDeviceMapper.bigNumberByOne(bigNumber,companyId);
        String bigDeviceId1 = values.get("bigDeviceId").toString();
        if(!StringUtil.isEmpty(bigDeviceId)&&!bigDeviceId.equals(bigDeviceId1)){
            return BasicResult.faild("11111","设备编号重复","error");
        }
        bigDeviceMapper.bigDevicePathDel(bigDeviceId1);
 
        List<String> pictures = (List<String>) values.get("pictures");
        if(pictures!=null && pictures.size()>0){
            for (String p:pictures){
                HashMap<String, Object> val = new HashMap<>();
                val.put("bigDeviceId",bigDeviceId1);
                val.put("id","");
                val.put("fileType",1);
                val.put("filePath",p);
                bigDeviceMapper.picturesInsert(val);
            }
        }
        //文件
        List<String> files = (List<String>) values.get("files");
        if(files!=null && files.size()>0){
            for (String f:files){
                HashMap<String, Object> val = new HashMap<>();
                val.put("bigDeviceId",bigDeviceId1);
                val.put("id","");
                val.put("fileType",2);
                val.put("filePath",f);
                bigDeviceMapper.picturesInsert(val);
            }
        }
        bigDeviceMapper.bigDeviceUpdate(values);
        return BasicResult.success("修改成功");
    }
 
    @Override
    public BasicResult bigDevicePull() {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        List<TBigDevice> bigDevices = bigDeviceMapper.bigDevicePull(companyId);
        return BasicResult.success(bigDevices);
    }
 
    @Override
    public void bigDeviceExcel(Map<String, Object> values, HttpServletResponse response) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        values.put("companyId",companyId);
        List<TBigDevice> tBigDevices = bigDeviceMapper.bigDeviceList(values);
        List<BigDeviceExcelDto> bigDeviceExcelDtos = new ArrayList<>();
        for (TBigDevice bigDevice : tBigDevices){
            BigDeviceExcelDto bigDeviceExcelDto = new BigDeviceExcelDto();
            bigDeviceExcelDto.setBigDeviceName(bigDevice.getBigDeviceName());
            bigDeviceExcelDto.setBigDeviceModel(bigDevice.getBigDeviceModel());
            bigDeviceExcelDto.setBigNumber(bigDevice.getBigNumber());
            bigDeviceExcelDto.setBigTypeName(bigDevice.getBigTypeName());
            bigDeviceExcelDto.setBigPosition(bigDevice.getBigPosition());
            if(bigDevice.getBigState()!=null&&bigDevice.getBigState()==1){
                bigDeviceExcelDto.setBigState("在线");
            }else{
                bigDeviceExcelDto.setBigState("离线");
            }
            bigDeviceExcelDto.setRealName(bigDevice.getRealName());
            bigDeviceExcelDto.setSupplierName(bigDevice.getSupplierName());
            bigDeviceExcelDto.setKeyCabinet(bigDevice.getKeyCabinet());
            bigDeviceExcelDto.setUseLife(bigDevice.getUseLife());
            bigDeviceExcelDtos.add(bigDeviceExcelDto);
        }
        ExcelWriter excel = new ExcelWriter();//定义excel
        excel.addHeaderAlias("bigDeviceName", "设备名称");
        excel.addHeaderAlias("bigDeviceModel", "规格型号");
        excel.addHeaderAlias("bigNumber", "设备编号");
        excel.addHeaderAlias("bigTypeName", "设备类型");
        excel.addHeaderAlias("bigPosition", "安装位置");
        excel.addHeaderAlias("bigState", "设备状态");
        excel.addHeaderAlias("realName", "操作工人");
        excel.addHeaderAlias("supplierName", "供应商");
        excel.addHeaderAlias("keyCabinet", "钥匙柜编号");
        excel.addHeaderAlias("useLife", "使用年限");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=test.xls");
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            excel.write(bigDeviceExcelDtos);
            excel.flush(out, true);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭writer,释放内存
            excel.close();
        }
        IoUtil.close(out);
 
    }
}