|
@@ -1,166 +1,174 @@
|
|
|
-package org.fouram.core.base.service;
|
|
|
-
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-
|
|
|
-import org.fouram.constants.WebConstants;
|
|
|
-import org.fouram.core.base.dao.DAO;
|
|
|
-import org.fouram.core.base.exception.PromptException;
|
|
|
-import org.fouram.core.util.Base64Util;
|
|
|
-import org.fouram.core.util.ConfConfig;
|
|
|
-import org.fouram.core.util.JsonNodeUtil;
|
|
|
-import org.fouram.core.util.Tools;
|
|
|
-import org.fouram.entity.SapUser;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
-
|
|
|
-import com.fasterxml.jackson.databind.JsonNode;
|
|
|
-import com.google.common.collect.Lists;
|
|
|
-
|
|
|
-/**
|
|
|
- * 业务逻辑层基类
|
|
|
- */
|
|
|
-public abstract class BaseService {
|
|
|
-
|
|
|
- @Resource(name = "daoSupport")
|
|
|
- private DAO dao;
|
|
|
- @Autowired
|
|
|
- protected RequestService requestService;
|
|
|
-
|
|
|
- public Integer save(String sqlStr, Object paramObj) throws Exception {
|
|
|
- return dao.save(sqlStr, paramObj);
|
|
|
- }
|
|
|
-
|
|
|
- public Integer update(String sqlStr, Object paramObj) throws Exception {
|
|
|
- return dao.update(sqlStr, paramObj);
|
|
|
- }
|
|
|
-
|
|
|
- public Integer delete(String sqlStr, Object paramObj) throws Exception {
|
|
|
- return dao.delete(sqlStr, paramObj);
|
|
|
- }
|
|
|
-
|
|
|
- public Object findObject(String sqlStr, Object paramObj) throws Exception {
|
|
|
- return dao.findForObject(sqlStr, paramObj);
|
|
|
- }
|
|
|
-
|
|
|
- public Object findList(String sqlStr, Object paramObj) throws Exception {
|
|
|
- return dao.findForList(sqlStr, paramObj);
|
|
|
- }
|
|
|
-
|
|
|
- public HttpHeaders getHttpHeaders() {
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
- StringBuilder stringBuilder = new StringBuilder();
|
|
|
- String sapApiUser = ConfConfig.getConfigString("sapApi.user");
|
|
|
- String authorizationStr = stringBuilder.append("Basic ").append(Base64Util.encodeString(sapApiUser)).toString();
|
|
|
- headers.add("Authorization", authorizationStr);
|
|
|
- return headers;
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- StringBuilder stringBuilder = new StringBuilder();
|
|
|
- String sapApiUser = "APIADMIN@cmccoltdD:1234567";
|
|
|
- String authorizationStr = stringBuilder.append("Basic ").append(Base64Util.encodeString(sapApiUser)).toString();
|
|
|
- System.out.println("测试:" + authorizationStr);
|
|
|
-
|
|
|
- stringBuilder = new StringBuilder();
|
|
|
- sapApiUser = "API01@cmccoltd:cmccoltd123";
|
|
|
- authorizationStr = stringBuilder.append("Basic ").append(Base64Util.encodeString(sapApiUser)).toString();
|
|
|
- System.out.println("正式:" + authorizationStr);
|
|
|
- }
|
|
|
-
|
|
|
- public String getPersonIds(List<SapUser> sapUsers) {
|
|
|
- String personIds = "";
|
|
|
- for (SapUser sapUser : sapUsers) {
|
|
|
- personIds += "'" + sapUser.getPersonId() + "',";
|
|
|
- }
|
|
|
- return personIds;
|
|
|
- }
|
|
|
-
|
|
|
- public String getUserIds(List<SapUser> sapUsers) {
|
|
|
- String userIds = "";
|
|
|
- for (SapUser sapUser : sapUsers) {
|
|
|
- userIds += "'" + sapUser.getUserId() + "',";
|
|
|
- }
|
|
|
- return userIds;
|
|
|
- }
|
|
|
-
|
|
|
- public static String formatPosition(String str) {
|
|
|
- if (Tools.isEmpty(str)) {
|
|
|
- return "";
|
|
|
- }
|
|
|
- str = str.replace("\n", "");
|
|
|
- return str;
|
|
|
- }
|
|
|
-
|
|
|
- public static String formatBusinessPhone(String str) {
|
|
|
- if (Tools.isEmpty(str)) {
|
|
|
- return "";
|
|
|
- }
|
|
|
- // 021 80171999 -> 021-80171999
|
|
|
- if (str.length() == 12) {
|
|
|
- str = str.replace(" ", "-");
|
|
|
- }
|
|
|
- if (str.indexOf("x") > -1) {
|
|
|
- str = str.replace("x", "-");
|
|
|
- }
|
|
|
- return str;
|
|
|
- }
|
|
|
-
|
|
|
- public Map<String, List<String>> groupList(List<String> list, int groupSize) {
|
|
|
- int listSize = list.size();
|
|
|
- int toIndex = groupSize;
|
|
|
- Map<String, List<String>> map = new HashMap<>(); // 用map存起来新的分组后数据
|
|
|
- int keyToken = 0;
|
|
|
- for (int i = 0; i < list.size(); i += groupSize) {
|
|
|
- if (i + groupSize > listSize) { // 作用为toIndex最后没有100条数据则剩余几条newList中就装几条
|
|
|
- toIndex = listSize - i;
|
|
|
- }
|
|
|
- List<String> newList = list.subList(i, i + toIndex);
|
|
|
- List<String> userIds = Lists.newArrayList();
|
|
|
- for (String userId : newList) {
|
|
|
- userIds.add(userId);
|
|
|
- }
|
|
|
- map.put("keyName" + keyToken, userIds);
|
|
|
- keyToken++;
|
|
|
- }
|
|
|
- return map;
|
|
|
- }
|
|
|
-
|
|
|
- public String getUserIdList(List<String> sapUserIds) {
|
|
|
- String userIds = "";
|
|
|
- for (String sapUser : sapUserIds) {
|
|
|
- userIds += "'" + sapUser + "',";
|
|
|
- }
|
|
|
- return userIds;
|
|
|
- }
|
|
|
-
|
|
|
- public void throwPromptException(String msg) throws Exception {
|
|
|
- throw new PromptException(msg);
|
|
|
- }
|
|
|
-
|
|
|
- private String getPicklistLabelUrl(String optionId) {
|
|
|
- return "https://api15.sapsf.cn/odata/v2/PicklistLabel(locale='zh_CN',optionId=" + optionId + ")?$format=json";
|
|
|
- }
|
|
|
-
|
|
|
- public String getLabelValue(String optionId) {
|
|
|
- if (Tools.isEmpty(optionId)) {
|
|
|
- return "";
|
|
|
- }
|
|
|
- if (WebConstants.LABEL_MAP.containsKey(optionId)) {
|
|
|
- return WebConstants.LABEL_MAP.get(optionId);
|
|
|
- } else {
|
|
|
- String value = "";
|
|
|
- try {
|
|
|
- String url = getPicklistLabelUrl(optionId);
|
|
|
- JsonNode node = requestService.getForObject(url, getHttpHeaders());
|
|
|
- value = JsonNodeUtil.getValue(node, "d", "label");
|
|
|
- } catch (Exception e) {
|
|
|
- }
|
|
|
- WebConstants.LABEL_MAP.put(optionId, value);
|
|
|
- return value;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+package org.fouram.core.base.service;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+
|
|
|
+import org.fouram.constants.WebConstants;
|
|
|
+import org.fouram.core.base.dao.DAO;
|
|
|
+import org.fouram.core.base.exception.PromptException;
|
|
|
+import org.fouram.core.util.Base64Util;
|
|
|
+import org.fouram.core.util.ConfConfig;
|
|
|
+import org.fouram.core.util.JsonNodeUtil;
|
|
|
+import org.fouram.core.util.Tools;
|
|
|
+import org.fouram.entity.SapUser;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 业务逻辑层基类
|
|
|
+ */
|
|
|
+public abstract class BaseService {
|
|
|
+
|
|
|
+ @Resource(name = "daoSupport")
|
|
|
+ private DAO dao;
|
|
|
+ @Autowired
|
|
|
+ protected RequestService requestService;
|
|
|
+
|
|
|
+ public Integer save(String sqlStr, Object paramObj) throws Exception {
|
|
|
+ return dao.save(sqlStr, paramObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Integer update(String sqlStr, Object paramObj) throws Exception {
|
|
|
+ return dao.update(sqlStr, paramObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Integer delete(String sqlStr, Object paramObj) throws Exception {
|
|
|
+ return dao.delete(sqlStr, paramObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object findObject(String sqlStr, Object paramObj) throws Exception {
|
|
|
+ return dao.findForObject(sqlStr, paramObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object findList(String sqlStr, Object paramObj) throws Exception {
|
|
|
+ return dao.findForList(sqlStr, paramObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ public HttpHeaders getHttpHeaders() {
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ String sapApiUser = ConfConfig.getConfigString("sapApi.user");
|
|
|
+ String authorizationStr = stringBuilder.append("Basic ").append(Base64Util.encodeString(sapApiUser)).toString();
|
|
|
+ headers.add("Authorization", authorizationStr);
|
|
|
+ return headers;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ String sapApiUser = "APIADMIN@cmccoltdD:1234567";
|
|
|
+ String authorizationStr = stringBuilder.append("Basic ").append(Base64Util.encodeString(sapApiUser)).toString();
|
|
|
+ System.out.println("测试:" + authorizationStr);
|
|
|
+
|
|
|
+ stringBuilder = new StringBuilder();
|
|
|
+ sapApiUser = "API01@cmccoltd:cmccoltd123";
|
|
|
+ authorizationStr = stringBuilder.append("Basic ").append(Base64Util.encodeString(sapApiUser)).toString();
|
|
|
+ System.out.println("正式:" + authorizationStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getPersonIds(List<SapUser> sapUsers) {
|
|
|
+ String personIds = "";
|
|
|
+ for (SapUser sapUser : sapUsers) {
|
|
|
+ personIds += "'" + sapUser.getPersonId() + "',";
|
|
|
+ }
|
|
|
+ return personIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getUserIds(List<SapUser> sapUsers) {
|
|
|
+ String userIds = "";
|
|
|
+ for (SapUser sapUser : sapUsers) {
|
|
|
+ userIds += "'" + sapUser.getUserId() + "',";
|
|
|
+ }
|
|
|
+ return userIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatPosition(String str) {
|
|
|
+ if (Tools.isEmpty(str)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ str = str.replace("\n", "");
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatBusinessPhone(String str) {
|
|
|
+ if (Tools.isEmpty(str)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ // 021 80171999 -> 021-80171999
|
|
|
+ if (str.length() == 12) {
|
|
|
+ str = str.replace(" ", "-");
|
|
|
+ }
|
|
|
+ if (str.indexOf("x") > -1) {
|
|
|
+ str = str.replace("x", "-");
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, List<String>> groupList(List<String> list, int groupSize) {
|
|
|
+ int listSize = list.size();
|
|
|
+ int toIndex = groupSize;
|
|
|
+ Map<String, List<String>> map = new HashMap<>(); // 用map存起来新的分组后数据
|
|
|
+ int keyToken = 0;
|
|
|
+ for (int i = 0; i < list.size(); i += groupSize) {
|
|
|
+ if (i + groupSize > listSize) { // 作用为toIndex最后没有100条数据则剩余几条newList中就装几条
|
|
|
+ toIndex = listSize - i;
|
|
|
+ }
|
|
|
+ List<String> newList = list.subList(i, i + toIndex);
|
|
|
+ List<String> userIds = Lists.newArrayList();
|
|
|
+ for (String userId : newList) {
|
|
|
+ userIds.add(userId);
|
|
|
+ }
|
|
|
+ map.put("keyName" + keyToken, userIds);
|
|
|
+ keyToken++;
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getUserIdList(List<String> sapUserIds) {
|
|
|
+ String userIds = "";
|
|
|
+ for (String sapUser : sapUserIds) {
|
|
|
+ userIds += "'" + sapUser + "',";
|
|
|
+ }
|
|
|
+ return userIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void throwPromptException(String msg) throws Exception {
|
|
|
+ throw new PromptException(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getPicklistLabelUrl(String optionId) {
|
|
|
+ return "https://api15.sapsf.cn/odata/v2/PicklistLabel(locale='zh_CN',optionId=" + optionId + ")?$format=json";
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getLabelValue(String optionId) {
|
|
|
+ if (Tools.isEmpty(optionId)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ if (WebConstants.LABEL_MAP.containsKey(optionId)) {
|
|
|
+ return WebConstants.LABEL_MAP.get(optionId);
|
|
|
+ } else {
|
|
|
+ String value = "";
|
|
|
+ try {
|
|
|
+ String url = getPicklistLabelUrl(optionId);
|
|
|
+ JsonNode node = requestService.getForObject(url, getHttpHeaders());
|
|
|
+ value = JsonNodeUtil.getValue(node, "d", "label");
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ WebConstants.LABEL_MAP.put(optionId, value);
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static JSONObject getPageResult(List<? extends Object> list, Object total) {
|
|
|
+ JSONObject object = new JSONObject();
|
|
|
+ object.put("total", total);
|
|
|
+ object.put("data", list);
|
|
|
+ return object;
|
|
|
+ }
|
|
|
+}
|