您的位置:首页 > 其它

编写复杂的HQL语句

2016-03-04 16:36 507 查看
之前在做项目时候需要一个功能是在页面显示总金额,但条件是要根据用户的搜索条件不同显示不同的总金额。在公司大牛指导下写了这么一长串代码,突然想起来了总结一下,免得以后自己忘掉。主要思想是利用map和String,如果map中有对应的搜索条件,就添加对应key的值。

/**
* 查询当前档口下的启用的日常收/支 合计金额
* @param map 查询条件
* @param incomeType 收支类型 0:支出 1:收入
* @return
*/
public double getSumPayment(Map<String, Object> map, int incomeType){
// 当前档口下的启用的日常收/支 合计金额HQL
StringBuffer  hql = new StringBuffer("select sum(df.payment) from DailyFinace ");
hql.append("df where df.account = ?1 ");
hql.append("and df.delFlag = ?2 ");
hql.append("and df.incomeType = ?3 ");
if(map != null && map.size() > 0) {
// 录单日期
if (map.containsKey("recordDate_ge") && StringUtils.isNotEmpty(map.get("recordDate_ge").toString())) {
hql.append("and date_format(df.recordDate,'%Y-%m-%d') >= ?4 ");
}
if (map.containsKey("recordDate_le") && StringUtils.isNotEmpty(map.get("recordDate_le").toString())) {
hql.append("and date_format(df.recordDate,'%Y-%m-%d') <= ?5 ");
}
// 业务时间
if (map.containsKey("serviceTime_ge") && StringUtils.isNotEmpty(map.get("serviceTime_ge").toString())) {
hql.append("and date_format(df.serviceTime,'%Y-%m-%d') >= ?6 ");
}
if (map.containsKey("serviceTime_le") && StringUtils.isNotEmpty(map.get("serviceTime_le").toString())) {
hql.append("and date_format(df.serviceTime,'%Y-%m-%d') <= ?7 ");
}
// 类型ID
if (map.containsKey("typeId") && StringUtils.isNotEmpty(map.get("typeId").toString())) {
hql.append("and df.typeId = ?8 ");
}
// 收/付款账号
if (map.containsKey("accountNo") && StringUtils.isNotEmpty(map.get("accountNo").toString())) {
hql.append("and df.accountNo = ?9 ");
}
// 经手人
if (map.containsKey("handPerson") && StringUtils.isNotEmpty(map.get("handPerson").toString())) {
hql.append("and df.handPerson.id = ?10 ");
}
}
Query query = em.createQuery(hql.toString());
// 当前档口
query.setParameter(1, accountService.getCurrentAccount());
// 状态未删除
query.setParameter(2, Boolean.FALSE);
// 收支类型
query.setParameter(3, incomeType);
if(map != null && map.size() > 0) {
// 录单日期
if(map.containsKey("recordDate_ge") && StringUtils.isNotEmpty(map.get("recordDate_ge").toString())){
query.setParameter(4, map.get("recordDate_ge"));
}
if(map.containsKey("recordDate_le") && StringUtils.isNotEmpty(map.get("recordDate_le").toString())){
query.setParameter(5, map.get("recordDate_le"));
}
// 业务时间
if(map.containsKey("serviceTime_ge") && StringUtils.isNotEmpty(map.get("serviceTime_ge").toString())){
query.setParameter(6, map.get("serviceTime_ge"));
}
if(map.containsKey("serviceTime_le") && StringUtils.isNotEmpty(map.get("serviceTime_le").toString())){
query.setParameter(7, map.get("serviceTime_le"));
}
// 类型ID
if(map.containsKey("typeId") && StringUtils.isNotEmpty(map.get("typeId").toString())){
query.setParameter(8, map.get("typeId"));
}
// 收/付款账号
if(map.containsKey("accountNo") && StringUtils.isNotEmpty(map.get("accountNo").toString())){
query.setParameter(9, map.get("accountNo"));
}
// 经手人
if(map.containsKey("handPerson") && StringUtils.isNotEmpty(map.get("handPerson").toString())) {
query.setParameter(10, map.get("handPerson"));
}
}
// 合计值
Double sumPayment = (Double) query.getResultList().get(0);
if(sumPayment == null){
sumPayment = 0.00;
}
return sumPayment;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: