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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.watermeter.custom.dto.AmountOrWaterVolumeInfoDto;
import com.tofly.watermeter.custom.entity.meterRead.InitCbByHb;
import com.tofly.watermeter.custom.entity.meterRead.readstatic.ReadCollect;
import com.tofly.watermeter.entity.Qfkhjbxx;
import com.tofly.watermeter.entity.Readmetereashis;
import com.tofly.watermeter.entity.Readmeterhis;
import com.tofly.watermeter.entity.Yhbingqy;
import com.tofly.watermeter.mapper.ReadmeterhisMapper;
import com.tofly.watermeter.service.ReadmeterhisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 抄表历史数据（有本系统自己管理的）
 *
 * @author wangjieliu
 * @date Wed Nov 10 00:00:00 CST 2021
 */
@Service("readmeterhisService")
public class ReadmeterhisServiceImpl extends ServiceImpl<ReadmeterhisMapper, Readmeterhis> implements ReadmeterhisService {

    @Override
    public List<Map> getStatisAudit(String yearMonth, String isGeneral) {
        if (StringUtil.isEmpty(yearMonth)) {
            throw new ToflyDeniedException("请选择月份");
        } else if (StringUtil.isEmpty(isGeneral)) {
            throw new ToflyDeniedException("请传递类型");
        }
        List<Readmeterhis> readmeterhisList = this.list(Wrappers.<Readmeterhis>lambdaQuery()
                .eq(Readmeterhis::getMonthPlan, yearMonth)
                .eq(Readmeterhis::getIsGeneral, isGeneral)
                .and(v -> v.eq(Readmeterhis::getShbz, "0").or().eq(Readmeterhis::getShbz, "1")
                        .or().eq(Readmeterhis::getShbz, "2").or().eq(Readmeterhis::getShbz, "3"))
                .select(Readmeterhis::getCbry, Readmeterhis::getCbqyBm,
                        Readmeterhis::getCbqyMc, Readmeterhis::getCbqydBm,
                        Readmeterhis::getShbz , Readmeterhis::getCbryName,
                        Readmeterhis::getQydmc));
        //对抄表人员分组
        Map<String, List<Readmeterhis>> groupByCbryMap = readmeterhisList.stream().collect(Collectors.groupingBy(Readmeterhis::getCbryName));
        List<Map> resultMapList = new ArrayList<>();
        //遍历抄表人员分组后的数据
        groupByCbryMap.forEach((k, v) -> {
            Map cbryMap = new HashMap(16);
            List<Map> qyList = new ArrayList<>();
            //抄表人员分组后，对区域分组数据
            Map<String, List<Readmeterhis>> groupByQyMap = v.stream().collect(Collectors.groupingBy(Readmeterhis::getCbqyMc));
            cbryMap.put("cbry", k);
            //计算区域异常总数
            groupByQyMap.forEach((kk, vv) -> {
                Map qyMap = new HashMap(16);
                qyMap.put("cbqy", kk);
                qyMap.put("qyNum", vv.size());
                //区域分组后，对区域段分组
                Map<String, List<Readmeterhis>> groupByQydMap = vv.stream().collect(Collectors.groupingBy(Readmeterhis::getQydmc));
                List<Map> qydList = new ArrayList<>();
                groupByQydMap.forEach((kkk, vvv) -> {
                    Map qydMap = new HashMap(16);
                    qydMap.put("cbqyd", kkk);
                    Map<String, List<Readmeterhis>> shbzMap = vvv.stream().collect(Collectors.groupingBy(Readmeterhis::getShbz));
                    if (StringUtil.isNotNull(shbzMap.get("0"))){
                        qydMap.put("noDisNum", shbzMap.get("0").size());
                    }else {
                        qydMap.put("noDisNum", 0);
                    }
                    if (StringUtil.isNotNull(shbzMap.get("1"))){
                        qydMap.put("disNum", shbzMap.get("1").size());
                    }else {
                        qydMap.put("disNum", 0);
                    }
                    qydList.add(qydMap);
                });
                //存放区域下的区域段集合
                qyMap.put("list", qydList);
                qyList.add(qyMap);
            });
            //存放抄表人员下的区域集合
            cbryMap.put("list", qyList);
            resultMapList.add(cbryMap);
        });
        return resultMapList;
    }

    @Override
    public List<Readmeterhis> getInitCbData(String qydbm , String isGeneral) {
        return baseMapper.getInitCbData(qydbm , isGeneral);
    }

    @Override
    public List<Readmeterhis> getInitCbDataList(List<String> qydbm, String isGeneral) {
        return baseMapper.getInitCbDataList(qydbm , isGeneral);
    }

