TaxReceiveService.java
4.76 KB
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
package com.diligrp.tax.boot.service;
import com.diligrp.tax.central.context.TenantStorageContext;
import com.diligrp.tax.central.domain.MessageContext;
import com.diligrp.tax.central.exception.TaxAgentServiceException;
import com.diligrp.tax.central.model.TaxPipelineMappingCreate;
import com.diligrp.tax.central.model.TaxPipelineMappingError;
import com.diligrp.tax.central.model.TenantPipeline;
import com.diligrp.tax.central.model.TenantTaxPipelineMapping;
import com.diligrp.tax.central.service.ITaxMappingErrorService;
import com.diligrp.tax.central.service.ITaxPipelineMappingService;
import com.diligrp.tax.central.service.ITaxTenantService;
import com.diligrp.tax.central.type.DocumentType;
import com.diligrp.tax.central.type.MappingStateType;
import com.diligrp.tax.central.type.SystemType;
import com.diligrp.tax.central.type.TaxSystemType;
import com.diligrp.tax.central.utils.JsonUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/**
* @Author: zhangmeiyang
* @CreateTime: 2025-11-07 17:22
* @Version: todo
*/
@Service
public class TaxReceiveService {
@Resource
private ITaxTenantService tenantTaxService;
@Resource
private ITaxPipelineMappingService taxPipelineMappingService;
@Resource
private ITaxMappingErrorService taxMappingErrorService;
@Resource
private TenantStorageContext tenantStorageContext;
public void messagePreHandle(MessageContext ctx) {
Long tenantId = tenantTaxService.getTenantId(ctx.getGroup(), ctx.getEntity());
ctx.setTenantId(tenantId);
DocumentType from = DocumentType.from(ctx.getDocumentType());
SystemType system = SystemType.from(ctx.getSystemType());
Optional.of(system).filter(s -> s.documentTypes.contains(from)).orElseThrow(() -> new TaxAgentServiceException(TaxSystemType.BUSINESS_MATCHES_ARE_INCORRECT));
// 校验业务类型和系统类型
ctx.setDocumentTypeEnum(from);
ctx.setSystemTypeEnum(system);
// 获取租户管道
Optional<TenantPipeline> option = tenantStorageContext.getTenantPipeline(ctx.getTenantId(), system, ctx.getPipelineCode());
TenantPipeline pipeline = option.orElseThrow(() -> new TaxAgentServiceException(TaxSystemType.NO_MATCHING_SET_OF_ACCOUNTS_FOUND));
//获取租户账套
ctx.setTenantPipeline(pipeline);
//获取租户id
ctx.setTenantId(tenantTaxService.getTenantId(ctx.getGroup(), ctx.getEntity()));
}
@Transactional
public void recordMapping(MessageContext messageContext) {
Optional<TenantTaxPipelineMapping> dbOptions = taxPipelineMappingService.findByPipelineIdAndDocumentTypeAndSystemDataIdAndPipelineDataId(messageContext.getTenantPipeline().getTenantId(), messageContext.getTenantPipeline().getId(), messageContext.getDocumentType(), messageContext.getSystemDataId(),messageContext.getPipelineDataId());
dbOptions.ifPresentOrElse(
e -> {
TaxPipelineMappingCreate create = new TaxPipelineMappingCreate();
create.setId(e.getId());
create.setTenantId(messageContext.getTenantId());
create.setOriginData(JsonUtils.toJsonString(messageContext.getMsgBody()));
taxPipelineMappingService.update(create);
}, () -> {
TaxPipelineMappingCreate create = new TaxPipelineMappingCreate();
create.setTenantId(messageContext.getTenantId());
create.setPipelineId(messageContext.getTenantPipeline().getId());
create.setDocumentType(messageContext.getDocumentType());
create.setSystemDataId(messageContext.getSystemDataId());
create.setPipelineDataId(messageContext.getPipelineDataId());
create.setState(MappingStateType.SYNCED.value);
create.setOriginData(JsonUtils.toJsonString(messageContext.getMsgBody()));
taxPipelineMappingService.insert(create);
});
}
@Transactional
public void recordMappingError(Exception e, MessageContext ctx) {
TaxPipelineMappingError error = new TaxPipelineMappingError();
error.setGroup(ctx.getGroup());
error.setEntity(ctx.getEntity());
error.setDocumentType(ctx.getDocumentType());
error.setSystemType(ctx.getSystemType());
error.setSystemDataId(ctx.getSystemDataId());
error.setPipelineCode(ctx.getPipelineCode());
error.setOriginData(JsonUtils.toJsonString(ctx.getMsgBody()));
error.setState(MappingStateType.SYNC_FAILED.value);
error.setErrorMessage(e.getMessage());
taxMappingErrorService.insert(error);
}
}