|
@@ -0,0 +1,187 @@
|
|
|
+package com.management.platform.controller;
|
|
|
+
|
|
|
+import com.management.platform.entity.Company;
|
|
|
+import com.management.platform.entity.ContractDetail;
|
|
|
+import com.management.platform.service.CompanyService;
|
|
|
+import com.management.platform.service.ContractDetailService;
|
|
|
+import com.management.platform.util.HttpRespMsg;
|
|
|
+import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
|
|
|
+import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
|
|
|
+import fr.opensagres.xdocreport.core.XDocReportException;
|
|
|
+import fr.opensagres.xdocreport.document.IXDocReport;
|
|
|
+import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
|
|
|
+import fr.opensagres.xdocreport.template.IContext;
|
|
|
+import fr.opensagres.xdocreport.template.TemplateEngineKind;
|
|
|
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.net.URL;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 前端控制器
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author Seyason
|
|
|
+ * @since 2023-02-06
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/contract-detail")
|
|
|
+public class ContractDetailController {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ContractDetailService contractDetailService;
|
|
|
+
|
|
|
+ @Value(value = "${upload.path}")
|
|
|
+ private String path;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private CompanyService companyService;
|
|
|
+
|
|
|
+ @RequestMapping("/detail")
|
|
|
+ public HttpRespMsg detail(Integer companyId){
|
|
|
+ return contractDetailService.detail(companyId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/update")
|
|
|
+ public HttpRespMsg update(ContractDetail contractDetail, HttpServletResponse response) throws IOException, XDocReportException {
|
|
|
+ HttpRespMsg httpRespMsg=new HttpRespMsg();
|
|
|
+ Company company = companyService.getById(contractDetail.getCompanyId());
|
|
|
+ if(contractDetail.getSecondParty().equals("")){
|
|
|
+ contractDetail.setSecondParty(company.getCompanyName());
|
|
|
+ }
|
|
|
+ DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
+ if(contractDetail.getStartDate().equals("")){
|
|
|
+ contractDetail.setStartDate(LocalDate.now().format(df));
|
|
|
+ }
|
|
|
+ if(!contractDetailService.saveOrUpdate(contractDetail)){
|
|
|
+ httpRespMsg.setError("验证失败");
|
|
|
+ }
|
|
|
+ String fileName = contractDetail.getSoftwareName()+"销售合同-"+contractDetail.getSecondParty()+".docx";
|
|
|
+ LocalDate startDate;
|
|
|
+ if(contractDetail.getStartDate()!=null&&!contractDetail.getStartDate().equals("")){
|
|
|
+ startDate=LocalDate.parse(contractDetail.getStartDate(),df);
|
|
|
+ }else {
|
|
|
+ startDate=LocalDate.now();
|
|
|
+ }
|
|
|
+ File file=new File(path+fileName);
|
|
|
+ //如果文件之前存在 并且是打开的情况下 会报错 另一个程序正在使用此文件,进程无法访问
|
|
|
+ if(file.exists()){
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ LocalDate endDate=startDate.plusYears(contractDetail.getServiceDate());
|
|
|
+ //根据模板生成word文件
|
|
|
+ InputStream ins =this.getClass().getResourceAsStream("/模板.docx");
|
|
|
+ /*new FileInputStream(new File("C:\\upload\\工时管家系统销售合同-模板.docx"));*/
|
|
|
+ //注册xdocreport实例并加载FreeMarker模板引擎
|
|
|
+ IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins,
|
|
|
+ TemplateEngineKind.Freemarker);
|
|
|
+ //创建xdocreport上下文对象
|
|
|
+ IContext context = report.createContext();
|
|
|
+ //创建要替换的文本变量
|
|
|
+ context.put("secondParty",contractDetail.getSecondParty());
|
|
|
+ context.put("secondPartyAddr", contractDetail.getSecondPartyAddr());
|
|
|
+ context.put("secondPartyContacts",contractDetail.getSecondPartyContacts() );
|
|
|
+ context.put("secondPartyTel",contractDetail.getSecondPartyTel() );
|
|
|
+ context.put("softwareName", contractDetail.getSoftwareName());
|
|
|
+ context.put("serviceDate", contractDetail.getServiceDate());
|
|
|
+ context.put("startDate",df.format(startDate));
|
|
|
+ context.put("endDate",df.format(endDate));
|
|
|
+ context.put("serviceChargeOfYear", contractDetail.getServiceChargeOfYear()==null?0:contractDetail.getServiceChargeOfYear());
|
|
|
+ context.put("customDevelopmentCosts", contractDetail.getCustomDevelopmentCosts()==null?0:contractDetail.getCustomDevelopmentCosts());
|
|
|
+ context.put("standardUnitPrice", contractDetail.getStandardUnitPrice()==null?0:contractDetail.getStandardUnitPrice());
|
|
|
+ context.put("firstOpenNum", contractDetail.getFirstOpenNum());
|
|
|
+ context.put("secondPartyAccount", contractDetail.getSecondPartyAccount());
|
|
|
+ BigDecimal bigDecimal=new BigDecimal(contractDetail.getServiceChargeOfYear()==null?0:contractDetail.getServiceChargeOfYear());
|
|
|
+ bigDecimal=bigDecimal.add(new BigDecimal(contractDetail.getCustomDevelopmentCosts()==null?0:contractDetail.getCustomDevelopmentCosts()));
|
|
|
+ context.put("allCost", bigDecimal.doubleValue());
|
|
|
+ context.put("secondPartyBankOfDeposit", contractDetail.getSecondPartyBankOfDeposit());
|
|
|
+ context.put("secondPartyBillingName",contractDetail.getSecondPartyBillingName() );
|
|
|
+ context.put("secondPartyTIN",contractDetail.getSecondPartyTIN() );
|
|
|
+ context.put("invoiceType",contractDetail.getInvoiceType());
|
|
|
+ //输出到本地目录
|
|
|
+ FileOutputStream out = new FileOutputStream(path+fileName);
|
|
|
+ report.process(context, out);
|
|
|
+ File newFile = new File(path+fileName.replaceAll("\\.docx","\\.pdf"));//转换之后文件生成的地址
|
|
|
+ if (newFile.exists()) {
|
|
|
+ newFile.delete();
|
|
|
+ }
|
|
|
+ //同时生成word文件转换的pdf文件
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(path+fileName);
|
|
|
+ XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
|
|
|
+ PdfOptions pdfOptions = PdfOptions.create();
|
|
|
+ FileOutputStream fileOutputStream = new FileOutputStream(path+fileName.replaceAll("\\.docx","\\.pdf"));
|
|
|
+ PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
|
|
|
+ fileInputStream.close();
|
|
|
+ fileOutputStream.close();
|
|
|
+ out.close();
|
|
|
+ ins.close();
|
|
|
+ httpRespMsg.data="/upload/"+fileName;
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/preview")
|
|
|
+ public void preview(HttpServletResponse response,Integer companyId) {
|
|
|
+ ContractDetail contractDetail = contractDetailService.getById(companyId);
|
|
|
+ String fileName = contractDetail.getSoftwareName()+"销售合同-"+contractDetail.getSecondParty()+".pdf";
|
|
|
+ String filePath = path+fileName;
|
|
|
+ System.out.println("filePath:" + filePath);
|
|
|
+ File f = new File(filePath);
|
|
|
+ BufferedInputStream br = null;
|
|
|
+ OutputStream out = null;
|
|
|
+ try {
|
|
|
+ br = new BufferedInputStream(new FileInputStream(f));
|
|
|
+ byte[] bs = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ response.reset(); // 非常重要
|
|
|
+ // 在线打开方式
|
|
|
+ URL u = new URL("file:///" + path + filePath);
|
|
|
+ String contentType = u.openConnection().getContentType();
|
|
|
+ response.setContentType(contentType);
|
|
|
+ response.setHeader("Content-Disposition", "inline;filename="
|
|
|
+ + fileName);
|
|
|
+ out = response.getOutputStream();
|
|
|
+ while ((len = br.read(bs)) > 0) {
|
|
|
+ out.write(bs, 0, len);
|
|
|
+ }
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ br.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (out != null) {
|
|
|
+ try {
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/finalize")
|
|
|
+ public HttpRespMsg contractFinalize(Integer companyId){
|
|
|
+ HttpRespMsg httpRespMsg=new HttpRespMsg();
|
|
|
+ ContractDetail contractDetail = contractDetailService.getById(companyId);
|
|
|
+ if(contractDetail!=null){
|
|
|
+ contractDetail.setFinalize(1);
|
|
|
+ }
|
|
|
+ if(!contractDetailService.updateById(contractDetail)){
|
|
|
+ httpRespMsg.setError("验证失败");
|
|
|
+ }
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|