    @Override
    public List<Readmeterhis> getInitCbDataCard(String qydbm) {
        return baseMapper.getInitCbDataCard(qydbm);
    }

    @Override
    public List<InitCbByHb> initCbDataByHb(String monthPlan) {
        return baseMapper.initCbDataByHb(monthPlan);
    }

    @Override
    public void initCbPlanHb(List<InitCbByHb> hbInfo , String monthPlan) {
        baseMapper.initCbPlanHb(hbInfo , monthPlan);
    }

    @Override
    public List<Readmeterhis> getInitFrontThreeData(Page page, String monthPlan) {
        return baseMapper.getInitFrontThreeData(page , monthPlan);
    }
    @Override
    public int getInitFrontThreeDataCount(String monthPlan) {
        return baseMapper.getInitFrontThreeDataCount(monthPlan);
    }

    @Override
    public void updateYearCbsl1(List qfcbqfmxl1,String yearMonth,String yearOnMonth1) {
        baseMapper.updateYearCbsl1(qfcbqfmxl1,yearMonth,yearOnMonth1);
    }

    @Override
    public void updateYearCbsl2(List qfcbqfmxl2, String yearMonth, String yearOnMonth2) {
        baseMapper.updateYearCbsl2(qfcbqfmxl2,yearMonth,yearOnMonth2);
    }

    @Override
    public void updateYearCbsl3(List qfcbqfmxl3, String yearMonth, String yearOnMonth3) {
        baseMapper.updateYearCbsl3(qfcbqfmxl3 , yearMonth, yearOnMonth3);
    }

    @Override
    public Long getMaxId() {
        return baseMapper.getMaxId();
    }

    @Override
    public void updateAuditCbData(List<Readmeterhis> readmeterhisList) {
        baseMapper.updateAuditCbData(readmeterhisList);
    }
    @Override
    public void updateAuditYcbData(List<Readmetereashis> readmeterhisList) {
        baseMapper.updateAuditYcbData(readmeterhisList);
    }

    @Override
    public String getMaxMonthPlan() {
        String monthPlan = this.getObj(new QueryWrapper<Readmeterhis>().select("MAX(MONTH_PLAN) as MONTH_PLAN").lambda()
                ,(o)->{return (String)o;} );
        return monthPlan;
    }

    @Override
    public void updateYhInfo(List<Qfkhjbxx> upts, String monthPlan) {
        if(CollUtil.isNotEmpty(upts)&& StringUtils.isNotEmpty(monthPlan)){
            this.baseMapper.updateReadmeterhisYhInfo(upts,monthPlan);
        }

    }

    @Override
    public void updateYhWaterVolumeInfos(List<AmountOrWaterVolumeInfoDto> upts, String monthPlan) {
        if(CollUtil.isNotEmpty(upts)&& StringUtils.isNotEmpty(monthPlan)){
            this.baseMapper.updateReadmeterhisYhWaterVolume(upts,monthPlan);
        }
    }

    @Override
    public void updateSoleSign(List<Readmeterhis> readmeterhisArrayList, String yearMonth) {
        baseMapper.updateSoleSign(readmeterhisArrayList , yearMonth);
    }

    @Override
    public List<ReadCollect> getOffline(String yearMonth, String isGeneral, List<String> cbryList, List<String> qybmList, List<String> qydbmList) {
        return baseMapper.getOffline(yearMonth , isGeneral , cbryList , qybmList , qydbmList);
    }

    @Override
    public boolean updateChildSort(List<Readmeterhis> readmeterhisList) {
        return baseMapper.updateChildSort(readmeterhisList);
    }

    @Override
    public boolean updateTaskCbxh(String qydbm, String monthPlan) {
        return baseMapper.updateTaskCbxh(qydbm , monthPlan);
    }

    /**
     * 查询水量与客户信息不一致的用户,并调整
     *
     * @return list
     */
    @Override
    public boolean handleAbnormalUser() {
        return baseMapper.handleAbnormalUser();
    }

    @Override
    public void initFrontThreeData(List<Readmeterhis> readmeterhisList , String monthPlan){
        baseMapper.initFrontThreeData(readmeterhisList , monthPlan);
    }

    @Override
    public void updateCbxh(List<Yhbingqy> resultYhList , String yearMonth , String qydbm) {
        baseMapper.updateCbxh(resultYhList,yearMonth,qydbm);
    }

    @Override
    public List<Yhbingqy> getNoSortYhList(String yearMonth, List<String> sortYhbhList, String qydbm) {
        return baseMapper.getNoSortYhList(yearMonth , sortYhbhList , qydbm);
    }


}
