Newer
Older
huludao / src / main / java / com / newfiber / api / pc / service / SiteBaseService.java
package com.newfiber.api.pc.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.newfiber.api.core.bean.ReqBodyObj;
import com.newfiber.api.core.scheduled.utils.Constant;
import com.newfiber.api.pc.dao.WarnConfigWaterLeverDao;
import com.newfiber.api.pc.dao.monitoring.WarnConfigDao;
import com.newfiber.api.pc.dao.newmonitoring.StationBaseDao;
import com.newfiber.api.pc.model.entity.WarnConfigWaterLever;
import com.newfiber.api.pc.model.monitor.StationBase;
import com.newfiber.api.pc.model.monitor.WarnConfig;
import com.newfiber.api.pc.model.monitor.Sl6512014;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SiteBaseService {

    @Autowired
    private StationBaseDao stationBaseDao;

    @Autowired
    private WarnConfigDao warnConfigDao;

    @Autowired
    private WarnConfigWaterLeverDao warnConfigWaterLeverDao;

    @Autowired
    private MongoTemplate mongoTemplate;

    public List<StationBase> listStationBase(Map<String,Object> map){
        return stationBaseDao.listStationBaseWithTempData(map);
    }

    public List<StationBase> listStationBase(StationBase stationBase, Page<StationBase> page){
        if (!StringUtils.isEmpty(stationBase.getStationType()) && !Constant.TWO.equals(Integer.valueOf(stationBase.getStationType()))){
            stationBase.setStationType(Constant.WaterQualityEnum.getValueBykey(Integer.valueOf(stationBase.getStationType())));
        }

        //1.分页查询预警报警信息站点信息
        List<StationBase> stationBases= stationBaseDao.listStationBase(stationBase,page);

        if (CollectionUtils.isEmpty(stationBases)){
            return stationBases;
        }

        List<String> stCodeList = stationBases.stream().map(StationBase::getStCode).collect(Collectors.toList());

        //2.批量查询站点预警报警配置信息
        List<WarnConfig> warnConfigs = warnConfigDao.warnConfigByStCodes(stCodeList);

        Map<String, List<WarnConfig>> warnConfigMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(warnConfigs)){
            warnConfigMap = warnConfigs.stream().collect(Collectors.groupingBy(WarnConfig::getSiteNo));
        }


        //3.批量查询水质预警报警配置信息
        List<WarnConfigWaterLever> warnConfigWaters= warnConfigWaterLeverDao.warnConfigWaterByStCodes(stCodeList);
        Map<String, List<WarnConfigWaterLever>> warnConfigWaterMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(warnConfigWaters)){
            warnConfigWaterMap = warnConfigWaters.stream().collect(Collectors.groupingBy(WarnConfigWaterLever::getStCode));
        }

        for (StationBase stBase : stationBases) {
            //4.站点预警报警配置信息组装
            if (!CollectionUtils.isEmpty(warnConfigMap.get(stBase.getStCode()))){
                List<WarnConfig> warnConfigList = warnConfigMap.get(stBase.getStCode());
                List<WarnConfig> warns = warnConfigList.stream().filter(e->e.getWarnType() == 1).collect(Collectors.toList());
                List<WarnConfig> alarms = warnConfigList.stream().filter(e->e.getWarnType() == 2).collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(warns)){
                    //4.1 站点预警信息
                    List<String> warnInfos = new ArrayList<>();
                    warns.forEach(item ->{
                        StringBuilder s = new StringBuilder();
                        if(item.getConditionType() == 2){
                            s.append(item.getCompareWay());
                        }else{
                            s.append(item.getFactorsName())
                            .append(":")
                            .append(item.getCompareWayDesc())
                            .append(item.getWarnValue().doubleValue())
                            .append(Constant.PROPERTY_INFO.get(item.getFactorsAscii()).get("unit"));
                        }
                        warnInfos.add(s.toString());
                    });
                    stBase.setWarnInfos(StringUtils.join(warnInfos,"、"));
                }

                if(!CollectionUtils.isEmpty(alarms)){
                    //4.2 站点报警信息
                    List<String> alarmInfos = new ArrayList<>();
                    alarms.forEach(item ->{
                        StringBuilder s = new StringBuilder();
                        if(item.getConditionType() == 2){
                            s.append(item.getCompareWay());
                        }else {
                            s.append(item.getFactorsName())
                            .append(":")
                            .append(item.getCompareWayDesc())
                            .append(item.getWarnValue().doubleValue())
                            .append(Constant.PROPERTY_INFO.get(item.getFactorsAscii()).get("unit"));
                        }
                        alarmInfos.add(s.toString());
                    });
                    stBase.setAlarmInfos(StringUtils.join(alarmInfos,"、"));
                }
            }

            //5.站点水质预警报警配置信息组装
            if (!CollectionUtils.isEmpty(warnConfigWaterMap.get(stBase.getStCode()))){
                //预警报警各自只能有一条
                List<WarnConfigWaterLever> warnConfigWaterLeverList = warnConfigWaterMap.get(stBase.getStCode());

                warnConfigWaterLeverList.forEach(item ->{
                    if (Constant.WranType.EARLY_WARNING.getKey().equals(item.getWarnType())){
                        stBase.setWaterWarnInfo(item.getCompareWayDesc() + item.getWarnWaterLevelDesc());
                    }else if (Constant.WranType.CALL_POLICE.getKey().equals(item.getWarnType())){
                        stBase.setWaterAlarmInfo(item.getCompareWayDesc() + item.getWarnWaterLevelDesc());
                    }
                });
            }
        }

        return stationBases;
    }

    public List<Map<String,Object>> getTempData(Map<String,Object> map){
        return stationBaseDao.getTempData(map);
    }

    public List<Map<String,Object>> getHistoryDataTitle(Map<String,Object> map){
        List<Map<String,Object>> result = new ArrayList<>();
        List<Map<String, Object>> tempData = stationBaseDao.getDataTitle(map);
        tempData.forEach(e -> {
            Map<String, Object> title = new HashMap<>();
            title.put("align", "center");
            title.put("field", "DO".equals(String.valueOf(e.get("code"))) || "LONG".equals(String.valueOf(e.get("code"))) ? String.valueOf(e.get("code")) : String.valueOf(e.get("code")).toLowerCase());
            title.put("title", e.get("name") + " " + (e.get("unit") == null ? "" : e.get("unit")));
            result.add(title);
        });
        return result;
    }

    public Page<Sl6512014> getHistoryData(ReqBodyObj<Map<String,Object>> params, Page<Sl6512014> page){

        /** 获取数据 */
        Query query = new Query().with(new Sort(Sort.Direction.DESC,"tt"));
        if (params.getCurrent() > 0 && params.getSize() > 0){
            query.skip((params.getCurrent() - 1) * params.getSize());
            query.limit(params.getSize());
        }
        setQueryWithTime(params.getData(),query);
        String stCode = String.valueOf(params.getData().get("stCode"));
        /** 查出记录总数 */
        Long count = mongoTemplate.count(query, Sl6512014.class, stCode);
        page.setTotal(count.intValue());
        /** 查出当前页的记录 */
        List<Sl6512014> sl6512014s = mongoTemplate.find(query, Sl6512014.class, stCode);
        page.setRecords(sl6512014s);
        return page;
    }

    private void setQueryWithTime(Map<String, Object> params, Query query) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Object startTime = params.get("startTime");
        Object endTime = params.get("endTime");
        if (startTime != null && endTime != null) {
            try {
                Date s = sdf.parse(startTime.toString());
                Date e = sdf.parse(endTime.toString());
                query.addCriteria(Criteria.where("tt").gte(s).lte(e));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}