package cn.exploring.engine.server.db.dao;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface SpecialSqlMapper {
    //批量设置将组织管理员设置成普通成员
    int updateOrganizeMemberRolesToGeneral(@Param("orgId") Integer orgId);

    //批量设置组织管理员
    int updateOrganizeMemberRoles(@Param("list") List userIdList, @Param("orginzeId") Integer orginzeId);

    //批量设置组织管理员
    int updateOrganizeMemberExamine(@Param("examine") Integer examine, @Param("orginzeId") Integer orginzeId);

    //批量更新组织成员中关于组织的信息
    int updateOrganizeMember(@Param("list") List id, @Param("reserveTwo") String reserveTwo, @Param("reserveThree") String reserveThree, @Param("reserveFour") String reserveFour, @Param("orgName") String orgName, @Param("examine") Integer examine);

    //删除组织成员
    int updateOrganizeMemberDeleteByOrgIds(@Param("list") List orgId);

    //批量更新戈壁活动（如：戈15）报名信息  组织名称
    int updateGobiMemberInfoOrgName(@Param("list") List id, @Param("orgName") String orgName);

    //批量删除 人的投票表 根据投票话题ids 删除
    int updateUserVoteLogDeleteByVotesIds(@Param("list") List voteIds);

    //批量删除 人的接龙 根据接龙话题ids 删除
    int updateSolitaireDetailsDeleteBySolitaireIds(@Param("list") List solitaireIds);


    //更新消息，设置成已读

    /**
     * @param userId   用户id
     * @param messType 消息类型 0:新的邀请, 1：新的组织申请，2：评论，3：赞，4：订阅
     * @param orgId    组织id
     * @return
     */
    int updateNewMessage(@Param("userId") Integer userId, @Param("messType") Integer messType, @Param("orgId") Integer orgId);

    //更新消息，设置成已读 根据ids

    /**
     * @return
     */
    int updateNewMessageByIds(@Param("list") List ids);


    //查询未读消息分类数量  消息类型 0:新的邀请, 1：新的组织申请，2：评论，3：赞，4：订阅



    /**
     * 信息流列表1——组织。查询权限组织内的
     *
     * @param orgIds
     * @param page
     * @param size
     * @param sort
     * @param order
     * @return
     */
    @Select("<script> " +
            "SELECT DISTINCT a.* FROM " +
            "(SELECT * FROM wx_feed WHERE deleted=FALSE" +
            "<if test='id != null'> AND id = ${id} </if>" +
            ") a " +
            "LEFT JOIN " +
            "(SELECT feed_id, org_id FROM wx_feed_permission) b " +
            "ON a.id = b.feed_id " +
            "WHERE b.org_id IN " +
            "<foreach collection='orgIds' item='org_id' index='index' open='(' separator=',' close=')'>" +
            "#{org_id}" +
            "</foreach>" +
            "<if test='allOpened'> OR b.org_id = 0 </if>" +
            "ORDER by  ${sort}  ${order} " +
            "LIMIT ${(page - 1) * size}, ${page * size} " +
            "</script>"
    )
    List<Object> listFeeds1(@Param("id") Integer id,
                            @Param("orgIds") List orgIds,
                            @Param("allOpened") Boolean allOpened,
                            @Param("page") Integer page,
                            @Param("size") Integer size,
                            @Param("sort") String sort,
                            @Param("order") String order);

    /**
     * 信息流总条数1——组织。查询权限组织内的
     * @param orgIds
     * @return
     */
    @Select("<script> " +
            "SELECT COUNT(DISTINCT a.id) FROM " +
            "(SELECT * FROM wx_feed WHERE deleted=FALSE" +
            "<if test='id != null'> AND id = ${id} </if>" +
            ") a " +
            "LEFT JOIN " +
            "(SELECT feed_id, org_id FROM wx_feed_permission) b " +
            "ON a.id = b.feed_id " +
            "WHERE b.org_id IN " +
            "<foreach collection='orgIds' item='org_id' index='index' open='(' separator=',' close=')'>" +
            "#{org_id}" +
            "</foreach>" +
            "<if test='allOpened'> OR b.org_id = 0 </if>" +
            "</script>"
    )
    long totalFeeds1(@Param("id") Integer id, @Param("orgIds") List orgIds, @Param("allOpened") Boolean allOpened);

    /**
     * 信息流总条数2——关注。查询权限组织内且我关注的人发的
     * @param orgIds
     * @param attentionUserIds
     * @return
     */
    @Select("<script> " +
            "SELECT COUNT(DISTINCT a.id) FROM " +
            "(SELECT * FROM wx_feed WHERE deleted=FALSE AND user_id IN " +
            "<foreach collection='attentionUserIds' item='user_id' index='index' open='(' separator=',' close=')'>" +
            "#{user_id}" +
            "</foreach>" +
            "<if test='id != null'> AND id = ${id} </if>" +
            ") a " +
            "LEFT JOIN " +
            "(SELECT feed_id, org_id FROM wx_feed_permission) b " +
            "ON a.id = b.feed_id " +
            "WHERE b.org_id IN " +
            "<foreach collection='orgIds' item='org_id' index='index' open='(' separator=',' close=')'>" +
            "#{org_id}" +
            "</foreach>" +
            " OR b.org_id = 0 " +
            "</script>"
    )
    long totalFeeds2(@Param("id") Integer id,
                     @Param("orgIds") List orgIds,
                     @Param("attentionUserIds") List attentionUserIds);

    /**
     * 信息流总条数3——推荐。查询权限为完全公开的
     * @return
     */
    @Select("<script> " +
            "SELECT COUNT(DISTINCT a.id) FROM " +
            "(SELECT * FROM wx_feed WHERE deleted=FALSE " +
            "<if test='id != null'> AND id = ${id} </if>" +
            ") a " +
            "LEFT JOIN " +
            "(SELECT feed_id, org_id FROM wx_feed_permission) b " +
            "ON a.id = b.feed_id " +
            "WHERE b.org_id = 0 " +
            "</script>"
    )
    long totalFeeds3(@Param("id") Integer id);



    @Select("SELECT chapters, chapters_name FROM lesson_info WHERE id in (SELECT MAX(id) FROM lesson_info WHERE deleted = FALSE GROUP BY chapters) ORDER BY chapters")
    List<Map> selectAllChaptersInfo();

    @Select("<script> " +
            "SELECT chapters, chapters_name, session_name, " +
            " CASE select_id WHEN 1 THEN option_A_point WHEN 2 THEN option_B_point WHEN 3 THEN option_C_point ELSE 0 END as chapters_point " +
            "FROM   (SELECT * FROM lesson_info WHERE deleted = FALSE) bb " +
            "LEFT JOIN  " +
            "(SELECT * FROM user_lesson_info WHERE deleted = FALSE" +
            "<if test = 'userId != null'> AND user_id = ${userId}  </if>" +
            " ) aa" +
            " ON aa.chapters_id = bb.chapters and aa.session_id = bb.session ORDER BY lesson_order" +
            "</script>"
    )
    List<Map> selectUserHistoryPointDetail(@Param("userId") Integer userId);

    @Select("<script> " +
            "SELECT chapters, chapters_name,  " +
            " sum(CASE select_id WHEN 1 THEN option_A_point WHEN 2 THEN option_B_point WHEN 3 THEN option_C_point ELSE 0 END) / count(select_id) as avg_chapters_point " +
            "FROM   (SELECT * FROM lesson_info WHERE deleted = FALSE) bb " +
            "LEFT JOIN  " +
            "(SELECT * FROM user_lesson_info WHERE deleted = FALSE" +
            "<if test = 'userId != null'> AND user_id = ${userId}  </if>" +
            " ) aa" +
            " ON aa.chapters_id = bb.chapters and aa.session_id = bb.session group by chapters order by chapters" +
            "</script>"
    )
    List<Map> selectChaptersAvgPoint(@Param("userId") Integer userId);

    /**
     * 筛选数据库中用户没有选课后答案的课题数量
     * @param userId
     * @return
     */
    @Select("<script> " +
            "SELECT count(*) as count_info FROM ((SELECT chapters, `session` FROM lesson_info WHERE deleted = FALSE) aa LEFT JOIN " +
            "(SELECT chapters_id, session_id, select_id FROM user_lesson_info WHERE  deleted = FALSE  " +
            "<if test = 'userId != null'>  AND user_id = ${userId}  </if>" +
            ") bb " +
            " ON aa.chapters = bb.chapters_id  AND aa.session = bb.session_id) " +
            "WHERE select_id is null " +
            "</script>"
    )
    Map selectUnfinishLessonCount(@Param("userId") Integer userId);

    /**
     * 随机选择一个的学生
     * @param classId
     * @return
     */
    @Select("SELECT * FROM (SELECT * FROM student_info WHERE deleted = FALSE AND class_id = ${classId})  a1 LEFT JOIN ( SELECT (RAND()  * max(id) ) as nid  FROM  student_info WHERE deleted = FALSE AND class_id = ${classId}) a2 ON a1.id > a2.nid LIMIT 1")
    Map<String, Object> selectRandomStudentInfo(@Param("classId") Integer classId);

    @Select("<script> " +
            "SELECT count(*) as count_info FROM user_reduce_info WHERE deleted = FALSE" +
            "<if test='lesson_average_point != null'>  AND lesson_average_point &gt;  ${lesson_average_point}  </if>" +
            "</script>")
    Map<String, Object> selectDataCount(@Param("lesson_average_point") Float lesson_average_point);

    //查询没有报名线上云备赛的用户id-->
    List<Integer> unEnrollUser();

    //查询没有参加任何组织的用户用户id-->
    List<Integer> unJoinOrganizeUser();
}
