TExceptionLogServiceImpl.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package com.sckj.warn.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.sckj.common.core.AjaxResult;
  6. import com.sckj.common.core.PageResult;
  7. import com.sckj.common.util.ExcelUtils;
  8. import com.sckj.common.validate.commons.PageValidate;
  9. import com.sckj.warn.dto.WarnDTO;
  10. import com.sckj.warn.entity.TExceptionLog;
  11. import com.sckj.warn.mapper.TExceptionLogMapper;
  12. import com.sckj.warn.validate.TExceptionLogCreateValidate;
  13. import com.sckj.warn.validate.TExceptionLogSearchValidate;
  14. import com.sckj.warn.validate.TExceptionLogUpdateValidate;
  15. import com.sckj.warn.vo.TExceptionLogDetailVo;
  16. import com.sckj.warn.vo.TExceptionLogExportVo;
  17. import com.sckj.warn.vo.TExceptionLogListedVo;
  18. import org.apache.poi.ss.usermodel.Cell;
  19. import org.apache.poi.ss.usermodel.Row;
  20. import org.apache.poi.ss.usermodel.Sheet;
  21. import org.apache.poi.ss.usermodel.Workbook;
  22. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  23. import org.springframework.beans.BeanUtils;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.util.Assert;
  26. import org.springframework.util.CollectionUtils;
  27. import javax.annotation.Resource;
  28. import javax.servlet.http.HttpServletResponse;
  29. import java.io.ByteArrayOutputStream;
  30. import java.io.IOException;
  31. import java.text.SimpleDateFormat;
  32. import java.util.Date;
  33. import java.util.LinkedList;
  34. import java.util.List;
  35. /**
  36. * 异常情况记录实现类
  37. * @author zhanghao
  38. */
  39. @Service
  40. public class TExceptionLogServiceImpl {
  41. @Resource
  42. TExceptionLogMapper tExceptionLogMapper;
  43. /**
  44. * 异常情况记录列表
  45. *
  46. * @author zhanghao
  47. * @param pageValidate 分页参数
  48. * @param searchValidate 搜索参数
  49. * @return PageResult<TExceptionLogListedVo>
  50. */
  51. public PageResult<TExceptionLogListedVo> list(PageValidate pageValidate, TExceptionLogSearchValidate searchValidate) {
  52. Integer page = pageValidate.getPageNo();
  53. Integer limit = pageValidate.getPageSize();
  54. QueryWrapper<TExceptionLog> queryWrapper = new QueryWrapper<>();
  55. queryWrapper.eq("del_flag", "1");
  56. queryWrapper.orderByDesc("id");
  57. /*数据库时间类型为datetime不可走setSearch()*/
  58. // if (StringUtils.isNotEmpty(searchValidate.getCreateTimeStart()) && StringUtils.isNotEmpty(searchValidate.getCreateTimeEnd())){
  59. // queryWrapper.ge("create_time", searchValidate.getCreateTimeStart())
  60. // .le("create_time", searchValidate.getCreateTimeEnd());
  61. // }
  62. //setSearch()时间参数移除
  63. /*"datetime:createTimeStart-createTimeEnd@create_time:str",
  64. "datetime:updateTimeStart-updateTimeEnd@update_time:str",*/
  65. tExceptionLogMapper.setSearch(queryWrapper, searchValidate, new String[]{
  66. "=:exceptionType@exception_type:int",
  67. "=:exceptionArea@exception_area:str",
  68. "=:exceptionLevel@exception_level:str",
  69. });
  70. IPage<TExceptionLog> iPage = tExceptionLogMapper.selectPage(new Page<>(page, limit), queryWrapper);
  71. List<TExceptionLogListedVo> list = new LinkedList<>();
  72. for(TExceptionLog item : iPage.getRecords()) {
  73. TExceptionLogListedVo vo = new TExceptionLogListedVo();
  74. BeanUtils.copyProperties(item, vo);
  75. vo.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.getCreateTime())); //由于QueryWrapper或IPage的原因导致JsonFormat注解未生效,此处手动转换格式
  76. list.add(vo);
  77. }
  78. return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
  79. }
  80. /**
  81. * 异常情况记录详情
  82. *
  83. * @author zhanghao
  84. * @param id 主键参数
  85. * @return TExceptionLog
  86. */
  87. public TExceptionLogDetailVo detail(Integer id) {
  88. TExceptionLog model = tExceptionLogMapper.selectOne(
  89. new QueryWrapper<TExceptionLog>()
  90. .eq("id", id).eq("del_flag", "1")
  91. .last("limit 1"));
  92. Assert.notNull(model, "数据不存在");
  93. TExceptionLogDetailVo vo = new TExceptionLogDetailVo();
  94. BeanUtils.copyProperties(model, vo);
  95. return vo;
  96. }
  97. /**
  98. * 异常情况记录新增
  99. *
  100. * @author zhanghao
  101. * @param createValidate 参数
  102. */
  103. public void add(TExceptionLogCreateValidate createValidate) {
  104. TExceptionLog model = new TExceptionLog();
  105. model.setCreateTime(new Date(System.currentTimeMillis()));//gg
  106. model.setUpdateTime(new Date(System.currentTimeMillis()));//gg
  107. model.setExceptionType(createValidate.getExceptionType());
  108. model.setExceptionLevel(createValidate.getExceptionLevel());
  109. model.setReportedStatus(createValidate.getReportedStatus());
  110. model.setBoilerId(createValidate.getBoilerId());
  111. model.setExceptionDesc(createValidate.getExceptionDesc());
  112. tExceptionLogMapper.insert(model);
  113. }
  114. /**
  115. * 异常情况记录编辑
  116. *
  117. * @author zhanghao
  118. * @param updateValidate 参数
  119. */
  120. public void edit(TExceptionLogUpdateValidate updateValidate) {
  121. TExceptionLog model = tExceptionLogMapper.selectOne(
  122. new QueryWrapper<TExceptionLog>()
  123. .eq("id", updateValidate.getId())
  124. .last("limit 1"));
  125. Assert.notNull(model, "数据不存在!");
  126. model.setUpdateTime(new Date(System.currentTimeMillis()));//gg
  127. tExceptionLogMapper.updateById(model);
  128. }
  129. /**
  130. * 异常情况记录删除
  131. *
  132. * @author zhanghao
  133. * @param id 主键ID
  134. */
  135. public void del(Integer id) {
  136. TExceptionLog model = tExceptionLogMapper.selectOne(
  137. new QueryWrapper<TExceptionLog>()
  138. .eq("id", id)
  139. .last("limit 1"));
  140. Assert.notNull(model, "数据不存在!");
  141. model.setDelFlag("0");
  142. tExceptionLogMapper.updateById(model);
  143. }
  144. public AjaxResult<Object> del_ex(List<Long> ids) {
  145. List<TExceptionLog> models = tExceptionLogMapper.selectList(
  146. new QueryWrapper<TExceptionLog>()
  147. .in("id", ids));
  148. if (CollectionUtils.isEmpty(models)){
  149. return AjaxResult.failed("数据不存在");
  150. }
  151. TExceptionLog model = new TExceptionLog();
  152. model.setDelFlag("0");
  153. tExceptionLogMapper.update(model, new QueryWrapper<TExceptionLog>().in("id", ids));
  154. return AjaxResult.success();
  155. }
  156. /**
  157. * 异常情况记录导出
  158. *
  159. * @author zhanghao
  160. * @param searchValidate 搜索参数·
  161. */
  162. public ByteArrayOutputStream exportTExceptionLog(TExceptionLogSearchValidate searchValidate) {
  163. Workbook workbook = new XSSFWorkbook();
  164. Sheet sheet = workbook.createSheet("Persons");
  165. QueryWrapper<TExceptionLog> queryWrapper = new QueryWrapper<>();
  166. tExceptionLogMapper.setSearch(queryWrapper, searchValidate, new String[]{
  167. "=:exceptionType@exception_type:int",
  168. "=:exceptionArea@exception_area:str",
  169. "=:exceptionLevel@exception_level:str",
  170. });
  171. List<TExceptionLog> tExceptionLogList = tExceptionLogMapper.selectList(new QueryWrapper<TExceptionLog>());
  172. // 创建标题行
  173. Row headerRow = sheet.createRow(0);
  174. String[] columnNames = {"高炉编号", "异常类型", "异常区域", "危害程度", "上报状态", "创建时间"};
  175. for (int i = 0; i < columnNames.length; i++) {
  176. Cell cell = headerRow.createCell(i);
  177. cell.setCellValue(columnNames[i]);
  178. }
  179. // 填充数据
  180. for (int i = 0; i < tExceptionLogList.size(); i++) {
  181. Row row = sheet.createRow(i + 1);
  182. TExceptionLog tExceptionLog = tExceptionLogList.get(i);
  183. row.createCell(0).setCellValue(tExceptionLog.getBoilerId());
  184. row.createCell(1).setCellValue(tExceptionLog.getExceptionType());
  185. row.createCell(2).setCellValue(tExceptionLog.getExceptionArea());
  186. row.createCell(3).setCellValue(tExceptionLog.getExceptionLevel());
  187. row.createCell(4).setCellValue(tExceptionLog.getReportedStatus().equals("0") ? "未上报" : "已上报");
  188. row.createCell(5).setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tExceptionLog.getCreateTime()));
  189. }
  190. // 将数据写入输出流
  191. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  192. try {
  193. workbook.write(outputStream);
  194. workbook.close();
  195. } catch (Exception e) {
  196. e.printStackTrace();
  197. }
  198. return outputStream;
  199. }
  200. /***
  201. * 查询预警统计
  202. * @return
  203. */
  204. public List<WarnDTO> selectWarnStats(){
  205. return tExceptionLogMapper.selectWarnStats();
  206. }
  207. /**
  208. * 异常统计
  209. * @param searchValidate
  210. * @param response
  211. */
  212. public void exportExceptionLogList(TExceptionLogSearchValidate searchValidate, HttpServletResponse response){
  213. List<TExceptionLogExportVo> tExceptionLogExportVos = tExceptionLogMapper.exportExceptionLogList();
  214. try {
  215. ExcelUtils.exportExcel(tExceptionLogExportVos, TExceptionLogExportVo.class, "异常统计", "异常统计", response);
  216. } catch (IOException e) {
  217. throw new RuntimeException(e);
  218. }
  219. }
  220. }