package com.whdc.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.whdc.exception.MyException; import com.whdc.model.dto.ApiDto; import com.whdc.model.dto.GroupWarningDto; import com.whdc.model.vo.WarningData; import com.whdc.model.vo.WarningHistoryListVo; import com.whdc.model.vo.WarningListVo; import com.whdc.service.IAddressBookService; import com.whdc.utils.HttpUtil; import com.whdc.utils.ResultJson; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.compress.utils.Lists; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * Description: * Created by XuSan on 2024/5/23. * * @author XuSan * @version 1.0 */ @Slf4j @Api(tags = "预警 - Controller") @RestController @RequestMapping("/warning") public class WarningController { @Autowired private IAddressBookService addressBookService; /** * 气象预警 * * @param dto * @return */ @ApiOperation(value = "气象预警") @PostMapping("/getGroupWarning") public ResultJson> getGroupWarning( @RequestBody GroupWarningDto dto) { ApiDto apiDto = new ApiDto(); apiDto.setStartTime(dto.getStartTime()); apiDto.setEndTime(dto.getEndTime()); apiDto.setPageNumber(dto.getPageNumber()); apiDto.setPageSize(dto.getPageSize()); ArrayList filter = Lists.newArrayList(); if (StringUtils.isNotBlank(dto.getPublishUnit())) { filter.add(new ApiDto.Filter("publishUnit", "LIKE", String.format("%s%s%s", "%", dto.getPublishUnit(), "%"))); } if (StringUtils.isNotBlank(dto.getWarnSignalLevel())) { filter.add(new ApiDto.Filter("warnSignalLevel", "LIKE", String.format("%s%s%s", "%", dto.getWarnSignalLevel(), "%"))); } if (StringUtils.isNotBlank(dto.getWarnSignalType())) { filter.add(new ApiDto.Filter("warnSignalType", "LIKE", String.format("%s%s%s", "%", dto.getWarnSignalType(), "%"))); } apiDto.setFilter(filter); // String str = HttpUtil.sendPost("http://223.75.53.141:8000/shzh/met/zyqxfw/api/warning/getGroupWarning", JSON.toJSONString(apiDto)); String str = HttpUtil.sendPost("http://64.97.146.240:9990/api/warning/getGroupWarning", JSON.toJSONString(apiDto)); JSONObject json = JSON.parseObject(str); if (json != null && json.getInteger("code") == HttpStatus.SC_OK) { List data = json.getJSONArray("data").toJavaList(WarningData.class); return ResultJson.ok(getList(data)); } return ResultJson.ok(json); } /** * 历史气象预警 * * @param dto * @return */ @ApiOperation(value = "历史气象预警") @PostMapping("/getHistoryGroupWarning") public ResultJson> getHistoryGroupWarning(@RequestBody GroupWarningDto dto) { ApiDto apiDto = new ApiDto(); apiDto.setStartTime(dto.getStartTime()); apiDto.setEndTime(dto.getEndTime()); apiDto.setPageNumber(dto.getPageNumber()); apiDto.setPageSize(dto.getPageSize()); ArrayList filter = Lists.newArrayList(); if (StringUtils.isNotBlank(dto.getPublishUnit())) { filter.add(new ApiDto.Filter("publishUnit", "LIKE", String.format("%s%s%s", "%", dto.getPublishUnit(), "%"))); } if (StringUtils.isNotBlank(dto.getWarnSignalLevel())) { filter.add(new ApiDto.Filter("warnSignalLevel", "LIKE", String.format("%s%s%s", "%", dto.getWarnSignalLevel(), "%"))); } if (StringUtils.isNotBlank(dto.getWarnSignalType())) { filter.add(new ApiDto.Filter("warnSignalType", "LIKE", String.format("%s%s%s", "%", dto.getWarnSignalType(), "%"))); } apiDto.setFilter(filter); // String str = HttpUtil.sendPost("http://223.75.53.141:8000/shzh/met/zyqxfw/api/warning/getGroupWarning", JSON.toJSONString(apiDto)); String str = HttpUtil.sendPost("http://64.97.146.240:9990/api/warning/getGroupWarning", JSON.toJSONString(apiDto)); JSONObject json = JSON.parseObject(str); if (json != null && json.getInteger("code") == HttpStatus.SC_OK) { List data = json.getJSONArray("data").toJavaList(WarningData.class); return ResultJson.ok(getCount(data)); } return ResultJson.ok(json); } /** * 正在生效预警 * * @param data * @return */ private List getList(List data) { List respList = Lists.newArrayList(); for (WarningData datum : data) { String effectArea = datum.getEffectArea(); for (WarningData.TypeList typeList : datum.getTypeList()) { // 预警类型 String type = typeList.getType(); for (WarningData.Warning warning : typeList.getWarnList()) { WarningListVo vo = new WarningListVo(); String publishUnit = warning.getPublishUnit(); vo.setTitle(effectArea + publishUnit + "发布" + type + "预警"); vo.setPublishTime(warning.getPublishTime()); vo.setContent(warning.getContent()); vo.setWarnSignalType(warning.getWarnSignalType()); vo.setWarnSignalLevel(warning.getWarnSignalLevel()); vo.setAddressBooks(addressBookService.getListByAdnm(publishUnit.replace("气象台", ""))); respList.add(vo); } } } return respList; } /** * 正在生效预警 * * @param data * @return */ private List getCount(List data) { List respList = Lists.newArrayList(); for (WarningData datum : data) { WarningHistoryListVo vo = new WarningHistoryListVo(); vo.setWarnSignalType("全部"); vo.setEffectArea(datum.getEffectArea()); List child = Lists.newArrayList(); for (WarningData.TypeList typeList : datum.getTypeList()) { // 预警类型 List warnList = typeList.getWarnList(); // 类型分组 Map> types = warnList.stream() .collect(Collectors.groupingBy(WarningData.Warning::getWarnSignalType, Collectors.toList())); types.forEach((k, v) -> { WarningHistoryListVo childVo = new WarningHistoryListVo(); // 等级分组 Map levels = v.stream() .collect(Collectors.groupingBy(WarningData.Warning::getWarnSignalLevel, Collectors.counting())); levels.forEach((k1, v1) -> { switch (k1) { case "红色": childVo.setRedCount(v1.intValue()); vo.setRedCount(v1.intValue() + vo.getRedCount()); break; case "橙色": childVo.setOraCount(v1.intValue()); vo.setOraCount(v1.intValue() + vo.getOraCount()); break; case "黄色": childVo.setYelCount(v1.intValue()); vo.setYelCount(v1.intValue() + vo.getYelCount()); break; case "蓝色": childVo.setBluCount(v1.intValue()); vo.setBluCount(v1.intValue() + vo.getBluCount()); break; default: throw new MyException("当前预警等级不存在: " + k1); } }); childVo.setSumCount(v.size()); vo.setSumCount(vo.getSumCount() + v.size()); childVo.setWarnSignalType(k); child.add(childVo); }); } vo.setSumCount(datum.getTypeList().size()); vo.setChild(child); respList.add(vo); } return respList; } }