测试用户
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
package com.deloitte.sbg.controller
 
import cn.hutool.core.util.StrUtil
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.common.annotation.NoToken
import com.common.core.enums.ResultCodeEnum
import com.common.core.exception.BizException
import com.alibaba.fastjson.serializer.SerializerFeature
import com.common.core.utils.IdUtils
import com.common.core.utils.RestUtil
import com.common.core.utils.SecretsManagerUtils
import com.common.core.utils.SpringContextUtils
import com.deloitte.system.model.Contact
import com.deloitte.system.model.Opportunity
import groovy.util.logging.Log
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.core.env.Environment
import org.springframework.http.HttpHeaders
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 javax.servlet.http.HttpServletResponse
import java.util.logging.Level
 
/**
 * @Author: Ben Pi
 * @Date: 23/02/2022 13:50
 */
 
 
@RestController
@RequestMapping("/sbg")
@Log("log")
class SapSBGRest {
 
    @Value("\${sap.url}")
    private String sapurl;
 
    @Autowired
    private IdUtils idUtils;
    @Autowired
    private Environment env;
    @Autowired
    private SecretsManagerUtils secretsManagerUtils;
 
    @RequestMapping(value = "/001", method = RequestMethod.POST)
    def sbg001(@RequestBody def body) {
        log.info("001接口请求=====================body:"+body)
        HttpServletResponse resp = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse()
        JSONObject object = secretsManagerUtils.getSecret(env.getProperty("aws.secrets.systemauth"));
        String saptoken = object.getString("sap_token");
        try {
            for (int i = 0; i < body.SSBDCustomerContacts.SSBDCustomerContact.size(); i++) {
                //判断是客户还是联系人,客户信息CustomerCode和ContactCode相等,不需要做解密
                String customerCode = body.SSBDCustomerContacts.SSBDCustomerContact[i].CustomerCode
                String contactCode = body.SSBDCustomerContacts.SSBDCustomerContact[i].ContactCode
                if(customerCode.equals(contactCode)){
                    if (body.SSBDCustomerContacts.SSBDCustomerContact[i].DataId != null) {
                        body.SSBDCustomerContacts.SSBDCustomerContact[i].remove("DataId")
                    }
                    continue
                }
 
                Contact contact = new Contact().findById(body.SSBDCustomerContacts.SSBDCustomerContact[i].DataId.toString())
                if (!contact) {
                    throw new BizException("无效的dataId")
                }
                String contactOffice = contact.getLastName()
                String telephoneMobile = (StrUtil.isBlank(contact.getPhone())?"":contact.getPhone())+","+(StrUtil.isBlank(contact.getMobilePhone())?"":contact.getMobilePhone())
                String address = contact.getAddress1()
                String postalCode = contact.getPostcode()
                String faxEmail = (StrUtil.isBlank(contact.getFax())?"":contact.getFax())+","+(StrUtil.isBlank(contact.getEmail())?"":contact.getEmail())
                body.SSBDCustomerContacts.SSBDCustomerContact[i].ContactOffice = contactOffice
                body.SSBDCustomerContacts.SSBDCustomerContact[i].TelephoneMobile = telephoneMobile
                body.SSBDCustomerContacts.SSBDCustomerContact[i].Address = address
                body.SSBDCustomerContacts.SSBDCustomerContact[i].PostalCode = postalCode
                body.SSBDCustomerContacts.SSBDCustomerContact[i].FaxEmail = faxEmail
                if (body.SSBDCustomerContacts.SSBDCustomerContact[i].DataId != null) {
                    body.SSBDCustomerContacts.SSBDCustomerContact[i].remove("DataId")
                }
            }
            HttpHeaders header = new HttpHeaders()
            header.add("Authorization", "Basic " + saptoken)
            ResponseEntity sapRes= RestUtil.postNative(sapurl + "SBG001", header, null, JSONObject.toJSONString(body, SerializerFeature.WriteMapNullValue))
            resp.setStatus(sapRes.getStatusCodeValue())
            return "success"
        } catch(HttpServerErrorException e){
            log.log(new Level("ERROR", Integer.MAX_VALUE, "ch.qos.logback.classic.Logger"), "001请求异常", e)
            resp.setStatus(e.getRawStatusCode())
            return JSON.parse(e.getResponseBodyAsString())
        } catch (Exception e) {
            log.log(new Level("ERROR",Integer.MAX_VALUE, "ch.qos.logback.classic.Logger"), "001请求异常", e)
            resp.setStatus(Integer.valueOf(ResultCodeEnum.AWS_RT_ERROR.getCode()))
            throw new BizException(ResultCodeEnum.AWS_RT_ERROR)
        }
    }
 
