<?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.ImGroupMemberAuditDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.ImGroupMemberAudit">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="user_id_" jdbcType="INTEGER" property="userId"/>
        <result column="role_type_" jdbcType="VARCHAR" property="roleType"/>
        <result column="group_id_" jdbcType="VARCHAR" property="groupId"/>
        <result column="desc_" jdbcType="VARCHAR" property="desc"/>
        <result column="audit_status_" jdbcType="VARCHAR" property="auditStatus"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time_" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <resultMap id="ImGroupMemberAuditDto" type="com.yonge.cooleshow.biz.dal.dto.ImGroupMemberAuditDto" extends="BaseResultMap">
        <result property="username" column="username_"/>
        <result property="avatar" column="avatar_"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_
        , user_id_, role_type_, group_id_, desc_, audit_status_, create_time_, update_time_
    </sql>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.yonge.cooleshow.biz.dal.entity.ImGroupMemberAudit">
        insert into im_group_member_audit(user_id_, role_type_, group_id_, desc_, audit_status_,
        create_time_, update_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.userId}, #{entity.roleType}, #{entity.groupId}, #{entity.desc},
            #{entity.auditStatus}, NOW(), NOW())
        </foreach>
    </insert>
    <update id="batchUpdateAuditStatus">
        UPDATE im_group_member_audit SET audit_status_ = #{auditStatus},update_time_ = NOW()
        WHERE FIND_IN_SET(id_,#{auditIds})
    </update>
    <delete id="delByGroupId">
        DELETE FROM im_group_member_audit WHERE group_id_ = #{groupId}
    </delete>
    <select id="findOne" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/> FROM im_group_member_audit
        <where>
            <if test="groupId != null">
                AND group_id_ = #{groupId}
            </if>
            <if test="userId != null">
                AND user_id_ = #{userId}
            </if>
            <if test="auditStatus != null and auditStatus != ''">
                AND audit_status_ = #{auditStatus}
            </if>
        </where>
        LIMIT 1
    </select>
    <select id="queryAll" resultMap="ImGroupMemberAuditDto">
        SELECT gma.*,su.avatar_,CASE WHEN gma.role_type_ = 'STUDENT' THEN su.username_ ELSE su.real_name_ END username_
        FROM im_group_member_audit gma
        LEFT JOIN sys_user su ON su.id_ = gma.user_id_
        <where>
            <if test="params.auditStatus != null and params.auditStatus != ''">
                AND gma.audit_status_ = #{params.auditStatus}
            </if>
            <if test="params.groupId != null">
                AND gma.group_id_ = #{params.groupId}
            </if>
            <if test="params.userId != null">
                AND gma.user_id_ = #{params.userId}
            </if>
        </where>
        ORDER BY gma.id_ DESC
    </select>
    <select id="findByIds" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/> FROM im_group_member_audit WHERE FIND_IN_SET(id_,#{auditIds})
    </select>

    <select id="selectAuditUserInfo" resultType="com.yonge.cooleshow.biz.dal.vo.AuditUserInfo">
        select distinct
            su.id_ as auditUserId,
            su.phone_ as auditUserPhone,
            ig.name_ as groupName,
            su2.id_ as groupUserId,
            su2.username_ as groupUsername
        from im_group_member_audit igma
        left join im_group ig on ig.id_ = igma.group_id_
        left join sys_user su on igma.user_id_ = su.id_
        left join sys_user su2 on ig.create_by_ = su2.id_
        where  igma.id_ in ( #{auditIds})
    </select>
</mapper>
