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
127
128
129
130
131
132
133
package com.by4cloud.platformx.business.service.impl;
 
import cn.hutool.core.date.DateUtil;
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.extension.service.impl.ServiceImpl;
import com.by4cloud.platformx.business.dto.BankStatementUpdateDTO;
import com.by4cloud.platformx.business.dto.PaymentConfirmAddDTO;
import com.by4cloud.platformx.business.entity.BankStatement;
import com.by4cloud.platformx.business.entity.BusinessCustomer;
import com.by4cloud.platformx.business.entity.Contract;
import com.by4cloud.platformx.business.mapper.BankStatementMapper;
import com.by4cloud.platformx.business.mapper.BusinessCustomerMapper;
import com.by4cloud.platformx.business.mapper.ContractMapper;
import com.by4cloud.platformx.business.service.BankStatementService;
import com.by4cloud.platformx.business.service.PaymentConfirmService;
import com.by4cloud.platformx.common.core.util.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
/**
 * 银行流水
 *
 * @author syt
 * @date 2026-08-07 16:10:36
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BankStatementServiceImpl extends ServiceImpl<BankStatementMapper, BankStatement> implements BankStatementService {
 
    private final PaymentConfirmService paymentConfirmService;
    private final ContractMapper contractMapper;
    private final BusinessCustomerMapper businessCustomerMapper;
    private final StringRedisTemplate redisTemplate;
 
    @Value("${bip.url}")
    private String url;
 
    @Override
    public R edit(BankStatementUpdateDTO updateDTO) {
        BankStatement statement = baseMapper.selectById(updateDTO.getId());
        Contract contract = contractMapper.selectById(updateDTO.getContractId());
        if (ObjUtil.isNull(contract)){
            return R.failed("合同查询失败");
        }
        PaymentConfirmAddDTO addDTO = new PaymentConfirmAddDTO();
        addDTO.setBusGuestId(statement.getBusGuestId()+"");
        addDTO.setConfirmTime(statement.getTranDate());
        addDTO.setContractNo(contract.getContractNo());
        addDTO.setInOrOut("1");
        addDTO.setTransationAmount(statement.getTranAmount());
        R r = paymentConfirmService.add(addDTO);
        if (!r.isOk()){
            return r;
        }
        statement.setContractId(updateDTO.getContractId());
        baseMapper.updateById(statement);
 
        //bip推送
        pushSaveCollection(statement);
        return R.ok();
    }
 
    private void pushSaveCollection(BankStatement statement) {
        String accessToken = "";
        if (redisTemplate.hasKey("BIP_TOKEN")) {
            accessToken = (String) redisTemplate.opsForValue().get("BIP_TOKEN");
        } else {
            paymentConfirmService.getAccessToken(accessToken);
        }
        if (StrUtil.isEmpty(accessToken)) {
            log.error("bip accessToken 获取异常");
            return;
        }
        String finalAccessToken = accessToken;
        JSONObject params = genRequestParam(statement);
 
        log.info("收款单集成 Request:{}", params.toJSONString());
        String result = HttpUtil.post(url + "/yonbip/EFI/receivable/save?access_token=" + finalAccessToken, params.toJSONString());
        log.info("收款单集成 Response:{}", result);
 
    }
 
    private JSONObject genRequestParam(BankStatement statement) {
        BusinessCustomer customer = businessCustomerMapper.selectById(statement.getBusGuestId());
        Contract contract = contractMapper.selectById(statement.getContractId());
        JSONObject req = new JSONObject();
 
        JSONObject data = new JSONObject();
 
 
        JSONObject freeCh = new JSONObject();
        freeCh.put("YSYF57","MJHYXGLXT");
 
        data.put("freeChId",freeCh);
        data.put("extVouchCode",statement.getBankSeqNo());
        data.put("billDate",statement.getTranDate());
        data.put("objectType","1");
        data.put("customerCode",customer.getCreditCode());
        data.put("exchangeRate",1);
        data.put("exchangeRateDate", DateUtil.now());
        data.put("contractNo",contract.getContractNo());
 
        JSONArray bodyItems = new JSONArray();
 
        JSONObject bodyItem = new JSONObject();
        bodyItem.put("srcBillType","38");
        bodyItem.put("srcBillNo",statement.getBankSeqNo());
 
        JSONObject freeChItem = new JSONObject();
        freeChItem.put("YSZJX01A","业务资金预算项目");
        freeChItem.put("YSZJX05","");
        freeChItem.put("YSZJX11","2146820445301112836");
        freeChItem.put("zzbkfl","");
        bodyItem.put("freeChId",freeChItem);
 
        bodyItems.add(bodyItem);
        data.put("bodyItem",bodyItems);
 
        data.put("_status","Insert");
        data.put("oriTaxIncludedAmount",statement.getTranAmount());
 
        req.put("data",data);
        return req;
 
    }
}