package com.tofly.xrtygis.custom.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.api.CodeClient;
import com.tofly.api.DepartmentClient;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.DateUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.pojo.Department;
import com.tofly.xrtygis.custom.entity.dto.BigScreeRealIndexDto;
import com.tofly.xrtygis.custom.entity.em.BigScreenEnum;
import com.tofly.xrtygis.custom.entity.vo.IndexInfoVo;
import com.tofly.xrtygis.custom.mapper.BigScreenMapper;
import com.tofly.xrtygis.custom.mapper.CustomPollSourceMapper;
import com.tofly.xrtygis.custom.service.BigScreenService;
import com.tofly.xrtygis.custom.service.CustomDisplayService;
import com.tofly.xrtygis.custom.service.CustomScadaAboutService;
import com.tofly.xrtygis.em.Constant;
import com.tofly.xrtygis.entity.*;
import com.tofly.xrtygis.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 大屏相关
 *
 * @author 陈浩
 * @date 2021/4/19
 */
@Service

public class BigScreenServiceImpl implements BigScreenService {

    private final static String WARN_HISTORY = "TF_YWPN_WARNHISTORY_W_";

    @Autowired
    private DepartmentClient departmentClient;
    @Autowired
    private CodeClient codeClient;
    @Autowired
    private ScadastaitemService scadastaitemService;
    @Autowired
    private ScadastationService scadastationService;
    @Autowired
    private VideoconfigService videoconfigService;
    @Autowired
    private CustomDisplayService customDisplayService;
    @Autowired
    private ScadarealtimeRService scadarealtimeRService;
    @Autowired
    private CustomScadaAboutService customScadaAboutService;
    @Autowired
    private WarnService warnService;
    @Autowired
    private BigScreenMapper bigScreenMapper;
    @Autowired
    private PollsourceService pollsourceService;
    @Autowired
    private CustomPollSourceMapper customPollSourceMapper;
    @Autowired
    private WaterparamsService waterparamsService;

    private static final Long YC_ID = 784L;
    private static final Long EC_ID = 785L;

    /**
     * 获取水质参数配置全部信息
     *
     * @return list
     */
    @Override
    public List getInfo() {
        return waterparamsService.list();
    }

    /**
     * 根据父级多个ID查看所有子级
     *
     * @param pid
     * @return ResultRespone
     */
    @Override
    public ResultRespone getBylistPcid(String pid) {
        return codeClient.getBylistPcid(pid);
    }

    /**
     * 获取全部部门信息
     *
     * @return ResultRespone
     */
    @Override
    public ResultRespone<List<Department>> departmentListAll() {
        return departmentClient.departmentListAll();
    }

    /**
     * 获取部门用户
     *
     * @param depts
     * @return ResultRespone
     */
    @Override
    public ResultRespone<List<Map<String, Object>>> getUserByDepts(String depts) {
        return departmentClient.getUserByDepts(depts);
    }

    /**
     * 查看测站信息
     *
     * @param stnm 测站名称
     * @param stcd 测站编号
     * @return page
     */
    @Override
    public List selectStation(Long id, String stnm, String stcd, String categoryName) {
        return customScadaAboutService.selectStation(id, stnm, stcd, categoryName);
    }

    /**
     * 查看对应测站指标
     *
     * @param ids 测站id
     * @return list
     */
    @Override
    public List selectIndex(String ids) {
        return scadastaitemService.list(Wrappers.<Scadastaitem>lambdaQuery()
                .eq(Scadastaitem::getState, 1)
                .in(Scadastaitem::getStationId, Arrays.asList(ids.split(","))));
    }

