
业务场景:JPA查询--》数据分组--》返回
这个只能给你们带来一种思路,具体分组要根据自己的业务场景,我这个业务场景比较复杂 因为JPA的查询含有权限控制
第一步:根据审批权限获取 当前用户可以操作的单据
这个省略。。。根据具体场景
第二步:根据sql查询对应的分组
SELECT
product_order_code,
employe_name
FROM
mes_buste_page
WHERE
code IN ('100000000850')--这个code是 第一步带过来的
GROUP BY
product_order_code,
employe_name
第三步:根据sql查询分组对应哪些code数据
SELECT
t0.*, code
FROM
(
SELECT
product_order_code,
employe_name
FROM
mes_buste_page
WHERE
code IN ('100000000850')--这个code是 第一步带过来的
GROUP BY
product_order_code,
employe_name
) t0
LEFT JOIN mes_buste_page t1 ON t0.product_order_code = t1.product_order_code
AND t0.employe_name = t1.employe_name
WHERE
code IN ('100000000850')--这个code是 第一步带过来的
第四步:遍历第二步的数据,把第三步的code赋值给第二步 也就是codes
第五步:遍历第一步的数据,根据分组过的code进行匹配 可以用ArrayList 的contains方法 然后就获取到了这一组 分组的数据
然后这里的动态字段分组怎么实现的呢?? 看下面代码!!!
/**
* codes 这个就是第一步的数据code
* conditions 自定义字段分组数据 如 ["product_order_code","employe_name"]
*/
public List<Map<String, Object>> conditionsGroup(ArrayList codes, String[] conditions) {
List<Map<String, Object>> result = new ArrayList<>();
Map<String, Object> params = new HashMap<>();
Map<String, Object> valueMap = new HashMap<>();
//下面的拼接sql其实就是 第二步和第三步 分组sql拼接自定义字段
StringBuffer sql = new StringBuffer("SELECT t0.*, code FROM ( SELECT ");
for (String condition : conditions) {
sql.append(condition + ",");
}
sql = new StringBuffer(sql.substring(0, sql.lastIndexOf(",")));
sql.append(" FROM mes_product_transaction WHERE code IN (:codes) GROUP BY ");
for (String condition : conditions) {
sql.append(condition + ",");
}
sql = new StringBuffer(sql.substring(0, sql.lastIndexOf(",")));
sql.append(") t0 LEFT JOIN mes_product_transaction t1 ON ");
for (String condition : conditions) {
sql.append("t0." + condition + " = t1." + condition + " AND ");
}
sql = new StringBuffer(sql.substring(0, sql.lastIndexOf("AND")));
sql.append(" WHERE code IN (:codes) ");
params.put(ApplicationConst.SQL_KEY, sql.toString());
valueMap.put("codes", codes);
params.put(ApplicationConst.SQL_QUERY_VALUE_KEY, valueMap);
//获取分组的 分组和对应的code
List<Map> groupAndCode = sqlDao.findList(params);
sql = new StringBuffer("SELECT ");
for (String condition : conditions) {
sql.append(condition + ",");
}
sql = new StringBuffer(sql.substring(0, sql.lastIndexOf(",")));
sql.append(" FROM mes_product_transaction WHERE code IN (:codes) GROUP BY ");
for (String condition : conditions) {
sql.append(condition + ",");
}
sql = new StringBuffer(sql.substring(0, sql.lastIndexOf(",")));
params.put(ApplicationConst.SQL_KEY, sql.toString());
//获取分组
List<Map> groupInfo = sqlDao.findList(params);
for (Map map : groupInfo) {
ArrayList troCode=new ArrayList();
for (Map map1 : groupAndCode) {
boolean cunzai = false;
//下面多看 这个是第四步 的比对操作
for (String condition : conditions) {
if (map.get(condition).equals(map1.get(condition))) {
cunzai = true;
} else {
cunzai = false;
//一次分组不符合 终身不用
break;
}
}
if (cunzai) {
troCode.add(map1.get("code"));
}
}
map.put("codes",troCode);
result.add(map);
}
return result;
}下面是数据返回集
{
"checkProductSize":1,
"busteSize":20,
"allSize":22,
"checkOrderSize":1,
"busteOrderSize":3,
"checkSize":1,
"busteProductSize":2,
"busteQuantity":100,
"busteDisSumQuantity":200,
"callSize":1,
"callOrderSize":1,
"callProductSize":1,
"group":[ //分组数据
{
"create_object_name":"张三",
"product_order_code":"6100000115",
"disSumQuantity":0,
"quantity":1000,
"orderSize":1,
"transactionList":Array[1]
},
{
"create_object_name":"李四",
"product_order_code":"6100000115",
"disSumQuantity":0,
"quantity":1000,
"orderSize":1,
"transactionList":Array[1]
}
]
}遗留的问题: 其实第 二、三、四 步 是可以合并的,犹豫时间不足没写 SQL大神可以写出的 要的效果就是 一组数据 后面一列多条code
本文为程序员之家原创文章,转载无需和我联系,但请注明来自程序员之家www.baldhome.cn
