前段时间遇到一个一个公文类的待阅排序,要求
1.将待阅列表里的收文类的优先显示(并且按照时间降序排列);
2.将非收文类的进行排序,在收文之后,也按照时间降序进行排列。
public Page queryToReadWorkitemsByPageshouwen(String userId, String passReadType, String moduleId, String titleName, String taskName, String startTime, String endTime, Page page, String type) {
StringBuffer hql = new StringBuffer("from PassReadObject a where a.toUserId=?");
Collection colargs = new ArrayList();
colargs.add(userId);
this.log.debug("userId:" + userId + "moduleId:" + moduleId + "titleName:" + titleName + "taskName" + taskName + "startTime:" + startTime + "endTime" + endTime);
addCondition(moduleId, passReadType, hql, colargs, startTime, endTime, taskName, titleName, type);
//402880591618b831011618ca16ca000c 收文和收文相关子流程
//4028816938faa90b0138faab88e00003 部门收文 和 文件下发等
hql.append(" order by(case when a.moduleId='4028816938faa90b0138faab88e00003' then 1 when a.moduleId='402880591618b831011618ca16ca000c' then 2 else 0 end) desc , a.createdTime desc");
this.log.debug("userId:" + userId + "moduleId:" + moduleId + "titleName:" + titleName + "taskName" + taskName + "startTime:" + startTime + "endTime" + endTime);
this.log.debug("The final search hql is : " + hql.toString());
boolean isScroll = true;
Page resultPage = pagedQuery(hql.toString(), colargs.toArray(), page.getCurrentPageNo(), page.getPageSize(), isScroll);
return resultPage;
}
之前试了很多次,比如1.使用两个方法,一个查询收文,一个查询非收文类,但是最后这个结果不尽人意,最后在网上看到了case when 进行辅助排序解决了这个问题。
select * from BUSINESS_PASSREAD
where TOUSER_ID='8a8a18f32d1b67cb012d1bfc46cd0134' and PASSREAD_TYPE='TODOREAD'
order by (case
when MODULE_ID='4028816938faa90b0138faab88e00003' then 1
when MODULE_ID='402880591618b831011618ca16ca000c' then 2
else 0 end
) desc ,CREATED_TIME desc