    /**
     * 大屏-实时指标信息
     *
     * @return map
     */
    @Override
    public Map<String, Object> realTimeIndexInfo(BigScreeRealIndexDto dto) {
        Map<String, Object> result = new HashMap<>(7);
        //一厂前一小时取供水量
        result.put("hourWaterOne", bigScreenMapper.getHourWater(dto.getYcid()));
        //二厂前一小时取供水量
        result.put("hourWaterTwo", bigScreenMapper.getHourWater(dto.getEcid()));
        //一厂取供水量
        result.put("oneWater", bigScreenMapper.getwaterVolume(dto.getYcid()));
        //二厂取供水量
        result.put("twoWater", bigScreenMapper.getwaterVolume(dto.getEcid()));
        //加压站电耗
        result.put("stationPower", bigScreenMapper.getStationPower());
        //一厂二厂的实时指标信息
        result.put("realTime", bigScreenMapper.getRealVal(Arrays.asList(dto.getItcds().split(","))));
        //一二厂耗药量
        result.put("value", bigScreenMapper.getValue());

        result.put("waterVolume", bigScreenMapper.viewWaterVolume());
        return result;
    }

    /**
     * 查询供售水历史
     *
     * @param page  page
     * @param start 开始时间
     * @param end   结束时间
     * @param code  0：供水量 1 售水量
     * @return
     */
    @Override
    public Page waterVolume(Page page, String start, String end, String code) {
        return bigScreenMapper.waterVolume(page, start, end, code);
    }


    /**
     * 查看小时去供水历史
     *
     * @param code  0：一厂  1：二厂
     * @param param 0：取水量  1：供水量
     * @param start 开始时间
     * @param end   结束时间
     * @return page
     */
    @Override
    public Page hourWaterHistory(Page page, String code, String param, String start, String end) {

        Page result;

        if ("0".equals(code)) {
            if ("0".equals(param)) {
                result = bigScreenMapper.hourQslHistory(page, start, end, YC_ID);
            } else {
                result = bigScreenMapper.hourGslHistory(page, start, end, YC_ID);
            }
        } else {
            if ("0".equals(param)) {
                result = bigScreenMapper.hourQslHistory(page, start, end, EC_ID);
            } else {
                result = bigScreenMapper.hourGslHistory(page, start, end, EC_ID);
            }
        }

        return result;
    }

    /**
     * 实时指标信息-查看加压站电耗历史信息
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return page
     */
    @Override
    public Page getStationHistory(Page page, String start, String end) {
        return bigScreenMapper.getStationHistory(page, start, end);
    }

    /**
     * 实时指标信息-获取水量历史
     *
     * @param start 开始时间
     * @param end   结束时间
     * @param param gsl/qsl  供水量/取水量
     * @param itid  水厂id
     * @return page
     */
    @Override
    public List<Map> getWaterValHistory(String start, String end, String param, Long itid) {
        List<Map> result;
        if (itid.equals(YC_ID)) {
            if ("qsl".equals(param)) {
                result = bigScreenMapper.dayQslHistory(start, end, YC_ID);
            } else {
                result = bigScreenMapper.dayGslHistory(start, end, YC_ID);
            }
        } else {
            if ("qsl".equals(param)) {
                result = bigScreenMapper.dayQslHistory(start, end, EC_ID);
            } else {
                result = bigScreenMapper.dayGslHistory(start, end, EC_ID);
            }
        }
        return result;
    }

    /**
     * 实时指标信息-获取药耗接口
     *
     * @param start 开始时间
     * @param end   结束时间
     * @param param param
     * @return page
     */
    @Override
    public List<Map> getConsumeHistory(String start, String end, String param) {
        return bigScreenMapper.getConsumeHistory(start, end, param);
    }

    /**
     * 实时指标信息-获取瞬时指标历史
     *
     * @param page  page
     * @param start 开始时间
     * @param end   结束时间
     * @param param param
     * @return page
     */
    @Override
    public Page getIndexHistory(Page page, String start, String end, String param) {
        return bigScreenMapper.getIndexHistory(page, start, end, param);
    }

    /**
     * 实时指标详细-一厂电耗历史
     *
     * @param start 开始时间
     * @param end   结束时间
     * @param code  0,一厂  1 二厂
     * @return page
     */
    @Override
    public List<Map> getPowerHistory(String start, String end, String code) {
        List<Map> result;
        if ("0".equals(code)) {
            result = bigScreenMapper.getPowerHistory(start, end, BigScreenEnum.DH_ONE.getParam());
        } else {
            result = bigScreenMapper.getPowerHistory(start, end, BigScreenEnum.DH_TWO.getParam());
        }
        return result;
    }


