|
@@ -21,22 +21,19 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import me.zhengjie.modules.dm.user.domain.DmUser;
|
|
|
-import me.zhengjie.modules.dm.user.service.DmUserService;
|
|
|
-import me.zhengjie.modules.dm.user.service.dto.DmUserDto;
|
|
|
+import me.zhengjie.modules.dm.user.repository.DmUserRepository;
|
|
|
import me.zhengjie.modules.system.domain.Dept;
|
|
|
import me.zhengjie.modules.system.domain.Job;
|
|
|
import me.zhengjie.modules.system.domain.Role;
|
|
|
import me.zhengjie.modules.system.domain.User;
|
|
|
+import me.zhengjie.modules.system.repository.DeptRepository;
|
|
|
+import me.zhengjie.modules.system.repository.UserRepository;
|
|
|
import me.zhengjie.modules.system.service.DeptService;
|
|
|
-import me.zhengjie.modules.system.service.UserService;
|
|
|
-import me.zhengjie.modules.system.service.dto.DeptDto;
|
|
|
-import me.zhengjie.modules.system.service.dto.UserDto;
|
|
|
import me.zhengjie.utils.SecurityUtils;
|
|
|
-import me.zhengjie.utils.StringUtils;
|
|
|
-import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.util.ObjectUtils;
|
|
|
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
@@ -48,100 +45,122 @@ import java.util.*;
|
|
|
@Component
|
|
|
public class ZkDataSyncTask {
|
|
|
|
|
|
- private final String erpApiUrl = "http://121.31.41.88:29312";
|
|
|
- private final String sign = "e34b2f59-0e9d-45ed-a32a-f4abd4381000";
|
|
|
- private final String header_key = "X_AUTO_USER_INFO_HEAD";
|
|
|
- private final String header_value = "{\"id\":\"anonymous\",\"tenantId\":\"caih\"}";
|
|
|
private final String key = "DMERPYT!@#$QWER2021+{:>";
|
|
|
+ private final String zkApiUrl = "http://192.168.1.14:6112";
|
|
|
+ private final String header_key = "X_YT_ACCESS_TOKEN";
|
|
|
|
|
|
- private final DeptService deptService;
|
|
|
- private final UserService userService;
|
|
|
- private final DmUserService dmUserService;
|
|
|
- private final PasswordEncoder passwordEncoder;
|
|
|
+ private final UserRepository userRepository;
|
|
|
+ private final DmUserRepository dmUserRepository;
|
|
|
+ private final DeptRepository deptRepository;
|
|
|
|
|
|
- public void run() {
|
|
|
+ public void run(){
|
|
|
log.info("deptsync 执行开始");
|
|
|
- deptsync();
|
|
|
+ deptSync();
|
|
|
log.info("deptsync 执行结束");
|
|
|
|
|
|
log.info("usersync 执行开始");
|
|
|
- usersync();
|
|
|
+ userSync();
|
|
|
log.info("usersync 执行结束");
|
|
|
}
|
|
|
|
|
|
- private JSONObject getDatas(String api, int pagenum, int pagesize) {
|
|
|
- String res = HttpRequest.get(erpApiUrl + api)
|
|
|
- .header(header_key, header_value)
|
|
|
- .body("{\n" +
|
|
|
- " \"formData\":{\n" +
|
|
|
- " \"pageNum\":" + pagenum + ",\n" +
|
|
|
- " \"pageSize\":" + pagesize + "\n" +
|
|
|
- " },\n" +
|
|
|
- " \"product\":\"17\",\n" +
|
|
|
- " \"sign\":\"" + sign + "\"\n" +
|
|
|
- "}").execute().body();
|
|
|
+ public void runDept(){
|
|
|
+ log.info("deptsync 执行开始");
|
|
|
+ deptSyncAll();
|
|
|
+ log.info("deptsync 执行结束");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void runUser(){
|
|
|
+ log.info("usersync 执行开始");
|
|
|
+ userSyncAll();
|
|
|
+ log.info("usersync 执行结束");
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject getDatas(String api,JSONArray time, int pageNum, int pageSize,int type) {
|
|
|
+ Map<String,Object> tokenData = SecurityUtils.getToken(key);
|
|
|
+ JSONObject body = new JSONObject();
|
|
|
+ body.put("timestamp",tokenData.get("timestamp"));
|
|
|
+ body.put("nonce",tokenData.get("nonce"));
|
|
|
+ JSONObject query = new JSONObject();
|
|
|
+ if(type == 1){
|
|
|
+ query.put("updateTime",time);
|
|
|
+ }
|
|
|
+ body.put("query",query);
|
|
|
+
|
|
|
+ String res = HttpRequest.get(zkApiUrl + api+"?page="+pageNum+"&size="+pageSize)
|
|
|
+ .header(header_key, tokenData.get("token").toString())
|
|
|
+ .body(body.toString()).execute().body();
|
|
|
JSONObject json = JSONObject.parseObject(res);
|
|
|
+ log.info("json:"+json);
|
|
|
return json;
|
|
|
}
|
|
|
|
|
|
- private int gettotal(String api) {
|
|
|
- JSONObject json = getDatas(api, 0, 100);
|
|
|
- return json.getJSONObject("page").getInteger("totalCount");
|
|
|
+ /**
|
|
|
+ * 部门数据同步
|
|
|
+ */
|
|
|
+ private void deptSync() {
|
|
|
+ String api = "/api/thirdparty/v1/user/getzkdeps";
|
|
|
+
|
|
|
+ JSONArray createTme = new JSONArray();
|
|
|
+ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY,-24);
|
|
|
+ String yesterdayDate = dateFormat.format(calendar.getTime());
|
|
|
+
|
|
|
+ String todayDate = dateFormat.format(new Date());
|
|
|
+
|
|
|
+ createTme.add(yesterdayDate);
|
|
|
+ createTme.add(todayDate);
|
|
|
+
|
|
|
+ //计算总页数
|
|
|
+ int total = getDatas(api,createTme,0,100,1).getJSONObject("data").getInteger("totalElements");
|
|
|
+ int pageSize = 100;
|
|
|
+ int pageTotal = (total / pageSize);
|
|
|
+ if (total % pageSize != 0) {
|
|
|
+ pageTotal++;
|
|
|
+ }
|
|
|
+ log.info("pageTotal:"+pageTotal);
|
|
|
+
|
|
|
+ //按页获取
|
|
|
+ for (int k = 0; k < pageTotal; k++) {
|
|
|
+ JSONObject json = getDatas(api, createTme,k,pageSize,1);
|
|
|
+ JSONObject data = json.getJSONObject("data");
|
|
|
+ JSONArray content = data.getJSONArray("content");
|
|
|
+ for (int i = 0; i < content.size(); i++) {
|
|
|
+ Dept dept = JSONObject.parseObject(content.getString(i), Dept.class);
|
|
|
+ deptRepository.save(dept);
|
|
|
+// DeptDto deptDto = deptService.findById(dept.getId());
|
|
|
+// if (deptDto != null) {
|
|
|
+// deptService.update(dept);
|
|
|
+// } else {
|
|
|
+// deptService.create(dept);
|
|
|
+// }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 部门数据同步
|
|
|
+ * 部门数据同步(全量)
|
|
|
*/
|
|
|
- private void deptsync() {
|
|
|
- String api = "/api-third-party/busi/comm/usky/queryGroup";
|
|
|
+ private void deptSyncAll() {
|
|
|
+ String api = "/api/thirdparty/v1/user/getzkdeps";
|
|
|
|
|
|
//计算总页数
|
|
|
- int total = gettotal(api);
|
|
|
- int pagesize = 100;
|
|
|
- int pagetotal = (total / pagesize);
|
|
|
- if (total % pagesize != 0) {
|
|
|
- pagetotal++;
|
|
|
+ int total = getDatas(api,null,0,100,2).getJSONObject("data").getInteger("totalElements");
|
|
|
+ int pageSize = 100;
|
|
|
+ int pageTotal = (total / pageSize);
|
|
|
+ if (total % pageSize != 0) {
|
|
|
+ pageTotal++;
|
|
|
}
|
|
|
+ log.info("pageTotal:"+pageTotal);
|
|
|
|
|
|
//按页获取
|
|
|
- for (int k = 0; k < pagetotal; k++) {
|
|
|
- JSONObject json = getDatas(api, k, pagesize);
|
|
|
- JSONArray datas = json.getJSONArray("data");
|
|
|
- JSONObject item;
|
|
|
- Dept dept;
|
|
|
- for (int i = 0; i < datas.size(); i++) {
|
|
|
- item = datas.getJSONObject(i);
|
|
|
- dept = new Dept();
|
|
|
-
|
|
|
- if (StringUtils.isBlank(item.getString("groupId"))) {
|
|
|
- dept.setId(UUID.randomUUID().toString());
|
|
|
- } else {
|
|
|
- dept.setId(item.getString("groupId"));
|
|
|
- }
|
|
|
- dept.setName(item.getString("groupName"));
|
|
|
-
|
|
|
- if (item.getString("parentId").equals("~")) {
|
|
|
- dept.setPid(null);
|
|
|
- } else {
|
|
|
- dept.setPid(item.getString("parentId"));
|
|
|
- }
|
|
|
- dept.setDeptSort(item.getInteger("sort"));
|
|
|
- dept.setEnabled(item.getBoolean("groupStatus"));
|
|
|
-// dept.setTreeNames(item.getString("groupNamePath"));
|
|
|
-// dept.setTreeIds(item.getString("groupIdPath"));
|
|
|
-// dept.setDeptType(item.getInteger("groupType"));
|
|
|
- dept.setDescription(item.getString("groupName"));
|
|
|
-
|
|
|
- try {
|
|
|
- DeptDto deptDto = deptService.findById(item.getString("groupId"));
|
|
|
- if (deptDto != null) {
|
|
|
- deptService.update(dept);
|
|
|
- } else {
|
|
|
- deptService.create(dept);
|
|
|
- }
|
|
|
- } catch (Exception ex) {
|
|
|
- System.out.println(JSONObject.toJSON(dept));
|
|
|
- }
|
|
|
+ for (int k = 0; k < pageTotal; k++) {
|
|
|
+ JSONObject json = getDatas(api,null,k,pageSize,2);
|
|
|
+ JSONObject data = json.getJSONObject("data");
|
|
|
+ JSONArray content = data.getJSONArray("content");
|
|
|
+ for (int i = 0; i < content.size(); i++) {
|
|
|
+ Dept dept = JSONObject.parseObject(content.getString(i), Dept.class);
|
|
|
+ deptRepository.save(dept);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -149,99 +168,111 @@ public class ZkDataSyncTask {
|
|
|
/**
|
|
|
* 人员数据同步
|
|
|
*/
|
|
|
- private void usersync() {
|
|
|
+ private void userSync() {
|
|
|
+
|
|
|
+ String api = "/api/thirdparty/v1/user/getzkusers";
|
|
|
|
|
|
- String api = "/api-third-party/busi/comm/usky/queryUser";
|
|
|
+ JSONArray createTme = new JSONArray();
|
|
|
+ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY,-24);
|
|
|
+ String yesterdayDate = dateFormat.format(calendar.getTime());
|
|
|
+
|
|
|
+ String todayDate = dateFormat.format(new Date());
|
|
|
+
|
|
|
+ createTme.add(yesterdayDate);
|
|
|
+ createTme.add(todayDate);
|
|
|
|
|
|
//计算总页数
|
|
|
- int total = gettotal(api);
|
|
|
- int pagesize = 100;
|
|
|
- int pagetotal = (total / pagesize);
|
|
|
- if (total % pagesize != 0) {
|
|
|
- pagetotal++;
|
|
|
+ int total = getDatas(api,createTme,0,100,1).getJSONObject("data").getInteger("totalElements");
|
|
|
+ int pageSize = 100;
|
|
|
+ int pageTotal = (total / pageSize);
|
|
|
+ if (total % pageSize != 0) {
|
|
|
+ pageTotal++;
|
|
|
}
|
|
|
+ log.info("pageTotal:"+pageTotal);
|
|
|
|
|
|
//按页获取
|
|
|
- for (int k = 0; k < pagetotal; k++) {
|
|
|
- JSONObject json = getDatas(api, k, pagesize);
|
|
|
- JSONArray datas = json.getJSONArray("data");
|
|
|
- JSONObject item;
|
|
|
- User user;
|
|
|
- UserDto userDto;
|
|
|
- Dept dept;
|
|
|
- DmUser dmUser;
|
|
|
- DmUserDto dmUserDto;
|
|
|
- for (int i = 0; i < datas.size(); i++) {
|
|
|
- item = datas.getJSONObject(i);
|
|
|
- user = new User();
|
|
|
- user.setId(item.getString("userId"));
|
|
|
-// user.setUsercode(item.getString("usercode"));
|
|
|
- user.setNickName(item.getString("userName"));
|
|
|
- user.setUsername(item.getString("userAccount"));
|
|
|
-
|
|
|
- if(StringUtils.isBlank(item.getString("sex"))){
|
|
|
- user.setGender("");
|
|
|
- }else{
|
|
|
- user.setGender(item.getString("sex").equals("m") ? "男" : "女");
|
|
|
- }
|
|
|
-
|
|
|
- user.setEmail(item.getString("email"));
|
|
|
- user.setPhone(item.getString("mobile"));
|
|
|
- user.setPassword(passwordEncoder.encode("12345678"));
|
|
|
- user.setEnabled(item.getInteger("userStatus") == 1);
|
|
|
- user.setIsAdmin(false);
|
|
|
- user.setOrgPassword("12345678");
|
|
|
- dept = new Dept();
|
|
|
- dept.setId(item.getString("userGroupId"));
|
|
|
- if (StringUtils.isBlank(dept.getId())) {
|
|
|
- dept.setId("0001T110000000000UKL");
|
|
|
- }
|
|
|
- user.setDept(dept);
|
|
|
-
|
|
|
- Set<Job> jobs = new HashSet<Job>();
|
|
|
- Job job = new Job();
|
|
|
- job.setId("10");
|
|
|
- jobs.add(job);
|
|
|
+ for (int k = 0; k < pageTotal; k++) {
|
|
|
+ JSONObject json = getDatas(api, createTme,k,pageSize,1);
|
|
|
+ JSONObject data = json.getJSONObject("data");
|
|
|
+ JSONArray content = data.getJSONArray("content");
|
|
|
+ for (int i = 0; i < content.size(); i++) {
|
|
|
+ User user = new User();
|
|
|
+ JSONObject userData = content.getJSONObject(i);
|
|
|
+
|
|
|
+ user.setId(userData.getString("id"));
|
|
|
+ Set<Job> jobs = new HashSet<>(JSONArray.parseArray(userData.getString("jobs"),Job.class));
|
|
|
user.setJobs(jobs);
|
|
|
+ Set<Role> roles = new HashSet<>(JSONArray.parseArray(userData.getString("roles"),Role.class));
|
|
|
+ user.setRoles(roles);
|
|
|
+ user.setGender(userData.getString("sex"));
|
|
|
+ user.setDept(JSONObject.parseObject(userData.getString("dept"),Dept.class));
|
|
|
+ user.setEmail(userData.getString("email"));
|
|
|
+ user.setNickName(userData.getString("name"));
|
|
|
+ user.setPhone(userData.getString("contacts"));
|
|
|
+
|
|
|
+ DmUser dmUser = new DmUser();
|
|
|
+ dmUser.setDept(user.getDept());
|
|
|
+ dmUser.setId(user.getId());
|
|
|
+ dmUser.setEmail(user.getEmail());
|
|
|
+ dmUser.setContacts(user.getPhone());
|
|
|
+ dmUser.setName(user.getNickName());
|
|
|
+ dmUser.setSex(user.getGender());
|
|
|
+ DmUser newDmUser = dmUserRepository.save(dmUser);
|
|
|
+ user.setDmUser(newDmUser);
|
|
|
+ userRepository.save(user);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 人员数据同步(全量)
|
|
|
+ */
|
|
|
+ private void userSyncAll() {
|
|
|
+
|
|
|
+ String api = "/api/thirdparty/v1/user/getzkusers";
|
|
|
|
|
|
- Set<Role> roles = new HashSet<Role>();
|
|
|
+ //计算总页数
|
|
|
+ int total = getDatas(api,null,0,100,2).getJSONObject("data").getInteger("totalElements");
|
|
|
+ int pageSize = 100;
|
|
|
+ int pageTotal = (total / pageSize);
|
|
|
+ if (total % pageSize != 0) {
|
|
|
+ pageTotal++;
|
|
|
+ }
|
|
|
+ log.info("pageTotal:"+pageTotal);
|
|
|
+
|
|
|
+ //按页获取
|
|
|
+ for (int k = 0; k < pageTotal; k++) {
|
|
|
+ JSONObject json = getDatas(api,null,k,pageSize,2);
|
|
|
+ JSONObject data = json.getJSONObject("data");
|
|
|
+ JSONArray content = data.getJSONArray("content");
|
|
|
+ for (int i = 0; i < content.size(); i++) {
|
|
|
+ User user = new User();
|
|
|
+ JSONObject userData = content.getJSONObject(i);
|
|
|
|
|
|
- Role role = new Role();
|
|
|
- role.setId("2");//普通用户
|
|
|
- roles.add(role);
|
|
|
+ user.setId(userData.getString("id"));
|
|
|
+ Set<Job> jobs = new HashSet<>(JSONArray.parseArray(userData.getString("jobs"),Job.class));
|
|
|
+ user.setJobs(jobs);
|
|
|
+ Set<Role> roles = new HashSet<>(JSONArray.parseArray(userData.getString("roles"),Role.class));
|
|
|
user.setRoles(roles);
|
|
|
- dmUser = new DmUser();
|
|
|
- dmUser.setDept(dept);
|
|
|
+ user.setGender(userData.getString("sex"));
|
|
|
+ user.setDept(JSONObject.parseObject(userData.getString("dept"),Dept.class));
|
|
|
+ user.setEmail(userData.getString("email"));
|
|
|
+ user.setNickName(userData.getString("name"));
|
|
|
+ user.setPhone(userData.getString("contacts"));
|
|
|
+
|
|
|
+ DmUser dmUser = new DmUser();
|
|
|
+ dmUser.setDept(user.getDept());
|
|
|
dmUser.setId(user.getId());
|
|
|
dmUser.setEmail(user.getEmail());
|
|
|
dmUser.setContacts(user.getPhone());
|
|
|
dmUser.setName(user.getNickName());
|
|
|
dmUser.setSex(user.getGender());
|
|
|
-// dmUser.setUsercode(user.getUsercode());
|
|
|
- dmUser.setJobs(jobs);
|
|
|
- dmUser.setRoles(roles);
|
|
|
-
|
|
|
- try {
|
|
|
- //1、入用户库
|
|
|
- dmUserDto = dmUserService.findById(dmUser.getId());
|
|
|
-
|
|
|
- if (dmUserDto != null) {
|
|
|
- dmUserService.update(dmUser);
|
|
|
- } else {
|
|
|
- dmUserService.create(dmUser);
|
|
|
- }
|
|
|
- user.setDmUser(dmUser);
|
|
|
-
|
|
|
- //2、入账户库并与用户库关联
|
|
|
- userDto = userService.findById(user.getId());
|
|
|
- if (userDto != null) {
|
|
|
- userService.update(user);
|
|
|
- } else {
|
|
|
- userService.create(user);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ DmUser newDmUser = dmUserRepository.save(dmUser);
|
|
|
+ user.setDmUser(newDmUser);
|
|
|
+ userRepository.save(user);
|
|
|
}
|
|
|
}
|
|
|
}
|