测试用户
2023-04-13 43393f2bb11cbf9e6af40077bbc5284660e8a754
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
package com.deloitte.sbg.controller
 
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.alibaba.fastjson.serializer.SerializerFeature
import com.common.core.domain.DesensitiveType
import com.common.core.enums.ResultCodeEnum
import com.common.core.service.SFService
import com.common.core.utils.DesensitiveUtils
import com.common.core.utils.IdUtils
import com.common.core.utils.RestUtil
import com.common.core.utils.StringUtils
import com.deloitte.system.model.Contact
import com.deloitte.system.request.SFTokenDto
import groovy.util.logging.Log
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.client.HttpServerErrorException
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes
import org.springframework.http.HttpHeaders
 
import javax.servlet.http.HttpServletResponse
import java.util.logging.Level
 
import static com.jfinal.plugin.activerecord.Db.tx
 
/**
 * @Author: Ben Pi
 * @Date: 22/02/2022 14:14
 */
 
 
@RestController
@RequestMapping("/sbg")
@Log("log")
class SfSBGRest {
    @Autowired
    private SFService sfService;
 
    @Autowired
    private IdUtils idUtils;
 
    @Value("\${salesforce.sbgurl}")
    private String uri;
 
    @RequestMapping(value = "/203", method = RequestMethod.POST)
    def sbg203(@RequestBody def body) {
        log.info("203接口请求=====================body:"+body)
        HttpServletResponse resp = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse()
        def result;
        tx({ ->
            SFTokenDto tokenDto = sfService.getToken()
            String accessToken = tokenDto.getAccessToken()
            HttpHeaders header = new HttpHeaders()
            header.add("Authorization", "Bearer " + accessToken)
            try {
                for (int i = 0; i < body.Partners.size(); i++) {
                    body.Partners[i].other1 = body.Partners[i].RegisterAddress
                    if(null == body.Partners[i].Consignee_Info){
                        continue;
                    }
                    String registerAddress = body.Partners[i].RegisterAddress
                    String englishAddress = body.Partners[i].STR_SUPPL1
                    String bpType = body.Partners[i].BPType
 
                    //置空字段
                    body.Partners[i].Invoice_phone = ""
                    body.Partners[i].Incorporator = ""
                    for (int k = 0; k < body.Partners[i].Consignee_Info.size(); k++) {
                        if (StringUtils.isNotBlank(body.Partners[i].Consignee_Info[k].ContactName)) {
                            //置空字段
                            body.Partners[i].Consignee_Info[k].ContactMobilePhone = ""
                            if(null != body.Partners[i].License_Info){
                                for(int j = 0 ; j < body.Partners[i].License_Info.size(); j++){
                                    body.Partners[i].License_Info[j].Principal = ""
                                    body.Partners[i].License_Info[j].BusinessAddress = ""
                                }
                            }
                            if ("23" == bpType){
                                //bpType 为23时不加密
                                continue
                            }
 
                            def contactDataId = idUtils.nextId()
                            Contact contact = new Contact()
                            String name = body.Partners[i].Consignee_Info[k].ContactName
                            String contactPhone = body.Partners[i].Consignee_Info[k].ContactPhone
                            String contactAddress = body.Partners[i].Consignee_Info[k].ContactAddress
                            String contactEnglishName = body.Partners[i].Consignee_Info[k].ContactEnglishName
                            // 邮政编码
                            String postalCode = body.Partners[i].Consignee_Info[k].PostalCode
                            contact.setDataId(Long.parseLong(contactDataId))
                            contact.setMobilePhone(contactPhone)
                            contact.setMobilePhoneD(contactPhone)
                            if (StringUtils.isNotBlank(contactEnglishName) && "22".equals(bpType)) {
                                contact.setLastName(name + '(' + contactEnglishName + ')')
                                contact.setContactEnglishName(contactEnglishName)
                                contact.setFirstName(null)
                            } else {
                                contact.setLastName(name)
                                contact.setFirstName(null)
                            }
                            contact.setAddress1("22".equals(bpType) ? registerAddress : contactAddress)
                            contact.setAddress1D("22".equals(bpType) ? registerAddress : contactAddress)
                            contact.setEnglishAddress("22".equals(bpType) ? englishAddress : null)
                            body.Partners[i].RegisterAddress = StringUtils.isNotBlank(registerAddress) ? DesensitiveUtils.getFieldDesensitive(DesensitiveType.ADDRESS) : ""
                            body.Partners[i].Consignee_Info[k].ContactName = StringUtils.isNotBlank(name) ? DesensitiveUtils.getFieldDesensitive(DesensitiveType.CHINESE_NAME) : ""
                            body.Partners[i].Consignee_Info[k].ContactPhone = StringUtils.isNotBlank(contactPhone) ? DesensitiveUtils.getFieldDesensitive(DesensitiveType.MOBILE_PHONE) : ""
                            body.Partners[i].Consignee_Info[k].ContactEnglishName = StringUtils.isNotBlank(contactEnglishName) ? DesensitiveUtils.getFieldDesensitive(DesensitiveType.CHINESE_NAME) : ""
                            //body.Partners[i].Consignee_Info[k].EnglishAddress = null//StringUtils.isNotBlank(contact.getEnglishAddress()) ? DesensitiveUtils.getFieldDesensitive(DesensitiveType.ADDRESS) : ""
                            body.Partners[i].Consignee_Info[k].remove("EnglishAddress")
                            body.Partners[i].Consignee_Info[k].ContactAddress = StringUtils.isNotBlank(contactAddress) ? DesensitiveUtils.getFieldDesensitive(DesensitiveType.ADDRESS) : ""
                            body.Partners[i].Consignee_Info[k].PostalCode = StringUtils.isNotBlank(postalCode) ? DesensitiveUtils.getFieldDesensitive(DesensitiveType.ZIP_CODE) : ""
 
                            body.Partners[i].RegisterAddressEncrypted = com.common.core.utils.SM4Utils.encryptStr(registerAddress)
                            body.Partners[i].Consignee_Info[k].ContactNameEncrypted = com.common.core.utils.SM4Utils.encryptStr(name)
                            body.Partners[i].Consignee_Info[k].ContactPhoneEncrypted = com.common.core.utils.SM4Utils.encryptStr(contactPhone)
                            body.Partners[i].Consignee_Info[k].ContactAddressEncrypted = com.common.core.utils.SM4Utils.encryptStr(contactAddress)
                            body.Partners[i].Consignee_Info[k].ContactEnglishNameEncrypted = StringUtils.isNotBlank(contactEnglishName) ? com.common.core.utils.SM4Utils.encryptStr(contactEnglishName) : ""
                            //body.Partners[i].Consignee_Info[k].EnglishAddressEncrypted = null//StringUtils.isNotBlank(contact.getEnglishAddress()) ? com.common.core.utils.SM4Utils.encryptStr(englishAddress) : ""
                            body.Partners[i].Consignee_Info[k].remove("EnglishAddressEncrypted")
                            body.Partners[i].Consignee_Info[k].PostalCodeEncrypted = com.common.core.utils.SM4Utils.encryptStr(postalCode)
                            body.Partners[i].Consignee_Info[k].DataId = contactDataId
                            contact.save()
                        }
                    }
                }
 
                ResponseEntity sfRes = RestUtil.postNative(uri + "SBG203/execute", header, null, JSONObject.toJSONString(body,SerializerFeature.WriteMapNullValue))
                resp.setStatus(sfRes.getStatusCodeValue())
                result = sfRes.getBody().getJSONObject("staticResponse")
                Boolean sfStatus = sfRes.getBody().get("SFStatus")
                if (!sfStatus) {
                    return false
                }
                return true
            } catch(HttpServerErrorException e){
                log.log(new Level("ERROR", Integer.MAX_VALUE, "ch.qos.logback.classic.Logger"), "203请求异常", e)
                resp.setStatus(e.getRawStatusCode())
                result = JSON.parse(e.getResponseBodyAsString())
                return false
            } catch (Exception e) {
                log.log(new Level("ERROR",Integer.MAX_VALUE, "ch.qos.logback.classic.Logger"), "203请求异常", e)
                resp.setStatus(Integer.valueOf(ResultCodeEnum.AWS_RT_ERROR.getCode()))
                result = [[errorCode: 500, message: e.getMessage()]] as JSONArray
                return false
            }
        })
        return result
    }
 
    @RequestMapping(value = "/comparaEncrypt", method = RequestMethod.POST)
    def comparaEncrypt(@RequestBody encryptBody){
        def root = new ArrayList()
        for(int i = 0 ; i  < encryptBody.size() ; i++){
            def node = [:]
            def encrypt = encryptBody[i];
            node.encryptStr = encrypt.encryptStr
            node.unencryptStr = encrypt.unencryptStr
            boolean flag = false;
            try{
                flag = com.common.core.utils.SM4Utils.decryptStr(encrypt.encryptStr.toString()).equals(encrypt.unencryptStr.toString())
            }catch(Exception e){
                flag = false;
            }
            if(flag){
                node.ismatch = "true"
            }else{
                node.ismatch = "false"
            }
            root.add(node)
        }
        return root
    }
}