Commit c9bd220f authored by jichao's avatar jichao

设备维护--设备报表,评估--分析报告 接口

parent 4e7a2d27
package org.rcisoft.business.device.report.controller;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.rcisoft.business.device.report.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
/**
* Created by JiChao on 2018/5/3.
* 设备维护--设备报表
*/
@RestController
@RequestMapping("report")
public class ReportController {
@Autowired
private ReportService reportServiceImpl;
@ApiOperation(value="下载文件", notes="下载文件")
@ApiImplicitParams({@ApiImplicitParam(name = "proId", value = "项目主键", required = true, dataType = "字符串"),
@ApiImplicitParam(name = "year", value = "年", required = true, dataType = "数字"),
@ApiImplicitParam(name = "month", value = "月", required = true, dataType = "数字"),
@ApiImplicitParam(name = "month", value = "日", required = true, dataType = "数字")})
@RequestMapping("/download")
public void download(HttpServletResponse response, @RequestParam String proId, Integer year, Integer month, Integer day) {
reportServiceImpl.download(response, proId, year, month, day);
}
}
package org.rcisoft.business.device.report.dao;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.rcisoft.business.device.report.entity.Original;
import org.rcisoft.business.device.report.entity.Report;
import org.rcisoft.business.device.report.vo.Params;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* Created by JiChao on 2018/5/3.
*/
@Repository
public interface ReportRepository {
/**
* 获取设备列表,原始格式
* @param params
* @return
*/
@Select("<script>select b.DEV_NUM,b.DEV_NM,bp.PARAM,dp.P_SOURCE,dp.PARAM_NM " +
"from bus_device_param bp,bus_devicetp_param dp,bus_device b " +
"where bp.PARAM_CODE=dp.PARAM_CODE and b.PRO_ID=dp.PRO_ID and bp.DEV_NUM=b.DEV_NUM and b.PRO_ID=#{proId}" +
"order by b.DEV_NUM,dp.P_SOURCE</script>")
@ResultMap(value = "report")
List<Report> reportList(Params params);
/**
* 获取数据
* @param params
* @return
*/
@Select("<script>select t.DEV_NUM,t.TM,t.ORIGINAL_STR,s.SENSOR_JSON from total_original t,total_sensor s " +
"where t.TM=s.TM and t.PRO_ID=s.PRO_ID and t.WATER_V is null and t.ELEC_V is null and t.GAS_V is null " +
"and t.PRO_ID=#{proId} and date_format(t.TM, '%Y-%c-%e')=#{time} " +
"order by t.DEV_NUM,t.TM</script>")
@ResultMap(value = "original")
List<Original> originalList(Params params);
}
package org.rcisoft.business.device.report.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
/**
* Created by JiChao on 2018/5/3.
* 原始数据 + 传感器数据
*/
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Original {
//设备num
private String devNum;
//时间
private String tm;
//设备参数集
private String originalStr;
//传感器参数集
private String sensorJson;
}
package org.rcisoft.business.device.report.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
/**
* Created by JiChao on 2018/5/3.
* 设备信息
*/
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Report {
//设备编号
private String devNum;
//设备名称
private String devNm;
//参数
private String param;
//所属类型 1:设备 2:传感器
private String psource;
//参数名称
private String paramNm;
}
package org.rcisoft.business.device.report.service;
import javax.servlet.http.HttpServletResponse;
/**
* Created by JiChao on 2018/5/3.
*/
public interface ReportService {
/**
* 下载选定日期的设备报表
* @param response
* @param proId
* @param year
* @param month
* @param day
*/
void download(HttpServletResponse response, String proId, Integer year, Integer month, Integer day);
}
package org.rcisoft.business.device.report.service.impl;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.rcisoft.business.device.report.dao.ReportRepository;
import org.rcisoft.business.device.report.entity.Original;
import org.rcisoft.business.device.report.entity.Report;
import org.rcisoft.business.device.report.service.ReportService;
import org.rcisoft.business.device.report.vo.DeviceVo;
import org.rcisoft.business.device.report.vo.Params;
import org.rcisoft.business.device.report.vo.ReportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
/**
* Created by JiChao on 2018/5/3.
*/
@Service
public class ReportServiceImpl implements ReportService {
@Value("${filepath.equipment}")
private String filePath;
@Autowired
private ReportRepository reportRepository;
@Override
public void download(HttpServletResponse response, String proId, Integer year, Integer month, Integer day) {
byte[] bytes = null;
//如果有文件
String fileName = filePath + proId + File.separator + year + File.separator + month + File.separator + day + ".xls";
File file = new File(fileName);
try(OutputStream outputStream = response.getOutputStream()) {
//如果不存在
if (!file.exists()) file = this.createExcel(proId, year, month, day, file);
//转成字节
bytes = FileUtils.readFileToByteArray(file);
//设置下载头
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(day + ".xls", "iso-8859-1"));
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 查找,组合数据,生成文件
* @param proId
* @param year
* @param month
* @param day
* @param file
* @return
*/
private File createExcel(String proId, Integer year, Integer month, Integer day, File file) {
//查询条件
Params p = new Params(proId, year, month, day, year + "-" + month + "-" + day);
//得到设备的结果集
List<ReportVo> reportVoList = this.getReportVoList(p);
//得到数据的结果集
List<Original> originalList = reportRepository.originalList(p);
//---------------定义excel-----------------------
HSSFWorkbook workbook = new HSSFWorkbook();
//---------------定义excel-----------------------
//循环设备结果集,每个设备创建一个sheet页
reportVoList.forEach(reportVo -> {
//定义sheet
HSSFSheet sheet = workbook.createSheet(reportVo.getDevNm());
//创建表头
HSSFRow head = sheet.createRow(0);
List<DeviceVo> deviceList = reportVo.getDeviceList();
//第一列填时间
head.createCell(0, CellType.STRING).setCellValue("时间");
for (int i = 0; i < deviceList.size(); i++) {
HSSFCell cell = head.createCell(i + 1, CellType.STRING);
//表头填入参数名称
cell.setCellValue(deviceList.get(i).getParamNm());
}
//定义一个增量
int x = 1;
//循环数据
for (int i = 0; i < originalList.size(); i++) {
Original original = originalList.get(i);
String devNum = original.getDevNum();
//判断devNum是否等于当前设备的devNum
if (StringUtils.equals(devNum, reportVo.getDevNum())) {
//第一列放入时间
HSSFRow row = sheet.createRow(x++);
row.createCell(0, CellType.STRING).setCellValue(original.getTm());
//设备的参数对象
JSONObject originalJson = JSONObject.parseObject(original.getOriginalStr());
//传感器参数对象
JSONObject sensorJson = JSONObject.parseObject(original.getSensorJson());
//循环设备
for (int j = 0; j < deviceList.size(); j++) {
DeviceVo deviceVo = deviceList.get(j);
String param = deviceVo.getParam();//参数code
String psource = deviceVo.getPsource();//参数来源,1:设备 2:传感器
//需要保存的结果
String res = "";
if (psource.equals("1")) res = originalJson.get(param) == null ? "" : originalJson.get(param).toString();
else if (psource.equals("2")) res = sensorJson.get(param) == null ? "" : sensorJson.get(param).toString();
//依次放入行中
row.createCell(j + 1, CellType.STRING).setCellValue(res);
}
//用完删除当前行的数据
originalList.remove(i--);
}
}
});
try {
workbook.write(file);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
/**
* 转换结果集,变成设备的list,每个设备有自己的参数list
* list套list形式
* @param params proId
* @return
*/
private List<ReportVo> getReportVoList(Params params) {
//所有设备参数的list
List<Report> reportList = reportRepository.reportList(params);
String _devNum = "";
//返回的结果
List<ReportVo> reportVoList = new ArrayList<>();
//记录最后结果集的标签
int num = -1;
for (int i = 0; i < reportList.size(); i++) {
Report report = reportList.get(i);
String devNum = report.getDevNum();
//不等于
if (!StringUtils.equals(_devNum, devNum)){
_devNum = devNum;
ReportVo reportVo = new ReportVo();
reportVo.setDevNm(devNum);
reportVo.setDevNum(report.getDevNum());
//加入新的集合中
reportVoList.add(reportVo);
num++;
}
//从集合里取到需要添加的记录,将当前的值放入
ReportVo reportVo = reportVoList.get(num);
List<DeviceVo> deviceVoList = reportVo.getDeviceList();
DeviceVo deviceVo = new DeviceVo(report.getParam(), report.getPsource(), report.getParamNm());
deviceVoList.add(deviceVo);
}
return reportVoList;
}
}
package org.rcisoft.business.device.report.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Created by JiChao on 2018/5/3.
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DeviceVo {
private String param;
private String psource;
private String paramNm;
}
package org.rcisoft.business.device.report.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Created by JiChao on 2018/5/3.
* 需要用到的查询参数
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Params {
private String proId;
private Integer year;
private Integer month;
private Integer day;
private String time;
}
package org.rcisoft.business.device.report.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
/**
* Created by JiChao on 2018/5/3.
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ReportVo {
private String devNum;
private String devNm;
private List<DeviceVo> deviceList = new ArrayList<>();
}
package org.rcisoft.business.evaluate.analysis.controller;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.rcisoft.business.evaluate.analysis.service.AnalysisService;
import org.rcisoft.core.constant.MessageConstant;
import org.rcisoft.core.model.PersistModel;
import org.rcisoft.core.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
/**
* Created by JiChao on 2018/5/2.
* 评估--分析报告
*/
@RestController
@RequestMapping("analysis")
public class AnalysisController {
@Autowired
private AnalysisService analysisServiceImpl;
@ApiOperation(value="上传文件", notes="上传文件")
@ApiImplicitParams({@ApiImplicitParam(name = "proId", value = "项目主键", required = true, dataType = "字符串"),
@ApiImplicitParam(name = "year", value = "年", required = true, dataType = "数字"),
@ApiImplicitParam(name = "month", value = "月", required = true, dataType = "数字")
})
@RequestMapping("/upload")
public Result upload(@RequestParam String proId, @RequestParam Integer year, @RequestParam Integer month, @RequestParam MultipartFile file) {
Integer result = analysisServiceImpl.upload(proId, year, month, file);
return Result.builder(new PersistModel(result), MessageConstant.MESSAGE_ALERT_SUCCESS, MessageConstant.MESSAGE_ALERT_ERROR, result);
}
@ApiOperation(value="下载文件", notes="下载文件")
@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "文件的主键", required = true, dataType = "数字")})
@RequestMapping("/download")
public void download(HttpServletResponse response, @RequestParam Integer id) {
analysisServiceImpl.download(response, id);
}
@ApiOperation(value="查询列表", notes="根据年份、项目主键查询已经存在的文件列表,最多返回12条记录")
@ApiImplicitParams({@ApiImplicitParam(name = "proId", value = "项目主键", required = true, dataType = "字符串"),
@ApiImplicitParam(name = "year", value = "年", required = true, dataType = "数字")
})
@RequestMapping("/analysisFile")
public Result analysisFile(@RequestParam String proId, @RequestParam Integer year) {
return Result.builder(new PersistModel(1), MessageConstant.MESSAGE_ALERT_SUCCESS, MessageConstant.MESSAGE_ALERT_ERROR, analysisServiceImpl.analysisFile(proId, year));
}
@ApiOperation(value="删除文件", notes="删除文件")
@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "文件的主键", required = true, dataType = "数字")})
@RequestMapping("/delete")
public Result delete(@RequestParam Integer id) {
Integer result = analysisServiceImpl.delete(id);
return Result.builder(new PersistModel(result), MessageConstant.MESSAGE_ALERT_SUCCESS, MessageConstant.MESSAGE_ALERT_ERROR, result);
}
}
package org.rcisoft.business.evaluate.analysis.dao;
import org.rcisoft.business.evaluate.analysis.entity.BusReport;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
/**
* Created by JiChao on 2018/5/3.
*/
@Repository
public interface BusReportRepository extends Mapper<BusReport> {
}
package org.rcisoft.business.evaluate.analysis.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
/**
* Created by JiChao on 2018/5/3.
*/
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "bus_report")
public class BusReport implements Serializable {
@Id
private Integer id;
private String proId;
private Integer year;
private Integer month;
private String path;
}
package org.rcisoft.business.evaluate.analysis.service;
import org.rcisoft.business.evaluate.analysis.entity.BusReport;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* Created by JiChao on 2018/5/3.
*/
public interface AnalysisService {
/**
* 上传文件,数据库新增数据,文件保存
* 如果已经存在,删除之前的(删文件,删数据库记录)
* @param proId
* @param year
* @param month
* @param file
* @return
*/
Integer upload(String proId, Integer year, Integer month, MultipartFile file);
/**
* 查出地址,转换成流,发送到前台
* @param response
* @param id
*/
void download(HttpServletResponse response, Integer id);
/**
* 查询列表
* @param proId
* @param year
* @return
*/
List<BusReport> analysisFile(String proId, Integer year);
/**
* 删除文件,删除数据库记录
* @param id
* @return
*/
Integer delete(Integer id);
}
package org.rcisoft.business.evaluate.analysis.service.impl;
import org.apache.commons.io.FileUtils;
import org.rcisoft.business.evaluate.analysis.dao.BusReportRepository;
import org.rcisoft.business.evaluate.analysis.entity.BusReport;
import org.rcisoft.business.evaluate.analysis.service.AnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
/**
* Created by JiChao on 2018/5/3.
*/
@Service
public class AnalysisServiceImpl implements AnalysisService {
@Value("${filepath.analysis}")
private String filepath;
@Autowired
private BusReportRepository busReportRepository;
@Transactional
@Override
public Integer upload(String proId, Integer year, Integer month, MultipartFile file) {
int result = 0;
BusReport busReport = new BusReport(null, proId, year, month, null);
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
//文件路径
String savePath = filepath + proId + File.separator + year + File.separator + month + suffixName;
File saveFile = new File(savePath);
//如果存在,删除文件,删除数据库记录
if (saveFile.exists()) {
saveFile.delete();
busReportRepository.delete(busReport);
}
//保存
busReport.setPath(savePath);
try {
FileUtils.copyInputStreamToFile(file.getInputStream(), saveFile);
result = busReportRepository.insert(busReport);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
public void download(HttpServletResponse response, Integer id) {
BusReport busReport = new BusReport();
busReport.setId(id);
busReport = busReportRepository.selectOne(busReport);
String path = busReport.getPath();
String filename = busReport.getProId() + "-" + busReport.getYear() + "-" + busReport.getMonth() + path.substring(path.lastIndexOf("."));
try(OutputStream outputStream = response.getOutputStream()) {
//获取文件字节
byte[] bytes = FileUtils.readFileToByteArray(new File(path));
//设置下载头
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "iso-8859-1"));
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public List<BusReport> analysisFile(String proId, Integer year) {
BusReport busReport = new BusReport();
busReport.setProId(proId);
busReport.setYear(year);
List<BusReport> list = busReportRepository.select(busReport);
list.forEach(b -> {
b.setProId(null);
b.setYear(null);
b.setPath(null);
});
return list;
}
@Transactional
@Override
public Integer delete(Integer id) {
BusReport busReport = new BusReport();
busReport.setId(id);
busReport = busReportRepository.selectOne(busReport);
String path = busReport.getPath();
File file = new File(path);
if (file.exists()) {
file.delete();
}
return busReportRepository.delete(busReport);
}
}
spring: spring:
profiles: profiles:
active: dev active: dev
\ No newline at end of file
filepath:
analysis: D:\zhny\filepath\analysis\
equipment: D:\zhny\filepath\equipment\
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.rcisoft.business.device.report.dao.ReportRepository">
<resultMap id="report" type="org.rcisoft.business.device.report.entity.Report">
<result column="DEV_NUM" jdbcType="VARCHAR" property="devNum"/>
<result column="DEV_NM" jdbcType="VARCHAR" property="devNm"/>
<result column="PARAM" jdbcType="VARCHAR" property="param"/>
<result column="P_SOURCE" jdbcType="VARCHAR" property="psource"/>
<result column="PARAM_NM" jdbcType="VARCHAR" property="paramNm"/>
</resultMap>
<resultMap id="original" type="org.rcisoft.business.device.report.entity.Original">
<result column="DEV_NUM" jdbcType="VARCHAR" property="devNum"/>
<result column="TM" jdbcType="TIMESTAMP" property="tm"/>
<result column="ORIGINAL_STR" jdbcType="VARCHAR" property="originalStr"/>
<result column="SENSOR_JSON" jdbcType="VARCHAR" property="sensorJson"/>
</resultMap>
</mapper>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.rcisoft.business.evaluate.analysis.dao.BusReportRepository">
<resultMap id="BaseResultMap" type="org.rcisoft.business.evaluate.analysis.entity.BusReport">
<id column="ID" jdbcType="INTEGER" property="id"/>
<result column="PRO_ID" jdbcType="VARCHAR" property="proId"/>
<result column="YEAR" jdbcType="INTEGER" property="year"/>
<result column="MONTH" jdbcType="INTEGER" property="month"/>
<result column="PATH" jdbcType="VARCHAR" property="path"/>
</resultMap>
<!--<cache type="${corePackag!}.util.RedisCache"/>-->
</mapper>
\ 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