| | |
| | | |
| | | import java.util.List; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import javax.annotation.Resource; |
| | | |
| | |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.ruoyi.tms.domain.PayableAuditLog; |
| | | import com.ruoyi.tms.service.IPayableAuditLogService; |
| | | |
| | | /** |
| | | * 应付账单Service业务层处理 |
| | |
| | | |
| | | @Autowired |
| | | private RestTemplate restTemplate; |
| | | |
| | | @Autowired |
| | | private IPayableAuditLogService payableAuditLogService; |
| | | |
| | | @Value("${custom.cwxtApi.url}") |
| | | private String url; |
| | |
| | | executor.execute(() -> pushPayableToExternalSystem(tmsApBill, tmsPayableFeeList)); |
| | | } |
| | | |
| | | @Override |
| | | public void cancelPushToExternalSystem(Integer id) { |
| | | TmsApBill tmsApBill = tmsApBillMapper.selectTmsApBillById(id); |
| | | if (tmsApBill == null) { |
| | | throw new RuntimeException("应付账单不存在"); |
| | | } |
| | | |
| | | // 异步推送作废请求 |
| | | AsyncTaskExecutor executor = new SimpleAsyncTaskExecutor(); |
| | | executor.execute(() -> pushCancelToExternalSystem(tmsApBill)); |
| | | } |
| | | |
| | | /** |
| | | * 向外部系统推送应付数据作废 |
| | | * @param tmsApBill 应付账单 |
| | | */ |
| | | @Async |
| | | protected void pushCancelToExternalSystem(TmsApBill tmsApBill) { |
| | | java.util.Map<String, Object> requestBody = new java.util.HashMap<>(); |
| | | try { |
| | | |
| | | |
| | | // 构建请求体 |
| | | String apiUrl = url+"/cancelPayableBill"; |
| | | |
| | | // 构建请求体,只需要sourceSystemId |
| | | requestBody.put("sourceSystemId", tmsApBill.getSourceSystemId()); |
| | | |
| | | // 设置HTTP头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_JSON); |
| | | HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(requestBody), headers); |
| | | |
| | | // 发送API请求 |
| | | ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.POST, entity, String.class); |
| | | logger.info("推送应付数据作废到外部系统成功,响应: {}", response.getBody()); |
| | | |
| | | // 更新推送状态为成功 |
| | | tmsApBill.setPushStatus(2); |
| | | tmsApBill.setStatus(3); // 设置账单状态为作废 |
| | | tmsApBill.setPushTime(DateUtils.getNowDate()); |
| | | tmsApBillMapper.updateTmsApBill(tmsApBill); |
| | | |
| | | // 重置关联的应付费用状态为待确认 |
| | | tmsPayableFeeMapper.update(new com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<TmsPayableFee>() |
| | | .set(TmsPayableFee::getStatus, 0) |
| | | .set(TmsPayableFee::getBillPayableId, null) |
| | | .set(TmsPayableFee::getBillPayableNo, null) |
| | | .eq(TmsPayableFee::getBillPayableId, tmsApBill.getId()) |
| | | ); |
| | | logger.info("重置应付费用状态成功,账单ID: {}", tmsApBill.getId()); |
| | | } catch (Exception e) { |
| | | logger.error("推送应付数据作废到外部系统失败,账单ID: {}, 供应商: {}", |
| | | tmsApBill.getId(), tmsApBill.getServiceProviderName(), e); |
| | | logger.debug("推送失败的请求数据: {}", JSON.toJSONString(requestBody)); |
| | | |
| | | // 更新推送状态为失败 |
| | | tmsApBill.setPushStatus(3); |
| | | tmsApBill.setPushTime(DateUtils.getNowDate()); |
| | | tmsApBillMapper.updateTmsApBill(tmsApBill); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 更新推送状态 |
| | | * |
| | | * @param id 应付账单ID |
| | | * @param pushStatus 推送状态 |
| | | * @param pushFailReason 推送失败原因 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int updatePushStatus(Integer id, Integer pushStatus, String pushFailReason) { |
| | | public int updatePushStatus(Integer id, Integer pushStatus) { |
| | | TmsApBill tmsApBill = new TmsApBill(); |
| | | tmsApBill.setId(id); |
| | | tmsApBill.setPushStatus(pushStatus); |
| | | tmsApBill.setPushTime(DateUtils.getNowDate()); |
| | | tmsApBill.setPushFailReason(pushFailReason); |
| | | return tmsApBillMapper.updateTmsApBill(tmsApBill); |
| | | } |
| | | |
| | | /** |
| | | * 添加应付账单审核日志 |
| | | * |
| | | * @param auditLog 审核日志 |
| | | * @return 结果 |
| | | */ |
| | | public int addApBillAuditLog(PayableAuditLog auditLog) { |
| | | return payableAuditLogService.insertPayableAuditLog(auditLog); |
| | | } |
| | | |
| | | /** |
| | |
| | | protected void pushPayableToExternalSystem(TmsApBill tmsApBill, List<TmsPayableFee> tmsPayableFeeList) { |
| | | java.util.Map<String, Object> requestBody = new java.util.HashMap<>(); |
| | | try { |
| | | // 更新推送状态为推送中 |
| | | tmsApBill.setPushStatus(1); |
| | | tmsApBill.setPushTime(DateUtils.getNowDate()); |
| | | tmsApBillMapper.updateTmsApBill(tmsApBill); |
| | | |
| | | |
| | | // 构建请求体 |
| | | String apiUrl = url+"/addPayableBill"; |
| | |
| | | billMap.put("billSendDate", ""); |
| | | billMap.put("billDueDate", ""); |
| | | billMap.put("remark", ""); |
| | | billMap.put("sourceSystemId", tmsApBill.getId()); |
| | | |
| | | // 构建fees部分 |
| | | List<java.util.Map<String, Object>> feesList = new java.util.ArrayList<>(); |
| | |
| | | ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.POST, entity, String.class); |
| | | logger.info("推送应付数据到外部系统成功,响应: {}", response.getBody()); |
| | | |
| | | // 解析响应,获取sourceSystemId |
| | | try { |
| | | JSONObject result = JSONObject.parseObject(response.getBody()); |
| | | String sourceSystemId = result.getString("sourceSystemId"); |
| | | if (sourceSystemId != null) { |
| | | tmsApBill.setSourceSystemId(Integer.parseInt(sourceSystemId)); |
| | | } |
| | | } catch (Exception e) { |
| | | logger.error("解析外部系统响应失败: {}", e.getMessage()); |
| | | } |
| | | |
| | | // 更新推送状态为成功 |
| | | tmsApBill.setPushStatus(2); |
| | | tmsApBill.setPushTime(DateUtils.getNowDate()); |
| | | tmsApBill.setPushFailReason(null); |
| | | tmsApBillMapper.updateTmsApBill(tmsApBill); |
| | | } catch (Exception e) { |
| | | logger.error("推送应付数据到外部系统失败,账单ID: {}, 供应商: {}", |
| | |
| | | // 更新推送状态为失败 |
| | | tmsApBill.setPushStatus(3); |
| | | tmsApBill.setPushTime(DateUtils.getNowDate()); |
| | | tmsApBill.setPushFailReason(e.getMessage()); |
| | | tmsApBillMapper.updateTmsApBill(tmsApBill); |
| | | } |
| | | } |