<?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.PortalProductDao">
    <resultMap id="cartProductMap" type="com.tuling.tulingmall.domain.CartProduct" autoMapping="true">
        <id column="id" jdbcType="BIGINT" property="id" />
        <collection property="productAttributeList" columnPrefix="attr_" resultMap="com.tuling.tulingmall.mapper.PmsProductAttributeMapper.BaseResultMap">
        </collection>
        <collection property="skuStockList" columnPrefix="sku_" resultMap="com.tuling.tulingmall.mapper.PmsSkuStockMapper.BaseResultMap">
        </collection>
    </resultMap>
    <resultMap id="promotionProductMap" type="com.tuling.tulingmall.domain.PromotionProduct" extends="com.tuling.tulingmall.mapper.PmsProductMapper.BaseResultMap">
        <id column="id" jdbcType="BIGINT" property="id" />
        <collection property="skuStockList" columnPrefix="sku_" resultMap="com.tuling.tulingmall.mapper.PmsSkuStockMapper.BaseResultMap">
        </collection>
        <collection property="productLadderList" columnPrefix="ladder_" resultMap="com.tuling.tulingmall.mapper.PmsProductLadderMapper.BaseResultMap">
        </collection>
        <collection property="productFullReductionList" columnPrefix="full_" resultMap="com.tuling.tulingmall.mapper.PmsProductFullReductionMapper.BaseResultMap">
        </collection>
    </resultMap>
    <resultMap id="productInfoMap" type="com.tuling.tulingmall.domain.PmsProductParam" extends="com.tuling.tulingmall.mapper.PmsProductMapper.ResultMapWithBLOBs">
        <id column="id" jdbcType="BIGINT" property="id" />
        <collection property="productLadderList" columnPrefix="ladder_" resultMap="com.tuling.tulingmall.mapper.PmsProductLadderMapper.BaseResultMap">
        </collection>
        <collection property="productFullReductionList" columnPrefix="full_" resultMap="com.tuling.tulingmall.mapper.PmsProductFullReductionMapper.BaseResultMap">
        </collection>
        <collection property="memberPriceList" columnPrefix="member_" resultMap="com.tuling.tulingmall.mapper.PmsMemberPriceMapper.BaseResultMap">
        </collection>
        <collection property="skuStockList" columnPrefix="sku_" resultMap="com.tuling.tulingmall.mapper.PmsSkuStockMapper.BaseResultMap">
        </collection>
        <collection property="productAttributeValueList" columnPrefix="attribute_" resultMap="com.tuling.tulingmall.mapper.PmsProductAttributeValueMapper.BaseResultMap">
        </collection>
    </resultMap>
    <select id="getCartProduct" resultMap="cartProductMap">
        SELECT
            p.id id,
            p.`name` name,
            p.sub_title subTitle,
            p.price price,
            p.pic pic,
            p.product_attribute_category_id productAttributeCategoryId,
            p.stock stock,
            pa.id attr_id,
            pa.`name` attr_name,
            pa.type attr_type,
            ps.id sku_id,
            ps.sku_code sku_code,
            ps.price sku_price,
            ps.sp1 sku_sp1,
            ps.sp2 sku_sp2,
            ps.sp3 sku_sp3,
            ps.stock sku_stock,
            ps.pic sku_pic
        FROM
            tl_mall_goods.pms_product p
            LEFT JOIN tl_mall_goods.pms_product_attribute pa ON p.product_attribute_category_id = pa.product_attribute_category_id
            LEFT JOIN tl_mall_goods.pms_sku_stock ps ON p.id=ps.product_id
        WHERE
            p.id = #{id}
            AND pa.type = 0
        ORDER BY pa.sort desc
    </select>
    <select id="getPromotionProductList" resultMap="promotionProductMap">
        SELECT
            p.id,
            p.`name`,
            p.promotion_type,
            p.gift_growth,
            p.gift_point,
            sku.id sku_id,
            sku.price sku_price,
            sku.sku_code sku_sku_code,
            sku.promotion_price sku_promotion_price,
            sku.stock sku_stock,
            sku.lock_stock sku_lock_stock,
            ladder.id ladder_id,
            ladder.count ladder_count,
            ladder.discount ladder_discount,
            full_re.id full_id,
            full_re.full_price full_full_price,
            full_re.reduce_price full_reduce_price
        FROM
        tl_mall_goods.pms_product p
            LEFT JOIN tl_mall_goods.pms_sku_stock sku ON p.id = sku.product_id
            LEFT JOIN tl_mall_goods.pms_product_ladder ladder ON p.id = ladder.product_id
            LEFT JOIN tl_mall_goods.pms_product_full_reduction full_re ON p.id = full_re.product_id
        WHERE
            p.id IN
        <foreach collection="ids" open="(" close=")" item="id" separator=",">
            #{id}
        </foreach>

    </select>
    <!-- 查看商品详情 -->
    <!--<select id="getProductInfo" resultMap="productInfoMap">
        SELECT *,l.id ladder_id,l.product_id ladder_product_id,l.discount ladder_discount,l.count ladder_count,l.price ladder_price,
        pf.id full_id,pf.product_id full_product_id,pf.full_price full_full_price,pf.reduce_price full_reduce_price,
        m.id member_id,m.product_id member_product_id,m.member_level_id member_member_level_id,m.member_price member_member_price,m.member_level_name member_member_level_name,
        s.id sku_id,s.product_id sku_product_id,s.price sku_price,s.low_stock sku_low_stock,s.pic sku_pic,s.sale sku_sale,s.sku_code sku_sku_code,s.sp1 sku_sp1,s.sp2 sku_sp2,s.sp3 sku_sp3,s.stock sku_stock,
        a.id attribute_id,a.product_id attribute_product_id,a.product_attribute_id attribute_product_attribute_id,a.value attribute_value
        ,pr.flash_promotion_price,pr.flash_promotion_count,pr.flash_promotion_limit
        ,sf.status flash_promotion_status,sf.start_date flash_promotion_start_date,sf.end_date flash_promotion_end_date
        FROM pms_product p
        LEFT JOIN pms_product_ladder l ON p.id = l.product_id
        LEFT JOIN pms_product_full_reduction pf ON pf.product_id=p.id
        LEFT JOIN pms_member_price m ON m.product_id = p.id
        LEFT JOIN pms_sku_stock s ON s.product_id = p.id
        LEFT JOIN pms_product_attribute_value a ON a.product_id=p.id
        left join sms_flash_promotion_product_relation pr on p.id = pr.product_id
        left join sms_flash_promotion sf on pr.flash_promotion_id = sf.id and sf.end_date > NOW() and NOW() >= sf.start_date and sf.status=1
        WHERE p.id=#{id}
        <if test="flashPromotionId != null">
            and pr.flash_promotion_id=#{flashPromotionId}
        </if>
        <if test="flashPromotionSessionId != null ">
            and pr.flash_promotion_session_id=#{flashPromotionSessionId}
        </if>
    </select>-->
    <select id="getProductInfo" resultMap="productInfoMap">
        SELECT *,l.id ladder_id,l.product_id ladder_product_id,l.discount ladder_discount,l.count ladder_count,l.price ladder_price,
        pf.id full_id,pf.product_id full_product_id,pf.full_price full_full_price,pf.reduce_price full_reduce_price,
        m.id member_id,m.product_id member_product_id,m.member_level_id member_member_level_id,m.member_price member_member_price,m.member_level_name member_member_level_name,
        s.id sku_id,s.product_id sku_product_id,s.price sku_price,s.low_stock sku_low_stock,s.pic sku_pic,s.sale sku_sale,s.sku_code sku_sku_code,s.sp1 sku_sp1,s.sp2 sku_sp2,s.sp3 sku_sp3,s.stock sku_stock,
        a.id attribute_id,a.product_id attribute_product_id,a.product_attribute_id attribute_product_attribute_id,a.value attribute_value
        FROM tl_mall_goods.pms_product p
        LEFT JOIN tl_mall_goods.pms_product_ladder l ON p.id = l.product_id
        LEFT JOIN tl_mall_goods.pms_product_full_reduction pf ON pf.product_id=p.id
        LEFT JOIN tl_mall_goods.pms_member_price m ON m.product_id = p.id
        LEFT JOIN tl_mall_goods.pms_sku_stock s ON s.product_id = p.id
        LEFT JOIN tl_mall_goods.pms_product_attribute_value a ON a.product_id=p.id
        WHERE p.id=#{id}
    </select>

    <select id="getAllProductId" resultType="java.lang.Long">
        select p.id from tl_mall_goods.pms_product p where p.delete_status=0
    </select>
</mapper>