TQTDReportAction.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package com.minpay.reportManage.action;
  2. import java.io.OutputStream;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.text.ParseException;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import javax.servlet.http.HttpServletResponse;
  10. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  11. import com.minpay.common.format.IFormatService;
  12. import com.minpay.common.util.CommonUtil;
  13. import com.minpay.common.util.DateUtil;
  14. import com.minpay.common.util.ExportExcelUtil;
  15. import com.minpay.db.table.mapper.DwFileDetail00Mapper;
  16. import com.minpay.db.table.mapper.DwReportInfMapper;
  17. import com.minpay.db.table.model.DwFileDetail00;
  18. import com.minpay.db.table.model.DwFileDetail00Example;
  19. import com.minpay.db.table.model.DwReportInf;
  20. import com.minpay.db.table.own.mapper.ReportServiceMapper;
  21. import com.startup.minpay.frame.business.IMINAction;
  22. import com.startup.minpay.frame.business.res.MINActionResult;
  23. import com.startup.minpay.frame.constant.IMINBusinessConstant;
  24. import com.startup.minpay.frame.exception.MINBusinessException;
  25. import com.startup.minpay.frame.jdbc.MINRowBounds;
  26. import com.startup.minpay.frame.service.base.IMINDataBaseService;
  27. import com.startup.minpay.frame.service.base.Service;
  28. import com.startup.minpay.frame.session.MINSession;
  29. import com.startup.minpay.frame.target.MINAction;
  30. import com.startup.minpay.frame.target.MINComponent;
  31. import com.startup.minpay.frame.target.MINParam;
  32. @MINComponent
  33. public class TQTDReportAction implements IMINAction {
  34. private IMINDataBaseService db;
  35. /** 台区停电日报穿透数据 */
  36. public final static String REPORT_INF_CHUANTOU = "reportInfChuantou";
  37. /** 台区停电月报附件 */
  38. public final static String REPORT_INF_FUJIAN = "reportInfFujian";
  39. /**
  40. * 台区停电穿透数据
  41. * @param reportId 报表id
  42. * @param chuantouType 穿透类型
  43. * @param session
  44. * @return
  45. * @throws MINBusinessException
  46. * @throws ParseException
  47. * @throws NumberFormatException
  48. */
  49. @MINAction(value = REPORT_INF_CHUANTOU)
  50. public MINActionResult reportInfChuantou(
  51. @MINParam(key = "reportId") String reportId,
  52. @MINParam(key = "chuantouType") String chuantouType,
  53. MINSession session
  54. ) throws MINBusinessException, NumberFormatException, ParseException {
  55. MINActionResult res = new MINActionResult();
  56. db = Service.lookup(IMINDataBaseService.class);
  57. //分页
  58. MINRowBounds rows = new MINRowBounds();
  59. rows.setSeparateSql(true);
  60. // 查询报表具体信息
  61. DwReportInf reportInf = db.selectByPrimaryKey(DwReportInfMapper.class, reportId);
  62. // 报表时间范围
  63. Map<String, Object> param = new HashMap<String, Object>();
  64. String dayInterval = reportInf.getDayInterval();
  65. String timeInterval = reportInf.getTimeInterval();
  66. String[] dayIntervalArray = dayInterval.split("-");
  67. String[] timeIntervalArray = timeInterval.split("-");
  68. String beginTime = dayIntervalArray[0] + timeIntervalArray[0];
  69. String endTime = dayIntervalArray[1] + timeIntervalArray[1];
  70. param.put("beginTime", beginTime);
  71. param.put("endTime", endTime);
  72. // 台区次数穿透数据
  73. if ("TQTD01".equals(chuantouType)) {
  74. List<Map<String, String>> fileList00 = db.getMybatisMapper(ReportServiceMapper.class).selectReportType00(param);
  75. fileList00 = Service.lookup(IFormatService.class).formatDate(fileList00, "date");
  76. fileList00 = Service.lookup(IFormatService.class).formatDateTime(fileList00, "startTime", "endTime");
  77. res.set(IMINBusinessConstant.F_PAGING_LAY, fileList00);
  78. // month个月停电count次及以上台区穿透
  79. } else if ("TQTD02".equals(chuantouType)) {
  80. String monthNum = reportInf.getTqtdMonth();
  81. String countNum = reportInf.getTqtdCount();
  82. // monthNum个月内停电 countNum次以上的台区
  83. String beginDay = DateUtil.dateAddMonth(dayIntervalArray[1], Integer.parseInt("-" + monthNum));
  84. param.put("beginDay", beginDay);
  85. param.put("endDay", dayIntervalArray[1]);
  86. param.put("countNum", countNum);
  87. List<Map<String, String>> mountCountList = db.getMybatisMapper(ReportServiceMapper.class).selectMountCount00(param);
  88. res.set(IMINBusinessConstant.F_PAGING_LAY, mountCountList);
  89. // month个月停电count次及以上台区且昨日停电穿透
  90. } else if ("TQTD03".equals(chuantouType)) {
  91. String monthNum = reportInf.getTqtdMonth();
  92. String countNum = reportInf.getTqtdCount();
  93. // monthNum个月内停电 countNum次以上的台区
  94. String beginDay = DateUtil.dateAddMonth(dayIntervalArray[1], Integer.parseInt("-" + monthNum));
  95. param.put("beginDay", beginDay);
  96. param.put("endDay", dayIntervalArray[1]);
  97. param.put("countNum", countNum);
  98. // 查询昨日停电的数据
  99. param.put("yesterday", DateUtil.dateAddDay(dayIntervalArray[1], -1));
  100. List<Map<String, String>> yesterdayList = db.getMybatisMapper(ReportServiceMapper.class).queryYesterdayDetail(param);
  101. param.put("yesterdayDetail", yesterdayList);
  102. List<Map<String, String>> mountCountList = db.getMybatisMapper(ReportServiceMapper.class).selectMountCount00(param);
  103. res.set(IMINBusinessConstant.F_PAGING_LAY, mountCountList);
  104. // 2个月停电1次及以上台区且昨日停电穿透
  105. } else if ("TQTD04".equals(chuantouType)) {
  106. String monthNum = "2";
  107. String countNum = "1";
  108. // monthNum个月内停电 countNum次以上的台区
  109. String beginDay = DateUtil.dateAddMonth(dayIntervalArray[1], Integer.parseInt("-" + monthNum));
  110. param.put("beginDay", beginDay);
  111. param.put("endDay", dayIntervalArray[1]);
  112. param.put("countNum", countNum);
  113. // 查询昨日停电的数据
  114. param.put("yesterday", DateUtil.dateAddDay(dayIntervalArray[1], -1));
  115. List<Map<String, String>> yesterdayList = db.getMybatisMapper(ReportServiceMapper.class).queryYesterdayDetail(param);
  116. param.put("yesterdayDetail", yesterdayList);
  117. List<Map<String, String>> mountCountList = db.getMybatisMapper(ReportServiceMapper.class).selectMountCount00(param);
  118. res.set(IMINBusinessConstant.F_PAGING_LAY, mountCountList);
  119. }
  120. return res;
  121. }
  122. /**
  123. * 台区停电附件导出
  124. * @param reportId
  125. * @param response
  126. * @param session
  127. * @return
  128. * @throws MINBusinessException
  129. * @throws ParseException
  130. * @throws NoSuchMethodException
  131. * @throws SecurityException
  132. * @throws IllegalAccessException
  133. * @throws IllegalArgumentException
  134. * @throws InvocationTargetException
  135. */
  136. @MINAction(value = REPORT_INF_FUJIAN)
  137. public HttpServletResponse reportInfFujian(
  138. @MINParam(key = "reportId") String reportId,
  139. HttpServletResponse response,
  140. MINSession session
  141. ) throws MINBusinessException, ParseException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  142. db = Service.lookup(IMINDataBaseService.class);
  143. //分页
  144. MINRowBounds rows = new MINRowBounds();
  145. rows.setSeparateSql(true);
  146. DwReportInf reportInf = db.selectByPrimaryKey(DwReportInfMapper.class, reportId);
  147. // 日期区间
  148. String dayInterval = reportInf.getDayInterval();
  149. // 时分秒区间
  150. String timeInterval = reportInf.getTimeInterval();
  151. // 获取时分秒
  152. String beginTime = null;
  153. String endTime = null;
  154. String[] rangeArray = dayInterval.split("-");
  155. if (CommonUtil.isEmpty(timeInterval)) {
  156. beginTime = rangeArray[0] + "000000";
  157. endTime = rangeArray[1] + "235959";
  158. } else {
  159. String[] timeRangeArray = timeInterval.split("-");
  160. beginTime = rangeArray[0] + timeRangeArray[0];
  161. endTime = rangeArray[1] + timeRangeArray[1];
  162. }
  163. Map<String, Object> param = new HashMap<String, Object>();
  164. param.put("beginTime", beginTime);
  165. param.put("endTime", endTime);
  166. String beginDay = DateUtil.dateAddMonth(rangeArray[1], Integer.parseInt("-2"));
  167. param.put("beginDay", beginDay);
  168. param.put("endDay", rangeArray[1]);
  169. param.put("countNum", "3");
  170. List<Map<String, String>> mountCountList = db.getMybatisMapper(ReportServiceMapper.class).selectMountCount00(param);
  171. List<String> addressNumList = CommonUtil.getIdFromList(mountCountList, "addressNum");
  172. DwFileDetail00Example fileDetail00Example = new DwFileDetail00Example();
  173. fileDetail00Example.createCriteria().andAddressNumIn(addressNumList)
  174. .andStartTimeGreaterThanOrEqualTo(beginDay).andStartTimeLessThanOrEqualTo(rangeArray[1]);
  175. fileDetail00Example.setOrderByClause("DFD0_ADDRESS_NUM desc");
  176. List<DwFileDetail00> fileDetail00List = db.selectByExample(DwFileDetail00Mapper.class, fileDetail00Example);
  177. int i = 0;
  178. String addressNumFlag = null;
  179. List<Map<String, String>> excelList = new ArrayList<Map<String,String>>();
  180. for (DwFileDetail00 dwFileDetail00 : fileDetail00List) {
  181. // 与上一条数据是否相同
  182. if (!dwFileDetail00.getAddressNum().equals(addressNumFlag) && addressNumFlag != null) {
  183. Map<String, String> excelMap = new HashMap<String, String>();
  184. excelMap.put("shichang", String.valueOf(i));
  185. excelList.add(excelMap);
  186. i = 0;
  187. }
  188. Map<String, String> excelMap = new HashMap<String, String>();
  189. excelMap.put("addressNum", dwFileDetail00.getAddressNum());
  190. excelMap.put("addressName", dwFileDetail00.getAddressName());
  191. excelMap.put("date", dwFileDetail00.getDate());
  192. excelMap.put("area", dwFileDetail00.getArea());
  193. excelMap.put("comonpanyName", dwFileDetail00.getComonpanyName());
  194. excelMap.put("terminalName", dwFileDetail00.getTerminalName());
  195. excelMap.put("terminalNum", dwFileDetail00.getTerminalNum());
  196. excelMap.put("terminalAddress", dwFileDetail00.getTerminalAddress());
  197. excelMap.put("startTime", dwFileDetail00.getStartTime());
  198. excelMap.put("endTime", dwFileDetail00.getEndTime());
  199. // 时差分钟
  200. Long dateDiff = DateUtil.dateDiff(dwFileDetail00.getStartTime(), dwFileDetail00.getEndTime());
  201. // 转换成小时
  202. String shichang = CommonUtil.divide(String.valueOf(dateDiff), "60", 2);
  203. excelMap.put("shichang", shichang);
  204. excelList.add(excelMap);
  205. addressNumFlag = dwFileDetail00.getAddressNum();
  206. i ++;
  207. }
  208. Map<String, String> excelMap = new HashMap<String, String>();
  209. excelMap.put("shichang", String.valueOf(i));
  210. excelList.add(excelMap);
  211. // 格式化时间
  212. excelList = Service.lookup(IFormatService.class).formatDateTime(excelList, "startTime", "endTime");
  213. OutputStream out=null;
  214. try {
  215. String[] excelHeader = {"台区名称#addressName","数据日期#date","台区编号#addressNum","单位#area","单位名称#comonpanyName","终端名称#terminalName","终端编号#terminalNum","终端地址码#terminalAddress","停电时间#startTime","来电时间#endTime","时长#shichang"};
  216. out = response.getOutputStream();
  217. HSSFWorkbook wb = ExportExcelUtil.export(response, reportInf.getFileName(), excelHeader, excelList);
  218. if(wb != null){
  219. wb.write(out);
  220. }
  221. out.flush();
  222. out.close();
  223. } catch (Exception e) {
  224. e.printStackTrace();
  225. }
  226. return response;
  227. }
  228. }