2019独角兽企业重金招聘Python工程师标准>>>
如题;此类查询,jpa自带的单表、本地sql查询已经无法满足需求,基于此要求,我们需要调用jpa的EntityManager内置查询引擎,方可实现。
在ReportRespository的实现类中引入实体管理引擎:
@PersistenceContext
private EntityManager entityManager;
sql组装:
@Override
public Long findOrderAmountByCondition(QueryParameter parameter) {
boolean isGroup =false;
String whereStr =" where 1=1 ";
StringBuffer sql = new StringBuffer();
sql.append("select ");
if( commonUtil.isObjNullOrEmp(parameter.getFirstDeptHid())
&& "1".equals(parameter.getDeptSelect())){
sql.append( " c_first_dept_hid,"); //部门ID
sql.append( " max( c_first_dept_name),");//部门名称
}else{
sql.append(" max( c_first_dept_hid),max(c_first_dept_name),");
}
if(!commonUtil.isObjNullOrEmp(parameter.getMonthSelect()) && "1".equals(parameter.getMonthSelect())){
sql.append(" to_char(createTime,'YYYY-MM') ,"); //月份
}else{
sql.append(" '月份' ,");
}
if( (commonUtil.isObjNullOrEmp(parameter.getSupplier()) && "1".equals(parameter.getSupplierSelect()))
|| !commonUtil.isObjNullOrEmp(parameter.getSupplier())){
sql.append( " (case supplier when '1' then '苏宁' when '2' then '史泰博' when '3' then '得力' else '' END )as supplier," );//平台
}else{
sql.append(" '全部平台' ,") ;//所有
}
sql.append(" sum(personalPrice) ,"//个人版价格
+ " count(1) ," //订单数量
+ " sum(totalprice), " //下单金额
+" sum(quantity) "
+ " from ");
selectOrderAmountUnionInnerQuerySql(parameter, sql);
sql.append(" where 1=1 ");
if(commonUtil.isObjNullOrEmp(parameter.getFirstDeptHid())
&& "1".equals(parameter.getDeptSelect())){
//区分部门
sql.append(" c_first_dept_hid,");
isGroup =true;
}
if( (commonUtil.isObjNullOrEmp(parameter.getSupplier()) && "1".equals(parameter.getSupplierSelect()))
|| !commonUtil.isObjNullOrEmp(parameter.getSupplier())){
sql.append( " supplier," );//平台
isGroup =true;
}
if(!commonUtil.isObjNullOrEmp(parameter.getMonthSelect())
&& "1".equals(parameter.getMonthSelect()) ){
isGroup =true;
sql.append(" to_char(createTime,'YYYY-MM') ");
sql.append(" order by to_char(createTime,'YYYY-MM') asc ");
}else{
sql = new StringBuffer(sql.substring(0, sql.length()-1));
}
if(isGroup){
sql.insert(sql.lastIndexOf(whereStr) + whereStr.length(), " group by ");
}
//通过组装好的sql创建查询引擎对象Query
Query query = entityManager.createNativeQuery(sql.toString());
if(!commonUtil.isObjNullOrEmp(parameter.getStartTime()) && !commonUtil.isObjNullOrEmp(parameter.getEndTime()) ){
if(!commonUtil.isObjNullOrEmp(parameter.getMonthSelect()) && "1".equals(parameter.getMonthSelect())){
parameter.setStartTime(parameter.getStartTime().substring(0, 7));
parameter.setEndTime(parameter.getEndTime().substring(0, 7));
}
}
setParameterValues(query,parameter);
//需要分页时,可在此处添加
//query.setFirstResult((pageable.getPageNumber()-1) * pageable.getPageSize());
//query.setMaxResults(pageable.getPageSize());
@SuppressWarnings("unchecked")
List
if(null==objectList || objectList.size()==0){
return 0L;
}else{
return new Long(objectList.size());
}
}
设置其他参数:
/**
* 设置参数值
* @param query
* @param supplier
* @param startTime
* @param endTime
* @param firstDeptId
*/
public void setParameterValues(Query query,QueryParameter parameter){
if(!commonUtil.isObjNullOrEmp(parameter)){
if(!commonUtil.isObjNullOrEmp(parameter.getStartTime())
&& !commonUtil.isObjNullOrEmp(parameter.getEndTime()) ){
query.setParameter("startTime", parameter.getStartTime());
query.setParameter("endTime", parameter.getEndTime());
}
if(!commonUtil.isObjNullOrEmp(parameter.getSupplier()) ){
query.setParameter("supplier", parameter.getSupplier());
}
if(!commonUtil.isObjNullOrEmp(parameter.getFirstDeptHid()) ){
query.setParameter("firstDeptHid", parameter.getFirstDeptHid());
}
if(null!=parameter.getStatusList() && parameter.getStatusList().size()>0){
query.setParameter("status", parameter.getStatusList());
}
if(!commonUtil.isObjNullOrEmp(parameter.getName()) ){
query.setParameter("name", "%"+parameter.getName()+"%");
}
}
}