荣誉模块数据看板查询报错

This commit is contained in:
2026-10-10 11:03:55 +08:00
parent c829cbf725
commit 9ffdbd21ad
3 changed files with 68 additions and 33 deletions
@@ -10,14 +10,11 @@ import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.util.Daos;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
@@ -42,36 +39,8 @@ public class HonorBoardController {
@At
@SaCheckPermission("honor.board")
public Result getHonorTypeByYear() {
Sql sqlYear = Sqls.create("""
select YEAR(grantDate) AS `year` from honor where grantDate is not null GROUP BY grantDate
""");
int[] yearList = (int[]) Daos.query(dao, sqlYear.toString(), Sqls.callback.ints());
List<NutMap> resultList = new ArrayList<>();
for (int year : yearList) {
resultList.add(NutMap.NEW().addv("name", "集体荣誉").addv("num", 0).addv("year", year));
resultList.add(NutMap.NEW().addv("name", "个人荣誉").addv("num", 0).addv("year", year));
}
Sql sql = Sqls.create("""
SELECT YEAR
( h.grantDate ) as year,
hbs.`name`
FROM
honor h
left join honor_basic_settings hbs on hbs.id = h.honorType
WHERE YEAR (h.grantDate) <= @currentYear
""");
sql.setParam("currentYear", Calendar.getInstance().get(Calendar.YEAR));
List<NutMap> list = honorCommonService.listMap(sql);
for (NutMap map : resultList) {
int year = map.getInt("year");
String name = map.getString("name");
long count = list.stream().filter(v -> v.getString("name").equals(name) && v.getInt("year") == year).count();
map.setv("num", count);
}
return Result.success(resultList);
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
return Result.success(honorCommonService.getHonorTypeStatisticsByYear(currentYear));
}
@At
@@ -3,6 +3,7 @@ package com.budwk.app.zhgh.dayofficework.honor.service;
import com.budwk.app.base.service.BaseService;
import com.budwk.app.zhgh.dayofficework.honor.models.Honor;
import com.budwk.app.zhgh.dayofficework.honor.models.HonorBasicSettings;
import org.nutz.lang.util.NutMap;
import java.util.List;
@@ -16,4 +17,12 @@ import java.util.List;
public interface HonorCommonService extends BaseService<Honor> {
List<HonorBasicSettings> getHonorBasicSettingsByQueryTypeCode(String queryTypeCode);
/**
* 按年度统计集体荣誉和个人荣誉数量。
*
* @param currentYear 当前允许统计的最大年份,晚于该年份的数据不返回
* @return 年度荣誉统计列表;每项包含 name(荣誉类别)、num(数量)和 year(年份)
*/
List<NutMap> getHonorTypeStatisticsByYear(int currentYear);
}
@@ -7,9 +7,16 @@ import com.budwk.app.zhgh.dayofficework.honor.models.HonorBasicSettings;
import com.budwk.app.zhgh.dayofficework.honor.service.HonorCommonService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @version 1.0
@@ -32,4 +39,54 @@ public class HonorCommonServiceImpl extends BaseServiceImpl<Honor> implements Ho
List<HonorBasicSettings> query = dao().query(HonorBasicSettings.class, Cnd.where("parentId", "=", basicSettings.getId()).asc("`code`"));
return query;
}
/**
* 按年度统计荣誉类别数量,并为每个存在荣誉数据的年度补齐集体荣誉、个人荣誉两类数据。
* SQL 将年份显式转换为字符类型,避免部分 JDBC 驱动把 YEAR 函数结果识别为 Timestamp。
*
* @param currentYear 当前允许统计的最大年份
* @return 年度荣誉统计列表;每项包含 name、num、year 三个字段
*/
@Override
public List<NutMap> getHonorTypeStatisticsByYear(int currentYear) {
Sql sql = Sqls.create("""
SELECT
CAST(YEAR(h.grantDate) AS CHAR) AS honorYear,
hbs.`name` AS honorName,
COUNT(1) AS honorCount
FROM honor h
LEFT JOIN honor_basic_settings hbs ON hbs.id = h.honorType
WHERE h.grantDate IS NOT NULL
AND YEAR(h.grantDate) <= @currentYear
GROUP BY CAST(YEAR(h.grantDate) AS CHAR), hbs.`name`
ORDER BY honorYear
""");
sql.setParam("currentYear", currentYear);
List<NutMap> statistics = listMap(sql);
// 每个年度固定返回两类荣誉,保证某一类别无数据时图表仍能显示 0。
Map<Integer, Map<String, Long>> statisticsByYear = new LinkedHashMap<>();
for (NutMap statistic : statistics) {
String honorYear = statistic.getString("honorYear");
if (Strings.isBlank(honorYear)) {
continue;
}
int year = Integer.parseInt(honorYear);
Map<String, Long> categoryStatistics = statisticsByYear.computeIfAbsent(year, key -> {
Map<String, Long> categoryMap = new LinkedHashMap<>();
categoryMap.put("集体荣誉", 0L);
categoryMap.put("个人荣誉", 0L);
return categoryMap;
});
String honorName = statistic.getString("honorName");
if (categoryStatistics.containsKey(honorName)) {
categoryStatistics.put(honorName, statistic.getLong("honorCount"));
}
}
List<NutMap> resultList = new ArrayList<>();
statisticsByYear.forEach((year, categoryStatistics) -> categoryStatistics.forEach((name, count) ->
resultList.add(NutMap.NEW().addv("name", name).addv("num", count).addv("year", year))));
return resultList;
}
}