<?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.tuling.tulingmall.dao.FlashPromotionProductDao">
    <resultMap id="flashPromotionProduct" type="com.tuling.tulingmall.domain.FlashPromotionProduct">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="brand_id" jdbcType="BIGINT" property="brandId" />
        <result column="product_category_id" jdbcType="BIGINT" property="productCategoryId" />
        <result column="feight_template_id" jdbcType="BIGINT" property="feightTemplateId" />
        <result column="product_attribute_category_id" jdbcType="BIGINT" property="productAttributeCategoryId" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="pic" jdbcType="VARCHAR" property="pic" />
        <result column="product_sn" jdbcType="VARCHAR" property="productSn" />
        <result column="delete_status" jdbcType="INTEGER" property="deleteStatus" />
        <result column="publish_status" jdbcType="INTEGER" property="publishStatus" />
        <result column="new_status" jdbcType="INTEGER" property="newStatus" />
        <result column="recommand_status" jdbcType="INTEGER" property="recommandStatus" />
        <result column="verify_status" jdbcType="INTEGER" property="verifyStatus" />
        <result column="sort" jdbcType="INTEGER" property="sort" />
        <result column="sale" jdbcType="INTEGER" property="sale" />
        <result column="price" jdbcType="DECIMAL" property="price" />
        <result column="promotion_price" jdbcType="DECIMAL" property="promotionPrice" />
        <result column="gift_growth" jdbcType="INTEGER" property="giftGrowth" />
        <result column="gift_point" jdbcType="INTEGER" property="giftPoint" />
        <result column="use_point_limit" jdbcType="INTEGER" property="usePointLimit" />
        <result column="sub_title" jdbcType="VARCHAR" property="subTitle" />
        <result column="original_price" jdbcType="DECIMAL" property="originalPrice" />
        <result column="stock" jdbcType="INTEGER" property="stock" />
        <result column="low_stock" jdbcType="INTEGER" property="lowStock" />
        <result column="unit" jdbcType="VARCHAR" property="unit" />
        <result column="weight" jdbcType="DECIMAL" property="weight" />
        <result column="preview_status" jdbcType="INTEGER" property="previewStatus" />
        <result column="service_ids" jdbcType="VARCHAR" property="serviceIds" />
        <result column="keywords" jdbcType="VARCHAR" property="keywords" />
        <result column="note" jdbcType="VARCHAR" property="note" />
        <result column="album_pics" jdbcType="VARCHAR" property="albumPics" />
        <result column="detail_title" jdbcType="VARCHAR" property="detailTitle" />
        <result column="promotion_start_time" jdbcType="TIMESTAMP" property="promotionStartTime" />
        <result column="promotion_end_time" jdbcType="TIMESTAMP" property="promotionEndTime" />
        <result column="promotion_per_limit" jdbcType="INTEGER" property="promotionPerLimit" />
        <result column="promotion_type" jdbcType="INTEGER" property="promotionType" />
        <result column="brand_name" jdbcType="VARCHAR" property="brandName" />
        <result column="product_category_name" jdbcType="VARCHAR" property="productCategoryName" />
        <result column="flash_promotion_price" property="flashPromotionPrice"/>
        <result column="flash_promotion_count" property="flashPromotionCount"/>
        <result column="flash_promotion_limit" property="flashPromotionLimit"/>
    </resultMap>
    <resultMap id="flashPromotionParam" type="com.tuling.tulingmall.domain.FlashPromotionParam">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="title" jdbcType="VARCHAR" property="title" />
        <result column="start_date" jdbcType="DATE" property="startDate" />
        <result column="end_date" jdbcType="DATE" property="endDate" />
        <result column="status" jdbcType="INTEGER" property="status" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <collection property="relation" columnPrefix="r_" resultMap="com.tuling.tulingmall.mapper.SmsFlashPromotionProductRelationMapper.BaseResultMap">
        </collection>
    </resultMap>
    <select id="getFlashProductList" resultMap="flashPromotionProduct">
        SELECT
            pr.flash_promotion_price,
            pr.flash_promotion_count,
            pr.flash_promotion_limit,
            p.*
        FROM
            tl_mall_promotion.sms_flash_promotion_product_relation pr
            LEFT JOIN tl_mall_goods.pms_product p ON pr.product_id = p.id
        WHERE
            pr.flash_promotion_id = #{flashPromotionId}
            AND pr.flash_promotion_session_id = #{sessionId}
    </select>
    <select id="getFlashPromotion" resultMap="flashPromotionParam">
        SELECT
        sp.*,
        sfp.id r_id,
        sfp.flash_promotion_id r_flash_promotion_id,
        sfp.flash_promotion_session_id r_flash_promotion_session_id,
        sfp.product_id r_product_id,
        sfp.flash_promotion_price r_flash_promotion_price,
        sfp.flash_promotion_count r_flash_promotion_count,
        sfp.flash_promotion_limit r_flash_promotion_limit,
        sfp.sort r_sort
        FROM
        tl_mall_promotion.sms_flash_promotion sp
        LEFT JOIN tl_mall_promotion.sms_flash_promotion_product_relation sfp ON sfp.flash_promotion_id=sp.id
        WHERE NOW()>=sp.start_date AND sp.end_date>NOW() and status=1
        <if test="pid != null">
            AND sfp.product_id=#{pid}
        </if>
        ORDER BY sfp.sort DESC
    </select>

    <select id="getHomePromotionProductList" resultMap="flashPromotionProduct">
        SELECT
        pr.flash_promotion_price,
        pr.flash_promotion_count,
        pr.flash_promotion_limit,
        p.*
        FROM
        tl_mall_promotion.sms_flash_promotion_product_relation pr
        LEFT JOIN tl_mall_goods.pms_product p ON pr.product_id = p.id
        WHERE
        pr.id IN
        <foreach collection="promotionIds" item="listItem" open="(" close=")" separator=",">
            #{listItem}
        </foreach>
    </select>
</mapper>