|
|
@@ -0,0 +1,216 @@
|
|
|
+package com.launch.job.task;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.launch.common.core.exception.BaseException;
|
|
|
+import com.launch.common.core.utils.StringUtils;
|
|
|
+import com.launch.job.config.BaseContextHandler;
|
|
|
+import com.launch.job.config.GetUserInfoUrl;
|
|
|
+import com.launch.job.domain.SysDeptENT;
|
|
|
+import com.launch.job.domain.SysUserENT;
|
|
|
+import com.launch.job.service.SysDeptService;
|
|
|
+import com.launch.job.service.SysUserService;
|
|
|
+import nonapi.io.github.classgraph.json.JSONUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.http.*;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+import java.util.concurrent.locks.ReentrantLock;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 定时任务调度测试
|
|
|
+ *
|
|
|
+ * @author launch
|
|
|
+ */
|
|
|
+@Component("DeptAndEmpTask")
|
|
|
+public class DeptAndEmpTask
|
|
|
+{
|
|
|
+ @Autowired
|
|
|
+ private GetUserInfoUrl getUserInfoUrl;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysDeptService sysDeptService;
|
|
|
+ @Autowired
|
|
|
+ private SysUserService sysUserService;
|
|
|
+
|
|
|
+ //调用接口
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取部门信息
|
|
|
+ */
|
|
|
+ public void getDept()
|
|
|
+ {
|
|
|
+ // 执行HTTP请求
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate
|
|
|
+ .getForEntity(getUserInfoUrl.getDeptUrl(),String.class);
|
|
|
+ String body = responseEntity.getBody();
|
|
|
+ if(body != null){
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
+ if(jsonObject.containsKey("code")&&null!= jsonObject.get("code") && jsonObject.get("code").toString().equals("200")){
|
|
|
+ Object data = jsonObject.get("data");
|
|
|
+ if (data != null) {
|
|
|
+ JSONArray objects = JSONUtil.parseArray(data);
|
|
|
+ ArrayList<SysDeptENT> sysDeptENTS = new ArrayList<>();
|
|
|
+ AtomicInteger atomicInteger = new AtomicInteger(1);
|
|
|
+ objects.forEach(x->{
|
|
|
+ JSONObject jsonObject1 = JSONUtil.parseObj(x);
|
|
|
+ SysDeptENT sysDeptENT = JSONUtil.toBean(jsonObject1, SysDeptENT.class);
|
|
|
+ sysDeptENT.setCreateTime(new Date());
|
|
|
+ sysDeptENT.setOrderNum(atomicInteger.get());
|
|
|
+ atomicInteger.incrementAndGet();
|
|
|
+ sysDeptENTS.add(sysDeptENT);
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ if(CollectionUtil.isNotEmpty(sysDeptENTS)){
|
|
|
+ List<String> collect = sysDeptENTS.stream().map(SysDeptENT::getDepartmentId).collect(Collectors.toList());
|
|
|
+ LambdaQueryWrapper<SysDeptENT> in = new LambdaQueryWrapper<SysDeptENT>().in(SysDeptENT::getDepartmentId, collect);
|
|
|
+ if (CollectionUtil.isNotEmpty(collect)) {
|
|
|
+ int count = sysDeptService.count(in);
|
|
|
+ boolean remove=true;
|
|
|
+ if (count > 0) {
|
|
|
+ remove = sysDeptService.remove(in);
|
|
|
+ }
|
|
|
+ boolean b = sysDeptService.saveBatch(sysDeptENTS);
|
|
|
+ if(!b || (count>0 && !remove)){
|
|
|
+ throw new BaseException("批量保存失败!");
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new BaseException("更新失败!");
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取部门ID
|
|
|
+ */
|
|
|
+ public ArrayList<String> recursion( String children){
|
|
|
+ ArrayList<String> rt = new ArrayList<>();
|
|
|
+ if (StringUtils.isNotEmpty(children)) {
|
|
|
+ //解析数组json
|
|
|
+ JSONArray objects = JSONUtil.parseArray(children);
|
|
|
+ if(!objects.isEmpty()){
|
|
|
+ //遍历json数组
|
|
|
+ objects.forEach(e->{
|
|
|
+ if (null != e) {
|
|
|
+ //解析json
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(e);
|
|
|
+ if (!jsonObject.containsKey("children") || jsonObject.get("children") == null || jsonObject.get("children").toString().trim().equals("")) {
|
|
|
+ if(jsonObject.containsKey("departmentId") ){
|
|
|
+ rt.add(jsonObject.get("departmentId").toString());
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ ArrayList<String> children1 = recursion( jsonObject.get("children").toString());
|
|
|
+ rt.addAll(children1);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return rt;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户信息
|
|
|
+ */
|
|
|
+ public void getUserInfo()
|
|
|
+ {
|
|
|
+ List<SysDeptENT> list = sysDeptService.list();
|
|
|
+ if (CollectionUtil.isNotEmpty(list)) {
|
|
|
+ ArrayList<String> strings = new ArrayList<>();
|
|
|
+ list.forEach(x->{
|
|
|
+ //获取一级子
|
|
|
+ String children = x.getChildren();
|
|
|
+ //为空
|
|
|
+ if (StringUtils.isEmpty(children)) {
|
|
|
+ strings.add(x.getDepartmentId());
|
|
|
+ }else {
|
|
|
+ ArrayList<String> recursion = recursion(children);
|
|
|
+ strings.addAll(recursion);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ //获取到部门id
|
|
|
+ if (CollectionUtil.isNotEmpty(strings)) {
|
|
|
+ ReentrantLock reentrantLock = new ReentrantLock();
|
|
|
+ ArrayList<SysUserENT> sysUserENTS = new ArrayList<>();
|
|
|
+ strings.forEach(w->{
|
|
|
+ try {
|
|
|
+ reentrantLock.lock();
|
|
|
+ Thread.sleep(500);//對服务器的保护
|
|
|
+
|
|
|
+ // 执行HTTP请求
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate
|
|
|
+ .getForEntity(getUserInfoUrl.getUserInfo()+"?page=1&size=1000&departmentId="+w,String.class);
|
|
|
+ String body = responseEntity.getBody();
|
|
|
+ if(body != null) {
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
+ if (jsonObject.containsKey("code") && null != jsonObject.get("code") && jsonObject.get("code").toString().equals("200")) {
|
|
|
+ Object data = jsonObject.get("data");
|
|
|
+ if (data != null && !data.toString().trim().equals("")) {
|
|
|
+ JSONArray objects = JSONUtil.parseArray(JSONUtil.parseObj(data).get("records"));
|
|
|
+ if(!objects.isEmpty()){
|
|
|
+ objects.forEach(x->{
|
|
|
+ if (x != null) {
|
|
|
+ JSONObject jsonObject1 = JSONUtil.parseObj(x);
|
|
|
+ SysUserENT sysUserENT = JSONUtil.toBean(jsonObject1, SysUserENT.class);
|
|
|
+ sysUserENT.setCreateTime(new Date());
|
|
|
+ sysUserENTS.add(sysUserENT);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ throw new BaseException(e.getMessage());
|
|
|
+ }finally {
|
|
|
+ reentrantLock.unlock();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (CollectionUtil.isNotEmpty(sysUserENTS)) {
|
|
|
+ List<String> collect = sysUserENTS.stream().map(SysUserENT::getUserId).collect(Collectors.toList());
|
|
|
+ LambdaQueryWrapper<SysUserENT> in = new LambdaQueryWrapper<SysUserENT>().in(SysUserENT::getUserId, collect);
|
|
|
+ int count = sysUserService.count(in);
|
|
|
+ boolean remove=true;
|
|
|
+ if (count > 0) {
|
|
|
+ remove = sysUserService.remove(in);
|
|
|
+ }
|
|
|
+ boolean b = sysUserService.saveBatch(sysUserENTS);
|
|
|
+ if(!b || (count>0 && !remove)){
|
|
|
+ throw new BaseException("批量保存失败!");
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }}
|
|
|
+
|
|
|
+
|
|
|
+ public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
|
|
|
+ {
|
|
|
+ System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void ryParams(String params)
|
|
|
+ {
|
|
|
+ System.out.println("执行有参方法:" + params);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|