<?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.zm.order.tool.server.tb.TbItemRecommendDao">

    <resultMap id="itemRecommendMap" type="com.zm.order.tool.server.model.ItemRecommend">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="seller_id" jdbcType="BIGINT" property="sellerId"/>
        <result column="store_id" jdbcType="VARCHAR" property="storeId"/>
        <result column="item_name" jdbcType="VARCHAR" property="itemName"/>
        <result column="spu_id" jdbcType="BIGINT" property="spuId"/>
        <result column="sku_id" jdbcType="VARCHAR" property="skuId"/>
        <result column="order_at" jdbcType="TIMESTAMP" property="orderAt"/>
        <result column="created_at" jdbcType="TIMESTAMP" property="createdAt"/>
        <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt"/>
        <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
    </resultMap>


    <sql id="tb">
        orderpro.item_recommends
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
        order_no, user_id, seller_id, store_id,
        item_name, spu_id, sku_id, order_at,
        created_at, updated_at, is_deleted
    </sql>

    <insert id="inserts" parameterType="list">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="itemList" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.orderNo}, #{item.userId}, #{item.sellerId},#{item.storeId},
            #{item.itemName}, #{item.spuId}, #{item.skuId}, #{item.orderAt},
            now(), now(), #{item.isDeleted}
            )
        </foreach>
    </insert>


    <select id="selectList" parameterType="map" resultMap="itemRecommendMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
             seller_id = #{sellerId}
             and spu_id =  #{spuId}
            <if test="storeId != null">
                and store_id = #{storeId}
            </if>
            and is_deleted = 0
            order by order_at desc
            limit #{size};
        </where>
    </select>

    <select id="selectIdList" parameterType="map" resultMap="itemRecommendMap">
        select
        id, seller_id
        from
        <include refid="tb"/>
        <where>
            <if test="orderAtStart != null">
                <![CDATA[ AND order_at >= #{orderAtStart} ]]>
            </if>
            <if test="orderAtEnd != null">
                <![CDATA[ AND order_at < #{orderAtEnd} ]]>
            </if>
            <if test="isDeleted != null">
                and is_deleted = #{isDeleted}
            </if>
            <if test="lastId != null">
                <![CDATA[ AND id > #{lastId} ]]>
            </if>
            order by order_at
            limit ${size}
        </where>
    </select>

    <update id="deleteByIdList" parameterType="map">
        update
        <include refid="tb"/>
        set is_deleted = -1,
        updated_at = now()
        where id in
        <foreach collection="idList" item="item" index="index" open="(" close=")" separator=",">
            #{item.id}
        </foreach>
    </update>

    <update id="realDeleteByIdList" parameterType="map">
        delete from
        <include refid="tb"/>
        where is_deleted = -1 and id in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>
</mapper>