package com.minpay.reportManage.action; import java.io.OutputStream; import java.lang.reflect.InvocationTargetException; import java.text.ParseException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.minpay.common.format.IFormatService; import com.minpay.common.service.IReportService; import com.minpay.common.util.CommonUtil; import com.minpay.common.util.DateUtil; import com.minpay.common.util.ExportExcelUtil; import com.minpay.db.table.mapper.DwFileDetail00Mapper; import com.minpay.db.table.mapper.DwReportInfMapper; import com.minpay.db.table.mapper.PubAppparMapper; import com.minpay.db.table.model.DwFileDetail00; import com.minpay.db.table.model.DwFileDetail00Example; import com.minpay.db.table.model.DwReportInf; import com.minpay.db.table.model.PubApppar; import com.minpay.db.table.model.PubAppparExample; import com.minpay.db.table.own.mapper.ReportServiceMapper; import com.startup.minpay.frame.business.IMINAction; import com.startup.minpay.frame.business.res.MINActionResult; import com.startup.minpay.frame.constant.IMINBusinessConstant; import com.startup.minpay.frame.exception.MINBusinessException; import com.startup.minpay.frame.jdbc.MINRowBounds; import com.startup.minpay.frame.service.base.IMINDataBaseService; import com.startup.minpay.frame.service.base.Service; import com.startup.minpay.frame.session.MINSession; import com.startup.minpay.frame.target.MINAction; import com.startup.minpay.frame.target.MINComponent; import com.startup.minpay.frame.target.MINParam; @MINComponent public class TQTDReportAction implements IMINAction { private IMINDataBaseService db; /** 台区停电日报穿透数据 */ public final static String REPORT_INF_CHUANTOU = "reportInfChuantou"; /** 台区停电月报附件 */ public final static String REPORT_INF_FUJIAN = "reportInfFujian"; /** 台区数量管理 */ public final static String AREA_NUM_MANAGE = "areaNumManage"; /** 台区数量编辑 */ public final static String AREA_NUM_UPDATE = "areaNumUpdate"; /** * 台区停电穿透数据 * @param reportId 报表id * @param chuantouType 穿透类型 * @param session * @return * @throws MINBusinessException * @throws ParseException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SecurityException * @throws NoSuchMethodException */ @MINAction(value = REPORT_INF_CHUANTOU) public MINActionResult reportInfChuantou( @MINParam(key = "reportId") String reportId, @MINParam(key = "chuantouType") String chuantouType, MINSession session ) throws MINBusinessException, ParseException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { MINActionResult res = new MINActionResult(); db = Service.lookup(IMINDataBaseService.class); //分页 MINRowBounds rows = new MINRowBounds(); rows.setSeparateSql(true); // 查询报表具体信息 DwReportInf reportInf = db.selectByPrimaryKey(DwReportInfMapper.class, reportId); // 报表时间范围 Map param = new HashMap(); String dayInterval = reportInf.getDayInterval(); String timeInterval = reportInf.getTimeInterval(); String[] dayIntervalArray = dayInterval.split("-"); String[] timeIntervalArray = timeInterval.split("-"); String beginTime = dayIntervalArray[0] + timeIntervalArray[0]; String endTime = dayIntervalArray[1] + timeIntervalArray[1]; param.put("beginTime", beginTime); param.put("endTime", endTime); // 台区次数穿透数据 if ("TQTD01".equals(chuantouType)) { List> fileList00 = db.getMybatisMapper(ReportServiceMapper.class).selectReportType00(param); fileList00 = Service.lookup(IFormatService.class).formatDate(fileList00, "date"); fileList00 = Service.lookup(IFormatService.class).formatDateTime(fileList00, "startTime", "endTime"); res.set(IMINBusinessConstant.F_PAGING_LAY, fileList00); // month个月停电count次及以上台区穿透 } else if ("TQTD02".equals(chuantouType)) { String monthNum = reportInf.getTqtdMonth(); String countNum = reportInf.getTqtdCount(); // monthNum个月内停电 countNum次以上的台区 String beginDay = DateUtil.dateAddMonth(dayIntervalArray[1], Integer.parseInt("-" + monthNum)); param.put("beginDay", beginDay); param.put("endDay", dayIntervalArray[1]); param.put("countNum", countNum); List> mountCountList = db.getMybatisMapper(ReportServiceMapper.class).selectMountCount00(param); List addressNumList = CommonUtil.getIdFromList(mountCountList, "addressNum"); DwFileDetail00Example example = new DwFileDetail00Example(); example.createCriteria().andAddressNumIn(addressNumList).andStartTimeGreaterThanOrEqualTo(beginDay).andEndTimeLessThanOrEqualTo(dayIntervalArray[1]); example.setOrderByClause("DFD0_ADDRESS_NUM"); List selectByExample = db.selectByExample(DwFileDetail00Mapper.class, example); selectByExample = Service.lookup(IFormatService.class).formatDate(selectByExample, "date"); selectByExample = Service.lookup(IFormatService.class).formatDateTime(selectByExample, "startTime", "endTime"); res.set(IMINBusinessConstant.F_PAGING_LAY, selectByExample); // month个月停电count次及以上台区且昨日停电穿透 } else if ("TQTD03".equals(chuantouType)) { String monthNum = reportInf.getTqtdMonth(); String countNum = reportInf.getTqtdCount(); // monthNum个月内停电 countNum次以上的台区 String beginDay = DateUtil.dateAddMonth(dayIntervalArray[1], Integer.parseInt("-" + monthNum)); param.put("beginDay", beginDay); param.put("endDay", dayIntervalArray[1]); param.put("countNum", countNum); // 查询昨日停电的数据 param.put("yesterday", DateUtil.dateAddDay(dayIntervalArray[1], -1)); List> yesterdayList = db.getMybatisMapper(ReportServiceMapper.class).queryYesterdayDetail(param); param.put("yesterdayDetail", yesterdayList); List> mountCountList = db.getMybatisMapper(ReportServiceMapper.class).selectMountCount00(param); List addressNumList = CommonUtil.getIdFromList(mountCountList, "addressNum"); DwFileDetail00Example example = new DwFileDetail00Example(); example.createCriteria().andAddressNumIn(addressNumList).andStartTimeGreaterThanOrEqualTo(beginDay).andEndTimeLessThanOrEqualTo(dayIntervalArray[1]); example.setOrderByClause("DFD0_ADDRESS_NUM"); List selectByExample = db.selectByExample(DwFileDetail00Mapper.class, example); selectByExample = Service.lookup(IFormatService.class).formatDate(selectByExample, "date"); selectByExample = Service.lookup(IFormatService.class).formatDateTime(selectByExample, "startTime", "endTime"); res.set(IMINBusinessConstant.F_PAGING_LAY, selectByExample); // 2个月停电1次及以上台区且昨日停电穿透 } else if ("TQTD04".equals(chuantouType)) { String monthNum = "2"; String countNum = "1"; // monthNum个月内停电 countNum次以上的台区 String beginDay = DateUtil.dateAddMonth(dayIntervalArray[1], Integer.parseInt("-" + monthNum)); param.put("beginDay", beginDay); param.put("endDay", dayIntervalArray[1]); param.put("countNum", countNum); // 查询昨日停电的数据 param.put("yesterday", DateUtil.dateAddDay(dayIntervalArray[1], -1)); List> yesterdayList = db.getMybatisMapper(ReportServiceMapper.class).queryYesterdayDetail(param); param.put("yesterdayDetail", yesterdayList); List> mountCountList = db.getMybatisMapper(ReportServiceMapper.class).selectMountCount00(param); List addressNumList = CommonUtil.getIdFromList(mountCountList, "addressNum"); DwFileDetail00Example example = new DwFileDetail00Example(); example.createCriteria().andAddressNumIn(addressNumList).andStartTimeGreaterThanOrEqualTo(beginDay).andEndTimeLessThanOrEqualTo(dayIntervalArray[1]); example.setOrderByClause("DFD0_ADDRESS_NUM"); List selectByExample = db.selectByExample(DwFileDetail00Mapper.class, example); selectByExample = Service.lookup(IFormatService.class).formatDate(selectByExample, "date"); selectByExample = Service.lookup(IFormatService.class).formatDateTime(selectByExample, "startTime", "endTime"); res.set(IMINBusinessConstant.F_PAGING_LAY, selectByExample); // 台区停电时户数 } else if ("TQTD05".equals(chuantouType)) { List> fileList00 = db.getMybatisMapper(ReportServiceMapper.class).selectReportType00(param); fileList00 = Service.lookup(IFormatService.class).formatDate(fileList00, "date"); fileList00 = Service.lookup(IFormatService.class).formatDateTime(fileList00, "startTime", "endTime"); res.set(IMINBusinessConstant.F_PAGING_LAY, fileList00); } return res; } /** * 台区停电附件导出 * @param reportId * @param response * @param session * @return * @throws MINBusinessException * @throws ParseException * @throws NoSuchMethodException * @throws SecurityException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException */ @MINAction(value = REPORT_INF_FUJIAN) public HttpServletResponse reportInfFujian( @MINParam(key = "reportId") String reportId, HttpServletResponse response, MINSession session ) throws MINBusinessException, ParseException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { db = Service.lookup(IMINDataBaseService.class); //分页 MINRowBounds rows = new MINRowBounds(); rows.setSeparateSql(true); DwReportInf reportInf = db.selectByPrimaryKey(DwReportInfMapper.class, reportId); // 日期区间 String dayInterval = reportInf.getDayInterval(); // 时分秒区间 String timeInterval = reportInf.getTimeInterval(); // 获取时分秒 String beginTime = null; String endTime = null; String[] rangeArray = dayInterval.split("-"); if (CommonUtil.isEmpty(timeInterval)) { beginTime = rangeArray[0] + "000000"; endTime = rangeArray[1] + "235959"; } else { String[] timeRangeArray = timeInterval.split("-"); beginTime = rangeArray[0] + timeRangeArray[0]; endTime = rangeArray[1] + timeRangeArray[1]; } Map param = new HashMap(); param.put("beginTime", beginTime); param.put("endTime", endTime); String beginDay = DateUtil.dateAddMonth(rangeArray[1], Integer.parseInt("-2")); param.put("beginDay", beginDay); param.put("endDay", rangeArray[1]); param.put("countNum", "3"); List> mountCountList = db.getMybatisMapper(ReportServiceMapper.class).selectMountCount00(param); List addressNumList = CommonUtil.getIdFromList(mountCountList, "addressNum"); DwFileDetail00Example fileDetail00Example = new DwFileDetail00Example(); fileDetail00Example.createCriteria().andAddressNumIn(addressNumList) .andStartTimeGreaterThanOrEqualTo(beginDay).andStartTimeLessThanOrEqualTo(rangeArray[1]); fileDetail00Example.setOrderByClause("DFD0_ADDRESS_NUM desc"); List fileDetail00List = db.selectByExample(DwFileDetail00Mapper.class, fileDetail00Example); int i = 0; String addressNumFlag = null; List> excelList = new ArrayList>(); for (int j = 0; j < fileDetail00List.size(); j ++) { DwFileDetail00 dwFileDetail00 = fileDetail00List.get(j); // 与上一条数据是否相同 if (!dwFileDetail00.getAddressNum().equals(addressNumFlag) && addressNumFlag != null) { /*Map excelMap = new HashMap(); excelMap.put("shichang", String.valueOf(i)); excelList.add(excelMap);*/ for (int k = 0; k < i; k ++) { Map map = excelList.get(j - 1 - k); map.put("cishu", String.valueOf(i)); } i = 0; } Map excelMap = new HashMap(); excelMap.put("addressNum", dwFileDetail00.getAddressNum()); excelMap.put("addressName", dwFileDetail00.getAddressName()); excelMap.put("date", dwFileDetail00.getDate()); excelMap.put("area", dwFileDetail00.getArea()); excelMap.put("comonpanyName", dwFileDetail00.getComonpanyName()); excelMap.put("terminalName", dwFileDetail00.getTerminalName()); excelMap.put("terminalNum", dwFileDetail00.getTerminalNum()); excelMap.put("terminalAddress", dwFileDetail00.getTerminalAddress()); excelMap.put("startTime", dwFileDetail00.getStartTime()); excelMap.put("endTime", dwFileDetail00.getEndTime()); // 时差分钟 Long dateDiff = DateUtil.dateDiff(dwFileDetail00.getStartTime(), dwFileDetail00.getEndTime()); // 转换成小时 String shichang = CommonUtil.divide(String.valueOf(dateDiff), "60", 2); excelMap.put("shichang", shichang); excelList.add(excelMap); addressNumFlag = dwFileDetail00.getAddressNum(); i ++; } Map excelMap = new HashMap(); excelMap.put("shichang", String.valueOf(i)); excelList.add(excelMap); // 格式化时间 excelList = Service.lookup(IFormatService.class).formatDateTime(excelList, "startTime", "endTime"); OutputStream out=null; try { String[] excelHeader = {"台区名称#addressName","数据日期#date","台区编号#addressNum","单位#area","单位名称#comonpanyName","终端名称#terminalName","终端编号#terminalNum","终端地址码#terminalAddress","停电时间#startTime","来电时间#endTime","时长#shichang","停电次数#cishu"}; out = response.getOutputStream(); HSSFWorkbook wb = ExportExcelUtil.export(response, reportInf.getFileName(), excelHeader, excelList); if(wb != null){ wb.write(out); } out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } return response; } /** * 台区数量管理 * @param session * @return * @throws MINBusinessException */ @MINAction(value = AREA_NUM_MANAGE) public MINActionResult areaNumManage( MINSession session ) throws MINBusinessException { MINActionResult res = new MINActionResult(); db = Service.lookup(IMINDataBaseService.class); PubAppparExample appparExample = new PubAppparExample(); appparExample.createCriteria().andCodeEqualTo("tqtd_num"); List appparList = db.selectByExample(PubAppparMapper.class, appparExample); List> resList = new ArrayList>(); Map resMap = new HashMap(); resList.add(resMap); String hejiNum = "0"; for (PubApppar pub : appparList) { resMap.put(pub.getValue(), pub.getShowmsg()); hejiNum = CommonUtil.add(hejiNum, pub.getShowmsg()); } resMap.put("合计", hejiNum); res.set(IMINBusinessConstant.F_PAGING_LAY, resList); return res; } @MINAction(value = AREA_NUM_UPDATE) public MINActionResult areaNumUpdate( @MINParam(key = "field") String field, @MINParam(key = "value") String value, MINSession session ) throws MINBusinessException { MINActionResult res = new MINActionResult(); // 修改数据库数据 db = Service.lookup(IMINDataBaseService.class); PubApppar apppar = new PubApppar(); apppar.setCode("tqtd_num"); apppar.setValue(field); apppar.setShowmsg(value); db.updateByPrimaryKeySelective(PubAppparMapper.class, apppar); // 更新值 Service.lookup(IReportService.class).reflush(); return res; } }