package com.fs.course.mapper;

import java.util.List;
import com.fs.course.domain.FsCourseFinishTemp;
import com.fs.course.vo.FsCourseFinishTempListVO;
import com.fs.course.vo.FsCourseFinishTempVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 完课模板Mapper接口
 *
 * @author fs
 * @date 2024-12-19
 */
public interface FsCourseFinishTempMapper
{
    /**
     * 查询完课模板
     *
     * @param id 完课模板主键
     * @return 完课模板
     */
    public FsCourseFinishTemp selectFsCourseFinishTempById(Long id);

    /**
     * 查询完课模板列表
     *
     * @param fsCourseFinishTemp 完课模板
     * @return 完课模板集合
     */
    public List<FsCourseFinishTemp> selectFsCourseFinishTempList(FsCourseFinishTemp fsCourseFinishTemp);

    /**
     * 新增完课模板
     *
     * @param fsCourseFinishTemp 完课模板
     * @return 结果
     */
    public int insertFsCourseFinishTemp(FsCourseFinishTemp fsCourseFinishTemp);

    /**
     * 修改完课模板
     *
     * @param fsCourseFinishTemp 完课模板
     * @return 结果
     */
    public int updateFsCourseFinishTemp(FsCourseFinishTemp fsCourseFinishTemp);

    /**
     * 删除完课模板
     *
     * @param id 完课模板主键
     * @return 结果
     */
    public int deleteFsCourseFinishTempById(Long id);

    /**
     * 批量删除完课模板
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsCourseFinishTempByIds(Long[] ids);

    @Select({"<script> " +
            "select t.*,v.title as video_name,c.course_name  from fs_course_finish_temp t " +
            " left join fs_user_course c on c.course_id = t.course_id " +
            " left join fs_user_course_video v on v.video_id = t.video_id" +
            " where t.is_del = 0  " +
            "<if test = ' maps.name !=null and maps.name!=\"\" '> " +
            "and t.name like concat('%', #{maps.name}, '%') " +
            "</if>" +
            "<if test = ' maps.parentId !=null'> " +
            "and t.parent_id = #{maps.parentId} " +
            "</if>" +
            "<if test = ' maps.courseId !=null  '> " +
            "and t.course_id = #{maps.courseId} " +
            "</if>" +
            "<if test = ' maps.videoId !=null  '> " +
            "and t.video_id = #{maps.videoId} " +
            "</if>" +
            "<if test = ' maps.companyId !=null  '> " +
            "and t.company_id = #{maps.companyId} " +
            "</if>" +
            "<if test = ' maps.status !=null '> " +
            "and t.status = #{maps.status} " +
            "</if>" +
            "            <if test=\" maps.userIds != null and !maps.userIds.isEmpty()\">\n" +
            "                AND create_by IN\n" +
            "                <foreach collection=' maps.userIds' item='item' open='(' separator=',' close=')'>\n" +
            "                    #{item}\n" +
            "                </foreach>\n" +
            "            </if>" +
            " order by t.id desc  "+
            "</script>"})
    List<FsCourseFinishTempListVO> selectFsCourseFinishTempListVO(@Param("maps") FsCourseFinishTemp fsCourseFinishTemp);

    @Select("select * from fs_course_finish_temp where video_id = #{videoId} " +
            "and find_in_set(#{userId},company_user_ids) and is_del= 0 AND status= 1 order by create_time desc  limit 1 ")
    FsCourseFinishTemp selectFsCourseFinishTempByCompanyUserId(@Param("userId") Long companyUserId, @Param("videoId")Long videoId);

    @Select("    SELECT *\n" +
            "    FROM fs_course_finish_temp\n" +
            "    WHERE video_id = #{videoId}\n" +
            "    AND is_del = 0 AND status= 1 \n" +
            "    AND (\n" +
            "        (is_all_company_user = 1 AND company_id = #{companyId})\n" +
            "        OR\n" +
            "        ((is_all_company_user != 1 OR is_all_company_user IS NULL) AND FIND_IN_SET(#{userId}, company_user_ids))\n" +
            "    )\n" +
            "    ORDER BY create_time DESC\n" +
            "    LIMIT 1\n")
    FsCourseFinishTemp selectFsCourseFinishTempByCompanyId(@Param("userId") Long companyUserId,@Param("companyId") Long companyId, @Param("videoId")Long videoId);

    @Select("SELECT * FROM fs_course_finish_temp WHERE id = #{id}")
    public FsCourseFinishTempVO selectFsCourseFinishTempByIdVO(Long id);

    void deleteByParentIds(@Param("ids") Long[] ids);

    /**
     * 按父模板取一条子模板（用于回显看课/群聊规则）
     */
    @Select("SELECT * FROM fs_course_finish_temp WHERE parent_id = #{parentId} AND (is_del = 0 OR is_del IS NULL) ORDER BY id ASC LIMIT 1")
    FsCourseFinishTemp selectOneByParentId(@Param("parentId") Long parentId);

    /**
     * 父模板修改时，同步更新其下所有子模板的看课规则与群聊恭喜规则
     */
    @Update("UPDATE fs_course_finish_temp SET setting = #{setting}, chat_setting = #{chatSetting}, update_time = NOW() " +
            "WHERE parent_id = #{parentId} AND (is_del = 0 OR is_del IS NULL)")
    int updateSettingByParentId(@Param("parentId") Long parentId,
                                @Param("setting") String setting,
                                @Param("chatSetting") String chatSetting);

    @Update({"<script> " +
            " update fs_course_finish_temp " +
            " set status = #{data.status} ," +
            " update_time = #{data.updateTime} " +
            " where id in " +
            " <foreach collection='data.ids' item='id' open='(' separator=',' close=')'>" +
              " #{id} " +
            " </foreach> " +
            "</script>"})
    int updateFsCourseFinishTempBatch(@Param("data") FsCourseFinishTemp fsCourseFinishTemp);
}
