package com.fs.statis.service.impl;

import com.fs.company.cache.ICompanyCacheService;
import com.fs.statis.dto.*;
import com.fs.statis.mapper.ConsumptionBalanceMapper;
import com.fs.statis.service.IStatisticsService;
import com.fs.statis.service.utils.TrendDataFiller;
import com.fs.store.service.cache.IFsUserCourseCacheService;
import com.hc.openapi.tool.util.ObjectUtils;
import com.hc.openapi.tool.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.List;

@Service
public class StatisticsServiceImpl implements IStatisticsService {
    @Autowired
    private ConsumptionBalanceMapper consumptionBalanceMapper;

    @Autowired
    private IFsUserCourseCacheService fsUserCourseCacheService;

    @Autowired
    private ICompanyCacheService companyCacheService;
    @Override
    public DealerAggregatedDTO dealerAggregated() {
        return consumptionBalanceMapper.dealerAggregated();
    }

    @Override
    public ConsumptionBalanceDataDTO rechargeConsumption() {
        return consumptionBalanceMapper.rechargeConsumption();
    }

    @Override
    public AnalysisPreviewDTO analysisPreview(AnalysisPreviewQueryDTO param) {
        AnalysisPreviewDTO dto = new AnalysisPreviewDTO();

        Long watchUserCount = consumptionBalanceMapper.queryWatchUserCount(param);
        Long completedUserCount = consumptionBalanceMapper.queryCompletedUserCount(param);

        if(watchUserCount == null){
            watchUserCount = 0L;
        }
        if(completedUserCount == null){
            completedUserCount = 0L;
        }

        // 观看人数
        dto.setWatchUserCount(watchUserCount);
        // 完播人数
        dto.setCompletedUserCount(completedUserCount);

        // 完播率
        if(!ObjectUtils.equals(watchUserCount,0L)){
            BigDecimal multiply = (BigDecimal.valueOf(completedUserCount)
                    .divide(BigDecimal.valueOf(watchUserCount),4,BigDecimal.ROUND_HALF_UP))
                    .multiply(BigDecimal.valueOf(100));

            dto.setCompletedRate(multiply.setScale(2,BigDecimal.ROUND_HALF_UP).toPlainString());
        } else {
            dto.setCompletedRate("0");
        }

        Long watchCount = consumptionBalanceMapper.queryWatchCount(param);
        Long completedCount = consumptionBalanceMapper.queryCompletedCount(param);

        if(watchCount == 0){
            watchCount = 0L;
        }
        if(completedCount == 0){
            completedCount = 0L;
        }

        // 观看次数
        dto.setWatchCount(watchCount);
        // 完播次数
        dto.setCompletedCount(completedCount);
        // 视频完播率
        if(!ObjectUtils.equals(watchCount, 0L)){
            BigDecimal multiply = BigDecimal.valueOf(completedCount)
                    .divide(BigDecimal.valueOf(watchCount),4,BigDecimal.ROUND_HALF_UP)
                    .multiply(BigDecimal.valueOf(100));

            dto.setCompletedRate(multiply.setScale(2,BigDecimal.ROUND_HALF_UP).toPlainString());
        } else {
            dto.setCompletedRate("0");
        }

        Long answerMemberCount = consumptionBalanceMapper.queryAnswerMemberCount(param);
        Long correctUserCount = consumptionBalanceMapper.queryCorrectUserCount(param);

        if(answerMemberCount == 0){
            answerMemberCount = 0L;
        }
        if(correctUserCount == 0){
            correctUserCount = 0L;
        }
        // 答题人数
        dto.setAnswerMemberCount(answerMemberCount);
        // 正确人数
        dto.setCorrectUserCount(correctUserCount);
        // 正确比例
        if(!ObjectUtils.equals(answerMemberCount, 0L)){
            BigDecimal multiply = BigDecimal.valueOf(correctUserCount)
                    .divide(BigDecimal.valueOf(answerMemberCount),4,BigDecimal.ROUND_HALF_UP)
                    .multiply(BigDecimal.valueOf(100));

            dto.setCorrectRate(multiply.setScale(2, RoundingMode.HALF_UP).toPlainString());
        } else {
            dto.setCorrectRate("0");
        }
        Long rewardCount = consumptionBalanceMapper.queryRewardCount(param);
        if(rewardCount == null) {
            rewardCount = 0L;
        }
        BigDecimal rewardMoney = consumptionBalanceMapper.queryRewardMoney(param);
        if(rewardMoney == null) {
            rewardMoney = BigDecimal.ZERO;
        }
        // 答题红包个数
        dto.setRewardCount(rewardCount);
        // 答题红包金额
        dto.setRewardMoney(rewardMoney);

        return dto;
    }

