diff --git a/jianshui-invoice/src/main/java/com/jianshui/invoice/constant/elephant/ElephantConstantsV6.java b/jianshui-invoice/src/main/java/com/jianshui/invoice/constant/elephant/ElephantConstantsV6.java index 75c8a44..f7a3e18 100644 --- a/jianshui-invoice/src/main/java/com/jianshui/invoice/constant/elephant/ElephantConstantsV6.java +++ b/jianshui-invoice/src/main/java/com/jianshui/invoice/constant/elephant/ElephantConstantsV6.java @@ -38,6 +38,7 @@ public class ElephantConstantsV6 { public static String GENERATE_QD_BDCXS_INVOICE = "/invoice/api/v6/GenerateQdBdcxsInvoice"; public static String GENERATE_QD_JZFW_INVOICE = "/invoice/api/v6/GenerateQdJzfwInvoice"; public static String GENERATE_QD_BDXZL_INVOICE = "/invoice/api/v6/GenerateQdBdczlInvoice"; + public static String QD_GET_RED_INVOICE_BACK = "/invoice/api/v6/getDownloadRedInvoice"; // 发票开票结果拉取 public static String ADD_INVOICE_RESULT = "order-api/order-api/v6/GetOrderInfoAndInvoiceInfo"; diff --git a/jianshui-invoice/src/main/java/com/jianshui/invoice/mapper/BillInfoMapper.java b/jianshui-invoice/src/main/java/com/jianshui/invoice/mapper/BillInfoMapper.java index 8a407e4..c902c42 100644 --- a/jianshui-invoice/src/main/java/com/jianshui/invoice/mapper/BillInfoMapper.java +++ b/jianshui-invoice/src/main/java/com/jianshui/invoice/mapper/BillInfoMapper.java @@ -102,4 +102,5 @@ public interface BillInfoMapper extends BaseMapper { public List selectByOutTradeOrdernoNotDel(@Param(("companyId")) Long companyId, @Param("out_trade_orderno") String outTradeOrderno, @Param("updateBy") String updateBy); + BillInfo findBillInfoByFphm(String originFphm); } diff --git a/jianshui-invoice/src/main/java/com/jianshui/invoice/service/impl/api/ElephantInvoiceApiV6ServiceImpl.java b/jianshui-invoice/src/main/java/com/jianshui/invoice/service/impl/api/ElephantInvoiceApiV6ServiceImpl.java index 6a4c071..06a85d5 100644 --- a/jianshui-invoice/src/main/java/com/jianshui/invoice/service/impl/api/ElephantInvoiceApiV6ServiceImpl.java +++ b/jianshui-invoice/src/main/java/com/jianshui/invoice/service/impl/api/ElephantInvoiceApiV6ServiceImpl.java @@ -17,6 +17,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.dxhy.order.protocol.invoke.DxhyInterfaceResponse; import com.google.gson.Gson; +import com.jianshui.common.config.JianshuiConfig; import com.jianshui.common.constant.Constants; import com.jianshui.common.constant.WebServiceConstant; import com.jianshui.common.core.domain.AjaxResult; @@ -27,6 +28,7 @@ import com.jianshui.common.exception.jianshui.JianshiSystemErrorException; import com.jianshui.common.exception.jianshui.JianshuiParamErrorException; import com.jianshui.common.exception.jianshui.JianshuiServiceException; import com.jianshui.common.utils.*; +import com.jianshui.common.utils.file.FileUtils; import com.jianshui.common.utils.http.HttpHelper; import com.jianshui.common.utils.spring.SpringUtils; import com.jianshui.common.utils.uuid.IdUtils; @@ -80,6 +82,7 @@ import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletRequest; +import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -725,41 +728,53 @@ public class ElephantInvoiceApiV6ServiceImpl implements IInvoiceApiService { private void saveFileUseLocal(Invoice invoice,DDFPXX ddfpxx) { try { - IStorageService service = storageFactory.getStorageInstance(Constants.LOCAL); - byte[] pdfBytes = Base64.decode(ddfpxx.getPDFZJL()); + String key = IdUtils.fastSimpleUUID(); + String fileName = key + "."+ddfpxx.getZJLLX().toLowerCase(); + String fileDir = JianshuiConfig.getDownloadPath() + "/storage/"; + // 判断fileDir是否存在,不存在则创建 + File fileDirInst = new File(fileDir); + if (!fileDirInst.exists()) { + fileDirInst.mkdirs(); + } + String realPath = fileDir + fileName; + FileUtils.byteToFile(pdfBytes, realPath); - AjaxResult upload = service.upload(pdfBytes, ""); +// IStorageService service = storageFactory.getStorageInstance(Constants.LOCAL); +// +// +// +// AjaxResult upload = service.upload(pdfBytes, ""); //文件名称 // fileName = fileName+ Constants.POINT + (StringUtils.equals(ddfpxx.getZJLLX(), Constants.PDF_UPCASE) ? Constants.PDF : Constants.OFD); //文件写入路径 // String targetFilePath = environment.getProperty(Constants.INVOICE_FILE_SAVE_PATH) + fileName; // 创建一个输出流对象,用于将数据写入到本地文件,如果文件存在则会覆盖 -// FileOutputStream fileOutputStream = new FileOutputStream(targetFilePath, false); + FileOutputStream fileOutputStream = new FileOutputStream(realPath, false); // 直接将字节数组写入到输出流(即本地文件) -// fileOutputStream.write(pdfBytes); + fileOutputStream.write(pdfBytes); // 完成写入后关闭流 -// fileOutputStream.close(); + fileOutputStream.close(); //拼接url - StorageUrlDTO uploadResultDTO = (StorageUrlDTO) upload.get(Constants.DATA); +// StorageUrlDTO uploadResultDTO = (StorageUrlDTO) upload.get(Constants.DATA); - String fileUrl = configService.selectConfigByKey(Constants.FILE_URL); +// String fileUrl = configService.selectConfigByKey(Constants.FILE_URL); // uploadResultDTO.setStoragePath(targetFilePath); - uploadResultDTO.setContentType(ddfpxx.getZJLLX().toLowerCase()); +// uploadResultDTO.setContentType(ddfpxx.getZJLLX().toLowerCase()); - Base64.encode(JSONObject.toJSONString(uploadResultDTO)); +// Base64.encode(JSONObject.toJSONString(uploadResultDTO)); - String pdfUrl = fileUrl + Constants.ESCAPE + Base64.encode(JSONObject.toJSONString(uploadResultDTO)); - invoice.setInvoicePdfUrl(pdfUrl); - invoice.setInvoiceFileStorage(uploadResultDTO.getServiceProvider()); +// String pdfUrl = fileUrl + Constants.ESCAPE + Base64.encode(JSONObject.toJSONString(uploadResultDTO)); + invoice.setInvoicePdfUrl(realPath); + invoice.setInvoiceFileStorage("local"); if (StringUtils.equals(ddfpxx.getZJLLX(), Constants.OFD_UPCASE)) { - invoice.setcOfdUrl(pdfUrl); + invoice.setcOfdUrl(realPath); } }catch (Exception e){ log.error("文件保存异常",e); diff --git a/jianshui-invoice/src/main/java/com/jianshui/invoice/task/RedInvoiceBackTask.java b/jianshui-invoice/src/main/java/com/jianshui/invoice/task/RedInvoiceBackTask.java new file mode 100644 index 0000000..1136c7e --- /dev/null +++ b/jianshui-invoice/src/main/java/com/jianshui/invoice/task/RedInvoiceBackTask.java @@ -0,0 +1,200 @@ +package com.jianshui.invoice.task; + +import cn.hutool.core.util.IdUtil; +import cn.hutool.http.HttpUtil; +import cn.hutool.json.JSONUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.jianshui.common.core.domain.AjaxResult; +import com.jianshui.common.core.domain.entity.Companyservice; +import com.jianshui.common.exception.jianshui.JianshuiServiceException; +import com.jianshui.common.utils.spring.SpringUtils; +import com.jianshui.invoice.constant.elephant.ElephantConstantsV6; +import com.jianshui.invoice.domain.BillInfo; +import com.jianshui.invoice.domain.Invoice; +import com.jianshui.invoice.domain.InvoiceBack; +import com.jianshui.invoice.domain.InvoiceDetail; +import com.jianshui.invoice.mapper.BillInfoMapper; +import com.jianshui.invoice.mapper.InvoiceBackMapper; +import com.jianshui.invoice.utils.elephant.ElephantUtils; +import com.jianshui.system.domain.CompanyserviceProp; +import com.jianshui.system.service.ICompanyservicePropService; +import com.jianshui.system.service.ICompanyserviceService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; +import java.util.Random; + +/** + * 定时任务-红票推送 + */ +@Component("redInvoiceBackTask") +@Slf4j +public class RedInvoiceBackTask { + + @Autowired + private ICompanyserviceService iCompanyserviceService; + + @Autowired + private ICompanyservicePropService propService; + + @Autowired + private BillInfoMapper billInfoMapper; + + @Autowired + private InvoiceBackMapper invoiceBackMapper; + + private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + private Random random = new Random(); + + public void getRedInvoiceAndBack() { + //查询需要推送的identity + CompanyserviceProp companyservicePropParam = new CompanyserviceProp(); + companyservicePropParam.setKey("red_invoice_back_service"); + List companyserviceProps = propService.selectCompanyservicePropList(companyservicePropParam); + //判断是否为空 + if (companyserviceProps != null && companyserviceProps.size() > 0) { + //遍历处理每一个公司 + for (CompanyserviceProp companyserviceProp : companyserviceProps) { + try { + Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByCompanyid(companyserviceProp.getCompanyid()); + JSONObject param = new JSONObject(); + param.put("nsrsbh", companyservice.getSellertax()); + param.put("startTime", companyserviceProp.getValue()); + //调用itax获取发票 + AjaxResult queryResult = ElephantUtils.sendRequestWithoutTokenV6New(ElephantConstantsV6.QD_GET_RED_INVOICE_BACK, "", JSONUtil.parse(param), companyservice); + log.info("【销项发票】【大象发票】获取重汽红票请求结果{}", queryResult.toString()); + + // 判断外层报文是否成功 + if (queryResult.isError()) { + throw new JianshuiServiceException(queryResult.getMsg()); + } + + //报文格式转化 + List invoices = JSONArray.parseArray((String) queryResult.getData(), Invoice.class); + + if (invoices != null && invoices.size() > 0) { + for (Invoice invoice : invoices) { + try { + //判断发票是否重复 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("status", "1"); + queryWrapper.eq("back_msg", invoice.getFphm()); + InvoiceBack invoiceBackSuccess = invoiceBackMapper.selectOne(queryWrapper); + if (invoiceBackSuccess != null) { + continue; + } + //获取蓝字发票 + BillInfo billInfo = billInfoMapper.findBillInfoByFphm(invoice.getOriginFphm()); + //处理大象没有返回的值 + invoice.setOutTradeOrderno(IdUtil.randomUUID()); + invoice.setId(billInfo.getId()+random.nextLong()); + invoice.setBillInfoId(billInfo.getId()+random.nextLong()); + invoice.setSystemOrderno(billInfo.getSystemOrderno()); + invoice.setCompanyId(companyserviceProp.getCompanyid()); + //转换发票类型 + if ("01".equals(invoice.getInvoiceType()) || "001".equals(invoice.getInvoiceType())) { + invoice.setInvoiceType("81"); + } else { + invoice.setInvoiceType("82"); + } + invoice.setState(2); + for (InvoiceDetail invoiceDetail : invoice.getInvoiceDetailList()) { + invoiceDetail.setId(random.nextLong()); + invoiceDetail.setInvoiceId(random.nextLong()); + } + //获取回调地址 + CompanyserviceProp secretIdProp = propService.selectPropByKey(Long.valueOf(companyserviceProp.getCompanyid()), "aisino_callback_url"); + //执行回调 + JSONObject invoiceJson = JSON.parseObject(JSON.toJSONString(invoice)); + invoiceJson.put("url",invoice.getInvoicePdfUrl()); + String result = HttpUtil.post(secretIdProp.getValue(),invoiceJson.toJSONString()); + //处理返回结果 + InvoiceBack invoiceBack = new InvoiceBack(); + invoiceBack.setId(IdUtil.randomUUID()); + invoiceBack.setSystemOrderno(invoice.getOutTradeOrderno()); + invoiceBack.setBackUrl(secretIdProp.getValue()); + invoiceBack.setIdentity(String.valueOf(companyserviceProp.getCompanyid())); + invoiceBack.setCreateTime(new Date()); + invoiceBack.setBackMsg(JSONUtil.toJsonStr(invoice)); + invoiceBack.setNum(1); + cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(result); + if (jsonObject != null) { + String status = jsonObject.get("status") != null ? jsonObject.get("status").toString() : ""; + String message = jsonObject.get("message") != null ? jsonObject.get("message").toString() : ""; + invoiceBack.setResultCode(status); + invoiceBack.setResultMsg(message); + if ("0000".equals(status)) { + invoiceBack.setStatus("1"); + invoiceBack.setBackMsg(invoice.getFphm()); + invoiceBack.setUpdateTime(new Date()); + invoiceBackMapper.insertInvoiceBack(invoiceBack); + } else if ("9999".equals(status)) { + invoiceBack.setStatus("4"); + invoiceBack.setUpdateTime(new Date()); + invoiceBackMapper.insertInvoiceBack(invoiceBack); + } else { + invoiceBack.setStatus("4"); + invoiceBack.setUpdateTime(new Date()); + invoiceBackMapper.insertInvoiceBack(invoiceBack); + } + } + }catch (Exception e){ + log.error("处理发票和回调失败{}",e); + } + } + invoices.sort((e1, e2) -> + Long.valueOf(e1.getStartTime()) > Long.valueOf(e2.getStartTime()) ? 1 : -1 + ); + Date date = new Date(Long.valueOf(invoices.get(0).getStartTime())); + companyserviceProp.setValue(formatter.format(date)); + propService.updateCompanyserviceProp(companyserviceProp); + } + } catch (Exception e) { + log.error("回调前获取发票信息失败{}",e); + } + } + } + } + + public void failRetryRedInvoiceBack(){ + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("status", "4"); + List invoiceBacks = invoiceBackMapper.selectList(queryWrapper); + + if (invoiceBacks != null && invoiceBacks.size() > 0){ + + ICompanyservicePropService companyserviceProp = SpringUtils.getBean(ICompanyservicePropService.class); + + for (InvoiceBack invoiceBack : invoiceBacks) { + try { + String identity = invoiceBack.getIdentity(); + CompanyserviceProp secretIdProp = companyserviceProp.selectPropByKey(Long.valueOf(identity), "aisino_callback_url"); + String result = HttpUtil.post(secretIdProp.getValue(), invoiceBack.getBackMsg()); + cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(result); + if(jsonObject != null){ + String status = jsonObject.get("status") != null ? jsonObject.get("status").toString():""; + String message = jsonObject.get("message")!= null ? jsonObject.get("message").toString():""; + + if("0000".equals(status)){ + JSONObject param = JSONObject.parseObject(invoiceBack.getBackMsg()); + invoiceBack.setStatus("1"); + invoiceBack.setBackMsg(param.getString("fphm")); + invoiceBack.setUpdateTime(new Date()); + invoiceBackMapper.updateInvoiceBack(invoiceBack); + } + } + }catch (Exception e){ + log.error("重汽红票回推重试失败{}",e); + } + } + } + + } +} diff --git a/jianshui-invoice/src/main/resources/mapper/invoice/BillInfoMapper.xml b/jianshui-invoice/src/main/resources/mapper/invoice/BillInfoMapper.xml index 1e2bc1b..b5f81d8 100644 --- a/jianshui-invoice/src/main/resources/mapper/invoice/BillInfoMapper.xml +++ b/jianshui-invoice/src/main/resources/mapper/invoice/BillInfoMapper.xml @@ -800,4 +800,9 @@ #{item.taxamt}, #{item.tspz}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updatedTime}) + +