<?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.product.mapper.GiveawaySpuMapper">
  <resultMap id="giveawaySpuMap" type="com.tmerclub.cloud.product.model.GiveawaySpu">
    <id column="giveaway_spu_id" property="giveawaySpuId" />
    <result column="giveaway_id" property="giveawayId"/>
    <result column="spu_id" property="spuId"/>
    <result column="sku_id" property="skuId"/>
    <result column="giveaway_num" property="giveawayNum"/>
    <result column="refund_price" property="refundPrice"/>
    <result column="status" property="status"/>
  </resultMap>

  <resultMap id="giveawaySpuVOMap" type="com.tmerclub.cloud.common.product.vo.GiveawaySpuVO">
    <id column="giveaway_spu_id" property="giveawaySpuId" />
    <result column="giveaway_id" property="giveawayId"/>
    <result column="spu_id" property="spuId"/>
    <result column="sku_id" property="skuId"/>
    <result column="giveaway_num" property="giveawayNum"/>
    <result column="refund_price" property="refundPrice"/>
    <result column="status" property="status"/>
    <result column="spu_name" property="spuName"/>
    <result column="main_img_url" property="mainImgUrl"/>
    <result column="sku_name" property="skuName"/>
    <result column="price_fee" property="priceFee"/>
    <result column="shop_id" property="shopId"/>
  </resultMap>

    <resultMap id="GiveawayVOMap" type="com.tmerclub.cloud.common.product.vo.GiveawayVO">
        <id column="giveaway_id" property="giveawayId" />
        <result column="name" property="name"/>
        <result column="spu_id" property="spuId"/>
        <result column="status" property="status"/>
        <result column="buy_num" property="buyNum"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <collection property="giveawaySpus" ofType="com.tmerclub.cloud.common.product.vo.GiveawaySpuVO">
            <result property="spuId" column="g_spu_id"/>
            <result property="skuId" column="g_sku_id"/>
            <result property="giveawayNum" column="giveaway_num"/>
            <result property="status" column="s_status"/>
            <result property="spuName" column="spu_name"/>
            <result property="skuName" column="sku_name"/>
            <result property="mainImgUrl" column="main_img_url"/>
            <result property="refundPrice" column="refund_price"/>
            <result property="shopId" column="gs_shop_id"/>
            <result property="priceFee" column="gs_price_fee"/>
            <result property="categoryId" column="category_id"/>
        </collection>
    </resultMap>
  <sql id="Vo_Column_List">
    `giveaway_spu_id`,`giveaway_id`,`spu_id`,`sku_id`,`giveaway_num`,`refund_price`,`status`
  </sql>
  <select id="list" resultMap="giveawaySpuVOMap">
    select <include refid="Vo_Column_List"/> from giveaway_spu order by giveaway_spu_id desc
  </select>
  <select id="getBySpuId" resultMap="giveawaySpuVOMap">
    select <include refid="Vo_Column_List"/> from giveaway_spu where spu_id = #{spuId} and status > 0
  </select>

  <select id="getByGiveawayId" resultMap="giveawaySpuVOMap">
    SELECT
      gs.*,
      s.main_img_url,
      s.price_fee,
      s.shop_id,
      s.shop_category_id,
      sl.spu_name,
      skl.sku_name
    FROM
      giveaway_spu gs
        LEFT JOIN spu s ON gs.spu_id = s.spu_id
        LEFT JOIN spu_lang sl ON gs.spu_id = sl.spu_id
        AND sl.lang = #{lang}
        JOIN sku sk ON gs.sku_id = sk.sku_id
        LEFT JOIN sku_lang skl ON skl.sku_id = sk.sku_id
        AND skl.lang = #{lang}
    WHERE
      gs.giveaway_id = #{giveawayId}
      AND gs.`status` > #{status}
    </select>

  <select id="getGiveawayBySpuId" resultMap="GiveawayVOMap">
    SELECT g.`name`,g.buy_num,g.`status`,g.`giveaway_id`,g.`start_time`,g.`end_time`,gs.refund_price,gs.`spu_id` g_spu_id,gs.`sku_id` g_sku_id, gs.`giveaway_num`,s.main_img_url,
           s.price_fee gs_price_fee,
           s.shop_id gs_shop_id,
           s.category_id
    FROM `giveaway` g
    JOIN `giveaway_spu` gs ON gs.`giveaway_id` = g.`giveaway_id` AND gs.`status` = 1
    JOIN `spu` s ON s.spu_id = gs.spu_id
    WHERE g.`status` > 0 AND s.status = 1 AND g.`spu_id` = #{spuId}
  </select>


  <insert id="save">
    insert into giveaway_spu (`giveaway_id`,`spu_id`,`sku_id`,`giveaway_num`,`refund_price`,`status`)
    values (#{giveawaySpu.giveawayId},#{giveawaySpu.spuId},#{giveawaySpu.skuId},#{giveawaySpu.giveawayNum},#{giveawaySpu.refundPrice},#{giveawaySpu.status});
  </insert>

  <update id="updateStatus">
    update giveaway_spu set status = #{status} where giveaway_id = #{giveawayId} and status > #{status}
  </update>

  <delete id="deleteById">
    delete from giveaway_spu where giveaway_spu_id = #{giveawaySpuId}
  </delete>
  <insert id="insertBatch">
    insert into giveaway_spu (`giveaway_id`,`spu_id`,`sku_id`,`giveaway_num`,`refund_price`,`status`)
    values
    <foreach collection="giveawaySpuList" item="giveawaySpu" separator=",">
      (#{giveawaySpu.giveawayId},#{giveawaySpu.spuId},#{giveawaySpu.skuId},#{giveawaySpu.giveawayNum},#{giveawaySpu.refundPrice},#{giveawaySpu.status})
    </foreach>
  </insert>

  <delete id="batchDeleteByIds">
    delete from giveaway_spu where giveaway_spu_id in
    <foreach collection="giveawaySpuIds" item="giveawaySpuId" open="(" close=")" separator=",">
      #{giveawaySpuId}
    </foreach>
  </delete>


  <select id="getMainSpuPage" resultType="com.tmerclub.cloud.common.product.vo.SpuVO">
    SELECT s.spu_id,s.main_img_url,s.price_fee,sl.spu_name as name, s.spu_mold
    FROM spu s
    LEFT JOIN spu_lang sl ON s.spu_id = sl.spu_id AND sl.lang = #{lang}
    WHERE s.spu_id NOT IN
    (
    SELECT spu_id FROM giveaway WHERE `status` in (1, 2)
    <if test="giveawaySpuDTO.giveawayId != null">
      AND giveaway_id != #{giveawaySpuDTO.giveawayId}
    </if>
    )
      AND s.spu_mold = 0
<!--      AND (s.spu_mold = 0 or (s.spu_mold = 2 and s.is_all_entity))-->
    AND s.status = 1 AND s.shop_id = #{giveawaySpuDTO.shopId} AND s.spu_type != 5
      AND s.supplier_spu_type != 1
      AND (s.supplier_delivery_type != 1 or s.supplier_delivery_type is null)
    <if test="giveawaySpuDTO.spuName != null and giveawaySpuDTO.spuName != ''">
      AND sl.spu_name LIKE CONCAT("%", #{giveawaySpuDTO.spuName} ,"%")
    </if>
    <if test="giveawaySpuDTO.shopCategoryId != null">
      AND s.shop_category_id = #{giveawaySpuDTO.shopCategoryId}
    </if>
  </select>


  <update id="changeGiveawayActivityStatusByGiveawayIdList">
    UPDATE giveaway_spu SET `status` = 0 WHERE giveaway_id IN
    <foreach collection="giveawayIdList" item="giveawayId" separator="," open="(" close=")">
      #{giveawayId}
    </foreach>
  </update>

</mapper>
