|
|
|
@ -1,6 +1,8 @@ |
|
|
|
|
package com.jianshui.api.controller.http.invoice.v1; |
|
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil; |
|
|
|
|
import cn.hutool.core.util.IdUtil; |
|
|
|
|
import cn.hutool.json.JSONArray; |
|
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
import com.google.gson.Gson; |
|
|
|
@ -11,13 +13,16 @@ import com.jianshui.common.exception.jianshui.JianshuiParamErrorException; |
|
|
|
|
import com.jianshui.common.exception.jianshui.JianshuiServiceException; |
|
|
|
|
import com.jianshui.common.utils.StringUtils; |
|
|
|
|
import com.jianshui.common.utils.http.HttpHelper; |
|
|
|
|
import com.jianshui.common.utils.uuid.IdUtils; |
|
|
|
|
import com.jianshui.invoice.constant.elephant.ElephantConstants; |
|
|
|
|
import com.jianshui.invoice.domain.BillInfo; |
|
|
|
|
import com.jianshui.invoice.domain.FindRedInfo; |
|
|
|
|
import com.jianshui.invoice.domain.InvoiceBack; |
|
|
|
|
import com.jianshui.invoice.domain.ele.EleNewMessage; |
|
|
|
|
import com.jianshui.invoice.domain.ele.EleOuterMessage; |
|
|
|
|
import com.jianshui.invoice.domain.ele.FindRedInfoDTO; |
|
|
|
|
import com.jianshui.invoice.domain.ele.HZSQDMessage; |
|
|
|
|
import com.jianshui.invoice.mapper.InvoiceBackMapper; |
|
|
|
|
import com.jianshui.invoice.task.InvoiceBackTask; |
|
|
|
|
import com.jianshui.invoice.utils.elephant.ElephantUtils; |
|
|
|
|
import com.jianshui.platform.dto.InvoiceAdd.BillInfoPDTO; |
|
|
|
@ -44,6 +49,7 @@ import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
|
import java.security.InvalidKeyException; |
|
|
|
|
import java.security.NoSuchAlgorithmException; |
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -75,6 +81,9 @@ public class InvoiceController { |
|
|
|
|
@Autowired |
|
|
|
|
private InvoiceAddPService addService; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private InvoiceBackMapper invoiceBackMapper; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -1752,61 +1761,149 @@ public class InvoiceController { |
|
|
|
|
* @throws Exception |
|
|
|
|
*/ |
|
|
|
|
@Validated |
|
|
|
|
@ApiOperation("开票请求接口-(蓝字发票开具,红字发票开具)") |
|
|
|
|
@ApiOperation("开票请求接口-(蓝字发票开具,红字发票开具)-异步接口") |
|
|
|
|
@ApiImplicitParams({ |
|
|
|
|
@ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true), |
|
|
|
|
@ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)}) |
|
|
|
|
@PostMapping({"/api/invoice/v1/addInvoiceBatch", "/api/invoice/v1/addInvoiceBatch/{identity}", "/invoice/addInvoiceBatch"}) |
|
|
|
|
public Object addInvoiceBatch(HttpServletRequest request, String identity) throws Exception { |
|
|
|
|
|
|
|
|
|
// TODO: 2023/11/2 批量开票接口
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 摘成异步逻辑,状态码过来所有的错误记录数据库,记录请求过来的数据先
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void addInvoiceBatch(HttpServletRequest request, String identity) throws Exception { |
|
|
|
|
|
|
|
|
|
// 1-前置条件判断
|
|
|
|
|
InvoiceBack invoiceBack = new InvoiceBack(); |
|
|
|
|
invoiceBack.setUpdateTime(new Date()); |
|
|
|
|
invoiceBack.setCreateTime(new Date()); |
|
|
|
|
invoiceBack.setId(IdUtil.randomUUID()); |
|
|
|
|
invoiceBack.setSystemOrderno(IdUtil.randomUUID()); |
|
|
|
|
invoiceBack.setStatus("1"); |
|
|
|
|
invoiceBack.setIdentity(identity); |
|
|
|
|
invoiceBack.setBackMsg(""); |
|
|
|
|
invoiceBack.setBackUrl(""); |
|
|
|
|
|
|
|
|
|
// 1-1 identity校验
|
|
|
|
|
if (StringUtils.isEmpty(identity)) { |
|
|
|
|
return AjaxResult.error(ErrorCode.EMPTY_IDENTITY); |
|
|
|
|
invoiceBack.setResultCode("9999"); |
|
|
|
|
invoiceBack.setResultMsg("身份认证参数为空!"); |
|
|
|
|
log.info("[重汽批量开票接口]身份认证参数为空!"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity); |
|
|
|
|
// 1-2 企业信息校验
|
|
|
|
|
Companyservice companyservice = null; |
|
|
|
|
try { |
|
|
|
|
companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
invoiceBack.setResultCode("9999"); |
|
|
|
|
invoiceBack.setResultMsg("企业信息不存在!"); |
|
|
|
|
log.info("[重汽批量开票接口]获取企业信息异常!identity={},e={}",identity,e); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (companyservice == null) { |
|
|
|
|
return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND); |
|
|
|
|
invoiceBack.setResultCode("9999"); |
|
|
|
|
invoiceBack.setResultMsg("企业信息不存在!"); |
|
|
|
|
log.info("[重汽批量开票接口]企业信息不存在!identity={}",identity); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 获得入口报文适配器
|
|
|
|
|
String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid()); |
|
|
|
|
IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey); |
|
|
|
|
JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "add"); |
|
|
|
|
|
|
|
|
|
// 报文处理
|
|
|
|
|
BillInfo billInfo = decryptResult.toJavaObject(BillInfo.class); |
|
|
|
|
if (billInfo == null) { |
|
|
|
|
throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice"); |
|
|
|
|
// 1-3 获得入口报文适配器
|
|
|
|
|
JSONObject decryptResult = null; |
|
|
|
|
try { |
|
|
|
|
String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid()); |
|
|
|
|
IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey); |
|
|
|
|
decryptResult = invoiceRequestService.decrypt(request, companyservice, "add"); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
invoiceBack.setResultCode("9999"); |
|
|
|
|
invoiceBack.setResultMsg("请求解密失败!"); |
|
|
|
|
log.info("[重汽批量开票接口]请求解密失败!identity={}",identity); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 2-报文处理
|
|
|
|
|
cn.hutool.json.JSONObject billJson = null; |
|
|
|
|
try { |
|
|
|
|
billJson = JSONUtil.parseObj(decryptResult); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
invoiceBack.setResultCode("9999"); |
|
|
|
|
invoiceBack.setResultMsg("未匹配到发票报文!"); |
|
|
|
|
log.info("[重汽批量开票接口]报文格式化异常!identity={},e={}",identity,e); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if(billJson.get("billInfo") == null ){ |
|
|
|
|
invoiceBack.setResultCode("9999"); |
|
|
|
|
invoiceBack.setResultMsg("未匹配到发票报文!"); |
|
|
|
|
log.info("[重汽批量开票接口]未匹配到发票报文!identity={}",identity); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
billInfo.setSource("2"); |
|
|
|
|
// 获得请求实例,并且进行扣费
|
|
|
|
|
String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid()); |
|
|
|
|
serviceManageService.companyConsume("invoice", companyservice.getCompanyid()); |
|
|
|
|
billInfo.setServiceSupplierKey(serviceKey); |
|
|
|
|
IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey); |
|
|
|
|
HXResponse result = invoiceService.addInvoice(billInfo, companyservice); |
|
|
|
|
|
|
|
|
|
// HXResponse result = preHandleDecryptResult(decryptResult, "add", companyservice);
|
|
|
|
|
|
|
|
|
|
String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid()); |
|
|
|
|
IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey); |
|
|
|
|
|
|
|
|
|
return invoiceResponseService.response(result, companyservice, "add"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONArray billJsonArray = null; |
|
|
|
|
try { |
|
|
|
|
billJsonArray = JSONUtil.parseArray(billJson.get("billInfo")); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
invoiceBack.setResultCode("9999"); |
|
|
|
|
invoiceBack.setResultMsg("发票报文为空!"); |
|
|
|
|
log.info("[重汽批量开票接口]发票报文获取异常!identity={},e={}",identity,e); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if(billJsonArray.size() == 0){ |
|
|
|
|
invoiceBack.setResultCode("9999"); |
|
|
|
|
invoiceBack.setResultMsg("发票报文为空!"); |
|
|
|
|
log.info("[重汽批量开票接口]发票报文为空!identity={}",identity); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 3-报文递交开票
|
|
|
|
|
for (Object billInfoObj : billJsonArray) { |
|
|
|
|
|
|
|
|
|
BillInfo billInfo = BeanUtil.copyProperties(billInfoObj,BillInfo.class); |
|
|
|
|
if (billInfo == null) { |
|
|
|
|
invoiceBack.setResultCode("9999"); |
|
|
|
|
invoiceBack.setResultMsg("报文格式错误!"); |
|
|
|
|
log.info("[重汽批量开票接口]报文格式错误!identity={}",identity); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
billInfo.setSource("2"); |
|
|
|
|
|
|
|
|
|
// 4-获得请求实例,并且进行扣费
|
|
|
|
|
IInvoiceApiService invoiceService = null; |
|
|
|
|
try { |
|
|
|
|
String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid()); |
|
|
|
|
serviceManageService.companyConsume("invoice", companyservice.getCompanyid()); |
|
|
|
|
billInfo.setServiceSupplierKey(serviceKey); |
|
|
|
|
invoiceService = invoiceServiceFactory.getService(serviceKey); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
invoiceBack.setResultCode("9999"); |
|
|
|
|
invoiceBack.setResultMsg("扣费失败!"); |
|
|
|
|
log.info("[重汽批量开票接口]扣费失败!identity={},e={}",identity,e); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 5-开票
|
|
|
|
|
try { |
|
|
|
|
HXResponse resultHx = invoiceService.addInvoice(billInfo, companyservice); |
|
|
|
|
log.info("[重汽批量开票接口]开票identity={},返回结果result={}",identity,resultHx); |
|
|
|
|
|
|
|
|
|
if(!resultHx.isSuccess()){ |
|
|
|
|
invoiceBack.setResultCode("9999"); |
|
|
|
|
invoiceBack.setResultMsg(resultHx.getMessage()); |
|
|
|
|
invoiceBack.setSystemOrderno(billInfo.getSystemOrderno()); |
|
|
|
|
invoiceBack.setStatus("0"); |
|
|
|
|
invoiceBackMapper.insertInvoiceBack(invoiceBack); |
|
|
|
|
log.info("[重汽批量开票接口]提交开票异常,开票失败!identity={},msg={}",identity,resultHx.getMessage()); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
invoiceBack.setResultCode("9999"); |
|
|
|
|
invoiceBack.setResultMsg("开票失败!"); |
|
|
|
|
invoiceBack.setSystemOrderno(billInfo.getSystemOrderno()); |
|
|
|
|
invoiceBack.setStatus("0"); |
|
|
|
|
invoiceBackMapper.insertInvoiceBack(invoiceBack); |
|
|
|
|
log.info("[重汽批量开票接口]提交开票异常,开票失败!identity={},e={}",identity,e); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.info("[重汽批量开票接口]{},执行完成!",identity); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|