<?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.tmerclub.cloud.discount.mapper.DiscountItemMapper">
  <resultMap id="discountItemMap" type="com.tmerclub.cloud.discount.model.DiscountItem">
    <id column="discount_item_id" property="discountItemId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="discount_id" property="discountId"/>
    <result column="need_amount" property="needAmount"/>
    <result column="discount" property="discount"/>
  </resultMap>

  <delete id="deleteByDiscountId">
    delete from discount_item where discount_id = #{discountId}
  </delete>
  <insert id="insertDiscountItems">
    insert into discount_item (discount_id,need_amount,discount) values
    <foreach collection="discountItems" item="discountItem" separator=",">
    (#{discountItem.discountId},#{discountItem.needAmount},#{discountItem.discount})
    </foreach>
  </insert>

  <select id="listDiscountBySpuIds" resultType="com.tmerclub.cloud.discount.model.DiscountSpu">
    SELECT dp.`spu_id`,d.`discount_id` FROM discount  d
    LEFT JOIN discount_spu dp ON d.`discount_id` = dp.`discount_id`
    WHERE  d.`suitable_spu_type` = 1 AND dp.`spu_id` IN (
    <foreach collection="spuIds" separator="," item="spuId">
      #{spuId}
    </foreach>
    )
  </select>

</mapper>
