package cn.com.goldenwater.dcproj.service.impl.plan; import cn.com.goldenwater.dcproj.dao.BisInspMtprggdDao; import cn.com.goldenwater.dcproj.model.BisInspMtprggd; import cn.com.goldenwater.dcproj.param.BisInspMtprggdParam; import cn.com.goldenwater.dcproj.param.BisInspPlanYearPrgParam; import cn.com.goldenwater.dcproj.service.BisInspMtprggdService; import cn.com.goldenwater.core.service.AbstractCrudService; import cn.com.goldenwater.dcproj.utils.StringUtils; import org.apache.poi.ss.usermodel.*; 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 cn.com.goldenwater.id.util.UuidUtil; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import java.util.Date; import java.util.Map; /** * @author lhc * @date 2022-3-17 */ @Service @Transactional public class BisInspMtprggdServiceImpl extends AbstractCrudService implements BisInspMtprggdService { @Autowired private BisInspMtprggdDao bisInspMtprggdDao; @Value("${export.templatePath}") private String templatePath; private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); private final SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM"); public BisInspMtprggdServiceImpl(BisInspMtprggdDao bisInspMtprggdDao) { super(bisInspMtprggdDao); this.bisInspMtprggdDao = bisInspMtprggdDao; } @Override public int insert(BisInspMtprggd bisInspMtprggd) { String uuid = UuidUtil.uuid(); // 生成uuid bisInspMtprggd.setId(uuid); bisInspMtprggd.setIntm(new Date()); bisInspMtprggd.setUptm(new Date()); bisInspMtprggd.setDataStat("0"); return this.bisInspMtprggdDao.insert(bisInspMtprggd); } @Override public int update(BisInspMtprggd bisInspMtprggd) { bisInspMtprggd.setUptm(new Date()); return this.bisInspMtprggdDao.update(bisInspMtprggd); } @Override public int delete(String id) { return this.bisInspMtprggdDao.delete(id); } @Override public List> getMonthRefrom(Map parmas) { List> listAll = new ArrayList>(); List> list = this.bisInspMtprggdDao.getMonthRefrom(parmas); if(list!=null && list.size()>0){ for(int i = 0;i> list1 = this.bisInspMtprggdDao.getObjNames(list.get(i).get("ID")); if(list1!=null && list1.size()>0){ String nameStr = ""; for(int j = 0;j-1){ listAll.add(list.get(i)); } } } } } if(StringUtils.isNotBlank(parmas.get("objName"))){ return listAll; }else{ return list; } } @Override public void export(HttpServletResponse response, Map parmas) { String templateFilePath = templatePath+ File.separator +"yjhtc.xls"; File file = new File(templateFilePath); if(file.exists()){ try{ InputStream inputStream = new FileInputStream(file); Workbook workbook = WorkbookFactory.create(inputStream); exportFile(workbook,parmas); writeToResponse(response,workbook,"月统筹计划.xls"); }catch(Exception e){ e.printStackTrace(); } } } @Override public List findListByMonth(BisInspPlanYearPrgParam bisInspPlanYearPrgParam) { return bisInspMtprggdDao.findListByMonth(bisInspPlanYearPrgParam); } @Override public void exportMonth(HttpServletResponse response, BisInspMtprggdParam bisInspMtprggdParam,BisInspPlanYearPrgParam bisInspPlanYearPrgParam,String type) { String templateFilePath = templatePath+ File.separator +"monthGd.xls"; File file = new File(templateFilePath); if(file.exists()){ try{ InputStream inputStream = new FileInputStream(file); Workbook workbook = WorkbookFactory.create(inputStream); exportFileMonth(workbook,bisInspMtprggdParam,bisInspPlanYearPrgParam,type); writeToResponse(response,workbook,"月进展情况.xls"); }catch(Exception e){ e.printStackTrace(); } } } private void exportFileMonth(Workbook xssfWorkbook, BisInspMtprggdParam bisInspMtprggdParam,BisInspPlanYearPrgParam bisInspPlanYearPrgParam,String type) { String title = "月进展情况"; List list = null; if(StringUtils.isNotBlank(type)){ list = bisInspMtprggdDao.findListByMonth(bisInspPlanYearPrgParam); }else{ list = bisInspMtprggdDao.findList(bisInspMtprggdParam); } Sheet sheet = xssfWorkbook.getSheetAt(0); if(list!=null && list.size()>0){ for(int i = 0;i parmas) { List> list = getMonthRefrom(parmas); Sheet sheet = xssfWorkbook.getSheetAt(0); String title = "月统筹计划"; if(StringUtils.isNotBlank(parmas.get("sttm")) && StringUtils.isNotBlank(parmas.get("entm"))){ title = parmas.get("sttm")+"~"+parmas.get("entm")+title; } CellStyle styleContent = xssfWorkbook.createCellStyle(); styleContent.setAlignment(HorizontalAlignment.CENTER); styleContent.setVerticalAlignment(VerticalAlignment.CENTER); setCellValue(sheet, 0, 1, title); if(list!=null && list.size()>0){ for(int i = 0;i map = list.get(i); setAllCell(sheet,i,map); } } } private void setAllCell(Sheet sheet, int i, Map map) { setCellValue(sheet, 2 + i, 0, String.valueOf(i + 1)); setCellValue(sheet, 2 + i, 1, map.get("GROUPNAME")); setCellValue(sheet, 2 + i, 2, map.get("OBJNAME")); setCellValue(sheet, 2 + i, 3, (map.get("STTM")==null?"":map.get("STTM"))+(map.get("ENTM")==null?"":"~"+map.get("ENTM"))); setCellValue(sheet, 2 + i, 4, map.get("CHKAREA")); setCellValue(sheet, 2 + i, 5, String.valueOf(map.get("COUNT"))); setCellValue(sheet, 2 + i, 6, "1".equals(map.get("CHKFORM"))?"暗访":"2".equals(map.get("CHKFORM"))?"明察":"3".equals(map.get("CHKFORM"))?"书面":"4".equals(map.get("CHKFORM"))?"四不两直":""); setCellValue(sheet, 2 + i, 7, map.get("LEADDEP")); setCellValue(sheet, 2 + i, 8, map.get("PERSNAME")); setCellValue(sheet, 2 + i, 9, map.get("MOBILENUMB")); setCellValue(sheet, 2 + i, 10,map.get("OBJNAMES")); } private void setCellValue(Sheet sheet, int rownum, int cellnum, String value) { Cell cell = sheet.getRow(rownum).getCell(cellnum); cell.setCellValue(value); } private void writeToResponse(HttpServletResponse response, Workbook workbook, String name) throws IOException { // 修改模板内容导出新模板 response.setContentType("application/x-msdownload"); String fileNameURL = URLEncoder.encode(name, "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileNameURL + ";filename*=utf-8''" + fileNameURL); OutputStream out = response.getOutputStream(); workbook.write(out); out.flush(); out.close(); } }