package com.playlet.web.service.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.playlet.system.domain.PlayletRevenueRecord;
import com.playlet.system.domain.PlayletUserTask;
import com.playlet.system.pojo.vo.RevenueRecordStatisticsVO;
import com.playlet.system.service.IPlayletRevenueRecordService;
import com.playlet.web.service.app.PlayletRevenueRecordAppService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Date: 2024-03-18 15:52
 * @Author: 但星霖
 * @Version: v1.0
 * @Description: 短剧任务收益Service app接口实现
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PlayletRevenueRecordAppServiceImpl implements PlayletRevenueRecordAppService {

    private final IPlayletRevenueRecordService revenueRecordService;

    @Override
    public PageInfo<PlayletRevenueRecord> getItemPage(PlayletRevenueRecord record, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PlayletRevenueRecord> revenueRecordList = revenueRecordService.selectPlayletRevenueRecordList(record);
        return PageInfo.of(revenueRecordList);
    }

    @Override
    public PlayletRevenueRecord statisticsTime(PlayletRevenueRecord record) {
        PlayletRevenueRecord revenueRecord = new PlayletRevenueRecord();
        List<PlayletRevenueRecord> revenueRecordList = revenueRecordService.selectPlayletRevenueRecordList(record);
        if (CollectionUtils.isNotEmpty(revenueRecordList)) {
            long rechargeMoney = 0L;
            long refundMoney = 0L;
            long estimateEarnings = 0L;
            for (PlayletRevenueRecord revenue : revenueRecordList) {
                rechargeMoney = rechargeMoney + revenue.getRechargeMoney();
                refundMoney = refundMoney + revenue.getRefundMoney();
                estimateEarnings = estimateEarnings + revenue.getEstimateEarnings();
            }
            revenueRecord.setRechargeMoney(rechargeMoney);
            revenueRecord.setRefundMoney(refundMoney);
            revenueRecord.setEstimateEarnings(estimateEarnings);
        } else {
            revenueRecord.setRechargeMoney(0L);
            revenueRecord.setRefundMoney(0L);
            revenueRecord.setEstimateEarnings(0L);
        }
        return revenueRecord;
    }

    @Override
    public RevenueRecordStatisticsVO statistics(PlayletRevenueRecord record) {
        RevenueRecordStatisticsVO vo = new RevenueRecordStatisticsVO();
        // 总金额。
        List<PlayletRevenueRecord> revenueRecordListByTotal = revenueRecordService.selectPlayletRevenueRecordList(record);
        if (CollectionUtils.isNotEmpty(revenueRecordListByTotal)) {
            vo.setTotalMoney(revenueRecordListByTotal.stream().mapToLong(PlayletRevenueRecord::getPracticalEarnings).sum());
        }
        // 开始时间结束时间缺省值
        Date startTime = null;
        Date endTime = record.getStartTime();
        // 今日
        startTime = timeGain(null);
        timeSupplement(record, startTime, endTime);
        List<PlayletRevenueRecord> revenueRecordListByDay = revenueRecordService.selectPlayletRevenueRecordList(record);
        if (CollectionUtils.isNotEmpty(revenueRecordListByDay)) {
            vo.setTodayEstimate(revenueRecordListByDay.stream().mapToLong(PlayletRevenueRecord::getEstimateEarnings).sum());
        }
        // 七日
        startTime = timeGain(-7);
        timeSupplement(record, startTime, endTime);
        List<PlayletRevenueRecord> revenueRecordListBySevenDay = revenueRecordService.selectPlayletRevenueRecordList(record);
        if (CollectionUtils.isNotEmpty(revenueRecordListBySevenDay)) {
            vo.setSevenDaysEstimate(revenueRecordListBySevenDay.stream().mapToLong(PlayletRevenueRecord::getEstimateEarnings).sum());
        }
        // 昨日
        startTime = timeGain(-1);
        endTime = timeGain(null);
        timeSupplement(record, startTime, endTime);
        List<PlayletRevenueRecord> revenueRecordListByYesterday = revenueRecordService.selectPlayletRevenueRecordList(record);
        if (CollectionUtils.isNotEmpty(revenueRecordListByYesterday)) {
            vo.setSevenDaysEstimate(revenueRecordListByYesterday.stream().mapToLong(PlayletRevenueRecord::getEstimateEarnings).sum());
        }
        // 本月
        startTime = timeGain(1);
        timeSupplement(record, startTime, endTime);
        List<PlayletRevenueRecord> revenueRecordListByMoney = revenueRecordService.selectPlayletRevenueRecordList(record);
        if (CollectionUtils.isNotEmpty(revenueRecordListByMoney)) {
            vo.setSevenDaysEstimate(revenueRecordListByMoney.stream().mapToLong(PlayletRevenueRecord::getEstimateEarnings).sum());
        }
        return vo;
    }

    @Override
    public List<PlayletRevenueRecord> statisticsTimeType(PlayletRevenueRecord record) {
        List<PlayletRevenueRecord> listByReturn = new ArrayList<>();

        List<PlayletRevenueRecord> revenueRecordList = revenueRecordService.selectPlayletRevenueRecordList(record);
        if (CollectionUtils.isNotEmpty(revenueRecordList)) {
            Map<Long, List<PlayletRevenueRecord>> listMap = revenueRecordList.stream().collect(Collectors.groupingBy(PlayletRevenueRecord::getEarningsType));
            for (Long key : listMap.keySet()) {
                List<PlayletRevenueRecord> list = listMap.get(key);
                long rechargeMoney = 0L;
                long refundMoney = 0L;
                long estimateEarnings = 0L;
                for (PlayletRevenueRecord revenue : list) {
                    rechargeMoney = rechargeMoney + revenue.getRechargeMoney();
                    refundMoney = refundMoney + revenue.getRefundMoney();
                    estimateEarnings = estimateEarnings + revenue.getEstimateEarnings();
                }
                PlayletRevenueRecord revenueRecord = new PlayletRevenueRecord();
                revenueRecord.setEarningsType(key);
                revenueRecord.setRechargeMoney(rechargeMoney);
                revenueRecord.setRefundMoney(refundMoney);
                revenueRecord.setEstimateEarnings(estimateEarnings);
                listByReturn.add(revenueRecord);
            }
        }
        return listByReturn;
    }

    @Override
    public List<PlayletRevenueRecord> searchByTaskId(Long taskId) {
        return revenueRecordService.searchByTaskId(taskId);
    }

    // 时间更新
    private void timeSupplement(PlayletRevenueRecord record, Date startTime, Date endTime) {
        record.setStartTime(startTime);
        record.setEndTime(endTime);
    }

    // 获取时间
    private Date timeGain(Integer day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        if (ObjectUtils.isNotNull(day)) {
            calendar.set(Calendar.DATE, day);
        }
        return calendar.getTime();
    }
}
