package com.fs.course.service.impl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.common.core.domain.R;
import com.fs.common.exception.base.BaseException;
import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.date.DateUtil;
import com.fs.common.utils.date.TimeTypeEnum;
import com.fs.course.domain.FsUserCoursePeriod;
import com.fs.course.mapper.FsUserCoursePeriodMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.fs.course.mapper.FsUserCoursePeriodDaysMapper;
import com.fs.course.domain.FsUserCoursePeriodDays;
import com.fs.course.service.IFsUserCoursePeriodDaysService;

/**
 * 营期课程Service业务层处理
 * 
 * @author fs
 * @date 2025-04-16
 */
@Service
@AllArgsConstructor
public class FsUserCoursePeriodDaysServiceImpl extends ServiceImpl<FsUserCoursePeriodDaysMapper, FsUserCoursePeriodDays> implements IFsUserCoursePeriodDaysService {

    private final FsUserCoursePeriodMapper fsUserCoursePeriodMapper;

    /**
     * 查询营期课程
     * 
     * @param id 营期课程主键
     * @return 营期课程
     */
    @Override
    public FsUserCoursePeriodDays selectFsUserCoursePeriodDaysById(Long id)
    {
        return baseMapper.selectFsUserCoursePeriodDaysById(id);
    }

    /**
     * 查询营期课程列表
     * 
     * @param fsUserCoursePeriodDays 营期课程
     * @return 营期课程
     */
    @Override
    public List<FsUserCoursePeriodDays> selectFsUserCoursePeriodDaysList(FsUserCoursePeriodDays fsUserCoursePeriodDays)
    {
        return baseMapper.selectFsUserCoursePeriodDaysList(fsUserCoursePeriodDays);
    }

    /**
     * 新增营期课程
     * 
     * @param fsUserCoursePeriodDays 营期课程
     * @return 结果
     */
    @Override
    public int insertFsUserCoursePeriodDays(FsUserCoursePeriodDays fsUserCoursePeriodDays)
    {
        fsUserCoursePeriodDays.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(fsUserCoursePeriodDays);
    }

    /**
     * 修改营期课程
     * 
     * @param fsUserCoursePeriodDays 营期课程
     * @return 结果
     */
    @Override
    public int updateFsUserCoursePeriodDays(FsUserCoursePeriodDays fsUserCoursePeriodDays)
    {
        fsUserCoursePeriodDays.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(fsUserCoursePeriodDays);
    }

    /**
     * 批量删除营期课程
     * 
     * @param ids 需要删除的营期课程主键
     * @return 结果
     */
    @Override
    public int deleteFsUserCoursePeriodDaysByIds(Long[] ids)
    {
        return baseMapper.deleteFsUserCoursePeriodDaysByIds(ids);
    }

    /**
     * 删除营期课程信息
     * 
     * @param id 营期课程主键
     * @return 结果
     */
    @Override
    public int deleteFsUserCoursePeriodDaysById(Long id)
    {
        return baseMapper.deleteFsUserCoursePeriodDaysById(id);
    }

    @Override
    public R addCourse(FsUserCoursePeriodDays entity) {
        FsUserCoursePeriod period = fsUserCoursePeriodMapper.selectFsUserCoursePeriodById(entity.getPeriodId());
        int num = count(new QueryWrapper<FsUserCoursePeriodDays>().eq("period_id", period.getPeriodId()));
        long days;
        if(period.getPeriodType() == 2){
            days = 1;
        }else{
            days = DateUtil.differenceTime(period.getPeriodStartingTime(), period.getPeriodEndTime(), TimeTypeEnum.DAY);
        }
        days++;
        if(num + entity.getVideoIds().size() > days) return R.error("课程不能超过营期范围");
        AtomicInteger i = new AtomicInteger(0);
        List<FsUserCoursePeriodDays> collect = entity.getVideoIds().stream().map(e -> {
            FsUserCoursePeriodDays day = new FsUserCoursePeriodDays();
            day.setPeriodId(entity.getPeriodId());
            day.setLesson(num + i.getAndIncrement());
            day.setDayDate(period.getPeriodStartingTime().plusDays(day.getLesson()));
            day.setCourseId(entity.getCourseId());
            day.setVideoId(e);
            day.setCreateTime(new Date());
            return day;
        }).collect(Collectors.toList());
        super.saveBatch(collect);
        return R.ok();
    }

    @Override
    public R updateListCourseData(List<FsUserCoursePeriodDays> entity) {
        List<FsUserCoursePeriodDays> collect = entity.stream().map(e -> {
            FsUserCoursePeriodDays day = new FsUserCoursePeriodDays();
            day.setId(e.getId());
            day.setMoney(e.getMoney());
            return day;
        }).collect(Collectors.toList());
        super.updateBatchById(collect);
        return R.ok();
    }
}
