Commit 83b10d74 authored by 李松's avatar 李松

修改bug

parent 0d55ba14
......@@ -58,16 +58,15 @@ public class AdpterController {
adpterService.executeSqlFile(request, response, dbHost, port, dbName, userName, dbPwd, file);
}
@ApiOperation(value = "区域信息")
@RequestMapping(value = "/areaExcel", method = RequestMethod.POST)
public void export(HttpServletResponse response,
@RequestParam(value = "type", required = false) String type) throws IOException {
adpterService.areaExcel(response);
@ApiOperation(value = "主设备关联信息同步")
@RequestMapping(value = "/syncMainDeviceRelation", method = RequestMethod.POST)
public void syncMainDeviceRelation() throws IOException {
adpterService.syncMainDeviceRelation();
}
@ApiOperation(value = "设备信息信息")
@RequestMapping(value = "/deviceExcel", method = RequestMethod.POST)
public void deviceExcel(HttpServletResponse response, String gatewayId) throws IOException {
adpterService.deviceExcel(response, gatewayId);
@ApiOperation(value = "buildingExcel")
@RequestMapping(value = "/buildingExcel", method = RequestMethod.POST)
public void systemExcel(HttpServletResponse response, String gatewayId) throws IOException {
adpterService.buildingExcel(response, gatewayId);
}
}
\ No newline at end of file
package com.yeejoin.amos.adpter.model;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class SystemExcelModel {
@ExcelProperty(value = "编码", index = 0)
private String code;
@ExcelProperty(value = "上级编码", index = 1)
private String parentCode;
@ExcelProperty(value = "类型", index = 2)
private String type;
@ExcelProperty(value = "网关名称", index = 3)
private String gatewayName;
@ExcelProperty(value = "系统类型", index = 4)
private String category;
@ExcelProperty(value = "系统名称", index = 5)
private String name;
}
package com.yeejoin.amos.adpter.service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.yeejoin.amos.adpter.model.AreaExcelModel;
import com.yeejoin.amos.adpter.model.DeviceExcelModel;
import com.yeejoin.amos.adpter.model.SystemExcelModel;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.apache.ibatis.jdbc.ScriptRunner;
......@@ -23,10 +26,9 @@ import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.*;
@Service
public class AdpterService {
......@@ -518,7 +520,7 @@ public class AdpterService {
return flag;
}
public void areaExcel(HttpServletResponse response) throws IOException {
public ArrayList<AreaExcelModel> areaExcel() throws IOException {
Resource resource = new ClassPathResource("sql/area.sql");
String sql = new String(Files.readAllBytes(resource.getFile().toPath()));
List<Map<String, Object>> list = query(dlBuss, sql);
......@@ -534,22 +536,192 @@ public class AdpterService {
areaExcelModel.setPartition(ObjectUtils.isEmpty(a.get("partition")) ? "" : String.valueOf(a.get("partition")));
areaExcelModels.add(areaExcelModel);
});
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("区域信息", "UTF-8") + ".xlsx");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
try (ServletOutputStream outputStream = response.getOutputStream()) {
EasyExcel.write(outputStream).head(AreaExcelModel.class).sheet("Sheet1").doWrite(areaExcelModels);
} catch (IOException e) {
log.error("dynamicExportSPTemplateExcel error:", e);
}
return areaExcelModels;
}
public void deviceExcel(HttpServletResponse response, String gatewayId) throws IOException {
public ArrayList<DeviceExcelModel> deviceExcel(String gatewayId) throws IOException {
Resource resource = new ClassPathResource("sql/device.sql");
String replace = new String(Files.readAllBytes(resource.getFile().toPath()));
String sql = replace.replace("#{gatewayId}", gatewayId);
List<Map<String, Object>> list = query(dlBuss, sql);
ArrayList<DeviceExcelModel> deviceList = new ArrayList<>();
setData(list, deviceList);
return deviceList;
}
public void syncMainDeviceRelation() throws IOException {
// 获取主设备关联物联设备记录
Resource mainResource = new ClassPathResource("sql/mainDeviceRelation.sql");
String mainSql = new String(Files.readAllBytes(mainResource.getFile().toPath()));
List<Map<String, Object>> mainList = query(dlBuss, mainSql);
// 构建入库sql
execute(mainList, dataView, userName, pwd);
}
private void execute(List<Map<String, Object>> params, String url, String username, String password) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Connection connection = null;
Statement statement = null;
try {
// 获取数据库连接
connection = DriverManager.getConnection(url, username, password);
connection.setAutoCommit(false);
statement = connection.createStatement();
Statement finalStatement = statement;
params.forEach(p -> {
try {
StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO iot_device_bind_relation_test (SEQUENCE_NBR" +
", DEVICE_SEQ" +
", SUB_DEVICE_SEQ" +
",SUB_DEVICE_ID" +
",DEVICE_ID" +
",REC_USER_ID" +
", REC_USER_NAME" +
", CREATE_DATE" +
", REC_DATE) VALUES(");
sql.append(p.get("id"));
sql.append(",");
sql.append("null");
sql.append(",");
sql.append("null");
sql.append(",");
sql.append("'");
sql.append(p.get("deviceCode"));
sql.append("'");
sql.append(",");
sql.append("'");
sql.append(p.get("mainDeviceCode"));
sql.append("'");
sql.append(",");
sql.append("null");
sql.append(",");
sql.append("null");
sql.append(",");
sql.append("'");
sql.append(sdf.format(new Date()));
sql.append("'");
sql.append(",");
sql.append("'");
sql.append(sdf.format(new Date()));
sql.append("'");
sql.append(")");
finalStatement.addBatch(sql.toString());
} catch (SQLException throwables) {
throwables.printStackTrace();
}
});
// 更新数据
finalStatement.addBatch("UPDATE iot_device_bind_relation_test a " +
"JOIN iot_device_info_test b1 ON b1.DEVICE_CODE = a.DEVICE_ID " +
"JOIN iot_device_info_test b2 ON b2.DEVICE_CODE = a.SUB_DEVICE_ID " +
"SET a.DEVICE_SEQ = b1.SEQUENCE_NBR," +
" a.DEVICE_ID = b1.DEVICE_ID," +
" a.SUB_DEVICE_SEQ = b2.SEQUENCE_NBR," +
" a.SUB_DEVICE_ID = b2.DEVICE_ID" +
" WHERE a.DEVICE_ID IS NOT NULL AND a.SUB_DEVICE_ID IS NOT NULL");
// 执行批处理
int[] result = statement.executeBatch();
// 提交事务
connection.commit();
System.out.println("批量插入完成,影响行数: " + Arrays.toString(result));
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
e.printStackTrace();
} finally {
// 关闭资源
try {
if (statement != null) statement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void buildingExcel(HttpServletResponse response, String gatewayId) throws IOException {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("数据导出", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
// 获取输出流
try (OutputStream outputStream = response.getOutputStream()) {
// 创建ExcelWriter
ExcelWriter excelWriter = EasyExcel.write(outputStream).build();
// 系统信息
WriteSheet buildingSheet = EasyExcel.writerSheet(0, "建筑信息")
.head(AreaExcelModel.class)
.build();
excelWriter.write(areaExcel(), buildingSheet);
// 系统信息
WriteSheet systemSheet = EasyExcel.writerSheet(1, "系统信息")
.head(SystemExcelModel.class)
.build();
excelWriter.write(systemInfo(), systemSheet);
// 设备信息
WriteSheet deviceSheet = EasyExcel.writerSheet(2, "设备信息")
.head(DeviceExcelModel.class)
.build();
excelWriter.write(deviceExcel(gatewayId), deviceSheet);
// 主设备信息
WriteSheet mainDeviceSheet = EasyExcel.writerSheet(3, "主设备信息")
.head(DeviceExcelModel.class)
.build();
excelWriter.write(mainDevice(), mainDeviceSheet);
// 应急物资
WriteSheet emergencySheet = EasyExcel.writerSheet(4, "应急物资")
.head(DeviceExcelModel.class)
.build();
excelWriter.write(emergency(), emergencySheet);
// 关闭ExcelWriter
excelWriter.finish();
}
}
public List<DeviceExcelModel> emergency() throws IOException {
Resource mainResource = new ClassPathResource("sql/emergency.sql");
String emergencySql = new String(Files.readAllBytes(mainResource.getFile().toPath()));
List<Map<String, Object>> emergencyList = query(dlBuss, emergencySql);
ArrayList<DeviceExcelModel> deviceExcelModels = new ArrayList<>();
setData(emergencyList, deviceExcelModels);
return deviceExcelModels;
}
public ArrayList<SystemExcelModel> systemInfo() throws IOException {
Resource resource = new ClassPathResource("sql/systemInfo.sql");
String sql = new String(Files.readAllBytes(resource.getFile().toPath()));
List<Map<String, Object>> list = query(dlBuss, sql);
ArrayList<SystemExcelModel> systemExcelModels = new ArrayList<>();
list.forEach(a -> {
SystemExcelModel systemExcelModel = new SystemExcelModel();
systemExcelModel.setCategory(ObjectUtils.isEmpty(a.get("category")) ? "" : String.valueOf(a.get("category")));
systemExcelModel.setCode(ObjectUtils.isEmpty(a.get("code")) ? "" : String.valueOf(a.get("code")));
systemExcelModel.setParentCode(ObjectUtils.isEmpty(a.get("parentCode")) ? "" : String.valueOf(a.get("parentCode")));
systemExcelModel.setType(ObjectUtils.isEmpty(a.get("type")) ? "" : String.valueOf(a.get("type")));
systemExcelModel.setName(ObjectUtils.isEmpty(a.get("name")) ? "" : String.valueOf(a.get("name")));
systemExcelModel.setGatewayName(ObjectUtils.isEmpty(a.get("gatewayName")) ? "" : String.valueOf(a.get("gatewayName")));
systemExcelModels.add(systemExcelModel);
});
return systemExcelModels;
}
public List<DeviceExcelModel> mainDevice() throws IOException {
// 主设备信息
Resource mainResource = new ClassPathResource("sql/mainDevice.sql");
String mainSql = new String(Files.readAllBytes(mainResource.getFile().toPath()));
List<Map<String, Object>> mainList = query(dlBuss, mainSql);
ArrayList<DeviceExcelModel> deviceExcelModels = new ArrayList<>();
setData(mainList, deviceExcelModels);
return deviceExcelModels;
}
public void setData(List<Map<String, Object>> list, List<DeviceExcelModel> models){
list.forEach(a -> {
DeviceExcelModel model = new DeviceExcelModel();
model.setGatewayDevice(ObjectUtils.isEmpty(a.get("gatewayDevice")) ? "" : String.valueOf(a.get("gatewayDevice")));
......@@ -575,16 +747,7 @@ public class AdpterService {
model.setLocation(ObjectUtils.isEmpty(a.get("location")) ? "" : String.valueOf(a.get("location")));
model.setDeviceNum(ObjectUtils.isEmpty(a.get("deviceNum")) ? "" : String.valueOf(a.get("deviceNum")));
model.setDeviceState(ObjectUtils.isEmpty(a.get("deviceState")) ? "" : String.valueOf(a.get("deviceState")));
deviceList.add(model);
models.add(model);
});
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("设备信息", "UTF-8") + ".xlsx");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
try (ServletOutputStream outputStream = response.getOutputStream()) {
EasyExcel.write(outputStream).head(DeviceExcelModel.class).sheet("Sheet1").doWrite(deviceList);
} catch (IOException e) {
log.error("dynamicExportSPTemplateExcel error:", e);
}
}
}
select temp.* from
(
SELECT '网关设备' as gatewayDevice,
wws.code as areaCode,
wes.code as deviceCode,
we.name as deviceType,
wes.name as deviceName,
wed.standard as specs,
DATE_FORMAT(wed.production_date, '%Y-%m-%d') as productionDate,
DATE_FORMAT(wed.delivery_date, '%Y-%m-%d') as useDate
FROM `wl_equipment_specific` wes
INNER JOIN wl_equipment we ON wes.equipment_code = we.code
LEFT JOIN wl_equipment_detail wed ON wes.equipment_detail_id = wed.id
LEFT JOIN wl_warehouse_structure wws ON wes.warehouse_structure_id = wws.id
WHERE we.CODE LIKE '3%')temp
\ No newline at end of file
select temp.* from (
SELECT
fe.code as deviceCode,
fe.name as deviceName,
(SELECT wws.code
from wl_warehouse_structure wws
where wws.id = fe.risk_source_id) as areaCode,
'换流变' as deviceType
FROM `f_equipment` fe
)temp
\ No newline at end of file
SELECT *
from (
SELECT fefe.id, fefe.fire_equipment_id,
(SELECT wes.code from wl_equipment_specific wes where wes.id = fefe.fire_equipment_id) as deviceCode,
fefe.equipment_id,
(SELECT fe.`code` from f_equipment fe where fefe.equipment_id = fe.id) as mainDeviceCode
from f_equipment_fire_equipment fefe
) temp
where temp.deviceCode is not null
and temp.mainDeviceCode is not null
\ No newline at end of file
select temp.* from (
SELECT sys.NAME as name,
sys.CODE as code,
dfg.group_name as category,
'系统' as type,
'网关名称' as gatewayName,
'' as parentCode
FROM `f_fire_fighting_system` sys
LEFT JOIN wl_dynamic_form_group dfg ON sys.system_type = dfg.relation_id
)temp
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment