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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.Date8Util;
import com.tofly.common.core.util.DateUtil;
import com.tofly.common.core.util.StringUtil;
import com.tofly.watermeter.custom.entity.meterRead.QydWater;
import com.tofly.watermeter.entity.Cbqydwater;
import com.tofly.watermeter.mapper.CbqydwaterMapper;
import com.tofly.watermeter.mapper.ReadmeterhisMapper;
import com.tofly.watermeter.service.CbqydwaterService;
import com.tofly.watermeter.service.ReaduserwaterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 区域段抄表水量汇总
 *
 * @author wangjieliu
 * @date Wed Dec 08 00:00:00 CST 2021
 */
@Service("cbqydwaterService")
public class CbqydwaterServiceImpl extends ServiceImpl<CbqydwaterMapper, Cbqydwater> implements CbqydwaterService {

    @Autowired
    private ReadmeterhisMapper readmeterhisMapper;
    @Autowired
    private ReaduserwaterService readuserwaterService;

    /**
     * 每日同步区域段用水量
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void waterAdding() {
        String currentDay = DateUtil.parseDateToStr("yyyyMMdd" , Date8Util.getCurrentDate());
        String yearMonth = Date8Util.getYearMonth();
        //查询今天抄表水量汇总数据
        List<QydWater> qydWaterList = readmeterhisMapper.getWaterByQyd(currentDay);
        if (StringUtil.isNotNull(qydWaterList) && qydWaterList.size() > 0){
            //一天的区域段目前估计不超过十个
            List<String> qydbmList= qydWaterList.stream().map(QydWater::getQydbm).collect(Collectors.toList());
            //得到当月已经保存了的汇总水量
            List<Cbqydwater> cbqydwaterList = this.list(Wrappers.<Cbqydwater>lambdaQuery().in(Cbqydwater::getCbqydBm , qydbmList)
                    .eq(Cbqydwater::getYearmonth , yearMonth));
            //过滤得到当月第一次保存汇总水量的区段信息  需注意的是  有可能存在更换了区域和区域段绑定关系的情况
            List<QydWater> firstQydList = qydWaterList.stream().filter(currentQqd -> cbqydwaterList.stream()
                    .noneMatch(existQyd -> currentQqd.getQydbm().equals(existQyd.getCbqydBm())&&currentQqd.getQybm().equals(existQyd.getCbqyBm())))
                    .collect(Collectors.toList());
            if (firstQydList.size() > 0){
                List<Cbqydwater> cbqydwaters = new ArrayList<>();
                for (QydWater qydWater : firstQydList) {
                    Cbqydwater cbqydwater = new Cbqydwater();
                    cbqydwater.setCbqydBm(qydWater.getQydbm());
                    cbqydwater.setQydmc(qydWater.getQydmc());
                    cbqydwater.setCbqyMc(qydWater.getQymc());
                    cbqydwater.setCbqyBm(qydWater.getQybm());
                    cbqydwater.setWaterValue(qydWater.getAddValue());
                    cbqydwater.setAddDay(currentDay);
                    cbqydwater.setYearmonth(yearMonth);
                    cbqydwaters.add(cbqydwater);
                }
                this.saveBatch(cbqydwaters);
            }
            //过滤得到当月已经保存的汇总水量
            List<QydWater> existQydList = qydWaterList.stream().filter(currentQqd -> firstQydList.stream()
                    .noneMatch(firstQyd -> currentQqd.getQydbm().equals(firstQyd.getQydbm()))).collect(Collectors.toList());
            if (existQydList.size() > 0){
                Map<String , Cbqydwater> cbqydwaterMap = cbqydwaterList.stream().collect(Collectors.toMap(Cbqydwater::getCbqydBm , v -> v));
                for (QydWater qydWater : existQydList) {
                    String qydbm = qydWater.getQydbm();
                    //保存了的汇总水量
                    Cbqydwater cbqydwater = cbqydwaterMap.get(qydbm);
                    //判断当天是否已经更新过，不能再次更新水量汇总
                    if (!currentDay.equals(cbqydwater.getAddDay())){
                        //水量相加
                        Long resultValue = qydWater.getAddValue() + cbqydwater.getWaterValue();
                        this.update(Wrappers.<Cbqydwater>lambdaUpdate().eq(Cbqydwater::getCbqydBm , qydbm).eq(Cbqydwater::getYearmonth , yearMonth)
                                .set(Cbqydwater::getWaterValue , resultValue).set(Cbqydwater::getAddDay , currentDay));
                    }
                }
            }
        }
    }

    /**
     * 每日同步用户累计用水量
     */
    public void userWaterAdding() {
        String currentDay = DateUtil.parseDateToStr("yyyyMMdd" , Date8Util.getCurrentDate());
        String yearMonth = Date8Util.getYearMonth();
        //查询今天抄表水量汇总数据
        List<QydWater> qydWaterList = readmeterhisMapper.getWaterByQyd(currentDay);
        if (StringUtil.isNotNull(qydWaterList) && qydWaterList.size() > 0){
            //一天的区域段目前估计不超过十个
            List<String> qydbmList= qydWaterList.stream().map(QydWater::getQydbm).collect(Collectors.toList());
            //得到当月已经保存了的汇总水量
            List<Cbqydwater> cbqydwaterList = this.list(Wrappers.<Cbqydwater>lambdaQuery().in(Cbqydwater::getCbqydBm , qydbmList)
                    .eq(Cbqydwater::getYearmonth , yearMonth));
            //过滤得到当月第一次保存汇总水量的区段信息  需注意的是  有可能存在更换了区域和区域段绑定关系的情况
            List<QydWater> firstQydList = qydWaterList.stream().filter(currentQqd -> cbqydwaterList.stream()
                    .noneMatch(existQyd -> currentQqd.getQydbm().equals(existQyd.getCbqydBm())&&currentQqd.getQybm().equals(existQyd.getCbqyBm())))
                    .collect(Collectors.toList());
            if (firstQydList.size() > 0){
                List<Cbqydwater> cbqydwaters = new ArrayList<>();
                for (QydWater qydWater : firstQydList) {
                    Cbqydwater cbqydwater = new Cbqydwater();
                    cbqydwater.setCbqydBm(qydWater.getQydbm());
                    cbqydwater.setQydmc(qydWater.getQydmc());
                    cbqydwater.setCbqyMc(qydWater.getQymc());
                    cbqydwater.setCbqyBm(qydWater.getQybm());
                    cbqydwater.setWaterValue(qydWater.getAddValue());
                    cbqydwater.setAddDay(currentDay);
                    cbqydwater.setYearmonth(yearMonth);
                    cbqydwaters.add(cbqydwater);
                }
                this.saveBatch(cbqydwaters);
            }
            //过滤得到当月已经保存的汇总水量
            List<QydWater> existQydList = qydWaterList.stream().filter(currentQqd -> firstQydList.stream()
                    .noneMatch(firstQyd -> currentQqd.getQydbm().equals(firstQyd.getQydbm()))).collect(Collectors.toList());
            if (existQydList.size() > 0){
                Map<String , Cbqydwater> cbqydwaterMap = cbqydwaterList.stream().collect(Collectors.toMap(Cbqydwater::getCbqydBm , v -> v));
                for (QydWater qydWater : existQydList) {
                    String qydbm = qydWater.getQydbm();
                    //保存了的汇总水量
                    Cbqydwater cbqydwater = cbqydwaterMap.get(qydbm);
                    //判断当天是否已经更新过，不能再次更新水量汇总
                    if (!currentDay.equals(cbqydwater.getAddDay())){
                        //水量相加
                        Long resultValue = qydWater.getAddValue() + cbqydwater.getWaterValue();
                        this.update(Wrappers.<Cbqydwater>lambdaUpdate().eq(Cbqydwater::getCbqydBm , qydbm).eq(Cbqydwater::getYearmonth , yearMonth)
                                .set(Cbqydwater::getWaterValue , resultValue).set(Cbqydwater::getAddDay , currentDay));
                    }
                }
            }
        }
    }






}
