<?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.liquidnet.service.goblin.mapper.GoblinNftExSkuMapper">
    <resultMap id="BaseResult" type="com.liquidnet.service.goblin.entity.GoblinNftExSku">
        <result column="activity_id" property="activityId" jdbcType="VARCHAR"/>
        <result column="sku_id" property="skuId" jdbcType="VARCHAR"/>
        <result column="store_id" property="storeId" jdbcType="VARCHAR"/>
        <result column="spu_id" property="spuId" jdbcType="VARCHAR"/>
        <result column="unbox" property="unbox" jdbcType="VARCHAR"/>
        <result column="ex_stock" property="exStock" jdbcType="INTEGER"/>
        <result column="ex_limit" property="exLimit" jdbcType="INTEGER"/>
        <result column="ex_start_time" property="exStartTime" jdbcType="TIMESTAMP"/>
        <result column="ex_stop_time" property="exStopTime" jdbcType="TIMESTAMP"/>
        <result column="created_at" property="createdAt" jdbcType="TIMESTAMP"/>
        <result column="updated_at" property="updatedAt" jdbcType="TIMESTAMP"/>
    </resultMap>

    <select id="selectGoblinNftExSkuByActivityIds" resultMap="BaseResult">
        select activity_id,sku_id,unbox,store_id,ex_start_time,ex_stop_time from goblin_nft_ex_sku where
        activity_id in
        <foreach collection="activityIds.split(',')" item="activityId" open="(" separator="," close=")">
            #{activityId,jdbcType=VARCHAR}
        </foreach>
    </select>
    <select id="selectExCodeNumAll" resultType="com.liquidnet.service.goblin.dto.GoblinNftExCodeNumDto">
        select gnes.activity_id, gnes.store_id, gnes.spu_id, gnes.sku_id, gnes.unbox, gnes.ex_start_time, gnes.ex_stop_time, count(*) as num, gnec.box_sku_id
        from goblin_nft_ex_sku as gnes
        left join goblin_nft_ex_code as gnec on gnes.activity_id = gnec.activity_id and gnes.sku_id =
        gnec.sku_id
        <where>
            gnes.activity_id = #{oldActivityId}
            and (gnec.state = 3 or (gnec.state = 1 and gnes.ex_stop_time <![CDATA[<]]> NOW()))
            group by gnes.sku_id
        </where>
    </select>
    <select id="selectExCodeNumBox" resultType="com.liquidnet.service.goblin.dto.GoblinNftExCodeNumDto">
        select gnes.activity_id, gnes.store_id, gnes.spu_id, gnes.sku_id, gnes.unbox, gnes.ex_start_time, gnes.ex_stop_time, count(*) as num, gnec.box_sku_id
        from goblin_nft_ex_sku as gnes
        left join goblin_nft_ex_code as gnec on gnes.activity_id = gnec.activity_id and gnes.sku_id =
        gnec.sku_id
        <where>
            gnes.activity_id = #{oldActivityId}
            and gnes.sku_id = #{skuId}
            and (gnec.state = 3 or (gnec.state = 1 and gnes.ex_stop_time <![CDATA[<]]> NOW()))
            and gnes.unbox = '1'
            group by gnec.box_sku_id
        </where>
    </select>

    <insert id="addGoblinNftExSkus">
        insert into goblin_nft_ex_sku
        (activity_id,spu_id,sku_id,store_id,unbox,ex_stock,ex_limit,ex_start_time,ex_stop_time,created_at)
        values
        <foreach collection="goblinNftExSkus" item="goblinNftExSku" separator=",">
            (
            #{goblinNftExSku.activityId},
            #{goblinNftExSku.spuId},
            #{goblinNftExSku.skuId},
            #{goblinNftExSku.storeId},
            #{goblinNftExSku.unbox},
            #{goblinNftExSku.exStock},
            #{goblinNftExSku.exLimit},
            #{goblinNftExSku.exStartTime},
            #{goblinNftExSku.exStopTime},
            #{goblinNftExSku.createdAt}
            )
        </foreach>
    </insert>
</mapper>
