<?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.GiveawayMapper">
  <resultMap id="giveawayMap" type="com.tmerclub.cloud.product.model.Giveaway">
    <id column="giveaway_id" property="giveawayId" />
    <result column="name" property="name"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="start_time" property="startTime"/>
    <result column="end_time" property="endTime"/>
    <result column="shop_id" property="shopId"/>
    <result column="buy_num" property="buyNum"/>
    <result column="spu_id" property="spuId"/>
    <result column="status" property="status"/>
  </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="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="start_time" property="startTime"/>
    <result column="end_time" property="endTime"/>
    <result column="shop_id" property="shopId"/>
    <result column="buy_num" property="buyNum"/>
    <result column="spu_id" property="spuId"/>
    <result column="status" property="status"/>
    <result column="main_img_url" property="mainImgUrl"/>
    <result column="spu_name" property="spuName"/>
    <result column="price_fee" property="price"/>

  </resultMap>
  <sql id="Vo_Column_List">
    `giveaway_id`,`name`,`create_time`,`update_time`,`start_time`,`end_time`,`shop_id`,`buy_num`,`spu_id`,`status`
  </sql>
  <select id="list" resultMap="giveawayMap">
    select <include refid="Vo_Column_List"/> from giveaway order by giveaway_id desc
  </select>

  <select id="getBySpuId" resultMap="giveawayVOMap">
    select <include refid="Vo_Column_List"/> from giveaway where spu_id = #{spuId} and status = 1
  </select>

  <select id ="listByShopId" resultMap="giveawayVOMap">
    SELECT
    DISTINCT g.*,
    s.main_img_url,
    sl.spu_name AS spu_name
    FROM
    giveaway g
    JOIN giveaway_spu gs ON gs.giveaway_id = g.giveaway_id
    JOIN spu s ON g.spu_id = s.spu_id
    LEFT JOIN spu_lang sl ON g.spu_id = sl.spu_id
    AND sl.lang = #{lang}
    JOIN spu su ON gs.spu_id = su.spu_id
    LEFT JOIN spu_lang sls ON su.spu_id = sls.spu_id
    AND sls.lang = #{lang}
    <where>
      <if test="giveawayDTO.shopId != null">
        and g.shop_id = #{giveawayDTO.shopId}
      </if>
      <if test="giveawayDTO.name != null and giveawayDTO.name != ''">
        and g.name like concat('%',#{giveawayDTO.name},'%')
      </if>
      <if test="giveawayDTO.status != null">
        and g.status = #{giveawayDTO.status}
      </if>
      <if test="giveawayDTO.spuName != null and giveawayDTO.spuName != ''">
        and sl.spu_name like concat('%',#{giveawayDTO.spuName},'%')
      </if>
      <if test="giveawayDTO.giveawaySpuName != null and giveawayDTO.giveawaySpuName != ''">
        and sls.spu_name like concat('%',#{giveawayDTO.giveawaySpuName},'%')
      </if>
      and g.status > -1
    </where>
    order by g.giveaway_id desc
  </select>

  <select id="getByGiveawayId" resultMap="giveawayVOMap">
    select g.*, s.price_fee, s.main_img_url, sl.spu_name
    from giveaway g
    join spu s on g.spu_id = s.spu_id
    left join spu_lang sl on s.spu_id = sl.spu_id and sl.lang = #{lang}
    where g.giveaway_id = #{giveawayId}
  </select>

  <insert id="save" parameterType="com.tmerclub.cloud.product.model.Giveaway" useGeneratedKeys="true" keyProperty="giveawayId">
    insert into giveaway (`name`,`start_time`,`end_time`,`shop_id`,`buy_num`,`spu_id`,`status`)
    values (#{giveaway.name},#{giveaway.startTime},#{giveaway.endTime},#{giveaway.shopId},#{giveaway.buyNum},#{giveaway.spuId},#{giveaway.status});
  </insert>

  <update id="update">
    update giveaway
    <set>
      <if test="giveaway.name != null">
        `name` = #{giveaway.name},
      </if>
      <if test="giveaway.startTime != null">
        `start_time` = #{giveaway.startTime},
      </if>
      <if test="giveaway.endTime != null">
        `end_time` = #{giveaway.endTime},
      </if>
      <if test="giveaway.shopId != null">
        `shop_id` = #{giveaway.shopId},
      </if>
      <if test="giveaway.buyNum != null">
        `buy_num` = #{giveaway.buyNum},
      </if>
      <if test="giveaway.spuId != null">
        `spu_id` = #{giveaway.spuId},
      </if>
      <if test="giveaway.status != null">
        `status` = #{giveaway.status},
      </if>
    </set>
    where giveaway_id = #{giveaway.giveawayId} and
    <if test="isUpdate == 0">
      `status` != 0
    </if>
    <if test="isUpdate == 1">
      `status` > #{giveaway.status}
    </if>

  </update>

  <select id="count" resultType="Integer">
    select COUNT(spu_id) from giveaway where status > 0 and spu_id = #{spuId}
    <if test="giveawayId != null">
     and giveaway_id != #{giveawayId}
    </if>
  </select>

  <select id="countList" resultMap="giveawayVOMap">
    select name, start_time , end_time from giveaway where status =  #{status} and spu_id = #{spuId}
    <if test="giveawayId != null">
      and giveaway_id != #{giveawayId}
    </if>
  </select>

  <select id="listUnEndButNeedEndActivity" resultMap="giveawayMap">
    select giveaway_id, spu_id, `status`
    FROM giveaway
    WHERE `status` = 1
      and end_time &lt;= now()
  </select>

  <select id="listUnStartButNeedStartActivity" resultType="java.lang.Long">
    select spu_id
    FROM giveaway
    WHERE `status` = 2
      and start_time &lt; now()
  </select>

  <update id="startGiveawayActivity" >
    UPDATE giveaway SET `status` = 1
    WHERE `status` = 2
      and start_time &lt; now()
  </update>



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

  <select id="listMainSpuIdByGiveawaySpuId" resultType="java.lang.Long">
    SELECT spu_id FROM `giveaway` WHERE `status` = 1 AND giveaway_id IN (SELECT giveaway_id FROM `giveaway_spu` WHERE `status` = 1 AND spu_id = #{spuId})
  </select>

</mapper>
