|
@@ -1,151 +1,154 @@
|
|
|
-package org.fouram.core.plugin.weixin.cp.util;
|
|
|
-
|
|
|
-import static org.fouram.core.plugin.weixin.cp.core.WXCpBase.userService;
|
|
|
-
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-import org.fouram.core.util.Tools;
|
|
|
-
|
|
|
-import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
-import me.chanjar.weixin.cp.bean.Gender;
|
|
|
-import me.chanjar.weixin.cp.bean.WxCpUser;
|
|
|
-import me.chanjar.weixin.cp.bean.WxCpUser.ExternalAttribute;
|
|
|
-
|
|
|
-/**
|
|
|
- * 企业微信用户工具类
|
|
|
- *
|
|
|
- * @author Liup
|
|
|
- */
|
|
|
-public class WXCpUserUtil {
|
|
|
-
|
|
|
- public static String createOrUpdate(String userId, String name, String[] subCompanyList, String[] departmentList,
|
|
|
- String position, String mobile, String gender, String email) {
|
|
|
- WxCpUser user = getById(userId);
|
|
|
- try {
|
|
|
- if (user == null) {
|
|
|
- user = getUser(userId, name, subCompanyList, departmentList, position, mobile, gender, email);
|
|
|
- userService.create(user);
|
|
|
- } else {
|
|
|
- user = getUser(userId, name, subCompanyList, departmentList, position, mobile, gender, email);
|
|
|
- userService.update(user);
|
|
|
- }
|
|
|
- return "SUCCESS";
|
|
|
- } catch (WxErrorException e) {
|
|
|
- return e.getMessage();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static String createOrUpdate(String userId, String name, String[] firstOrgList, String[] secondOrgList,
|
|
|
- String[] thirdOrgList, String[] fourthOrgList, String position, String mobile, String gender,
|
|
|
- String email) {
|
|
|
- WxCpUser user = getById(userId);
|
|
|
- try {
|
|
|
- if (user == null) {
|
|
|
- user = getUser(userId, name, firstOrgList, secondOrgList, thirdOrgList, fourthOrgList, position, mobile,
|
|
|
- gender, email);
|
|
|
- userService.create(user);
|
|
|
- } else {
|
|
|
- user = getUser(userId, name, firstOrgList, secondOrgList, thirdOrgList, fourthOrgList, position, mobile,
|
|
|
- gender, email);
|
|
|
- userService.update(user);
|
|
|
- }
|
|
|
- return "SUCCESS";
|
|
|
- } catch (WxErrorException e) {
|
|
|
- return e.getMessage();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static WxCpUser getUser(String userId, String name, String[] firstOrgList, String[] secondOrgList,
|
|
|
- String[] thirdOrgList, String[] fourthOrgList, String position, String mobile, String gender, String email)
|
|
|
- throws WxErrorException {
|
|
|
- WxCpUser user = new WxCpUser();
|
|
|
- if (Tools.notEmpty(userId)) {
|
|
|
- user.setUserId(userId);
|
|
|
- }
|
|
|
- if (Tools.notEmpty(name)) {
|
|
|
- user.setName(name);
|
|
|
- }
|
|
|
- Long[] departIds = new Long[firstOrgList.length];
|
|
|
- for (int i = 0; i < firstOrgList.length; i++) {
|
|
|
- departIds[i] = WXCpDepartUtil.create(firstOrgList[i], secondOrgList[i], thirdOrgList[i],
|
|
|
- fourthOrgList[i]);
|
|
|
- }
|
|
|
- user.setDepartIds(departIds);
|
|
|
- if (Tools.notEmpty(position)) {
|
|
|
- user.setPosition(position);
|
|
|
- }
|
|
|
- if (Tools.notEmpty(mobile)) {
|
|
|
- user.setMobile(mobile);
|
|
|
- }
|
|
|
- if (Tools.notEmpty(gender)) {
|
|
|
- user.setGender(Gender.MALE.getGenderName().equals(gender) ? Gender.MALE : Gender.FEMALE);
|
|
|
- }
|
|
|
- if (Tools.notEmpty(email)) {
|
|
|
- user.setEmail(email);
|
|
|
- }
|
|
|
- user.setToInvite(false);
|
|
|
- ExternalAttribute attribute = ExternalAttribute.builder().name("所属公司").type(0).value(firstOrgList[0]).build();
|
|
|
- user.addExternalAttr(attribute);
|
|
|
- return user;
|
|
|
- }
|
|
|
-
|
|
|
- public static void updateUser(WxCpUser user) throws WxErrorException {
|
|
|
- userService.update(user);
|
|
|
- }
|
|
|
-
|
|
|
- private static WxCpUser getUser(String userId, String name, String[] subCompanyList, String[] departmentList,
|
|
|
- String position, String mobile, String gender, String email) throws WxErrorException {
|
|
|
- WxCpUser user = new WxCpUser();
|
|
|
- if (Tools.notEmpty(userId)) {
|
|
|
- user.setUserId(userId);
|
|
|
- }
|
|
|
- if (Tools.notEmpty(name)) {
|
|
|
- user.setName(name);
|
|
|
- }
|
|
|
- Long[] departIds = new Long[subCompanyList.length];
|
|
|
- for (int i = 0; i < subCompanyList.length; i++) {
|
|
|
- departIds[i] = WXCpDepartUtil.create(subCompanyList[i], departmentList[i]);
|
|
|
- }
|
|
|
- user.setDepartIds(departIds);
|
|
|
- if (Tools.notEmpty(position)) {
|
|
|
- user.setPosition(position);
|
|
|
- }
|
|
|
- if (Tools.notEmpty(mobile)) {
|
|
|
- user.setMobile(mobile);
|
|
|
- }
|
|
|
- if (Tools.notEmpty(gender)) {
|
|
|
- user.setGender(Gender.MALE.getGenderName().equals(gender) ? Gender.MALE : Gender.FEMALE);
|
|
|
- }
|
|
|
- if (Tools.notEmpty(email)) {
|
|
|
- user.setEmail(email);
|
|
|
- }
|
|
|
- user.setToInvite(false);
|
|
|
- ExternalAttribute attribute = ExternalAttribute.builder().name("所属公司").type(0).value(subCompanyList[0]).build();
|
|
|
- user.addExternalAttr(attribute);
|
|
|
- return user;
|
|
|
- }
|
|
|
-
|
|
|
- public static WxCpUser getById(String userId) {
|
|
|
- try {
|
|
|
- return userService.getById(userId);
|
|
|
- } catch (WxErrorException e) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void delete(String userId) {
|
|
|
- try {
|
|
|
- userService.delete(userId);
|
|
|
- } catch (WxErrorException e) {
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static List<WxCpUser> listByDepartment(Long departId) throws WxErrorException {
|
|
|
- return userService.listByDepartment(departId, true, 0);
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- createOrUpdate("yangbin", "杨斌", new String[] { "华人时代" }, new String[] { "管理层" }, "", "", "男",
|
|
|
- "13671179900@jiinfo.cn");
|
|
|
- }
|
|
|
-}
|
|
|
+package org.fouram.core.plugin.weixin.cp.util;
|
|
|
+
|
|
|
+import static org.fouram.core.plugin.weixin.cp.core.WXCpBase.userService;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import org.fouram.core.util.Tools;
|
|
|
+
|
|
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
+import me.chanjar.weixin.cp.bean.Gender;
|
|
|
+import me.chanjar.weixin.cp.bean.WxCpUser;
|
|
|
+import me.chanjar.weixin.cp.bean.WxCpUser.ExternalAttribute;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 企业微信用户工具类
|
|
|
+ *
|
|
|
+ * @author Liup
|
|
|
+ */
|
|
|
+public class WXCpUserUtil {
|
|
|
+
|
|
|
+ public static String createOrUpdate(String userId, String name, String[] subCompanyList, String[] departmentList,
|
|
|
+ String position, String mobile, String gender, String email) {
|
|
|
+ WxCpUser user = getById(userId);
|
|
|
+ try {
|
|
|
+ if (user == null) {
|
|
|
+ user = getUser(userId, name, subCompanyList, departmentList, position, mobile, gender, email);
|
|
|
+ userService.create(user);
|
|
|
+ } else {
|
|
|
+ user = getUser(userId, name, subCompanyList, departmentList, position, mobile, gender, email);
|
|
|
+ userService.update(user);
|
|
|
+ }
|
|
|
+ return "SUCCESS";
|
|
|
+ } catch (WxErrorException e) {
|
|
|
+ return e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String createOrUpdate(String userId, String name, String[] firstOrgList, String[] secondOrgList,
|
|
|
+ String[] thirdOrgList, String[] fourthOrgList, String position, String mobile, String gender,
|
|
|
+ String email, String telephone) {
|
|
|
+ WxCpUser user = getById(userId);
|
|
|
+ try {
|
|
|
+ if (user == null) {
|
|
|
+ user = getUser(userId, name, firstOrgList, secondOrgList, thirdOrgList, fourthOrgList, position, mobile,
|
|
|
+ gender, email, telephone);
|
|
|
+ userService.create(user);
|
|
|
+ } else {
|
|
|
+ user = getUser(userId, name, firstOrgList, secondOrgList, thirdOrgList, fourthOrgList, position, mobile,
|
|
|
+ gender, email, telephone);
|
|
|
+ userService.update(user);
|
|
|
+ }
|
|
|
+ return "SUCCESS";
|
|
|
+ } catch (WxErrorException e) {
|
|
|
+ return e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static WxCpUser getUser(String userId, String name, String[] firstOrgList, String[] secondOrgList,
|
|
|
+ String[] thirdOrgList, String[] fourthOrgList, String position, String mobile, String gender, String email, String telephone)
|
|
|
+ throws WxErrorException {
|
|
|
+ WxCpUser user = new WxCpUser();
|
|
|
+ if (Tools.notEmpty(userId)) {
|
|
|
+ user.setUserId(userId);
|
|
|
+ }
|
|
|
+ if (Tools.notEmpty(name)) {
|
|
|
+ user.setName(name);
|
|
|
+ }
|
|
|
+ Long[] departIds = new Long[firstOrgList.length];
|
|
|
+ for (int i = 0; i < firstOrgList.length; i++) {
|
|
|
+ departIds[i] = WXCpDepartUtil.create(firstOrgList[i], secondOrgList[i], thirdOrgList[i],
|
|
|
+ fourthOrgList[i]);
|
|
|
+ }
|
|
|
+ user.setDepartIds(departIds);
|
|
|
+ if (Tools.notEmpty(position)) {
|
|
|
+ user.setPosition(position);
|
|
|
+ }
|
|
|
+ if (Tools.notEmpty(mobile)) {
|
|
|
+ user.setMobile(mobile);
|
|
|
+ }
|
|
|
+ if (Tools.notEmpty(gender)) {
|
|
|
+ user.setGender(Gender.MALE.getGenderName().equals(gender) ? Gender.MALE : Gender.FEMALE);
|
|
|
+ }
|
|
|
+ if (Tools.notEmpty(email)) {
|
|
|
+ user.setEmail(email);
|
|
|
+ }
|
|
|
+ if (Tools.notEmpty(telephone)) {
|
|
|
+ user.setTelephone(telephone);
|
|
|
+ }
|
|
|
+ user.setToInvite(false);
|
|
|
+ ExternalAttribute attribute = ExternalAttribute.builder().name("所属公司").type(0).value(firstOrgList[0]).build();
|
|
|
+ user.addExternalAttr(attribute);
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void updateUser(WxCpUser user) throws WxErrorException {
|
|
|
+ userService.update(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static WxCpUser getUser(String userId, String name, String[] subCompanyList, String[] departmentList,
|
|
|
+ String position, String mobile, String gender, String email) throws WxErrorException {
|
|
|
+ WxCpUser user = new WxCpUser();
|
|
|
+ if (Tools.notEmpty(userId)) {
|
|
|
+ user.setUserId(userId);
|
|
|
+ }
|
|
|
+ if (Tools.notEmpty(name)) {
|
|
|
+ user.setName(name);
|
|
|
+ }
|
|
|
+ Long[] departIds = new Long[subCompanyList.length];
|
|
|
+ for (int i = 0; i < subCompanyList.length; i++) {
|
|
|
+ departIds[i] = WXCpDepartUtil.create(subCompanyList[i], departmentList[i]);
|
|
|
+ }
|
|
|
+ user.setDepartIds(departIds);
|
|
|
+ if (Tools.notEmpty(position)) {
|
|
|
+ user.setPosition(position);
|
|
|
+ }
|
|
|
+ if (Tools.notEmpty(mobile)) {
|
|
|
+ user.setMobile(mobile);
|
|
|
+ }
|
|
|
+ if (Tools.notEmpty(gender)) {
|
|
|
+ user.setGender(Gender.MALE.getGenderName().equals(gender) ? Gender.MALE : Gender.FEMALE);
|
|
|
+ }
|
|
|
+ if (Tools.notEmpty(email)) {
|
|
|
+ user.setEmail(email);
|
|
|
+ }
|
|
|
+ user.setToInvite(false);
|
|
|
+ ExternalAttribute attribute = ExternalAttribute.builder().name("所属公司").type(0).value(subCompanyList[0]).build();
|
|
|
+ user.addExternalAttr(attribute);
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static WxCpUser getById(String userId) {
|
|
|
+ try {
|
|
|
+ return userService.getById(userId);
|
|
|
+ } catch (WxErrorException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void delete(String userId) {
|
|
|
+ try {
|
|
|
+ userService.delete(userId);
|
|
|
+ } catch (WxErrorException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<WxCpUser> listByDepartment(Long departId) throws WxErrorException {
|
|
|
+ return userService.listByDepartment(departId, true, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ createOrUpdate("yangbin", "杨斌", new String[] { "华人时代" }, new String[] { "管理层" }, "", "", "男",
|
|
|
+ "13671179900@jiinfo.cn");
|
|
|
+ }
|
|
|
+}
|