<?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.NewsZanMapper">
  <resultMap id="BaseResultMap" type="com.dgtis.dorm.manager.db.domain.NewsZan">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="fk_user_id" jdbcType="INTEGER" property="userId" />
    <result column="fk_news_id" jdbcType="INTEGER" property="newsId" />
    <result column="deleted" jdbcType="BIT" property="deleted" />

    <result column="userName" jdbcType="VARCHAR" property="userName" />
    <result column="photo" jdbcType="VARCHAR" property="photo" />
  </resultMap>

  <sql id="Base_Column_List">
    zhll_news_zan.id, zhll_news_zan.fk_user_id, zhll_news_zan.fk_news_id, zhll_news_zan.deleted
  </sql>

  <select id="selectByExample" parameterType="com.dgtis.dorm.manager.db.domain.NewsZan" resultMap="BaseResultMap">
    select distinct
    <include refid="Base_Column_List" />
    , au.user_name as userName, au.photo
    from zhll_news_zan
    left join zhll_user au
    on au.id = zhll_news_zan.fk_user_id
    <where>
      <if test="userId != 0">
          and zhll_news_zan.fk_user_id = #{userId }
      </if>
      <if test="newsId != 0">
          and zhll_news_zan.fk_news_id = #{newsId }
      </if>
    </where>
  </select>

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

  <insert id="insertSelective" parameterType="com.dgtis.dorm.manager.db.domain.NewsZan">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into zhll_news_zan
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="userId != null">
          fk_user_id,
      </if>
      <if test="newsId != null">
          fk_news_id,
      </if>
      <if test="deleted != null">
        deleted,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="userId != null">
        #{userId,jdbcType=VARCHAR},
      </if>
      <if test="newsId != null">
        #{newsId,jdbcType=INTEGER},
      </if>
      <if test="deleted != null">
        #{deleted,jdbcType=BIT},
      </if>
    </trim>
  </insert>

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

  <delete id="deleteByExample" parameterType="com.dgtis.dorm.manager.db.domain.NewsZan">
    delete from zhll_news_zan
    where fk_news_id = #{newsId,jdbcType=INTEGER} and fk_user_id = #{userId, jdbcType=INTEGER}
  </delete>

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