package com.wgcloud.service; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.wgcloud.dto.SubtitleDto; import com.wgcloud.entity.CustomState; import com.wgcloud.mapper.CustomStateMapper; import com.wgcloud.util.DateUtil; import com.wgcloud.util.FormatUtil; import com.wgcloud.util.UUIDUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.ui.Model; import java.util.List; import java.util.Map; /** * @version v3.3 * @ClassName:CustomStateService.java * @author: http://www.wgstart.com * @date: 2022年9月16日 * @Description: CustomStateService.java * @Copyright: 2019-2022 wgcloud. All rights reserved. */ @Service public class CustomStateService { public PageInfo selectByParams(Map params, int currPage, int pageSize) throws Exception { PageHelper.startPage(currPage, pageSize); List list = customStateMapper.selectByParams(params); PageInfo pageInfo = new PageInfo(list); return pageInfo; } public void save(CustomState customState) throws Exception { customState.setId(UUIDUtil.getUUID()); customState.setCreateTime(DateUtil.getNowTime()); customStateMapper.save(customState); } public void saveRecord(List recordList) throws Exception { if (recordList.size() < 1) { return; } for (CustomState as : recordList) { as.setId(UUIDUtil.getUUID()); } customStateMapper.insertList(recordList); } public int deleteByCustomInfoId(String customInfoId) throws Exception { return customStateMapper.deleteByCustomInfoId(customInfoId); } public int deleteById(String[] id) throws Exception { return customStateMapper.deleteById(id); } public CustomState selectById(String id) throws Exception { return customStateMapper.selectById(id); } public List selectAllByParams(Map params) throws Exception { return customStateMapper.selectAllByParams(params); } public int deleteByDate(Map map) throws Exception { return customStateMapper.deleteByDate(map); } /** * 设置图表的副标题,自定义监控项值的最高、平均、最低值 * * @param model * @param customStateList 自定义监控项状态数据 */ public void setSubtitle(Model model, List customStateList) { //自定义监控项值最高 Double maxValue = 0d; //自定义监控项值平均 Double avgValue = 0d; //自定义监控项值最低 Double minValue = 1000000d; Double sumValue = 0d; double valueTmp = 0d; for (CustomState customState : customStateList) { if (null != customState.getCustomValue()) { valueTmp = Double.valueOf(customState.getCustomValue()); if (valueTmp > maxValue) { maxValue = valueTmp; } if (valueTmp < minValue) { minValue = valueTmp; } sumValue += valueTmp; } } if (customStateList.size() > 0) { avgValue = sumValue / customStateList.size(); } else { avgValue = 0d; minValue = 0d; } SubtitleDto subtitleDto = new SubtitleDto(); subtitleDto.setAvgValue(FormatUtil.formatDouble(avgValue, 2) + ""); subtitleDto.setMaxValue(maxValue + ""); subtitleDto.setMinValue(minValue + ""); model.addAttribute("subtitleDto", subtitleDto); } @Autowired private CustomStateMapper customStateMapper; }