package com.tofly.xrtygis.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.xrtygis.entity.dto.AnalyzeDto;
import com.tofly.xrtygis.entity.vo.LeakageAnalysisViewVo;
import com.tofly.xrtygis.entity.vo.LeakageAnalysisVo;
import com.tofly.xrtygis.entity.vo.ScadaStatisticsAppVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

public interface AnalyzeMapper {

    @Select("SELECT n.leakName,n.loss,n.address,n.djsj findTime ,n.finishTime overTime,n.rzry leakUser,round((n.differenceTime/3600),2) differenceTimes,round(decode(n.differenceTime, 0, 0, n.loss/(n.differenceTime/3600)*100),2)  leakageRate  " +
            "FROM " +
            "(SELECT t.leakName,t.loss,t.address,t.djsj,t.finishTime,t.rzry,ROUND(TO_NUMBER(t.finishTime - t.djsj) * 24 * 60 * 60) differenceTime " +
            "FROM " +
            "(SELECT T1.LEAK_NAME leakName, nvl(T1.ESTIMATED_WATER_LOSS,0) loss,T2.ADDRESS address,T2.DJSJ djsj,T2.FINISH_TIME finishTime,T2.RZRY rzry " +
            "FROM TF_YWPN_LEAK_INFO_W T1 LEFT JOIN TF_YWPN_GDINFO_W T2 " +
            "ON T1.GDINFO_ID = T2.ID " +
            "${ew.customSqlSegment}  " +
            "order by T2.FINISH_TIME desc ) t)n")
    Page<LeakageAnalysisVo> getLeakageAnalysisVo(Page<LeakageAnalysisVo> page, @Param(Constants.WRAPPER) QueryWrapper<AnalyzeDto> queryWrapper);

    @Select("SELECT to_char(n.finishTime,'yyyy-mm') month ,round(decode(sum(n.differenceTime), 0, 0, sum(n.loss)/(sum(n.differenceTime)/3600)*100),2)  leakageRate " +
            "FROM " +
            "(SELECT t.loss,t.finishTime,ROUND(TO_NUMBER(t.finishTime - t.djsj) * 24 * 60 * 60) differenceTime " +
            "FROM " +
            "(SELECT nvl(T1.ESTIMATED_WATER_LOSS,0) loss,T2.DJSJ djsj,T2.FINISH_TIME finishTime " +
            "FROM TF_YWPN_LEAK_INFO_W T1 LEFT JOIN TF_YWPN_GDINFO_W T2 " +
            "ON T1.GDINFO_ID = T2.ID " +
            "${ew.customSqlSegment}  " +
            ") t " +
            ")n " +
            "GROUP BY to_char(n.finishTime,'yyyy-mm')")
    List<LeakageAnalysisViewVo> getLeakageAnalysisViewVo(@Param(Constants.WRAPPER) QueryWrapper<AnalyzeDto> queryWrapper);

    @Select("SELECT " +
            "(SELECT count(1)  " +
            "FROM TF_WATER_INFO ) waterPlantNum, " +
            "(SELECT sum(DESIGN_PROCESSING_CAPACITY)  " +
            "FROM TF_WATER_INFO ) waterSupplyCapacity, " +
            "(SELECT count(1)  " +
            "FROM TF_YWPN_COSTPRESTATION_W) pressurizationStationNum, " +
            "(SELECT count(1)  " +
            "FROM TF_YWPN_WORKRATOITEM_W " +
            "WHERE type = 0) waterStation, " +
            "(SELECT sum((case DEVICE_TYPE when '1' then 1 else 0 end))   " +
            "FROM " +
            "TF_DEVICE_ARCHIVE_MANAGE) pressureMonitoringNum, " +
            "(SELECT sum((case DEVICE_TYPE when '3' then 1 else 0 end))   " +
            "FROM " +
            "TF_DEVICE_ARCHIVE_MANAGE) flowMonitoringNum " +
            "FROM " +
            "dual ")
    ScadaStatisticsAppVo getScadaStatisticsApp();
}
