<?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.giftcoupon.dao.impl.GiftCategoryDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.giftcoupon.entity.GiftCategory">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="gift_package_id" property="giftPackageId" 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, gift_package_id, creator, create_time, first_classify,second_classify, grade_label_id, subject_label_id
  </sql>

    <insert id="insert" parameterType="com.pcloud.book.giftcoupon.entity.GiftCategory" useGeneratedKeys="true"
            keyProperty="id">
    insert into gift_category (
       gift_package_id, creator, create_time, first_classify,second_classify, grade_label_id, subject_label_id
      )
    values (
      #{giftPackageId}, #{creator}, NOW(), #{firstClassify}, #{secondClassify}, #{gradeLabelId}, #{subjectLabelId}
      )
  </insert>

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

    <select id="getByPackageId" parameterType="long" resultType="com.pcloud.book.giftcoupon.dto.GiftCategoryDTO">
        select
        category.id,
        category.gift_package_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 gift_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 gift_package_id=#{giftPackageId}
    </select>

    <select id="getByPackageIds" parameterType="list" resultType="com.pcloud.book.giftcoupon.dto.GiftCategoryDTO">
         select
        category.id,
        category.gift_package_id giftPackageId,
        category.first_classify firstClassify,
        category.second_classify secondClassify,
        category.grade_label_id gradeLabelId,
        category.subject_label_id subjectLabelId
        from gift_category category
        where gift_package_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            ${item}
        </foreach>
    </select>

    <delete id="deleteByPackageId" parameterType="long">
        DELETE FROM gift_category
        WHERE gift_package_id = #{giftPackageId}
    </delete>

    <select id="getTemplateIds" parameterType="list" resultType="Long">
        select first_classify from
        gift_category
        where gift_package_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            ${item}
        </foreach>
        and first_classify>0
        union
        select second_classify from
        gift_category
        where gift_package_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            ${item}
        </foreach>
        and second_classify>0
    </select>

    <select id="getLabels" parameterType="list" resultType="Long">
        select grade_label_id from
        gift_category
        where gift_package_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            ${item}
        </foreach>
        and grade_label_id>0
        union
        select subject_label_id from
        gift_category
        where gift_package_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            ${item}
        </foreach>
        and subject_label_id>0
    </select>

    <delete id="deleteByPackageIds" parameterType="list">
         DELETE FROM gift_category
        WHERE gift_package_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            ${item}
        </foreach>
    </delete>

    <select id="getByParams" parameterType="map" resultType="Long">
        select
        a.id
        from
        gift_coupon_package a
        INNER JOIN (
        select DISTINCT gift_package_id from
        gift_category
        where
         first_classify =#{firstClassify}
         <if test="gradeLabelId!=null">
             AND grade_label_id = #{gradeLabelId}
         </if>
        <if test="gradeLabelId==null">
            AND grade_label_id is null
        </if>
        <if test="subjectLabelId!=null">
            AND subject_label_id = #{subjectLabelId}
        </if>
        <if test="subjectLabelId==null">
            AND subject_label_id is null
        </if>

        )t on a.id=t.gift_package_id
        where a.is_delete=0
        and a.stock>0
        and a.on_shelves=1
        and a.rec_to_page=1
        and now() <![CDATA[ < ]]> a.valid_date_end
    </select>

    <select id="getOtherByParams" parameterType="map" resultType="Long">
        select
        a.id
        from
        gift_coupon_package a
        where a.is_delete=0
        and a.stock>0
        and a.on_shelves=1
        and now() <![CDATA[ < ]]> a.valid_date_end
        and a.id not in
        (
        select DISTINCT gift_package_id from
        gift_category
        )
    </select>
</mapper>