/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.xrtygis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.Date8Util;
import com.tofly.xrtygis.config.CallCenterFeign;
import com.tofly.xrtygis.em.Constant;
import com.tofly.xrtygis.entity.Warn;
import com.tofly.xrtygis.entity.dto.MeterReadingNumberDto;
import com.tofly.xrtygis.entity.dto.WarnInfoDto;
import com.tofly.xrtygis.entity.vo.*;
import com.tofly.xrtygis.mapper.WarnMapper;
import com.tofly.xrtygis.service.WarnService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

/**
 * 报警实时信息表
 *
 * @author admin
 * @date Wed Mar 10 00:00:00 CST 2021
 */
@Service("warnService")
@AllArgsConstructor
public class WarnServiceImpl extends ServiceImpl<WarnMapper, Warn> implements WarnService {


    private final WarnMapper warnMapper;

    private final CallCenterFeign callCenterFeign;

    @Override
    public Page<WarnInfoVo> getHomeAlarmInformation(Page page, WarnInfoDto dto) {
        QueryWrapper<WarnInfoDto> queryWrapper = new QueryWrapper<>();
        String warnType = dto.getWarnType();
        String isconfirm = dto.getIsconfirm();
        String isclear = dto.getIsclear();
        String stationId = dto.getStationId();
        Long rangeId = dto.getRangeId();
        Date startTime = dto.getStartTime();
        Date endTime = dto.getEndTime();
        if (!StringUtils.isEmpty(warnType)) {
            List<String> warnTypeIdList = Arrays.stream(warnType.split(Constant.CSV))
                    .collect(Collectors.toList());
            queryWrapper.in("P.WARN_TYPE", warnTypeIdList);
        }
        if (!StringUtils.isEmpty(isconfirm)) {
            queryWrapper.eq("P.ISCONFIRM", isconfirm);
        }
        if (!StringUtils.isEmpty(isclear)) {
            queryWrapper.eq("P.ISCLEAR", isclear);
        }
        if (null != rangeId) {
            queryWrapper.eq("B.RANGEID", rangeId);
        }
        if (null != startTime && null != endTime) {
            queryWrapper.ge("P.SCADA_TIME", startTime);
            queryWrapper.lt("P.SCADA_TIME", endTime);
        }
        if (!StringUtils.isEmpty(stationId)) {
            List<Long> stationIdList = Arrays.stream(stationId.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
            queryWrapper.in("B.stationId", stationIdList);
        }
        // 字典处理
        queryWrapper.eq("N.PCID", 845);
        return (Page<WarnInfoVo>) warnMapper.searchWeeklyPage(page, queryWrapper);
    }

    @Override
    public MeterReadingNumberVo getMeterReadingMonthNum(MeterReadingNumberDto dto) {
        QueryWrapper<MeterReadingNumberDto> queryWrapper = new QueryWrapper<>();
//        queryWrapper.ge("B.JHRQ", dto.getStartTime());
//        queryWrapper.le("B.JHRQ", dto.getEndTime());
        queryWrapper.eq("MONTH_PLAN", Date8Util.getYearMonth());
//        queryWrapper.eq("C.CBRY",dto.getUserId());
        return warnMapper.getMeterReadingMonthNum(queryWrapper);
    }

    @Override
    public RepairOrderNumberVo getRepairOrderNum(MeterReadingNumberDto dto) {
        QueryWrapper<MeterReadingNumberDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("DJSJ", dto.getStartTime());
        queryWrapper.le("DJSJ", dto.getEndTime());
        //驳回情况

//        queryWrapper.eq("DJRY",dto.getUserId());
        return warnMapper.getRepairOrderNum(queryWrapper);
    }

    @Override
    public WaterMeterNumberVo getWaterMeterNumberNum() {
        return warnMapper.getWaterMeterNumberNum();
    }

    @Override
    public List<WaterMeterPutStorageStatisticalVo> getWaterMeterPutStorageStatistical() {
        return warnMapper.getWaterMeterPutStorageStatistical();
    }

    @Override
    public List<WaterMeterInstallationStatisticalVo> getWaterMeterInstallationStatistical() {
        return warnMapper.getWaterMeterInstallationStatistical();
    }

    @Override
    public List<WaterMeterOutboundStatisticalVo> getWaterMeterOutboundStatistical() {
        return warnMapper.getWaterMeterOutboundStatistical();
    }

    @Override
    public List<WaterMeterOldTransferStatisticalVo> getWaterMeterOldTransferStatistical() {
        return warnMapper.getWaterMeterOldTransferStatistical();
    }

    @Override
    public List<WaterMeteFaultTableStatisticalVo> getWaterMeteFaultTableStatistical() {
        return warnMapper.getWaterMeteFaultTableStatistical();
    }

    @Override
    public List<WaterMeteWeeklyCheckStatisticalVo> getWaterMeteWeeklyCheckStatistical() {
        return warnMapper.getWaterMeteWeeklyCheckStatistical();
    }

    @Override
    public List<WaterMeterEnterpriseVo> getWaterMeterEnterprise() {
        return warnMapper.getWaterMeterEnterprise();
    }

    @Override
    public List<InspectionTypeStatisticalVo> getNowMonthInspectionStatistical(MeterReadingNumberDto dto) {
        QueryWrapper<MeterReadingNumberDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("PLAN_BEGINDATE", dto.getStartTime()).le("PLAN_ENDDATE", dto.getEndTime())
                .or().ge("PLAN_ENDDATE", dto.getStartTime()).le("PLAN_BEGINDATE", dto.getEndTime());

        List<InspectionTypeStatisticalVo> nowInspectionStatistical = warnMapper.getNowInspectionStatistical(queryWrapper);
        if (null == nowInspectionStatistical) {
            return new ArrayList<>();
        }
        return nowInspectionStatistical;
    }

    @Override
    public InspectionStatisticalVo getInspectionStatistical() {
        return warnMapper.getInspectionStatistical();
    }

    @Override
    public List<InspectionTypeStatisticalVo> getNowMonthInspectionDayStatistical(MeterReadingNumberDto dto) {
        QueryWrapper<MeterReadingNumberDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("PLAN_BEGINDATE", dto.getStartTime()).le("PLAN_ENDDATE", dto.getStartTime())
                .or().ge("PLAN_ENDDATE", dto.getStartTime()).le("PLAN_BEGINDATE", dto.getStartTime());
        List<InspectionTypeStatisticalVo> nowInspectionStatistical = warnMapper.getNowInspectionStatistical(queryWrapper);
        if (null == nowInspectionStatistical) {
            return new ArrayList<>();
        }
        return nowInspectionStatistical;
    }

    @Override
    public InspectionUserStateStatisticalVo getInspectionUserStateNum(MeterReadingNumberDto dto) {
        QueryWrapper<MeterReadingNumberDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("SIGN_TIME", dto.getStartTime());
        queryWrapper.le("SIGN_TIME", dto.getEndTime());
        return warnMapper.getInspectionUserStateNum(queryWrapper);
    }

    @Override
    public List<RepairOrderStatisticalVo> getRepairOrderStatistical(MeterReadingNumberDto dto) {

        return warnMapper.getRepairOrderStatistical(dto.getStartTime(), dto.getEndTime());
    }

    @Override
    public List<GroupAreaPipeLengthStatisticalVo> getGroupAreaPipeLengthStatistical() {
        return warnMapper.getGroupAreaPipeLengthStatistical();
    }

    @Override
    public List<PipeMaterialStatisticalVo> getPipeMaterialStatistical() {
        return warnMapper.getPipeMaterialStatistical();
    }

    @Override
    public List<PipeMaterialStatisticalVo> getPipeCaliberCensus() {
        List<PipeMaterialStatisticalVo> censusVos =  warnMapper.getPipeCaliberCensus();
//        for(int i = censusVos.size() - 1;i > -1;i--){
//            if(org.apache.commons.lang3.StringUtils.isBlank(censusVos.get(i).getBuryyear())){
//                continue;
//            }
//            String buryyear = censusVos.get(i).getBuryyear();
//            SimpleDateFormat sdfB = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
//            String year = null;
//            try {
//                year = sdf.format(sdfB.parse(buryyear));
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//            String thisYear = sdf.format(new Date());
//            int yearDistance = 0;
//            if(year != null){
//                yearDistance = Integer.parseInt(thisYear) - Integer.parseInt(year);
//            }
//            if(yearDistance < 5){
//                censusVos.remove(i);
//            }
//        }
        return censusVos;
    }

    @Override
    public List<QualityCertificationStatisticalVo> getQualityCertificationStatistical(MeterReadingNumberDto dto) {
        QueryWrapper<MeterReadingNumberDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("t.DISPATCH_TIME", dto.getStartTime());
        queryWrapper.le("t.DISPATCH_TIME", dto.getEndTime());
        return warnMapper.getQualityCertificationStatistical(queryWrapper);
    }

    @Override
    public QualityQualifiedTimeoutStatisticalVo getQualityQualifiedTimeoutStatistical(MeterReadingNumberDto dto) {
        QueryWrapper<MeterReadingNumberDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("DISPATCH_TIME", dto.getStartTime());
        queryWrapper.le("DISPATCH_TIME", dto.getEndTime());
        return warnMapper.getQualityQualifiedTimeoutStatistical(queryWrapper);
    }

    @Override
    public List<QualityBenefitStatisticalVo> getQualityBenefitStatistical(MeterReadingNumberDto dto) {
        QueryWrapper<MeterReadingNumberDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("rz.DISPATCH_TIME", dto.getStartTime());
        queryWrapper.le("rz.DISPATCH_TIME", dto.getEndTime());
        queryWrapper.eq("zg.VERIFY_STATUS", "4");
        return warnMapper.getQualityBenefitStatistical(queryWrapper);
    }

    @Override
    public HotlineInboundLossStatisticalVo getHotlineTrafficFlowStatistical(MeterReadingNumberDto dto) {
//        QueryWrapper<MeterReadingNumberDto> queryWrapper = new QueryWrapper<>();
//        queryWrapper.ge("CALL_TIME", dto.getStartTime());
//        queryWrapper.le("CALL_TIME", dto.getEndTime());
//        queryWrapper.eq("CALL_TYPE", "1");
//        return warnMapper.getHotlineTrafficFlowStatistical(queryWrapper);
        Map<String, BigDecimal> map = callCenterFeign.incomingCallCount();
        HotlineInboundLossStatisticalVo vo = new HotlineInboundLossStatisticalVo();
        if (map.size() != 0) {
            vo.setInboundLossNum(map.get("inboundlossnum")).setInboundNum(map.get("inboundnum")).setTotalInbound(map.get("totalinbound"));
        }
        return vo;
    }

    @Override
    public HotlineOutLossStatisticalVo getHotlineOutLossStatistical(MeterReadingNumberDto dto) {
//        QueryWrapper<MeterReadingNumberDto> queryWrapper = new QueryWrapper<>();
//        queryWrapper.ge("CALL_TIME", dto.getStartTime());
//        queryWrapper.le("CALL_TIME", dto.getEndTime());
//        queryWrapper.eq("CALL_TYPE", "2");
//        return warnMapper.getHotlineOutLossStatistical(queryWrapper);
        Map<String, BigDecimal> map = callCenterFeign.exhalationCount();
        HotlineOutLossStatisticalVo vo = new HotlineOutLossStatisticalVo();
        if (map.size() != 0) {
            vo.setOutLossNum(map.get("outlossnum")).setOutNum(map.get("outnum")).setTotalOut(map.get("totalout"));
        }
        return vo;
    }

    @Override
    public HotlineSatisfactionStatisticalVo getHotlineSatisfactionStatistical(MeterReadingNumberDto dto) {
        QueryWrapper<MeterReadingNumberDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("RETURN_TIME", dto.getStartTime());
        queryWrapper.le("RETURN_TIME", dto.getEndTime());
        return warnMapper.getHotlineSatisfactionStatistical(queryWrapper);
    }

    @Override
    public ProductionMarketingPoorStatisticalVo getProductionMarketingPoorStatistical(String startTime, String endTime) {
        return warnMapper.getProductionMarketingPoorStatistical(startTime, endTime);
    }

    @Override
    public List<ProductionMarketingPoorMonthVo> getProductionMarketingPoorMonth(String startTime, String endTime) {
        return warnMapper.getProductionMarketingPoorMonth(startTime, endTime);
    }

    @Override
    public LeakageStatisticalVo getLeakageStatistical(String startTime, String endTime) {
        return warnMapper.getLeakageStatistical(startTime, endTime);
    }

    @Override
    public List<LeakageMonthVo> getLeakageMonth(String startTime, String endTime) {
        return warnMapper.getLeakageMonthVo(startTime, endTime);
    }

    @Override
    public FeedSaleStatisticalVo getFeedSaleStatistical(String startTime, String endTime) {
        return warnMapper.getFeedSaleStatistical(startTime, endTime);
    }

    @Override
    public List<FeedSaleMonthVo> getFeedSaleMonth(String startTime, String endTime) {
        return warnMapper.getFeedSaleMonth(startTime, endTime);
    }

    @Override
    public UnitVariableCostStatisticalVo getUnitVariableCostStatistical(String year) {
        return warnMapper.getUnitVariableCostStatistical(year);
    }

    @Override
    public List<UnitVariableCostMonthVo> getUnitVariableCostMonth(String year) {
        return warnMapper.getUnitVariableCostMonth(year);
    }

    @Override
    public List<OneFactoryTakingWaterSupplyStatisticalVo> getOneFactoryTakingWaterSupplyStatistical(String year) {
        return warnMapper.getOneFactoryTakingWaterSupplyStatistical(year);
    }

    @Override
    public List<TwoFactoryTakingWaterSupplyStatisticalVo> getTwoFactoryTakingWaterSupplyStatistical(String year) {
        return warnMapper.getTwoFactoryTakingWaterSupplyStatistical(year);
    }

    @Override
    public List<OneFactoryWaterQualityYLStatisticalVo> getOneFactoryWaterQualityYLStatistical(Date startTime, Date endTime) {
        return warnMapper.getOneFactoryWaterQualityYLStatistical(startTime, endTime);
    }

    @Override
    public List<OneFactoryWaterQualityZDStatisticalVo> getOneFactoryWaterQualityZDStatistical(Date startTime, Date endTime) {
        return warnMapper.getOneFactoryWaterQualityZDStatistical(startTime, endTime);
    }

    @Override
    public List<TwoFactoryWaterQualityYLStatisticalVo> getTwoFactoryWaterQualityYLStatistical(Date startTime, Date endTime) {
        return warnMapper.getTwoFactoryWaterQualityYLStatistical(startTime, endTime);
    }

    @Override
    public List<TwoFactoryWaterQualityZDStatisticalVo> getTwoFactoryWaterQualityZDStatistical(Date startTime, Date endTime) {
        return warnMapper.getTwoFactoryWaterQualityZDStatistical(startTime, endTime);
    }

    @Override
    public List<OneFactoryWaterPressureStatisticalVo> getOneFactoryWaterPressureStatistical(Date startTime, Date endTime) {
        return warnMapper.getOneFactoryWaterPressureStatistical(startTime, endTime);
    }

    @Override
    public List<TwoFactoryWaterPressureStatisticalVo> getTwoFactoryWaterPressureStatistical(Date startTime, Date endTime) {
        return warnMapper.getTwoFactoryWaterPressureStatistical(startTime, endTime);
    }

    @Override
    public OneFactoryKeyIndexStatisticalVo getOneFactoryKeyIndexStatistical(Date startTime, Date endTime) {
        OneFactoryKeyIndexStatisticalVo oneFactoryKeyIndexStatistical = warnMapper.getOneFactoryKeyIndexStatistical(startTime, endTime);
        if (null != oneFactoryKeyIndexStatistical) {
            BigDecimal ccsy = warnMapper.getOneFactoryCCSY();
            BigDecimal yl = warnMapper.getOneFactoryYL();
            BigDecimal zd = warnMapper.getOneFactoryZD();
            oneFactoryKeyIndexStatistical.setSy(ccsy == null ? BigDecimal.ZERO : ccsy);
            oneFactoryKeyIndexStatistical.setYl(yl == null ? BigDecimal.ZERO : yl);
            oneFactoryKeyIndexStatistical.setZd(zd == null ? BigDecimal.ZERO : zd);
        }
        return oneFactoryKeyIndexStatistical;
    }

    @Override
    public TwoFactoryKeyIndexStatisticalVo getTwoFactoryKeyIndexStatistical(Date startTime, Date endTime) {
        TwoFactoryKeyIndexStatisticalVo twoFactoryKeyIndexStatistical = warnMapper.getTwoFactoryKeyIndexStatistical(startTime, endTime);
        TwoFactoryKeyIndexStatisticalVo twoFactoryCCSY = warnMapper.getTwoFactoryCCSY();

        if (null != twoFactoryKeyIndexStatistical) {
            BigDecimal yl = warnMapper.getTwoFactoryYL();
            BigDecimal cn = warnMapper.getTwoFactoryCn();
            BigDecimal zd = warnMapper.getTwoFactoryZD();
            twoFactoryKeyIndexStatistical.setYl(yl == null ? BigDecimal.ZERO : yl);
            twoFactoryKeyIndexStatistical.setZd(zd == null ? BigDecimal.ZERO : zd);
            twoFactoryKeyIndexStatistical.setCn(cn == null ? BigDecimal.ZERO : cn);
            if (null == twoFactoryCCSY) {
                twoFactoryKeyIndexStatistical.setSy1(BigDecimal.ZERO);
                twoFactoryKeyIndexStatistical.setSy2(BigDecimal.ZERO);
            } else {
                twoFactoryKeyIndexStatistical.setSy1(twoFactoryCCSY.getSy1() == null ? BigDecimal.ZERO : twoFactoryCCSY.getSy1());
                twoFactoryKeyIndexStatistical.setSy2(twoFactoryCCSY.getSy2() == null ? BigDecimal.ZERO : twoFactoryCCSY.getSy2());
            }
        }


        return twoFactoryKeyIndexStatistical;
    }

    @Override
    public IndividualRankingVo getIndividualRanking(Date startTime, Date endTime, String userName, Long userId) {
        //todo 这个地方用的是mysql 数据库是啥样都不知道就直接返回null了
        return null;

//        IndividualRankingVo rankingVo = new IndividualRankingVo();
//        BigDecimal zero1 = BigDecimal.ZERO;
//        Map<String, BigDecimal> userScoreInfo = callCenterFeign.getUserScoreInfo(startTime, endTime, userName);
//        if (null != userScoreInfo && userScoreInfo.size() != 0) {
//            BigDecimal favorableRate = userScoreInfo.get("favorablerate") == null ? BigDecimal.ZERO : userScoreInfo.get("favorablerate");
//            BigDecimal rownum = userScoreInfo.get("rownum") == null ? BigDecimal.ZERO : userScoreInfo.get("rownum");
//            rankingVo.setFavorableRate(favorableRate);
//            rankingVo.setIndividualRanking(rownum);
//        } else {
//            rankingVo.setFavorableRate(zero1);
//            rankingVo.setIndividualRanking(zero1);
//        }
//
//        Map<String, BigDecimal> callCount = callCenterFeign.getCallCount(userName);
//        if (null != callCount && callCount.size() != 0) {
//            rankingVo.setInboundNumber(callCount.get("inboundnumber"));
//            rankingVo.setCallSeveral(callCount.get("callseveral"));
//        } else {
//            rankingVo.setInboundNumber(zero1);
//            rankingVo.setCallSeveral(zero1);
//        }
//        IndividualRankingVo individualRankingThree = warnMapper.getIndividualRankingThree(startTime, endTime, userId);
//        if (null != individualRankingThree) {
//            rankingVo.setSendSingular(individualRankingThree.getSendSingular());
//            rankingVo.setNumberVisit(individualRankingThree.getNumberVisit());
//        } else {
//            rankingVo.setSendSingular(zero1);
//            rankingVo.setNumberVisit(zero1);
//        }
//
//        return rankingVo;


    }
}
