package com.fs.course.mapper;

import com.fs.common.core.domain.entity.SysDictData;
import com.fs.course.domain.FsUserCoursePeriod;
import com.fs.course.param.CompanyRedPacketParam;
import com.fs.course.param.PeriodStatisticCountParam;
import com.fs.course.vo.FsUserCoursePeriodVO;
import com.fs.course.vo.PeriodRedPacketVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 会员营期Mapper接口
 *
 * @author fs
 * @date 2025-04-15
 */
public interface FsUserCoursePeriodMapper
{
    /**
     * 查询会员营期
     *
     * @param periodId 会员营期ID
     * @return 会员营期
     */
    public FsUserCoursePeriod selectFsUserCoursePeriodById(Long periodId);

    /**
     * 查询会员营期列表
     *
     * @param fsUserCoursePeriod 会员营期
     * @return 会员营期集合
     */
    public List<FsUserCoursePeriod> selectFsUserCoursePeriodList(FsUserCoursePeriod fsUserCoursePeriod);

    /**
     * 新增会员营期
     *
     * @param fsUserCoursePeriod 会员营期
     * @return 结果
     */
    public int insertFsUserCoursePeriod(FsUserCoursePeriod fsUserCoursePeriod);

    /**
     * 修改会员营期
     *
     * @param fsUserCoursePeriod 会员营期
     * @return 结果
     */
    public int updateFsUserCoursePeriod(FsUserCoursePeriod fsUserCoursePeriod);

    /**
     * 删除会员营期
     *
     * @param periodId 会员营期ID
     * @return 结果
     */
    public int deleteFsUserCoursePeriodById(Long periodId);

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

    /**
     * 查询主页分页列表
     * @return
     */
    List<FsUserCoursePeriodVO> selectFsUserCoursePeriodPage(FsUserCoursePeriod fsUserCoursePeriod);

    /**
     * 查询训练营营期
     * @param id    训练营ID
     * @return  list
     */
    @Select("select * from fs_user_course_period where training_camp_id = #{id}")
    List<FsUserCoursePeriod> selectFsUserCoursePeriodListByTrainingCampId(@Param("id") Long id);

    /**
     * 根据营期id和视频id查询关联的公司
     * @param periodId 营期id
     * @param videoId 视频id
     * @return
     */
    List<CompanyRedPacketParam> selectCompanyByPeriodVideo(@Param("periodId")Long periodId, @Param("videoId") Long videoId);

    /**
     * 根据营期id查询对应的公司
     * @param periodId 营期id
     * @return
     */
    List<CompanyRedPacketParam> selectCompanyByPeriod(@Param("periodId")Long periodId);

    List<PeriodRedPacketVO> selectPeriodRedPacket(@Param("periodId")Long periodId, @Param("companyId")Long companyId);

    /**
     * 根据营期ids批量获取营期数据
     * @param periodIds 营期ids
     * @return
     */
    List<FsUserCoursePeriod> selectFsUserCoursePeriodsByIds(@Param("periodIds") Set<Long> periodIds);

    /**
     * 查询训练营进行中的营期条数
     * @param params    参数
     * @return  count
     */
    int countPeriodByCampIds(@Param("params") Map<String, Object> params);

    /**
     * 开营（跳过自由模式的营期）
     * @param now   当前日期
     */
    @Update("update fs_user_course_period set period_status = 2, update_time = now() where period_status = 1 and period_starting_time <= #{now} and (free_mode is null or free_mode != 1)")
    void startPeriod(@Param("now") LocalDate now);

    /**
     * 关营（跳过自由模式的营期）
     * @param now   当前日期
     */
    @Update("update fs_user_course_period set period_status = 3, update_time = now() where period_status = 2 and period_end_time < #{now} and (free_mode is null or free_mode != 1)")
    void endPeriod(@Param("now") LocalDate now);


    @Select("select * from fs_user_course_period where find_in_set(${companyId},company_id) and #{previousDay} >= period_starting_time and #{previousDay} <= period_end_time ")
    List<Long> queryPeriod(@Param("companyId") Long companyId,
                           @Param("previousDay") LocalDate previousDay);

    List<FsUserCoursePeriod> selectPeriodListByTrainingCampIds(@Param("trainingCampIds") Long[] trainingCampIds);

    @Update("<script>" +
            "UPDATE fs_user_course_period SET del_flag = #{delFlag} WHERE period_id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int updateBatchDelFlag(@Param("ids") Long [] ids, @Param("delFlag") Integer delFlag);


    Long setlectCourseCompleteNum(PeriodStatisticCountParam param);

    Long setlectCourseWatchNum(PeriodStatisticCountParam param);

    Long setlectRedPacketCount(PeriodStatisticCountParam param);

    BigDecimal setlectRedPacketAmount(PeriodStatisticCountParam param);

    Long setlectCorrectAnswerNum(PeriodStatisticCountParam param);

    List<FsUserCoursePeriod> selectFsPeriodlist(PeriodStatisticCountParam param);

    @Select("select cp.period_id from fs_user_course_period cp left where company_id=#{companyId}")
    List<Long> selectCoursePeriodDaysByTime(@Param("periodSTime") String periodSTime,@Param("periodETime") String periodETime,@Param("companyId") Long companyId);

    @Select("<script>" +
            "select period_id from fs_user_course_period where " +
            "FIND_IN_SET(#{companyId}, company_id) > 0 " +
            " AND period_id IN" +
            "<foreach collection='periodIds' item='periodId' open='(' separator=',' close=')'> " +
            "#{periodId} " +
            "</foreach> " +
            "</script> ")
    List<Long> selectFsUserCoursePeriodListByPeriodId(@Param("periodIds") List<Long> qwUserIds,@Param("companyId") Long companyId);

    List<SysDictData> selectFsUserCoursePeriodListLabel(FsUserCoursePeriod fsUserCoursePeriod);
}
