<?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="cn.reghao.tnb.content.app.chat.db.mapper.GroupInfoMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into chat_group_info
        (`id`,`deleted`,`create_time`,`update_time`,`group_id`,`name`,`avatar`,`profile`,`owner_id`)
        values 
        (#{id},#{deleted},#{createTime},#{updateTime},#{groupId},#{name},#{avatar},#{profile},#{ownerId})
    </insert>

    <update id="updateGroupInfo">
        update chat_group_info set update_time=now(),name=#{name},avatar=#{avatar},profile=#{profile}
        where group_id=#{groupId}
    </update>

    <select id="findByGroupId" resultType="cn.reghao.tnb.content.app.chat.model.po.GroupInfo">
        select * from chat_group_info where id=#{groupId}
    </select>
    <select id="findDetailByGroupId" resultType="cn.reghao.tnb.content.app.chat.model.dto.group.GroupDetailRet">
        select groupInfo.id as groupId,groupInfo.name as groupName,groupInfo.avatar,groupInfo.profile,
        groupInfo.owner_id as ownerId,date_format(groupInfo.create_time,'%Y-%m-%d %H:%i:%s') as createdAt
        from chat_group_info groupInfo
        where groupInfo.id=#{groupId}
    </select>
    <select id="findGroupsByUserId" resultType="cn.reghao.tnb.content.app.chat.model.dto.group.GroupInfoRet">
        select groupInfo.id,groupInfo.name as groupName,groupInfo.profile as groupProfile,groupInfo.avatar,
        groupMember.owner,groupMember.disturb
        from chat_group_info groupInfo
        inner join group_member groupMember
        on groupInfo.id=groupMember.group_id and groupMember.user_id=#{userId}
    </select>
    <select id="findDialogGroupInfo" resultType="cn.reghao.tnb.content.app.chat.model.dto.ChatUserInfo">
        select group_id as receiverId,`name`,avatar
        from chat_group_info
        where group_id=#{groupId}
    </select>
</mapper>
