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) } } }