<?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.dgtis.dorm.manager.db.dao.NewsOrganizeMapper">
  <resultMap id="BaseResultMap" type="com.dgtis.dorm.manager.db.domain.NewsOrganize">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="fk_news_id" jdbcType="INTEGER" property="newsId" />
    <result column="fk_organize_id" jdbcType="INTEGER" property="organizeId" />
    <result column="fk_user_id" jdbcType="INTEGER" property="userId" />
    <result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
    <result column="from_type" jdbcType="VARCHAR" property="fromType" />
    <result column="organizeName" jdbcType="VARCHAR" property="organizeName" />
    <result column="activityName" jdbcType="VARCHAR" property="activityName" />
  </resultMap>

  <sql id="Base_Column_List">
    au.id, au.fk_news_id, au.fk_user_id, au.fk_organize_id, au.create_date, au.from_type, o.organize_name AS organizeName, n.news_title AS activityName
  </sql>

  <select id="selectByExample" parameterType="com.dgtis.dorm.manager.db.domain.NewsOrganize" resultMap="BaseResultMap">
    select distinct
    <include refid="Base_Column_List" />
    from zhll_news_organize au
    left join zhll_organize o
    on au.fk_organize_id = o.id
    left join zhll_news n
    on au.fk_news_id = n.id
    <where>
      <if test="newsId != null and newsId != ''">
        au.fk_news_id = #{newsId}
      </if>
      <if test="organizeId != null and organizeId != ''">
        au.fk_organize_id = #{organizeId}
      </if>
    </where>
  </select>

  <select id="countByExample" parameterType="com.dgtis.dorm.manager.db.domain.NewsOrganize" resultType="java.lang.Long">
    select count(*) from zhll_news_organize
    <where>
      <if test="newsId != null and newsId != ''">
        fk_news_id = #{newsId}
      </if>
      <if test="userId != null and userId != ''">
        fk_user_id = #{userId}
      </if>
    </where>
  </select>

  <insert id="insertSelective" parameterType="com.dgtis.dorm.manager.db.domain.NewsOrganize">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into zhll_news_organize
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="newsId != null">
        fk_news_id,
      </if>
      <if test="organizeId != null">
        fk_organize_id,
      </if>
      <if test="userId != null">
        fk_user_id,
      </if>
      <if test="createDate != null">
        create_date,
      </if>
      <if test="fromType != null">
        from_type
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="newsId != null">
        #{newsId,jdbcType=INTEGER},
      </if>
      <if test="organizeId != null">
        #{organizeId,jdbcType=INTEGER},
      </if>
      <if test="userId != null">
        #{userId,jdbcType=INTEGER},
      </if>
      <if test="createDate != null">
        #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="fromType != null">
        #{fromType, jdbcType=VARCHAR}
      </if>
    </trim>
  </insert>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from zhll_news_organize
    where id = #{id,jdbcType=INTEGER}
  </delete>

  <delete id="deleteByActivityId" parameterType="java.lang.Integer">
    delete from zhll_news_organize
    where fk_news_id = #{newsId,jdbcType=INTEGER}
  </delete>

  <delete id="deleteOrganizeNewsByActivity" parameterType="java.lang.String">
    delete from zhll_news_organize
    where fk_news_id = #{newsId} and fk_organize_id = #{organizeId}
  </delete>

</mapper>