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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package com.by4cloud.platformx.business.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
 
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by4cloud.platformx.business.dto.BusinessCustomerAddDTO;
import com.by4cloud.platformx.business.entity.BusinessCustomer;
import com.by4cloud.platformx.business.entity.Contract;
import com.by4cloud.platformx.business.mapper.BipBanklViewMapper;
import com.by4cloud.platformx.business.mapper.BipViewMapper;
import com.by4cloud.platformx.business.mapper.BusinessCustomerMapper;
import com.by4cloud.platformx.business.mapper.ContractMapper;
import com.by4cloud.platformx.business.service.BusinessCustomerService;
import com.by4cloud.platformx.business.vo.*;
import com.by4cloud.platformx.common.core.util.R;
import com.by4cloud.platformx.common.excel.vo.ErrorMessage;
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.stereotype.Service;
import org.springframework.validation.BindingResult;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
/**
 * 客商信息
 *
 * @author platformx
 * @date 2026-05-07 10:13:22
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BusinessCustomerServiceImpl extends ServiceImpl<BusinessCustomerMapper, BusinessCustomer> implements BusinessCustomerService {
 
    @Value("${dept.smj}")
    private String smj;
 
    @Value("${dept.smjhw}")
    private String smjhw;
 
    private final BipViewMapper bipViewMapper;
    private final BipBanklViewMapper bipBanklViewMapper;
    private final ContractMapper contractMapper;
 
    @Override
    public R syncMainBusCus(String compId) {
        List<BusinessCustomer> bcList = baseMapper.selectList(Wrappers.<BusinessCustomer>lambdaQuery()
                .eq(StrUtil.isNotEmpty(compId),BusinessCustomer::getCompId,compId)
                .and(wrapper -> wrapper.isNull(BusinessCustomer::getCreditCode)
                .or().isNull(BusinessCustomer::getLegalPerson)
                .or().isNull(BusinessCustomer::getBankName)
                .or().isNull(BusinessCustomer::getBankAccount)
                .or().isNull(BusinessCustomer::getRegisterName)
                .or().isNull(BusinessCustomer::getBipCode))
        );
        if (CollUtil.isNotEmpty(bcList)){
            for (BusinessCustomer bc:bcList
                 ) {
                log.info("开始同步{}信息",bc.getCompanyName());
                CustInfoVo custInfo = bipViewMapper.selectBaseInfoByCustName(bc.getCompanyName());
                if (ObjUtil.isNull(custInfo)){
                    continue;
                }
                if (ObjUtil.isNotNull(custInfo)){
                    bc.setCreditCode(custInfo.getCertificateId());
                    bc.setRegisterName(custInfo.getCustName());
                    bc.setLegalPerson(custInfo.getLegalPerson());
                    bc.setAddress(custInfo.getRegistrationAddress());
                    bc.setBipCode(custInfo.getCustCode());
                    baseMapper.updateById(bc);
                    log.info("{}同步名称、法人、地址、信用码、bipcode信息成功",bc.getCompanyName());
                }
                List<CustBankVo> bankVoList = bipViewMapper.selectBankByCustName(bc.getCompanyName());
                if (CollUtil.isEmpty(bankVoList)){
                    continue;
                }
                if (CollUtil.isNotEmpty(bankVoList)){
                    if (StrUtil.isEmpty(bc.getBankAccount())){
                        bc.setBankAccount(bankVoList.get(0).getBankAccount());
                        baseMapper.updateById(bc);
                        log.info("{}同步银行卡号信息成功",bc.getCompanyName());
                    }
                    List<BankNameVo> bankNameVoList = bipBanklViewMapper.selectBankByCode(bankVoList.get(0).getBankName());
                    if (CollUtil.isNotEmpty(bankNameVoList)){
                        bc.setBankName(bankNameVoList.get(0).getBankNameName());
                        baseMapper.updateById(bc);
                        log.info("{}同步银行名称信息成功",bc.getCompanyName());
                    }
                }
 
            }
        }
        return R.ok();
    }
 
    @Override
    public BusinessCustomerMainVo queryMainBC(String companyName) {
        BusinessCustomerMainVo bc = new BusinessCustomerMainVo();
        bc.setCompanyName(companyName);
        log.info("主数据查询{}信息",companyName);
        CustInfoVo custInfo = bipViewMapper.selectBaseInfoByCustName(companyName);
        if (ObjUtil.isNotNull(custInfo)){
            bc.setCreditCode(custInfo.getCertificateId());
            bc.setRegisterName(custInfo.getCustName());
            bc.setLegalPerson(custInfo.getLegalPerson());
            bc.setAddress(custInfo.getRegistrationAddress());
            log.info("{}主数据查询名称、法人、地址、信用码信息成功",companyName);
        }
        List<CustBankVo> bankVoList = bipViewMapper.selectBankByCustName(bc.getCompanyName());
        if (CollUtil.isNotEmpty(bankVoList)){
            if (StrUtil.isEmpty(bc.getBankAccount())){
                bc.setBankAccount(bankVoList.get(0).getBankAccount());
                log.info("{}主数据查询银行卡号信息成功",companyName);
            }
            List<BankNameVo> bankNameVoList = bipBanklViewMapper.selectBankByCode(bankVoList.get(0).getBankName());
            if (CollUtil.isNotEmpty(bankNameVoList)){
                bc.setBankName(bankNameVoList.get(0).getBankNameName());
                log.info("{}主数据查询银行名称信息成功",companyName);
            }
        }
        return bc;
    }
 
    @Override
    public R saveCustomerSgb(BusinessCustomerAddDTO addDTO) {
        if (StrUtil.isBlank(addDTO.getCompanyName())||StrUtil.isBlank(addDTO.getCreditCode())){
            return R.failed("客商名称或社会信用代码不能为空");
        }
        BusinessCustomer bc = baseMapper.selectOne(Wrappers.<BusinessCustomer>lambdaQuery()
                .eq(BusinessCustomer::getCreditCode,addDTO.getCreditCode())
                .eq(BusinessCustomer::getCompId, SecurityUtils.getUser().getCompId())
                .last("limit 1"));
        if (ObjUtil.isNotNull(bc)){
            return R.failed("客商已存在");
        }
        BusinessCustomer customer = new BusinessCustomer();
        customer.setErpCompanyCode(addDTO.getErpCompanyCode());
        log.info("主数据查询{}信息",addDTO.getCompanyName());
        CustInfoVo custInfo = bipViewMapper.selectBaseInfoByCustName(addDTO.getCompanyName());
        if (ObjUtil.isNotNull(custInfo)){
            if (!addDTO.getCreditCode().equals(custInfo.getCertificateId())){
                return R.failed("主数据社会信用代码与新增的不一致");
            }
            customer.setCompanyName(addDTO.getCompanyName());
            customer.setCreditCode(custInfo.getCertificateId());
            customer.setRegisterName(custInfo.getCustName());
            customer.setLegalPerson(custInfo.getLegalPerson());
            customer.setAddress(custInfo.getRegistrationAddress());
            customer.setProvince(customer.getCreditCode().substring(2,4));
            if (StrUtil.isNotEmpty(addDTO.getSalesModel())&& addDTO.getSalesModel().matches("[0-9]+")){
                customer.setSalesModel(Long.valueOf(addDTO.getSalesModel()));
            }
            if (StrUtil.isNotEmpty(addDTO.getExportServiceAttr())&& addDTO.getExportServiceAttr().matches("[0-9]+")){
                customer.setExportServiceAttr(Long.valueOf(addDTO.getExportServiceAttr()));
            }
            if (StrUtil.isNotEmpty(addDTO.getClassId())&& addDTO.getClassId().matches("[0-9]+")){
                customer.setClassId(Long.valueOf(addDTO.getClassId()));
            }
            customer.setInvoiceType("01");
            log.info("{}主数据查询名称、法人、地址、信用码信息成功",addDTO.getCompanyName());
            List<CustBankVo> bankVoList = bipViewMapper.selectBankByCustName(addDTO.getCompanyName());
            if (CollUtil.isNotEmpty(bankVoList)){
                customer.setBankAccount(bankVoList.get(0).getBankAccount());
                log.info("{}主数据查询银行卡号信息成功",addDTO.getCompanyName());
                List<BankNameVo> bankNameVoList = bipBanklViewMapper.selectBankByCode(bankVoList.get(0).getBankName());
                if (CollUtil.isNotEmpty(bankNameVoList)){
                    customer.setBankName(bankNameVoList.get(0).getBankNameName());
                    log.info("{}主数据查询银行名称信息成功",addDTO.getCompanyName());
                }
            }
            customer.setRegisterName(custInfo.getCustName());
            customer.setBipCode(custInfo.getCertificateId());
            baseMapper.insert(customer);
            return R.ok();
        }
        return R.failed("主数据客商信息不存在");
    }
 
    @Override
    public R importBusinessCustomer(List<BusinessCustomerImportVo> excelVOList, BindingResult bindingResult) {
        List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();
        if (CollUtil.isNotEmpty(excelVOList)) {
            Long line = 0L;
            for (BusinessCustomerImportVo excelVO : excelVOList
            ) {
                Set<String> errorMsg = new HashSet<>();
                if (StrUtil.isBlank(excelVO.getCreditCode())||StrUtil.isBlank(excelVO.getErpCompanyCode())){
                    errorMsg.add(excelVO.getCompanyName()+"社会信用代码或ERP客户编码不能为空");
                    errorMessageList.add(new ErrorMessage(line++, errorMsg));
                    continue;
                }
                BusinessCustomer customer = null;
                if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smj) || StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjhw)) {
                    List<String> smjList = new ArrayList<>();
                    smjList.add(smj);
                    smjList.add(smjhw);
                    customer = baseMapper.selectOne(Wrappers.<BusinessCustomer>lambdaQuery()
                            .eq(BusinessCustomer::getCreditCode, excelVO.getCreditCode())
                            .in(BusinessCustomer::getCompId, smjList)
                            .last("limit 1"));
                }else {
                    customer = baseMapper.selectOne(Wrappers.<BusinessCustomer>lambdaQuery()
                            .eq(BusinessCustomer::getCreditCode, excelVO.getCreditCode())
                            .last("limit 1"));
                }
                if (ObjUtil.isNull(customer)) {
                    customer = BeanUtil.copyProperties(excelVO, BusinessCustomer.class);
                    CustInfoVo custInfo = bipViewMapper.selectBaseInfoByCertId(excelVO.getCreditCode());
                    if (ObjUtil.isNotNull(custInfo)) {
                        customer.setCreditCode(custInfo.getCertificateId());
                        customer.setRegisterName(custInfo.getCustName());
                        customer.setLegalPerson(custInfo.getLegalPerson());
                        customer.setAddress(custInfo.getRegistrationAddress());
                        customer.setProvince(customer.getCreditCode().substring(2, 4));
                        customer.setInvoiceType("01");
                        log.info("{}主数据查询名称、法人、地址、信用码信息成功", excelVO.getCompanyName());
                        List<CustBankVo> bankVoList = bipViewMapper.selectBankByCustName(excelVO.getCompanyName());
                        if (CollUtil.isNotEmpty(bankVoList)) {
                            customer.setBankAccount(bankVoList.get(0).getBankAccount());
                            log.info("{}主数据查询银行卡号信息成功", excelVO.getCompanyName());
                            List<BankNameVo> bankNameVoList = bipBanklViewMapper.selectBankByCode(bankVoList.get(0).getBankName());
                            if (CollUtil.isNotEmpty(bankNameVoList)) {
                                customer.setBankName(bankNameVoList.get(0).getBankNameName());
                                log.info("{}主数据查询银行名称信息成功", excelVO.getCompanyName());
                            }
                        }
                        customer.setRegisterName(custInfo.getCustName());
                        baseMapper.insert(customer);
                    }
                }
            }
        }
        return R.ok();
    }
 
    @Override
    public R syncCustomer(Long id) {
        BusinessCustomer customer = baseMapper.selectById(id);
        if (ObjUtil.isNull(customer)||StrUtil.isBlank(customer.getCreditCode())){
            return R.failed("客户信息异常");
        }
        CustInfoVo custInfo = bipViewMapper.selectBaseInfoByCertId(customer.getCreditCode());
        if (ObjUtil.isNull(custInfo)) {
            return R.failed("主数据无该客户信息,无法更新");
        }
        customer.setCreditCode(custInfo.getCertificateId());
        baseMapper.updateById(customer);
        List<Contract> contractList = contractMapper.selectList(Wrappers.<Contract>lambdaQuery()
                .eq(Contract::getPartyAId,id)
                .eq(Contract::getContractStatus,2));
        if (CollUtil.isNotEmpty(contractList)){
            contractList.stream().forEach(contract -> {
                contract.setPartyA(customer.getCompanyName());
                contractMapper.updateById(contract);
            });
        }
        return R.ok();
    }
}