package com.fr.plugin.web.hander; import com.fr.decision.fun.impl.BaseHttpHandler; import com.fr.json.JSONArray; import com.fr.json.JSONObject; import com.fr.plugin.beans.MyDepBean; import com.fr.plugin.beans.MyGroupBean; import com.fr.plugin.dao.MyGroupDao; import com.fr.plugin.entitys.YtGroupEntity; import com.fr.plugin.utils.MyUtils; import com.fr.plugin.yt.MyCoreDBAccess; import com.fr.plugin.yt.MyUserSyncManager; 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 com.fr.third.springframework.web.bind.annotation.RequestMethod; import com.fr.web.utils.WebUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; public class GetGroupsHandler extends BaseHttpHandler { @Override public RequestMethod getMethod() { return RequestMethod.GET; } @Override public String getPath() { return "/yt/groups"; } @Override public boolean isPublic() { return false; } @Override public void handle(HttpServletRequest request, HttpServletResponse response) throws Exception { int startIdx = WebUtils.getHTTPRequestIntParameter(request, "startIdx", 0); int count = WebUtils.getHTTPRequestIntParameter(request, "count", 10); String keyword = WebUtils.getHTTPRequestParameter(request, "keyword"); List users; Long size = 0L; try { users = this.getGroups(keyword, startIdx, count); size = this.countSelectedUser(keyword); JSONObject jsonObject = toUserResultJo(users, size); JSONObject responseJSONObject = MyUtils.createSuccessResponseJSONObject(); responseJSONObject.put("userList", jsonObject); WebUtils.flushSuccessMessageAutoClose(request, response, responseJSONObject); } catch (Exception e) { e.printStackTrace(response.getWriter()); } } private Long countSelectedDepartmentUser( ) throws Exception { return MyUserSyncManager.getInstance().countDepartmentUserList( ); } private List getGroups(String name, int start, int count) throws Exception { if (StringUtils.isNotBlank(name)) { List entityList = MyCoreDBAccess.getAccessor().runQueryAction(new DBAction>() { public List run(DAOContext content) throws Exception { QueryCondition queryCondition = QueryFactory.create(); queryCondition.addRestriction(RestrictionFactory.and(RestrictionFactory.like("name", name))); queryCondition.setSkip(start); queryCondition.setCount(count); return content.getDAO(MyGroupDao.class).find(queryCondition); } }); return entityList.stream().map(YtGroupEntity::createBean).collect(Collectors.toList()); } else { List entityList = MyCoreDBAccess.getAccessor().runQueryAction(new DBAction>() { public List run(DAOContext content) throws Exception { QueryCondition queryCondition = QueryFactory.create(); queryCondition.setSkip(start); queryCondition.setCount(count); return content.getDAO(MyGroupDao.class).find(queryCondition); } }); return entityList.stream().map(YtGroupEntity::createBean).collect(Collectors.toList()); } } private Long countSelectedUser(String key) throws Exception { return MyCoreDBAccess.getAccessor().runQueryAction(new DBAction() { public Long run(DAOContext content) throws Exception { QueryCondition queryCondition = QueryFactory.create(); queryCondition.addRestriction(RestrictionFactory.and(RestrictionFactory.like("name", key))); System.out.println(queryCondition); return content.getDAO(MyGroupDao.class).count(queryCondition); } }); } public static JSONObject toUserResultJo(List users, Long var1) throws Exception { JSONObject var2 = JSONObject.create(); JSONArray var3 = JSONArray.create(); JSONObject var6; for (Iterator var4 = users.iterator(); var4.hasNext(); var3.put(var6)) { MyGroupBean var5 = (MyGroupBean) var4.next(); var6 = var5.createJSONConfig(); } var2.put("total", var1); var2.put("users", var3); return var2; } }