sen
4 天以前 5627248e473253b3f10615d6be8b27bb7cc7c4f7
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package com.ruoyi.tms.service.impl;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.SystemDataNoEnum;
import com.ruoyi.common.utils.DateUtils;
import javax.annotation.Resource;
 
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISystemDataNoService;
import com.ruoyi.tms.domain.*;
import com.ruoyi.tms.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.client.RestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.core.service.BaseService;
 
import com.ruoyi.tms.service.ITmsPayableFeeService;
import com.ruoyi.common.core.text.Convert;
 
/**
 * 应付费用Service业务层处理
 *
 * @author ruoyi
 * @date 2026-01-12
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TmsPayableFeeServiceImpl  extends BaseService<TmsPayableFeeMapper, TmsPayableFee> implements ITmsPayableFeeService
{
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private TmsPayableFeeMapper tmsPayableFeeMapper;
    @Resource
    private TmsDispatchOrderMapper tmsDispatchOrderMapper;
 
    @Resource
    private TmsPayableFeeItemMapper tmsPayableFeeItemMapper;
    @Resource
    private TmsApBillMapper tmsApBillMapper;
    @Resource
    private TmsApBillItemMapper tmsApBillItemMapper;
 
 
    @Resource
    ISysConfigService sysConfigService;
    @Autowired
    ISystemDataNoService systemDataNoService;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${custom.cwxtApi.url}")
    private String url;
    /**
     * 查询应付费用
     *
     * @param id 应付费用ID
     * @return 应付费用
     */
    @DataSource(DataSourceType.SLAVE)
    @Override
    public TmsPayableFee selectTmsPayableFeeById(Integer id)
    {
        TmsPayableFee tmsPayableFee = tmsPayableFeeMapper.selectTmsPayableFeeById(id);
        List<TmsPayableFeeItem> tmsPayableFeeItems = tmsPayableFeeItemMapper.selectTmsPayableFeeItemList(
                new TmsPayableFeeItem() {{
                    setHeadId(id);
                }}
        );
        tmsPayableFee.setPayableFeeItems(tmsPayableFeeItems);
        return tmsPayableFee;
    }
 
    @DataSource(DataSourceType.SLAVE)
    @Override
    public List<TmsPayableFee> selectTmsPayableFeeByDispatchNo(String dispatchNo)
    {
        List<TmsPayableFee> tmsPayableFeeList = tmsPayableFeeMapper.selectList(new LambdaQueryWrapper<TmsPayableFee>()
                .eq(TmsPayableFee::getDispatchNo, dispatchNo)
                .ne(TmsPayableFee::getStatus, 2)
        );
 
        if (tmsPayableFeeList != null && !tmsPayableFeeList.isEmpty()){
            tmsPayableFeeList.forEach(tmsPayableFee -> {
                List<TmsPayableFeeItem> tmsPayableFeeItems = tmsPayableFeeItemMapper.selectTmsPayableFeeItemList(
                        new TmsPayableFeeItem() {{
                            setHeadId(tmsPayableFee.getId());
                        }}
                );
                tmsPayableFee.setPayableFeeItems(tmsPayableFeeItems);
            });
        }
        return tmsPayableFeeList;
    }
 
    /**
     * 查询应付费用 记录数
     *
     * @param tmsPayableFee 应付费用
     * @return 应付费用集合
     */
    @DataSource(DataSourceType.SLAVE)
    @Override
    public int selectTmsPayableFeeCount(TmsPayableFee tmsPayableFee)
    {
        return tmsPayableFeeMapper.selectTmsPayableFeeCount(tmsPayableFee);
    }
 
    /**
     * 查询应付费用列表
     *
     * @param tmsPayableFee 应付费用
     * @return 应付费用
     */
    @DataSource(DataSourceType.SLAVE)
    @Override
    public List<TmsPayableFee> selectTmsPayableFeeList(TmsPayableFee tmsPayableFee)
    {
        return tmsPayableFeeMapper.selectTmsPayableFeeList(tmsPayableFee);
    }
 
    /**
     * 查询应付费用列表 异步 导出
     *
     * @param tmsPayableFee 应付费用
     * @param exportKey 导出功能的唯一标识
     * @return 应付费用集合
     */
    @DataSource(DataSourceType.SLAVE)
    @Async
    @Override
    public void export(TmsPayableFee tmsPayableFee,String exportKey) {
 
        super.export(TmsPayableFee.class,exportKey,"tmsPayableFeeData",(pageNum)->{
            PageUtils.startPage(pageNum, Constants.EXPORT_PATE_SIZE);
            return selectTmsPayableFeeList(tmsPayableFee);
        });
    }
 
 
    /**
     * 新增应付费用
     *
     * @param tmsPayableFee 应付费用
     * @return 结果
     */
    @Override
    public int insertTmsPayableFee(TmsPayableFee tmsPayableFee)
    {
        tmsPayableFee.setCreateTime(DateUtils.getNowDate());
        return tmsPayableFeeMapper.insertTmsPayableFee(tmsPayableFee);
    }
 
    /**
     * 新增应付费用[批量]
     *
     * @param tmsPayableFees 应付费用
     * @return 结果
     */
    @Override
    public int insertTmsPayableFeeBatch(List<TmsPayableFee> tmsPayableFees)
    {
        int rows = tmsPayableFeeMapper.insertTmsPayableFeeBatch(tmsPayableFees);
        return rows;
    }
 
    /**
     * 修改应付费用
     *
     * @param tmsPayableFee 应付费用
     * @return 结果
     */
    @Override
    public int updateTmsPayableFee(TmsPayableFee tmsPayableFee)
    {
        tmsPayableFee.setUpdateTime(DateUtils.getNowDate());
        return tmsPayableFeeMapper.updateTmsPayableFee(tmsPayableFee);
    }
 
    /**
     * 修改应付费用[批量]
     *
     * @param tmsPayableFees 应付费用
     * @return 结果
     */
    @Override
    public int updateTmsPayableFeeBatch(List<TmsPayableFee> tmsPayableFees){
        return tmsPayableFeeMapper.updateTmsPayableFeeBatch(tmsPayableFees);
    }
 
    @Override
    public int closeTmsPayableFeeById(Integer id) {
        TmsPayableFee tmsPayableFee = selectTmsPayableFeeById(id);
        if(tmsPayableFee != null){
            tmsPayableFee.setStatus(2);
            updateTmsPayableFee(tmsPayableFee);
            tmsDispatchOrderMapper.update(new LambdaUpdateWrapper<TmsDispatchOrder>()
                    .eq(TmsDispatchOrder::getId,tmsPayableFee.getDispatchId())
                    .set(TmsDispatchOrder::getAccountsPayableStatus,0)
            );
        }else{
            throw new RuntimeException("数据不存在");
        }
 
        return 1;
    }
 
    /**
     * 删除应付费用对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTmsPayableFeeByIds(String ids)
    {
        return deleteTmsPayableFeeByIds(Convert.toIntArray(ids));
    }
 
    /**
     * 删除应付费用对象
     *
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTmsPayableFeeByIds(Integer[] ids)
    {
        return tmsPayableFeeMapper.deleteTmsPayableFeeByIds(ids);
    }
 
    @Override
    public TmsApBill initApGenerate(Integer[] ids) {
        List<TmsPayableFee> tmsPayableFeeList = tmsPayableFeeMapper.selectBatchIds(Arrays.asList(ids));
        boolean hasDifferentCustomers = tmsPayableFeeList.stream()
                .map(item-> item.getServiceProviderType()+"_"+ item.getServiceProviderId())
                .distinct()
                .count() > 1;
        if (hasDifferentCustomers) {
            throw new RuntimeException("所选费用包含不同供应商,无法合并生成应收账单");
        }
 
        TmsApBill  tmsApBill = new TmsApBill();
        tmsApBill.setDispatchCount(tmsPayableFeeList.size());
        String s = sysConfigService.selectConfigByKey("sys.hk.rmb.rate");
        BigDecimal exchangeRate = new BigDecimal(s);
        tmsApBill.setSettleRate(exchangeRate);
        TmsPayableFee tmsPayableFee = tmsPayableFeeList.get(0);
        tmsApBill.setServiceProviderId(tmsPayableFee.getServiceProviderId());
        tmsApBill.setServiceProviderName(tmsPayableFee.getServiceProviderName());
        tmsApBill.setServiceProviderType(tmsPayableFee.getServiceProviderType());
        tmsApBill.setBillName(DateUtils.dateTime()+tmsApBill.getServiceProviderName()+"账单");
 
        tmsApBill.setSettleAmount(BigDecimal.ZERO);
        tmsPayableFeeList.forEach(item ->{
            List<TmsPayableFeeItem> tmsPayableFeeItems = tmsPayableFeeItemMapper.selectTmsPayableFeeItemList(new TmsPayableFeeItem() {{
                setHeadId(item.getId());
            }});
            item.setPayableFeeItems(tmsPayableFeeItems);
            BigDecimal rmbAmount = item.getPayableHkbAmount()
                    .multiply(exchangeRate)
                    .setScale(2, RoundingMode.HALF_UP);
            item.setPayableRmbSumAmount( item.getPayableRmbAmount().add(rmbAmount));
            tmsApBill.setSettleAmount(tmsApBill.getSettleAmount().add(item.getPayableRmbSumAmount()));
 
        });
        tmsApBill.setTmsPayableFeeList(tmsPayableFeeList);
        tmsApBill.setActualSettlementAmount(tmsApBill.getSettleAmount());
        return tmsApBill;
    }
 
    @Override
    public AjaxResult apGenerate(Integer[] ids) {
 
        TmsApBill tmsApBill = initApGenerate(ids);
        tmsApBill.setSystemNo(systemDataNoService.getNoByKey(SystemDataNoEnum.YFZD));
        tmsApBill.setSettledAmount(BigDecimal.ZERO);
        tmsApBill.setInvoiceStatus(0);
        tmsApBill.setStatus(0);
        tmsApBill.setPushStatus(0); // 初始推送状态:未推送
        tmsApBill.setCreateBy(SecurityUtils.getUsername());
        tmsApBillMapper.insertTmsApBill(tmsApBill);
 
        tmsPayableFeeMapper.update(new LambdaUpdateWrapper<TmsPayableFee>()
                .set(TmsPayableFee::getBillPayableId,tmsApBill.getId())
                .set(TmsPayableFee::getBillPayableNo,tmsApBill.getSystemNo())
                .set(TmsPayableFee::getStatus,1)
                .in(TmsPayableFee::getId, ids)
        );
        List<TmsPayableFee> tmsPayableFeeList = tmsApBill.getTmsPayableFeeList();
        List<TmsApBillItem> rmb = tmsPayableFeeList.stream().map(item -> {
            TmsApBillItem billItem = new TmsApBillItem();
            billItem.setBillId(tmsApBill.getId());
            billItem.setProjectName(item.getProjectName());
            billItem.setDispatchNo(item.getDispatchNo());
            billItem.setOrderTime(item.getCreateTime());
            billItem.setEstimateAmount(item.
                    getPayableRmbSumAmount());
            billItem.setCurrency("RMB");
            billItem.setApFeeId(item.getId());
            billItem.setStatus(0);
            return billItem;
        }).collect(Collectors.toList());
        if (!rmb.isEmpty()){
            tmsApBillItemMapper.insertTmsApBillItemBatch(rmb);
        }
        
        //向外部系统推送数据
        AsyncTaskExecutor executor = new SimpleAsyncTaskExecutor();
        executor.execute(() -> pushPayableToExternalSystem(tmsApBill, tmsPayableFeeList));
 
        return AjaxResult.success();
    }
    
    /**
     * 向外部系统推送应付数据
     * @param tmsApBill 应付账单
     * @param tmsPayableFeeList 应付费用列表
     */
    @Async
    protected void pushPayableToExternalSystem(TmsApBill tmsApBill, List<TmsPayableFee> tmsPayableFeeList) {
        java.util.Map<String, Object> requestBody = new java.util.HashMap<>();
        try {
            // 构建请求体
            String apiUrl = url+"/addPayableBill";
            
            // 构建bill部分
            java.util.Map<String, Object> billMap = new java.util.HashMap<>();
            billMap.put("billName", tmsApBill.getBillName());
            billMap.put("supplierName", tmsApBill.getServiceProviderName());
            billMap.put("totalAmount", tmsApBill.getSettleAmount());
            billMap.put("currency", "RMB");
            billMap.put("status", "0");
            billMap.put("isInternalSettlement", "0");
            billMap.put("internalSettlementUnit", tmsApBill.getServiceProviderName());
            billMap.put("documentCount", tmsPayableFeeList.size());
            billMap.put("discountAmount", 0.00);
            billMap.put("paidAmount", 0.00);
            billMap.put("pendingAmount", tmsApBill.getSettleAmount());
            billMap.put("exchangeRate", tmsApBill.getSettleRate());
            billMap.put("cnyAmount", tmsApBill.getSettleAmount());
            billMap.put("periodType", "");
            billMap.put("businessStartDate", "");
            billMap.put("businessEndDate", "");
            billMap.put("billingStartDate", "");
            billMap.put("billingEndDate", "");
            billMap.put("billGenerateDate", "");
            billMap.put("billSendDate", "");
            billMap.put("billDueDate", "");
            billMap.put("remark", "");
            
            // 构建fees部分
            List<java.util.Map<String, Object>> feesList = new java.util.ArrayList<>();
            for (int i = 0; i < tmsPayableFeeList.size(); i++) {
                TmsPayableFee fee = tmsPayableFeeList.get(i);
                java.util.Map<String, Object> feeMap = new java.util.HashMap<>();
                feeMap.put("sourceSystem", "TMS");
                feeMap.put("documentNo", fee.getDispatchNo() != null ? fee.getDispatchNo() : "");
                feeMap.put("supplierName", tmsApBill.getServiceProviderName());
                feeMap.put("payableAmount", fee.getPayableRmbAmount().add(fee.getPayableHkbAmount()));
                BigDecimal rmbAmount = fee.getPayableRmbAmount();
                BigDecimal hkbAmount = fee.getPayableHkbAmount();
                StringBuilder amountStr = new StringBuilder();
                if (rmbAmount.compareTo(BigDecimal.ZERO) > 0) {
                    amountStr.append(rmbAmount).append("人民币");
                }
                if (hkbAmount.compareTo(BigDecimal.ZERO) > 0) {
                    if (amountStr.length() > 0) {
                        amountStr.append(" ");
                    }
                    amountStr.append(hkbAmount).append("港币");
                }
                feeMap.put("payableAmountStr", amountStr.toString());
                feeMap.put("serialNumber", String.format("%03d", i + 1));
                feeMap.put("relatedBillNo", "");
                feeMap.put("businessSector", "0");
                feeMap.put("documentType", "0");
                feeMap.put("isInternalSettlement", "0");
                feeMap.put("internalSettlementUnit", "");
                feeMap.put("projectName", fee.getProjectName() != null ? fee.getProjectName() : "");
                feeMap.put("businessTime", fee.getDispatchConfirmTime());
                feeMap.put("payableConfirmTime", fee.getDispatchConfirmTime());
                feeMap.put("status", "1");
                feeMap.put("remark", "");
                
                // 构建feeDetails部分
                List<java.util.Map<String, Object>> feeDetailsList = new java.util.ArrayList<>();
                List<TmsPayableFeeItem> payableFeeItems = fee.getPayableFeeItems();
                for (TmsPayableFeeItem payableFeeItem : payableFeeItems) {
                    java.util.Map<String, Object> feeDetailMap = new java.util.HashMap<>();
                    feeDetailMap.put("feeType", payableFeeItem.getFeeType());
                    feeDetailMap.put("feeName", payableFeeItem.getFeeName());
                    feeDetailMap.put("unitPrice", payableFeeItem.getRegisterAmount());
                    feeDetailMap.put("billingQuantity", payableFeeItem.getRegisterAmount());
                    feeDetailMap.put("billingAmount", payableFeeItem.getRegisterAmount());
                    feeDetailMap.put("billingUnit", "次");
                    feeDetailMap.put("actualAmount", payableFeeItem.getRegisterAmount());
                    feeDetailMap.put("currency", payableFeeItem.getCurrency());
                    feeDetailMap.put("feeRegTime",payableFeeItem.getRegisterTime());
                    feeDetailMap.put("remark", "");
                    feeDetailsList.add(feeDetailMap);
                }
 
                
                feeMap.put("feeDetails", feeDetailsList);
                feesList.add(feeMap);
            }
            
            // 构建完整请求体
            requestBody.put("bill", billMap);
            requestBody.put("fees", feesList);
            
            // 设置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());
            
     
            // 解析响应,获取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());
            tmsApBillMapper.updateTmsApBill(tmsApBill);
        } 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
     * @return 结果
     */
    @Override
    public int deleteTmsPayableFeeById(Integer id)
    {
        return tmsPayableFeeMapper.deleteTmsPayableFeeById(id);
    }
}