|
@@ -0,0 +1,155 @@
|
|
|
+package com.tzld.piaoquan.wecom.service.Impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.tzld.piaoquan.wecom.dao.mapper.StaffMapper;
|
|
|
+import com.tzld.piaoquan.wecom.dao.mapper.StaffWithUserMapper;
|
|
|
+import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
|
|
|
+import com.tzld.piaoquan.wecom.model.po.*;
|
|
|
+import com.tzld.piaoquan.wecom.service.AccessTokenService;
|
|
|
+import com.tzld.piaoquan.wecom.service.UserService;
|
|
|
+import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
|
|
|
+import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.GET_WE_COM_EXTERNAL_CONTACT_GET;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class UserServiceImpl implements UserService {
|
|
|
+ private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AccessTokenService accessTokenService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StaffWithUserMapper staffWithUserMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StaffMapper staffMapper;
|
|
|
+
|
|
|
+ public void addStaffWithUser(String externalUserId, String staffUserId) {
|
|
|
+ if (StringUtils.isEmpty(externalUserId) || StringUtils.isEmpty(staffUserId)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ StaffExample example = new StaffExample();
|
|
|
+ example.createCriteria().andCarrierIdEqualTo(staffUserId);
|
|
|
+ List<Staff> staffList = staffMapper.selectByExample(example);
|
|
|
+ if (CollectionUtils.isEmpty(staffList)) {
|
|
|
+ log.error("addUserWithStaff staff empty staffUserId={}", staffUserId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Staff staff = staffList.get(0);
|
|
|
+// insertStaffWithUser(externalUserId, staff);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void insertStaffWithUser(String externalUserId, Staff staff) {
|
|
|
+ try {
|
|
|
+ UserExample userExample = new UserExample();
|
|
|
+ userExample.createCriteria().andExternalUserIdEqualTo(externalUserId);
|
|
|
+ List<User> userList = userMapper.selectByExample(userExample);
|
|
|
+ if (CollectionUtils.isEmpty(userList)) {
|
|
|
+ JSONObject userDetail = getUserDetail(externalUserId);
|
|
|
+ if (userDetail == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ JSONObject externalContact = userDetail.getJSONObject("external_contact");
|
|
|
+ JSONArray followUserList = userDetail.getJSONArray("follow_user");
|
|
|
+ Long createAt = null;
|
|
|
+ List<String> staffUserIdList = new ArrayList<>();
|
|
|
+ for (int i = 0; i < followUserList.size(); i++) {
|
|
|
+ JSONObject followUser = followUserList.getJSONObject(i);
|
|
|
+ if (createAt == null) {
|
|
|
+ createAt = followUser.getLong("createtime");
|
|
|
+ } else {
|
|
|
+ createAt = Math.min(createAt, followUser.getLong("createtime"));
|
|
|
+ }
|
|
|
+ String userId = followUser.getString("userid");
|
|
|
+ if (StringUtils.isNotEmpty(userId)) {
|
|
|
+ staffUserIdList.add(userId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String name = externalContact.getString("name");
|
|
|
+ String unionId = externalContact.getString("unionid");
|
|
|
+ String avatar = externalContact.getString("avatar");
|
|
|
+ Integer type = externalContact.getInteger("type");
|
|
|
+ Integer gender = externalContact.getInteger("gender");
|
|
|
+ User user = new User();
|
|
|
+ user.setExternalUserId(externalUserId);
|
|
|
+ user.setCreatedAt(createAt);
|
|
|
+ user.setName(name);
|
|
|
+ user.setUnionId(unionId);
|
|
|
+ user.setAvatar(avatar);
|
|
|
+ user.setType(type);
|
|
|
+ user.setGender(gender);
|
|
|
+ userMapper.insert(user);
|
|
|
+ Long userId = user.getId();
|
|
|
+ if (userId == null) {
|
|
|
+ log.error("insertStaffWithUserJob insert user error user={}", user);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isEmpty(staffUserIdList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (String staffUserId : staffUserIdList) {
|
|
|
+ StaffExample staffExample = new StaffExample();
|
|
|
+ staffExample.createCriteria().andCarrierIdEqualTo(staffUserId);
|
|
|
+ List<Staff> staffList1 = staffMapper.selectByExample(staffExample);
|
|
|
+ if (CollectionUtils.isEmpty(staffList1)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Staff staff1 = staffList1.get(0);
|
|
|
+ Long staffId = staff1.getId();
|
|
|
+ StaffWithUser staffWithUser = new StaffWithUser();
|
|
|
+ staffWithUser.setStaffId(staffId);
|
|
|
+ staffWithUser.setUserId(userId);
|
|
|
+ staffWithUserMapper.insert(staffWithUser);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ User user = userList.get(0);
|
|
|
+ StaffWithUserExample staffWithUserExample = new StaffWithUserExample();
|
|
|
+ staffWithUserExample.createCriteria().andUserIdEqualTo(user.getId()).andStaffIdEqualTo(staff.getId());
|
|
|
+ List<StaffWithUser> staffWithUserList = staffWithUserMapper.selectByExample(staffWithUserExample);
|
|
|
+ if (CollectionUtils.isEmpty(staffWithUserList)) {
|
|
|
+ StaffWithUser staffWithUser = new StaffWithUser();
|
|
|
+ staffWithUser.setStaffId(staff.getId());
|
|
|
+ staffWithUser.setUserId(user.getId());
|
|
|
+ staffWithUserMapper.insert(staffWithUser);
|
|
|
+ } else {
|
|
|
+ StaffWithUser staffWithUser = staffWithUserList.get(0);
|
|
|
+ if (staffWithUser.getIsDelete() == 1) {
|
|
|
+ staffWithUser.setIsDelete(0);
|
|
|
+ staffWithUserMapper.updateByPrimaryKeySelective(staffWithUser);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("insertUser error", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public JSONObject getUserDetail(String externalUserId) throws IOException {
|
|
|
+ String weComAccessToken = accessTokenService.getWeComAccessToken();
|
|
|
+ String url = String.format(GET_WE_COM_EXTERNAL_CONTACT_GET + "?access_token=%s&external_userid=%s", weComAccessToken, externalUserId);
|
|
|
+ String res = httpPoolClientDefault.get(url);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
+ Integer errcode = jsonObject.getInteger("errcode");
|
|
|
+ if (errcode == 0) {
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|