Blame view

parking-module-pay/src/main/java/com/zteits/pay/job/InvoiceNuonuoQueryJob.java 5.3 KB
55f68655   王彪总   新项目
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
  package com.zteits.pay.job;
  
  import org.apache.commons.collections4.CollectionUtils;
  import com.xxl.job.core.biz.model.ReturnT;
  import com.xxl.job.core.context.XxlJobHelper;
  import com.xxl.job.core.handler.annotation.XxlJob;
  import com.zteits.clouds.api.apibase.constants.InvoiceStateEnum;
  import com.zteits.pay.biz.invoice.handler.hx.InvoiceOpenHandlerNuonuo;
  import com.zteits.pay.biz.invoice.handler.hx.param.NuonuoInvoiceResponse;
  import com.zteits.pay.biz.invoice.handler.hx.param.NuonuoInvoiceResponse.NuonuoInvoiceResult;
  import com.zteits.pay.biz.invoice.handler.hx.param.NuonuoResultCodeEnum;
  import com.zteits.pay.dao.TdCCustInvoiceHisDao;
  import com.zteits.pay.domain.TdCCustInvoiceHis;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  
  import java.util.Date;
  import java.util.List;
  
  /**
   * 诺诺发票状态查询定时任务
   * 查询invoice_code不为空且invoice_url为空的发票记录,调用诺诺接口获取最新状态并更新数据库
   *
   * @author wangbiao
   * @date 2026-08-10
   */
  @Component
  public class InvoiceNuonuoQueryJob {
      private static final Logger logger = LoggerFactory.getLogger(InvoiceNuonuoQueryJob.class);
  
      @Autowired
      private InvoiceOpenHandlerNuonuo invoiceOpenHandlerNuonuo;
  
      @Autowired
      private TdCCustInvoiceHisDao custInvoiceHisDao;
  
      @XxlJob(value = "InvoiceNuonuoQueryJob")
      public ReturnT<String> execute(String... params) throws Exception {
          this.logger("---begin 诺诺发票状态查询定时任务---");
  
          List<TdCCustInvoiceHis> pendingList = custInvoiceHisDao.selectNuonuoPendingInvoiceList();
          if (CollectionUtils.isEmpty(pendingList)) {
              this.logger("---本次没有需要查询的诺诺发票记录---");
              return ReturnT.SUCCESS;
          }
  
          this.logger("本次查询到待同步诺诺发票记录条数=" + pendingList.size());
          int successCount = 0;
          int failCount = 0;
  
          for (TdCCustInvoiceHis record : pendingList) {
              try {
                  this.logger("查询诺诺发票详情,invoiceCode=" + record.getInvoiceCode());
                  NuonuoInvoiceResponse response = invoiceOpenHandlerNuonuo.queryInvoiceDetail(record.getInvoiceCode());
  
                  if (response == null || !NuonuoResultCodeEnum.E0000.getCode().equals(response.getCode())) {
                      String errorMsg = response != null ? response.getDescribe() : "响应为空";
                      this.logger("诺诺查询发票详情失败,invoiceCode=" + record.getInvoiceCode() + ",原因:" + errorMsg);
                      failCount++;
                      continue;
                  }
  
                  List<NuonuoInvoiceResult> resultList = response.getResult();
                  if (CollectionUtils.isEmpty(resultList)) {
                      this.logger("诺诺查询发票详情返回结果为空,invoiceCode=" + record.getInvoiceCode());
                      failCount++;
                      continue;
                  }
  
                  NuonuoInvoiceResult result = resultList.get(0);
  
                  TdCCustInvoiceHis updateRecord = new TdCCustInvoiceHis();
                  updateRecord.setId(record.getId());
                  updateRecord.setModfiyDate(new Date());
  
                  if (result.getInvoiceNo() != null) {
                      updateRecord.setInvoiceNumber(result.getInvoiceNo());
                  }
  
                  if (result.getInvoiceDate() != null) {
                      updateRecord.setInvoiceDate(result.getInvoiceDate());
                  }
  
                  if (result.getPdfUrl() != null) {
                      updateRecord.setInvoiceUrl(result.getPdfUrl());
                  }
  
                  if (result.getStatusMsg() != null) {
                      updateRecord.setMsg(result.getStatusMsg());
                  } else if (result.getFailCause() != null) {
                      updateRecord.setMsg(result.getFailCause());
                  }
  
                  if ("2".equals(result.getStatus())) {
                      if (result.getPdfUrl() != null && !result.getPdfUrl().isEmpty()) {
                          updateRecord.setInvoiceState(String.valueOf(InvoiceStateEnum.ALREADY_DOWNLOAD.getValue()));
                      } else {
                          updateRecord.setInvoiceState(String.valueOf(InvoiceStateEnum.ALREADY_OPEN_INVOICE.getValue()));
                      }
                  }
  
                  custInvoiceHisDao.updateInvioceById(updateRecord);
                  this.logger("诺诺发票状态更新成功,invoiceCode=" + record.getInvoiceCode()
                          + ",status=" + result.getStatus()
                          + ",invoiceNo=" + result.getInvoiceNo()
                          + ",pdfUrl=" + result.getPdfUrl());
                  successCount++;
  
              } catch (Exception e) {
                  this.logger("诺诺发票查询异常,invoiceCode=" + record.getInvoiceCode() + ",异常:" + e.getMessage());
                  logger.error("诺诺发票查询异常", e);
                  failCount++;
              }
          }
  
          this.logger("---end 诺诺发票状态查询定时任务,成功=" + successCount + ",失败=" + failCount + "---");
          return ReturnT.SUCCESS;
      }
  
      private void logger(String msg) {
          XxlJobHelper.log(msg);
          logger.info(msg);
      }
  }