package com.by4cloud.platformx.business.service.impl;
|
|
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.date.DatePattern;
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ObjUtil;
|
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.http.HttpUtil;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.by4cloud.platformx.admin.api.entity.SysDept;
|
import com.by4cloud.platformx.admin.api.feign.RemoteDeptService;
|
import com.by4cloud.platformx.business.dto.PaymentConfirmAddDTO;
|
import com.by4cloud.platformx.business.entity.*;
|
import com.by4cloud.platformx.business.mapper.*;
|
import com.by4cloud.platformx.business.service.BusinessCustomerService;
|
import com.by4cloud.platformx.business.service.ContractService;
|
import com.by4cloud.platformx.business.service.PaymentConfirmService;
|
import com.by4cloud.platformx.business.utils.SignHelper;
|
import com.by4cloud.platformx.common.core.util.R;
|
import com.by4cloud.platformx.common.security.util.SecurityUtils;
|
import lombok.RequiredArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.stereotype.Service;
|
|
import java.io.UnsupportedEncodingException;
|
import java.math.BigDecimal;
|
import java.security.InvalidKeyException;
|
import java.security.NoSuchAlgorithmException;
|
import java.util.*;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* 收款确认
|
*
|
* @author syt
|
* @date 2026-04-29 11:33:26
|
*/
|
@Slf4j
|
@Service
|
@RequiredArgsConstructor
|
public class PaymentConfirmServiceImpl extends ServiceImpl<PaymentConfirmMapper, PaymentConfirm> implements PaymentConfirmService {
|
|
private final ContractService contractService;
|
private final BusinessCustomerService businessCustomerService;
|
private final ContractPaymentScheduleMapper contractPaymentScheduleMapper;
|
private final CurrentOverdueMapper currentOverdueMapper;
|
private final HistoryOverdueMapper historyOverdueMapper;
|
private final PaymentSlipMapper paymentSlipMapper;
|
private final BipRequestRecordMapper bipRequestRecordMapper;
|
private final RemoteDeptService remoteDeptService;
|
private final RedisTemplate redisTemplate;
|
|
@Value("${bip.url}")
|
private String url;
|
|
@Value("${bip.codes}")
|
private String codes;
|
|
@Value("${bip.appKey}")
|
private String appKey;
|
|
@Value("${bip.appSecret}")
|
private String appSecret;
|
|
@Override
|
public R add(PaymentConfirmAddDTO addDTO) {
|
if (StrUtil.isNotBlank(addDTO.getContractNo())) {
|
Contract contract = contractService.getOne(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo, addDTO.getContractNo()));
|
BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
|
.eq(StrUtil.isNotBlank(addDTO.getBusGuestName()), BusinessCustomer::getCompanyName, addDTO.getBusGuestName())
|
.eq(StrUtil.isNotBlank(addDTO.getBusGuestId()), BusinessCustomer::getId, addDTO.getBusGuestId()));
|
if (ObjUtil.isNull(customer)) {
|
return R.failed("客商查询失败,请检查客商");
|
}
|
|
PaymentConfirm lastConfirm = baseMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
|
.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
|
BigDecimal lastTotal = new BigDecimal("0");
|
if (ObjUtil.isNotNull(lastConfirm)) {
|
lastTotal = lastConfirm.getTotalAmount();
|
}
|
|
//转入
|
if (StrUtil.equals(addDTO.getInOrOut(), "1")) {
|
BigDecimal newtotal = addDTO.getTransationAmount().add(lastTotal);
|
PaymentConfirm entity = new PaymentConfirm();
|
entity.setContractId(contract.getId());
|
entity.setContractName(contract.getContractName());
|
entity.setContractNo(addDTO.getContractNo());
|
if (ObjUtil.isNotNull(lastConfirm)) {
|
entity.setScheduleId(lastConfirm.getScheduleId());
|
entity.setScheduleName(lastConfirm.getScheduleName());
|
}
|
entity.setBusGuestId(customer.getId());
|
entity.setBusGuestName(customer.getCompanyName());
|
entity.setBusinessType("客户付款");
|
if (newtotal.compareTo(new BigDecimal("0")) > 0) {
|
entity.setTransationAmount(addDTO.getTransationAmount());
|
entity.setAdvanceAmount(newtotal);
|
entity.setTotalAmount(newtotal);
|
entity.setConfirmTime(new Date());
|
baseMapper.insert(entity);
|
} else if (newtotal.compareTo(new BigDecimal("0")) == 0) {
|
entity.setTransationAmount(addDTO.getTransationAmount());
|
entity.setTotalAmount(newtotal);
|
entity.setConfirmTime(new Date());
|
baseMapper.insert(entity);
|
} else {
|
entity.setTransationAmount(addDTO.getTransationAmount());
|
entity.setReceivableAmount(newtotal.multiply(new BigDecimal("-1")));
|
entity.setTotalAmount(newtotal);
|
entity.setConfirmTime(new Date());
|
baseMapper.insert(entity);
|
}
|
//更新付款阶段付款
|
List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
|
.eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2)
|
.orderByAsc(ContractPaymentSchedule::getCreateTime));
|
if (ArrayUtil.isNotEmpty(scheduleList.toArray())) {
|
BigDecimal remain = addDTO.getTransationAmount();
|
for (ContractPaymentSchedule schedule : scheduleList) {
|
BigDecimal lastRemain = remain;
|
remain = remain.subtract(StrUtil.equals(schedule.getPaymentStatus() + "", "0") ? schedule.getPlannedAmount() :
|
schedule.getPlannedAmount().subtract(schedule.getActualAmount()));
|
if (remain.compareTo(new BigDecimal("0")) >= 0) {
|
schedule.setActualAmount(schedule.getPlannedAmount());
|
schedule.setPaymentDate(addDTO.getConfirmTime());
|
schedule.setPaymentStatus(2);
|
contractPaymentScheduleMapper.updateById(schedule);
|
//查询是否有当前逾期
|
CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId, schedule.getId())
|
.eq(CurrentOverdue::getContractId, schedule.getContractId()).last("limit 1"));
|
if (ObjUtil.isNotNull(currentOverdue)) {
|
//新增逾期历史
|
HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue, HistoryOverdue.class, "id");
|
historyOverdue.setPaymentTime(addDTO.getConfirmTime());
|
historyOverdue.setCompId(schedule.getCompId());
|
historyOverdueMapper.insert(historyOverdue);
|
//删除当前逾期
|
currentOverdueMapper.deleteById(currentOverdue);
|
}
|
} else {
|
schedule.setActualAmount(StrUtil.equals(schedule.getPaymentStatus() + "", "0") ? schedule.getPlannedAmount().add(remain) :
|
schedule.getActualAmount().add(lastRemain));
|
schedule.setPaymentDate(addDTO.getConfirmTime());
|
schedule.setPaymentStatus(1);
|
contractPaymentScheduleMapper.updateById(schedule);
|
//查询是否有当前逾期
|
CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId, schedule.getId())
|
.eq(CurrentOverdue::getContractId, schedule.getContractId()).last("limit 1"));
|
if (ObjUtil.isNotNull(currentOverdue)) {
|
//新增逾期历史
|
HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue, HistoryOverdue.class, "id");
|
historyOverdue.setReceivableAmount(lastRemain);
|
historyOverdue.setPaymentTime(addDTO.getConfirmTime());
|
historyOverdue.setCompId(schedule.getCompId());
|
historyOverdueMapper.insert(historyOverdue);
|
//查询历史已付逾期金额
|
List<HistoryOverdue> historyOverdueList = historyOverdueMapper.selectList(Wrappers.<HistoryOverdue>lambdaQuery().eq(HistoryOverdue::getScheduleId, currentOverdue.getScheduleId()));
|
BigDecimal hisTotal = historyOverdueList.stream().map(item -> item.getReceivableAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
|
//更新当前逾期
|
currentOverdue.setReceivableAmount(schedule.getPlannedAmount().subtract(hisTotal));
|
currentOverdueMapper.updateById(currentOverdue);
|
}
|
break;
|
}
|
}
|
}
|
//更新合同已付款
|
BigDecimal paid = new BigDecimal("0");
|
if (ObjUtil.isNotNull(contract.getPaidAmount())) {
|
paid = contract.getPaidAmount();
|
}
|
paid = paid.add(addDTO.getTransationAmount());
|
contract.setPaidAmount(paid);
|
if (paid.compareTo(contract.getAmount()) > 0) {
|
contract.setPaidAmount(contract.getAmount());
|
}
|
contractService.updateById(contract);
|
}
|
//转出
|
if (StrUtil.equals(addDTO.getInOrOut(), "2")) {
|
PaymentConfirm entity = new PaymentConfirm();
|
entity.setContractId(contract.getId());
|
entity.setContractName(contract.getContractName());
|
entity.setContractNo(addDTO.getContractNo());
|
|
entity.setBusGuestId(customer.getId());
|
entity.setBusGuestName(customer.getCompanyName());
|
entity.setBusinessType("");
|
entity.setTransationAmount(addDTO.getTransationAmount());
|
BigDecimal newtotal = addDTO.getTransationAmount().multiply(new BigDecimal("-1")).add(lastConfirm.getTotalAmount());
|
if (newtotal.compareTo(new BigDecimal("0")) > 0) {
|
entity.setAdvanceAmount(newtotal);
|
entity.setTotalAmount(newtotal);
|
} else if (newtotal.compareTo(new BigDecimal("0")) == 0) {
|
entity.setTotalAmount(newtotal);
|
} else {
|
return R.failed("当前客户余额为" + lastConfirm.getTotalAmount() + "不足以满足当前发生金额,无法操作");
|
}
|
entity.setConfirmTime(new Date());
|
baseMapper.insert(entity);
|
}
|
// 查询所有付款阶段是否都付款完成
|
List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contract.getId()));
|
if (ArrayUtil.isNotEmpty(scheduleList.toArray())) {
|
if (scheduleList.stream().allMatch(item -> Objects.equals(item.getPaymentStatus(), 2))) {
|
contract.setContractStatus(3);
|
contractService.updateById(contract);
|
}
|
;
|
}
|
}else {
|
List<Contract> customerContractList = contractService.list(Wrappers.<Contract>lambdaQuery().eq(Contract::getPartyAId, addDTO.getBusGuestId())
|
.eq(Contract::getPartyBId,SecurityUtils.getUser().getCompId()).orderByAsc(Contract::getCreateTime).apply(" amount != paid_amount"));
|
if (ArrayUtil.isEmpty(customerContractList.toArray())) {
|
return R.failed("为查询到未付款合同");
|
}
|
BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
|
.eq(StrUtil.isNotBlank(addDTO.getBusGuestName()), BusinessCustomer::getCompanyName, addDTO.getBusGuestName())
|
.eq(ObjUtil.isNotNull(addDTO.getBusGuestId()), BusinessCustomer::getId, addDTO.getBusGuestId())
|
.last("limit 1"));
|
if (ObjUtil.isNull(customer)) {
|
return R.failed("客商查询失败");
|
}
|
|
BigDecimal transtionAmount = addDTO.getTransationAmount();
|
//剩余付款
|
BigDecimal transtionRemainAmount = transtionAmount;
|
//收到付款的合同
|
List<Contract> payContractList = new ArrayList<>();
|
//付款完成的合同
|
List<Contract> payCompleteContractList = new ArrayList<>();
|
//付款未完成的合同
|
List<Contract> payNoCompleteContractList = new ArrayList<>();
|
//有预付款的合同
|
List<Contract> advanceContractList = new ArrayList<>();
|
//付款时间
|
Date payDate = addDTO.getConfirmTime();
|
for (Contract contract : customerContractList) {
|
PaymentConfirm lastConfirm = baseMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
|
.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
|
BigDecimal lastTotal = new BigDecimal("0");
|
if (ObjUtil.isNotNull(lastConfirm)) {
|
lastTotal = lastConfirm.getTotalAmount();
|
}
|
|
//合同最近明细总额为0 或 > 0 情况下
|
if (lastTotal.compareTo(new BigDecimal("0")) >= 0) {
|
log.info("当前合同号:{},明细总额无应收款", contract.getContractNo());
|
advanceContractList.add(contract);
|
continue;
|
}
|
|
transtionRemainAmount = transtionRemainAmount.add(lastTotal);
|
PaymentConfirm entity = new PaymentConfirm();
|
entity.setContractId(contract.getId());
|
entity.setContractName(contract.getContractName());
|
entity.setContractNo(contract.getContractNo());
|
if (ObjUtil.isNotNull(lastConfirm)) {
|
entity.setScheduleId(lastConfirm.getScheduleId());
|
entity.setScheduleName(lastConfirm.getScheduleName());
|
}
|
entity.setBusGuestId(customer.getId());
|
entity.setBusGuestName(customer.getCompanyName());
|
entity.setBusinessType("客户付款");
|
if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
|
entity.setTransationAmount(lastTotal.multiply(new BigDecimal("-1")));
|
entity.setTotalAmount(new BigDecimal("0"));
|
entity.setConfirmTime(payDate);
|
baseMapper.insert(entity);
|
payContractList.add(contract);
|
} else if (transtionRemainAmount.compareTo(new BigDecimal("0")) == 0) {
|
entity.setTransationAmount(lastTotal.multiply(new BigDecimal("-1")));
|
entity.setTotalAmount(new BigDecimal("0"));
|
entity.setConfirmTime(payDate);
|
baseMapper.insert(entity);
|
payContractList.add(contract);
|
break;
|
} else {
|
entity.setTransationAmount(transtionRemainAmount.subtract(lastTotal));
|
entity.setTotalAmount(transtionAmount);
|
entity.setConfirmTime(payDate);
|
baseMapper.insert(entity);
|
payContractList.add(contract);
|
break;
|
}
|
}
|
|
payContractList.stream().forEach(contract -> {
|
//更新付款阶段付款
|
List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
|
.eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2)
|
.orderByAsc(ContractPaymentSchedule::getCreateTime));
|
if (ArrayUtil.isNotEmpty(scheduleList.toArray())) {
|
BigDecimal remain = transtionAmount;
|
for (ContractPaymentSchedule schedule : scheduleList) {
|
BigDecimal lastRemain = remain;
|
remain = lastRemain.subtract(StrUtil.equals(schedule.getPaymentStatus() + "", "0") ? schedule.getPlannedAmount() :
|
schedule.getPlannedAmount().subtract(schedule.getActualAmount()));
|
if (remain.compareTo(new BigDecimal("0")) >= 0) {
|
schedule.setActualAmount(schedule.getPlannedAmount());
|
schedule.setPaymentDate(payDate);
|
schedule.setPaymentStatus(2);
|
contractPaymentScheduleMapper.updateById(schedule);
|
//查询是否有当前逾期
|
CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId, schedule.getId())
|
.eq(CurrentOverdue::getContractId, schedule.getContractId()).last("limit 1"));
|
if (ObjUtil.isNotNull(currentOverdue)) {
|
//新增逾期历史
|
HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue, HistoryOverdue.class, "id");
|
historyOverdue.setPaymentTime(payDate);
|
historyOverdue.setCompId(schedule.getCompId());
|
historyOverdueMapper.insert(historyOverdue);
|
//删除当前逾期
|
currentOverdueMapper.deleteById(currentOverdue);
|
}
|
} else {
|
schedule.setActualAmount(StrUtil.equals(schedule.getPaymentStatus() + "", "0") ? schedule.getPlannedAmount().add(remain) :
|
schedule.getActualAmount().add(lastRemain));
|
schedule.setPaymentDate(payDate);
|
schedule.setPaymentStatus(1);
|
contractPaymentScheduleMapper.updateById(schedule);
|
//查询是否有当前逾期
|
CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId, schedule.getId())
|
.eq(CurrentOverdue::getContractId, schedule.getContractId()).last("limit 1"));
|
if (ObjUtil.isNotNull(currentOverdue)) {
|
//新增逾期历史
|
HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue, HistoryOverdue.class, "id");
|
historyOverdue.setReceivableAmount(lastRemain);
|
historyOverdue.setPaymentTime(payDate);
|
historyOverdue.setCompId(schedule.getCompId());
|
historyOverdueMapper.insert(historyOverdue);
|
//查询历史已付逾期金额
|
List<HistoryOverdue> historyOverdueList = historyOverdueMapper.selectList(Wrappers.<HistoryOverdue>lambdaQuery().eq(HistoryOverdue::getScheduleId, currentOverdue.getScheduleId()));
|
BigDecimal hisTotal = historyOverdueList.stream().map(item -> item.getReceivableAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
|
//更新当前逾期
|
currentOverdue.setReceivableAmount(schedule.getPlannedAmount().subtract(hisTotal));
|
currentOverdueMapper.updateById(currentOverdue);
|
}
|
break;
|
}
|
}
|
}
|
//合同所有付款阶段
|
List<ContractPaymentSchedule> payComScheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
|
.eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 0)
|
);
|
if (ArrayUtil.isNotEmpty(payComScheduleList.toArray())){
|
BigDecimal payTotal = payComScheduleList.stream().map(item->StrUtil.equals(item.getPaymentStatus() + "", "0") ? item.getPlannedAmount() :
|
item.getActualAmount()).reduce(BigDecimal.ZERO,BigDecimal::add);
|
contract.setPaidAmount(payTotal);
|
if (payComScheduleList.stream().allMatch(item -> Objects.equals(item.getPaymentStatus(), 2))) {
|
contract.setContractStatus(3);
|
}
|
contractService.updateById(contract);
|
if (contract.getAmount().compareTo(contract.getPaidAmount()) == 0) {
|
payCompleteContractList.add(contract);
|
}
|
//付款 且 付款未全付
|
if (contract.getAmount().compareTo(contract.getPaidAmount()) > 0) {
|
payNoCompleteContractList.add(contract);
|
}
|
}
|
});
|
|
//客户预付有剩余 且 有预付款的合同
|
if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
|
if (ArrayUtil.isNotEmpty(payNoCompleteContractList.toArray())){
|
//余额 放到付款未完成合同 预收
|
Contract lastContract = payNoCompleteContractList.get(0);
|
//保存明细
|
saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
|
}
|
if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isNotEmpty(advanceContractList.toArray())){
|
//客户预付有剩余 没有预付款的合同
|
Contract lastContract = advanceContractList.get(0);
|
//保存明细
|
saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
|
}
|
if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isEmpty(advanceContractList.toArray())
|
&&ArrayUtil.isNotEmpty(payCompleteContractList.toArray())) {
|
//客户预付有剩余 没有预付款的合同
|
Contract lastContract = payCompleteContractList.get(payCompleteContractList.size() - 1);
|
//保存明细
|
saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
|
|
}
|
}
|
}
|
return R.ok();
|
}
|
|
@Override
|
public R syncPaymentRecepit() {
|
String accessToken = "";
|
if (redisTemplate.hasKey("BIP_TOKEN")) {
|
accessToken = (String) redisTemplate.opsForValue().get("BIP_TOKEN");
|
} else {
|
getAccessToken(accessToken);
|
}
|
if (StrUtil.isEmpty(accessToken)) {
|
return R.failed("获取token失败");
|
}
|
String[] bipCodeArr = codes.split(",");
|
for (String bipCode : bipCodeArr) {
|
//入参
|
JSONObject params = genParams(bipCode);
|
String finalAccessToken = accessToken;
|
new Thread(() -> {
|
try {
|
Thread.sleep(500);
|
} catch (InterruptedException e) {
|
throw new RuntimeException(e);
|
}
|
log.info("SK_CX Request:", params.toJSONString());
|
String result = HttpUtil.post(url + "/yonbip/EFI/collection/list?access_token=" + finalAccessToken, params.toJSONString());
|
log.info("SK_CX Response:{}", result);
|
//保存请求记录
|
saveBipRequestRecord(params, result);
|
JSONObject resultJson = JSONObject.parseObject(result);
|
if (resultJson.containsKey("code") && resultJson.getString("code").equals("200")) {
|
handleAndSave(bipCode, resultJson);
|
}
|
|
}).start();
|
}
|
return R.ok();
|
}
|
|
private JSONObject genParams(String bipCode){
|
JSONObject params = new JSONObject();
|
params.put("pageIndex", 1);
|
params.put("pageSize", 999);
|
params.put("open_billDate_begin", DateUtil.offsetDay(new Date(), -1) + " 00:00:00");
|
params.put("open_billDate_begin", DateUtil.today() + " 00:00:00");
|
//单据状态已审批
|
params.put("verifyState", new String[]{"2"});
|
//往来对象客户
|
params.put("objectType", new String[]{"1"});
|
//单位
|
JSONObject simpleMap = new JSONObject();
|
simpleMap.put("financeOrg.code", bipCode);
|
params.put("simple", simpleMap);
|
return params;
|
}
|
|
private void getAccessToken(String accessToken) {
|
long timestamp = System.currentTimeMillis();
|
Map<String, Object> paramMap = new HashMap<>();
|
paramMap.put("appKey", appKey);
|
paramMap.put("timestamp", timestamp);
|
String signature = null;
|
try {
|
signature = SignHelper.sign(paramMap, appSecret);
|
} catch (NoSuchAlgorithmException e) {
|
throw new RuntimeException(e);
|
} catch (UnsupportedEncodingException e) {
|
throw new RuntimeException(e);
|
} catch (InvalidKeyException e) {
|
throw new RuntimeException(e);
|
}
|
String getTokenResult = HttpUtil.get(url + "/iuap-api-auth/open-auth/selfAppAuth/getAccessToken?appKey=" + appKey + "×tamp=" + timestamp
|
+ "&signature=" + signature);
|
log.info("Get access_token Response:", getTokenResult);
|
JSONObject tokenJson = JSONObject.parseObject(getTokenResult);
|
if (!tokenJson.containsKey("code") || !tokenJson.getString("code").equals("200")) {
|
log.error("Get access_token ERROR");
|
return;
|
}
|
if (ObjUtil.isNull(tokenJson.get("data")) || ObjUtil.isNull(JSONObject.parseObject(tokenJson.getString("data")))) {
|
log.error("access_token value exception");
|
return;
|
}
|
accessToken = JSONObject.parseObject(tokenJson.getString("data")).getString("access_token");
|
log.info("access_token value:", accessToken);
|
redisTemplate.opsForValue().set("BIP_TOKEN", accessToken, tokenJson.getIntValue("expire") - 10, TimeUnit.SECONDS);
|
}
|
|
private void saveBipRequestRecord(JSONObject params, String result) {
|
BipRequestRecord record = new BipRequestRecord();
|
record.setInterfaceName("<XSJZ_SK_CX_001>-收款单查询接口");
|
record.setRequestParams(params.toJSONString());
|
record.setResponseParams(result);
|
record.setResponseCode(JSONObject.parseObject(result).getString("code"));
|
bipRequestRecordMapper.insert(record);
|
}
|
|
private void handleAndSave(String bipCode, JSONObject resultJson) {
|
R<SysDept> r = remoteDeptService.selectDeptByBipCode(bipCode);
|
if (!r.isOk()) {
|
log.error("bipcode查询单位失败");
|
return;
|
}
|
SysDept dept = r.getData();
|
JSONObject dataJson = JSONObject.parseObject(resultJson.getString("data"));
|
if (ObjUtil.isNull(dataJson)) {
|
log.error("返回结果中data解析失败");
|
return;
|
}
|
JSONArray recordList = JSONArray.parseArray(resultJson.getString("recordList"));
|
if (ArrayUtil.isEmpty(recordList.toArray())) {
|
log.error("返回结果中recordList为空");
|
return;
|
}
|
log.info("==================开始处理收款单信息===================");
|
recordList.stream().forEach(record -> {
|
|
savePaymentConfirm((JSONObject) record, dept);
|
});
|
log.info("==================结束处理收款单信息===================");
|
}
|
|
private void savePaymentConfirm(JSONObject record, SysDept dept) {
|
|
List<Contract> contractList = contractService.list(Wrappers.<Contract>lambdaQuery().eq(Contract::getPartyA, record.getString("bodyItemCustomerName"))
|
.eq(Contract::getPartyBId, dept.getDeptId()).orderByAsc(Contract::getCreateTime));
|
if (ArrayUtil.isEmpty(contractList.toArray())) {
|
log.error("付款单id:{},甲方:{},乙方:{},无相关合同", record.getString("id"), record.getString("bodyItemCustomerName"), dept.getName());
|
return;
|
}
|
BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
|
.eq(StrUtil.isNotBlank(record.getString("bodyItemCustomerName")), BusinessCustomer::getCompanyName, record.getString("bodyItemCustomerName"))
|
);
|
if (ObjUtil.isNull(customer)) {
|
log.error("付款单id:{},客商查询失败,请检查客商", record.getString("id"));
|
return;
|
}
|
if (ObjUtil.isNull(record.getBigDecimal("bodyItemOriTaxExcludedAmount"))) {
|
log.error("付款单id:{},付款单无税金额,异常", record.getString("id"));
|
return;
|
}
|
//保存付款单
|
Boolean codeFlag = savePaymentSlip(customer,record);
|
if (codeFlag){
|
log.error("付款单id:{},付款单已存在");
|
return;
|
}
|
BigDecimal transtionAmount = record.getBigDecimal("bodyItemOriTaxExcludedAmount");
|
//剩余付款
|
BigDecimal transtionRemainAmount = transtionAmount;
|
//收到付款的合同
|
List<Contract> payContractList = new ArrayList<>();
|
//付款完成的合同
|
List<Contract> payCompleteContractList = new ArrayList<>();
|
//付款未完成的合同
|
List<Contract> payNoCompleteContractList = new ArrayList<>();
|
//有预付款的合同
|
List<Contract> advanceContractList = new ArrayList<>();
|
//付款时间
|
Date payDate = DateUtil.parse(record.getString("billDate"), DatePattern.NORM_DATETIME_FORMAT);
|
for (Contract contract : contractList) {
|
PaymentConfirm lastConfirm = baseMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
|
.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
|
BigDecimal lastTotal = new BigDecimal("0");
|
if (ObjUtil.isNotNull(lastConfirm)) {
|
lastTotal = lastConfirm.getTotalAmount();
|
}
|
|
//合同最近明细总额为0 或 > 0 情况下
|
if (lastTotal.compareTo(new BigDecimal("0")) >= 0) {
|
log.info("当前合同号:{},明细总额无应收款", contract.getContractNo());
|
advanceContractList.add(contract);
|
continue;
|
}
|
|
transtionRemainAmount = transtionRemainAmount.add(lastTotal);
|
PaymentConfirm entity = new PaymentConfirm();
|
entity.setContractId(contract.getId());
|
entity.setContractName(contract.getContractName());
|
entity.setContractNo(contract.getContractNo());
|
if (ObjUtil.isNotNull(lastConfirm)) {
|
entity.setScheduleId(lastConfirm.getScheduleId());
|
entity.setScheduleName(lastConfirm.getScheduleName());
|
}
|
entity.setBusGuestId(customer.getId());
|
entity.setBusGuestName(customer.getCompanyName());
|
entity.setBusinessType("客户付款");
|
entity.setIsBip(1);
|
entity.setPaymentCode(record.getString("code"));
|
entity.setPayType(record.get("bodyItemSettleModeName") != null ? record.getString("bodyItemSettleModeName").contains("承兑汇票") ? 1 : 0 : null);
|
entity.setBipId(record.getString("id"));
|
if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
|
entity.setTransationAmount(lastTotal.multiply(new BigDecimal("-1")));
|
entity.setTotalAmount(new BigDecimal("0"));
|
entity.setConfirmTime(payDate);
|
baseMapper.insert(entity);
|
} else if (transtionRemainAmount.compareTo(new BigDecimal("0")) == 0) {
|
entity.setTransationAmount(lastTotal.multiply(new BigDecimal("-1")));
|
entity.setTotalAmount(new BigDecimal("0"));
|
entity.setConfirmTime(payDate);
|
baseMapper.insert(entity);
|
break;
|
} else {
|
entity.setTransationAmount(transtionRemainAmount.subtract(lastTotal));
|
entity.setTotalAmount(transtionAmount);
|
entity.setConfirmTime(payDate);
|
baseMapper.insert(entity);
|
break;
|
}
|
}
|
|
payContractList.stream().forEach(contract -> {
|
//更新付款阶段付款
|
List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
|
.eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2)
|
.orderByAsc(ContractPaymentSchedule::getCreateTime));
|
if (ArrayUtil.isNotEmpty(scheduleList.toArray())) {
|
BigDecimal remain = transtionAmount;
|
for (ContractPaymentSchedule schedule : scheduleList) {
|
BigDecimal lastRemain = remain;
|
remain = remain.subtract(StrUtil.equals(schedule.getPaymentStatus() + "", "0") ? schedule.getPlannedAmount() :
|
schedule.getPlannedAmount().subtract(schedule.getActualAmount()));
|
if (remain.compareTo(new BigDecimal("0")) >= 0) {
|
schedule.setActualAmount(schedule.getPlannedAmount());
|
schedule.setPaymentDate(payDate);
|
schedule.setPaymentStatus(2);
|
contractPaymentScheduleMapper.updateById(schedule);
|
//查询是否有当前逾期
|
CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId, schedule.getId())
|
.eq(CurrentOverdue::getContractId, schedule.getContractId()).last("limit 1"));
|
if (ObjUtil.isNotNull(currentOverdue)) {
|
//新增逾期历史
|
HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue, HistoryOverdue.class, "id");
|
historyOverdue.setPaymentTime(payDate);
|
historyOverdue.setCompId(schedule.getCompId());
|
historyOverdueMapper.insert(historyOverdue);
|
//删除当前逾期
|
currentOverdueMapper.deleteById(currentOverdue);
|
}
|
} else {
|
schedule.setActualAmount(StrUtil.equals(schedule.getPaymentStatus() + "", "0") ? schedule.getPlannedAmount().add(remain) :
|
schedule.getActualAmount().add(lastRemain));
|
schedule.setPaymentDate(payDate);
|
schedule.setPaymentStatus(1);
|
contractPaymentScheduleMapper.updateById(schedule);
|
//查询是否有当前逾期
|
CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId, schedule.getId())
|
.eq(CurrentOverdue::getContractId, schedule.getContractId()).last("limit 1"));
|
if (ObjUtil.isNotNull(currentOverdue)) {
|
//新增逾期历史
|
HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue, HistoryOverdue.class, "id");
|
historyOverdue.setReceivableAmount(lastRemain);
|
historyOverdue.setPaymentTime(payDate);
|
historyOverdue.setCompId(schedule.getCompId());
|
historyOverdueMapper.insert(historyOverdue);
|
//查询历史已付逾期金额
|
List<HistoryOverdue> historyOverdueList = historyOverdueMapper.selectList(Wrappers.<HistoryOverdue>lambdaQuery().eq(HistoryOverdue::getScheduleId, currentOverdue.getScheduleId()));
|
BigDecimal hisTotal = historyOverdueList.stream().map(item -> item.getReceivableAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
|
//更新当前逾期
|
currentOverdue.setReceivableAmount(schedule.getPlannedAmount().subtract(hisTotal));
|
currentOverdueMapper.updateById(currentOverdue);
|
}
|
break;
|
}
|
}
|
}
|
//合同所有付款阶段
|
List<ContractPaymentSchedule> payComScheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
|
.eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 0)
|
);
|
if (ArrayUtil.isNotEmpty(payComScheduleList.toArray())){
|
BigDecimal payTotal = payComScheduleList.stream().map(item->StrUtil.equals(item.getPaymentStatus() + "", "0") ? item.getPlannedAmount() :
|
item.getActualAmount()).reduce(BigDecimal.ZERO,BigDecimal::add);
|
contract.setPaidAmount(payTotal);
|
if (payComScheduleList.stream().allMatch(item -> Objects.equals(item.getPaymentStatus(), 2))) {
|
contract.setContractStatus(3);
|
}
|
contractService.updateById(contract);
|
if (contract.getAmount().compareTo(contract.getPaidAmount()) == 0) {
|
payCompleteContractList.add(contract);
|
}
|
//付款 且 付款未全付
|
if (contract.getAmount().compareTo(contract.getPaidAmount()) > 0) {
|
payNoCompleteContractList.add(contract);
|
}
|
}
|
});
|
|
//客户预付有剩余 且 有预付款的合同
|
if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
|
if (ArrayUtil.isNotEmpty(payNoCompleteContractList.toArray())){
|
//余额 放到付款未完成合同 预收
|
Contract lastContract = payNoCompleteContractList.get(payNoCompleteContractList.size() - 1);
|
//保存明细
|
saveNewPaymentConfirm(customer,record,lastContract,transtionRemainAmount,payDate);
|
}
|
if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isNotEmpty(advanceContractList.toArray())){
|
//客户预付有剩余 没有预付款的合同
|
Contract lastContract = payNoCompleteContractList.get(payNoCompleteContractList.size() - 1);
|
//保存明细
|
saveNewPaymentConfirm(customer,record,lastContract,transtionRemainAmount,payDate);
|
}
|
if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isEmpty(advanceContractList.toArray())
|
&&ArrayUtil.isNotEmpty(payCompleteContractList.toArray())) {
|
//客户预付有剩余 没有预付款的合同
|
Contract lastContract = payCompleteContractList.get(payCompleteContractList.size() - 1);
|
//保存明细
|
saveNewPaymentConfirm(customer,record,lastContract,transtionRemainAmount,payDate);
|
|
}
|
}
|
}
|
|
private Boolean savePaymentSlip(BusinessCustomer customer, JSONObject record) {
|
PaymentSlip slip = new PaymentSlip();
|
slip.setBusGuestId(customer.getId());
|
slip.setBusGuestName(customer.getCompanyName());
|
slip.setPaymentTime(ObjUtil.isNotNull(record.get("billDate"))?
|
DateUtil.parse(record.getString("billDate"), DatePattern.NORM_DATETIME_FORMAT):new Date());
|
slip.setPaymentAmount(ObjUtil.isNotNull(record.get("bodyItemOriTaxExcludedAmount"))?record.getBigDecimal("bodyItemOriTaxExcludedAmount"):new BigDecimal("0"));
|
slip.setPayType(ObjUtil.isNotNull(record.get("bodyItemSettleModeName"))?record.getString("bodyItemSettleModeName"):"");
|
if (ObjUtil.isNotNull(record.get("code"))){
|
slip.setPaymentNumber(record.getString("code"));
|
PaymentSlip paymentSlip = paymentSlipMapper.selectOne(Wrappers.<PaymentSlip>lambdaQuery().eq(PaymentSlip::getPaymentNumber,slip.getPaymentNumber()));
|
if (ObjUtil.isNull(paymentSlip)) {
|
paymentSlipMapper.insert(slip);
|
return false;
|
}
|
}
|
return true;
|
}
|
|
public void saveNewPaymentConfirm(BusinessCustomer customer,JSONObject record,Contract contract,BigDecimal transtionRemainAmount
|
,Date payDate){
|
PaymentConfirm entity = new PaymentConfirm();
|
entity.setContractId(contract.getId());
|
entity.setContractName(contract.getContractName());
|
entity.setContractNo(contract.getContractNo());
|
PaymentConfirm lastConfirm = baseMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
|
.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
|
BigDecimal lastTotal = new BigDecimal("0");
|
if (ObjUtil.isNotNull(lastConfirm)) {
|
lastTotal = lastConfirm.getTotalAmount();
|
}
|
|
if (ObjUtil.isNotNull(lastConfirm)) {
|
entity.setScheduleId(lastConfirm.getScheduleId());
|
entity.setScheduleName(lastConfirm.getScheduleName());
|
}
|
|
entity.setBusGuestId(customer.getId());
|
entity.setBusGuestName(customer.getCompanyName());
|
entity.setBusinessType("客户付款");
|
if (ObjUtil.isNotNull(record)){
|
entity.setIsBip(1);
|
entity.setPaymentCode(record.getString("code"));
|
entity.setPayType(record.get("bodyItemSettleModeName") != null ? record.getString("bodyItemSettleModeName").contains("承兑汇票") ? 1 : 0 : null);
|
entity.setBipId(record.getString("id"));
|
}
|
entity.setConfirmTime(payDate);
|
entity.setTransationAmount(transtionRemainAmount);
|
entity.setTotalAmount(transtionRemainAmount.add(lastTotal));
|
if (entity.getTotalAmount().compareTo(new BigDecimal("0"))>0){
|
entity.setAdvanceAmount(entity.getTotalAmount());
|
}else if (entity.getTotalAmount().compareTo(new BigDecimal("0"))<0){
|
entity.setReceivableAmount(entity.getTotalAmount().multiply(new BigDecimal("-1")));
|
}
|
baseMapper.insert(entity);
|
}
|
}
|