<?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.gic.haoban.manage.service.dao.mapper.NoticeMessageMapper">
  <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabNoticeMessage">
    <id column="notice_message_id" jdbcType="BIGINT" property="noticeMessageId"/>
    <result column="enterprise_id" jdbcType="VARCHAR" property="enterpriseId"/>
    <result column="store_id" jdbcType="VARCHAR" property="storeId"/>
    <result column="clerk_id" jdbcType="VARCHAR" property="clerkId"/>
    <result column="category_type" jdbcType="INTEGER" property="categoryType"/>
    <result column="message_type" jdbcType="INTEGER" property="messageType"/>
    <result column="template_code" jdbcType="VARCHAR" property="templateCode"/>
    <result column="title" jdbcType="VARCHAR" property="title"/>
    <result column="description" jdbcType="VARCHAR" property="description"/>
    <result column="relation_id" jdbcType="VARCHAR" property="relationId"/>
    <result column="message_content" jdbcType="VARCHAR" property="messageContent"/>
    <result column="read_flag" jdbcType="INTEGER" property="readFlag"/>
    <result column="delete_flag" jdbcType="INTEGER" property="deleteFlag"/>
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
  </resultMap>
  <sql id="Base_Column_List">
    notice_message_id, enterprise_id, store_id, clerk_id, category_type, message_type, 
    template_code, title, description, relation_id, message_content, read_flag, delete_flag, 
    create_time, update_time
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from tab_haoban_notice_message
    where notice_message_id = #{noticeMessageId,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from tab_haoban_notice_message
    where notice_message_id = #{noticeMessageId,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.TabNoticeMessage">
    insert into tab_haoban_notice_message (notice_message_id, enterprise_id, store_id, 
      clerk_id, category_type, message_type, 
      template_code, title, description, 
      relation_id, message_content, read_flag, 
      delete_flag, create_time, update_time
      )
    values (#{noticeMessageId,jdbcType=BIGINT}, #{enterpriseId,jdbcType=VARCHAR}, #{storeId,jdbcType=VARCHAR}, 
      #{clerkId,jdbcType=VARCHAR}, #{categoryType,jdbcType=INTEGER}, #{messageType,jdbcType=INTEGER}, 
      #{templateCode,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, 
      #{relationId,jdbcType=VARCHAR}, #{messageContent,jdbcType=VARCHAR}, #{readFlag,jdbcType=INTEGER}, 
      #{deleteFlag,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}
      )
  </insert>
  <insert id="insertBatch">
    insert into tab_haoban_notice_message (notice_message_id, enterprise_id, store_id,
    clerk_id, category_type, message_type,
    template_code, title, description,
    relation_id, message_content, create_time, update_time
    )
    values
    <foreach collection="records" item="record" separator=",">
      (#{record.noticeMessageId,jdbcType=BIGINT}, #{record.enterpriseId,jdbcType=VARCHAR}, #{record.storeId,jdbcType=VARCHAR},
      #{record.clerkId,jdbcType=VARCHAR}, #{record.categoryType,jdbcType=INTEGER}, #{record.messageType,jdbcType=INTEGER},
      #{record.templateCode,jdbcType=VARCHAR}, #{record.title,jdbcType=VARCHAR}, #{record.description,jdbcType=VARCHAR},
      #{record.relationId,jdbcType=VARCHAR}, #{record.messageContent,jdbcType=VARCHAR}, #{record.createTime,jdbcType=TIMESTAMP}, #{record.updateTime,jdbcType=TIMESTAMP}
      )
    </foreach>
  </insert>
  <insert id="insertSelective" parameterType="com.gic.haoban.manage.service.entity.TabNoticeMessage">
    insert into tab_haoban_notice_message
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="noticeMessageId != null">
        notice_message_id,
      </if>
      <if test="enterpriseId != null">
        enterprise_id,
      </if>
      <if test="storeId != null">
        store_id,
      </if>
      <if test="clerkId != null">
        clerk_id,
      </if>
      <if test="categoryType != null">
        category_type,
      </if>
      <if test="messageType != null">
        message_type,
      </if>
      <if test="templateCode != null">
        template_code,
      </if>
      <if test="title != null">
        title,
      </if>
      <if test="description != null">
        description,
      </if>
      <if test="relationId != null">
        relation_id,
      </if>
      <if test="messageContent != null">
        message_content,
      </if>
      <if test="readFlag != null">
        read_flag,
      </if>
      <if test="deleteFlag != null">
        delete_flag,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="noticeMessageId != null">
        #{noticeMessageId,jdbcType=BIGINT},
      </if>
      <if test="enterpriseId != null">
        #{enterpriseId,jdbcType=VARCHAR},
      </if>
      <if test="storeId != null">
        #{storeId,jdbcType=VARCHAR},
      </if>
      <if test="clerkId != null">
        #{clerkId,jdbcType=VARCHAR},
      </if>
      <if test="categoryType != null">
        #{categoryType,jdbcType=INTEGER},
      </if>
      <if test="messageType != null">
        #{messageType,jdbcType=INTEGER},
      </if>
      <if test="templateCode != null">
        #{templateCode,jdbcType=VARCHAR},
      </if>
      <if test="title != null">
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="description != null">
        #{description,jdbcType=VARCHAR},
      </if>
      <if test="relationId != null">
        #{relationId,jdbcType=VARCHAR},
      </if>
      <if test="messageContent != null">
        #{messageContent,jdbcType=VARCHAR},
      </if>
      <if test="readFlag != null">
        #{readFlag,jdbcType=INTEGER},
      </if>
      <if test="deleteFlag != null">
        #{deleteFlag,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.gic.haoban.manage.service.entity.TabNoticeMessage">
    update tab_haoban_notice_message
    <set>
      <if test="enterpriseId != null">
        enterprise_id = #{enterpriseId,jdbcType=VARCHAR},
      </if>
      <if test="storeId != null">
        store_id = #{storeId,jdbcType=VARCHAR},
      </if>
      <if test="clerkId != null">
        clerk_id = #{clerkId,jdbcType=VARCHAR},
      </if>
      <if test="categoryType != null">
        category_type = #{categoryType,jdbcType=INTEGER},
      </if>
      <if test="messageType != null">
        message_type = #{messageType,jdbcType=INTEGER},
      </if>
      <if test="templateCode != null">
        template_code = #{templateCode,jdbcType=VARCHAR},
      </if>
      <if test="title != null">
        title = #{title,jdbcType=VARCHAR},
      </if>
      <if test="description != null">
        description = #{description,jdbcType=VARCHAR},
      </if>
      <if test="relationId != null">
        relation_id = #{relationId,jdbcType=VARCHAR},
      </if>
      <if test="messageContent != null">
        message_content = #{messageContent,jdbcType=VARCHAR},
      </if>
      <if test="readFlag != null">
        read_flag = #{readFlag,jdbcType=INTEGER},
      </if>
      <if test="deleteFlag != null">
        delete_flag = #{deleteFlag,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where notice_message_id = #{noticeMessageId,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.gic.haoban.manage.service.entity.TabNoticeMessage">
    update tab_haoban_notice_message
    set enterprise_id = #{enterpriseId,jdbcType=VARCHAR},
      store_id = #{storeId,jdbcType=VARCHAR},
      clerk_id = #{clerkId,jdbcType=VARCHAR},
      category_type = #{categoryType,jdbcType=INTEGER},
      message_type = #{messageType,jdbcType=INTEGER},
      template_code = #{templateCode,jdbcType=VARCHAR},
      title = #{title,jdbcType=VARCHAR},
      description = #{description,jdbcType=VARCHAR},
      relation_id = #{relationId,jdbcType=VARCHAR},
      message_content = #{messageContent,jdbcType=VARCHAR},
      read_flag = #{readFlag,jdbcType=INTEGER},
      delete_flag = #{deleteFlag,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where notice_message_id = #{noticeMessageId,jdbcType=BIGINT}
  </update>

  <select id="listNoticeMessage" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from tab_haoban_notice_message
    where enterprise_id = #{enterpriseId}
    and store_id = #{storeId}
    and delete_flag=0
    <if test="clerkId != null">
      and clerk_id =#{clerkId}
    </if>
    <if test="categoryType != -1">
      and category_type =#{categoryType}
    </if>
    and create_time > DATE_SUB(now(),INTERVAL 30 DAY)
    order by create_time desc
  </select>

  <select id="countUnreadNoticeMessage" resultType="Integer">
    select
    count(*)
    from tab_haoban_notice_message
    where enterprise_id = #{enterpriseId}
    and read_flag=0
    and delete_flag=0
    and store_id = #{storeId}
    <if test="clerkId != null">
      and clerk_id =#{clerkId}
    </if>
    <if test="categoryType != -1">
      and category_type =#{categoryType}
    </if>
    and create_time > DATE_SUB(now(),INTERVAL 30 DAY)
    order by create_time desc
  </select>

  <update id="updateNoticeMessageReadFlag">
    update tab_haoban_notice_message
    set read_flag=1,update_time = now()
    WHERE enterprise_id = #{enterpriseId}
    and clerk_id = #{clerkId}
    and read_flag=0
    and delete_flag=0
    <if test="categoryType!=-1">
      and category_type=#{categoryType}
    </if>
    and create_time > DATE_SUB(now(),INTERVAL 30 DAY)
  </update>
</mapper>