package com.nova.sankuai.controller.v2; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.nova.sankuai.domain.entity.Customer; import com.nova.sankuai.domain.entity.CustomerZcy; import com.nova.sankuai.domain.entity.PlatformCapital; import com.nova.sankuai.domain.entity.ProtocolInfo; import com.nova.sankuai.infra.config.CommonException; import com.nova.sankuai.infra.constants.ResultCode; import com.nova.sankuai.infra.utils.ResultJson; import com.nova.sankuai.service.ICustomerService; import com.nova.sankuai.service.ICustomerZcyService; import com.nova.sankuai.service.IPlatformCapitalService; import com.nova.sankuai.service.IProtocolInfoService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.List; import java.util.Random; /** * @author weikangdi * @create 2021/10/29 */ @Api(tags = "协议管理V2") @RestController @RequestMapping(value = "/api/v2/protocolInfo") @AllArgsConstructor @Slf4j public class ProtocolInfoV2Controller { private final IProtocolInfoService protocolInfoService; private final IPlatformCapitalService platformCapitalService; private final ICustomerService customerService; private final ICustomerZcyService customerZcyService; @GetMapping(value = "/getProtocol") @ApiOperation(value = "获取协议") public ResultJson getProtocol(String code) { ProtocolInfo protocolInfo = protocolInfoService.lambdaQuery().eq(ProtocolInfo::getCode, code).one(); if (protocolInfo == null) { return ResultJson.failure(ResultCode.SERVER_ERROR, "没有找到对应的协议"); } return ResultJson.ok(protocolInfo); } @GetMapping(value = "/getPlatformProtocol") @ApiOperation(value = "获取平台服务协议") public ResultJson> getPlatformProtocol() { List list = protocolInfoService.lambdaQuery().eq(ProtocolInfo::getAlias, "平台协议").list(); if (list == null || list.size() == 0) { return ResultJson.failure(ResultCode.SERVER_ERROR, "没有找到对应的协议"); } return ResultJson.ok(list); } // @GetMapping(value = "/getProtocolInfo.html") @GetMapping(value = "/getProtocolInfo/{code}/{id}.html") @ApiOperation(value = "获取协议2") // public void getProtocolInfo(HttpServletResponse response, Long id, String code) { public void getProtocolInfo(HttpServletResponse response, @PathVariable("id") Long id, @PathVariable("code") String code) { PlatformCapital platformCapital = platformCapitalService.lambdaQuery().eq(PlatformCapital::getId, id).one(); if (platformCapital == null) { throw new CommonException("没有找到对应的对象信息"); } LambdaQueryWrapper wr = new LambdaQueryWrapper<>(); wr.eq(Customer::getId, platformCapital.getCustomerId()); wr.last(" limit 1 "); Customer customer = customerService.getOne(wr); if (customer == null) { throw new CommonException("没有找到对应的用户信息"); } ProtocolInfo protocolInfo = protocolInfoService.lambdaQuery().eq(ProtocolInfo::getCode, code).one(); if (protocolInfo == null) { throw new CommonException("没有找到对应的协议"); } List platformCapitalList = platformCapitalService.lambdaQuery().eq(PlatformCapital::getCustomerId, platformCapital.getCustomerId()).list(); if (platformCapitalList == null) { throw new CommonException("没有找到对应的对象信息"); } String content = protocolInfo.getContent(); content = StringUtils.replace(content, "AAAAAA", customer.getName()); content = StringUtils.replace(content, "BBBBBB", customer.getIdCard()); String names = ""; // String companys = ""; for(PlatformCapital vo : platformCapitalList) { // ProductNameVo productNameVo = new ProductNameVo(); // productNameVo.setProductName(vo.getProductName()); // CompanyNameVo companyNameVo = new CompanyNameVo(); // companyNameVo.setCompanyName(vo.getCompanyName()); // // productNameList.add(productNameVo); // compayNameList.add(companyNameVo); String productName = vo.getProductName() == null ? "" : vo.getProductName(); names += productName + ";"; String companyName = vo.getCompanyName() == null ? "" : vo.getCompanyName(); // companys += companyName + ";"; } if (names.length() > 0) { names = names.substring(0, names.length() - 1); } // if (companys.length() > 0) { // companys = companys.substring(0, companys.length() - 1); // } content = StringUtils.replace(content, "CCCCCC", names); content = StringUtils.replace(content, "DDDDDD", "杭州紫荆花互动广告有限公司"); content = StringUtils.replace(content, "GGGGGG", ""); content = StringUtils.replace(content, "EEEEEE", customer.getPhone()); content = StringUtils.replace(content, "FFFFFF", generateSixDigitNumber()); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); content = StringUtils.replace(content, "HHHHHH", dateFormat.format(System.currentTimeMillis())); // 生成HTML内容 String htmlContent = "\n" + "\n" + "\n" + "个人信息共享授权协议\n" + "\n" + "\n" + content + // 添加解析后的内容 "\n\n" + ""; try { // 创建PDF // PdfRendererBuilder builder = new PdfRendererBuilder(); // builder.useFastMode(); // builder.withHtmlContent(contentntent, null); // BufferedWriter writer = new BufferedWriter(new FileWriter("output.html")); // writer.write(htmlContent); // 写入HTML内容 // writer.close(); // 关闭writer response.setContentType("text/html;charset=UTF-8"); // 设置HTTP响应的内容类型为PDF // response.setHeader("Content-Disposition", "attachment; filename=\"data.html\""); // 设置HTTP响应的头信息,告诉浏览器这是一个附件,建议保存的文件名为"data.pdf" // response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode("个人信息共享授权协议.html", "UTF-8")); // response.setHeader("Content-disposition", "attachment;filename=" + new String(("个人信息共享授权协议.html").getBytes(StandardCharsets.UTF_8), "ISO8859-1")); // ByteArrayInputStream in = new ByteArrayInputStream(htmlContent.getBytes(StandardCharsets.UTF_8)); // org.apache.commons.io.IOUtils.copy(in, response.getOutputStream()); // 上面一行利用Apache Commons IO库的IOUtils类的copy方法, // 将PDF文件的内容(现在存储在ByteArrayInputStream 'in')复制到HTTP响应的输出流中 // 这使得PDF的内容能够被发送到请求该服务的客户端 byte[] isoBytes = htmlContent.getBytes("UTF-8"); String html = new String(isoBytes, "ISO-8859-1"); response.getOutputStream().println(html); response.flushBuffer(); // 刷新响应的缓冲区,完成响应的发送 } catch (Exception ex) { log.error("getProtocolInfo Exception:{}", ex); } // CustomerProtocolInfo customerProtocolInfo = new CustomerProtocolInfo(); // customerProtocolInfo.setName(customer.getName()); // customerProtocolInfo.setPhone(customer.getPhone()); // customerProtocolInfo.setIdCard(customer.getIdCard()); // customerProtocolInfo.setContent(protocolInfo.getContent()); // customerProtocolInfo.setProductNameList(productNameList); // customerProtocolInfo.setCompanyNameList(compayNameList); // // return ResultJson.ok(customerProtocolInfo); }// @GetMapping(value = "/getProtocolInfo.html") @GetMapping(value = "/getProtocolInfoWld/{code}/{id}.html") @ApiOperation(value = "获取协议3(我来贷)") public void getProtocolInfoByWld(HttpServletResponse response, @PathVariable("id") Long id, @PathVariable("code") String code) { PlatformCapital platformCapital = platformCapitalService.lambdaQuery().eq(PlatformCapital::getId, id).one(); if (platformCapital == null) { throw new CommonException("没有找到对应的对象信息"); } LambdaQueryWrapper wr = new LambdaQueryWrapper<>(); wr.eq(CustomerZcy::getId, platformCapital.getCustomerId()); wr.last(" limit 1 "); CustomerZcy customer = customerZcyService.getOne(wr); if (customer == null) { throw new CommonException("没有找到对应的用户信息"); } ProtocolInfo protocolInfo = protocolInfoService.lambdaQuery().eq(ProtocolInfo::getCode, code).one(); if (protocolInfo == null) { throw new CommonException("没有找到对应的协议"); } List platformCapitalList = platformCapitalService.lambdaQuery().eq(PlatformCapital::getCustomerId, platformCapital.getCustomerId()).list(); if (platformCapitalList == null) { throw new CommonException("没有找到对应的对象信息"); } String content = protocolInfo.getContent(); content = StringUtils.replace(content, "AAAAAA", customer.getName()); content = StringUtils.replace(content, "BBBBBB", customer.getIdCard()); StringBuilder names = new StringBuilder(); for(PlatformCapital vo : platformCapitalList) { String productName = vo.getProductName() == null ? "" : vo.getProductName(); names.append(productName).append(";"); } if (names.length() > 0) { names = new StringBuilder(names.substring(0, names.length() - 1)); } content = StringUtils.replace(content, "CCCCCC", names.toString()); content = StringUtils.replace(content, "DDDDDD", "杭州紫荆花互动广告有限公司"); content = StringUtils.replace(content, "GGGGGG", ""); content = StringUtils.replace(content, "EEEEEE", customer.getPhone()); content = StringUtils.replace(content, "FFFFFF", generateSixDigitNumber()); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); content = StringUtils.replace(content, "HHHHHH", dateFormat.format(System.currentTimeMillis())); // 生成HTML内容 String htmlContent = "\n" + "\n" + "\n" + "个人信息共享授权协议\n" + "\n" + "\n" + content + // 添加解析后的内容 "\n\n" + ""; try { response.setContentType("text/html;charset=UTF-8"); // 设置HTTP响应的内容类型为PDF byte[] isoBytes = htmlContent.getBytes(StandardCharsets.UTF_8); String html = new String(isoBytes, StandardCharsets.ISO_8859_1); response.getOutputStream().println(html); response.flushBuffer(); // 刷新响应的缓冲区,完成响应的发送 } catch (Exception ex) { log.error("getProtocolInfo Exception:{}", ex.getMessage()); } } private String generateSixDigitNumber() { Random random = new Random(); int randomNumber = 100000 + random.nextInt(999999); // 生成一个6位数,范围在100000到 999999 之间 return randomNumber + ""; } }