liupeng vor 5 Jahren
Ursprung
Commit
4ce87a774a

+ 48 - 46
code/sapparent/sapservice/src/main/java/org/fouram/entity/SapUser.java

@@ -1,46 +1,48 @@
-package org.fouram.entity;
-
-import java.io.Serializable;
-import java.util.Date;
-
-import org.apache.ibatis.type.Alias;
-
-import lombok.AllArgsConstructor;
-import lombok.Builder;
-import lombok.Data;
-import lombok.NoArgsConstructor;
-
-@Data
-@Builder
-@NoArgsConstructor
-@AllArgsConstructor
-@Alias(value = "SapUser")
-public class SapUser implements Serializable {
-	private static final long serialVersionUID = 1L;
-
-	private String userId;
-	private String personId;
-	private String username;
-	private String name;
-	private String firstOrg;
-	private String secondOrg;
-	private String thirdOrg;
-	private String fourthOrg;
-	private String position;
-	private String formatPosition;
-	private String mobile;
-	private String formatMobile;
-	private String gender;
-	private String email;
-	private Boolean isPrimary;
-	private String result;
-	private Boolean delFlag;
-	private Date createDate;
-
-	public String toCompareString() {
-		StringBuilder sBuilder = new StringBuilder();
-		sBuilder.append(userId).append(personId).append(name).append(firstOrg).append(secondOrg).append(thirdOrg)
-				.append(fourthOrg).append(position).append(mobile).append(gender).append(email).append(isPrimary);
-		return sBuilder.toString();
-	}
-}
+package org.fouram.entity;
+
+import java.io.Serializable;
+import java.util.Date;
+
+import org.apache.ibatis.type.Alias;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+@Alias(value = "SapUser")
+public class SapUser implements Serializable {
+	private static final long serialVersionUID = 1L;
+
+	private String userId;
+	private String personId;
+	private String username;
+	private String name;
+	private String firstOrg;
+	private String secondOrg;
+	private String thirdOrg;
+	private String fourthOrg;
+	private String position;
+	private String formatPosition;
+	private String mobile;
+	private String formatMobile;
+	private String businessPhone;
+	private String gender;
+	private String email;
+	private Boolean isPrimary;
+	private String result;
+	private Boolean delFlag;
+	private Date createDate;
+
+	public String toCompareString() {
+		StringBuilder sBuilder = new StringBuilder();
+		sBuilder.append(userId).append(personId).append(name).append(firstOrg).append(secondOrg).append(thirdOrg)
+				.append(fourthOrg).append(position).append(mobile).append(businessPhone).append(gender).append(email)
+				.append(isPrimary);
+		return sBuilder.toString();
+	}
+}

+ 57 - 57
code/sapparent/sapservice/src/main/java/org/fouram/mapper/SapUserMapper.xml

