liupeng hace 4 años
padre
commit
a88e8fd9c6

+ 2 - 1
code/sapparent/sapcms/src/main/java/org/fouram/controller/ApiController.java

@@ -22,6 +22,7 @@ import org.fouram.core.util.ResultUtil;
 import org.fouram.entity.SapOrg;
 import org.fouram.entity.SapUser;
 import org.fouram.enums.DelFlagEnum;
+import org.fouram.enums.SapUserResultEnum;
 import org.fouram.input.FindUsersDTO;
 import org.fouram.service.SapCheckReportService;
 import org.fouram.service.SapOrgService;
@@ -174,7 +175,7 @@ public class ApiController extends BaseController {
 			return AppUtil.error(ResultConstant.NO_TOKEN, ResultConstant.TOKEN_ERR_MSG);
 		}
 		try {
-			return AppUtil.success(sapUserService.selectDeleteList(), null);
+			return AppUtil.success(sapUserService.selectListByResult(SapUserResultEnum.LEAVE.getCode()), null);
 		} catch (Exception e) {
 			LoggerUtil.error(e);
 			return AppUtil.error(ResultConstant.WEB_ERR_MSG);

+ 0 - 19
code/sapparent/sapcms/src/main/java/org/fouram/controller/TaskController.java

@@ -5,7 +5,6 @@ import org.fouram.core.util.AppUtil;
 import org.fouram.core.util.AppUtil.ResultConstant;
 import org.fouram.core.util.LoggerUtil;
 import org.fouram.service.SapReportService;
-import org.fouram.service.SapUserService;
 import org.fouram.service.SyncSapUserToWxService;
 import org.fouram.service.TestUserService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -18,8 +17,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
 public class TaskController extends BaseController {
 
 	@Autowired
-	private SapUserService sapUserService;
-	@Autowired
 	private TestUserService testUserService;
 	@Autowired
 	private SapReportService sapReportService;
@@ -73,20 +70,4 @@ public class TaskController extends BaseController {
 			return AppUtil.error(ResultConstant.WEB_ERR_MSG);
 		}
 	}
-	
-	/**
-	 * http://localhost:8080/sapcms/task/updateUnableData
-	 * @return
-	 */
-	@RequestMapping(value = "/updateUnableData", produces = "application/json;charset=utf-8")
-	@ResponseBody
-	public Object updateUnableData() {
-		try {
-			sapUserService.updateUnableData();
-			return AppUtil.success();
-		} catch (Exception e) {
-			LoggerUtil.error(e);
-			return AppUtil.error(ResultConstant.WEB_ERR_MSG);
-		}
-	}
 }

+ 3 - 10
code/sapparent/sapservice/src/main/java/org/fouram/entity/SapUser.java

