public List getHeader(Long evaluateTypeId){
List parameters = new ArrayList();
parameters.add(evaluateTypeId);
//查询出来 循环sql的长度
String sql = "select i.percentage percentage,q.id quotaId,q.type_name quotaName from c_project_evaluate_type hbtype "
+ "left join b_project_evaluate_template t on t.project_evaluate_type = hbtype.id "
+ "left join b_project_evaluate_item i on i.project_evaluate_template = t.id "
+ "left join c_project_evaluate_quota q on q.id = i.project_evaluate_quota where hbtype.id = ? ";
List quotaList = entityManager.nativeFindList(sql, parameters.toArray(), new RowMapper() {
public Object mapRow(ResultSet rs, int num) throws SQLException {
JSONObject jt = new JSONObject();
jt.element("percentage", rs.getDouble("percentage"));
jt.element("quotaId", rs.getLong("quotaId"));
jt.element("quotaName", rs.getString("quotaName"));
return jt;
}
});
StringBuilder sb = new StringBuilder();
//正常的查询sql start
sb.append(" with temp as ( ");
sb.append(" select distinct bp.ID projectId, bp.entity_code projectCode, bp.entity_name projectName, ");
sb.append(" co.party_name orgName, bcp.fea_total_no_tax_invest feainvest, ");
sb.append(" spu.party_name projectmname, spu.party_code projectmcode, ");
sb.append(" cpet.type_name specName, bpes.id systemId,bped.id detailId,bped.value val,cpeq.id quotaId, ");
sb.append(" cpeq.type_name quotaName, bpei.percentage, bpei.sort ");
sb.append(" from b_project_evaluate_system bpes ");
sb.append(" left join b_project_evaluate_detail bped on bpes.ID = bped.project_evaluate_system ");
sb.append(" left join b_project_evaluate_item bpei on bped.project_evaluate_item = bpei.ID ");
sb.append(" left join c_project_evaluate_quota cpeq on cpeq.id = bpei.project_evaluate_quota ");
sb.append(" left join b_project bp on bpes.project_id = bp.ID ");
sb.append(" left join b_capital_proj bcp on bcp.id = bp.capital_proj_id ");
sb.append(" left join b_proj_info bpi on bpi.id = bp.proj_info_id ");
sb.append(" left join s_privilege_user spu on spu.id = bpi.proj_cm_id ");
sb.append(" left join c_organization co on co.id = bpi.construct_org_id ");
sb.append(" left join c_project_evaluate_type cpet on bpes.hb_projct_type = cpet.ID ");
sb.append(" where 1=1 ");
sb.append(" and cpet.ID in ( " + evaluateTypeId + " )");
sb.append(" order by bpes.id, bpei.sort ) ");
//正常的查询sql end
//开始行转列
sb.append(" select systemid, orgName, specname, projectid, projectname, projectcode, feainvest, projectmname, projectmcode, ");
//循环要输出的表头
for (int i = 0; i < quotaList.size(); i++) {
long quotaId = quotaList.get(i).getLong("quotaId");
sb.append(" max(f"+i+") " + "h_" + quotaId);
if( i < quotaList.size()-1){
sb.append(", ");
}
}
//循环结果注释 【注意不能使用 纯数字,orcale会默认为关键字,所以我添加了 h_ 】
//--sb.append(" max(f0) h_67919589, max(f1) h_67919590, max(f2) h_67919591, max(f3) h_67919592, max(f4) h_67919593, max(f5) h_67919682, ");
//--sb.append(" max(f6) h_67919687, max(f7) h_67919698, max(f8) h_67919892, max(f9) h_67923169 ");
sb.append(" from temp pivot ( ");
sb.append(" max(temp.val) "); //要行转列的行
sb.append(" for quotaId in( ");//按照哪一行转的哪一行
//循环要输出的列
for (int i = 0; i < quotaList.size(); i++) {
long quotaId = quotaList.get(i).getLong("quotaId");
sb.append(quotaId +" f"+i);
if( i < quotaList.size()-1){
sb.append(", ");
}
}
sb.append(" ) ) ");
//循环结果注释
//--sb.append(" 67919589 f0, 67919590 f1, 67919591 f2, 67919592 f3, 67919593 f4, 67919682 f5, ");
//--sb.append(" 67919687 f6, 67919698 f7, 67919892 f8, 67923169 f9) ) ");
sb.append(" group by systemid, projectid, projectcode, projectname, specname,orgName,feainvest,projectmname,projectmcode ");
sb.append(" order by projectId, specName; ");
log.info("report sql:" + sb.toString());
entityManager.nativeFindList(sb.toString(), parameters.toArray(), new RowMapper() {
public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
return null;
}
});
return null;
}