<?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.wzj.soopin.goods.mapper.ProductMapper">

    <select id="getlist" resultMap="ProductWithSkusResultMap">
        SELECT
        p.*,
        b.name AS brandName,
        b.logo AS brandLogo,
        t.store_name AS contactUserName,
        t.contact_phone AS contactPhone,
        pc.name AS productCategoryName,
        s.id AS sku_id,
        s.out_sku_id,
        s.price AS skuPrice,
        s.pic AS skuPic,
        s.stock,
        s.sp_data,
        s.product_id
        FROM
        pms_product p
        LEFT JOIN pms_brand b ON p.brand_id = b.id
        LEFT JOIN sys_tenant t ON p.tenant_id = t.tenant_id
        LEFT JOIN pms_product_category pc ON p.category_id = pc.id
        LEFT JOIN pms_sku s ON p.id = s.product_id
        <where>
            <if test="query.nameLike != null and query.nameLike != ''">
                AND p.name LIKE CONCAT('%', #{query.nameLike}, '%')
            </if>
            <if test="query.outProductId != null and query.outProductId != ''">
                AND p.out_product_id = #{query.outProductId}
            </if>
            <if test="query.brandId != null">
                AND p.brand_id = #{query.brandId}
            </if>
            <if test="query.publishStatus != null">
                AND p.publish_status = #{query.publishStatus}
            </if>
            <if test="query.authFlag != null">
                AND p.auth_flag = #{query.authFlag}
            </if>
            <if test="query.categoryId != null">
                AND p.category_id = #{query.categoryId}
            </if>
            and p.del_flag = '0' and s.del_flag = '0'
        </where>

        ORDER BY
        p.sales DESC,
        p.price ASC,
        CASE p.auth_flag
        WHEN 1 THEN 1
        WHEN 3 THEN 2
        WHEN 2 THEN 3
        ELSE 4
        END
    </select>

    <select id="selectProductWithSkus" resultMap="ProductWithSkusResultMap">
        SELECT
            p.*,
            pc.name AS productCategoryName,
            s.id AS sku_id,
            s.out_sku_id,
            s.price as skuPrice,
            s.pic as skuPic,
            s.stock,
            s.sp_data
        FROM
            pms_product p
                LEFT JOIN pms_product_category pc ON p.category_id = pc.id
                LEFT JOIN pms_sku s ON p.id = s.product_id
        WHERE
            p.id = #{id}
            and p.del_flag = '0'
            and s.del_flag = '0'
    </select>

    <select id="getProduct" resultType="com.wzj.soopin.goods.domain.vo.ProductVO">
        SELECT
        p.*,
        t.store_name AS storeName
        FROM
        pms_product p
        LEFT JOIN
        sys_tenant t ON p.tenant_id = t.tenant_id
        <where>
            <if test="query.nameLike != null and query.nameLike != ''">
                AND p.name LIKE CONCAT('%', #{query.nameLike}, '%')
            </if>
            <if test="query.publishStatus != null">
                AND p.publish_status = #{query.publishStatus}
            </if>
            <if test="query.categoryIds != null and query.categoryIds.size() > 0">
                AND p.category_id IN
                <foreach collection="query.categoryIds" item="categoryId" open="(" separator="," close=")">
                    #{categoryId}
                </foreach>
            </if>
        </where>
    </select>

    <resultMap id="ProductWithSkusResultMap" type="ProductVO">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="productCategoryName" column="productCategoryName"/>
        <collection property="skuList" ofType="SkuVO">
            <id property="id" column="sku_id"/>
            <result property="outSkuId" column="out_sku_id"/>
            <result property="price" column="skuPrice"/>
            <result property="pic" column="skuPic"/>
            <result property="stock" column="stock"/>
            <result property="spData" column="sp_data"/>
        </collection>
    </resultMap>

</mapper>
