<?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.pcloud.book.applet.dao.impl.AppletNewsCategoryDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletNewsCategory">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="applet_news_id" property="appletNewId" jdbcType="BIGINT"/>
        <result column="creator" property="creator" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="first_classify" property="firstClassify" jdbcType="BIGINT"/>
        <result column="second_classify" property="secondClassify" jdbcType="BIGINT"/>
        <result column="grade_label_id" property="gradeLabelId" jdbcType="BIGINT"/>
        <result column="subject_label_id" property="subjectLabelId" jdbcType="BIGINT"/>
    </resultMap>

    <sql id="Base_Column_List">
    id, applet_news_id, creator, create_time, first_classify,second_classify, grade_label_id, subject_label_id
  </sql>

    <insert id="insert" parameterType="com.pcloud.book.applet.entity.AppletNewsCategory" useGeneratedKeys="true"
            keyProperty="id">
    insert into applet_news_category (
       applet_news_id, creator, create_time, first_classify,second_classify, grade_label_id, subject_label_id
      )
    values (
      #{appletNewId}, #{creator}, NOW(), #{firstClassify}, #{secondClassify}, #{gradeLabelId}, #{subjectLabelId}
      )
  </insert>

    <insert id="batchCreate" parameterType="list" keyProperty="id" useGeneratedKeys="true">
        insert into applet_news_category (
        applet_news_id, creator, create_time, first_classify,second_classify, grade_label_id, subject_label_id)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            ( #{item.appletNewId}, #{item.creator}, NOW(), #{item.firstClassify}, #{item.secondClassify},
            #{item.gradeLabelId}, #{item.subjectLabelId} )
        </foreach>
    </insert>

    <update id="update" parameterType="com.pcloud.book.applet.entity.AppletNewsCategory">
        update applet_news_category
        <set>
            <if test="firstClassify != null">
                first_classify = #{firstClassify,jdbcType=BIGINT},
            </if>
            <if test="secondClassify != null">
                second_classify = #{secondClassify,jdbcType=BIGINT},
            </if>
            <if test="gradeLabelId != null">
                grade_label_id = #{gradeLabelId,jdbcType=BIGINT},
            </if>
            <if test="subjectLabelId != null">
                subject_label_id = #{subjectLabelId,jdbcType=BIGINT},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <select id="getByNewsId" parameterType="long" resultType="com.pcloud.book.applet.dto.AppletNewsCategoryDTO">
        select
        category.id,
        category.applet_news_id,
        category.first_classify firstClassify,
        category.second_classify secondClassify,
        category.grade_label_id gradeLabelId,
        category.subject_label_id subjectLabelId,
        label.name gradeLabelName,
        label2.name subjectLabelName
        from applet_news_category category
        LEFT JOIN  book_label label ON category.grade_label_id = label.id AND label.type=1
        LEFT JOIN  book_label label2 ON category.subject_label_id = label2.id AND label2.type=2
        where applet_news_id=#{appletNewId}
    </select>

    <delete id="deleteById" parameterType="long">
    delete from applet_news_category
    where id=#{id}
  </delete>

    <delete id="deletebyNewsId" parameterType="long">
        DELETE FROM applet_news_category
        WHERE applet_news_id = #{appletNewId}
    </delete>

</mapper>