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.
183 lines
6.7 KiB
183 lines
6.7 KiB
package com.fr.plugin.yt; |
|
|
|
import com.fr.json.JSONArray; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.plugin.beans.MyDepBean; |
|
import com.fr.plugin.config.YituanPri; |
|
import com.fr.plugin.dao.MyDepDao; |
|
import com.fr.plugin.entitys.YtDepEntity; |
|
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 org.slf4j.Logger; |
|
import org.slf4j.LoggerFactory; |
|
|
|
import java.util.*; |
|
import java.util.stream.Collectors; |
|
|
|
public class MyCorpManager { |
|
// private List<WeiXinCorp> corpList; |
|
private List<MyDepBean> depList = new ArrayList<>(); |
|
|
|
private MyCorpManager() { |
|
|
|
} |
|
|
|
public static MyCorpManager getInstance() { |
|
return HOLDER.INSTANCE; |
|
} |
|
|
|
|
|
public JSONArray generateDepartmentTree() { |
|
JSONArray var1 = new JSONArray(); |
|
try { |
|
this.depList = this.getDepFormDB(); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
if (this.depList.isEmpty()) { |
|
return new JSONArray(); |
|
} |
|
// Map<String, JSONObject> parentTree = new HashMap<>(); |
|
for (MyDepBean depBean : this.depList) { |
|
var1.add(depBean.createJSONConfig()); |
|
} |
|
return var1; |
|
} |
|
|
|
private List<MyDepBean> getDepFormDB() throws Exception { |
|
List<YtDepEntity> entities = MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<List<YtDepEntity>>() { |
|
@Override |
|
public List<YtDepEntity> run(DAOContext daoContext) throws Exception { |
|
return daoContext.getDAO(MyDepDao.class).find(QueryFactory.create()); |
|
} |
|
}); |
|
return entities.stream().map(YtDepEntity::createBean).collect(Collectors.toList()); |
|
} |
|
|
|
|
|
public void syncDepList() throws Exception { |
|
YituanPri instance = YituanPri.getInstance(); |
|
String eid = instance.getEid(); |
|
String readKey = instance.getReadKey(); |
|
if (StringUtils.isNotBlank(eid) && StringUtils.isNotBlank(readKey)) { |
|
List<MyDepBean> allDep = WebUtils.getAllDep(eid, readKey); |
|
Logger logger = LoggerFactory.getLogger("部门同步"); |
|
List<String> idsFromService = new ArrayList<>(allDep.size()); |
|
if (allDep.isEmpty()) { |
|
return; |
|
} |
|
int count=0; |
|
for (MyDepBean depBean : allDep) { |
|
String id = depBean.getId(); |
|
idsFromService.add(id); |
|
if (depNotExist(id)) { |
|
logger.error("同步一个新的部门:{}", depBean.getName()); |
|
save(depBean.createEntity()); |
|
logger.error("同步一个新的部门:{} 完成", depBean.getName()); |
|
} else { |
|
logger.error("同步更新部门:{}", depBean.getName()); |
|
update(depBean.createEntity()); |
|
logger.error("同步更新部门完成:{}", depBean.getName()); |
|
} |
|
count++; |
|
} |
|
logger.error("共处理更新{}个部门",count); |
|
int delCount=0; |
|
List<String> allDepId = findAllDepId(); |
|
if (allDepId.removeAll(idsFromService)) { |
|
for (String id : allDepId) { |
|
logger.error("删除了一个部门:{}",id); |
|
delCount++; |
|
remove(id); |
|
} |
|
} |
|
logger.error("共处理删除{}个部门",delCount); |
|
} |
|
} |
|
|
|
private List<String> findAllDepId() throws Exception { |
|
List<YtDepEntity> entityList = MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<List<YtDepEntity>>() { |
|
public List<YtDepEntity> run(DAOContext content) throws Exception { |
|
QueryCondition queryCondition = QueryFactory.create(); |
|
return content.getDAO(MyDepDao.class).find(queryCondition); |
|
} |
|
}); |
|
List<String> ids = new ArrayList<>(entityList.size()); |
|
for (YtDepEntity entity : entityList) { |
|
ids.add(entity.getId()); |
|
} |
|
return ids; |
|
} |
|
|
|
private boolean depNotExist(final String id) throws Exception { |
|
return MyCoreDBAccess.getAccessor().runQueryAction(new DBAction<List<YtDepEntity>>() { |
|
@Override |
|
public List<YtDepEntity> run(DAOContext daoContext) throws Exception { |
|
return daoContext.getDAO(MyDepDao.class).find(QueryFactory.create().addRestriction(RestrictionFactory.eq("id", id))); |
|
} |
|
}).isEmpty(); |
|
} |
|
|
|
public void saveOrUpdateDep(MyDepBean depBean) throws Exception { |
|
String id = depBean.getId(); |
|
if (depNotExist(id)) { |
|
save(depBean.createEntity()); |
|
} else { |
|
update(depBean.createEntity()); |
|
} |
|
} |
|
|
|
private boolean save(final YtDepEntity entity) throws Exception { |
|
return MyCoreDBAccess.getAccessor().runDMLAction(new DBAction<YtDepEntity>() { |
|
@Override |
|
public YtDepEntity run(DAOContext daoContext) throws Exception { |
|
daoContext.getDAO(MyDepDao.class).add(entity); |
|
return null; |
|
} |
|
}) == null; |
|
} |
|
|
|
|
|
private void remove(final String id) throws Exception { |
|
MyCoreDBAccess.getAccessor().runDMLAction(new DBAction<YtDepEntity>() { |
|
@Override |
|
public YtDepEntity run(DAOContext daoContext) throws Exception { |
|
daoContext.getDAO(MyDepDao.class).remove(id); |
|
return null; |
|
} |
|
}); |
|
} |
|
|
|
private boolean update(final YtDepEntity entity) throws Exception { |
|
return MyCoreDBAccess.getAccessor().runDMLAction(new DBAction<YtDepEntity>() { |
|
@Override |
|
public YtDepEntity run(DAOContext daoContext) throws Exception { |
|
daoContext.getDAO(MyDepDao.class).update(entity); |
|
return null; |
|
} |
|
}) == null; |
|
} |
|
|
|
public void removeDep(MyDepBean depBean) throws Exception { |
|
FineLoggerFactory.getLogger().error("移除了一个部门:{},id :{}", depBean.getDepName(), depBean.getId()); |
|
MyCoreDBAccess.getAccessor().runDMLAction(new DBAction<YtDepEntity>() { |
|
@Override |
|
public YtDepEntity run(DAOContext daoContext) throws Exception { |
|
daoContext.getDAO(MyDepDao.class).remove(depBean.getId()); |
|
return null; |
|
} |
|
}); |
|
} |
|
|
|
private static class HOLDER { |
|
private static final MyCorpManager INSTANCE = new MyCorpManager(); |
|
|
|
private HOLDER() { |
|
} |
|
} |
|
}
|
|
|