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

import cn.hutool.core.date.DateUtil;
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.exception.ServiceException;
import com.playlet.common.utils.DateUtils;
import com.playlet.system.domain.*;
import com.playlet.system.service.IPlayletRevenueRecordService;
import com.playlet.system.service.IPlayletTaskService;
import com.playlet.web.service.app.PlayletTaskAppService;
import com.playlet.web.service.app.PlayletUserTaskAppService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

/**
 * @Date: 2024-03-22 00:02
 * @Author: 但星霖
 * @Version: v1.0
 * @Description: 任务app Service接口实现
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PlayletTaskAppServiceImpl implements PlayletTaskAppService {

    private final IPlayletTaskService playletTaskService;
    private final PlayletUserTaskAppService playletUserTaskAppService;
    private final IPlayletRevenueRecordService revenueRecordService;

    @Override
    public PageInfo<PlayletTask> getTaskPage(PlayletTask playletTask, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PlayletTask> revenueRecordList = playletTaskService.selectPlayletTaskList(playletTask);
        return PageInfo.of(revenueRecordList);
    }

    @Override
    public PageInfo<PlayletTask> getTaskPageByUser(PlayletTask playletTask, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PlayletTask> revenueRecordList = playletTaskService.selectPlayletTaskListByUser(playletTask);
        return PageInfo.of(revenueRecordList);
    }

    @Override
    public Boolean userDesignateTask(PlayletTask playletTask) {
        // 检查任务数据。
        PlayletTask task = playletTaskService.selectPlayletTaskById(playletTask.getId());
        // 任务数据数据
        if (ObjectUtils.isNull(task)) {
            throw new ServiceException("参与任务失败，未找到匹配任务数据。");
        }
        // 任务状态
        if (task.getState() != 1) {
            throw new ServiceException("参与任务失败，任务还未开始或者已结束。");
        }
        // 是否在任务时间线内
        if (DateUtil.compare(task.getStartTime(), new Date()) >=0 &&  DateUtil.compare(task.getEndTime(), new Date())<=0) {
            // 用户数据绑定。
            playletUserTaskAppService.insertPlayletUserTask(PlayletUserTask.dataSupplement(task));
            return Boolean.TRUE;
        } else {
            throw new ServiceException("参与任务失败，任务未开始或者已结束。");
        }
    }

    /**
     * 查询任务
     *
     * @param id 任务主键
     * @return 任务
     */
    @Override
    public PlayletTask selectPlayletTaskById(Long id) {
        PlayletTask task = playletTaskService.selectPlayletTaskById(id);
        if (ObjectUtils.isNotNull(task)) {
            // 奖金划分数据查询
            List<PlayletRevenueRecord> revenueRecordList = revenueRecordService.searchByTaskId(task.getId());
            if (CollectionUtils.isNotEmpty(revenueRecordList)) {
                Long totalMoney = revenueRecordList.stream().mapToLong(PlayletRevenueRecord::getPracticalEarnings).sum();
                task.setBonusDividePercent(new DecimalFormat("#.0").format((totalMoney / task.getBonusDivideMoney() * 100)) + "%");
                task.setBonusDividePercent(totalMoney.toString());
            } else {
                task.setBonusDividePercent("100%");
                task.setBonusDividePercent("0");
            }
        }
        return task;
    }

    /**
     * 查询任务列表
     *
     * @param playletTask 任务
     * @return 任务
     */
    @Override
    public List<PlayletTask> selectPlayletTaskList(PlayletTask playletTask) {
        return playletTaskService.selectPlayletTaskList(playletTask);
    }

    /**
     * 新增任务
     *
     * @param playletTask 任务
     * @return 结果
     */
    @Override
    public int insertPlayletTask(PlayletTask playletTask) {
        playletTask.setCreateTime(DateUtils.getNowDate());
        return playletTaskService.insertPlayletTask(playletTask);
    }

    /**
     * 修改任务
     *
     * @param playletTask 任务
     * @return 结果
     */
    @Override
    public int updatePlayletTask(PlayletTask playletTask) {
        playletTask.setUpdateTime(DateUtils.getNowDate());
        return playletTaskService.updatePlayletTask(playletTask);
    }

    /**
     * 批量删除任务
     *
     * @param ids 需要删除的任务主键
     * @return 结果
     */
    @Override
    public int deletePlayletTaskByIds(String ids) {
        return playletTaskService.deletePlayletTaskByIds(ids);
    }

    /**
     * 删除任务信息
     *
     * @param id 任务主键
     * @return 结果
     */
    @Override
    public int deletePlayletTaskById(Long id) {
        return playletTaskService.deletePlayletTaskById(id);
    }
}
