liupeng 4 years ago
parent
commit
5f5eb8698a

+ 12 - 33
code/sapparent/sapservice/src/main/java/org/fouram/core/plugin/weixin/cp/util/WXCpDepartUtil.java

@@ -22,11 +22,6 @@ import me.chanjar.weixin.cp.bean.WxCpDepart;
  */
 public class WXCpDepartUtil {
 
-	public static void main(String[] args) throws WxErrorException {
-//		{"id":228,"name":"财务计划与分析","parentid":223}
-		createOrUpdate(228L, 223L, "财务计划与分析");
-	}
-
 	public static List<WxCpDepart> getChildDeparts(Long id) throws WxErrorException {
 		List<WxCpDepart> departs = Lists.newArrayList();
 		for (WxCpDepart depart : departService.list(id)) {
@@ -98,35 +93,19 @@ public class WXCpDepartUtil {
 		return depatmentId;
 	}
 	
-	public static Long createOrUpdate(Long id, Long parentId, String name) throws WxErrorException {
-		if(id == null) {
-			Map<String, Long> map = getChildDepartMap(parentId);
-			Long depatmentId = map.get(name);
-			if (depatmentId == null) {
-				WxCpDepart depart = new WxCpDepart();
-				depart.setName(name);
-				depart.setParentId(parentId);
-				depatmentId = departService.create(depart);
-			}
-			return depatmentId;
-		} else {
-			Map<String, Long> map = getChildDepartMap(parentId);
-			LoggerUtil.info("createOrUpdate->" + JSON.toJSONString(map));
-			Long depatmentId = map.get(name);
-			if(!id.equals(depatmentId)) {
-				WxCpDepart depart = new WxCpDepart();
-				depart.setId(id);
-				depart.setName(name);
-				depart.setParentId(parentId);
-				try {
-					// 部门删除情况下,更新报错,变成创建
-					departService.update(depart);
-				} catch (WxErrorException e) {
-					id = departService.create(depart);
-				}
-			}
-			return id;
+	public static Long createOrUpdate(Long parentId, String name) throws WxErrorException {
+		Map<String, Long> map = getChildDepartMap(parentId);
+		LoggerUtil.info("createOrUpdate->" + JSON.toJSONString(map));
+		Long wxDepartId = map.get(name);
+		// 1.判断parentId对应子部门是否存在对应部门,存在返回对应
+		// 2.不存在创建对应子部门
+		if(wxDepartId == null) {
+			WxCpDepart depart = new WxCpDepart();
+			depart.setName(name);
+			depart.setParentId(parentId);
+			wxDepartId = departService.create(depart);
 		}
+		return wxDepartId;
 	}
 
 	public static List<WxCpDepart> getAllChildDeparts(Long id) throws WxErrorException {

+ 2 - 1
code/sapparent/sapservice/src/main/java/org/fouram/service/SapOrgService.java

@@ -151,6 +151,7 @@ public class SapOrgService extends BaseService {
 			}
 		}
 
+		// 更新对应微信部门id
 		for (SapOrg org : allOrgs) {
 			updateWxDepartId(org);
 			result.put(org.getId(), org.getWxDepartId());
@@ -159,7 +160,7 @@ public class SapOrgService extends BaseService {
 	}
 
 	private Long getWxDepartId(SapOrg org, Long parentId) throws WxErrorException {
-		return WXCpDepartUtil.createOrUpdate(org.getWxDepartId(), parentId, org.getSapName());
+		return WXCpDepartUtil.createOrUpdate(parentId, org.getSapName());
 	}
 
 	@SuppressWarnings("unchecked")