<?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.GoblinNftExCodeTaskMapper">
    <resultMap id="BaseResult" type="com.liquidnet.service.goblin.entity.GoblinNftExCodeTask">
        <result column="task_id" property="taskId" jdbcType="VARCHAR"/>
        <result column="activity_id" property="activityId" jdbcType="VARCHAR"/>
        <result column="store_id" property="storeId" jdbcType="VARCHAR"/>
        <result column="spu_id" property="spuId" jdbcType="VARCHAR"/>
        <result column="sku_id" property="skuId" 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="typ" property="typ" jdbcType="INTEGER"/>
        <result column="created_at" property="createdAt" jdbcType="TIMESTAMP"/>
        <result column="updated_at" property="updatedAt" jdbcType="TIMESTAMP"/>
    </resultMap>


    <select id="selectByActivity" resultMap="BaseResult">
        SELECT a.task_id,
               a.activity_id,
               a.store_id,
               a.spu_id,
               a.sku_id,
               a.unbox,
               a.ex_stock,
               a.ex_limit,
               a.ex_start_time,
               a.ex_stop_time,
               a.created_at
        FROM goblin_nft_ex_code_task a,
             (
                 SELECT activity_id
                 FROM goblin_nft_ex_code_task where typ = 1
                 GROUP BY activity_id LIMIT 1
             ) b
        where a.activity_id = b.activity_id
          and a.typ = 1

    </select>

    <select id="selectByActivityIds" resultMap="BaseResult">
        select activity_id from goblin_nft_ex_code_task where typ = 1 and activity_id in
        <foreach collection="activityIds.split(',')" item="activityId" open="(" separator="," close=")">
            #{activityId}
        </foreach>
    </select>

    <insert id="addGoblinNftExCodeTasks">
        insert into goblin_nft_ex_code_task
        (task_id,activity_id,store_id,spu_id,sku_id,unbox,ex_stock,ex_limit,ex_start_time,ex_stop_time,typ,created_at)
        values
        <foreach collection="goblinNftExCodeTasks" item="goblinNftExCodeTask" separator=",">
            (
            #{goblinNftExCodeTask.taskId},
            #{goblinNftExCodeTask.activityId},
            #{goblinNftExCodeTask.storeId},
            #{goblinNftExCodeTask.spuId},
            #{goblinNftExCodeTask.skuId},
            #{goblinNftExCodeTask.unbox},
            #{goblinNftExCodeTask.exStock},
            #{goblinNftExCodeTask.exLimit},
            #{goblinNftExCodeTask.exStartTime},
            #{goblinNftExCodeTask.exStopTime},
            #{goblinNftExCodeTask.typ},
            #{goblinNftExCodeTask.createdAt}
            )
        </foreach>
    </insert>


    <delete id="deleteByTaskIds">
        delete from goblin_nft_ex_code_task where task_id in
        <foreach collection="taskIds.split(',')" item="taskId" open="(" separator="," close=")">
            #{taskId}
        </foreach>
    </delete>

    <update id="updateByTaskIds">
        update goblin_nft_ex_code_task
        <set>
            typ = 0 , updated_at = now()
        </set>
        where task_id in
        <foreach collection="taskIds.split(',')" item="taskId" index="index" open="(" separator="," close=")">
            #{taskId}
        </foreach>
    </update>

</mapper>
