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.
246 lines
10 KiB
246 lines
10 KiB
3 years ago
|
package com.fr.plugin.yt;
|
||
|
|
||
|
import com.fr.decision.authority.AuthorityContext;
|
||
|
import com.fr.decision.authority.data.User;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.log.FineLoggerProvider;
|
||
|
import com.fr.plugin.beans.MyUserBean;
|
||
|
import com.fr.plugin.config.YituanPri;
|
||
|
import com.fr.plugin.dao.MyUserDao;
|
||
|
import com.fr.plugin.entitys.YTUserEntity;
|
||
|
import com.fr.plugin.utils.WebUtils;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
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 java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
import java.util.stream.Collectors;
|
||
|
|
||
|
public class MyUserSyncManager {
|
||
|
List<MyUserBean> list = new ArrayList<>();
|
||
|
|
||
|
public static MyUserSyncManager getInstance() {
|
||
|
return MyUserSyncManager.HOLDER.INSTANCE;
|
||
|
}
|
||
|
|
||
|
private MyUserSyncManager() {
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 同步用户,先把数据库中的所有用户查询出来,
|
||
|
*/
|
||
|
public void synUserList() throws Exception {
|
||
|
YituanPri instance = YituanPri.getInstance();
|
||
|
String eid = instance.getEid();
|
||
|
String readKey = instance.getReadKey();
|
||
|
FineLoggerProvider logger = FineLoggerFactory.getLogger();
|
||
|
if (StringUtils.isNotBlank(eid) && StringUtils.isNotBlank(readKey)) {
|
||
|
int page = 0;
|
||
|
List<String> allIdsFormService = new ArrayList<>();
|
||
|
while (true) {
|
||
|
List<MyUserBean> userByPage = WebUtils.getUserByPage(eid, readKey, page);
|
||
|
if (userByPage.isEmpty()) {
|
||
|
break;
|
||
|
}
|
||
|
for (MyUserBean myUserBean : userByPage) {
|
||
|
String openId = myUserBean.getOpenId();
|
||
|
allIdsFormService.add(openId);
|
||
|
if (userNotExist(openId)) {
|
||
|
YTUserEntity entity = myUserBean.createEntity();
|
||
|
save(entity);
|
||
|
} else {
|
||
|
YTUserEntity entity = myUserBean.createEntity();
|
||
|
update(entity);
|
||
|
}
|
||
|
}
|
||
|
logger.error("同步用户第:{}页", page);
|
||
|
page++;
|
||
|
}
|
||
|
List<String> allUserId = findAllUserId();
|
||
|
allUserId.removeAll(allIdsFormService);//减去服务端的ids,得到一个
|
||
|
if (!allUserId.isEmpty()) {
|
||
|
for (String s : allUserId) {
|
||
|
logger.error("删除了一个用户:{}", s);
|
||
|
remove(s);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private List<String> findAllUserId() throws Exception {
|
||
|
List<YTUserEntity> entityList = MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<List<YTUserEntity>>() {
|
||
|
public List<YTUserEntity> run(DAOContext content) throws Exception {
|
||
|
QueryCondition queryCondition = QueryFactory.create();
|
||
|
return content.getDAO(MyUserDao.class).find(queryCondition);
|
||
|
}
|
||
|
});
|
||
|
List<String> ids = new ArrayList<>(entityList.size());
|
||
|
for (YTUserEntity entity : entityList) {
|
||
|
ids.add(entity.getOpenId());
|
||
|
}
|
||
|
return ids;
|
||
|
}
|
||
|
|
||
|
public void saveOrUpdateUser(MyUserBean userBean) throws Exception {
|
||
|
String openId = userBean.getOpenId();
|
||
|
if (userNotExist(openId)) {
|
||
|
YTUserEntity entity = userBean.createEntity();
|
||
|
save(entity);
|
||
|
} else {
|
||
|
YTUserEntity entity = userBean.createEntity();
|
||
|
update(entity);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private boolean userNotExist(final String openid) throws Exception {
|
||
|
return MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<List<YTUserEntity>>() {
|
||
|
public List<YTUserEntity> run(DAOContext content) throws Exception {
|
||
|
QueryCondition queryCondition = QueryFactory.create();
|
||
|
queryCondition.addRestriction(RestrictionFactory.eq("openId", openid));
|
||
|
return content.getDAO(MyUserDao.class).find(queryCondition);
|
||
|
}
|
||
|
}).isEmpty();
|
||
|
}
|
||
|
|
||
|
private void remove(final String openId) throws Exception {
|
||
|
MyCoreDBAccess.getAccessor().runDMLAction(new DBAction<YTUserEntity>() {
|
||
|
public YTUserEntity run(DAOContext content) throws Exception {
|
||
|
content.getDAO(MyUserDao.class).remove(QueryFactory.create().addRestriction(RestrictionFactory.eq("openId", openId)));
|
||
|
return null;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
private void save(final YTUserEntity entity) throws Exception {
|
||
|
MyCoreDBAccess.getAccessor().runDMLAction(new DBAction<YTUserEntity>() {
|
||
|
public YTUserEntity run(DAOContext content) throws Exception {
|
||
|
content.getDAO(MyUserDao.class).add(entity);
|
||
|
return null;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
private void update(final YTUserEntity entity) throws Exception {
|
||
|
MyCoreDBAccess.getAccessor().runDMLAction(new DBAction<YTUserEntity>() {
|
||
|
public YTUserEntity run(DAOContext content) throws Exception {
|
||
|
content.getDAO(MyUserDao.class).update(entity);
|
||
|
return null;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 通过部门长名取用户
|
||
|
*
|
||
|
* @return
|
||
|
*/
|
||
|
public List<MyUserBean> getDepartmentUserList(final int start, final int size) throws Exception {
|
||
|
|
||
|
List<YTUserEntity> entityList = MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<List<YTUserEntity>>() {
|
||
|
public List<YTUserEntity> run(DAOContext content) throws Exception {
|
||
|
QueryCondition queryCondition = QueryFactory.create();
|
||
|
queryCondition.setSkip(start);
|
||
|
queryCondition.setCount(size);
|
||
|
return content.getDAO(MyUserDao.class).find(queryCondition);
|
||
|
}
|
||
|
});
|
||
|
return entityList.stream().map(YTUserEntity::createBean).collect(Collectors.toList());
|
||
|
}
|
||
|
|
||
|
public static User getFSUserByPhone(String phone) throws Exception {
|
||
|
return AuthorityContext.getInstance().getUserController().findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("mobile", phone)));
|
||
|
}
|
||
|
|
||
|
public static User getFSUserByName(String name) throws Exception {
|
||
|
return AuthorityContext.getInstance().getUserController().findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("userName", name)));
|
||
|
}
|
||
|
|
||
|
public List<MyUserBean> getUserList(final String key, final int start, final int count) throws Exception {
|
||
|
List<YTUserEntity> entityList = MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<List<YTUserEntity>>() {
|
||
|
public List<YTUserEntity> run(DAOContext content) throws Exception {
|
||
|
QueryCondition queryCondition = QueryFactory.create();
|
||
|
queryCondition.addRestriction(RestrictionFactory.or(RestrictionFactory.like("name", key), RestrictionFactory.like("phone", key)));
|
||
|
queryCondition.setSkip(start);
|
||
|
queryCondition.setCount(count);
|
||
|
System.out.println(queryCondition);
|
||
|
return content.getDAO(MyUserDao.class).find(queryCondition);
|
||
|
}
|
||
|
});
|
||
|
return entityList.stream().map(YTUserEntity::createBean).collect(Collectors.toList());
|
||
|
}
|
||
|
|
||
|
public long countUserList(final String key) throws Exception {
|
||
|
|
||
|
return MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<Long>() {
|
||
|
public Long run(DAOContext content) throws Exception {
|
||
|
QueryCondition queryCondition = QueryFactory.create();
|
||
|
queryCondition.addRestriction(RestrictionFactory.or(RestrictionFactory.like("name", key), RestrictionFactory.like("phone", key)));
|
||
|
System.out.println(queryCondition);
|
||
|
return content.getDAO(MyUserDao.class).count(queryCondition);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
public Long countDepartmentUserList() throws Exception {
|
||
|
return MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<Long>() {
|
||
|
public Long run(DAOContext content) throws Exception {
|
||
|
QueryCondition queryCondition = QueryFactory.create();
|
||
|
System.out.println(queryCondition);
|
||
|
return content.getDAO(MyUserDao.class).count(queryCondition);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
}
|
||
|
|
||
|
public MyUserBean getUserByOpenId(final String openid) throws Exception {
|
||
|
YTUserEntity entity = MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<YTUserEntity>() {
|
||
|
public YTUserEntity run(DAOContext content) throws Exception {
|
||
|
QueryCondition queryCondition = QueryFactory.create();
|
||
|
queryCondition.addRestriction(RestrictionFactory.eq("openId", openid));
|
||
|
System.out.println(queryCondition);
|
||
|
queryCondition.addRestriction(RestrictionFactory.eq("status", 1));
|
||
|
return content.getDAO(MyUserDao.class).findOne(queryCondition);
|
||
|
}
|
||
|
});
|
||
|
return entity == null ? null : entity.createBean();
|
||
|
}
|
||
|
|
||
|
public YTUserEntity getUserByFrName(String names) throws Exception {
|
||
|
YTUserEntity entity = MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<YTUserEntity>() {
|
||
|
public YTUserEntity run(DAOContext content) throws Exception {
|
||
|
QueryCondition queryCondition = QueryFactory.create();
|
||
|
queryCondition.addRestriction(RestrictionFactory.eq("fsUserName", names));
|
||
|
queryCondition.addRestriction(RestrictionFactory.eq("status", 1));
|
||
|
System.out.println(queryCondition);
|
||
|
return content.getDAO(MyUserDao.class).findOne(queryCondition);
|
||
|
}
|
||
|
});
|
||
|
return entity;
|
||
|
}
|
||
|
|
||
|
public YTUserEntity getUserByPhone(final String mobile) throws Exception {
|
||
|
YTUserEntity entity = MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<YTUserEntity>() {
|
||
|
public YTUserEntity run(DAOContext content) throws Exception {
|
||
|
QueryCondition queryCondition = QueryFactory.create();
|
||
|
queryCondition.addRestriction(RestrictionFactory.eq("phone", mobile));
|
||
|
queryCondition.addRestriction(RestrictionFactory.eq("status", 1));
|
||
|
System.out.println(queryCondition);
|
||
|
return content.getDAO(MyUserDao.class).findOne(queryCondition);
|
||
|
}
|
||
|
});
|
||
|
return entity;
|
||
|
}
|
||
|
|
||
|
|
||
|
private static class HOLDER {
|
||
|
private static final MyUserSyncManager INSTANCE = new MyUserSyncManager();
|
||
|
|
||
|
private HOLDER() {
|
||
|
}
|
||
|
}
|
||
|
}
|