@@ -1,58 +1,58 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-<mapper namespace="SapUserMapper">
-	<insert id="saveOne">
-		insert into sap_user(userId,personId,username,name,
-			firstOrg,secondOrg,thirdOrg,fourthOrg,
-			position,formatPosition,mobile,formatMobile,gender,
-			email,isPrimary,delFlag,createDate) 
-		values (#{userId},#{personId},#{username},#{name},
-			#{firstOrg},#{secondOrg},#{thirdOrg},#{fourthOrg},
-			#{position},#{formatPosition},#{mobile},#{formatMobile},#{gender},
-			#{email},#{isPrimary},#{delFlag},#{createDate})
-	</insert>
-	
-	<update id="updateOne">
-		update sap_user 
-		set username=#{username},name=#{name},personId=#{personId},
-			firstOrg=#{firstOrg},secondOrg=#{secondOrg},thirdOrg=#{thirdOrg},fourthOrg=#{fourthOrg},
-			position=#{position},formatPosition=#{formatPosition},
-			mobile=#{mobile},formatMobile=#{formatMobile},gender=#{gender},email=#{email},
-			isPrimary=#{isPrimary},delFlag=#{delFlag}
-		where userId=#{userId}
-	</update>
-	
-	<update id="updateDelFlag">
-		update sap_user 
-		set delFlag=#{delFlag}
-		where userId=#{userId}
-	</update>
-	
-	<update id="updateResult">
-		update sap_user set result=#{result} where personId=#{personId}
-	</update>
-	
-	<select id="selectOne" resultType="SapUser">
-		select * from sap_user where userId=#{userId}
-	</select>
-	
-	<select id="selectListByPersonId" resultType="SapUser">
-		select * from sap_user where personId=#{personId} order by isPrimary desc
-	</select>
-	
-	<select id="selectUnSyncList" resultType="SapUser">
-		select * from sap_user where result is null and delFlag = 0
-	</select>
-	
-	<select id="selectDeleteList" resultType="SapUser">
-		select * from sap_user where delFlag = 1
-	</select>
-	
-	<update id="updateAllDelete">
-		update sap_user set delFlag = 1
-	</update>
-	
-	<select id="selectReportUserList" resultType="SapUser">
-		select * from sap_user where result != '垃圾数据' or result is null
-	</select>
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="SapUserMapper">
+	<insert id="saveOne">
+		insert into sap_user(userId,personId,username,name,
+			firstOrg,secondOrg,thirdOrg,fourthOrg,
+			position,formatPosition,mobile,formatMobile,businessPhone,gender,
+			email,isPrimary,delFlag,createDate) 
+		values (#{userId},#{personId},#{username},#{name},
+			#{firstOrg},#{secondOrg},#{thirdOrg},#{fourthOrg},
+			#{position},#{formatPosition},#{mobile},#{formatMobile},#{businessPhone},#{gender},
+			#{email},#{isPrimary},#{delFlag},#{createDate})
+	</insert>
+	
+	<update id="updateOne">
+		update sap_user 
+		set username=#{username},name=#{name},personId=#{personId},
+			firstOrg=#{firstOrg},secondOrg=#{secondOrg},thirdOrg=#{thirdOrg},fourthOrg=#{fourthOrg},
+			position=#{position},formatPosition=#{formatPosition},
+			mobile=#{mobile},formatMobile=#{formatMobile},businessPhone=#{businessPhone},gender=#{gender},email=#{email},
+			isPrimary=#{isPrimary},delFlag=#{delFlag}
+		where userId=#{userId}
+	</update>
+	
+	<update id="updateDelFlag">
+		update sap_user 
+		set delFlag=#{delFlag}
+		where userId=#{userId}
+	</update>
+	
+	<update id="updateResult">
+		update sap_user set result=#{result} where personId=#{personId}
+	</update>
+	
+	<select id="selectOne" resultType="SapUser">
+		select * from sap_user where userId=#{userId}
+	</select>
+	
+	<select id="selectListByPersonId" resultType="SapUser">
+		select * from sap_user where personId=#{personId} order by isPrimary desc
+	</select>
+	
+	<select id="selectUnSyncList" resultType="SapUser">
+		select * from sap_user where result is null and delFlag = 0
+	</select>
+	
+	<select id="selectDeleteList" resultType="SapUser">
+		select * from sap_user where delFlag = 1
+	</select>
+	
+	<update id="updateAllDelete">
+		update sap_user set delFlag = 1
+	</update>
+	
+	<select id="selectReportUserList" resultType="SapUser">
+		select * from sap_user where result != '垃圾数据' or result is null
+	</select>
 </mapper>

+ 194 - 193
code/sapparent/sapservice/src/main/java/org/fouram/service/SapUserService.java

@@ -1,193 +1,194 @@
-package org.fouram.service;
-
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-
-import org.apache.commons.lang3.StringUtils;
-import org.fouram.core.base.service.BaseService;
-import org.fouram.core.base.service.RequestService;
-import org.fouram.core.plugin.weixin.cp.util.WXCpUserUtil;
-import org.fouram.core.util.Base64Util;
-import org.fouram.core.util.ConfConfig;
-import org.fouram.core.util.Tools;
-import org.fouram.entity.SapUser;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpHeaders;
-import org.springframework.stereotype.Service;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.google.common.collect.Lists;
-
-@Service
-public class SapUserService extends BaseService {
-
-	@Autowired
-	private SapOrgService sapOrgService;
-	@Autowired
-	private RequestService requestService;
-	@Autowired
-	private SapPersonalService sapPersonalService;
-
-	private static boolean isTestWXSync = true;
-//	private static boolean isTestWXSync = false;
-
-	public List<SapUser> getSapUsers() throws Exception {
-		HttpHeaders headers = getHttpHeaders();
-		String userUrl = "https://api15.sapsf.cn/odata/v2/User?$filter=status in 'active'&$format=json&$expand=personKeyNav";
-		// get
-		JsonNode node = requestService.getForObject(userUrl, headers);
-		List<SapUser> sapUsers = Lists.newArrayList();
-		if (node != null) {
-			Iterator<JsonNode> iterator = node.get("d").get("results").elements();
-			while (iterator.hasNext()) {
-				JsonNode userNode = iterator.next();
-				String userId = userNode.get("userId").asText().trim();
-				// 去除非员工账号和测试数据
-				if (!userId.startsWith("Test")) {
-					String username = userNode.get("username").asText().trim();
-					String name = userNode.get("lastName").asText().trim() + userNode.get("firstName").asText().trim();
-					String position = userNode.get("jobCode").asText().trim();
-					String formatPosition = formatLastCode(position).trim();
-					String mobile = userNode.get("cellPhone").asText().trim();
-					String formatMobile = formatMobile(mobile).trim();
-					String email = userNode.get("email").asText().trim();
-					String personId = null;
-					JsonNode personKeyNav =  userNode.get("personKeyNav");
-					if(personKeyNav != null && personKeyNav.get("personIdExternal") != null) {
-						personId = personKeyNav.get("personIdExternal").asText().trim();
-					}
-					// 手机号码脱敏
-//					if (StringUtils.isNotBlank(formatMobile) && formatMobile.length() == 11) {
-//						formatMobile = formatMobile.substring(0, 8) + "999";
-//					} else {
-//						formatMobile = "";
-//					}
-					// 员工信息
-					SapUser sapUser = SapUser.builder().userId(userId).personId(personId).username(username).name(name)
-							.position(position).formatPosition(formatPosition).mobile(mobile).formatMobile(formatMobile)
-							.email(email).isPrimary(userId.equals(personId)).build();
-					sapUsers.add(sapUser);
-				}
-			}
-		}
-		// 获取用户对应机构
-		sapUsers = sapOrgService.setOrg(headers, sapUsers);
-		// 获取用户对应性别
-		sapUsers = sapPersonalService.setGender(headers, sapUsers);
-		return sapUsers;
-	}
-
-	private 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;
-	}
-
-	private String formatLastCode(String str) {
-		str = str.replace(" ", "");
-		if (str.endsWith(")")) {
-			return str.substring(0, str.lastIndexOf("("));
-		}
-		return str;
-	}
-
-	private String formatMobile(String mobile) {
-		return mobile.replace(" ", "").replace("(86)", "").replace("(86)", "");
-	}
-
-	public void saveOneAndSyncWX(SapUser entity) throws Exception {
-		entity.setCreateDate(new Date());
-		entity.setDelFlag(false);
-		save("SapUserMapper.saveOne", entity);
-		// 更新同步企业微信备注
-		updateWXResult(entity.getPersonId());
-	}
-
-	public void updateEnable(String userId) throws Exception {
-		SapUser entity = SapUser.builder().userId(userId).delFlag(false).build();
-		update("SapUserMapper.updateDelFlag", entity);
-	}
-
-	public void updateOneAndSyncWX(SapUser entity) throws Exception {
-		entity.setDelFlag(false);
-		update("SapUserMapper.updateOne", entity);
-		// 更新同步企业微信备注
-		updateWXResult(entity.getPersonId());
-	}
-
-	public void updateWXResult(String personId) throws Exception {
-		String result = createOrUpdateWXUser(personId);
-		SapUser entity = SapUser.builder().personId(personId).result(result).build();
-		update("SapUserMapper.updateResult", entity);
-	}
-
-	private String createOrUpdateWXUser(String personId) throws Exception {
-		// 测试
-		if (isTestWXSync) {
-			return "Success";
-		}
-		List<SapUser> sapUsers = selectListByPersonId(personId);
-		SapUser wxSapUser = sapUsers.get(0);
-		String[] firstOrgList = new String[sapUsers.size()];
-		String[] secondOrgList = new String[sapUsers.size()];
-		String[] thirdOrgList = new String[sapUsers.size()];
-		String[] fourthOrgList = new String[sapUsers.size()];
-		for (int i = 0; i < sapUsers.size(); i++) {
-			SapUser sapUser = sapUsers.get(i);
-			String checkResult = checkSapUser(sapUser);
-			if (StringUtils.isNotBlank(checkResult)) {
-				return checkResult;
-			}
-			firstOrgList[i] = sapUser.getFirstOrg();
-			secondOrgList[i] = sapUser.getSecondOrg();
-			thirdOrgList[i] = sapUser.getThirdOrg();
-			fourthOrgList[i] = sapUser.getFourthOrg();
-		}
-		return WXCpUserUtil.createOrUpdate(personId, wxSapUser.getName(), firstOrgList, secondOrgList, thirdOrgList,
-				fourthOrgList, wxSapUser.getFormatPosition(), wxSapUser.getFormatMobile(), wxSapUser.getGender(),
-				wxSapUser.getEmail());
-	}
-
-	public String checkSapUser(SapUser sapUser) {
-		if (Tools.isEmpty(sapUser.getPersonId())) {
-			return "员工ID为空";
-		} else if (Tools.isEmpty(sapUser.getFirstOrg())) {
-			return "一级组织为空";
-		}
-		return null;
-	}
-
-	public SapUser selectOne(String userId) throws Exception {
-		return (SapUser) findObject("SapUserMapper.selectOne", userId);
-	}
-
-	@SuppressWarnings("unchecked")
-	public List<SapUser> selectListByPersonId(String personId) throws Exception {
-		return (List<SapUser>) findList("SapUserMapper.selectListByPersonId", personId);
-	}
-
-	@SuppressWarnings("unchecked")
-	public List<SapUser> selectUnSyncList() throws Exception {
-		return (List<SapUser>) findList("SapUserMapper.selectUnSyncList", null);
-	}
-
-	@SuppressWarnings("unchecked")
-	public List<SapUser> selectDeleteList() throws Exception {
-		return (List<SapUser>) findList("SapUserMapper.selectDeleteList", null);
-	}
-
-	public void updateAllDelete() throws Exception {
-		update("SapUserMapper.updateAllDelete", null);
-	}
-	
-	public static void main(String[] args) {
-		StringBuilder stringBuilder = new StringBuilder();
-		String sapApiUser = "API01@cmccoltd:cmccoltd123";
-		String authorizationStr = stringBuilder.append("Basic ").append(Base64Util.encodeString(sapApiUser)).toString();
-		System.out.println(authorizationStr);
-	}
-}
+package org.fouram.service;
+
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.lang3.StringUtils;
+import org.fouram.core.base.service.BaseService;
+import org.fouram.core.base.service.RequestService;
+import org.fouram.core.plugin.weixin.cp.util.WXCpUserUtil;
+import org.fouram.core.util.Base64Util;
+import org.fouram.core.util.ConfConfig;
+import org.fouram.core.util.Tools;
+import org.fouram.entity.SapUser;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
+import org.springframework.stereotype.Service;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.google.common.collect.Lists;
+
+@Service
+public class SapUserService extends BaseService {
+
+	@Autowired
+	private SapOrgService sapOrgService;
+	@Autowired
+	private RequestService requestService;
+	@Autowired
+	private SapPersonalService sapPersonalService;
+
+	private static boolean isTestWXSync = true;
+//	private static boolean isTestWXSync = false;
+
+	public List<SapUser> getSapUsers() throws Exception {
+		HttpHeaders headers = getHttpHeaders();
+		String userUrl = "https://api15.sapsf.cn/odata/v2/User?$filter=status in 'active'&$format=json&$expand=personKeyNav";
+		// get
+		JsonNode node = requestService.getForObject(userUrl, headers);
+		List<SapUser> sapUsers = Lists.newArrayList();
+		if (node != null) {
+			Iterator<JsonNode> iterator = node.get("d").get("results").elements();
+			while (iterator.hasNext()) {
+				JsonNode userNode = iterator.next();
+				String userId = userNode.get("userId").asText().trim();
+				// 去除非员工账号和测试数据
+				if (!userId.startsWith("Test")) {
+					String username = userNode.get("username").asText().trim();
+					String name = userNode.get("lastName").asText().trim() + userNode.get("firstName").asText().trim();
+					String position = userNode.get("jobCode").asText().trim();
+					String formatPosition = formatLastCode(position).trim();
+					String mobile = userNode.get("cellPhone").asText().trim();
+					String businessPhone = userNode.get("businessPhone").asText().trim();
+					String formatMobile = formatMobile(mobile).trim();
+					String email = userNode.get("email").asText().trim();
+					String personId = null;
+					JsonNode personKeyNav =  userNode.get("personKeyNav");
+					if(personKeyNav != null && personKeyNav.get("personIdExternal") != null) {
+						personId = personKeyNav.get("personIdExternal").asText().trim();
+					}
+					// 手机号码脱敏
+//					if (StringUtils.isNotBlank(formatMobile) && formatMobile.length() == 11) {
+//						formatMobile = formatMobile.substring(0, 8) + "999";
+//					} else {
+//						formatMobile = "";
+//					}
+					// 员工信息
+					SapUser sapUser = SapUser.builder().userId(userId).personId(personId).username(username).name(name)
+							.position(position).formatPosition(formatPosition).mobile(mobile).formatMobile(formatMobile)
+							.businessPhone(businessPhone).email(email).isPrimary(userId.equals(personId)).build();
+					sapUsers.add(sapUser);
+				}
+			}
+		}
+		// 获取用户对应机构
+		sapUsers = sapOrgService.setOrg(headers, sapUsers);
+		// 获取用户对应性别
+		sapUsers = sapPersonalService.setGender(headers, sapUsers);
+		return sapUsers;
+	}
+
+	private 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;
+	}
+
+	private String formatLastCode(String str) {
+		str = str.replace(" ", "");
+		if (str.endsWith(")")) {
+			return str.substring(0, str.lastIndexOf("("));
+		}
+		return str;
+	}
+
+	private String formatMobile(String mobile) {
+		return mobile.replace(" ", "").replace("(86)", "").replace("(86)", "");
+	}
+
+	public void saveOneAndSyncWX(SapUser entity) throws Exception {
+		entity.setCreateDate(new Date());
+		entity.setDelFlag(false);
+		save("SapUserMapper.saveOne", entity);
+		// 更新同步企业微信备注
+		updateWXResult(entity.getPersonId());
+	}
+
+	public void updateEnable(String userId) throws Exception {
+		SapUser entity = SapUser.builder().userId(userId).delFlag(false).build();
+		update("SapUserMapper.updateDelFlag", entity);
+	}
+
+	public void updateOneAndSyncWX(SapUser entity) throws Exception {
+		entity.setDelFlag(false);
+		update("SapUserMapper.updateOne", entity);
+		// 更新同步企业微信备注
+		updateWXResult(entity.getPersonId());
+	}
+
+	public void updateWXResult(String personId) throws Exception {
+		String result = createOrUpdateWXUser(personId);
+		SapUser entity = SapUser.builder().personId(personId).result(result).build();
+		update("SapUserMapper.updateResult", entity);
+	}
+
+	private String createOrUpdateWXUser(String personId) throws Exception {
+		// 测试
+		if (isTestWXSync) {
+			return "Success";
+		}
+		List<SapUser> sapUsers = selectListByPersonId(personId);
+		SapUser wxSapUser = sapUsers.get(0);
+		String[] firstOrgList = new String[sapUsers.size()];
+		String[] secondOrgList = new String[sapUsers.size()];
+		String[] thirdOrgList = new String[sapUsers.size()];
+		String[] fourthOrgList = new String[sapUsers.size()];
+		for (int i = 0; i < sapUsers.size(); i++) {
+			SapUser sapUser = sapUsers.get(i);
+			String checkResult = checkSapUser(sapUser);
+			if (StringUtils.isNotBlank(checkResult)) {
+				return checkResult;
+			}
+			firstOrgList[i] = sapUser.getFirstOrg();
+			secondOrgList[i] = sapUser.getSecondOrg();
+			thirdOrgList[i] = sapUser.getThirdOrg();
+			fourthOrgList[i] = sapUser.getFourthOrg();
+		}
+		return WXCpUserUtil.createOrUpdate(personId, wxSapUser.getName(), firstOrgList, secondOrgList, thirdOrgList,
+				fourthOrgList, wxSapUser.getFormatPosition(), wxSapUser.getFormatMobile(), wxSapUser.getGender(),
+				wxSapUser.getEmail());
+	}
+
+	public String checkSapUser(SapUser sapUser) {
+		if (Tools.isEmpty(sapUser.getPersonId())) {
+			return "员工ID为空";
+		} else if (Tools.isEmpty(sapUser.getFirstOrg())) {
+			return "一级组织为空";
+		}
+		return null;
+	}
+
+	public SapUser selectOne(String userId) throws Exception {
+		return (SapUser) findObject("SapUserMapper.selectOne", userId);
+	}
+
+	@SuppressWarnings("unchecked")
+	public List<SapUser> selectListByPersonId(String personId) throws Exception {
+		return (List<SapUser>) findList("SapUserMapper.selectListByPersonId", personId);
+	}
+
+	@SuppressWarnings("unchecked")
+	public List<SapUser> selectUnSyncList() throws Exception {
+		return (List<SapUser>) findList("SapUserMapper.selectUnSyncList", null);
+	}
+
+	@SuppressWarnings("unchecked")
+	public List<SapUser> selectDeleteList() throws Exception {
+		return (List<SapUser>) findList("SapUserMapper.selectDeleteList", null);
+	}
+
+	public void updateAllDelete() throws Exception {
+		update("SapUserMapper.updateAllDelete", null);
+	}
+	
+	public static void main(String[] args) {
+		StringBuilder stringBuilder = new StringBuilder();
+		String sapApiUser = "API01@cmccoltd:cmccoltd123";
+		String authorizationStr = stringBuilder.append("Basic ").append(Base64Util.encodeString(sapApiUser)).toString();
+		System.out.println(authorizationStr);
+	}
+}