前言
最近在項目中,需要後端提供扇形圖的接口,因爲業務比較複雜,有的數據不只是在一張表,需要我們自己組裝後返給前端,其實扇形圖的接口就是一個map集合的數據。
接口返回數據
接口返回參數Map<String,Object>,而Object裏面包含了list+Integer,而list裏面由多個map組成的。
{
"success": true,
"code": 200,
"msg": "操作成功",
"data": {
"list": [
{
"pointType": 2,
"idList": [],
"sumPoint": 0
},
{
"pointType": 1,
"idList": [],
"sumPoint": 0
},
{
"idList": false,
"pointType": 0,
"sumPoint": 5
}
],
"points": 95
}
}
具體業務實現
/**
* 扇形圖
*
* @param companyId 公司id
* @param year 年度
* @param index
* @return
*/
public Map<String,Object> getPieChart(Integer companyId, Integer year, Integer index) {
List<Object> resultList = new ArrayList<>();
Map<String, Object> result = new HashMap<>();
Map<String, Object> diyMap = new HashMap<>();
Map<String, Object> autoMap = new HashMap<>();
Map<String, Object> totalMap = new HashMap<>();
List<Integer> idList = new ArrayList<>();//存放重大事件id集合
List<Integer> pointList = new ArrayList<>();//存放重大事件id集合
List<Integer> idList1 = new ArrayList<>();
List<Integer> pointList1 = new ArrayList<>();
Integer firmId = PrincipalAware.getRelatedId();
// 獲取週期下標
Integer period = periodSettingService.getDataOfPeriod(year, firmId);
boolean isNowYear = DateExtendUtil.isNowYear(year);
//若週期標誌未傳,則根據當前period取值
if (index == null) {
index = period > 0 ? 1 : -1;
}
//手動扣分項
PeriodPoints periodPoints = periodPointsRepository
.findByFirmIdAndCompanyIdAndYearAndPeriodIndex(firmId, companyId, year, index);
List<DiyPoints> diyPoints = diyPointsRepository
.findByFirmIdAndCompanyIdAndYearAndPeriodIndex(firmId, companyId, year, index);
if (periodPoints == null) {
if (isNowYear) {
throw new ResException("當前週期統計結果未完成,請確認!");
}
return result;
}
for (DiyPoints diyPoint : diyPoints) {
idList.add(diyPoint.getId());
pointList.add(diyPoint.getMinusPoints());
}
String sumPoint = ArrayUtil.getSumData(pointList);
diyMap.put("pointType", PointType.MAJOR.getValue());// 扣分項名稱
diyMap.put("sumPoint", sumPoint == null ? 0 : sumPoint);// 扣除的總分
diyMap.put("idList", idList);// 重大扣分事項id
resultList.add(diyMap);
//站點扣分項
List<AutoPortPoints> autoPortPoints = autoPortPointsRepository.findByFirmIdAndCompanyIdAndYearAndPeriodIndex(firmId, companyId, year, index);
for (AutoPortPoints autoPortPoint : autoPortPoints) {
idList1.add(autoPortPoint.getId());
pointList1.add(autoPortPoint.getMinusPoints());
}
String sumPoint1 = ArrayUtil.getSumData(pointList1);
autoMap.put("pointType", PointType.AUTOMATIC.getValue());// 扣分項名稱
assert sumPoint1 != null;
// 自動扣分取平均分
autoMap.put("sumPoint", autoPortPoints.size() == 0 ? 0 : (Integer.valueOf(sumPoint1) / autoPortPoints.size()));
autoMap.put("idList", idList1);// 自動
resultList.add(autoMap);
//是否扣分
List<AutoPoints> autoPoints = autoPointsRepository.findByFirmIdAndCompanyIdAndYearAndPeriodIndex(firmId, companyId, year, index);
autoPoints.stream()
.filter(x -> PointsType.OPERATION.getValue().equals(x.getPointsType()) && x.getMinusPoints() > 0)
.map(AutoPoints::getMinusPoints)
.findFirst()
.ifPresent(minusPoints -> totalMap.put("sumPoint", minusPoints));
result.put("points", periodPoints.getPoints());
totalMap.put("idList", periodPoints.getIsOperation());
totalMap.put("pointType", PointType.OPERATIONS.getValue());
resultList.add(totalMap);
result.put("list",resultList);
return result;
}