<?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.tmerclub.cloud.multishop.mapper.NoticeMapper">
  <resultMap id="noticeMap" type="com.tmerclub.cloud.multishop.model.Notice">
    <id column="id" property="id" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="shop_id" property="shopId"/>
    <result column="title" property="title"/>
    <result column="content" property="content"/>
    <result column="types" property="types"/>
    <result column="status" property="status"/>
    <result column="is_top" property="isTop"/>
    <result column="publish_time" property="publishTime"/>
    <result column="is_learning" property="isLearning"/>
    <result column="immediately_send" property="immediatelySend"/>
    <result column="send_time" property="sendTime"/>
    <result column="multi_shop_ids" property="multiShopIds"/>
    <result column="user_ids" property="userIds"/>
    <result column="supplier_ids" property="supplierIds"/>
  </resultMap>
  <select id="list" resultType="com.tmerclub.cloud.multishop.vo.NoticeVO">
    select `id`,`title`,`content`,`status`,`is_top`,`types`,IFNULL(send_time, publish_time) as publishTime,`is_learning`,  immediately_send, send_time, multi_shop_ids,  user_ids, supplier_ids from notice
    <where>
        <!-- 平台端查询 -->
        <if test="notice.accountId == 0">
          <if test="notice.type != null  ">
            FIND_IN_SET (#{notice.type}, `types`)
          </if>
        </if>
      <!-- 非平台端查询 -->
        <if test="notice.accountId != 0">
          <if test="notice.type != null  ">
            (immediately_send = 1 or (immediately_send = 0 and send_time &lt;= #{notice.sendTime} ))
            and  FIND_IN_SET (#{notice.type}, `types`)
          </if>

          <!-- 用户端(查询包括指定用户可见) -->
          <if test="notice.accountId != null and notice.type == 2">
            and (FIND_IN_SET (#{notice.accountId}, `user_ids`) or `user_ids` is null  )
          </if>
          <!-- 商家端(查询包括指定商家可见) -->
          <if test="notice.accountId != null and notice.type == 1">
            and  ( FIND_IN_SET (#{notice.accountId}, `multi_shop_ids`) or `multi_shop_ids` is null )
          </if>
          <!-- 供应商端(查询包括指定供应商可见) -->
          <if test="notice.accountId != null and notice.type == 3">
            and ( FIND_IN_SET (#{notice.accountId}, `supplier_ids`) or `supplier_ids` is null )
          </if>
          <if test="notice.accountId == null and notice.type == 2">
            and  `user_ids` is null
          </if>
          <if test="notice.accountId == null and notice.type == 1">
            and  multi_shop_ids` is null
          </if>
          <if test="notice.accountId == null and notice.type == 3">
            and  `supplier_ids` is null
          </if>
        </if>

      <if test="notice.shopId != null">
        and `shop_id` = #{notice.shopId}
      </if>
      <if test="notice.isTop != null">
        and `is_top` = #{notice.isTop}
      </if>
      <if test="notice.status != null">
        and `status` = #{notice.status}
      </if>
      <if test="notice.title != null and notice.title != ''">
        and `title` like CONCAT('%',#{notice.title},'%')
      </if>
      <if test="notice.startTime != null">
        and `publish_time` &gt;= #{notice.startTime}
      </if>
      <if test="notice.endTime != null">
        and `publish_time` &lt;= #{notice.endTime}
      </if>
    </where>
    order by is_top desc, publish_time desc
  </select>
  <select id="getById" resultType="com.tmerclub.cloud.multishop.vo.NoticeVO">
    select `id`,`shop_id`,`title`,`content`,`status`,`is_top`,`types`,IFNULL(send_time, publish_time) as publishTime,`is_learning`,  immediately_send, send_time, `multi_shop_ids`, `user_ids`, `supplier_ids` from notice where id = #{id}
  </select>
  <select id="listAllByTypeAndShopId" resultType="com.tmerclub.cloud.multishop.vo.NoticeVO">
    select `id`,`title`,`content`,`status`,`is_top`,`types`,IFNULL(send_time, publish_time) as publishTime,`is_learning`,  immediately_send, send_time, multi_shop_ids,  user_ids, supplier_ids from notice
    <where>

    <if test="notice.type != null  ">
      and FIND_IN_SET (#{notice.type}, `types`)
    </if>
      <if test="notice.shopId != null">
        and `shop_id` = #{notice.shopId}
      </if>
      <if test="notice.isTop != null">
        and `is_top` = #{notice.isTop}
      </if>
      <if test="notice.status != null">
        and `status` = #{notice.status}
      </if>
      <if test="notice.title != null and notice.title != ''">
        and `title` like CONCAT('%',#{notice.title},'%')
      </if>
    </where>
    order by is_top desc, publish_time desc
  </select>
  <update id="updateIsLearning">
    update notice
    <set>
      `is_learning` = 1
    </set>
    where id = #{id}
  </update>
  <insert id="save">
    insert into notice (`shop_id`,`title`,`content`,`types`,`status`,`is_top`,`publish_time`,  `immediately_send`, `send_time`, `multi_shop_ids`, `user_ids`, `supplier_ids`)
    values (#{notice.shopId}, #{notice.title}, #{notice.content}, #{notice.types}, #{notice.status}, #{notice.isTop}, #{notice.publishTime},  #{notice.immediatelySend}, #{notice.sendTime}, #{notice.multiShopIds}, #{notice.userIds}, #{notice.supplierIds});
  </insert>
  <insert id="saveBatch">
    insert into notice (`shop_id`,`title`,`content`,`types`,`status`,`is_top`,`publish_time`,  `immediately_send`, `send_time`)
    values
    <foreach collection="noticeList" item="notice" separator=",">
      (#{notice.shopId}, #{notice.title}, #{notice.content}, #{notice.type}, #{notice.status}, #{notice.isTop}, #{notice.publishTime},  #{notice.immediatelySend}, #{notice.sendTime})
    </foreach>
  </insert>
  <update id="update">
    update notice
    <set>
      <if test="notice.title != null">
        `title` = #{notice.title},
      </if>
      <if test="notice.content != null">
        `content` = #{notice.content},
      </if>
      <if test="notice.types != null and notice.types != ''">
        `types` = #{notice.types},
      </if>
      <if test="notice.status != null">
        `status` = #{notice.status},
      </if>
      <if test="notice.immediatelySend != null">
        `immediately_send` = #{notice.immediatelySend},
      </if>
      <if test="notice.sendTime != null">
        `send_time` = #{notice.sendTime},
      </if>
      <if test="notice.isTop != null">
        `is_top` = #{notice.isTop},
      </if>
      <if test="notice.publishTime != null">
        `publish_time` = #{notice.publishTime},
      </if>

        `multi_shop_ids` = #{notice.multiShopIds},


        `supplier_ids` = #{notice.supplierIds},

        `user_ids` = #{notice.userIds}

    </set>
    where id = #{notice.id}
  </update>
  <delete id="deleteById">
    delete from notice where id = #{id}
  </delete>

</mapper>
