/**
 * 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.ConcurrentDateFormat;
import org.springblade.core.tool.utils.Func;
import org.springblade.enums.OrderCloseEnum;
import org.springblade.enums.OrderConfirmEnum;
import org.springblade.enums.RtuKindEnum;
import org.springblade.enums.WarnKindEnum;
import org.springblade.modules.baseinfo.dict.entity.DictInfoEntity;
import org.springblade.modules.baseinfo.dict.service.IBaseInfoDictService;
import org.springblade.modules.baseinfo.org.dto.OrgInfoDTO;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.baseinfo.org.service.IOrgService;
import org.springblade.modules.baseinfo.org.vo.OrgInfoVO;
import org.springblade.modules.baseinfo.rtu.dto.RtuInfoDTO;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.business.check.order.dto.CheckOrderInfoDTO;
import org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity;
import org.springblade.modules.business.check.order.entity.CheckOrderSettingEntity;
import org.springblade.modules.business.check.order.service.ICheckOrderSettingService;
import org.springblade.modules.business.check.order.service.IRtuCheckOrderService;
import org.springblade.modules.business.check.order.vo.CheckOrderInfoVO;
import org.springblade.modules.business.check.process.dto.OrderProcessInfoDTO;
import org.springblade.modules.business.check.process.service.IRtuOrderProcessService;
import org.springblade.modules.business.check.process.vo.OrderProcessInfoVO;
import org.springblade.modules.business.check.process.wrapper.RtuOrderProcessWrapper;
import org.springblade.modules.business.data.dto.RtuDataGroundDTO;
import org.springblade.modules.business.data.dto.RtuDataRainDTO;
import org.springblade.modules.business.data.dto.RtuDataRiverDTO;
import org.springblade.modules.business.data.service.IRtuDataGroundService;
import org.springblade.modules.business.data.service.IRtuDataRainService;
import org.springblade.modules.business.data.service.IRtuDataRiverService;
import org.springblade.modules.business.datagram.dto.RtuDatagramInfoDTO;
import org.springblade.modules.business.datagram.service.IRtuDatagramService;
import org.springblade.modules.business.equipment.inspection.base.dto.EquipmentInspectionInfoDTO;
import org.springblade.modules.business.equipment.inspection.base.vo.OrgEquipmentInspectionStatisticsVO;
import org.springblade.modules.business.equipment.inspection.plan.entity.EquipmentInspectionPlanEntity;
import org.springblade.modules.business.equipment.inspection.plan.service.IEquipmentInspectionPlanService;
import org.springblade.modules.business.equipment.inspection.report.dto.EquipmentInspectionReportDTO;
import org.springblade.modules.business.equipment.inspection.report.service.IEquipmentInspectionReportService;
import org.springblade.modules.business.equipment.inspection.report.vo.EquipmentInspectionReportVO;
import org.springblade.modules.business.equipment.inspection.report.wrapper.EquipmentInspectionReportWrapper;
import org.springblade.modules.business.home.VO.*;
import org.springblade.modules.business.notice.dto.NoticeDTO;
import org.springblade.modules.business.notice.entity.NoticeViewEntity;
import org.springblade.modules.business.notice.service.INoticeService;
import org.springblade.modules.business.notice.service.INoticeViewService;
import org.springblade.modules.business.rtumanage.dto.RtuStatusDTO;
import org.springblade.modules.business.rtumanage.service.IRtuManageService;
import org.springblade.modules.business.warning.dto.RtuWarningInfoDTO;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springblade.modules.business.warning.vo.RtuWarningInfoVO;
import org.springblade.modules.business.warning.wrapper.RtuWarningWrapper;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IDictBizService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.time.Duration;
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 IRtuCheckOrderService checkOrderService;
    private final INoticeService noticeService;
    private final INoticeViewService noticeViewService;
    private final IRtuBaseInfoService rtuBaseInfoService;
    private final IRtuWarningService rtuWarningService;
    private final IDeptRegionService deptRegionService;
    private final IEquipmentInspectionReportService inspectionReportService;
    private final ICheckOrderSettingService checkOrderSettingService;
    private final IEquipmentInspectionPlanService planService;
    private final IRtuOrderProcessService rtuOrderProcessService;
    private final IEquipmentInspectionReportService equipmentInspectionReportService;
    private final IDeptService deptService;
    private final IRtuDataRainService rainService;
    private final IRtuDataRiverService riverService;
    private final IRtuDataGroundService groundService;
    private final IOrgService orgService;
    private final IDictBizService dictBizService;
    private final IBaseInfoDictService dictService;
    private final IRtuManageService rtuManageService;
    private final IRtuDatagramService datagramService;

    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping("/org/info")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<OrgCountInfoVO> getOrgCountInfo() {
        OrgCountInfoVO infoVO = new OrgCountInfoVO();
        OrgInfoDTO dto = new OrgInfoDTO();
        dto.setParentId(0L);
        List<OrgInfoVO> list = this.orgService.selectList(dto);
        if (null != list) {
            dto.setParentId(list.get(0).getId());
            long count = this.orgService.orgCount(dto);
            infoVO.setOrgCount(count);
        }
        return R.data(infoVO);
    }

    @GetMapping("/rtu/info")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<RtuCountInfoVO> getRtuCountInfo() {
        RtuCountInfoVO infoVO = new RtuCountInfoVO();
        RtuInfoDTO dto = new RtuInfoDTO();
        long rtuCount = this.rtuBaseInfoService.rtuCount(dto);
        infoVO.setRtuCount(rtuCount);

        dto.setIsUpYwxt(1);
        rtuCount = this.rtuBaseInfoService.rtuCount(dto);
        infoVO.setRtuUpYwxtCount(rtuCount);

        LocalDateTime dt = LocalDateTime.now().withSecond(0).withNano(0);
        LocalDateTime st = dt.minusHours(1);

        RtuStatusDTO statusDTO = new RtuStatusDTO();
        statusDTO.setBeginDate(Date.from(st.atZone(ZoneId.systemDefault()).toInstant()));
        statusDTO.setEndDate(Date.from(dt.atZone(ZoneId.systemDefault()).toInstant()));
        long onlineCount = this.rtuManageService.rtuOnlineCount(statusDTO);
        infoVO.setRtuOnlineCount(onlineCount);

        return R.data(infoVO);
    }

    @GetMapping("/rtu/kind/info")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<List<RtuCountInfoVO>> getRtuKindCountInfo() {
        List<RtuCountInfoVO> voList = new ArrayList<>();

        RtuInfoDTO dto = new RtuInfoDTO();
        long rtuCount = this.rtuBaseInfoService.rtuCount(dto);

        RtuCountInfoVO vo = new RtuCountInfoVO();
        vo.setRtuCount(rtuCount);
        vo.setRtuKindName(RtuKindEnum.RTU_RAIN.getName());
        DictInfoEntity dictInfoEntity = this.dictService.getDict("rtu_kind", RtuKindEnum.RTU_RAIN.getCode());
        dto.setRtuKind(Func.toStr(dictInfoEntity.getId()));
        long rtuKindCount = this.rtuBaseInfoService.rtuCount(dto);
        vo.setRtuKindCount(rtuKindCount);
        voList.add(vo);

        vo = new RtuCountInfoVO();
        vo.setRtuCount(rtuCount);
        vo.setRtuKindName(RtuKindEnum.RTU_RIVER.getName());
        dictInfoEntity = this.dictService.getDict("rtu_kind", RtuKindEnum.RTU_RIVER.getCode());
        dto.setRtuKind(Func.toStr(dictInfoEntity.getId()));
        rtuKindCount = this.rtuBaseInfoService.rtuCount(dto);
        vo.setRtuKindCount(rtuKindCount);
        voList.add(vo);

        vo = new RtuCountInfoVO();
        vo.setRtuCount(rtuCount);
        vo.setRtuKindName(RtuKindEnum.RTU_RESERVOIR.getName());
        dictInfoEntity = this.dictService.getDict("rtu_kind", RtuKindEnum.RTU_RESERVOIR.getCode());
        dto.setRtuKind(Func.toStr(dictInfoEntity.getId()));
        rtuKindCount = this.rtuBaseInfoService.rtuCount(dto);
        vo.setRtuKindCount(rtuKindCount);
        voList.add(vo);

        vo = new RtuCountInfoVO();
        vo.setRtuCount(rtuCount);
        vo.setRtuKindName(RtuKindEnum.RTU_BARRAGE.getName());
        dictInfoEntity = this.dictService.getDict("rtu_kind", RtuKindEnum.RTU_BARRAGE.getCode());
        dto.setRtuKind(Func.toStr(dictInfoEntity.getId()));
        rtuKindCount = this.rtuBaseInfoService.rtuCount(dto);
        vo.setRtuKindCount(rtuKindCount);
        voList.add(vo);

        vo = new RtuCountInfoVO();
        vo.setRtuCount(rtuCount);
        vo.setRtuKindName(RtuKindEnum.RTU_PUMP_SITE.getName());
        dictInfoEntity = this.dictService.getDict("rtu_kind", RtuKindEnum.RTU_PUMP_SITE.getCode());
        dto.setRtuKind(Func.toStr(dictInfoEntity.getId()));
        rtuKindCount = this.rtuBaseInfoService.rtuCount(dto);
        vo.setRtuKindCount(rtuKindCount);
        voList.add(vo);

        vo = new RtuCountInfoVO();
        vo.setRtuCount(rtuCount);
        vo.setRtuKindName(RtuKindEnum.RTU_TIDE.getName());
        dictInfoEntity = this.dictService.getDict("rtu_kind", RtuKindEnum.RTU_TIDE.getCode());
        dto.setRtuKind(Func.toStr(dictInfoEntity.getId()));
        rtuKindCount = this.rtuBaseInfoService.rtuCount(dto);
        vo.setRtuKindCount(rtuKindCount);
        voList.add(vo);

        vo = new RtuCountInfoVO();
        vo.setRtuCount(rtuCount);
        vo.setRtuKindName(RtuKindEnum.RTU_GROUND_SITE.getName());
        dictInfoEntity = this.dictService.getDict("rtu_kind", RtuKindEnum.RTU_GROUND_SITE.getCode());
        dto.setRtuKind(Func.toStr(dictInfoEntity.getId()));
        rtuKindCount = this.rtuBaseInfoService.rtuCount(dto);
        vo.setRtuKindCount(rtuKindCount);
        voList.add(vo);

        vo = new RtuCountInfoVO();
        vo.setRtuCount(rtuCount);
        vo.setRtuKindName(RtuKindEnum.RTU_GROUND_WATER.getName());
        dictInfoEntity = this.dictService.getDict("rtu_kind", RtuKindEnum.RTU_GROUND_WATER.getCode());
        dto.setRtuKind(Func.toStr(dictInfoEntity.getId()));
        rtuKindCount = this.rtuBaseInfoService.rtuCount(dto);
        vo.setRtuKindCount(rtuKindCount);
        voList.add(vo);

        vo = new RtuCountInfoVO();
        vo.setRtuCount(rtuCount);
        vo.setRtuKindName(RtuKindEnum.RTU_WATER_QUALITY.getName());
        dictInfoEntity = this.dictService.getDict("rtu_kind", RtuKindEnum.RTU_WATER_QUALITY.getCode());
        dto.setRtuKind(Func.toStr(dictInfoEntity.getId()));
        rtuKindCount = this.rtuBaseInfoService.rtuCount(dto);
        vo.setRtuKindCount(rtuKindCount);
        voList.add(vo);

        vo = new RtuCountInfoVO();
        vo.setRtuCount(rtuCount);
        vo.setRtuKindName(RtuKindEnum.RTU_WATER_INTAKE.getName());
        dictInfoEntity = this.dictService.getDict("rtu_kind", RtuKindEnum.RTU_WATER_INTAKE.getCode());
        dto.setRtuKind(Func.toStr(dictInfoEntity.getId()));
        rtuKindCount = this.rtuBaseInfoService.rtuCount(dto);
        vo.setRtuKindCount(rtuKindCount);
        voList.add(vo);

        vo = new RtuCountInfoVO();
        vo.setRtuCount(rtuCount);
        vo.setRtuKindName(RtuKindEnum.RTU_WATER_OUTLET.getName());
        dictInfoEntity = this.dictService.getDict("rtu_kind", RtuKindEnum.RTU_WATER_OUTLET.getCode());
        dto.setRtuKind(Func.toStr(dictInfoEntity.getId()));
        rtuKindCount = this.rtuBaseInfoService.rtuCount(dto);
        vo.setRtuKindCount(rtuKindCount);
        voList.add(vo);


        return R.data(voList);
    }


    @GetMapping("/rtu/datagram/count/line")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<List<RtuCountInfoVO>> getRtuDatagramUpCountLine() {
        List<RtuCountInfoVO> voList = new ArrayList<>();


        LocalDateTime dt = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);
        LocalDateTime st = dt.minusHours(1);
        LocalDateTime et = st.plusMinutes(10);

        RtuDatagramInfoDTO dto = new RtuDatagramInfoDTO();


        //10
        dto.setBeginDate(Date.from(st.atZone(ZoneId.systemDefault()).toInstant()));
        dto.setEndDate(Date.from(et.atZone(ZoneId.systemDefault()).toInstant()));
        long count = this.datagramService.datagramCount(dto);

        RtuCountInfoVO vo = new RtuCountInfoVO();
        vo.setRtuDatagramUpCount(count);
        voList.add(vo);

        //20
        dto.setBeginDate(Date.from(et.atZone(ZoneId.systemDefault()).toInstant()));
        et = et.plusMinutes(10);
        dto.setEndDate(Date.from(et.atZone(ZoneId.systemDefault()).toInstant()));
        count = this.datagramService.datagramCount(dto);

        vo = new RtuCountInfoVO();
        vo.setRtuDatagramUpCount(count);
        voList.add(vo);

        //30
        dto.setBeginDate(Date.from(et.atZone(ZoneId.systemDefault()).toInstant()));
        et = et.plusMinutes(10);
        dto.setEndDate(Date.from(et.atZone(ZoneId.systemDefault()).toInstant()));
        count = this.datagramService.datagramCount(dto);

        vo = new RtuCountInfoVO();
        vo.setRtuDatagramUpCount(count);
        voList.add(vo);

        //40
        dto.setBeginDate(Date.from(et.atZone(ZoneId.systemDefault()).toInstant()));
        et = et.plusMinutes(10);
        dto.setEndDate(Date.from(et.atZone(ZoneId.systemDefault()).toInstant()));
        count = this.datagramService.datagramCount(dto);

        vo = new RtuCountInfoVO();
        vo.setRtuDatagramUpCount(count);
        voList.add(vo);

        //50
        dto.setBeginDate(Date.from(et.atZone(ZoneId.systemDefault()).toInstant()));
        et = et.plusMinutes(10);
        dto.setEndDate(Date.from(et.atZone(ZoneId.systemDefault()).toInstant()));
        count = this.datagramService.datagramCount(dto);

        vo = new RtuCountInfoVO();
        vo.setRtuDatagramUpCount(count);
        voList.add(vo);

        //60
        dto.setBeginDate(Date.from(et.atZone(ZoneId.systemDefault()).toInstant()));
        et = et.plusMinutes(10);
        dto.setEndDate(Date.from(et.atZone(ZoneId.systemDefault()).toInstant()));
        count = this.datagramService.datagramCount(dto);

        vo = new RtuCountInfoVO();
        vo.setRtuDatagramUpCount(count);

        voList.add(vo);


        return R.data(voList);
    }

    @GetMapping("/real/info")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<HomeInfoVO> detail() {
        //HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
        BladeUser user = AuthUtil.getUser();
        String adCode = null;
        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();
            }
        }
        if (null == adCode) {
            log.error("adcd is null********************");
            return R.status(true);
        }
        HomeInfoVO infoVO = new HomeInfoVO();
        //测站数量
        long rtuCount = rtuBaseInfoService.rtuCount(adCode);
        infoVO.setRtus(rtuCount);
        //预警中的测站数量
        RtuWarningInfoDTO warningInfoDTO = new RtuWarningInfoDTO();
        warningInfoDTO.setAdCode(adCode);
        long warningRtuCount = rtuWarningService.warningRtuCount(warningInfoDTO);
        infoVO.setWarningRtus(warningRtuCount);

        //任务总数
        CheckOrderInfoDTO checkOrderInfoDTO = new CheckOrderInfoDTO();
        checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
        checkOrderInfoDTO.setAdCode(adCode);
        long count = checkOrderService.orderCount(checkOrderInfoDTO);
        infoVO.setOrderCount(count);
        //未确认任务
        checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CREATE.getCode());
        count = checkOrderService.orderCount(checkOrderInfoDTO);
        infoVO.setUnconfirmOrderCount(count);

        checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CONFIRM.getCode());
        checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
        if (BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
            checkOrderInfoDTO.setContactUser(user.getUserId());
        }
        count = checkOrderService.orderCount(checkOrderInfoDTO);
        infoVO.setProcessCheckOrderCount(count);


        checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CREATE.getCode());
        checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
        checkOrderInfoDTO.setAdCode(adCode);
        List<CheckOrderInfoVO> orderList = checkOrderService.allOrders(checkOrderInfoDTO);
        if (null != orderList && orderList.size() > 0) {
            long delayTime = 120;
            LambdaQueryWrapper<CheckOrderSettingEntity> wrapper = Wrappers.<CheckOrderSettingEntity>query().lambda();
            wrapper.eq(CheckOrderSettingEntity::getIsDeleted, 0);
            List<CheckOrderSettingEntity> list = checkOrderSettingService.list(wrapper);
            if (null != list && list.size() > 0) {
                delayTime = list.get(0).getOrderComfireDelayTime();
            }
            long delayCount = 0;
            Date dt = new Date();
            for (CheckOrderInfoVO checkOrderInfoVO : orderList) {
                Duration duration = Func.between(checkOrderInfoVO.getCreateTime(), dt);
                long second = duration.getSeconds();
                if (second > 0) {
                    second = second / 60L;
                    if (second > delayTime) {
                        delayCount += 1;
                    }
                }
            }
            infoVO.setDelayComfireOrders(delayCount);
        } else {
            infoVO.setDelayComfireOrders(0L);
        }

        infoVO.setEquipmentInspectionCount(infoVO.getRtus());
        EquipmentInspectionReportDTO inspectionReportDTO = new EquipmentInspectionReportDTO();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        inspectionReportDTO.setInspectionYearStartDate(calendar.getTime());
        calendar.add(Calendar.YEAR, 1);
        inspectionReportDTO.setInspectionYearEndDate(calendar.getTime());
        inspectionReportDTO.setRainSeasonKind(1);
        inspectionReportDTO.setAdCode(adCode);
        count = inspectionReportService.equipmentInspectionReportCount(inspectionReportDTO);
        infoVO.setEquipmentInspectionBeforeRainReports(count);
        inspectionReportDTO.setRainSeasonKind(2);
        count = inspectionReportService.equipmentInspectionReportCount(inspectionReportDTO);
        infoVO.setEquipmentInspectionRainFirstReports(count);
        inspectionReportDTO.setRainSeasonKind(3);
        count = inspectionReportService.equipmentInspectionReportCount(inspectionReportDTO);
        infoVO.setEquipmentInspectionRainSecondReports(count);

        //处理汛期类别
        LambdaQueryWrapper<EquipmentInspectionPlanEntity> wrapper = Wrappers.<EquipmentInspectionPlanEntity>query().lambda();
        wrapper.eq(EquipmentInspectionPlanEntity::getIsDeleted, 0);
        wrapper.orderByDesc(EquipmentInspectionPlanEntity::getCreateTime);
        List<EquipmentInspectionPlanEntity> list = planService.list(wrapper);
        if (null != list && list.size() > 0) {
            EquipmentInspectionPlanEntity entity = list.get(0);
            Calendar dt = Calendar.getInstance();
            dt.setTime(new Date());
            Calendar calendarStart = Calendar.getInstance();
            Calendar calendarEnd = Calendar.getInstance();
            //汛前上报
            calendarStart.setTime(entity.getBeforeRainSeasonStartTime());
            calendarEnd.setTime(entity.getBeforeRainSeasonEndTime());
            if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
                infoVO.setEquipmentInspectionRainCount(infoVO.getEquipmentInspectionCount() - infoVO.getEquipmentInspectionBeforeRainReports());
            }

            //汛中第一次上报
            calendarStart.setTime(entity.getRainSeasonFirstStartTime());
            calendarEnd.setTime(entity.getRainSeasonFirstEndTime());
            if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
                infoVO.setEquipmentInspectionRainCount(infoVO.getEquipmentInspectionCount() - infoVO.getEquipmentInspectionRainFirstReports());
            }

            //汛中第二次上报
            calendarStart.setTime(entity.getRainSeasonSecondStartTime());
            calendarEnd.setTime(entity.getRainSeasonSecondEndTime());
            if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
                infoVO.setEquipmentInspectionRainCount(infoVO.getEquipmentInspectionCount() - infoVO.getEquipmentInspectionRainSecondReports());
            }
        } else {
            Calendar dt = Calendar.getInstance();
            dt.setTime(new Date());
            //汛前上报
            Calendar calendarStart = Calendar.getInstance();
            calendarStart.setTime(dt.getTime());
            calendarStart.set(Calendar.MONTH, 4 - 1);
            calendarStart.set(Calendar.DAY_OF_MONTH, 1);
            calendarStart.set(Calendar.HOUR_OF_DAY, 0);
            calendarStart.set(Calendar.MINUTE, 0);
            calendarStart.set(Calendar.SECOND, 0);
            Calendar calendarEnd = Calendar.getInstance();
            calendarEnd.setTime(dt.getTime());
            calendarEnd.set(Calendar.MONTH, 6 - 1);
            calendarEnd.set(Calendar.DAY_OF_MONTH, 1);
            calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
            calendarEnd.set(Calendar.MINUTE, 0);
            calendarEnd.set(Calendar.SECOND, 0);
            if (dt.after(calendarStart) && dt.before(calendarEnd)) {
                infoVO.setEquipmentInspectionRainCount(infoVO.getEquipmentInspectionCount() - infoVO.getEquipmentInspectionBeforeRainReports());
            }

            //汛中第一次上报
            calendarStart.setTime(dt.getTime());
            calendarStart.set(Calendar.MONTH, 6 - 1);
            calendarStart.set(Calendar.DAY_OF_MONTH, 1);
            calendarStart.set(Calendar.HOUR_OF_DAY, 0);
            calendarStart.set(Calendar.MINUTE, 0);
            calendarStart.set(Calendar.SECOND, 0);
            calendarEnd.setTime(dt.getTime());
            calendarEnd.set(Calendar.MONTH, 8 - 1);
            calendarEnd.set(Calendar.DAY_OF_MONTH, 15);
            calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
            calendarEnd.set(Calendar.MINUTE, 0);
            calendarEnd.set(Calendar.SECOND, 0);
            if (dt.after(calendarStart) && dt.before(calendarEnd)) {
                infoVO.setEquipmentInspectionRainCount(infoVO.getEquipmentInspectionCount() - infoVO.getEquipmentInspectionRainFirstReports());
            }

            //汛中第二次上报
            calendarStart.setTime(dt.getTime());
            calendarStart.set(Calendar.MONTH, 8 - 1);
            calendarStart.set(Calendar.DAY_OF_MONTH, 15);
            calendarStart.set(Calendar.HOUR_OF_DAY, 0);
            calendarStart.set(Calendar.MINUTE, 0);
            calendarStart.set(Calendar.SECOND, 0);
            calendarEnd.setTime(dt.getTime());
            calendarEnd.set(Calendar.MONTH, 10 - 1);
            calendarEnd.set(Calendar.DAY_OF_MONTH, 1);
            calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
            calendarEnd.set(Calendar.MINUTE, 0);
            calendarEnd.set(Calendar.SECOND, 0);
            if (dt.after(calendarStart) && dt.before(calendarEnd)) {
                infoVO.setEquipmentInspectionRainCount(infoVO.getEquipmentInspectionCount() - infoVO.getEquipmentInspectionRainSecondReports());
            }
        }
        LambdaQueryWrapper<NoticeViewEntity> noticewrapper = Wrappers.<NoticeViewEntity>query().lambda();
        noticewrapper.eq(NoticeViewEntity::getCreateUser, user.getUserId());
        noticewrapper.eq(NoticeViewEntity::getIsDeleted, 0);
        NoticeViewEntity entity = noticeViewService.getOne(noticewrapper);

        NoticeDTO noticeDTO = new NoticeDTO();
        if (null != entity) {
            noticeDTO.setViewDate(entity.getViewTime());
        }
        if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
            noticeDTO.setCategory(0);
        } else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName())) {
            //盟市查看本部门通知，以及自治区公开的通知
            noticeDTO.setCategory(1);
        } else if (BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
            //运维公司查看本部门通知，以及盟市公开的通知
            noticeDTO.setCategory(2);
        }
        count = noticeService.unviewNoticeCount(noticeDTO);
        if (count > 0) {
            infoVO.setHasNewNotice(true);
            infoVO.setNewNoticeCount(count);
        } else {
            infoVO.setHasNewNotice(false);
            infoVO.setNewNoticeCount(0L);
        }

        OrderProcessInfoDTO processInfoDTO = new OrderProcessInfoDTO();
        processInfoDTO.setAdCode(adCode);
        Date date = new Date();
        String dt = Func.formatDateTime(date);
        processInfoDTO.setCreateTime(Func.parse(dt, ConcurrentDateFormat.of("yyyy-MM-dd")));
        count = rtuOrderProcessService.todayProcessCount(processInfoDTO);
        infoVO.setTodayOrderProcessCount("" + count);

        inspectionReportDTO = new EquipmentInspectionReportDTO();
        inspectionReportDTO.setAdCode(adCode);
        date = new Date();
        dt = Func.formatDateTime(date);
        inspectionReportDTO.setCreateTime(Func.parse(dt, ConcurrentDateFormat.of("yyyy-MM-dd")));
        count = equipmentInspectionReportService.todayReportCount(inspectionReportDTO);
        infoVO.setTodayIinspectionReportCount("" + count);

        //我的任务
        LambdaQueryWrapper<CheckOrderInfoEntity> orderWrapper = Wrappers.<CheckOrderInfoEntity>query().lambda();
        orderWrapper.eq(CheckOrderInfoEntity::getContactUser, user.getUserId());
        orderWrapper.eq(CheckOrderInfoEntity::getOrderConfirm, OrderConfirmEnum.ACTIVE_CONFIRM.getCode());
        orderWrapper.eq(CheckOrderInfoEntity::getOrderClose, OrderCloseEnum.ACTIVE_OPEN.getCode());
        orderWrapper.eq(CheckOrderInfoEntity::getIsDeleted, 0);
        count = checkOrderService.count(orderWrapper);
        infoVO.setMyOrderCount(count);
        return R.data(infoVO);
    }

    @GetMapping("/rtu/status/count/info")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<RtuStatusCountInfoVO> rtuStatusCountInfo() {
        BladeUser user = AuthUtil.getUser();
        String adCode = null;
        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();
            }
        }
        if (null == adCode) {
            log.error("adcd is null********************");
            return R.status(false);
        }
        RtuStatusCountInfoVO infoVO = new RtuStatusCountInfoVO();
        //测站数量
        long count = rtuBaseInfoService.rtuCount(adCode);
        infoVO.setRtus(count);
        RtuWarningInfoDTO warningInfoDTO = new RtuWarningInfoDTO();
        warningInfoDTO.setAdCode(adCode);
        count = rtuWarningService.warningRtuCount(warningInfoDTO);
        infoVO.setWarningRtus(count);
        RtuWarningInfoDTO rtuWarningInfoDTO = new RtuWarningInfoDTO();
        rtuWarningInfoDTO.setAdCode(adCode);
        count = rtuWarningService.warningOfflineRtuCount(rtuWarningInfoDTO);
        infoVO.setOfflineRtus(count);

        return R.data(infoVO);
    }

    @GetMapping("/rtu/checkorder/count/info")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<RtuCheckOrderCountInfoVO> rtuCheckOrderCountInfo() {
        BladeUser user = AuthUtil.getUser();
        String adCode = null;
        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();
            }
        }
        if (null == adCode) {
            log.error("adcd is null********************");
            return R.status(true);
        }
        RtuCheckOrderCountInfoVO infoVO = new RtuCheckOrderCountInfoVO();


        //任务总数
        CheckOrderInfoDTO checkOrderInfoDTO = new CheckOrderInfoDTO();
        checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
        checkOrderInfoDTO.setAdCode(adCode);
        long count = checkOrderService.orderCount(checkOrderInfoDTO);
        infoVO.setOrderCount(count);
        //未确认任务
        checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CREATE.getCode());
        count = checkOrderService.orderCount(checkOrderInfoDTO);
        infoVO.setUnconfirmOrderCount(count);

        checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CONFIRM.getCode());
        checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
        if (BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
            checkOrderInfoDTO.setContactUser(user.getUserId());
        }
        count = checkOrderService.orderCount(checkOrderInfoDTO);
        infoVO.setProcessCheckOrderCount(count);

        checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CREATE.getCode());
        checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
        checkOrderInfoDTO.setAdCode(adCode);
        List<CheckOrderInfoVO> orderList = checkOrderService.allOrders(checkOrderInfoDTO);
        if (null != orderList && orderList.size() > 0) {
            long delayTime = 120;
            LambdaQueryWrapper<CheckOrderSettingEntity> wrapper = Wrappers.<CheckOrderSettingEntity>query().lambda();
            wrapper.eq(CheckOrderSettingEntity::getIsDeleted, 0);
            List<CheckOrderSettingEntity> list = checkOrderSettingService.list(wrapper);
            if (null != list && list.size() > 0) {
                delayTime = list.get(0).getOrderComfireDelayTime();
            }
            long delayCount = 0;
            Date dt = new Date();
            for (CheckOrderInfoVO checkOrderInfoVO : orderList) {
                Duration duration = Func.between(checkOrderInfoVO.getCreateTime(), dt);
                long second = duration.getSeconds();
                if (second > 0) {
                    second = second / 60L;
                    if (second > delayTime) {
                        delayCount += 1;
                    }
                }
            }
            infoVO.setDelayComfireOrders(delayCount);
        } else {
            infoVO.setDelayComfireOrders(0L);
        }

        //我的任务
        LambdaQueryWrapper<CheckOrderInfoEntity> orderWrapper = Wrappers.<CheckOrderInfoEntity>query().lambda();
        orderWrapper.eq(CheckOrderInfoEntity::getContactUser, user.getUserId());
        orderWrapper.eq(CheckOrderInfoEntity::getOrderConfirm, OrderConfirmEnum.ACTIVE_CONFIRM.getCode());
        orderWrapper.eq(CheckOrderInfoEntity::getOrderClose, OrderCloseEnum.ACTIVE_OPEN.getCode());
        orderWrapper.eq(CheckOrderInfoEntity::getIsDeleted, 0);
        count = checkOrderService.count(orderWrapper);
        infoVO.setMyOrderCount(count);
        return R.data(infoVO);
    }

    @GetMapping("/rtu/inspectionreport/count/info")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<RtuInspectionReportCountInfoVO> rtuInspectionReportCountInfo() {
        BladeUser user = AuthUtil.getUser();
        String adCode = null;
        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();
            }
        }
        if (null == adCode) {
            log.error("adcd is null********************");
            return R.status(true);
        }
        RtuInspectionReportCountInfoVO infoVO = new RtuInspectionReportCountInfoVO();
        long count = rtuBaseInfoService.rtuCount(adCode);
        infoVO.setEquipmentInspectionCount(count);
        EquipmentInspectionReportDTO inspectionReportDTO = new EquipmentInspectionReportDTO();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        inspectionReportDTO.setInspectionYearStartDate(calendar.getTime());
        calendar.add(Calendar.YEAR, 1);
        inspectionReportDTO.setInspectionYearEndDate(calendar.getTime());
        inspectionReportDTO.setRainSeasonKind(1);
        inspectionReportDTO.setAdCode(adCode);
        count = inspectionReportService.equipmentInspectionReportCount(inspectionReportDTO);
        infoVO.setEquipmentInspectionBeforeRainReports(count);
        inspectionReportDTO.setRainSeasonKind(2);
        count = inspectionReportService.equipmentInspectionReportCount(inspectionReportDTO);
        infoVO.setEquipmentInspectionRainFirstReports(count);
        inspectionReportDTO.setRainSeasonKind(3);
        count = inspectionReportService.equipmentInspectionReportCount(inspectionReportDTO);
        infoVO.setEquipmentInspectionRainSecondReports(count);

        //处理汛期类别
        LambdaQueryWrapper<EquipmentInspectionPlanEntity> wrapper = Wrappers.<EquipmentInspectionPlanEntity>query().lambda();
        wrapper.eq(EquipmentInspectionPlanEntity::getIsDeleted, 0);
        wrapper.orderByDesc(EquipmentInspectionPlanEntity::getCreateTime);
        List<EquipmentInspectionPlanEntity> list = planService.list(wrapper);
        if (null != list && list.size() > 0) {
            EquipmentInspectionPlanEntity entity = list.get(0);
            Calendar dt = Calendar.getInstance();
            dt.setTime(new Date());
            Calendar calendarStart = Calendar.getInstance();
            Calendar calendarEnd = Calendar.getInstance();
            //汛前上报
            calendarStart.setTime(entity.getBeforeRainSeasonStartTime());
            calendarEnd.setTime(entity.getBeforeRainSeasonEndTime());
            if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
                infoVO.setEquipmentInspectionRainCount(infoVO.getEquipmentInspectionCount() - infoVO.getEquipmentInspectionBeforeRainReports());
            }

            //汛中第一次上报
            calendarStart.setTime(entity.getRainSeasonFirstStartTime());
            calendarEnd.setTime(entity.getRainSeasonFirstEndTime());
            if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
                infoVO.setEquipmentInspectionRainCount(infoVO.getEquipmentInspectionCount() - infoVO.getEquipmentInspectionRainFirstReports());
            }

            //汛中第二次上报
            calendarStart.setTime(entity.getRainSeasonSecondStartTime());
            calendarEnd.setTime(entity.getRainSeasonSecondEndTime());
            if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
                infoVO.setEquipmentInspectionRainCount(infoVO.getEquipmentInspectionCount() - infoVO.getEquipmentInspectionRainSecondReports());
            }
        } else {
            Calendar dt = Calendar.getInstance();
            dt.setTime(new Date());
            //汛前上报
            Calendar calendarStart = Calendar.getInstance();
            calendarStart.setTime(dt.getTime());
            calendarStart.set(Calendar.MONTH, 4 - 1);
            calendarStart.set(Calendar.DAY_OF_MONTH, 1);
            calendarStart.set(Calendar.HOUR_OF_DAY, 0);
            calendarStart.set(Calendar.MINUTE, 0);
            calendarStart.set(Calendar.SECOND, 0);
            Calendar calendarEnd = Calendar.getInstance();
            calendarEnd.setTime(dt.getTime());
            calendarEnd.set(Calendar.MONTH, 6 - 1);
            calendarEnd.set(Calendar.DAY_OF_MONTH, 1);
            calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
            calendarEnd.set(Calendar.MINUTE, 0);
            calendarEnd.set(Calendar.SECOND, 0);
            if (dt.after(calendarStart) && dt.before(calendarEnd)) {
                infoVO.setEquipmentInspectionRainCount(infoVO.getEquipmentInspectionCount() - infoVO.getEquipmentInspectionBeforeRainReports());
            }

            //汛中第一次上报
            calendarStart.setTime(dt.getTime());
            calendarStart.set(Calendar.MONTH, 6 - 1);
            calendarStart.set(Calendar.DAY_OF_MONTH, 1);
            calendarStart.set(Calendar.HOUR_OF_DAY, 0);
            calendarStart.set(Calendar.MINUTE, 0);
            calendarStart.set(Calendar.SECOND, 0);
            calendarEnd.setTime(dt.getTime());
            calendarEnd.set(Calendar.MONTH, 8 - 1);
            calendarEnd.set(Calendar.DAY_OF_MONTH, 15);
            calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
            calendarEnd.set(Calendar.MINUTE, 0);
            calendarEnd.set(Calendar.SECOND, 0);
            if (dt.after(calendarStart) && dt.before(calendarEnd)) {
                infoVO.setEquipmentInspectionRainCount(infoVO.getEquipmentInspectionCount() - infoVO.getEquipmentInspectionRainFirstReports());
            }

            //汛中第二次上报
            calendarStart.setTime(dt.getTime());
            calendarStart.set(Calendar.MONTH, 8 - 1);
            calendarStart.set(Calendar.DAY_OF_MONTH, 15);
            calendarStart.set(Calendar.HOUR_OF_DAY, 0);
            calendarStart.set(Calendar.MINUTE, 0);
            calendarStart.set(Calendar.SECOND, 0);
            calendarEnd.setTime(dt.getTime());
            calendarEnd.set(Calendar.MONTH, 10 - 1);
            calendarEnd.set(Calendar.DAY_OF_MONTH, 1);
            calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
            calendarEnd.set(Calendar.MINUTE, 0);
            calendarEnd.set(Calendar.SECOND, 0);
            if (dt.after(calendarStart) && dt.before(calendarEnd)) {
                infoVO.setEquipmentInspectionRainCount(infoVO.getEquipmentInspectionCount() - infoVO.getEquipmentInspectionRainSecondReports());
            }
        }

        return R.data(infoVO);
    }


    @GetMapping("/rtu/up/count/info")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<RtuUpDataCountInfoVO> rtuUpCountInfo() {
        BladeUser user = AuthUtil.getUser();
        String adCode = null;
        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();
            }
        }
        if (null == adCode) {
            log.error("adcd is null********************");
            return R.status(false);
        }
        RtuUpDataCountInfoVO infoVO = new RtuUpDataCountInfoVO();
        long count = rtuBaseInfoService.rtuRainCount(adCode);
        infoVO.setRainRtuCount(count);
        count = rtuBaseInfoService.rtuRiverCount(adCode);
        infoVO.setRiverRtuCount(count);
        count = rtuBaseInfoService.rtuGroundCount(adCode);
        infoVO.setGroundRtuCount(count);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date endTime = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date beiganTime = calendar.getTime();
        //测站上报统计
        RtuDataRainDTO rainDTO = new RtuDataRainDTO();
        rainDTO.setAdCode(adCode);
        rainDTO.setBeginTime(beiganTime);
        rainDTO.setEndTime(endTime);
        count = rainService.rtuUpDataCount(rainDTO);
        infoVO.setRainUpDataCount(count);
        RtuDataRiverDTO riverDTO = new RtuDataRiverDTO();
        riverDTO.setAdCode(adCode);
        riverDTO.setBeginTime(beiganTime);
        riverDTO.setEndTime(endTime);
        count = riverService.rtuUpDataCount(riverDTO);
        infoVO.setRiverUpDataCount(count);
        RtuDataGroundDTO groundDTO = new RtuDataGroundDTO();
        groundDTO.setAdCode(adCode);
        groundDTO.setBeginTime(beiganTime);
        groundDTO.setEndTime(endTime);
        count = groundService.rtuUpDataCount(groundDTO);
        infoVO.setGroundUpDataCount(count);
        return R.data(infoVO);
    }

    @GetMapping("/warn/count/list")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "预警统计列表", notes = "")
    public R<List<WarnCountInfoVO>> warnCountInfoList() {
        BladeUser user = AuthUtil.getUser();
        if (!BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
            return R.data(null);
        }
        List<WarnCountInfoVO> list = new LinkedList<>();
//		//合计
//		WarnCountInfoVO sumVo = new WarnCountInfoVO();
//		sumVo.setDeptId(0L);
//		sumVo.setAdCity("合计");
//		//测站数量
//		Long count = rtuBaseInfoService.rtuCount(BusinessConstant.REGION_NM_ADCODE);
//		sumVo.setRtus(count.intValue());
//		//离线数量
//		RtuWarningInfoDTO dto = new RtuWarningInfoDTO();
//		dto.setAdCode(BusinessConstant.REGION_NM_ADCODE);
//		count = rtuWarningService.warningOfflineRtuCount(dto);
//		sumVo.setWarnOffline(count.intValue());
//		//小时漏报
//		count = rtuWarningService.warningHourMissOutRtuCount(dto);
//		sumVo.setWarnMissOut(count.intValue());
//		//5分钟延时
//		count = rtuWarningService.warningMinUpDelayRtuCount(dto);
//		sumVo.setWarnUpMinDelay(count.intValue());
//		//小时报延时
//		count = rtuWarningService.warningHourUpDelayRtuCount(dto);
//		sumVo.setWarnUpHourDelay(count.intValue());
//		list.add(sumVo);
        //盟市统计
        LambdaQueryWrapper<Dept> wrapper = Wrappers.<Dept>query().lambda();
        wrapper.eq(Dept::getIsDeleted, 0);
        wrapper.eq(Dept::getParentId, Func.toLong(user.getDeptId()));
        List<Dept> deptList = this.deptService.list(wrapper);
        for (Dept dept : deptList) {
            WarnCountInfoVO vo = new WarnCountInfoVO();
            //盟市名称
            vo.setDeptId(dept.getId());
            vo.setAdCity(dept.getDeptName());
            //测站数量
            LambdaQueryWrapper<DeptRegionEntity> deptLambdaQueryWrapper = Wrappers.<DeptRegionEntity>query().lambda();
            deptLambdaQueryWrapper.eq(DeptRegionEntity::getIsDeleted, 0);
            deptLambdaQueryWrapper.eq(DeptRegionEntity::getDeptId, dept.getId());
            DeptRegionEntity deptRegionEntity = deptRegionService.getOne(deptLambdaQueryWrapper);
            Long count = rtuBaseInfoService.rtuCount(deptRegionEntity.getAdcd());
            vo.setRtus(count.intValue());
            //离线数量
            RtuWarningInfoDTO dto = new RtuWarningInfoDTO();
            dto.setAdCode(deptRegionEntity.getAdcd());
            count = rtuWarningService.warningOfflineRtuCount(dto);
            vo.setWarnOffline(count.intValue());
            //小时漏报
            count = rtuWarningService.warningHourMissOutRtuCount(dto);
            vo.setWarnMissOut(count.intValue());
            //5分钟延时
            count = rtuWarningService.warningMinUpDelayRtuCount(dto);
            vo.setWarnUpMinDelay(count.intValue());
            //小时报延时
            count = rtuWarningService.warningHourUpDelayRtuCount(dto);
            vo.setWarnUpHourDelay(count.intValue());
            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;
        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();
            }
        }
        if (null == adCode) {
            log.error("adcd is null********************");
            return R.status(false);
        }
        WarnKindCountInfoVO infoVO = new WarnKindCountInfoVO();
        RtuWarningInfoDTO dto = new RtuWarningInfoDTO();
        dto.setAdCode(adCode);
        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());
        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);
    }
}
