Java tutorial
package DataAn.reportManager.service.impl; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestMapping; import com.aspose.words.Document; import com.aspose.words.SaveFormat; import com.aspose.words.net.System.Data.DataColumn; import com.aspose.words.net.System.Data.DataRelation; import com.aspose.words.net.System.Data.DataRow; import com.aspose.words.net.System.Data.DataSet; import com.aspose.words.net.System.Data.DataTable; import DataAn.common.config.CommonConfig; import DataAn.common.pageModel.Pager; import DataAn.common.utils.DateUtil; import DataAn.common.utils.UUIDGeneratorUtil; import DataAn.fileSystem.dto.FileDto; import DataAn.fileSystem.dto.MongoFSDto; import DataAn.fileSystem.option.FileType; import DataAn.galaxyManager.option.J9Series_Star_ParameterType; import DataAn.jfreechart.dto.ConstraintDto; import DataAn.jfreechart.dto.LineChartDto; import DataAn.jfreechart.service.IJfreechartServcie; import DataAn.mongo.db.MongodbUtil; import DataAn.mongo.fs.IDfsDb; import DataAn.mongo.fs.MongoDfsDb; import DataAn.mongo.service.IMongoService; import DataAn.mongo.zip.ZipCompressorByAnt; import DataAn.reportManager.config.OptionConfig; import DataAn.reportManager.dao.IReportFileSystemDao; import DataAn.reportManager.domain.ReportFileSystem; import DataAn.reportManager.domain.StarParam; import DataAn.reportManager.dto.DataToDocDto; import DataAn.reportManager.dto.ParamDto; import DataAn.reportManager.dto.ParamImgDataDto; import DataAn.reportManager.dto.ProductDto; import DataAn.reportManager.dto.ReportFileDto; import DataAn.reportManager.option.ReportDataType; import DataAn.reportManager.service.IReoportService; import DataAn.reportManager.service.IStarParamService; import DataAn.reportManager.util.AsposeLicenseManage; import DataAn.reportManager.util.MapMailMergeDataSource; @Service public class ReportServiceImpl implements IReoportService { @Resource private IReportFileSystemDao fileDao; @Resource private IStarParamService starParamService; @Resource private IJfreechartServcie jfreechartServcie; @Resource private IMongoService iMongoService; @Override public void reportNullDoc(String filename, String templateUrl, String docPath, String beginDate, String endDate, String msg) throws Exception { // ?License if (!AsposeLicenseManage.getAsposeLicense()) { return; } // 1 ?? Document doc = new Document(templateUrl); doc.getMailMerge() .executeWithRegions(new MapMailMergeDataSource(getNullDoc(beginDate, endDate, msg), "nullTab")); // 3? doc.save(docPath, SaveFormat.DOC); } private List<Map<String, Object>> getNullDoc(String beginDate, String endDate, String msg) { List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>(); Map<String, Object> record = new HashMap<String, Object>(); // record.put("title", "" + beginDate + "" + endDate + "" + msg); record.put("title", "" + beginDate + "" + endDate + "?"); dataList.add(record); return dataList; } @Override public void reportDoc(String filename, DataToDocDto data, String templateUrl, String docPath) throws Exception { // ?License if (!AsposeLicenseManage.getAsposeLicense()) { return; } // 1 ?? Document doc = new Document(templateUrl); DataSet dataSet = new DataSet(); DataTable product = new DataTable("productList"); product.getColumns().add(new DataColumn("productName")); product.getColumns().add(new DataColumn("movableNum")); DataRow row_pro = null; List<ProductDto> products = data.getProducts(); for (ProductDto productDto : products) { row_pro = product.newRow(); row_pro.set("productName", productDto.getProductName()); row_pro.set("movableNum", productDto.getMovableNum()); product.getRows().add(row_pro); } DataTable param = new DataTable("paramList"); param.getColumns().add(new DataColumn("productName")); param.getColumns().add(new DataColumn("paramName")); param.getColumns().add(new DataColumn("paramNumMax")); param.getColumns().add(new DataColumn("paramNumMin")); List<ParamDto> params = data.getParams(); DataRow row_par = null; for (ParamDto paramDto : params) { row_par = param.newRow(); row_par.set("productName", paramDto.getProductName()); row_par.set("paramName", paramDto.getParamName()); row_par.set("paramNumMax", paramDto.getParamNumMax()); row_par.set("paramNumMin", paramDto.getParamNumMin()); param.getRows().add(row_par); } dataSet.getTables().add(product); dataSet.getTables().add(param); dataSet.getRelations().add(new DataRelation("paramListForProduct", product.getColumns().get("productName"), param.getColumns().get("productName"))); doc.getMailMerge().executeWithRegions(new MapMailMergeDataSource(getTitle(data), "titleTab")); doc.getMailMerge().executeWithRegions(new MapMailMergeDataSource( getImgTab(data.getOneParamImg(), "parNameOne", "parImgOne"), "firstImgTab")); doc.getMailMerge().executeWithRegions(new MapMailMergeDataSource( getImgTab(data.getTwoParamImg(), "parNameTwo", "parImgTwo"), "twoImgTab")); doc.getMailMerge().executeWithRegions(new MapMailMergeDataSource( getImgTab(data.getThreeParamImg(), "parNameThree", "parImgThree"), "thirdImgTab")); doc.getMailMerge().executeWithRegions(dataSet); // 3? doc.save(docPath, SaveFormat.DOC); } private List<Map<String, Object>> getTitle(DataToDocDto data) throws Exception { List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>(); Map<String, Object> record = new HashMap<String, Object>(); record.put("series", data.getSeries()); record.put("star", data.getStar()); record.put("beginDate", data.getBeginDate()); record.put("endDate", data.getEndDate()); record.put("createDate", data.getCreateDate()); dataList.add(record); return dataList; } private List<Map<String, Object>> getImgTab(List<ParamImgDataDto> paramImgDatas, String parName, String parImg) throws Exception { List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>(); if (paramImgDatas != null && paramImgDatas.size() > 0) { byte[] image = null; for (ParamImgDataDto param : paramImgDatas) { if (StringUtils.isNotBlank(parImg) && StringUtils.isNotBlank(param.getParImg())) { FileInputStream fis = new FileInputStream(param.getParImg()); image = new byte[fis.available()]; fis.read(image); fis.close(); } Map<String, Object> record = new HashMap<String, Object>(); record.put(parName, param.getParName()); record.put(parImg, image); dataList.add(record); } } return dataList; } @Override public ReportFileSystem saveReport(ReportFileDto reportFileDto, Map<String, String> dataMap) { String uuId = dataMap.get("versions"); String series = dataMap.get("series"); String star = dataMap.get("star"); String date = dataMap.get("date"); String year = dataMap.get("year"); String month = dataMap.get("month"); String startTime = dataMap.get("startTime"); String endTime = dataMap.get("endTime"); String partsType = dataMap.get("partsType"); String partsName = dataMap.get("partsName"); String databaseName = dataMap.get("databaseName"); saveDocToMongoDFs(reportFileDto, uuId, databaseName); ReportFileSystem docDir = createDocDir(series, star, partsType, partsName); ReportFileSystem yearDir = createYearDir(series, star, year, partsType, docDir); ReportFileSystem monthDir = createMonthDir(series, star, year, month, partsType, yearDir); return saveFile(reportFileDto, uuId, series, star, date, startTime, endTime, partsType, monthDir); } private void saveDocToMongoDFs(ReportFileDto reportFileDto, String uuId, String databaseName) { IDfsDb dfs = MongoDfsDb.getInstance(); dfs.upload(databaseName, reportFileDto.getFileName(), uuId, reportFileDto.getIn()); } private ReportFileSystem saveFile(ReportFileDto reportFileDto, String uuId, String series, String star, String date, String startTime, String endTime, String partsType, ReportFileSystem monthDir) { ReportFileSystem file = new ReportFileSystem(); file.setSeries(series); file.setStar(star); file.setStartTime(startTime); file.setEndTime(endTime); file.setPartsType(partsType); file.setDataType(ReportDataType.DOC); file.setFileName(reportFileDto.getFileName()); file.setFileSize(reportFileDto.getFileSize()); file.setFileType(FileType.FILE); file.setParentId(monthDir.getId()); file.setYear_month_day(date); file.setMongoFSUUId(uuId); return fileDao.add(file); } private ReportFileSystem createMonthDir(String series, String star, String year, String month, String partsType, ReportFileSystem yearDir) { // ReportFileSystem monthDir = fileDao.selectByParentIdAndFileName(yearDir.getId(), month); ReportFileSystem monthDir = fileDao.selectBySeriesAndStarAndParameterTypeAndParentIdAndFileName(series, star, partsType, yearDir.getId(), month); if (monthDir == null) { monthDir = new ReportFileSystem(); monthDir.setSeries(series); monthDir.setStar(star); monthDir.setPartsType(partsType); monthDir.setDataType(ReportDataType.DOC); monthDir.setFileName(month); monthDir.setFileType(FileType.DIR); monthDir.setYear_month_day(year + "-" + month); monthDir.setParentId(yearDir.getId()); monthDir = fileDao.add(monthDir); } return monthDir; } private ReportFileSystem createYearDir(String series, String star, String year, String partsType, ReportFileSystem docDir) { // ReportFileSystem yearDir = fileDao.selectByParentIdAndFileName(docDir.getId(), year); ReportFileSystem yearDir = fileDao.selectBySeriesAndStarAndParameterTypeAndParentIdAndFileName(series, star, partsType, docDir.getId(), year); if (yearDir == null) { yearDir = new ReportFileSystem(); yearDir.setSeries(series); yearDir.setStar(star); yearDir.setPartsType(partsType); yearDir.setDataType(ReportDataType.DOC); yearDir.setFileName(year); yearDir.setFileType(FileType.DIR); yearDir.setYear_month_day(year); yearDir.setParentId(docDir.getId()); yearDir = fileDao.add(yearDir); } return yearDir; } private ReportFileSystem createDocDir(String series, String star, String partsType, String partsName) { // ReportFileSystem docDir = fileDao.selectByParentIdisNullAndFileName(partsName); ReportFileSystem docDir = fileDao.selectBySeriesAndStarAndParameterTypeAndParentIdAndFileName(series, star, partsType, 0, partsName); if (docDir == null) { docDir = new ReportFileSystem(); docDir.setSeries(series); docDir.setPartsType(partsType); docDir.setStar(star); docDir.setDataType(ReportDataType.DOC); docDir.setFileName(partsName); docDir.setFileType(FileType.DIR); docDir = fileDao.add(docDir); } return docDir; } @Override public void downLoadReportForDb(long fileId, String databaseName, HttpServletResponse response) { OutputStream os = null; InputStream inputStream = null; try { ReportFileDto fileDto = downloadFile(fileId, databaseName); response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName=" + new String(fileDto.getFileName().getBytes("gb2312"), "ISO8859-1")); inputStream = fileDto.getIn(); os = response.getOutputStream(); byte[] b = new byte[2048]; int length; while ((length = inputStream.read(b)) > 0) { os.write(b, 0, length); } } catch (Exception e) { e.printStackTrace(); } finally { closeStream(os, inputStream); } } private void closeStream(OutputStream os, InputStream inputStream) { // ? if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (os != null) { // ? try { os.close(); } catch (Exception e) { e.printStackTrace(); } } } @Transactional(readOnly = true) public ReportFileDto downloadFile(long fileId, String databaseName) throws Exception { ReportFileDto fileDto = new ReportFileDto(); // ??? ReportFileSystem file = fileDao.get(fileId); fileDto.setFileName(file.getFileName()); fileDto.setFileSize(file.getFileSize()); // mongofs??? IDfsDb dfs = MongoDfsDb.getInstance(); fileDto.setIn(dfs.downLoadToStream(databaseName, file.getMongoFSUUId())); return fileDto; } @Override public void downLoadReportForDis(InputStream inputStream, String fileName, HttpServletResponse response) { OutputStream os = null; try { response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1")); os = response.getOutputStream(); byte[] b = new byte[2048]; int length; while ((length = inputStream.read(b)) > 0) { os.write(b, 0, length); } } catch (Exception e) { e.printStackTrace(); } finally { closeStream(os, inputStream); } } @RequestMapping("/downloads") public String downLoadsReportForDb(String itemIds, String databaseName, HttpServletResponse response) { BufferedInputStream buff = null; OutputStream myout = null; FileInputStream fis = null; try { FileDto fileDto = downloadFiles(itemIds, databaseName); response.setContentType("text/html; charset=GBK"); // file File file = new File(fileDto.getFilePath()); // response?? response.setContentType("application/octet-stream"); // ?? response.setContentLength((int) file.length()); // ? response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileDto.getFileName().getBytes("gb2312"), "ISO8859-1")); // i/o? fis = new FileInputStream(file); buff = new BufferedInputStream(fis); byte[] b = new byte[1024];// long k = 0;// ? // response?, myout = response.getOutputStream(); // while (k < file.length()) { int j = buff.read(b, 0, 1024); k += j; // b? myout.write(b, 0, j); } // ?,? myout.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (buff != null) { buff.close(); } if (myout != null) { myout.close(); } if (fis != null) { fis.close(); } } catch (Exception e) { e.printStackTrace(); } } return null; } @Override public void deleteFile(String ids) { String[] arrayIds = ids.split(","); ReportFileSystem file = null; Set<String> uuIds = new HashSet<String>(); for (String id : arrayIds) { String[] items = id.split("/"); // ?? if ("doc".equals(items[1])) { ReportFileSystem dir = fileDao.get(Long.parseLong(items[0])); uuIds.addAll(deleteFile(dir)); } else { file = fileDao.get(Long.parseLong(items[0])); uuIds.add(file.getMongoFSUUId()); fileDao.delete(file); } } // mongodb? IDfsDb dfs = MongoDfsDb.getInstance(); MongodbUtil mg = MongodbUtil.getInstance(); String collectionName = ""; for (String uuId : uuIds) { if (uuId != null && !uuId.equals("")) { dfs.delete(uuId); } } } private Set<String> deleteFile(ReportFileSystem dir) { Set<String> uuIds = new HashSet<String>(); List<ReportFileSystem> fileList = fileDao.findByParam("parentId", dir.getId()); if (fileList != null && fileList.size() > 0) { for (ReportFileSystem childFile : fileList) { if (childFile.getFileType().getName().equals("dir")) { uuIds.addAll(deleteFile(childFile)); } uuIds.add(childFile.getMongoFSUUId()); // fileDao.delete(childFile); } } return uuIds; } @Override @Transactional(readOnly = true) public Pager<MongoFSDto> getMongoFSList(int pageIndex, int pageSize, String series, String star, String partsType, long dirId, String beginTime, String endTime, String sort, String order) { String sort_order = ""; if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) sort_order = sort + " " + order; else sort_order = "updateDate desc"; Pager<ReportFileSystem> pager = fileDao.selectByOption(series, star, partsType, dirId, beginTime, endTime, sort_order, pageIndex, pageSize); return returnPager(pageIndex, pageSize, pager.getRows(), pager.getTotalCount()); } private Pager<MongoFSDto> returnPager(int pageIndex, int pageSize, List<ReportFileSystem> fileList, long totalCount) { List<MongoFSDto> fsList = new ArrayList<MongoFSDto>(); if (fileList != null && fileList.size() > 0) { MongoFSDto fsDto = null; for (ReportFileSystem fs : fileList) { fsDto = new MongoFSDto(); fsDto.setId(fs.getId()); fsDto.setCreateDate(DateUtil.format(fs.getUpdateDate())); if (fs.getFileType().getName().equals("dir")) { fsDto.setFileSize("-"); } else { fsDto.setFileSize(String.valueOf(fs.getFileSize()) + " KB"); } fsDto.setName(fs.getFileName()); fsDto.setType(fs.getFileType().getName()); fsList.add(fsDto); } } Pager<MongoFSDto> pager = new Pager<MongoFSDto>(pageIndex, pageSize, totalCount, fsList); return pager; } @Override public Pager<MongoFSDto> getMongoFSList(int pageIndex, int pageSize, String series, String star, String partsType, long dirId, String sort, String order) { String sort_order = ""; if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) sort_order = sort + " " + order; else sort_order = "updateDate desc"; Pager<ReportFileSystem> pager = null; if (dirId == 0) { pager = fileDao.selectBySeriesAndStarAndParameterTypeAndParentIdisNullAndOrder(series, star, partsType, sort_order, pageIndex, pageSize); } else { pager = fileDao.selectBySeriesAndStarAndParameterTypeAndParentIdAndOrder(series, star, partsType, dirId, sort_order, pageIndex, pageSize); } return this.returnPager(pageIndex, pageSize, pager.getRows(), pager.getTotalCount()); } @Override public String getParentFSCatalog(long dirId) { List<ReportFileSystem> list = new ArrayList<ReportFileSystem>(); ReportFileSystem fs = fileDao.get(dirId); list.add(fs); Long parentId = fs.getParentId(); while (parentId != null) { ReportFileSystem parentFs = fileDao.get(parentId); list.add(parentFs); parentId = parentFs.getParentId(); } Collections.reverse(list); StringBuilder sb = new StringBuilder(); sb.append("{"); for (ReportFileSystem f : list) { sb.append("\"" + f.getId().toString() + "\"" + ":" + "\"" + f.getFileName() + "\"" + ","); } if (sb.lastIndexOf(",") != -1) { sb.deleteCharAt(sb.lastIndexOf(",")); } sb.append("}"); return sb.toString(); } @Override @Transactional(readOnly = true) public FileDto downloadFiles(String ids, String databaseName) throws Exception { FileDto fileDto = new FileDto(); String[] arrayIds = ids.split(","); String mogodbFilePath = CommonConfig.getDownloadCachePath(); ReportFileSystem file = null; IDfsDb dfs = MongoDfsDb.getInstance(); for (String id : arrayIds) { String[] items = id.split("/"); // ?? if ("dir".equals(items[1])) { writeDirFile(Long.parseLong(items[0]), dfs, mogodbFilePath, databaseName); } else { file = fileDao.get(Long.parseLong(items[0])); // ? dfs.downLoadToLocal(databaseName, file.getMongoFSUUId(), mogodbFilePath); } } String zipFileName = DateUtil.format(new Date(), "yyyy-MM-dd-HH-mm-ss") + ".zip"; String zipPath = CommonConfig.getZipCachePath() + File.separator + zipFileName; ZipCompressorByAnt zca = new ZipCompressorByAnt(zipPath); zca.compressExe(mogodbFilePath); fileDto.setFileName(zipFileName); fileDto.setFilePath(zipPath); return fileDto; } private void writeDirFile(long dirId, IDfsDb dfs, String path, String databaseName) throws Exception { ReportFileSystem dir = fileDao.get(dirId); path = path + File.separator + dir.getFileName(); List<ReportFileSystem> fileList = fileDao.findByParam("parentId", dirId); if (fileList != null && fileList.size() > 0) { for (ReportFileSystem childFile : fileList) { if ("dir".equals(childFile.getFileType().getName())) { writeDirFile(childFile.getId(), dfs, path, databaseName); } else { dfs.downLoadToLocal(databaseName, childFile.getMongoFSUUId(), path); } } } } @Override public void createReport(Date beginDate, Date endDate, String filename, String templateUrl, String docPath, String seriesId, String starId, String partsType) throws Exception { if (J9Series_Star_ParameterType.TOP.getValue().equals(partsType)) { this.createTopReport(beginDate, endDate, filename, templateUrl, docPath, seriesId, starId, partsType); } else { this.createFlywheelReport(beginDate, endDate, filename, templateUrl, docPath, seriesId, starId, partsType); } } @Override public Map<String, List<ConstraintDto>> getConstraintDtoList(String seriesId, String starId, String partsType) { List<StarParam> starParamList = starParamService.getStarParamForReport(seriesId, starId, partsType); List<String> parList = new ArrayList<String>(); String paramStr = ",?"; String[] parArr = paramStr.split(","); for (String p : parArr) { parList.add(p); } Map<String, List<ConstraintDto>> constraintsMap = new HashMap<String, List<ConstraintDto>>(); List<StarParam> doubleList = new ArrayList<StarParam>(); // ???? List<StarParam> firstList = new ArrayList<StarParam>(); for (StarParam starParam : starParamList) { if (!parList.contains(starParam.getParameterType())) { String key = starParam.getProductName() + starParam.getParameterType(); List<ConstraintDto> listSingle = new ArrayList<ConstraintDto>(); listSingle.add(this.StarParamToConstraintDto(starParam)); constraintsMap.put(key, listSingle); firstList.add(starParam); } else { doubleList.add(starParam); } } // ? A?B List<String> productType = new ArrayList<String>(); for (StarParam starParam : doubleList) { if (!productType.contains(starParam.getProductName())) { productType.add(starParam.getProductName()); } } // ??,?? A,? B,? for (String product : productType) { List<ConstraintDto> productlist = new ArrayList<ConstraintDto>(); for (StarParam starParam : doubleList) { if (product.equals(starParam.getProductName())) { productlist.add(this.StarParamToConstraintDto(starParam)); } } constraintsMap.put(product + paramStr, productlist); } // ? ???? List<String> parameterType = new ArrayList<String>(); for (StarParam starParam : starParamList) { if (!parameterType.contains(starParam.getParameterType())) { parameterType.add(starParam.getParameterType()); } } // ???? ??? for (String string : parameterType) { List<ConstraintDto> parameterTypelist = new ArrayList<ConstraintDto>(); for (StarParam starParam : starParamList) { if (string.equals(starParam.getParameterType())) { parameterTypelist.add(this.StarParamToConstraintDto(starParam)); } } constraintsMap.put(string, parameterTypelist); } return constraintsMap; } private ConstraintDto StarParamToConstraintDto(StarParam starParam) { ConstraintDto constraintDto = new ConstraintDto(); constraintDto.setParamName(starParam.getParamName()); constraintDto.setParamCode(starParam.getParamCode()); constraintDto.setMax(starParam.getEffeMax()); constraintDto.setMin(starParam.getEffeMin()); constraintDto.setUnits(starParam.getValueUnit()); return constraintDto; } private int getJobNumByDeviceName(String seriesId, String starId, String partsType, Date beginDate, Date endDate, String deviceName) { long mnum = iMongoService.findJobNumByDeviceName(seriesId, starId, partsType, deviceName, beginDate, endDate); return Integer.parseInt(String.valueOf(mnum)); } @Override public ReportFileSystem insertReportToDB(String filename, String docPath, String seriesId, String starId, String partsType, String startTime, String endTime, String databaseName, String partsName) throws FileNotFoundException, IOException { /******************************** ?db ***********************************/ Map<String, String> dataMap = new HashMap<String, String>(); dataMap.put("series", seriesId); dataMap.put("star", starId); String date = DateUtil.getNowTime("yyyy-MM-dd"); dataMap.put("date", DateUtil.formatString(date, "yyyy-MM-dd", "yyyy-MM-dd")); String year = DateUtil.formatString(date, "yyyy-MM-dd", "yyyy"); dataMap.put("year", year); String month = DateUtil.formatString(date, "yyyy-MM-dd", "MM"); dataMap.put("month", month); String versions = UUIDGeneratorUtil.getUUID(); dataMap.put("versions", versions); dataMap.put("startTime", startTime); dataMap.put("endTime", endTime); dataMap.put("partsType", partsType); dataMap.put("partsName", partsName); dataMap.put("databaseName", databaseName); InputStream input = new FileInputStream(docPath); ReportFileDto reportFileDto = new ReportFileDto(); DecimalFormat df = new DecimalFormat("#.00"); reportFileDto.setFileName(filename); double size = input.available() / 1024; String strSize = df.format(size); reportFileDto.setFileSize(Float.parseFloat(strSize)); reportFileDto.setIn(input); ReportFileSystem reportFileSystem = saveReport(reportFileDto, dataMap); input.close(); return reportFileSystem; } @Override public void removeDoc(String docPath) { File file = new File(docPath); if (file.exists()) { file.delete(); } } @Override public void downloadReport(HttpServletResponse response, String docPath, String filename) throws FileNotFoundException { InputStream inputStream = new FileInputStream(docPath); downLoadReportForDis(inputStream, filename, response); } private void createFlywheelReport(Date beginDate, Date endDate, String filename, String templateUrl, String docPath, String seriesId, String starId, String partsType) throws Exception { DataToDocDto data = new DataToDocDto(); data.setSeries(seriesId); data.setStar(starId); data.setBeginDate(DateUtil.format(beginDate, "yyyy-MM-dd")); data.setEndDate(DateUtil.format(endDate, "yyyy-MM-dd")); data.setCreateDate(DateUtil.getNowTime("yyyy-MM-dd")); List<StarParam> starParamList = starParamService.getStarParamForReport(seriesId, starId, partsType); if (starParamList == null || starParamList.size() < 1) { String templateNullUrl = OptionConfig.getWebPath() + "\\report\\wordtemplate\\nullData.doc"; reportNullDoc(filename, templateNullUrl, docPath, data.getBeginDate(), data.getEndDate(), "?????"); return; } Map<String, List<ConstraintDto>> constraintsMap = new HashMap<String, List<ConstraintDto>>(); List<String> parList = new ArrayList<String>(); String paramStr = OptionConfig.getParamStr(partsType); if (StringUtils.isNotBlank(paramStr)) { String[] parArr = paramStr.split(","); for (String p : parArr) { parList.add(p); } } List<StarParam> doubleList = new ArrayList<StarParam>(); // ???? List<StarParam> firstList = new ArrayList<StarParam>(); for (StarParam starParam : starParamList) { if (!parList.contains(starParam.getParameterType())) { String key = starParam.getProductName() + starParam.getParameterType(); List<ConstraintDto> listSingle = new ArrayList<ConstraintDto>(); listSingle.add(this.StarParamToConstraintDto(starParam)); constraintsMap.put(key, listSingle); firstList.add(starParam); } else { doubleList.add(starParam); } } // ? A?B List<String> productType = new ArrayList<String>(); // for (StarParam starParam : doubleList) { for (StarParam starParam : starParamList) { if (!productType.contains(starParam.getProductName())) { productType.add(starParam.getProductName()); } } // ??,?? A,? B,? for (String product : productType) { List<ConstraintDto> productlist = new ArrayList<ConstraintDto>(); for (StarParam starParam : doubleList) { if (product.equals(starParam.getProductName())) { productlist.add(this.StarParamToConstraintDto(starParam)); } } // if(doubleList.size() > 0) constraintsMap.put(product + paramStr, productlist); } // ? ???? List<String> parameterType = new ArrayList<String>(); for (StarParam starParam : starParamList) { if (!parameterType.contains(starParam.getParameterType())) { parameterType.add(starParam.getParameterType()); } } // ???? ??? for (String string : parameterType) { List<ConstraintDto> parameterTypelist = new ArrayList<ConstraintDto>(); for (StarParam starParam : starParamList) { if (string.equals(starParam.getParameterType())) { parameterTypelist.add(this.StarParamToConstraintDto(starParam)); } } if (parameterTypelist.size() > 0) constraintsMap.put(string, parameterTypelist); } LineChartDto lineChartDto = null; // ? lineChartDto = jfreechartServcie.createLineChart(seriesId, starId, partsType, beginDate, endDate, constraintsMap); Map<String, Double> minMap = lineChartDto.getMinMap();// ??Map Map<String, Double> maxMap = lineChartDto.getMaxMap();// ?Map Map<String, String> chartMap = lineChartDto.getChartMap();// Map // ??list List<ParamDto> params = new ArrayList<ParamDto>(); Double paramNumMax = null; Double paramNumMin = null; for (StarParam starParam : starParamList) { ParamDto param = new ParamDto(); param.setParamName(starParam.getParamName()); param.setProductName(starParam.getProductName()); if (maxMap != null && maxMap.size() != 0) { paramNumMax = maxMap.get(starParam.getParamCode()); paramNumMin = minMap.get(starParam.getParamCode()); } if (paramNumMax != null) param.setParamNumMax(String.valueOf(paramNumMax)); else param.setParamNumMax("null"); if (paramNumMin != null) param.setParamNumMin(String.valueOf(paramNumMin)); else param.setParamNumMin("null"); params.add(param); } //?? Set<String> paramNameSet = new HashSet<String>(); // ?? list List<ParamImgDataDto> threeParamImgList = new ArrayList<ParamImgDataDto>(); String chartPathThree = OptionConfig.getWebPath() + "\\report\\wordtemplate\\NoData.png"; for (String parName : parameterType) { ParamImgDataDto paramImgData = new ParamImgDataDto(); paramImgData.setParName( J9Series_Star_ParameterType.getJ9SeriesStarParameterType(partsType).getName() + ":" + parName); if (chartMap != null && chartMap.size() != 0) { chartPathThree = chartMap.get(parName); } paramImgData.setParImg(chartPathThree); if (!paramNameSet.contains(paramImgData.getParName())) { threeParamImgList.add(paramImgData); paramNameSet.add(paramImgData.getParName()); } } if (threeParamImgList.size() == 0) { ParamImgDataDto paramImgData = new ParamImgDataDto(); paramImgData.setParName(""); paramImgData.setParImg(""); threeParamImgList.add(paramImgData); } // ???? list List<ParamImgDataDto> twoParamImgList = new ArrayList<ParamImgDataDto>(); String chartPathTwo = OptionConfig.getWebPath() + "\\report\\wordtemplate\\NoData.png"; for (String product : productType) { ParamImgDataDto paramImgData = new ParamImgDataDto(); if (StringUtils.isNotBlank(product) && StringUtils.isNotBlank(paramStr)) { paramImgData .setParName(J9Series_Star_ParameterType.getJ9SeriesStarParameterType(partsType).getName() + ":" + product + paramStr); if (chartMap != null && chartMap.size() != 0) { chartPathTwo = chartMap.get(product + paramStr); } paramImgData.setParImg(chartPathTwo); if (!paramNameSet.contains(paramImgData.getParName())) { twoParamImgList.add(paramImgData); paramNameSet.add(paramImgData.getParName()); } } } if (twoParamImgList.size() == 0) { ParamImgDataDto paramImgData = new ParamImgDataDto(); paramImgData.setParName(""); paramImgData.setParImg(""); twoParamImgList.add(paramImgData); } // ????? list List<ParamImgDataDto> oneParamImgList = new ArrayList<ParamImgDataDto>(); String chartPathOne = OptionConfig.getWebPath() + "\\report\\wordtemplate\\NoData.png"; for (StarParam starParam : firstList) { ParamImgDataDto paramImgData = new ParamImgDataDto(); paramImgData.setParName(J9Series_Star_ParameterType.getJ9SeriesStarParameterType(partsType).getName() + ":" + starParam.getProductName() + starParam.getParameterType()); if (chartMap != null && chartMap.size() != 0) { chartPathOne = chartMap.get(starParam.getProductName() + starParam.getParameterType()); } paramImgData.setParImg(chartPathOne); if (!paramNameSet.contains(paramImgData.getParName())) { oneParamImgList.add(paramImgData); paramNameSet.add(paramImgData.getParName()); } } if (oneParamImgList.size() == 0) { ParamImgDataDto paramImgData = new ParamImgDataDto(); paramImgData.setParName(""); paramImgData.setParImg(""); oneParamImgList.add(paramImgData); } // //?? // if(twoParamImgList.size() > 0 && !twoParamImgList.get(0).getParName().equals("")) // for (ParamImgDataDto paramImgDataDto2 : twoParamImgList) { // for (ParamImgDataDto paramImgDataDto1 : oneParamImgList) { // if(!paramImgDataDto2.getParName().equals("") && paramImgDataDto2.getParName().equals(paramImgDataDto1.getParName())) // twoParamImgList.remove(paramImgDataDto2); // } // } // if(threeParamImgList.size() > 0 && !threeParamImgList.get(0).getParName().equals("")) // for (ParamImgDataDto paramImgDataDto3 : threeParamImgList) { // for (ParamImgDataDto paramImgDataDto1 : oneParamImgList) { // if(!paramImgDataDto3.getParName().equals("") && paramImgDataDto3.getParName().equals(paramImgDataDto1.getParName())) // threeParamImgList.remove(paramImgDataDto3); // } // for (ParamImgDataDto paramImgDataDto2 : twoParamImgList) { // if(!paramImgDataDto3.getParName().equals("") && paramImgDataDto3.getParName().equals(paramImgDataDto2.getParName())) // threeParamImgList.remove(paramImgDataDto3); // } // } // ??list List<ProductDto> products = new ArrayList<ProductDto>(); for (String product : productType) { int proMovableNum = 0; // for (StarParam starParam : starParamList) { // int movableNum = getJobNumByParamCode(seriesId, starId, partsType, beginDate, endDate, // starParam.getParamCode()); // if (product.equals(starParam.getProductName())) { // proMovableNum += movableNum; // } // } proMovableNum = this.getJobNumByDeviceName(seriesId, starId, partsType, beginDate, endDate, product); ProductDto productDto = new ProductDto(); productDto.setProductName(product); productDto.setMovableNum(proMovableNum); products.add(productDto); } data.setParams(params); data.setProducts(products); data.setOneParamImg(oneParamImgList); data.setTwoParamImg(twoParamImgList); data.setThreeParamImg(threeParamImgList); reportDoc(filename, data, templateUrl, docPath); } private void createTopReport(Date beginDate, Date endDate, String filename, String templateUrl, String docPath, String seriesId, String starId, String partsType) throws Exception { DataToDocDto data = new DataToDocDto(); data.setSeries(seriesId); data.setStar(starId); data.setBeginDate(DateUtil.format(beginDate, "yyyy-MM-dd")); data.setEndDate(DateUtil.format(endDate, "yyyy-MM-dd")); data.setCreateDate(DateUtil.getNowTime("yyyy-MM-dd")); List<StarParam> starParamList = new ArrayList<StarParam>(); List<StarParam> starParams = starParamService.getStarParamForReport(seriesId, starId, partsType); for (StarParam starParam : starParams) { if (starParam.getProductName().equals("null")) starParam.setProductName(""); if (starParam.getParameterType().equals("null")) starParam.setParameterType(starParam.getParamName()); ; starParamList.add(starParam); } if (starParamList == null || starParamList.size() < 1) { String templateNullUrl = OptionConfig.getWebPath() + "\\report\\wordtemplate\\nullData.doc"; reportNullDoc(filename, templateNullUrl, docPath, data.getBeginDate(), data.getEndDate(), "?????"); return; } Map<String, List<ConstraintDto>> constraintsMap = new HashMap<String, List<ConstraintDto>>(); List<String> parList = new ArrayList<String>(); String paramStr = OptionConfig.getParamStr(partsType); if (StringUtils.isNotBlank(paramStr)) { String[] parArr = paramStr.split(","); for (String p : parArr) { parList.add(p); } } // ???? L,,, //List<StarParam> firstList = new ArrayList<StarParam>(); for (StarParam starParam : starParamList) { String key = starParam.getParameterType(); List<ConstraintDto> list = constraintsMap.get(key); if (list == null) { list = new ArrayList<ConstraintDto>(); } list.add(this.StarParamToConstraintDto(starParam)); constraintsMap.put(key, list); //firstList.add(starParam); } Map<String, String> code_productNameMap = new HashMap<String, String>(); // ? List<String> productNameList = new ArrayList<String>(); // for (StarParam starParam : doubleList) { for (StarParam starParam : starParamList) { code_productNameMap.put(starParam.getParamCode(), starParam.getProductName()); if (!productNameList.contains(starParam.getProductName())) { productNameList.add(starParam.getProductName()); } } // List<StarParam> doubleList = new ArrayList<StarParam>(); // // ??,?? A,? B,? // for (String product : productType) { // List<ConstraintDto> productlist = new ArrayList<ConstraintDto>(); // for (StarParam starParam : doubleList) { // if (product.equals(starParam.getProductName())) { // productlist.add(this.StarParamToConstraintDto(starParam)); // } // } // if(doubleList.size() > 0) // constraintsMap.put(product + paramStr, productlist); // } // ? ???? List<String> parameterType = new ArrayList<String>(); for (String key : constraintsMap.keySet()) { List<ConstraintDto> list = constraintsMap.get(key); if (list == null || list.size() == 0) { constraintsMap.remove(key); } else { parameterType.add(key); } } LineChartDto lineChartDto = null; // ? lineChartDto = jfreechartServcie.createLineChart(seriesId, starId, partsType, beginDate, endDate, constraintsMap); Map<String, Double> minMap = lineChartDto.getMinMap();// ??Map Map<String, Double> maxMap = lineChartDto.getMaxMap();// ?Map Map<String, String> chartMap = lineChartDto.getChartMap();// Map // ??list List<ParamDto> params = new ArrayList<ParamDto>(); Double paramNumMax = null; Double paramNumMin = null; // for (StarParam starParam : starParamList) { // ParamDto param = new ParamDto(); // param.setParamName(starParam.getParamName()); // param.setProductName(starParam.getProductName()); // if (maxMap != null && maxMap.size() != 0) { // paramNumMax = maxMap.get(starParam.getParamCode()); // paramNumMin = minMap.get(starParam.getParamCode()); // } // if(paramNumMax != null) // param.setParamNumMax(String.valueOf(paramNumMax)); // else // param.setParamNumMax("null"); // if(paramNumMin != null) // param.setParamNumMin(String.valueOf(paramNumMin)); // else // param.setParamNumMin("null"); // params.add(param); // } for (String key : constraintsMap.keySet()) { List<ConstraintDto> list = constraintsMap.get(key); if (list != null && list.size() > 0) { for (ConstraintDto constraintDto : list) { ParamDto param = new ParamDto(); param.setParamName(constraintDto.getParamName()); param.setProductName(code_productNameMap.get(constraintDto.getParamCode())); if (maxMap != null && maxMap.size() != 0) { paramNumMax = maxMap.get(constraintDto.getParamCode()); paramNumMin = minMap.get(constraintDto.getParamCode()); } if (paramNumMax != null) param.setParamNumMax(String.valueOf(paramNumMax)); else param.setParamNumMax("null"); if (paramNumMin != null) param.setParamNumMin(String.valueOf(paramNumMin)); else param.setParamNumMin("null"); params.add(param); } } } //?? Set<String> paramNameSet = new HashSet<String>(); // ?? list List<ParamImgDataDto> threeParamImgList = new ArrayList<ParamImgDataDto>(); String chartPathThree = OptionConfig.getWebPath() + "\\report\\wordtemplate\\NoData.png"; for (String parName : parameterType) { ParamImgDataDto paramImgData = new ParamImgDataDto(); paramImgData.setParName( J9Series_Star_ParameterType.getJ9SeriesStarParameterType(partsType).getName() + ":" + parName); if (chartMap != null && chartMap.size() != 0) { chartPathThree = chartMap.get(parName); } paramImgData.setParImg(chartPathThree); if (!paramNameSet.contains(paramImgData.getParName())) { threeParamImgList.add(paramImgData); paramNameSet.add(paramImgData.getParName()); } } if (threeParamImgList.size() == 0) { ParamImgDataDto paramImgData = new ParamImgDataDto(); paramImgData.setParName(""); paramImgData.setParImg(""); threeParamImgList.add(paramImgData); } // ???? list List<ParamImgDataDto> twoParamImgList = new ArrayList<ParamImgDataDto>(); String chartPathTwo = OptionConfig.getWebPath() + "\\report\\wordtemplate\\NoData.png"; for (String product : productNameList) { ParamImgDataDto paramImgData = new ParamImgDataDto(); if (StringUtils.isNotBlank(product) && StringUtils.isNotBlank(paramStr)) { paramImgData .setParName(J9Series_Star_ParameterType.getJ9SeriesStarParameterType(partsType).getName() + ":" + product + paramStr); if (chartMap != null && chartMap.size() != 0) { chartPathTwo = chartMap.get(product + paramStr); } paramImgData.setParImg(chartPathTwo); if (!paramNameSet.contains(paramImgData.getParName())) { twoParamImgList.add(paramImgData); paramNameSet.add(paramImgData.getParName()); } } } if (twoParamImgList.size() == 0) { ParamImgDataDto paramImgData = new ParamImgDataDto(); paramImgData.setParName(""); paramImgData.setParImg(""); twoParamImgList.add(paramImgData); } // ????? list List<ParamImgDataDto> oneParamImgList = new ArrayList<ParamImgDataDto>(); String chartPathOne = OptionConfig.getWebPath() + "\\report\\wordtemplate\\NoData.png"; // for (StarParam starParam : firstList) { // ParamImgDataDto paramImgData = new ParamImgDataDto(); // paramImgData.setParName(J9Series_Star_ParameterType.getJ9SeriesStarParameterType(partsType).getName() + ":" + starParam.getProductName() + starParam.getParameterType()); // if (chartMap != null && chartMap.size() != 0) { // chartPathOne = chartMap.get(starParam.getProductName() + starParam.getParameterType()); // } // paramImgData.setParImg(chartPathOne); // if(!paramNameSet.contains(paramImgData.getParName())){ // oneParamImgList.add(paramImgData); // paramNameSet.add(paramImgData.getParName()); // } // } if (oneParamImgList.size() == 0) { ParamImgDataDto paramImgData = new ParamImgDataDto(); paramImgData.setParName(""); paramImgData.setParImg(""); oneParamImgList.add(paramImgData); } // //?? // if(twoParamImgList.size() > 0 && !twoParamImgList.get(0).getParName().equals("")) // for (ParamImgDataDto paramImgDataDto2 : twoParamImgList) { // for (ParamImgDataDto paramImgDataDto1 : oneParamImgList) { // if(!paramImgDataDto2.getParName().equals("") && paramImgDataDto2.getParName().equals(paramImgDataDto1.getParName())) // twoParamImgList.remove(paramImgDataDto2); // } // } // if(threeParamImgList.size() > 0 && !threeParamImgList.get(0).getParName().equals("")) // for (ParamImgDataDto paramImgDataDto3 : threeParamImgList) { // for (ParamImgDataDto paramImgDataDto1 : oneParamImgList) { // if(!paramImgDataDto3.getParName().equals("") && paramImgDataDto3.getParName().equals(paramImgDataDto1.getParName())) // threeParamImgList.remove(paramImgDataDto3); // } // for (ParamImgDataDto paramImgDataDto2 : twoParamImgList) { // if(!paramImgDataDto3.getParName().equals("") && paramImgDataDto3.getParName().equals(paramImgDataDto2.getParName())) // threeParamImgList.remove(paramImgDataDto3); // } // } // ??list List<ProductDto> products = new ArrayList<ProductDto>(); for (String product : productNameList) { int proMovableNum = 0; // for (StarParam starParam : starParamList) { // int movableNum = getJobNumByParamCode(seriesId, starId, partsType, beginDate, endDate, // starParam.getParamCode()); // if (product.equals(starParam.getProductName())) { // proMovableNum += movableNum; // } // } proMovableNum = this.getJobNumByDeviceName(seriesId, starId, partsType, beginDate, endDate, product); ProductDto productDto = new ProductDto(); productDto.setProductName(product); productDto.setMovableNum(proMovableNum); products.add(productDto); } data.setParams(params); data.setProducts(products); data.setOneParamImg(oneParamImgList); data.setTwoParamImg(twoParamImgList); data.setThreeParamImg(threeParamImgList); reportDoc(filename, data, templateUrl, docPath); } }