<?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">

    <resultMap type="Product" id="ProductResult">
        <result property="id" column="id"/>
        <result property="brandId" column="brand_id"/>
        <result property="categoryId" column="category_id"/>
        <result property="outProductId" column="out_product_id"/>
        <result property="name" column="name"/>
        <result property="pic" column="pic"/>
        <result property="albumPics" column="album_pics"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="sort" column="sort"/>
        <result property="price" column="price"/>
        <result property="unit" column="unit"/>
        <result property="weight" column="weight"/>
        <result property="detailHtml" column="detail_html"/>
        <result property="detailMobileHtml" column="detail_mobile_html"/>
        <result property="brandName" column="brand_name"/>
        <result property="productCategoryName" column="product_category_name"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time" />
    </resultMap>

    <sql id="selectProductVo">
        select id, brand_id, category_id, out_product_id, name, pic, album_pics, publish_status, sort, price, unit, weight, detail_html, detail_mobile_html, brand_name, product_category_name, create_by, create_time, update_by, update_time   from pms_product
    </sql>

    <select id="selectByEntity" parameterType="Product" resultMap="ProductResult">
        <include refid="selectProductVo"/>
        <where>
            <if test="brandId != null "> and brand_id = #{brandId}</if>
            <if test="categoryId != null "> and category_id = #{categoryId}</if>
            <if test="outProductId != null  and outProductId != ''"> and out_product_id = #{outProductId}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="pic != null  and pic != ''"> and pic = #{pic}</if>
            <if test="albumPics != null  and albumPics != ''"> and album_pics = #{albumPics}</if>
            <if test="publishStatus != null "> and publish_status = #{publishStatus}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="price != null "> and price = #{price}</if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>
            <if test="weight != null "> and weight = #{weight}</if>
            <if test="detailHtml != null  and detailHtml != ''"> and detail_html = #{detailHtml}</if>
            <if test="detailMobileHtml != null  and detailMobileHtml != ''"> and detail_mobile_html = #{detailMobileHtml}</if>
            <if test="brandName != null  and brandName != ''"> and brand_name like concat('%', #{brandName}, '%')</if>
            <if test="productCategoryName != null  and productCategoryName != ''"> and product_category_name like concat('%', #{productCategoryName}, '%')</if>
        </where>
    </select>

    <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,
        s.pic,
        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 1=1
        <if test="query.nameLike != null and query.nameLike != ''">
            AND p.name LIKE CONCAT('%', #{query.nameLike}, '%')
        </if>
        <if test="query.outProductId != null">
            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>
        <if test="tenantIds != null and tenantIds.size() > 0">
            AND p.tenant_id  IN
            <foreach collection="tenantIds" item="tenantId" open="(" separator="," close=")">
                #{tenantId}
            </foreach>
        </if>

        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.*,
            s.id AS sku_id,
            s.out_sku_id,
            s.price,
            s.pic,
            s.stock,
            s.sp_data
        FROM
            pms_product p
                LEFT JOIN
            pms_sku s ON p.id = s.product_id
        WHERE
            p.id = #{id}
    </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.categoryId != null">
                AND p.category_id = #{query.categoryId}
            </if>
        </where>
    </select>

    <resultMap id="ProductWithSkusResultMap" type="ProductVO">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="status" column="status"/>

        <collection property="skuList" ofType="SkuVO">
            <id property="id" column="sku_id"/>
            <result property="outSkuId" column="out_sku_id"/>
            <result property="price" column="price"/>
            <result property="pic" column="pic"/>
            <result property="stock" column="stock"/>
            <result property="spData" column="sp_data"/>
        </collection>
    </resultMap>

</mapper>
