xinyb
2023-11-22 158ac54d6c427f48ae36f4f88e572653e95d9ef3
提交 | 用户 | age
75ba62 1 package com.yc.open.baiwang.controller;
F 2
3 import com.alibaba.fastjson.JSON;
4 import com.baiwang.open.client.BWRestClient;
5 import com.baiwang.open.client.IBWClient;
6 import com.baiwang.open.entity.request.OutputBasicinfoGetAuthQrCodeRequest;
7 import com.baiwang.open.entity.request.OutputFormatQueryQdInvoiceRequest;
8 import com.baiwang.open.entity.request.OutputInvoiceIssueRequest;
9 import com.baiwang.open.entity.request.OutputRedinvoiceAddRequest;
10 import com.baiwang.open.entity.request.node.OutputFormatQueryQdInvoiceData;
11 import com.baiwang.open.entity.response.OutputBasicinfoGetAuthQrCodeResponse;
12 import com.baiwang.open.entity.response.OutputFormatQueryQdInvoiceResponse;
13 import com.baiwang.open.entity.response.OutputInvoiceIssueResponse;
14 import com.baiwang.open.entity.response.OutputRedinvoiceAddResponse;
15 import com.baiwang.open.exception.BWOpenException;
16 import com.yc.entity.DataSourceEntity;
17 import com.yc.entity.attachment.AttachmentEntity;
18 import com.yc.entity.attachment.AttachmentWhereEntity;
19 import com.yc.exception.ApplicationException;
20 import com.yc.exception.CallBackMessage;
21 import com.yc.factory.FactoryBean;
22 import com.yc.multiData.MultiDataSource;
23 import com.yc.multiData.SpObserver;
24 import com.yc.open.baiwang.schedule.BaiwangThread;
25 import com.yc.open.baiwang.schedule.QueryThread;
26 import com.yc.open.baiwang.service.BaiwangIfc;
1df3c0 27 import com.yc.open.baiwang.service.QueryEntiy;
75ba62 28 import com.yc.open.baiwang.utils.TokenUtils;
F 29 import com.yc.open.controller.BaseController;
30 import com.yc.open.jinwu.entity.T110565Entity;
31 import com.yc.open.mutual.schedule.GateEntity;
32 import com.yc.sdk.weixincp.util.UploadFiles;
33 import com.yc.service.build.type.T_22_Ifc;
34 import com.yc.service.upload.AttachmentIfc;
35 import com.yc.utils.SessionKey;
36 import lombok.extern.slf4j.Slf4j;
37 import org.apache.commons.lang3.StringUtils;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
40 import org.springframework.util.FileCopyUtils;
41 import org.springframework.web.bind.annotation.RequestBody;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.ResponseBody;
44 import org.springframework.web.bind.annotation.RestController;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import java.io.IOException;
49 import java.io.InputStream;
50 import java.net.HttpURLConnection;
51 import java.net.URL;
1df3c0 52 import java.util.HashMap;
F 53 import java.util.Map;
54 import java.util.UUID;
75ba62 55
F 56 /**
57  * 百望云相关功能
58  */
59 @RestController
60 @Slf4j
61 public class BaiwangController extends BaseController {
62     @Autowired
63     T_22_Ifc t22ifc;
64     @Autowired
65     AttachmentIfc attachmentIfc;
66
67     @Autowired
68     BaiwangIfc baiwangIfc;
69     @Autowired
70     ThreadPoolTaskExecutor threadPoolExecutor;
71
72     /**
73      * 刷新刷脸认证二维码
74      *
75      * @return
76      */
77     @RequestMapping(value = "/baiwang/refreshQrcode.do")
78     public @ResponseBody Object refreshQrcode(@RequestBody FaceSwipingEntry entry, HttpServletRequest request, HttpServletResponse response) {
79         CallBackMessage callBackMessage = new CallBackMessage();
80         try {
81             IBWClient client = new BWRestClient(TokenUtils.url, TokenUtils.appKey, TokenUtils.appSecret); // 初始化一个客户端
82             String token = TokenUtils.getToken();
1df3c0 83             request.setAttribute("doccode", entry.getDoccode());
F 84             request.setAttribute("funclinkFormid",entry.getFunclinkFormid());
75ba62 85             confrimFace(client, entry.getTaxNo(), token, request, response, callBackMessage);
F 86             callBackMessage.sendSuccessMessage("刷新成功");
87             return callBackMessage.toJSONObject();
88         } catch (BWOpenException e) {
89             callBackMessage.sendErrorMessage(e.getSubMessage());
90             return callBackMessage.toJSONObject();
91         } catch (Exception ex) {
92             callBackMessage.sendErrorMessage(ex.getMessage());
93             return callBackMessage.toJSONObject();
94         }
95     }
96
97     /**
98      * 刷脸认证回调状态
99      *
100      * @return
101      */
102     @RequestMapping(value = "/baiwang/confirmFaceSwiping.do")
103     public @ResponseBody Object confirmFaceSwiping(@RequestBody FaceSwipingEntry entry, HttpServletRequest request, HttpServletResponse response) {
104         CallBackMessage callBackMessage = new CallBackMessage();
105         try {
106             IBWClient client = new BWRestClient(TokenUtils.url, TokenUtils.appKey, TokenUtils.appSecret); // 初始化一个客户端
107             String token = TokenUtils.getToken();
108             //循环执行是否扫码成功,成功则发通知关闭页面并且提示成功
109             threadPoolExecutor.execute(new BaiwangThread(client, token, entry, request.getSession()));
110             callBackMessage.sendSuccessMessage("成功");
111             return  callBackMessage.toJSONObject();
112         } catch (BWOpenException e) {
113             callBackMessage.sendErrorMessage(e.getSubMessage());
114             return  callBackMessage.toJSONObject();
115         } catch (Exception ex) {
116             callBackMessage.sendErrorMessage(ex.getMessage());
117             return  callBackMessage.toJSONObject();
118         }
119     }
120
121     /**
122      * 红冲电子发票
123      *
124      * @return
125      */
126     @RequestMapping(value = "/baiwang/redEinvoice.do")
127     public @ResponseBody Object redEinvoice(HttpServletRequest request, HttpServletResponse response) {
128         CallBackMessage callBackMessage = new CallBackMessage();
129         OutputRedinvoiceAddRequest einvoiceInfo = null;
130         IBWClient client = null;
131         String token = null;
1df3c0 132         String funclinkFormid = null;
F 133         GateEntity entity = new GateEntity();
75ba62 134         try {
F 135             Map<String, String> map = null;
136             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);
137             try {
138                 SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
139                 map = t22ifc.getParamMap(request);
140             } catch (Exception e) {
141                 e.printStackTrace();
142                 this.print(response, this.getErrorMsg(e));
143                  callBackMessage.sendErrorMessage(this.getErrorMsg(e));
144                  return callBackMessage.toJSONObject();
145             } finally {
146                 SpObserver.setDBtoInstance();
147             }
148             String docCode = null;//单号
149
150             if (map != null) {
151                 docCode = map.get("doccode");
152             }
153             if (docCode == null) {
154                 if (log.isDebugEnabled()) {
155                     log.error("通过22类型传进来组装出来的map:" + map.toString());
156                 }
157                 callBackMessage.sendErrorMessage("单号不能为空");
158                 return callBackMessage.toJSONObject();
159             }
1df3c0 160             funclinkFormid=request.getParameter("formid");//功能链接功能号,返回给前端在刷脸成功后自动再次执行使用
75ba62 161
F 162             entity.setFormid(150504);
163             entity.setDbid(dataSourceEntity.getDbId() + "");
164             entity.setDocCode(docCode);
165             entity.setUserName(request.getSession().getAttribute(SessionKey.USER_NAME) + "");
166             einvoiceInfo = this.baiwangIfc.getRedEinvoiceInfo(entity);
167             if (einvoiceInfo != null) {
168                 client = new BWRestClient(TokenUtils.url, TokenUtils.appKey, TokenUtils.appSecret); // 初始化一个客户端
169                 token = TokenUtils.getToken();
170                 OutputRedinvoiceAddResponse issue = client.outputRedinvoice().add(einvoiceInfo, token);
171                 log.info("msg:" + JSON.toJSONString(issue));
172                 if (!issue.isSuccess()) {
173                     //失败
174                     callBackMessage.sendErrorMessage(issue.getErrorResponse().getSubMessage());
175                     return callBackMessage.toJSONObject();
176                 }
177                 //-------成功
178                 //1--记录日志
179                 com.yc.open.jinwu.entity.T110565Entity t110565Entity = new T110565Entity(entity.getFormid(), entity.getDocCode(), null, "接收红冲电子发票", JSON.toJSONString(issue));
180                 this.sendLogBy110565(t110565Entity);
181                 //3---更新状态
182                 baiwangIfc.updateRedStatus(entity, issue);
183                 //--红字发票不需要下载文件
184                // threadPoolExecutor.execute(new QueryThread(client, token,issue.getResponse().get(0).getRedInvoiceNo(), einvoiceInfo.getTaxNo(),entity, request.getSession(),this,baiwangIfc));
185                 callBackMessage.setData("成功");
186                 callBackMessage.setMemo("冲销成功!").sendSuccessMessageByDefault();
187                 return callBackMessage.toJSONObject();
188             }else {
189                 callBackMessage.sendErrorMessage(String.format("%s没有所需要的推送数据", entity.getDocCode()));
190                 return callBackMessage.toJSONObject();
191             }
192         } catch (BWOpenException ex) {
193             ex.printStackTrace();
194             int errorCode = -1;
195             String errorMsg = StringUtils.isNotBlank(ex.getSubMessage())?ex.getSubMessage():ex.getLocalizedMessage();
196             if ("70031".equals(ex.getSubCode())) {
197                 //未登录或登录失效
198                 Map map = new HashMap();
199                 map.put("url", TokenUtils.LOGIN_URL);
200                 errorCode = -1007;
7bdadc 201                 errorMsg = "【" + ex.getSubMessage() + "】点击【确定】按钮在弹出的百望云平台重新登录,成功后关闭弹出窗口再次执行冲销电子发票操作";
75ba62 202                 callBackMessage.setInfo(map);
F 203             } else if ("70045".equals(ex.getSubCode())) {
204                 //刷脸验证,生成二维码图片给税局APP扫码
205                 errorCode = -1008;
1df3c0 206                 request.setAttribute("doccode", entity.getDocCode());
F 207                 request.setAttribute("funclinkFormid",funclinkFormid);
75ba62 208                 confrimFace(client, einvoiceInfo.getTaxNo(), token, request, response, callBackMessage);
F 209             } else if ("70035".equals(ex.getSubCode())) {
210                 //红字录入单操作异常,该发票存在进行中的红字确认单!
211             }else if ("70180".equals(ex.getSubCode())) {
212                 //红字确认单确认即开票失败,稍后可手动发起开具红票请求
213
214             }
215             callBackMessage.sendErrorMessage(errorMsg, errorCode);
216             return callBackMessage.toJSONObject();
217         } catch (Exception e) {
218             e.printStackTrace();
219             callBackMessage.sendErrorMessage(e.getMessage());
220             return callBackMessage.toJSONObject();
221         } finally {
222             SpObserver.setDBtoInstance();
223         }
224     }
225
226     /**
227      * 生成电子发票
228      *
229      * @return
230      */
231     @RequestMapping(value = "/baiwang/einvoice.do")
232     public @ResponseBody Object einvoice(HttpServletRequest request, HttpServletResponse response) {
233
234         CallBackMessage callBackMessage = new CallBackMessage();
235         OutputInvoiceIssueRequest einvoiceInfo = null;
236         IBWClient client = null;
237         String token = null;
1df3c0 238         String funclinkFormid = null;
F 239         GateEntity entity = new GateEntity();
75ba62 240         try {
F 241             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);
242             Map<String, String> map = null;
243             try {
244                 SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
245                 map = t22ifc.getParamMap(request);
246             } catch (Exception e) {
247                 e.printStackTrace();
248                callBackMessage.sendErrorMessage(this.getErrorMsg(e));
249                 return callBackMessage.toJSONObject();
250             } finally {
251                 SpObserver.setDBtoInstance();
252             }
253             String docCode = null;//单号
254
255             if (map != null) {
256                 docCode = map.get("doccode");
257             }
258             if (docCode == null) {
259                 callBackMessage.sendErrorMessage("单号不能为空");
260                 return callBackMessage.toJSONObject();
261             }
1df3c0 262              funclinkFormid=request.getParameter("formid");//功能链接功能号,返回给前端在刷脸成功后自动再次执行使用
75ba62 263             entity.setFormid(150504);
F 264             entity.setDbid(dataSourceEntity.getDbId() + "");
265             entity.setDocCode(docCode);
1df3c0 266             entity.setUserCode(request.getSession().getAttribute(SessionKey.USERCODE) + "");
75ba62 267             entity.setUserName(request.getSession().getAttribute(SessionKey.USER_NAME) + "");
F 268             einvoiceInfo = this.baiwangIfc.getEinvoiceInfo(entity);
1df3c0 269     //---------测试代码
F 270          /*  int  errorCode = -1008;
271             client = new BWRestClient(TokenUtils.url, TokenUtils.appKey, TokenUtils.appSecret); // 初始化一个客户端
272             token = TokenUtils.getToken();
273             request.setAttribute("doccode", entity.getDocCode());
274             request.setAttribute("funclinkFormid",funclinkFormid);
275             confrimFace(client, einvoiceInfo.getTaxNo(), token, request, response, callBackMessage);
276         callBackMessage.sendErrorMessage("errorMsg", errorCode);
277         return callBackMessage.toJSONObject();*/
278         //-----
75ba62 279             if (einvoiceInfo != null) {
F 280                 client = new BWRestClient(TokenUtils.url, TokenUtils.appKey, TokenUtils.appSecret); // 初始化一个客户端
281                 token = TokenUtils.getToken();
282                 OutputInvoiceIssueResponse issue = client.outputInvoice().issue(einvoiceInfo, token);
283                 log.info("msg:" + JSON.toJSONString(issue));
1df3c0 284                 //1--记录日志
F 285                 baiwangIfc.sendLog(entity, issue);
75ba62 286                 if (!issue.isSuccess()) {
F 287                     //失败
288                     callBackMessage.sendErrorMessage(issue.getErrorResponse().getSubMessage());
289                     return callBackMessage.toJSONObject();
290                 }
1df3c0 291
75ba62 292                 //-------成功
1df3c0 293                 //2---更新状态
75ba62 294                 baiwangIfc.updateStatus(entity, issue);
F 295                 threadPoolExecutor.execute(new QueryThread(client, token,issue.getResponse().getSuccess().get(0).getInvoiceNo(), einvoiceInfo.getTaxNo(),entity, request.getSession(),this,baiwangIfc));
296                 callBackMessage.setData("成功");
1df3c0 297                 callBackMessage.setMemo("生成成功!正在下载电子发票,稍候手动刷新界面显示发票附件").sendSuccessMessageByDefault();
75ba62 298                 return callBackMessage.toJSONObject();
F 299             } else {
300                 callBackMessage.sendErrorMessage(String.format("%s没有所需要的推送数据", entity.getDocCode()));
301                 return callBackMessage.toJSONObject();
302             }
303         } catch (BWOpenException ex) {
304             ex.printStackTrace();
305             int errorCode = -1;
306             String errorMsg = ex.getSubMessage();
307             if ("70031".equals(ex.getSubCode())) {
308                 //未登录或登录失效
309                 Map map = new HashMap();
310                 map.put("url", TokenUtils.LOGIN_URL);
311                 errorCode = -1007;
1df3c0 312                 errorMsg = "【" + ex.getSubMessage() + "】点击【确定】按钮在弹出的百望云平台重新登录,成功后关闭弹窗然后再次执行生成电子发票操作";
75ba62 313                 callBackMessage.setInfo(map);
F 314             } else if ("70045".equals(ex.getSubCode())) {
315                 //刷脸验证,生成二维码图片给税局APP扫码
316                 errorCode = -1008;
1df3c0 317                 request.setAttribute("doccode", entity.getDocCode());
F 318                 request.setAttribute("funclinkFormid",funclinkFormid);
75ba62 319                 confrimFace(client, einvoiceInfo.getTaxNo(), token, request, response, callBackMessage);
F 320             }
321             callBackMessage.sendErrorMessage(errorMsg, errorCode);
322             return callBackMessage.toJSONObject();
323         } catch (Exception e) {
324             callBackMessage.sendErrorMessage(e.getMessage());
325             return callBackMessage.toJSONObject();
326         } finally {
327             SpObserver.setDBtoInstance();
328         }
329     }
1df3c0 330     @RequestMapping(value = "/baiwang/download.do")
F 331     private @ResponseBody Object downLoadEinvoiceFile(HttpServletRequest request, HttpServletResponse response) {
332         CallBackMessage callBackMessage = new CallBackMessage();
333         try {
334             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);
335             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
336             Map<String, String> map = null;
337             try {
338                 map = t22ifc.getParamMap(request);
339             } catch (Exception e) {
340                 e.printStackTrace();
341                 callBackMessage.sendErrorMessage(this.getErrorMsg(e));
342                 return callBackMessage.toJSONObject();
343             }
344             String docCode = null;//单号
75ba62 345
1df3c0 346             if (map != null) {
F 347                 docCode = map.get("doccode");
348             }
349             if (docCode == null) {
350                 callBackMessage.sendErrorMessage("单号不能为空");
351                 return callBackMessage.toJSONObject();
352             }
353             GateEntity entity=new GateEntity();
354             entity.setDocCode(docCode);
355             QueryEntiy einvoiceInfo = this.baiwangIfc.getEinvoiceInfo(docCode);
356             if(einvoiceInfo!=null) {
357                 BWRestClient client = new BWRestClient(TokenUtils.url, TokenUtils.appKey, TokenUtils.appSecret); // 初始化一个客户端
358                 String token = TokenUtils.getToken();
359                 OutputFormatQueryQdInvoiceRequest query = new OutputFormatQueryQdInvoiceRequest();
360                 OutputFormatQueryQdInvoiceData data = new OutputFormatQueryQdInvoiceData();
361                 data.setInvoiceNo(einvoiceInfo.getInvoiceNo());
362                 query.setData(data);
363                 query.setTaxNo(einvoiceInfo.getTaxNo());
364                 OutputFormatQueryQdInvoiceResponse outputFormatQueryQdInvoiceResponse = client.outputFormat().queryQdInvoice(query,
365                         token);
366                 log.info("msg:" + JSON.toJSONString(outputFormatQueryQdInvoiceResponse));
367                 if (!outputFormatQueryQdInvoiceResponse.isSuccess()) {
368                     //失败
369                     throw new ApplicationException(outputFormatQueryQdInvoiceResponse.getErrorResponse().getSubMessage());
370
371                 }
372                 String fielid = "InvoiceFile";//附件字段
373                 String unid = this.saveAttachment(outputFormatQueryQdInvoiceResponse, entity, fielid);
374                 //--更新状态
375                 baiwangIfc.updateByAttachment(entity, unid, fielid);
376             }
377             callBackMessage.setData("成功");
378             callBackMessage.setMemo("下载成功").sendSuccessMessageByDefault();
379             return callBackMessage.toJSONObject();
380         }catch (Exception ex) {
381             ex.printStackTrace();
382             callBackMessage.sendErrorMessage(ex.getMessage());
383             return callBackMessage.toJSONObject();
384         }finally {
385             SpObserver.setDBtoInstance();
386         }
387     }
75ba62 388     private void confrimFace(IBWClient client, String taxNo, String token, HttpServletRequest request, HttpServletResponse response, CallBackMessage callBackMessage) {
F 389         try {
390             OutputBasicinfoGetAuthQrCodeRequest qrCodeRequest = new OutputBasicinfoGetAuthQrCodeRequest();
391             qrCodeRequest.setTaxNo(taxNo);
392             OutputBasicinfoGetAuthQrCodeResponse qrCodeResponse = client.outputBasicinfo().getAuthQrCode(qrCodeRequest, token);
393             if (!qrCodeResponse.isSuccess()) {
394                 //失败
395                 callBackMessage.sendErrorMessage(qrCodeResponse.getErrorResponse().getSubMessage());
396                 this.print(response, JSON.toJSONString(callBackMessage.toJSONObject()));
397                 return;
398             }
399             Map map = new HashMap();
400             map.put("qrCode", qrCodeResponse.getResponse().get(0).getQrCode());
401             map.put("authId", qrCodeResponse.getResponse().get(0).getAuthId());
402             map.put("taxNo", taxNo);
1df3c0 403             map.put("doccode", request.getAttribute("doccode"));//传多一个单号,实现扫码成功自动执行生成电子发票操作
F 404             map.put("funclinkFormid", request.getAttribute("funclinkFormid"));//传多一个功能链接功能号
75ba62 405             map.put("taxUserName", request.getSession().getAttribute(SessionKey.USER_TELE_PHONE) + "");//要用手机号做扫码认证,不能用账号
F 406             callBackMessage.setInfo(map);
407         }catch (Exception e){
408             callBackMessage.sendErrorMessage(e.getMessage());
409             this.print(response, JSON.toJSONString(callBackMessage.toJSONObject()));
410         }
411     }
412
413     private byte[] getAttachment(String imageHostUrl) throws IOException {
414         URL url = new URL(imageHostUrl);
415         //打开链接
416         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
417         //设置请求方式为"GET"
418         conn.setRequestMethod("GET");
419         //超时响应时间为5秒
420         conn.setConnectTimeout(5 * 1000);
421         //通过输入流获取图片数据
422         InputStream inStream = conn.getInputStream();
423         //得到图片的二进制数据,以二进制封装得到数据,具有通用性
424         byte[] bytes = FileCopyUtils.copyToByteArray(inStream);
425         conn.disconnect();
426         return bytes;
427     }
428
429     public String saveAttachment(OutputFormatQueryQdInvoiceResponse response, GateEntity entity, String fieldId) throws Exception {
430         if (response == null)
431             return "";
432         String uuid = UUID.randomUUID().toString().toUpperCase();
433         String seqString = "";
434         //存在附件需要处理
435         String url = response.getResponse().getPdfUrl();
436         //源文件二进制
437         AttachmentIfc attachmentIfc = (AttachmentIfc) FactoryBean.getBean("AttachmentImpl");
438         byte[] orgBinaryimages = getAttachment(url);
439         com.yc.entity.attachment.AttachmentEntity attachmentEntity = new AttachmentEntity();
440         attachmentEntity.setUnid(uuid);
441         attachmentEntity.setSeq(null);
442         attachmentEntity.setFormId(entity.getFormid());
443         attachmentEntity.setDoccode(entity.getDocCode());
444         attachmentEntity.setFieldId(fieldId);
445         attachmentEntity.setRowId(null);
446         attachmentEntity.setPhysicalFile(null);
447         attachmentEntity.setPhysicalPath(null);
448         attachmentEntity.setOriginalFileName(entity.getDocCode() + ".pdf");
449         attachmentEntity.setFileType("pdf");
1df3c0 450         attachmentEntity.setFileSize(new Long(orgBinaryimages.length));
F 451         attachmentEntity.setAuthorCode(entity.getUserCode());
452         attachmentEntity.setAuthorName(entity.getUserName());
75ba62 453         UploadFiles uploadFiles = new UploadFiles();
F 454         uploadFiles.setOrgBinaryimages(orgBinaryimages);
455         uploadFiles.setSmallBinaryimages(orgBinaryimages);
456         AttachmentWhereEntity attachmentWhereEntity = attachmentIfc.saveAttachment(uploadFiles, attachmentEntity, "0");
457         seqString += attachmentWhereEntity.getSeq();
458         return uuid + ";" + seqString;
459     }
460 }