/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/27
 */
package org.springblade.modules.business.home.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.baseinfo.monitorpoint.dto.MonitorPointInfoDTO;
import org.springblade.modules.baseinfo.monitorpoint.service.IMonitorPointService;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.baseinfo.project.entity.ProjectInfoEntity;
import org.springblade.modules.baseinfo.project.service.IProjectBaseInfoService;
import org.springblade.modules.baseinfo.region.entity.RegionInfoEntity;
import org.springblade.modules.baseinfo.region.service.IBaseInfoRegionService;
import org.springblade.modules.baseinfo.rtu.dto.RtuInfoDTO;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.business.home.VO.*;
import org.springblade.modules.business.upcount.entity.RtuUpCountInfoEntity;
import org.springblade.modules.business.upcount.service.IRtuUpCountService;
import org.springblade.modules.business.warning.dto.RtuWarningInfoDTO;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/***
 * Date:2022/8/27
 * Title:首页模块接口
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@Slf4j
@RequestMapping("galaxy-business/home")
@AllArgsConstructor
@Api(value = "实时数据管理模块", tags = "实时数据管理模块")
public class HomeController {
    private final IRtuBaseInfoService rtuBaseInfoService;
    private final IRtuWarningService rtuWarningService;
    private final IDeptRegionService deptRegionService;
    private final IDeptService deptService;
    private final IBaseInfoRegionService regionService;
    private final IProjectBaseInfoService projectBaseInfoService;
    private final IMonitorPointService monitorPointService;
    private final IRtuUpCountService rtuUpCountService;

    @GetMapping("/rtu/status/count/info")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<RtuStatusCountInfoVO> rtuStatusCountInfo() {
        BladeUser user = AuthUtil.getUser();
        RtuStatusCountInfoVO infoVO = new RtuStatusCountInfoVO();
        LambdaQueryWrapper<ProjectInfoEntity> wrapper = Wrappers.<ProjectInfoEntity>query().lambda();
        wrapper.eq(ProjectInfoEntity::getIsDeleted, 0);
        wrapper.eq(ProjectInfoEntity::getOrgId, Func.toLong(user.getDeptId()));
        long count = projectBaseInfoService.count(wrapper);
        infoVO.setProjects(count);

        //测站数量
        RtuInfoDTO rtuInfoDTO = new RtuInfoDTO();
        count = rtuBaseInfoService.rtuCount(rtuInfoDTO);
        infoVO.setMonitorSites(count);

        //预警数量
        RtuWarningInfoDTO dto = new RtuWarningInfoDTO();
        long warningRtuCount = rtuWarningService.warningRtuCount(dto);
        infoVO.setWarnRtus(warningRtuCount);
        //离线数量
        count = rtuWarningService.warningOfflineRtuCount(dto);
        infoVO.setOfflineRtus(count);
        //其他异常
        infoVO.setOtherWarnRtus((warningRtuCount - count));

        return R.data(infoVO);
    }

    @GetMapping("/rtu/up/count/info")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<RtuUpDataCountInfoVO> rtuUpCountInfo() {
        LocalDateTime checkTime = LocalDateTime.now();
        RtuUpDataCountInfoVO infoVO = new RtuUpDataCountInfoVO();
        RtuInfoDTO rtuInfoDTO = new RtuInfoDTO();
        long count = rtuBaseInfoService.rtuCount(rtuInfoDTO);
        infoVO.setMonitorPoints(count);

        LambdaQueryWrapper<RtuUpCountInfoEntity> wrapper = Wrappers.<RtuUpCountInfoEntity>query().lambda();
        wrapper.eq(RtuUpCountInfoEntity::getTimeKind, 1);
        LocalDateTime et = checkTime.withMinute(0).withSecond(0).withNano(0);
       // LocalDateTime st = et.minusHours(1);
        wrapper.eq(RtuUpCountInfoEntity::getCountTime, Date.from(et.atZone(ZoneId.systemDefault()).toInstant()));
        wrapper.gt(RtuUpCountInfoEntity::getUpCount, 0);
        count = rtuUpCountService.count(wrapper);
        infoVO.setHourUpDataCount(count);

        wrapper = Wrappers.<RtuUpCountInfoEntity>query().lambda();
        wrapper.eq(RtuUpCountInfoEntity::getTimeKind, 2);
        et = checkTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
       // st = et.minusDays(1);
        wrapper.eq(RtuUpCountInfoEntity::getCountTime, Date.from(et.atZone(ZoneId.systemDefault()).toInstant()));
        wrapper.gt(RtuUpCountInfoEntity::getUpCount, 0);
        count = rtuUpCountService.count(wrapper);
        infoVO.setDayUpDataCount(count);

        return R.data(infoVO);
    }

    @GetMapping("/warn/count/list")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "预警统计列表", notes = "")
    public R<List<WarnCountInfoVO>> warnCountInfoList() {
        List<WarnCountInfoVO> list = new LinkedList<>();
        //盟市统计
        LambdaQueryWrapper<Dept> wrapper = Wrappers.<Dept>query().lambda();
        wrapper.eq(Dept::getParentId, BusinessConstant.DEPT_XAM_ID);
        List<Dept> deptList = this.deptService.list(wrapper);
        for (Dept dept : deptList) {
            WarnCountInfoVO vo = new WarnCountInfoVO();
            //盟市名称
            vo.setDeptId(dept.getId());
            vo.setAdCity(dept.getDeptName());
            //测站数量
            MonitorPointInfoDTO rtuInfoDTO = new MonitorPointInfoDTO();
            rtuInfoDTO.setOrgId(dept.getId());
            long count = monitorPointService.getMonitorPointCount(rtuInfoDTO);
            vo.setRtus(count);
            //预警数量
            RtuWarningInfoDTO dto = new RtuWarningInfoDTO();
            dto.setDeptId(dept.getId());
            long warningRtuCount = rtuWarningService.warningRtuCount(dto);
            vo.setWarnRtus(warningRtuCount);
            //离线数量
            count = rtuWarningService.warningOfflineRtuCount(dto);
            vo.setOfflineRtus(count);
            //其他异常
            vo.setOtherWarnRtus((warningRtuCount - count));
            list.add(vo);
        }
        return R.data(list);
    }

    @GetMapping("/rtu/warn/kind/count/info")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<WarnKindCountInfoVO> rtuWarnKindCountInfo() {
        BladeUser user = AuthUtil.getUser();
        String adCode = null;
        int excludeCode = 0;
        if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
            adCode = BusinessConstant.REGION_NM_ADCODE;
        } else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
            LambdaQueryWrapper<DeptRegionEntity> deptRegionEntityLambdaQueryWrapper = Wrappers.<DeptRegionEntity>query().lambda();
            deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getIsDeleted, 0);
            deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getDeptId, Func.toLong(user.getDeptId()));
            DeptRegionEntity deptRegionEntity = deptRegionService.getOne(deptRegionEntityLambdaQueryWrapper);
            if (deptRegionEntity != null) {
                adCode = deptRegionEntity.getAdcd();
                LambdaQueryWrapper<RegionInfoEntity> regionWrapper = Wrappers.<RegionInfoEntity>query().lambda();
                regionWrapper.eq(RegionInfoEntity::getAdcd, deptRegionEntity.getAdcd());
                RegionInfoEntity regionInfoEntity = this.regionService.getOne(regionWrapper);
                if (regionInfoEntity.getParentCode().equals(BusinessConstant.REGION_NM_ADCODE)) {
                    excludeCode = 1;
                }
            }
        }
        if (null == adCode) {
            return R.status(false);
        }
        WarnKindCountInfoVO infoVO = new WarnKindCountInfoVO();
        RtuWarningInfoDTO dto = new RtuWarningInfoDTO();
        dto.setAdCode(adCode);
        dto.setExcludeCode(excludeCode);
        //异常测站总数
        Long count = rtuWarningService.warningRtuCount(dto);
        infoVO.setWarnRtus(count.intValue());
        //时钟异常数量
        count = rtuWarningService.warningClockRtuCount(dto);
        infoVO.setClockWarn(count.intValue());
        //离线数量
        count = rtuWarningService.warningOfflineRtuCount(dto);
        infoVO.setOfflineRtus(count.intValue());
        //雨量站小时漏报
        count = rtuWarningService.warningRainHourMissOutRtuCount(dto);
        infoVO.setRainUpMissOut(count.intValue());
        //水位站小时漏报
        count = rtuWarningService.warningRiverHourMissOutRtuCount(dto);
        infoVO.setRiverUpMissOut(count.intValue());
        //5分钟延时
        count = rtuWarningService.warningMinUpDelayRtuCount(dto);
        infoVO.setMinUpDelay(count.intValue());
        //雨量站小时延时
        count = rtuWarningService.warningRainHourUpDelayRtuCount(dto);
        infoVO.setRainHourUpDelay(count.intValue());
        //水位站小时延时
        count = rtuWarningService.warningRiverHourUpDelayRtuCount(dto);
        infoVO.setRiverHourUpDelay(count.intValue());
        //异常值
        count = rtuWarningService.warningOutlierValueRtuCount(dto);
        infoVO.setOutlierValue(count.intValue());
        return R.data(infoVO);
    }
}
