|
@@ -6,7 +6,7 @@ import com.tzld.piaoquan.wecom.common.constant.TimeConstant;
|
|
|
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.bo.StaffWithUserParam;
|
|
|
+import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
|
|
|
import com.tzld.piaoquan.wecom.model.po.*;
|
|
|
import com.tzld.piaoquan.wecom.service.AccessTokenService;
|
|
|
import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
|
|
@@ -17,7 +17,6 @@ import com.xxl.job.core.handler.annotation.XxlJob;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.checkerframework.checker.units.qual.A;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
@@ -50,24 +49,24 @@ public class WeComUserDataJob {
|
|
|
|
|
|
@XxlJob("insertStaffWithUserJob")
|
|
|
public ReturnT<String> insertStaffWithUser(String param) {
|
|
|
- StaffWithUserParam staffWithUserParam = new StaffWithUserParam();
|
|
|
+ XxlJobParam xxlJobParam = new XxlJobParam();
|
|
|
if (StringUtils.isNotEmpty(param)) {
|
|
|
- staffWithUserParam = JSONObject.parseObject(param, StaffWithUserParam.class);
|
|
|
+ xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
|
|
|
}
|
|
|
- if (staffWithUserParam.getStartTime() == null) {
|
|
|
- staffWithUserParam.setStartTime(1720540800L);
|
|
|
+ if (xxlJobParam.getStartTime() == null) {
|
|
|
+ xxlJobParam.setStartTime(1720540800L);
|
|
|
}
|
|
|
- if (staffWithUserParam.getEndTime() == null) {
|
|
|
- staffWithUserParam.setEndTime(System.currentTimeMillis() / 1000);
|
|
|
+ if (xxlJobParam.getEndTime() == null) {
|
|
|
+ xxlJobParam.setEndTime(System.currentTimeMillis() / 1000);
|
|
|
}
|
|
|
StaffExample example = new StaffExample();
|
|
|
StaffExample.Criteria criteria = example.createCriteria();
|
|
|
- if (staffWithUserParam.getStaffId() != null) {
|
|
|
- criteria.andIdEqualTo(staffWithUserParam.getStaffId());
|
|
|
+ if (xxlJobParam.getStaffId() != null) {
|
|
|
+ criteria.andIdEqualTo(xxlJobParam.getStaffId());
|
|
|
}
|
|
|
List<Staff> staffList = staffMapper.selectByExample(example);
|
|
|
for (Staff staff : staffList) {
|
|
|
- insertAllUser(staffWithUserParam.getStartTime(), staffWithUserParam.getEndTime(), staff);
|
|
|
+ insertAllUser(xxlJobParam.getStartTime(), xxlJobParam.getEndTime(), staff);
|
|
|
}
|
|
|
return ReturnT.SUCCESS;
|
|
|
}
|
|
@@ -119,12 +118,12 @@ public class WeComUserDataJob {
|
|
|
staffWithUserMapper.insertList(insertStaffWithUserList);
|
|
|
}
|
|
|
if (jsonArray.size() < size) {
|
|
|
- if (total > sum) {
|
|
|
- log.error("insertAllUser插入数量不足 total = {}, sum={}", total, sum);
|
|
|
- }
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
+ if (total > sum) {
|
|
|
+ log.error("insertAllUser插入数量不足 total = {}, sum={}", total, sum);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
log.error("insertAllUser error", e);
|
|
|
}
|
|
@@ -149,28 +148,28 @@ public class WeComUserDataJob {
|
|
|
|
|
|
@XxlJob("updateStaffWithUserJob")
|
|
|
public ReturnT<String> updateStaffWithUser(String param) {
|
|
|
- StaffWithUserParam staffWithUserParam = new StaffWithUserParam();
|
|
|
+ XxlJobParam xxlJobParam = new XxlJobParam();
|
|
|
if (StringUtils.isNotEmpty(param)) {
|
|
|
- staffWithUserParam = JSONObject.parseObject(param, StaffWithUserParam.class);
|
|
|
+ xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
|
|
|
}
|
|
|
- if (staffWithUserParam.getStartTime() == null) {
|
|
|
+ if (xxlJobParam.getStartTime() == null) {
|
|
|
UserExample userExample = new UserExample();
|
|
|
userExample.setOrderByClause("create_time desc");
|
|
|
userExample.setPage(new Page<>(1, 1));
|
|
|
List<User> userList = userMapper.selectByExample(userExample);
|
|
|
- staffWithUserParam.setStartTime(userList.get(0).getCreateTime().getTime() / 1000 - 2L * TimeConstant.HOUR);
|
|
|
+ xxlJobParam.setStartTime(userList.get(0).getCreateTime().getTime() / 1000 - 2L * TimeConstant.HOUR);
|
|
|
}
|
|
|
- if (staffWithUserParam.getEndTime() == null) {
|
|
|
- staffWithUserParam.setEndTime(System.currentTimeMillis() / 1000);
|
|
|
+ if (xxlJobParam.getEndTime() == null) {
|
|
|
+ xxlJobParam.setEndTime(System.currentTimeMillis() / 1000);
|
|
|
}
|
|
|
StaffExample example = new StaffExample();
|
|
|
StaffExample.Criteria criteria = example.createCriteria();
|
|
|
- if (staffWithUserParam.getStaffId() != null) {
|
|
|
- criteria.andIdEqualTo(staffWithUserParam.getStaffId());
|
|
|
+ if (xxlJobParam.getStaffId() != null) {
|
|
|
+ criteria.andIdEqualTo(xxlJobParam.getStaffId());
|
|
|
}
|
|
|
List<Staff> staffList = staffMapper.selectByExample(example);
|
|
|
for (Staff staff : staffList) {
|
|
|
- updateUser(staffWithUserParam.getStartTime(), staffWithUserParam.getEndTime(), staff);
|
|
|
+ updateUser(xxlJobParam.getStartTime(), xxlJobParam.getEndTime(), staff);
|
|
|
}
|
|
|
return ReturnT.SUCCESS;
|
|
|
}
|
|
@@ -226,12 +225,13 @@ public class WeComUserDataJob {
|
|
|
sum++;
|
|
|
}
|
|
|
if (jsonArray.size() < size) {
|
|
|
- if (total > sum) {
|
|
|
- log.error("updateUser插入数量不足 total = {}, sum={}", total, sum);
|
|
|
- }
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ if (total > sum) {
|
|
|
+ log.error("updateUser插入数量不足 total = {}, sum={}", total, sum);
|
|
|
+ }
|
|
|
} catch (IOException e) {
|
|
|
log.error("updateUser error", e);
|
|
|
}
|
|
@@ -255,4 +255,82 @@ public class WeComUserDataJob {
|
|
|
return httpPoolClientDefault.get(url);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ @XxlJob("deleteUserJob")
|
|
|
+ public ReturnT<String> deleteUserJob(String param) {
|
|
|
+ XxlJobParam xxlJobParam = new XxlJobParam();
|
|
|
+ if (StringUtils.isNotEmpty(param)) {
|
|
|
+ xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
|
|
|
+ }
|
|
|
+ if (xxlJobParam.getStartTime() == null) {
|
|
|
+ UserExample userExample = new UserExample();
|
|
|
+ userExample.createCriteria().andIsDeleteEqualTo(1);
|
|
|
+ userExample.setOrderByClause("deleted_at desc");
|
|
|
+ userExample.setPage(new Page<>(1, 1));
|
|
|
+ List<User> userList = userMapper.selectByExample(userExample);
|
|
|
+ if (CollectionUtils.isEmpty(userList)) {
|
|
|
+ xxlJobParam.setStartTime(1720540800L);
|
|
|
+ } else {
|
|
|
+ xxlJobParam.setStartTime(userList.get(0).getDeletedAt());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (xxlJobParam.getEndTime() == null) {
|
|
|
+ xxlJobParam.setEndTime(System.currentTimeMillis() / 1000);
|
|
|
+ }
|
|
|
+ deleteUser(xxlJobParam.getStartTime(), xxlJobParam.getEndTime());
|
|
|
+ return ReturnT.SUCCESS;
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询删除用户并更新
|
|
|
+ private void deleteUser(long startTime, long endTime) {
|
|
|
+ try {
|
|
|
+ Integer total = getDeleteUserTotal(startTime, endTime);
|
|
|
+ if (total == null || total == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ int page = total / size + 1;
|
|
|
+ int sum = 0;
|
|
|
+ for (int i = 0; i < page; i++) {
|
|
|
+ String res = getDeleteUser(size, i * size, startTime, endTime);
|
|
|
+ log.info("deleteUser size={}, i={}, startTime={}, endTime={}, res={}", size, i, startTime, endTime, res);
|
|
|
+ if (ObjectUtils.isEmpty(res)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
+ JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
|
|
|
+ for (int j = 0; j < jsonArray.size(); j++) {
|
|
|
+ JSONObject staffRelation = jsonArray.getJSONObject(j).getJSONObject("staff_relation");
|
|
|
+ Long deletedAt = staffRelation.getLong("deleted_at");
|
|
|
+ String externalUserId3rdParty = staffRelation.getString("external_user_ext_id");
|
|
|
+ Long userId = userMapper.selectIdByExternalUserId3rdParty(externalUserId3rdParty);
|
|
|
+ User updateUser = new User();
|
|
|
+ updateUser.setId(userId);
|
|
|
+ updateUser.setIsDelete(1);
|
|
|
+ updateUser.setDeletedAt(deletedAt);
|
|
|
+ userMapper.updateByPrimaryKeySelective(updateUser);
|
|
|
+ sum++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (total > sum) {
|
|
|
+ log.error("deleteUser数量不足 total = {}, sum={}", total, sum);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("deleteUser error", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer getDeleteUserTotal(Long startTime, Long endTime) throws IOException {
|
|
|
+ String res = getDeleteUser(1, 0, startTime, endTime);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
+ return jsonObject.getInteger("total_count");
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getDeleteUser(Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
|
|
|
+ String accessToken = accessTokenService.getAccessToken();
|
|
|
+ String url = GET_DELETE_USER_URL
|
|
|
+ + "?access_token=" + accessToken
|
|
|
+ + "&limit=" + limit + "&offset=" + offset + "&start_time=" + startTime + "&end_time=" + endTime;
|
|
|
+ return httpPoolClientDefault.get(url);
|
|
|
+ }
|
|
|
+
|
|
|
}
|