package com.zcxk.rmcp.web.service;

import com.github.pagehelper.PageHelper;
import com.zcxk.core.common.pojo.UserCondition;
import com.zcxk.core.mysql.pageing.Pagination;
import com.zcxk.core.oauth2.util.UserUtil;
import com.zcxk.core.utils.BigDecimalUtils;
import com.zcxk.rmcp.api.dto.readrate.ReadRateCountDto;
import com.zcxk.rmcp.api.dto.readrate.ReadRateDto;
import com.zcxk.rmcp.api.vo.ReadRatePageVo;
import com.zcxk.rmcp.api.vo.ReadRateVo;
import com.zcxk.rmcp.api.vo.StatisticsReadRateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author Andy
 * @version V1.0
 * @description: 报表率
 * @date 2021/7/26
 **/
public abstract class AbstractReadRateService implements ReadRateService{


    @Override
    public List<StatisticsReadRateVo> statReadRateGroupCategory(){
        List<StatisticsReadRateVo> list = this.statReadRateGroupCategory(UserUtil.getCurrentUser().getUserCondition());
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        int readTimes  = 0;
        int realReadTimes = 0;
        int unReadTimes = 0;
        StatisticsReadRateVo allStatisticsReadRateVo = new StatisticsReadRateVo();
        for (StatisticsReadRateVo statisticsReadRateVo : list) {
            readTimes += statisticsReadRateVo.getReadTimes();
            realReadTimes += statisticsReadRateVo.getRealReadTimes();
            unReadTimes += statisticsReadRateVo.getUnReadTimes();
        }
        allStatisticsReadRateVo.setName("所有水表");
        allStatisticsReadRateVo.setReadTimes(readTimes);
        allStatisticsReadRateVo.setRealReadTimes(realReadTimes);
        allStatisticsReadRateVo.setUnReadTimes(unReadTimes);
        allStatisticsReadRateVo.setReadRate(BigDecimalUtils.divide(realReadTimes, readTimes, 4).stripTrailingZeros().toPlainString());
        list.add(0, allStatisticsReadRateVo);
        return list;
    }

    @Override
    public Pagination<ReadRateVo> pageReadRate(ReadRateDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        UserCondition userCondition = UserUtil.getCurrentUser().getUserCondition();
        List<ReadRateVo> list = this.listReadRate(dto, userCondition);
        if (CollectionUtils.isEmpty(list)){
            return new Pagination<>();
        }
        return new Pagination<>(list);
    }

    @Override
    public List<ReadRateVo> listReadRate(ReadRateDto dto){
        return this.listReadRate(dto, UserUtil.getCurrentUser().getUserCondition());
    }

    @Override
    public StatisticsReadRateVo countReadRate(ReadRateCountDto dto){
        ReadRateDto rateDto = new ReadRateDto();
        BeanUtils.copyProperties(dto, rateDto, ReadRateDto.class);
        return this.countReadRate(rateDto, UserUtil.getCurrentUser().getUserCondition());
    }

    /**
    * @author Andy
    * @description 统计抄表率
    * @date 11:31 2021/7/26
    * @param
    * @return java.util.List<com.zcxk.rmcp.api.vo.StatisticsReadRateVo>
    **/
    public abstract List<StatisticsReadRateVo> statReadRateGroupCategory(UserCondition userCondition);

    /**
     * @author Andy
     * @description 统计抄表率
     * @date 11:31 2021/7/26
     * @param
     * @return java.util.List<com.zcxk.rmcp.api.vo.StatisticsReadRateVo>
     **/
    public abstract List<ReadRateVo> listReadRate(ReadRateDto dto, UserCondition userCondition);

    /**
     * @author Andy
     * @description 统计抄表总数
     * @date 11:31 2021/7/26
     * @param
     * @return java.util.List<com.zcxk.rmcp.api.vo.StatisticsReadRateVo>
     **/
    public abstract StatisticsReadRateVo countReadRate(ReadRateDto dto, UserCondition userCondition);

}
