<?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.zcxk.dao.MessageMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.entity.Message" id="MessageMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="messageId" column="message_id" jdbcType="VARCHAR"/>
        <result property="messageType" column="message_type" jdbcType="INTEGER"/>
        <result property="messageContent" column="message_content" jdbcType="VARCHAR"/>
        <result property="messageTemplateId" column="message_template_id" jdbcType="INTEGER"/>
        <result property="url" column="url" jdbcType="VARCHAR"/>
        <result property="messageStatus" column="message_status" jdbcType="INTEGER"/>
        <result property="userId" column="user_id" jdbcType="INTEGER"/>
        <result property="channel" column="channel" jdbcType="INTEGER"/>
        <result property="title" column="title" jdbcType="VARCHAR"/>
        <result property="tenantId" column="tenant_id" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="dateCreate" column="date_create" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="uniqueFlag" column="unique_flag" jdbcType="INTEGER"/>
        <result property="dateUpdate" column="date_update" jdbcType="TIMESTAMP"/>
        <result property="appId" column="app_id" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        a.id, message_id, message_type, message_content, message_template_id, url, message_status, user_id, channel, title, tenant_id, a.status, a.date_create, a.create_by, a.update_by, unique_flag, a.date_update    </sql>

    <!-- 查询单个 -->
    <select id="selectById" resultMap="MessageMap">
        select
        <include refid="Base_Column_List"/>
        from uims_message
        where id = #{id}
    </select>


    <!-- 查询全部 -->
    <select id="selectAll" resultMap="MessageMap">
        select
        <include refid="Base_Column_List"/>
        from uims_message
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultMap="MessageMap">
        select
        <include refid="Base_Column_List"/>
        from uims_message a
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="messageId != null and messageId != ''">
                and message_id = #{messageId}
            </if>
            <if test="messageType != null">
                and message_type = #{messageType}
            </if>
            <if test="messageContent != null and messageContent != ''">
                and message_content = #{messageContent}
            </if>
            <if test="messageTemplateId != null">
                and message_template_id = #{messageTemplateId}
            </if>
            <if test="url != null and url != ''">
                and url = #{url}
            </if>
            <if test="messageStatus != null">
                and message_status = #{messageStatus}
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="channel != null">
                and channel = #{channel}
            </if>
            <if test="title != null and title != ''">
                and title = #{title}
            </if>
            <if test="tenantId != null and tenantId != ''">
                and tenant_id = #{tenantId}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="dateCreate != null">
                and date_create = #{dateCreate}
            </if>
            <if test="createBy != null and createBy != ''">
                and create_by = #{createBy}
            </if>
            <if test="updateBy != null and updateBy != ''">
                and update_by = #{updateBy}
            </if>
            <if test="uniqueFlag != null">
                and unique_flag = #{uniqueFlag}
            </if>
            <if test="dateUpdate != null">
                and date_update = #{dateUpdate}
            </if>
        </where>
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into uims_message(id, message_id, message_type, message_content, message_template_id, url, message_status, user_id, channel, title, tenant_id, status, date_create, create_by, update_by, unique_flag, date_update)
        values ( #{id}, #{messageId}, #{messageType}, #{messageContent}, #{messageTemplateId}, #{url}, #{messageStatus}, #{userId}, #{channel}, #{title}, #{tenantId}, #{status}, #{dateCreate}, #{createBy}, #{updateBy}, #{uniqueFlag}, #{dateUpdate})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into uims_message(id, message_id, message_type, message_content, message_template_id, url,
        message_status, user_id, channel, title, tenant_id, status, date_create, create_by, update_by, unique_flag,
        date_update)
        values
        <foreach collection="messages" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.messageId}, #{item.messageType}, #{item.messageContent}, #{item.messageTemplateId},
            #{item.url}, #{item.messageStatus}, #{item.userId}, #{item.channel}, #{item.title}, #{item.tenantId},
            #{item.status}, #{item.dateCreate}, #{item.createBy}, #{item.updateBy}, #{item.uniqueFlag},
            #{item.dateUpdate} )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update uims_message.uims_message
        <set>
            <if test="messageId != null and messageId != ''">
                message_id = #{messageId},
            </if>
            <if test="messageType != null">
                message_type = #{messageType},
            </if>
            <if test="messageContent != null and messageContent != ''">
                message_content = #{messageContent},
            </if>
            <if test="messageTemplateId != null">
                message_template_id = #{messageTemplateId},
            </if>
            <if test="url != null and url != ''">
                url = #{url},
            </if>
            <if test="messageStatus != null">
                message_status = #{messageStatus},
            </if>
            <if test="userId != null">
                user_id = #{userId},
            </if>
            <if test="channel != null">
                channel = #{channel},
            </if>
            <if test="title != null and title != ''">
                title = #{title},
            </if>
            <if test="tenantId != null and tenantId != ''">
                tenant_id = #{tenantId},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="dateCreate != null">
                date_create = #{dateCreate},
            </if>
            <if test="createBy != null and createBy != ''">
                create_by = #{createBy},
            </if>
            <if test="updateBy != null and updateBy != ''">
                update_by = #{updateBy},
            </if>
            <if test="uniqueFlag != null">
                unique_flag = #{uniqueFlag},
            </if>
            <if test="dateUpdate != null">
                date_update = #{dateUpdate},
            </if>
        </set>
        where id = #{id}
    </update>


    <!--通过主键删除-->
    <delete id="deleteById">
        delete from uims_message where id = #{id}
    </delete>

    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*) from uims_message
    </select>
    <select id="selectPage" resultMap="MessageMap">
        select
        <include refid="Base_Column_List"/>,b.type typeName,b.path,b.id typeId,b.app_id
        from uims_message a join uims_message_type b on a.message_type=b.id
        <where>
             a.status=1
            <if test="message.id != null">
                and id = #{message.id}
            </if>
            <if test="message.messageId != null and message.messageId != ''">
                and message_id = #{message.messageId}
            </if>
            <if test="message.messageType != null">
                and message_type = #{message.messageType}
            </if>
            <if test="message.messageContent != null and message.messageContent != ''">
                and message_content = #{message.messageContent}
            </if>
            <if test="message.messageTemplateId != null">
                and message_template_id = #{message.messageTemplateId}
            </if>
            <if test="message.url != null and message.url != ''">
                and url = #{message.url}
            </if>
            <if test="message.messageStatus != null">
                and message_status = #{message.messageStatus}
            </if>
            <if test="message.userId != null">
                and user_id = #{message.userId}
            </if>
            <if test="message.channel != null">
                and channel = #{message.channel}
            </if>
            <if test="message.title != null and message.title != ''">
                and title = #{message.title}
            </if>
            <if test="message.tenantId != null and message.tenantId != ''">
                and tenant_id = #{message.tenantId}
            </if>

            <if test="message.dateCreate != null">
                and date_create = #{message.dateCreate}
            </if>
            <if test="message.createBy != null and message.createBy != ''">
                and create_by = #{message.createBy}
            </if>
            <if test="message.updateBy != null and message.updateBy != ''">
                and update_by = #{message.updateBy}
            </if>
            <if test="message.uniqueFlag != null">
                and unique_flag = #{message.uniqueFlag}
            </if>
            <if test="message.dateUpdate != null">
                and date_update = #{message.dateUpdate}
            </if>
        </where>
        order by a.date_create desc
    </select>
    <select id="queryLastMessage" resultType="com.zcxk.entity.Message">
        select a.date_create,a.message_content,b.short_name,b.path,b.id typeId,a.url,
               b.type typeName,b.app_id
        from uims_message a
            join uims_message_type b
         on a.message_type=b.id
         where user_id=#{id} and a.status=1
        <if test="type!= null">
            and message_status =0
        </if>
        order by date_create desc limit #{num}
    </select>
    <select id="queryUnreadMessageStatic" resultType="com.zcxk.Dto.MessageStatic">
         select a.typeId,b.type,a.cn from (select message_type typeId ,
         count(*)cn  from
         uims_message where user_id=#{id} and message_status=0 and status=1
        group by message_type) a join uims_message_type b on a.typeId=b.id
    </select>
    <select id="getUserMessageTypeByApp" resultType="com.zcxk.Dto.MessageStatic">
        select id typeId,0 cn,type from uims_message_type where status=1 and app_id in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        order by seq
    </select>
    <update id="updateMultiple">
        update uims_message
        <set>
            <if test="status!= null">
                status =#{status},
            </if>
            <if test="messageStatus != null">
                message_status =#{messageStatus},
            </if>
        </set>

        where  user_id =#{userId} and status=1

        <if test="ids != null">
           and id in
            <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>


    </update>
</mapper>