    @RequestMapping(value = "/027", method = RequestMethod.POST)
    def sbg027(@RequestBody def body) {
        log.info("027接口请求=====================body:"+body)
        HttpServletResponse resp = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse()
        JSONObject object = secretsManagerUtils.getSecret(env.getProperty("aws.secrets.systemauth"));
        String saptoken = object.getString("sap_token");
        log.info("saptoken:"+saptoken);
        try {
            def datas = body.GeDatas
            for (int i = 0; i < datas.GeData.size(); i++) {
                Opportunity opportunity = new Opportunity().findById(datas.GeData[i].DataId)
                if (!opportunity) {
                    throw new BizException("无效的dataId")
                }
                datas.GeData[i].DealerSalesStaffName = opportunity.getDealerSalesStaffName()
                if (datas.GeData[i].DataId != null) {
                    datas.GeData[i].remove("DataId")
                }
            }
            HttpHeaders header = new HttpHeaders()
            header.add("Authorization", "Basic " + saptoken)
            ResponseEntity sapRes = RestUtil.postNativeObject(sapurl + "SBG027", header, null, JSONObject.toJSONString(body, SerializerFeature.WriteMapNullValue))
            resp.setStatus(sapRes.getStatusCodeValue())
            return "success"
        } catch(HttpServerErrorException e){
            log.log(new Level("ERROR", Integer.MAX_VALUE, "ch.qos.logback.classic.Logger"), "027请求异常", e)
            resp.setStatus(e.getRawStatusCode())
            return JSON.parse(e.getResponseBodyAsString())
        } catch (Exception e) {
            log.log(new Level("ERROR",Integer.MAX_VALUE, "ch.qos.logback.classic.Logger"), "027请求异常", e)
            resp.setStatus(Integer.valueOf(ResultCodeEnum.AWS_RT_ERROR.getCode()))
            throw new BizException(ResultCodeEnum.AWS_RT_ERROR)
        }
    }
 
    @RequestMapping(value = "/007", method = RequestMethod.POST)
    def sbg007(@RequestBody def body) {
        log.info("007接口请求=====================body:"+body)
        HttpServletResponse resp = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse()
        JSONObject object = secretsManagerUtils.getSecret(env.getProperty("aws.secrets.systemauth"));
        String saptoken = object.getString("sap_token");
        try {
            def datas = body.GeDatas
            for (int i = 0; i < datas.GeData.size(); i++) {
                Opportunity opportunity = new Opportunity().findById(datas.GeData[i].DataId)
                if (!opportunity) {
                    throw new BizException("无效的dataId")
                }
                datas.GeData[i].DealerSalesStaffName = opportunity.getDealerSalesStaffName()
                if (datas.GeData[i].DataId != null) {
                    datas.GeData[i].remove("DataId")
                }
            }
            HttpHeaders header = new HttpHeaders()
            header.add("Authorization", "Basic " + saptoken)
            ResponseEntity sapRes = RestUtil.postNative(sapurl + "SBG007", header, null, JSONObject.toJSONString(body, SerializerFeature.WriteMapNullValue))
            resp.setStatus(sapRes.getStatusCodeValue())
            return "success"
        } catch(HttpServerErrorException e){
            log.log(new Level("ERROR", Integer.MAX_VALUE, "ch.qos.logback.classic.Logger"), "007请求异常", e)
            resp.setStatus(e.getRawStatusCode())
            return JSON.parse(e.getResponseBodyAsString())
        } catch (Exception e) {
            log.log(new Level("ERROR",Integer.MAX_VALUE, "ch.qos.logback.classic.Logger"), "007请求异常", e)
            resp.setStatus(Integer.valueOf(ResultCodeEnum.AWS_RT_ERROR.getCode()))
            throw new BizException(ResultCodeEnum.AWS_RT_ERROR)
        }
    }
}