You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
6.9 KiB
179 lines
6.9 KiB
2 years ago
|
package com.fr.plugin.utils;
|
||
|
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.plugin.dao.MyUserDao;
|
||
|
import com.fr.plugin.dao.MyYiTuanDao;
|
||
|
import com.fr.plugin.entitys.YTUserEntity;
|
||
|
import com.fr.plugin.entitys.YTYiTuanEntity;
|
||
|
import com.fr.plugin.yt.MyCoreDBAccess;
|
||
|
import com.fr.stable.db.action.DBAction;
|
||
|
import com.fr.stable.db.dao.DAOContext;
|
||
|
import com.fr.stable.query.QueryFactory;
|
||
|
import com.fr.stable.query.condition.QueryCondition;
|
||
|
import com.fr.stable.query.restriction.RestrictionFactory;
|
||
|
import org.json.JSONArray;
|
||
|
import org.json.JSONObject;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
|
||
|
public class UserSync {
|
||
|
|
||
|
|
||
|
public static void startSyncUser() {
|
||
|
YiTuanMaManager manager = YiTuanMaManager.getInstance();
|
||
|
/**
|
||
|
* 循环所有的agent
|
||
|
*/
|
||
|
List<YTYiTuanEntity> ytYiTuanEntities = null;
|
||
|
try {
|
||
|
ytYiTuanEntities = MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<List<YTYiTuanEntity>>() {
|
||
|
@Override
|
||
|
public List<YTYiTuanEntity> run(DAOContext daoContext) throws Exception {
|
||
|
return daoContext.getDAO(MyYiTuanDao.class).find(QueryFactory.create());
|
||
|
}
|
||
|
});
|
||
|
|
||
|
if (ytYiTuanEntities.isEmpty()) {
|
||
|
FineLoggerFactory.getLogger().info(" 用户同步 未配置任何的一团企业配置 ");
|
||
|
return;
|
||
|
}
|
||
|
List<String> fromCloud = new ArrayList<>();
|
||
|
for (YTYiTuanEntity ytYiTuanEntity : ytYiTuanEntities) {
|
||
|
String access_token = YiTuanMaUtils.getAccessToken(ytYiTuanEntity.getClientId());
|
||
|
FineLoggerFactory.getLogger().info("同步用户获取的accessTOken:{} ", access_token);
|
||
|
try {
|
||
|
JSONArray userInfo = YiTuanMaUtils.getUserInfo(ytYiTuanEntity.getClientId() , access_token);
|
||
|
int length = userInfo.length();
|
||
|
FineLoggerFactory.getLogger().info("同步用户获取的数量:{} ", length);
|
||
|
for (int i = 0; i < length; i++) {
|
||
|
JSONObject userModel = userInfo.getJSONObject(i);
|
||
|
String userid;
|
||
|
if (userModel.has("id")) {
|
||
|
userid = userModel.getString("id");
|
||
|
userModel.put("userId", userid);
|
||
|
userModel.remove("id");
|
||
|
} else {
|
||
|
userid = userModel.getString("userId");
|
||
|
}
|
||
|
fromCloud.add(userid);
|
||
|
String name = userModel.getString("name");
|
||
|
String mobile = userModel.getString("mobile");
|
||
|
YTUserEntity entity = new YTUserEntity();
|
||
|
entity.setId(userid);
|
||
|
entity.setName(name);
|
||
|
entity.setPhone(mobile);
|
||
|
FineLoggerFactory.getLogger().info("同步用户:{} name{} mob:{} ", userid, name, mobile);
|
||
|
if (existEntity(userid)) {
|
||
|
update(entity);
|
||
|
} else {
|
||
|
save(entity);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
List<String> allUserId = getAllUserId();
|
||
|
allUserId.removeAll(fromCloud);
|
||
|
for (String id : allUserId) {
|
||
|
FineLoggerFactory.getLogger().info("同步用户--删除用户 {} ", id);
|
||
|
remove(id);
|
||
|
}
|
||
|
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static List<String> getAllUserId() {
|
||
|
try {
|
||
|
List<YTUserEntity> ytUserEntities = MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<List<YTUserEntity>>() {
|
||
|
public List<YTUserEntity> run(DAOContext content) throws Exception {
|
||
|
return content.getDAO(MyUserDao.class).find(QueryFactory.create());
|
||
|
}
|
||
|
});
|
||
|
int size = ytUserEntities.size();
|
||
|
ArrayList<String> objects = new ArrayList<>(size);
|
||
|
for (int i = 0; i < size; i++) {
|
||
|
YTUserEntity entity = ytUserEntities.get(i);
|
||
|
objects.add(entity.getId());
|
||
|
}
|
||
|
return objects;
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
return new ArrayList<>();
|
||
|
}
|
||
|
|
||
|
private static boolean existEntity(String id) {
|
||
|
try {
|
||
|
YTUserEntity entity = MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<YTUserEntity>() {
|
||
|
public YTUserEntity run(DAOContext content) throws Exception {
|
||
|
return content.getDAO(MyUserDao.class).findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("id", id)));
|
||
|
}
|
||
|
});
|
||
|
return entity != null;
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
private static void remove(String id) {
|
||
|
try {
|
||
|
MyCoreDBAccess.getAccessor().runDMLAction(new DBAction<YTUserEntity>() {
|
||
|
public YTUserEntity run(DAOContext content) throws Exception {
|
||
|
content.getDAO(MyUserDao.class).remove(id);
|
||
|
return null;
|
||
|
}
|
||
|
});
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static void save(YTUserEntity entity) {
|
||
|
try {
|
||
|
MyCoreDBAccess.getAccessor().runDMLAction(new DBAction<YTUserEntity>() {
|
||
|
public YTUserEntity run(DAOContext content) throws Exception {
|
||
|
content.getDAO(MyUserDao.class).add(entity);
|
||
|
return null;
|
||
|
}
|
||
|
});
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static void update(YTUserEntity entity) {
|
||
|
try {
|
||
|
MyCoreDBAccess.getAccessor().runDMLAction(new DBAction<YTUserEntity>() {
|
||
|
public YTUserEntity run(DAOContext content) throws Exception {
|
||
|
content.getDAO(MyUserDao.class).update(entity);
|
||
|
return null;
|
||
|
}
|
||
|
});
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static YTUserEntity findById(String id) {
|
||
|
try {
|
||
|
YTUserEntity entity = MyCoreDBAccess.getAccessor().runDMLAction(new DBAction<YTUserEntity>() {
|
||
|
public YTUserEntity run(DAOContext content) throws Exception {
|
||
|
QueryCondition queryCondition = QueryFactory.create().addRestriction(RestrictionFactory.eq("id", id));
|
||
|
return content.getDAO(MyUserDao.class).findOne(queryCondition);
|
||
|
}
|
||
|
});
|
||
|
return entity;
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
}
|