<?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.huiju.estateagents.property.dao.TpAnnouncementMapper" >
  <sql id="Base_Column_List" >
    id, org_id, announcement_title, announcement_carousel_img, announcement_content,
    sort, view_count, set_up_carousel, status, create_user, create_date, update_user,
    update_date
  </sql>
  <select id="getAnnouncements" resultType="com.huiju.estateagents.property.model.TpAnnouncement">
    SELECT *
   FROM
      tp_announcement
      <where>
        community_id = #{record.communityId}
        <if test="record.id != null" >
          AND id like concat('%',#{record.id,jdbcType=VARCHAR},'%')
        </if>
        <if test="record.announcementTitle != null and record.announcementTitle != ''" >
          AND announcement_title like concat('%',#{record.announcementTitle,jdbcType=VARCHAR},'%')
        </if>
        <if test="record.annType != null and record.annType != ''" >
          AND ann_type = #{record.annType,jdbcType=VARCHAR}
        </if>
        <if test="record.status != null and record.status != ''" >
          AND status = #{record.status,jdbcType=VARCHAR}
        </if>
        <if test="record.startCreateDate != null and record.startCreateDate != ''">
          AND DATE_FORMAT(create_date, '%Y-%m-%d') &gt;= #{record.startCreateDate}
        </if>
        <if test="record.endCreateDate != null and record.endCreateDate != ''">
          AND DATE_FORMAT(create_date, '%Y-%m-%d') &lt;= #{record.endCreateDate}
        </if>
          AND org_id = #{record.orgId,jdbcType=INTEGER}
          ORDER BY
        <if test="record.sortKey != null and record.sortKey != ''">
          ${record.sortKey} ${record.sortOrder},
        </if>
        sort DESC,create_date desc
      </where>
  </select>
  <select id="getById" resultType="com.huiju.estateagents.property.model.TpAnnouncement">
    SELECT
    id, org_id, announcement_title, announcement_carousel_img, announcement_content,
    sort, view_count, set_up_carousel, status, create_user, create_date, update_user,
    update_date
    FROM
    tp_announcement
    WHERE org_id = #{record.orgId,jdbcType=INTEGER}
  </select>
  <update id="updateTpAnnouncement" parameterType="com.huiju.estateagents.property.model.TpAnnouncement" >
    update tp_announcement
    <set >
        ann_type = #{annType},
      <if test="announcementTitle != null" >
        announcement_title = #{announcementTitle,jdbcType=VARCHAR},
      </if>
      <if test="announcementCarouselImg != null and announcementCarouselImg !=''">
        announcement_carousel_img = #{announcementCarouselImg,jdbcType=VARCHAR},
      </if>
      <if test="announcementContent != null" >
        announcement_content = #{announcementContent,jdbcType=LONGVARCHAR},
      </if>
      <if test="sort != null" >
        sort = #{sort,jdbcType=INTEGER},
      </if>
      <if test="viewCount != null" >
        view_count = #{viewCount,jdbcType=INTEGER},
      </if>
      <if test="setUpCarousel != null" >
        set_up_carousel = #{setUpCarousel,jdbcType=CHAR},
      </if>
      <if test="status != null" >
        status = #{status,jdbcType=CHAR},
      </if>
      <if test="createUser != null" >
        create_user = #{createUser,jdbcType=INTEGER},
      </if>
      <if test="createDate != null" >
        create_date = #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null" >
        update_user = #{updateUser,jdbcType=INTEGER},
      </if>
      <if test="updateDate != null" >
        update_date = #{updateDate,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>


  <select id="getWxAnnouncements" resultType="com.huiju.estateagents.property.model.TpAnnouncement">
    SELECT
    a.id,
    a.announcement_title,
    a.announcement_content,
    a.create_date,
    u.user_name as user_name,
    a.sort,
    a.view_count
    FROM
    tp_announcement a
    LEFT JOIN ta_user u ON a.create_user = u.user_id
    AND u.org_id = #{orgId,jdbcType=INTEGER}
    WHERE
    a.org_id = #{orgId,jdbcType=INTEGER}
    AND ( a.STATUS = 1 OR a.STATUS = 3 )
    <if test="title != null and title != ''">
      and a.announcement_title like concat('%',#{title,jdbcType=VARCHAR},'%')
    </if>
    <if test="annType != null and annType != ''">
      and a.ann_type = #{annType}
    </if>
    <if test="communityId != null and communityId != ''">
      and a.community_id = #{communityId}
    </if>
    ORDER BY
    a.sort DESC,
    a.update_date DESC,
    a.create_date DESC
  </select>

  <update id="updateByPrimaryKeySelective" >
    update tp_announcement
    <set >
      <if test="viewCount != null" >
        view_count = #{viewCount,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="selectByPrimaryKey" resultType="com.huiju.estateagents.property.model.TpAnnouncement">
    select
    <include refid="Base_Column_List" />
    from tp_announcement
    where id = #{id,jdbcType=INTEGER} and org_id = #{orgId,jdbcType=INTEGER}
  </select>
</mapper>