Java tutorial
package com.icebreak.p2p.front.controller.trade.download; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.icebreak.p2p.dataobject.*; import com.icebreak.p2p.division.DivisionService; import com.icebreak.p2p.division.DivisionTemplateYrdService; import com.icebreak.p2p.loandemand.LoanDemandManager; import com.icebreak.p2p.trade.TradeService; import com.icebreak.p2p.user.PersonalInfoManager; import com.icebreak.p2p.user.UserBaseInfoManager; import com.icebreak.p2p.util.AppConstantsUtil; import com.icebreak.p2p.util.CommonUtil; import com.icebreak.p2p.util.DateUtil; import com.icebreak.p2p.util.MoneyUtil; import com.icebreak.p2p.util.StringUtil; import com.icebreak.p2p.ws.enums.DivisionPhaseEnum; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Service public class InvestReceiptPDFCreator { @Autowired protected TradeService tradeService; @Autowired protected LoanDemandManager loanDemandManager; @Autowired protected DivisionService divisionService; @Autowired protected DivisionTemplateYrdService divisionTemplateLoanService; @Autowired protected PersonalInfoManager personalInfoManager; @Autowired protected UserBaseInfoManager userBaseInfoManager; protected final Logger logger = LoggerFactory.getLogger(this.getClass()); private String receiptFilePath; /** * ??? * @param tradeId * @param detailId * @param servletPath * @return ?url * @throws Exception */ public String creatFile4Receipt(long tradeId, long detailId, String servletPath) throws Exception { creatFileData4Receipt(tradeId, detailId, servletPath); return this.receiptFilePath; } /** * ??? * @param tradeId * @param detailId * @param servletPath * @return ? byte[] * @throws Exception */ public byte[] creatFileData4Receipt(long tradeId, long detailId, String servletPath) throws Exception { FileInputStream fis = null; BufferedInputStream buff = null; String fileKey = tradeId + "_" + detailId;//System.currentTimeMillis() + ""; String filePath = servletPath + "/resources/pdf/investReceipt_" + fileKey + ".pdf"; this.receiptFilePath = filePath; File file = new File(filePath); if (!file.exists()) { String timeLimit = ""; String interestRate = ""; String guaranteeName = ""; String investFlowCode = null; String investor = ""; String investorReal = ""; String investorCertNo = ""; String loanner = ""; String loannerReal = ""; String loannerCertNo = ""; String investAmount = ""; String totalAmountStr = ""; String effectiveDate = ""; String expireDate = ""; Trade trade = tradeService.getByTradeId(tradeId); effectiveDate = DateUtil.simpleFormat(trade.getEffectiveDateTime()); expireDate = DateUtil.simpleFormat(trade.getExpireDateTime()); LoanDemandDO loanDemand = loanDemandManager.queryLoanDemandByDemandId(trade.getDemandId()); guaranteeName = loanDemand.getGuaranteeName(); if ("W".equals(loanDemand.getTimeLimitUnit()) || "M".equals(loanDemand.getTimeLimitUnit())) { timeLimit = loanDemand.getTimeLimit() + ""; } else if ("Y".equals(loanDemand.getTimeLimitUnit())) { timeLimit = loanDemand.getTimeLimit() + ""; } else { timeLimit = loanDemand.getTimeLimit() + ""; } interestRate = CommonUtil.mul(loanDemand.getInterestRate(), 100) + "%"; List<UserInvestEntry> userInvests = tradeService.getEntriesByTradeIdAndDetailId(tradeId, detailId); long totalAmount = 0; if (userInvests != null && userInvests.size() > 0) { UserInvestEntry tradeItem = userInvests.get(0); investAmount = MoneyUtil.getFormatAmount(tradeItem.getAmount()); long investorId = userInvests.get(0).getInvestorId(); long loannerId = userInvests.get(0).getLoanerId(); investorCertNo = getCertNoByUserId(investorId); loannerCertNo = getCertNoByUserId(loannerId); investor = userInvests.get(0).getInvestorUserName(); investorReal = userInvests.get(0).getInvestorRealName(); loannerReal = userInvests.get(0).getLoanerRealName(); loanner = userInvests.get(0).getLoanerUserName(); totalAmount = userInvests.get(0).getAmount(); } //? /*interest = caculateInterest(new Money(totalAmount), loanDemand.getInterestRate(), loanDemand.getTimeLimit(), loanDemand.getTimeLimitUnit());*/ long divisionAmount = 0; long profitAmount = 0; List<TradeDetail> details = tradeService.getInvestProfitTrade(detailId); if (details != null && details.size() > 0) { for (TradeDetail detail : details) { divisionAmount += detail.getAmount(); if (detail.getProfitType() > 0) { profitAmount += detail.getAmount(); } } } totalAmount += divisionAmount; TradeFlowCode tradeFlow = tradeService.queryInvestFlowCodesByTradeDetailId(detailId); if (tradeFlow != null) { investFlowCode = tradeFlow.getTradeFlowCode(); } String guaranteeLicenseNo = ""; Map<String, Object> cond = new HashMap<String, Object>(); cond.put("roleId", 8L); cond.put("tradeId", trade.getId()); List<TradeQueryDetail> det = loanDemandManager.getTradeDetailByConditions(cond); if (det != null && det.size() > 0) { tradeFlow = tradeService.queryInvestFlowCodesByTradeDetailId(det.get(0).getId()); if (tradeFlow != null) { guaranteeLicenseNo = tradeFlow.getTradeFlowCode(); } } LoanDemandDO demand = loanDemandManager.queryLoanDemandByDemandId(trade.getDemandId()); long divisionTemplateId = demand.getDivisionTemplateId(); DivisionTemplateLoanDO divisionTemplateLoan = divisionTemplateLoanService .getByBaseId(divisionTemplateId); List<DivsionRuleRole> investRolelist = divisionService .getRuleRole(String.valueOf(divisionTemplateLoan.getInvestTemplateId())); List<DivsionRuleRole> repayRolelist = divisionService .getRuleRole(String.valueOf(divisionTemplateLoan.getRepayTemplateId())); //?? double totalAnnualInterest = 0; investRolelist.addAll(repayRolelist); if (investRolelist != null && investRolelist.size() > 0) { for (DivsionRuleRole druleRole : investRolelist) { if (DivisionPhaseEnum.INVESET_PHASE.code().equals(druleRole.getPhase())) { if ("11".equals(String.valueOf(druleRole.getRoleId()))) { totalAnnualInterest += druleRole.getRule(); } } } } totalAmountStr = MoneyUtil.getFormatAmount(totalAmount); String divisionAmountStr = MoneyUtil.getFormatAmount(divisionAmount); FileOutputStream fos = null; Document doc = new Document(PageSize.A4, 20, 20, 140, 20); try { fos = new FileOutputStream(filePath); PdfWriter writer = PdfWriter.getInstance(doc, fos); doc.open(); // BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font titleChinese = new Font(bfChinese, 20, Font.BOLD); // ? Paragraph title = new Paragraph(AppConstantsUtil.getProductName() + "?", titleChinese);// title.setAlignment(Element.ALIGN_CENTER); // title.setLeading(1f);//?//? doc.add(title); Font fontZH = new Font(bfChinese, 12, Font.NORMAL); float[] widths = { 20f, 30f, 25f, 25f }; PdfPTable table = new PdfPTable(widths); table.setSpacingBefore(20f);// ? table.setTotalWidth(500);// table.setWidthPercentage(100);//%100 // table.getDefaultCell().setBorder(0);// PdfPCell cell; // cell = new PdfPCell(new Paragraph("?",fontZH)); // cell.setColspan(4); // table.addCell(cell); cell = new PdfPCell(new Paragraph("??", fontZH)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); table.addCell(cell); cell = new PdfPCell(new Paragraph(AppConstantsUtil.getPlatformName(), fontZH)); table.addCell(cell); cell = new PdfPCell(new Paragraph("?", fontZH)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); table.addCell(cell); cell = new PdfPCell(new Paragraph(loanDemand.getRepayDivisionWayMsg(), fontZH)); table.addCell(cell); cell = new PdfPCell(new Paragraph("", fontZH)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); table.addCell(cell); cell = new PdfPCell(new Paragraph(interestRate, fontZH)); table.addCell(cell); cell = new PdfPCell(new Paragraph("?", fontZH)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); table.addCell(cell); cell = new PdfPCell(new Paragraph(timeLimit, fontZH)); table.addCell(cell); cell = new PdfPCell(new Paragraph("??", fontZH)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); table.addCell(cell); cell = new PdfPCell(new Paragraph(guaranteeName, fontZH)); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Paragraph("??", fontZH)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); table.addCell(cell); cell = new PdfPCell( new Paragraph(StringUtil.nullToEmpty(loanDemand.getGuaranteeLicenseNo()), fontZH)); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Paragraph("", fontZH)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); table.addCell(cell); cell = new PdfPCell(new Paragraph((loanDemand.getLoanAmount() / 100) + "", fontZH)); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Paragraph("", fontZH)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); table.addCell(cell); cell = new PdfPCell(new Paragraph(loanDemand.getLoanPurpose(), fontZH)); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Paragraph("??", fontZH)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); table.addCell(cell); cell = new PdfPCell(new Paragraph(investFlowCode, fontZH)); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Paragraph("?", fontZH)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); cell.setColspan(2); table.addCell(cell); cell = new PdfPCell(new Paragraph("??", fontZH)); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); cell.setColspan(2); table.addCell(cell); Paragraph iparas = new Paragraph("??" + investor, fontZH); iparas.add(Chunk.NEWLINE); iparas.add("??" + investorReal); iparas.add(Chunk.NEWLINE); iparas.add("??" + StringUtil.subString(investorCertNo, 7, "****")); iparas.add(Chunk.NEWLINE); iparas.add("?" + effectiveDate); iparas.add(Chunk.NEWLINE); iparas.add("" + expireDate); iparas.add(Chunk.NEWLINE); iparas.add("()" + investAmount); iparas.add(Chunk.NEWLINE); iparas.add("()" + divisionAmountStr); iparas.add(Chunk.NEWLINE); iparas.add("()" + totalAmountStr); cell = new PdfPCell(iparas); cell.setColspan(2); cell.setRowspan(8); cell.setMinimumHeight(120); table.addCell(cell); Paragraph paras = new Paragraph("??" + loanner, fontZH); paras.add(Chunk.NEWLINE); paras.add("??" + loannerReal); paras.add(Chunk.NEWLINE); paras.add("??" + StringUtil.subString(loannerCertNo, 7, "****")); paras.add(Chunk.NEWLINE); paras.add("?" + effectiveDate); paras.add(Chunk.NEWLINE); paras.add("" + expireDate); paras.add(Chunk.NEWLINE); paras.add(Chunk.NEWLINE); paras.add(Chunk.NEWLINE); paras.add("()" + totalAmountStr); cell = new PdfPCell(paras); cell.setColspan(2); cell.setRowspan(8); table.addCell(cell); doc.add(table); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); Paragraph tips = new Paragraph(" ?? ?" + AppConstantsUtil.getPlatformName() + "", fontZH);// tips.setLeading(1f);//?//? doc.add(tips); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); tips = new Paragraph(" ??" + AppConstantsUtil.getPlatformAddress(), fontZH);// tips.setLeading(1f);//?//? doc.add(tips); // XMLWorkerHelper.getInstance().parseXHtml(writer, doc, // new ByteArrayInputStream(str.getBytes())); doc.close(); logger.info("?"); } catch (Exception e) { logger.error("?", e); throw new Exception("?:" + e.getMessage()); } finally { if (fos != null) { fos.close(); } } } byte[] data = new byte[1024]; file = new File(filePath); try { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] temp = new byte[1024]; int size = 0; fis = new FileInputStream(file); buff = new BufferedInputStream(fis); int i = 0; while ((size = buff.read(temp)) != -1) { out.write(temp, 0, size); i += 1; } data = out.toByteArray(); buff.close(); fis.close(); //file.delete(); if (i == 0 && size == -1) { //PDF file.delete(); } } catch (FileNotFoundException e) { logger.error("?", e); } catch (IOException e) { logger.error("delete file", e); } finally { if (fis != null) { fis.close(); } } return data; } private String getCertNoByUserId(long userId) { String certNo = null; UserBaseInfoDO user = userBaseInfoManager.queryByUserId(userId); try { PersonalInfoDO person = personalInfoManager.query(user.getUserBaseId()); certNo = person.getCertNo(); } catch (Exception e) { logger.error("getCertNoByUserId is error", e); } return certNo; } }