package com.fs.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fs.common.exception.ServiceException;
import com.fs.course.domain.FsUserCoursePeriod;
import com.fs.course.domain.FsUserCoursePeriodDays;
import com.fs.course.mapper.FsUserCoursePeriodDaysMapper;
import com.fs.course.mapper.FsUserCoursePeriodMapper;
import com.fs.course.service.IFsUserCoursePeriodService;
import com.fs.course.vo.FsUserCoursePeriodVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 会员营期Service业务层处理
 *
 * @author fs
 * @date 2025-04-15
 */
@Service
public class FsUserCoursePeriodServiceImpl implements IFsUserCoursePeriodService
{
    @Autowired
    private FsUserCoursePeriodMapper fsUserCoursePeriodMapper;
    @Resource
    private FsUserCoursePeriodDaysMapper fsUserCoursePeriodDaysMapper;

    /**
     * 查询会员营期
     *
     * @param periodId 会员营期ID
     * @return 会员营期
     */
    @Override
    public FsUserCoursePeriod selectFsUserCoursePeriodById(Long periodId)
    {
        return fsUserCoursePeriodMapper.selectFsUserCoursePeriodById(periodId);
    }

    /**
     * 查询会员营期列表
     *
     * @param fsUserCoursePeriod 会员营期
     * @return 会员营期
     */
    @Override
    public List<FsUserCoursePeriod> selectFsUserCoursePeriodList(FsUserCoursePeriod fsUserCoursePeriod)
    {
        return fsUserCoursePeriodMapper.selectFsUserCoursePeriodList(fsUserCoursePeriod);
    }

    /**
     * 新增会员营期
     *
     * @param fsUserCoursePeriod 会员营期
     * @return 结果
     */
    @Override
    public int insertFsUserCoursePeriod(FsUserCoursePeriod fsUserCoursePeriod)
    {
        fsUserCoursePeriod.setCreateTime(LocalDateTime.now());
        // 设置营期状态
        if(LocalDate.now().isBefore(fsUserCoursePeriod.getPeriodStartingTime())){
            fsUserCoursePeriod.setPeriodStatus(1L);
        } else if(LocalDate.now().isAfter(fsUserCoursePeriod.getPeriodEndTime())){
            fsUserCoursePeriod.setPeriodStatus(3L);
        } else{
            fsUserCoursePeriod.setPeriodStatus(2L);
        }
        return fsUserCoursePeriodMapper.insertFsUserCoursePeriod(fsUserCoursePeriod);
    }

    /**
     * 修改会员营期
     *
     * @param fsUserCoursePeriod 会员营期
     * @return 结果
     */
    @Override
    public int updateFsUserCoursePeriod(FsUserCoursePeriod fsUserCoursePeriod)
    {
        fsUserCoursePeriod.setUpdateTime(LocalDateTime.now());
        return fsUserCoursePeriodMapper.updateFsUserCoursePeriod(fsUserCoursePeriod);
    }

    /**
     * 批量删除会员营期
     *
     * @param periodIds 需要删除的会员营期ID
     * @return 结果
     */
    @Override
    public int deleteFsUserCoursePeriodByIds(Long[] periodIds)
    {
        return fsUserCoursePeriodMapper.deleteFsUserCoursePeriodByIds(periodIds);
    }

    /**
     * 删除会员营期信息
     *
     * @param periodId 会员营期ID
     * @return 结果
     */
    @Override
    public int deleteFsUserCoursePeriodById(Long periodId)
    {
        return fsUserCoursePeriodMapper.deleteFsUserCoursePeriodById(periodId);
    }

    @Override
    public List<FsUserCoursePeriodVO> selectFsUserCoursePeriodPage(FsUserCoursePeriod fsUserCoursePeriod) {
        return fsUserCoursePeriodMapper.selectFsUserCoursePeriodPage(fsUserCoursePeriod);
    }

    /**
     * 结束营期
     * @param id    营期ID
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void closePeriod(Long id) {
        // 修改营期结束时间
        FsUserCoursePeriod fsUserCoursePeriod = fsUserCoursePeriodMapper.selectFsUserCoursePeriodById(id);
        if (Objects.isNull(fsUserCoursePeriod)) {
            throw new ServiceException("结束营期失败：营期不存在");
        }

        if (fsUserCoursePeriod.getPeriodStatus() == 1) {
            throw new ServiceException("结束营期失败：营期未开始不能进行结束操作！");
        }

        if (fsUserCoursePeriod.getPeriodStatus() == 3) {
            throw new ServiceException("结束营期失败：营期已结束不能进行结束操作！");
        }

        fsUserCoursePeriod.setPeriodEndTime(LocalDate.now());
        fsUserCoursePeriodMapper.updateFsUserCoursePeriod(fsUserCoursePeriod);

        // 修改大于当前时间的课程状态
        Wrapper<FsUserCoursePeriodDays> updateWrapper = Wrappers.<FsUserCoursePeriodDays>lambdaUpdate()
        .set(FsUserCoursePeriodDays::getStatus, 2)
        .eq(FsUserCoursePeriodDays::getPeriodId, fsUserCoursePeriod.getPeriodId())
        .eq(FsUserCoursePeriodDays::getStatus, 0)
        .gt(FsUserCoursePeriodDays::getDayDate, LocalDate.now());
        fsUserCoursePeriodDaysMapper.update(null, updateWrapper);
    }

    /**
     * 更新营期状态
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changePeriodStatus() {
        // 开营
        fsUserCoursePeriodMapper.startPeriod(LocalDate.now());

        // 关营
        fsUserCoursePeriodMapper.endPeriod(LocalDate.now());
    }
}
