package cn.com.goldenwater.dcproj.service.impl.ducha; import cn.com.goldenwater.core.service.AbstractCrudService; import cn.com.goldenwater.dcproj.dao.BisInspPlanYearPrgDao; import cn.com.goldenwater.dcproj.model.BisInspPlanYearPrg; import cn.com.goldenwater.dcproj.param.BisInspPlanYearPrgParam; import cn.com.goldenwater.dcproj.service.BisInspPlanYearPrgService; import cn.com.goldenwater.dcproj.service.OlBisInspOrgService; import cn.com.goldenwater.dcproj.utils.AdLevelUtil; import cn.com.goldenwater.dcproj.utils.StringUtils; import cn.com.goldenwater.id.util.UuidUtil; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; 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 javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.List; /** * @author lhc * @date 2022-3-3 */ @Service @Transactional public class BisInspPlanYearPrgServiceImpl extends AbstractCrudService implements BisInspPlanYearPrgService { @Autowired private BisInspPlanYearPrgDao bisInspPlanYearPrgDao; @Value("${export.templatePath}") private String templatePath; @Autowired private OlBisInspOrgService olBisInspOrgService; private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); private static final SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-mm") ; public BisInspPlanYearPrgServiceImpl(BisInspPlanYearPrgDao bisInspPlanYearPrgDao) { super(bisInspPlanYearPrgDao); this.bisInspPlanYearPrgDao = bisInspPlanYearPrgDao; } @Override public int insert(BisInspPlanYearPrg bisInspPlanYearPrg) { String uuid = UuidUtil.uuid(); // 生成uuid bisInspPlanYearPrg.setId(uuid); bisInspPlanYearPrg.setIntm(new Date()); bisInspPlanYearPrg.setUptm(new Date()); bisInspPlanYearPrg.setDataStat("0"); return this.bisInspPlanYearPrgDao.insert(bisInspPlanYearPrg); } @Override public int update(BisInspPlanYearPrg bisInspPlanYearPrg) { bisInspPlanYearPrg.setUptm(new Date()); return this.bisInspPlanYearPrgDao.update(bisInspPlanYearPrg); } @Override public int delete(String id) { return this.bisInspPlanYearPrgDao.delete(id); } @Override public List getPlanByYearId(String yearId) { return this.bisInspPlanYearPrgDao.getPlanByYearId(yearId); } @Override public List findPageList(BisInspPlanYearPrgParam bisInspPlanYearPrgParam) { List list = bisInspPlanYearPrgDao.findList(bisInspPlanYearPrgParam); return list; } // @Override // public void export(HttpServletResponse response, BisInspPlanYearPrgParam bisInspPlanYearPrgParam,String type) { // String templateFilePath = templatePath+ File.separator +"planCheckSdMonth.xls"; // String addvcd = AdLevelUtil.getAddvcd(olBisInspOrgService.getProvince(bisInspPlanYearPrgParam.getOrgId())); // if(StringUtils.equals(addvcd, "37")){ // // 若是 山东省 // templateFilePath = templatePath+ File.separator +"planCheckSdMonth37.xls"; // } // File file = new File(templateFilePath); // if(file.exists()){ // try{ // InputStream inputStream = new FileInputStream(file); // Workbook workbook = WorkbookFactory.create(inputStream); // exportFile(workbook,bisInspPlanYearPrgParam,type,addvcd); // writeToResponse(response,workbook,"监督进度月报.xls"); // }catch(Exception e){ // e.printStackTrace(); // } // } // } @Override public void export(HttpServletResponse response, BisInspPlanYearPrgParam bisInspPlanYearPrgParam,String type) { String templateFilePath = templatePath+ File.separator +"planCheckSdMonth.xls"; String addvcd = AdLevelUtil.getAddvcd(olBisInspOrgService.getProvince(bisInspPlanYearPrgParam.getOrgId())); if(StringUtils.equals(addvcd, "37")){ // 若是 山东省 templateFilePath = templatePath+ File.separator +"监督检查开展情况表.xls"; } File file = new File(templateFilePath); if(file.exists()){ try{ InputStream inputStream = new FileInputStream(file); Workbook workbook = WorkbookFactory.create(inputStream); exportFile(workbook,bisInspPlanYearPrgParam,type,addvcd); writeToResponse(response,workbook,"监督检查开展情况表.xls"); }catch(Exception e){ e.printStackTrace(); } } } @Override public void export1(HttpServletResponse response, BisInspPlanYearPrgParam bisInspPlanYearPrgParam,String type) { String templateFilePath = templatePath+ File.separator +"planCheckSdMonth.xls"; String addvcd = AdLevelUtil.getAddvcd(olBisInspOrgService.getProvince(bisInspPlanYearPrgParam.getOrgId())); if(StringUtils.equals(addvcd, "37")){ // 若是 山东省 templateFilePath = templatePath+ File.separator +"业务性监督检查计划表.xls"; } File file = new File(templateFilePath); if(file.exists()){ try{ InputStream inputStream = new FileInputStream(file); Workbook workbook = WorkbookFactory.create(inputStream); exportFile1(workbook,bisInspPlanYearPrgParam,type,addvcd); writeToResponse(response,workbook,"业务性监督检查计划.xls"); }catch(Exception e){ e.printStackTrace(); } } } @Override public List findListByMonth(BisInspPlanYearPrgParam bisInspPlanYearPrgParam) { return bisInspPlanYearPrgDao.findListByMonth(bisInspPlanYearPrgParam); } // private void exportFile(Workbook xssfWorkbook, BisInspPlanYearPrgParam bisInspPlanYearPrgParam,String type, String addvcd) throws ParseException { // String title = "监督进度月报"; // List list = null; // if(null != bisInspPlanYearPrgParam.getChkMnth()){ // Date mnth = bisInspPlanYearPrgParam.getChkMnth(); // Instant instant = mnth.toInstant(); // ZoneId zoneId = ZoneId.systemDefault(); // LocalDateTime mnLocalDateTime = instant.atZone(zoneId).toLocalDateTime(); // DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM"); // bisInspPlanYearPrgParam.setChkMnth(monthFormat.parse(dtf.format(mnLocalDateTime))); // } // if(StringUtils.isNotBlank(type)){ // title = "月进度汇总"; // list = bisInspPlanYearPrgDao.findListByMonth(bisInspPlanYearPrgParam); // }else{ // title = "监督检查实施情况月报"; // list = bisInspPlanYearPrgDao.findList(bisInspPlanYearPrgParam); // } // int num[] = {0,0,0,0,0,0}; // Sheet sheet = xssfWorkbook.getSheetAt(0); // if(StringUtils.isBlank(type) && StringUtils.isNotBlank(bisInspPlanYearPrgParam.getSttm()) && StringUtils.isNotBlank(bisInspPlanYearPrgParam.getEntm())){ // title = title+"("+simpleDateFormat.format(simpleDateFormat.parse(bisInspPlanYearPrgParam.getSttm()))+"~"+simpleDateFormat.format(simpleDateFormat.parse(bisInspPlanYearPrgParam.getEntm()))+")"; // } // if(StringUtils.isBlank(type) && StringUtils.isNotNull(bisInspPlanYearPrgParam.getChkMnth()) ){ // // 设置sheet名称 // xssfWorkbook.setSheetName(0, monthFormat.format(bisInspPlanYearPrgParam.getChkMnth()) ); // title = title+"("+monthFormat.format(bisInspPlanYearPrgParam.getChkMnth())+")"; // } // CellStyle styleColor = sheet.getRow(1).getCell(0).getCellStyle(); // CellStyle styleContent = xssfWorkbook.createCellStyle(); // styleColor.setAlignment(HorizontalAlignment.CENTER); // styleColor.setVerticalAlignment(VerticalAlignment.CENTER); // styleContent.setAlignment(HorizontalAlignment.CENTER); // styleContent.setVerticalAlignment(VerticalAlignment.CENTER); // if(!StringUtils.equals(addvcd, "37")){ // setCellValue(sheet, 0, 1, title); // } // if(list!=null && list.size()>0){ // CellStyle contentStyle = sheet.getRow(3).getCell(0).getCellStyle(); // contentStyle.setAlignment(HorizontalAlignment.CENTER); // contentStyle.setVerticalAlignment(VerticalAlignment.CENTER); // int size = list.size(); // if(StringUtils.equals(addvcd, "37")){ // size = size+1; // } // for(int i = 0;i0){ // // 山东省 增加汇总行 // int lastRowIndex = list.size()+3; // CellRangeAddress region = new CellRangeAddress(lastRowIndex, lastRowIndex, 0, 1); // sheet.addMergedRegion(region); // Cell mergeCell = sheet.getRow(lastRowIndex).getCell(0) ; // mergeCell.setCellValue("合计"); // CellStyle mergeCellCellStyle = mergeCell.getCellStyle(); // mergeCellCellStyle.setAlignment(HorizontalAlignment.CENTER); // mergeCellCellStyle.setAlignment(HorizontalAlignment.CENTER); // mergeCell.setCellStyle(mergeCellCellStyle); // // 从 第3列 到14列 // for(int i=2 ;i<=13 ;i++){ // // 从第4行到最后一行 求和 // String colString = CellReference.convertNumToColString(i); // if( 4==i || 5 ==i){ // // 求和公式 // String sumstring = "SUM(" ; // for(int k= 4;k<=lastRowIndex;k++){ // sumstring += "IF(COUNTBLANK("+colString +k+"),0,1)," ; // } // sumstring = sumstring.substring(0,sumstring.length()-1); // sumstring += ")"; // // 设置求和 // sheet.getRow(lastRowIndex).getCell(i).setCellFormula(sumstring); // continue; // } // // 求和公式 // String sumstring = "SUM(" + colString + "4:" + colString + lastRowIndex + ")"; // // 设置求和 // sheet.getRow(lastRowIndex).getCell(i).setCellFormula(sumstring); // } // } // int lastRowIndex = list.size()+2; // setCellValue(sheet, lastRowIndex, 5, String.valueOf(num[0])); // setCellValue(sheet, lastRowIndex, 6, String.valueOf(num[1])); // setCellValue(sheet, lastRowIndex, 7, String.valueOf(num[2])); // setCellValue(sheet, lastRowIndex, 9, String.valueOf(num[3])); // setCellValue(sheet, lastRowIndex, 10, String.valueOf(num[4])); // setCellValue(sheet, lastRowIndex, 11, String.valueOf(num[5])); // Row row = sheet.getRow(lastRowIndex); // CellRangeAddress region = new CellRangeAddress(lastRowIndex, lastRowIndex, 0, 4); // sheet.addMergedRegion(region); // row.getCell(0).setCellValue("总计"); // row.getCell(0).setCellStyle(style); // } // // } private void exportFile(Workbook xssfWorkbook, BisInspPlanYearPrgParam bisInspPlanYearPrgParam,String type, String addvcd) throws ParseException { String title = "月份业务性监督检查计划表"; if(null != bisInspPlanYearPrgParam.getChkMnth()){ Date mnth = bisInspPlanYearPrgParam.getChkMnth(); Instant instant = mnth.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime mnLocalDateTime = instant.atZone(zoneId).toLocalDateTime(); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM"); bisInspPlanYearPrgParam.setChkMnth(monthFormat.parse(dtf.format(mnLocalDateTime))); } if(StringUtils.isBlank(type) && StringUtils.isNotBlank(bisInspPlanYearPrgParam.getSttm()) && StringUtils.isNotBlank(bisInspPlanYearPrgParam.getEntm())){ title = "("+monthFormat.format(bisInspPlanYearPrgParam.getSttm())+"~"+monthFormat.format(bisInspPlanYearPrgParam.getEntm())+")"+title; } if(StringUtils.isBlank(type) && StringUtils.isNotNull(bisInspPlanYearPrgParam.getChkMnth()) ){ // 设置sheet名称 xssfWorkbook.setSheetName(0, monthFormat.format(bisInspPlanYearPrgParam.getChkMnth()) ); title = "("+monthFormat.format(bisInspPlanYearPrgParam.getChkMnth())+")"+title; } List list = bisInspPlanYearPrgDao.findListByMonth(bisInspPlanYearPrgParam); int num[] = {0,0,0,0,0,0}; Sheet sheet = xssfWorkbook.getSheetAt(0); CellStyle styleColor = sheet.getRow(1).getCell(0).getCellStyle(); CellStyle styleContent = xssfWorkbook.createCellStyle(); styleColor.setAlignment(HorizontalAlignment.CENTER); styleColor.setVerticalAlignment(VerticalAlignment.CENTER); styleContent.setAlignment(HorizontalAlignment.CENTER); styleContent.setVerticalAlignment(VerticalAlignment.CENTER); if(!StringUtils.equals(addvcd, "37")){ setCellValue(sheet, 0, 1, title); } if(list!=null && list.size()>0){ CellStyle contentStyle = sheet.getRow(2).getCell(0).getCellStyle(); contentStyle.setAlignment(HorizontalAlignment.CENTER); contentStyle.setVerticalAlignment(VerticalAlignment.CENTER); int size = list.size(); for(int i = 0;i list = bisInspPlanYearPrgDao.findListByMonth(bisInspPlanYearPrgParam); int num[] = {0,0,0,0,0,0}; Sheet sheet = xssfWorkbook.getSheetAt(0); CellStyle styleColor = sheet.getRow(1).getCell(0).getCellStyle(); CellStyle styleContent = xssfWorkbook.createCellStyle(); styleColor.setAlignment(HorizontalAlignment.CENTER); styleColor.setVerticalAlignment(VerticalAlignment.CENTER); styleContent.setAlignment(HorizontalAlignment.CENTER); styleContent.setVerticalAlignment(VerticalAlignment.CENTER); if(!StringUtils.equals(addvcd, "37")){ setCellValue(sheet, 0, 1, title); } if(list!=null && list.size()>0){ CellStyle contentStyle = sheet.getRow(2).getCell(0).getCellStyle(); contentStyle.setAlignment(HorizontalAlignment.CENTER); contentStyle.setVerticalAlignment(VerticalAlignment.CENTER); int size = list.size(); for(int i = 0;i