    @Override
    public Long smsBalance() {
        return consumptionBalanceMapper.smsBalance();
    }

    @Override
    public AuthorizationInfoDTO authorizationInfo() {
        return consumptionBalanceMapper.authorizationInfo();
    }

    @Override
    public List<WatchEndPlayTrendDTO> watchEndPlayTrend(AnalysisPreviewQueryDTO param) {
        List<WatchEndPlayTrendDTO> watchEndPlayTrendDTOS = consumptionBalanceMapper.watchEndPlayTrend(param);
        // 今日，昨日 格式为24小时
        if(ObjectUtils.equals(param.getType(),0) || ObjectUtils.equals(param.getType(),1)){
            watchEndPlayTrendDTOS = TrendDataFiller.fillHourData(watchEndPlayTrendDTOS);
            // 否则都是按天为维度的时间范围
        } else {
            watchEndPlayTrendDTOS = TrendDataFiller.fillDateSegmentData(watchEndPlayTrendDTOS, param.getStartTime(), param.getEndTime());
        }

        return watchEndPlayTrendDTOS;
    }

    @Override
    public List<DeaMemberTopTenDTO> deaMemberTopTen(AnalysisPreviewQueryDTO param) {
        List<DeaMemberTopTenDTO> deaMemberTopTenDTOS = consumptionBalanceMapper.deaMemberTopTen(param);
        for (DeaMemberTopTenDTO dto : deaMemberTopTenDTOS) {
            Long companyId = dto.getCompanyId();
            String companyName = companyCacheService.selectCompanyNameById(companyId);
            if(StringUtils.isNotBlank(companyName)){
                dto.setCompanyName(companyName);
            }
        }
        return deaMemberTopTenDTOS;
    }

    @Override
    public List<CourseStatsDTO> watchCourseTopTen(AnalysisPreviewQueryDTO param) {
        List<CourseStatsDTO> courseStatsDTOS = consumptionBalanceMapper.watchCourseTopTen(param);
        for (CourseStatsDTO courseStatsDTO : courseStatsDTOS) {
            String courseName = fsUserCourseCacheService.selectCourseNameByCourseId(courseStatsDTO.getCourseId());
            if(StringUtils.isNotBlank(courseName)){
                courseStatsDTO.setCourseName(courseName);
            }
        }
        return courseStatsDTOS;
    }

    @Override
    public List<RewardMoneyTopTenDTO> rewardMoneyTopTen(AnalysisPreviewQueryDTO param) {
        List<RewardMoneyTopTenDTO> rewardMoneyTopTenDTOS = consumptionBalanceMapper.rewardMoneyTopTen(param);
        return rewardMoneyTopTenDTOS;
    }

    @Override
    public List<RewardMoneyTrendDTO> rewardMoneyTrendDTO(AnalysisPreviewQueryDTO param) {
        List<RewardMoneyTrendDTO> rewardMoneyTrendDTOS = consumptionBalanceMapper.rewardMoneyTrendDTO(param);
        return rewardMoneyTrendDTOS;
    }
}