    /**
     * 获取产销差
     *
     * @return map
     */
    @Override
    public Map<String, Object> getNrw() {
        Map<String, Object> returnMap = new HashMap<>(3);

        returnMap.put("monthNrw", bigScreenMapper.monthNrw());
        returnMap.put("yearNrwPlan", bigScreenMapper.yearNrwPlan());
        returnMap.put("yearNrw", bigScreenMapper.yearNrw());

        return returnMap;
    }

    /**
     * 查看更多产销差信息
     *
     * @param years 年份
     * @param code  0，按月查看   1，季度查看  2 近三年
     * @return map
     */
    @Override
    public Map<String, Object> nrwMore(String years, Integer code) {

        String[] split = years.split(",");

        Map<String, Object> result = new HashMap();

        Map<String, Object> map;

        for (String year : split) {
            map = new HashMap<>();
            if (code == 1) {
                map.put("nrw", bigScreenMapper.nrwByQuarterly(year));
                map.put("nrwPlan", bigScreenMapper.nrwByQuarterlyPlan(year));
            } else {
                map.put("nrw", bigScreenMapper.nrwByMonth(year, code));
                map.put("nrwPlan", bigScreenMapper.nrwByCodePlan(year, code));
            }
            map.put("yearSum", bigScreenMapper.getNrwByYear(year));
            map.put("yearSumPlan", bigScreenMapper.getNrwPlanByYear(year));
            result.put(year, map);
        }


        return result;
    }

    /**
     * 获取漏损率
     *
     * @return map
     */
    @Override
    public Map<String, Object> getLeakage() {
        Map<String, Object> returnMap = new HashMap<>(3);

        //本年每月漏损率
        returnMap.put("monthLeakage", bigScreenMapper.monthLeakage());
        //本年漏损率
        returnMap.put("yearLeakage", bigScreenMapper.yearLeakage());
        //本年预算漏损率
        returnMap.put("yearLeakagePlan", bigScreenMapper.yearLeakagePlan());

        return returnMap;
    }

    /**
     * 查看更多漏损率信息
     *
     * @param years 年份
     * @param code  0，按月查看   1，季度查看  2 近三年查看
     * @return map
     */
    @Override
    public Map<String, Object> leakageMore(String years, Integer code) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map;