@@ -43,8 +43,11 @@ public class SapUser implements Serializable {
 	private Long thirdOrgWxDepartId;
 	private Long fourthOrgWxDepartId;
 
+	// sap职位
 	private String position;
 	private String formatPosition;
+	// 微信职位
+	private String wxPosition;
 	// 手机
 	private String mobile;
 	private String formatMobile;
@@ -62,14 +65,4 @@ public class SapUser implements Serializable {
 
 	private List<List<SapOrg>> departList;
 
-	@Override
-	public String toString() {
-		StringBuilder sBuilder = new StringBuilder();
-		sBuilder.append(userId).append(personId).append(username).append(name).append(firstOrgName).append(firstOrgId)
-				.append(firstOrgWxDepartId).append(secondOrgName).append(secondOrgId).append(secondOrgWxDepartId)
-				.append(thirdOrgName).append(thirdOrgId).append(thirdOrgWxDepartId).append(fourthOrgName)
-				.append(fourthOrgId).append(fourthOrgWxDepartId).append(position).append(mobile).append(businessPhone)
-				.append(gender).append(email).append(isPrimary);
-		return sBuilder.toString();
-	}
 }

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

@@ -5,13 +5,11 @@
 		insert into sap_user(userId,personId,username,name,
 			firstOrgId,secondOrgId,thirdOrgId,fourthOrgId,
 			firstOrgName,secondOrgName,thirdOrgName,fourthOrgName,
-			firstOrgWxDepartId,secondOrgWxDepartId,thirdOrgWxDepartId,fourthOrgWxDepartId,
 			position,formatPosition,mobile,formatMobile,businessPhone,formatBusinessPhone,gender,
 			email,formatEmail,isPrimary,delFlag,createDate,sortNumber) 
 		values (#{userId},#{personId},#{username},#{name},
 			#{firstOrgId},#{secondOrgId},#{thirdOrgId},#{fourthOrgId},
 			#{firstOrgName},#{secondOrgName},#{thirdOrgName},#{fourthOrgName},
-			#{firstOrgWxDepartId},#{secondOrgWxDepartId},#{thirdOrgWxDepartId},#{fourthOrgWxDepartId},
 			#{position},#{formatPosition},#{mobile},#{formatMobile},#{businessPhone},#{formatBusinessPhone},#{gender},
 			#{email},#{formatEmail},#{isPrimary},#{delFlag},#{createDate},#{sortNumber})
 	</insert>
@@ -23,8 +21,6 @@
 			thirdOrgId=#{thirdOrgId},fourthOrgId=#{fourthOrgId},
 			firstOrgName=#{firstOrgName},secondOrgName=#{secondOrgName},
 			thirdOrgName=#{thirdOrgName},fourthOrgName=#{fourthOrgName},
-			firstOrgWxDepartId=#{firstOrgWxDepartId},secondOrgWxDepartId=#{secondOrgWxDepartId},
-			thirdOrgWxDepartId=#{thirdOrgWxDepartId},fourthOrgWxDepartId=#{fourthOrgWxDepartId},
 			position=#{position},formatPosition=#{formatPosition},
 			mobile=#{mobile},formatMobile=#{formatMobile},
 			businessPhone=#{businessPhone},formatBusinessPhone=#{formatBusinessPhone},
@@ -57,10 +53,6 @@
 		select * from sap_user where result is null and delFlag = 0
 	</select>
 	
-	<select id="selectDeleteList" resultType="SapUser">
-		select * from sap_user where delFlag = 1 and isPrimary = 1
-	</select>
-	
 	<update id="updateAllDelFlag">
 		update sap_user set delFlag = #{delFlag}
 	</update>
@@ -75,9 +67,9 @@
 		order by sortNumber ASC
 	</select>
 	
-	<select id="selectSuccessList" resultType="SapUser">
+	<select id="selectListByResult" resultType="SapUser">
 		select * from sap_user 
-		where result = 'SUCCESS' and isPrimary = 1 and delFlag = 0 
+		where result = #{result} and isPrimary = 1 and delFlag = 0 
 		order by sortNumber ASC
 	</select>
 	

+ 27 - 0
code/sapparent/sapservice/src/main/java/org/fouram/service/SapOrgService.java

@@ -155,6 +155,33 @@ public class SapOrgService extends BaseService {
 		return result;
 	}
 
+	public Map<String, Long> updateWxDepartId(SapUser user) throws Exception {
+		Map<String, Long> result = Maps.newHashMap();
+		List<SapOrg> allOrgs = Lists.newArrayList();
+
+		SapOrg firstOrg = selectById(user.getFirstOrgId());
+		firstOrg.setWxDepartId(getWxDepartId(firstOrg, WebConstants.TOP_DEPART));
+		allOrgs.add(firstOrg);
+
+		SapOrg sencodOrg = selectById(user.getSecondOrgId());
+		sencodOrg.setWxDepartId(getWxDepartId(sencodOrg, firstOrg.getWxDepartId()));
+		allOrgs.add(sencodOrg);
+
+		SapOrg thirdOrg = selectById(user.getThirdOrgId());
+		thirdOrg.setWxDepartId(getWxDepartId(thirdOrg, sencodOrg.getWxDepartId()));
+		allOrgs.add(thirdOrg);
+
+		SapOrg fourthOrg = selectById(user.getFourthOrgId());
+		fourthOrg.setWxDepartId(getWxDepartId(fourthOrg, thirdOrg.getWxDepartId()));
+		allOrgs.add(fourthOrg);
+
+		for (SapOrg org : allOrgs) {
+			updateWxDepartId(org);
+			result.put(org.getId(), org.getWxDepartId());
+		}
+		return result;
+	}
+
 	private Long getWxDepartId(SapOrg org, Long parentId) throws WxErrorException {
 		return WXCpDepartUtil.createOrUpdate(org.getWxDepartId(), parentId, org.getSapName());
 	}

+ 19 - 27
code/sapparent/sapservice/src/main/java/org/fouram/service/SapUserService.java

@@ -25,8 +25,6 @@ import org.springframework.stereotype.Service;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.google.common.collect.Lists;
 
-import me.chanjar.weixin.cp.bean.WxCpUser;
-
 @Service
 public class SapUserService extends BaseService {
 
@@ -44,7 +42,7 @@ public class SapUserService extends BaseService {
 	private static boolean isTestWXSync = true;
 	private static boolean isTestMobile = false;
 
-	public List<SapUser> getUsersFromSap() throws Exception {
+	public void saveOrUpdateSapUsers() throws Exception {
 		HttpHeaders headers = getHttpHeaders();
 		String userUrl = "https://api15.sapsf.cn/odata/v2/User?$filter=status in 'active'&$format=json&$expand=personKeyNav";
 		// get
@@ -86,7 +84,17 @@ public class SapUserService extends BaseService {
 		sapPersonalService.setGender(sapUsers);
 		// 获取用户对应职务
 		sapEmpJobService.setPosition(sapUsers);
-		return sapUsers;
+		// 更新用户
+		for (int i = 0; i < sapUsers.size(); i++) {
+			SapUser sapUser = sapUsers.get(i);
+			sapUser.setSortNumber(i + 1);
+			SapUser sapUserDb = this.selectByUserId(sapUser.getUserId());
+			if (sapUserDb == null) {
+				this.saveSapUser(sapUser);
+			} else {
+				this.updateSapUser(sapUser);
+			}
+		}
 	}
 
 	public void saveSapUser(SapUser entity) throws Exception {
@@ -95,11 +103,6 @@ public class SapUserService extends BaseService {
 		save("SapUserMapper.saveOne", entity);
 	}
 
-	public void updateDelFlag(String userId, DelFlagEnum delFlagEnum) throws Exception {
-		SapUser entity = SapUser.builder().userId(userId).delFlag(delFlagEnum.getCode()).build();
-		update("SapUserMapper.updateDelFlag", entity);
-	}
-
 	public void updateSapUser(SapUser entity) throws Exception {
 		entity.setDelFlag(DelFlagEnum.ENABLE.getCode());
 		update("SapUserMapper.updateOne", entity);
@@ -167,14 +170,14 @@ public class SapUserService extends BaseService {
 		return (List<SapUser>) findList("SapUserMapper.selectUnSyncList", null);
 	}
 
-	@SuppressWarnings("unchecked")
-	public List<SapUser> selectDeleteList() throws Exception {
-		return (List<SapUser>) findList("SapUserMapper.selectDeleteList", null);
-	}
-
 	public void updateAllDelFlag(String delFlag) throws Exception {
 		update("SapUserMapper.updateAllDelFlag", delFlag);
 	}
+	
+	public void updateDelFlag(String userId, DelFlagEnum delFlagEnum) throws Exception {
+		SapUser entity = SapUser.builder().userId(userId).delFlag(delFlagEnum.getCode()).build();
+		update("SapUserMapper.updateDelFlag", entity);
+	}
 
 	public static void main(String[] args) {
 		StringBuilder stringBuilder = new StringBuilder();
@@ -189,8 +192,8 @@ public class SapUserService extends BaseService {
 	}
 
 	@SuppressWarnings("unchecked")
-	public List<SapUser> selectSuccessList() throws Exception {
-		return (List<SapUser>) findList("SapUserMapper.selectSuccessList", null);
+	public List<SapUser> selectListByResult(String result) throws Exception {
+		return (List<SapUser>) findList("SapUserMapper.selectListByResult", null);
 	}
 
 	@SuppressWarnings("unchecked")
@@ -259,15 +262,4 @@ public class SapUserService extends BaseService {
 		WXCpUserUtil.delete(personId);
 		sapOprLogService.saveOne(JSONUtil.toString(sapUsers), WebConstants.OPRTYPE_DELETE, oprUserId);
 	}
-
-	public void updateUnableData() throws Exception {
-		List<SapUser> users = selectSuccessList();
-		for (SapUser user : users) {
-			WxCpUser wxUser = WXCpUserUtil.getByIdOrEmail(user.getUserId(), null);
-			if (wxUser == null) {
-				System.out.println(user.getUserId());
-				updateDelFlag(user.getUserId(), DelFlagEnum.UNABLE);
-			}
-		}
-	}
 }

+ 7 - 34
code/sapparent/sapservice/src/main/java/org/fouram/service/SyncSapUserToWxService.java

@@ -1,7 +1,6 @@
 package org.fouram.service;
 
 import java.util.List;
-import java.util.Map;
 
 import org.apache.commons.lang3.StringUtils;
 import org.fouram.core.base.service.BaseService;
@@ -11,6 +10,7 @@ import org.fouram.core.util.DateUtil;
 import org.fouram.core.util.LoggerUtil;
 import org.fouram.entity.SapUser;
 import org.fouram.enums.DelFlagEnum;
+import org.fouram.enums.SapUserResultEnum;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -21,43 +21,16 @@ import me.chanjar.weixin.cp.bean.WxCpUser;
 public class SyncSapUserToWxService extends BaseService {
 
 	@Autowired
-	private SapOrgService sapOrgService;
-	@Autowired
 	private SapUserService sapUserService;
 
 	@Transactional
 	public void execute() throws Exception {
-
-		// 更新所有数据无效
+		// 1.更新所有数据无效
 		sapUserService.updateAllDelFlag(DelFlagEnum.UNABLE.getCode());
+		// 2.保存SAP用户数据
+		sapUserService.saveOrUpdateSapUsers();
 
-		// 获取SAP所有用户数据
-		List<SapUser> sapUsers = sapUserService.getUsersFromSap();
-
-		// 根据机构获取对应企业微信id,同时更新企业微信对应部门名称
-		Map<String, Long> orgWxDepartMap = sapOrgService.updateAllWxDepartId();
-
-		LoggerUtil.info("接口获取所有有效数据->" + sapUsers.size());
-
-		// 遍历用户,企业微信用户没有,新增用户并且同步企业微信;数据已有情况,数据未发生变化的,更新为未删除;数据发生变化的,同步微信用户,更新为未删除
-		for (int i = 0; i < sapUsers.size(); i++) {
-			SapUser sapUser = sapUsers.get(i);
-			sapUser.setFirstOrgWxDepartId(orgWxDepartMap.get(sapUser.getFirstOrgId()));
-			sapUser.setSecondOrgWxDepartId(orgWxDepartMap.get(sapUser.getSecondOrgId()));
-			sapUser.setThirdOrgWxDepartId(orgWxDepartMap.get(sapUser.getThirdOrgId()));
-			sapUser.setFourthOrgWxDepartId(orgWxDepartMap.get(sapUser.getFourthOrgId()));
-			sapUser.setSortNumber(i + 1);
-			SapUser sapUserDb = sapUserService.selectByUserId(sapUser.getUserId());
-			if (sapUserDb == null) {
-				sapUserService.saveSapUser(sapUser);
-			} else {
-				if (!sapUserDb.toString().equals(sapUser.toString())) {
-					sapUserService.updateSapUser(sapUser);
-				} else {
-					sapUserService.updateDelFlag(sapUser.getUserId(), DelFlagEnum.ENABLE);
-				}
-			}
-		}
+//		sapUserService.updateDelFlag(sapUser.getUserId(), DelFlagEnum.ENABLE);
 
 		// 更新用户头像
 		this.updateAvatar();
@@ -76,7 +49,7 @@ public class SyncSapUserToWxService extends BaseService {
 
 	// 更新用户头像
 	private void updateAvatar() throws Exception {
-		List<SapUser> successUsers = sapUserService.selectSuccessList();
+		List<SapUser> successUsers = sapUserService.selectListByResult(SapUserResultEnum.SUCCESS.getCode());
 		for (SapUser sapUser : successUsers) {
 			WxCpUser user = WXCpUserUtil.getByIdOrEmail(sapUser.getPersonId(), sapUser.getEmail());
 			if (user != null && StringUtils.isNotBlank(user.getAvatar())) {
@@ -88,7 +61,7 @@ public class SyncSapUserToWxService extends BaseService {
 
 	// 发送禁用企业微信账号信息
 	private void sendQuitUserMsg() throws Exception {
-		List<SapUser> unableSapUsers = sapUserService.selectDeleteList();
+		List<SapUser> unableSapUsers = sapUserService.selectListByResult(SapUserResultEnum.LEAVE.getCode());
 		if (unableSapUsers.size() > 0) {
 			WXCpMessageUtil.sendQuitUserMsg("离职用户" + unableSapUsers.size() + "个,请确认");
 		}