<?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="org.springblade.modules.shopping.mapper.ConsignMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="consignResultMap" type="org.springblade.modules.shopping.entity.Consign">
        <id column="id" property="id"/>
        <result column="order_id" property="orderId"/>
        <result column="goods_id" property="goodsId"/>
        <result column="goods_name" property="goodsName"/>
        <result column="total_price" property="totalPrice"/>
        <result column="consign_config_id" property="consignConfigId"/>
        <result column="total_num" property="totalNum"/>
        <result column="user_id" property="userId"/>
        <result column="consign_price" property="consignPrice"/>
        <result column="create_time" property="createTime"/>
        <result column="confirm_time" property="confirmTime"/>
    </resultMap>


    <select id="selectConsignPage" resultMap="consignResultMap">
        SELECT * FROM t_consign WHERE is_deleted = 0
    </select>

    <!--查询是否有单可抢-->
    <select id="queryConsign" resultMap="consignResultMap">
        SELECT
        *
        FROM
        t_consign t
        WHERE
        consign_config_id = #{consignConfigId}
        AND t.now_stock > 0
        AND is_sale = 2
        AND consign_state = 1
        AND is_deleted = 0
        AND consign_user_id != #{userId}
        AND TO_DAYS(now()) BETWEEN TO_DAYS(start_time) and TO_DAYS(end_time)
        LIMIT 1
    </select>

    <!--抢购  AND TO_DAYS(now()) BETWEEN TO_DAYS(start_time) and TO_DAYS(end_time)-->
    <select id="randomConsign" resultMap="consignResultMap">
        SELECT
        *
        FROM
        t_consign t
        WHERE
        consign_config_id = #{consignConfigId}
        AND t.now_stock > 0
        AND is_sale = 2
        AND consign_state = 1
        AND is_deleted = 0
        AND consign_for = 2
        AND consign_user_id != #{userId}
        AND TO_DAYS(now()) BETWEEN TO_DAYS(start_time) and TO_DAYS(end_time)
        AND t.id >= (
            RAND() * (SELECT min(id) FROM t_consign)
        )
        LIMIT 1
    </select>


    <!--获取平台单-->
    <select id="platformConsign" resultMap="consignResultMap">
        SELECT
        *
        FROM
        t_consign t
        WHERE
        consign_config_id = #{consignConfigId}
        AND t.now_stock > 0
        AND is_sale = 2
        AND consign_state = 1
        AND consign_for = 1
        AND is_deleted = 0
        AND TO_DAYS(now()) BETWEEN TO_DAYS(start_time) and TO_DAYS(end_time)
        AND t.id = #{consignId}
        LIMIT 1
    </select>


</mapper>