        for (String year : years.split(",")) {
            map = new HashMap<>();
            if (code == 1) {
                map.put("leakage", bigScreenMapper.leakageByQuarterly(year));
                map.put("leakagePlan", bigScreenMapper.leakageByQuarterlyPlan(year));
            } else {
                map.put("leakage", bigScreenMapper.leakageByMonth(year, code));
                map.put("leakagePlan", bigScreenMapper.leakageByCodePlan(year, code));
            }
            map.put("yearSum", bigScreenMapper.getLeakageByYear(year));
            map.put("yearSumPlan", bigScreenMapper.getLeakagePlanByYear(year));
            result.put(year, map);
        }
        return result;
    }

    /**
     * 获取供水量和售水量
     *
     * @return map
     */
    @Override
    public Map<String, Object> getSupSellWater() {
        Map<String, Object> returnMap = new HashMap<>(4);

        returnMap.put("monSupSellWater", bigScreenMapper.monSupSellWater());
        returnMap.put("monSupSellWaterPlan", bigScreenMapper.monSupSellWaterPlan());
        returnMap.put("yearSupSellWater", bigScreenMapper.yearSupSellWater());
        returnMap.put("yearSupSellWaterPlan", bigScreenMapper.yearSupSellWaterPlan());

        return returnMap;
    }

    /**
     * 查看供水量和售水量更多信息
     *
     * @param years 年份
     * @param code  0，按月查看   1，季度查看  2 近三年查看
     * @return map
     */
    @Override
    public Map<String, Object> supSellWaterMore(String years, Integer code) {
        Map<String, Object> result = new HashMap<>(years.split(",").length);
        Map<String, Object> map;

        for (String year : years.split(",")) {

            map = new HashMap<>();

            if (code == 1) {
                map.put("supSell", bigScreenMapper.supSellMoreQuarterly(year));
                map.put("supSellPlan", bigScreenMapper.supSellMoreQuarterlyPlan(year));
            } else {
                map.put("supSell", bigScreenMapper.supSellMore(year, code));
                map.put("supSellPlan", bigScreenMapper.supSellMorePlan(year, code));
            }
            map.put("supSellYear", bigScreenMapper.getSupSellByYear(year));
            map.put("supSellPlanYear", bigScreenMapper.getSupSellPlanByYear(year));
            result.put(year, map);
        }
        return result;
    }

    /**
     * 获取制水单变
     *
     * @return map
     */
    @Override
    public Map<String, Object> getFixed() {
        Map<String, Object> returnMap = new HashMap<>();

        List<Map> monthFixed = bigScreenMapper.monthFixed();

        Double yearFixed = bigScreenMapper.yearFixed();

        Double yearFixedPlan = bigScreenMapper.yearFixedPlan();

        returnMap.put("monthFixed", monthFixed);
        returnMap.put("yearFixed", yearFixed);
        returnMap.put("yearFixedPlan", yearFixedPlan);

        return returnMap;
    }

    /**
     * 获取更多单变信息
     *
     * @param years 年份
     * @param code  0:月  1：季   2 近三年
     * @param type  0:一厂 1：二厂 2：全部
     * @return
     */
    @Override
    public Map<String, Object> newfixedMore(String years, Integer code, String type) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map;


        for (String year : years.split(",")) {
            map = new HashMap<>();

            List<Map> fixed;
            List<Map> fixedPlan;
            List<Map> costInfo;

            if ("0".equals(type)) {

                if (1 == code) {
                    fixed = bigScreenMapper.firstPlantQue(year);
                    fixedPlan = bigScreenMapper.firstPlantQuePlan(year);
                    costInfo = bigScreenMapper.getEveryCostOneQue(year);
                } else {
                    costInfo = bigScreenMapper.getEveryCostOneMonth(year);
                    fixed = bigScreenMapper.firstPlantFixed(year, code);
                    fixedPlan = bigScreenMapper.firstPlantFixedPlan(year, code);
                }
                map.put("cost", bigScreenMapper.getEveryCostOne(year));
                map.put("fixedYear", bigScreenMapper.firstPlantFixedYear(year));
                map.put("fixedPlanYear", bigScreenMapper.firstPlantFixedPlanYear(year));
                map.put("fixed", fixed);
                map.put("fixedPlan", fixedPlan);
                map.put("costInfo", costInfo);
            } else if ("1".equals(type)) {

                if (1 == code) {
                    fixed = bigScreenMapper.secondPlantQue(year);
                    fixedPlan = bigScreenMapper.secondPlantQuePlan(year);
                    costInfo = bigScreenMapper.getEveryCostTwoQue(year);
                } else {
                    fixed = bigScreenMapper.secondPlantFixed(year, code);
                    fixedPlan = bigScreenMapper.secondPlantFixedPlan(year, code);
                    costInfo = bigScreenMapper.getEveryCostTwoMonth(year);
                }
                map.put("cost", bigScreenMapper.getEveryCostTwo(year));
                map.put("fixedYear", bigScreenMapper.secondPlantFixedYear(year));
                map.put("fixedPlanYear", bigScreenMapper.secondPlantFixedPlanYear(year));
                map.put("fixed", fixed);
                map.put("fixedPlan", fixedPlan);
                map.put("costInfo", costInfo);

            } else {

                if (1 == code) {
                    fixed = bigScreenMapper.allFixedQue(year);
                    fixedPlan = bigScreenMapper.allFixedQuePlan(year);
                    costInfo = bigScreenMapper.getEveryCostAllQue(year);
                } else {
                    fixed = bigScreenMapper.allFixed(year, code);
                    fixedPlan = bigScreenMapper.allFixedPlan(year, code);
                    costInfo = bigScreenMapper.getEveryCostAllMonth(year);
                }
                map.put("cost", bigScreenMapper.getEveryCostAll(year));
                map.put("fixedYear", bigScreenMapper.allFixedYear(year));
                map.put("fixedPlanYear", bigScreenMapper.allFixedPlanYear(year));
                map.put("fixed", fixed);
                map.put("fixedPlan", fixedPlan);
                map.put("costInfo", costInfo);
            }
            result.put(year, map);
        }
        return result;
    }

    /**
     * 获取多个测站指标
     *
     * @param stids 测站id集合
     * @return map
     */
    @Override
    public Map<Long, Map<Long, Object>> getSiteInfos(String stids) {
        return customDisplayService.getSiteInfos(stids);
    }

    /**
     * 获取水量
     *
     * @param itcds 指标编号
     * @return double
     */
    @Override
    public Double getWaterVolume(String itcds) {

        String[] itcdList = itcds.split(",");

        double waterVolume = 0D;

        for (String itcd : itcdList) {
            double result;

            Double todayWater = bigScreenMapper.getTodayWater(itcd);
            Double yesterday = bigScreenMapper.getYesterday(itcd);

            if (yesterday == null) {
                result = todayWater;
            } else {
                result = todayWater - yesterday;
            }

            waterVolume += result;

        }
        return waterVolume;
    }

    /**
     * 获取耗量
     *
     * @return map
     */
    @Override
    public List<Map> getConsumption() {

        List<Map> list = new ArrayList<>();

        Map<String, Double> medicine = bigScreenMapper.medicine();
        Map<String, Double> power = bigScreenMapper.power();

        list.add(medicine);
        list.add(power);
        return list;
    }

    /**
     * 获取指标实时信息
     *
     * @param itcds 指标信息
     * @return list
     */
    @Override
    public List getRealTimeItVal(String itcds) {
        List<String> itcdList = Arrays.asList(itcds.split(","));

        List<ScadarealtimeR> list = scadarealtimeRService.list(Wrappers.<ScadarealtimeR>lambdaQuery()
                .in(ScadarealtimeR::getItcd, itcdList)
                .select(ScadarealtimeR::getItcd, ScadarealtimeR::getItval,ScadarealtimeR::getItnumVal));

        if (list.isEmpty()) {
            throw new ToflyDeniedException("未查询到相关数据");
        }

        return list;
    }

    /**
     * 获取指标信息
     *
     * @param page      page
     * @param stid      测站id
     * @param itnm      指标项
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return page
     */
    @Override
    public Page getIndexInfo(Page page, Long stid, String itnm, String startTime, String endTime) {
        return customScadaAboutService.viewHistory(page, stid, itnm, startTime, endTime);
    }

    @Override
    public List getIndexInfo(Long stid, String itnm, String startTime, String endTime) {
        return customScadaAboutService.viewHistory(stid, itnm, startTime, endTime);
    }

    /**
     * 大屏视频监控
     *
     * @return list
     */
    @Override
    public List getVideoInfo() {
        return videoconfigService.list(Wrappers.<Videoconfig>lambdaQuery().eq(Videoconfig::getIsshow, "1"));
    }

    /**
     * 获取测站指标实时信息
     *
     * @param dto 测站和指标集合
     * @return map
     */
    @Override
    public Map<String, Object> artwork(Map<String, String> dto) {
        Map<String, Object> result = new HashMap<>();

        dto.forEach((k, v) -> {
            List<String> itcdList = Arrays.asList(v.split(","));
            List<IndexInfoVo> realIndexInfo = bigScreenMapper.getRealIndexInfo(itcdList);
            result.put(k, realIndexInfo);
        });
        return result;
    }

    /**
     * 获取全部实时指标信息
     *
     * @param scids 多个站点id
     * @return
     */
    @Override
    public Map<String, Object> getAllRealIndex(String scids) {
        Map<String, Object> result = new HashMap<>();
        //将测站id根据，分隔
        String[] scidList = scids.split(",");

        Map<String, Object> scInfo;

        for (String scid : scidList) {
            scInfo = new HashMap<>();
            //根据站点id取出关联指标编号，用于查询实时信息
            List<Scadastaitem> staitem = scadastaitemService.list(Wrappers.<Scadastaitem>lambdaQuery()
                    .eq(Scadastaitem::getState, 1)
                    .select(Scadastaitem::getItcd)
                    .eq(Scadastaitem::getStationId, scid));
            if (staitem.isEmpty()) {
                result.put(scid, null);
                continue;
            }
            //只拿指标编号
            List<String> itcdList = staitem.stream().map(Scadastaitem::getItcd).collect(Collectors.toList());
            //指标的实时信息
            List<IndexInfoVo> realIndexInfo = bigScreenMapper.getRealIndexInfo(itcdList);

            if (!realIndexInfo.isEmpty()) {
                for (IndexInfoVo indexInfoVo : realIndexInfo) {
                    List<Map> warnType = bigScreenMapper.existWarn(indexInfoVo.getItcd());
                    if (!warnType.isEmpty()) {
                        indexInfoVo.setIsWarn("1");
                    }
                }
            }
            //查询站点信息获取坐标
            Scadastation station = scadastationService.getById(scid);

            if (null != station) {
                scInfo.put("lgtd", station.getLgtd());
                scInfo.put("lttd", station.getLttd());
            } else {
                scInfo.put("lgtd", null);
                scInfo.put("lttd", null);
            }

            scInfo.put("realIndexInfo", realIndexInfo);

            result.put(scid, scInfo);
        }

        return result;
    }

    /**
     * 确认报警信息
     *
     * @param warnIds 信息id
     * @param userId  确认人id
     * @return true
     */
    @Override
    public boolean handleWarn(String warnIds, Long userId) {
        for (String wanrId : warnIds.split(",")) {
            Warn warn = warnService.getById(wanrId);
            if (null != warn) {
                warnService.update(Wrappers.<Warn>lambdaUpdate()
                        .eq(Warn::getId, wanrId)
                        .set(Warn::getConfirmUser, userId)
                        .set(Warn::getConfirmTime, new Date())
                        .set(Warn::getIsconfirm, "1"));
                if (warn.getScadaTime() != null) {
                    String hisTab = WARN_HISTORY + DateUtil.getDateFormat(warn.getScadaTime(), "yyyyMM");
                    if (!customPollSourceMapper.doesItExistTable(hisTab).isEmpty()) {
                        bigScreenMapper.changeHistory(hisTab, wanrId, userId, null);
                    }
                }
            }
        }
        return true;
    }

    /**
     * 历史表确认报警信息
     *
     * @param warnIds warnId
     * @param userId  userId
     * @return boolean
     */
    @Override
    public boolean handleWarnHistory(String warnIds, Long userId) {
        for (String wanrId : warnIds.split(",")) {
            //查询是否存在该历史数据
            Date date = bigScreenMapper.viewWarnHistory(wanrId);
            if (null != date) {
                //表存在
                String hisTab = WARN_HISTORY + DateUtil.getDateFormat(date, "yyyyMM");
                if (!customPollSourceMapper.doesItExistTable(hisTab).isEmpty()) {
                    //实时表不为空
                    if (null != warnService.getById(wanrId)) {
                        bigScreenMapper.changeHistory(hisTab, wanrId, userId, null);

                        warnService.update(Wrappers.<Warn>lambdaUpdate()
                                .eq(Warn::getId, wanrId)
                                .set(Warn::getConfirmUser, userId)
                                .set(Warn::getConfirmTime, new Date())
                                .set(Warn::getIsconfirm, "1"));
                    } else {
                        //为空修改为清除
                        bigScreenMapper.changeHistory(hisTab, wanrId, userId, "1");
                    }
                }
            }
        }
        return true;
    }

    /**
     * 获取报警类型
     *
     * @return list
     */
    @Override
    public List<Map> getWarnType() {
        return bigScreenMapper.getWarnType();
    }

    /**
     * 获取部分指标的合计值
     *
     * @param index 指标
     * @return Double
     */
    @Override
    public Double getSumIndexInfo(String index) {
        if (index == null || "".equals(index)) {
            throw new ToflyDeniedException("指标信息为空");
        }
        return bigScreenMapper.getSumIndexInfo(Arrays.asList(index.split(",")));
    }


    /**
     * 获取报警实时信息
     *
     * @param code null:全部  0：未确认  1：确认
     * @return list
     */
    @Override
    public List getWarn(String code) {
        List<Map> warn = bigScreenMapper.getWarn();
        for (Map map : warn) {
            if (Constant.POLL_TYPE.equals(map.get("warnType"))) {
                Pollsource poll = pollsourceService.getById(Long.parseLong(map.get("itcd").toString()));
                if (null != poll) {
                    map.put("lgtd", poll.getLgtd());
                    map.put("lttd", poll.getLttd());
                }

            } else {
                Scadastaitem staitem = scadastaitemService.getOne(Wrappers.<Scadastaitem>lambdaQuery()
                        .eq(Scadastaitem::getState, 1)
                        .eq(Scadastaitem::getTycd, map.get("itcd")).select(Scadastaitem::getStationId));
                if (null != staitem) {
                    Scadastation station = scadastationService.getById(staitem.getStationId());
                    if (null != station) {
                        map.put("lgtd", station.getLgtd());
                        map.put("lttd", station.getLttd());
                        map.put("rangeid", station.getRangeid());
                    }
                }
            }
        }
        return warn;
    }

    /**
     * 获取历史信息
     *
     * @param page  page
     * @param type  报警类型类型
     * @param start 开始时间
     * @param end   结束时间
     * @return page
     */
    @Override
    public Page getWarnHistory(Page page, String type, String start, String end, String isclear, String isconfirm) {
        Page<Map> warHistory = bigScreenMapper.getWarHistory(page, type, start, end, isclear, isconfirm);
        List<Map> records = warHistory.getRecords();
        for (Map map : records) {
            if (Constant.POLL_TYPE.equals(map.get("warnType"))) {
                Pollsource poll = pollsourceService.getById(Long.parseLong(map.get("itcd").toString()));
                if (null != poll) {
                    map.put("lgtd", poll.getLgtd());
                    map.put("lttd", poll.getLttd());
                }

            } else {
                Scadastaitem staitem = scadastaitemService.getOne(Wrappers.<Scadastaitem>lambdaQuery()
                        .eq(Scadastaitem::getTycd, map.get("itcd"))
                        .select(Scadastaitem::getStationId));
                if (null != staitem) {
                    Scadastation station = scadastationService.getById(staitem.getStationId());
                    if (null != station) {
                        map.put("station",station.getStnm());
                        map.put("lgtd", station.getLgtd());
                        map.put("lttd", station.getLttd());
                        map.put("rangeid", station.getRangeid());
                    }

                }
            }
        }
        return warHistory;
    }

    /**
     * 查看报警详情
     *
     * @param ids id
     * @return list
     */
    @Override
    public List getWarnInfo(String ids) {
        String[] idArr = ids.split(",");
        List result = new ArrayList();
        for (String id : idArr) {
            Warnhistory byId = bigScreenMapper.viewWarnInfo(id);
            if (null != byId) {
                if (Constant.POLL_TYPE.equals(byId.getWarnType())) {
                    result.add(pollsourceService.getById(byId.getItcd()));
                } else {
                    result.add(byId);
                }
            }
        }
        return result;
    }

    /**
     * 统计报警类型
     *
     * @return map
     */
    @Override
    public List<Map> countWarnType() {
        return bigScreenMapper.countWarnType();
    }

    /**
     * 获取报警信息
     *
     * @param page page
     * @return page
     */
    @Override
    public Page<Map> getWarnInfo(Page page, String type, String start, String end) {
        return bigScreenMapper.getWarnInfo(page, type, start, end);
    }

    @Override
    public Object getItcdValeHistory(Page page, String itcd, String start, String end, String state) {
        if ("0".equals(state)) {
            return bigScreenMapper.getItcdValeHistory(page, itcd, start, end);
        } else {
            return bigScreenMapper.getItcdValeHistory(itcd, start, end);
        }
    }

    @Override
    public Scadastaitem getItcdInfo(String itcd) {
        return scadastaitemService.getOne(Wrappers.<Scadastaitem>lambdaQuery().eq(Scadastaitem::getItcd, itcd));
    }
}
