Air
2024-11-04 a1f06d31b7b4cac569c34bdfbb68de77f2858ffe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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<ProtocolInfo> 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<List<ProtocolInfo>> getPlatformProtocol() {
        List<ProtocolInfo> 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<Customer> 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<PlatformCapital> 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 = "<html>\n" +
                "<head>\n" +
                "<meta charset=\"UTF-8\">\n" +
                "<title>个人信息共享授权协议</title>\n" +
                "</head>\n" +
                "<body>\n" +
                content + // 添加解析后的内容
                "\n</body>\n" +
                "</html>";
 
        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<CustomerZcy> 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<PlatformCapital> 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 = "<html>\n" +
                "<head>\n" +
                "<meta charset=\"UTF-8\">\n" +
                "<title>个人信息共享授权协议</title>\n" +
                "</head>\n" +
                "<body>\n" +
                content + // 添加解析后的内容
                "\n</body>\n" +
                "</html>";
 
        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 + "";
    }
}