package com.playlet.system.service.impl;

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

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.common.utils.DateUtils;
import com.playlet.system.pojo.vo.RevenueRecordStatisticsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.playlet.system.mapper.PlayletRevenueRecordMapper;
import com.playlet.system.domain.PlayletRevenueRecord;
import com.playlet.system.service.IPlayletRevenueRecordService;
import com.playlet.common.core.text.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Date: 2024-03-17
 * @Author: 但星霖
 * @Version: v1.0
 * @Description: 短剧任务收益Service业务层处理
 */
@Service
public class PlayletRevenueRecordServiceImpl extends ServiceImpl<PlayletRevenueRecordMapper, PlayletRevenueRecord> implements IPlayletRevenueRecordService {

    @Autowired
    private PlayletRevenueRecordMapper playletRevenueRecordMapper;

    /**
     * 查询短剧任务收益
     *
     * @param id 短剧任务收益主键
     * @return 短剧任务收益
     */
    @Override
    public PlayletRevenueRecord selectPlayletRevenueRecordById(Long id) {
        return playletRevenueRecordMapper.selectPlayletRevenueRecordById(id);
    }

    /**
     * 查询短剧任务收益列表
     *
     * @param playletRevenueRecord 短剧任务收益
     * @return 短剧任务收益
     */
    @Override
    public List<PlayletRevenueRecord> selectPlayletRevenueRecordList(PlayletRevenueRecord playletRevenueRecord) {
        return playletRevenueRecordMapper.selectPlayletRevenueRecordList(playletRevenueRecord);
    }

    /**
     * 新增短剧任务收益
     *
     * @param playletRevenueRecord 短剧任务收益
     * @return 结果
     */
    @Override
    public int insertPlayletRevenueRecord(PlayletRevenueRecord playletRevenueRecord) {
        playletRevenueRecord.setCreateTime(DateUtils.getNowDate());
        return playletRevenueRecordMapper.insertPlayletRevenueRecord(playletRevenueRecord);
    }

    /**
     * 修改短剧任务收益
     *
     * @param playletRevenueRecord 短剧任务收益
     * @return 结果
     */
    @Override
    public int updatePlayletRevenueRecord(PlayletRevenueRecord playletRevenueRecord) {
        playletRevenueRecord.setUpdateTime(DateUtils.getNowDate());
        return playletRevenueRecordMapper.updatePlayletRevenueRecord(playletRevenueRecord);
    }

    /**
     * 批量删除短剧任务收益
     *
     * @param ids 需要删除的短剧任务收益主键
     * @return 结果
     */
    @Override
    public int deletePlayletRevenueRecordByIds(String ids) {
        return playletRevenueRecordMapper.deletePlayletRevenueRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除短剧任务收益信息
     *
     * @param id 短剧任务收益主键
     * @return 结果
     */
    @Override
    public int deletePlayletRevenueRecordById(Long id) {
        return playletRevenueRecordMapper.deletePlayletRevenueRecordById(id);
    }

    @Override
    public PlayletRevenueRecord statisticsTime(PlayletRevenueRecord record) {
        PlayletRevenueRecord revenueRecord = new PlayletRevenueRecord();
        List<PlayletRevenueRecord> revenueRecordList = 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 = selectPlayletRevenueRecordList(record);
        if (CollectionUtils.isNotEmpty(revenueRecordListByTotal)) {
            vo.setTotalMoney(revenueRecordListByTotal.stream().mapToLong(PlayletRevenueRecord::getPracticalEarnings).sum());
        }
        // 开始时间结束时间缺省值
        Date endTime = record.getStartTime();
        Date startTime = null;
        // 今日
        startTime = timeGain(null);
        timeSupplement(record, startTime, endTime);
        List<PlayletRevenueRecord> revenueRecordListByDay = selectPlayletRevenueRecordList(record);
        if (CollectionUtils.isNotEmpty(revenueRecordListByDay)) {
            vo.setTodayEstimate(revenueRecordListByDay.stream().mapToLong(PlayletRevenueRecord::getEstimateEarnings).sum());
        }
        // 七日
        startTime = timeGain(-7);
        timeSupplement(record, startTime, endTime);
        List<PlayletRevenueRecord> revenueRecordListBySevenDay = 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 = selectPlayletRevenueRecordList(record);
        if (CollectionUtils.isNotEmpty(revenueRecordListByYesterday)) {
            vo.setSevenDaysEstimate(revenueRecordListByYesterday.stream().mapToLong(PlayletRevenueRecord::getEstimateEarnings).sum());
        }
        // 本月
        startTime = timeGain(1);
        timeSupplement(record, startTime, endTime);
        List<PlayletRevenueRecord> revenueRecordListByMoney = 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 = 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) {
        QueryWrapper<PlayletRevenueRecord> wrapper = new QueryWrapper<>();
        wrapper.lambda().select()
                .eq(PlayletRevenueRecord::getTaskId, taskId);
        return playletRevenueRecordMapper.selectList(wrapper);
    }

    // 时间更新
    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();
    }
}
