<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yonge.cooleshow.biz.dal.dao.ImGroupDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.ImGroup">
        <id column="id_" jdbcType="VARCHAR" property="id"/>
        <result column="name_" jdbcType="VARCHAR" property="name"/>
        <result column="introduce_" jdbcType="VARCHAR" property="introduce"/>
        <result column="member_num_" jdbcType="INTEGER" property="memberNum"/>
        <result column="memo_" jdbcType="VARCHAR" property="memo"/>
        <result column="img_" jdbcType="VARCHAR" property="img"/>
        <result column="type_" jdbcType="VARCHAR" property="type"/>
        <result column="create_by_" property="createBy"/>
        <result column="auto_pass_flag_" property="autoPassFlag"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time_" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="config_json_" jdbcType="VARCHAR" property="configJson"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_, name_, introduce_, member_num_, memo_, img_, type_,create_by_, create_time_, update_time_
    </sql>

    <update id="updateById" parameterType="com.yonge.cooleshow.biz.dal.entity.ImGroup">
        update im_group
        <set>
            <if test="et.name != null">
                name_ = #{et.name},
            </if>
            <if test="et.autoPassFlag != null">
                auto_pass_flag_ = #{et.autoPassFlag},
            </if>
            <if test="et.introduce != null and et.introduce != ''">
                introduce_ = #{et.introduce},
            </if>
            <if test="et.memo != null and et.memo != ''">
                memo_ = #{et.memo},
            </if>
            <if test="et.configJson != null">
                config_json_ = #{et.configJson},
            </if>
            update_time_ = NOW()
        </set>
        where id_ = #{et.id}
    </update>
    <update id="updateMemberNum">
        UPDATE im_group ig
        LEFT JOIN (SELECT COUNT(id_) num_,group_id_ FROM im_group_member
        <if test="groupId != null">
            WHERE group_id_ = #{groupId}
        </if>
        GROUP BY group_id_) igm ON ig.id_ = igm.group_id_
        SET member_num_ = igm.num_
        WHERE ig.id_ = igm.group_id_
    </update>
    <update id="updateStatus">
        update im_history_message set status_ = 1
        where msgUID_ in <foreach collection="info" open="(" close=")" separator="," item="info">
        #{info.msgUID}
    </foreach>
    </update>
    <select id="findAll" resultMap="BaseResultMap">
        SELECT ig.* FROM im_group ig
        LEFT JOIN im_group_member igm ON igm.group_id_ = ig.id_
        <where>
            <if test="imGroupDto.type != null and imGroupDto.type != ''">
                AND ig.type_ = #{imGroupDto.type}
            </if>
            <if test="imGroupDto.createUserId != null">
                AND ig.create_by_ = #{imGroupDto.createUserId}
            </if>
            <if test="imGroupDto.userId != null">
                AND igm.user_id_ = #{imGroupDto.userId}
            </if>
            <if test="imGroupDto.roleType != null">
                AND igm.role_type_ = #{imGroupDto.roleType}
            </if>
            <if test="imGroupDto.search != null and imGroupDto.search != ''">
                AND (ig.id_ = #{imGroupDto.search} OR
                ig.name_ LIKE CONCAT('%',#{imGroupDto.search},'%') OR ig.introduce_ LIKE CONCAT('%',#{imGroupDto.search},'%'))
            </if>
        </where>
        ORDER BY id_ DESC
    </select>
    <resultMap id="ImGroupResultDto" type="com.yonge.cooleshow.biz.dal.dto.ImGroupResultDto" extends="BaseResultMap">
        <result property="existFlag" column="existFlag"/>
        <result property="hasWaitAuditFlag" column="hasWaitAuditFlag"/>
    </resultMap>
    <select id="queryTeacherFun" resultMap="ImGroupResultDto">
        SELECT ig.*,
               CASE WHEN igm.id_ IS NULL THEN 0 ELSE 1 END existFlag,
               CASE WHEN igma.id_ IS NULL THEN 0 ELSE 1 END hasWaitAuditFlag
        FROM im_group ig
                 LEFT JOIN im_group_member igm ON (ig.id_ = igm.group_id_ AND igm.user_id_ = #{imGroupDto.userId} <if test="imGroupDto.roleType != null"> AND igm.role_type_ = #{imGroupDto.roleType} </if>)
                 LEFT JOIN im_group_member_audit igma ON igma.group_id_ = ig.id_ AND igma.audit_status_ = 'AUDITING' AND igma.user_id_ = #{imGroupDto.userId}
        WHERE ig.create_by_ = #{imGroupDto.createUserId}
        <if test="imGroupDto.type != null and imGroupDto.type != ''">
            AND ig.type_ = #{imGroupDto.type}
        </if>
        <if test="imGroupDto.search != null and imGroupDto.search != ''">
            AND (ig.id_ = #{imGroupDto.search} OR
            ig.name_ LIKE CONCAT('%',#{imGroupDto.search},'%') OR ig.introduce_ LIKE CONCAT('%',#{imGroupDto.search},'%'))
        </if>
        GROUP BY ig.id_
    </select>
    <select id="selectAll" resultType="com.yonge.cooleshow.biz.dal.entity.ImHistoryMessage">
        select msgUID_          AS msgUID,
               fromUserId_      AS fromUserId,
               targetId_        AS targetId,
               targetType_      AS targetType,
               GroupId_         AS groupId,
               busChannel_      AS busChannel,
               classname_       AS classname,
               content_         AS content,
               extraContent_    AS extraContent,
               dateTime_        AS dateTime,
               source_          AS source,
               isDiscard_       AS isDiscard,
               isSensitiveWord_ AS isSensitiveWord,
               isForbidden_     AS isForbidden,
               isNotForward_    AS isNotForward,
               groupUserIds_    AS groupUserIds,
               appId_           AS appId

        from im_history_message
        where status_ = 0
        limit #{result},#{size}

    </select>
    <select id="queryCount" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM im_history_message where status_ = 0
    </select>

    <insert id="batchInsert" parameterType="com.yonge.cooleshow.biz.dal.entity.ImHistoryMessage">
        insert ignore into im_history_message(msgUID_,fromUserId_,targetId_,targetType_,
        GroupId_,busChannel_,classname_,content_,
        extraContent_,dateTime_,source_,isDiscard_,
        isSensitiveWord_,isForbidden_,isNotForward_,
        groupUserIds_,appId_)
        values
        <foreach collection="list" separator="," item="item" >
            (#{item.msgUID,jdbcType=VARCHAR},#{item.fromUserId,jdbcType=VARCHAR},#{item.targetId,jdbcType=VARCHAR},
            #{item.targetType},#{item.groupId,jdbcType=VARCHAR},#{item.busChannel,jdbcType=VARCHAR},
            #{item.classname,jdbcType=VARCHAR},#{item.content,jdbcType=VARCHAR},#{item.extraContent,jdbcType=VARCHAR},
            #{item.dateTime,jdbcType=VARCHAR},#{item.source,jdbcType=VARCHAR},#{item.isDiscard,jdbcType=VARCHAR},
            #{item.isSensitiveWord,jdbcType=VARCHAR},#{item.isForbidden,jdbcType=VARCHAR},#{item.isNotForward,jdbcType=VARCHAR},
            #{item.groupUserIds,jdbcType=VARCHAR},#{item.appId,jdbcType=VARCHAR})
        </foreach>
    </insert>
</mapper>
