<?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.storlead.sales.mapper.sku.SsdProductMapper">

        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="com.storlead.sales.pojo.entity.sku.SsdProduct">

        </resultMap>

        <select id="queryProductTotal" resultType="java.lang.Long">
                SELECT COUNT(DISTINCT sp.ssd_product_id  )
                FROM ssd_product sp
                LEFT JOIN ssd_order_detail sod ON sod.ssd_product_id = sp.ssd_product_id
                LEFT JOIN ssd_order so ON so.ssd_order_id = sod.ssd_order_id AND so.order_status IN (6,7)
                WHERE 1=1
                AND sp.`status` in (1,2,3)
                <if test="productName != null and productName != ''">
                        AND sp.product_name LIKE '%${productName}%'
                </if>
                <if test="ssdProductId != null and ssdProductId != ''">
                        AND sp.ssd_product_id = #{ssdProductId}
                </if>
                <if test="status != null and status > 0">
                        AND sp.STATUS = #{status}
                </if>
                <if test="ssdProductTypeId != null and ssdProductTypeId != 0">
                        AND sp.ssd_product_type_id = #{ssdProductTypeId}
                </if>
        </select>

        <select id="queryProductPageList" resultType="com.storlead.sales.pojo.vo.sku.SsdProductVO">
                SELECT
                sp.ssd_product_id                 AS ssdProductId,
                sp.ssd_product_type_id            AS ssdProductTypeId,
                sp.product_name                   AS productName,
                sp.STATUS,
                COUNT(DISTINCT viw_order.ssd_order_id)  AS orderCount,
                SUM(IFNULL(viw_order.product_count,0))  AS shipmentCount
                FROM ssd_product sp
                LEFT JOIN
                (
                SELECT sod.ssd_product_id, sod.product_count, so.ssd_order_id
                FROM ssd_order so
                LEFT JOIN ssd_order_detail sod ON sod.ssd_order_id = so.ssd_order_id
                WHERE so.ssd_order_id = sod.ssd_order_id AND so.order_status IN (6, 7)
                GROUP BY so.ssd_order_id
                )
                AS viw_order ON viw_order.ssd_product_id = sp.ssd_product_id
                WHERE 1=1
                AND sp.`status` in (1,2,3)
                <if test="productName != null and productName != ''">
                        AND sp.product_name LIKE '%${productName}%'
                </if>
                <if test="ssdProductId != null and ssdProductId != ''">
                        AND sp.ssd_product_id = #{ssdProductId}
                </if>
                <if test="status != null and status > 0">
                        AND sp.STATUS = #{status}
                </if>
                <if test="ssdProductTypeId != null and ssdProductTypeId != 0">
                        AND sp.ssd_product_type_id = #{ssdProductTypeId}
                </if>
                GROUP BY sp.ssd_product_id
                ORDER BY sp.top_sort_num DESC,sp.ssd_product_id DESC
                <if test="pageIndex != null and pageSize != null">
                        LIMIT #{pageIndex}, #{pageSize}
                </if>

        </select>

        <!--产品列表分页COUNT storage-->
        <select id="queryProductCount" resultType="java.lang.Long">
                SELECT
                COUNT(DISTINCT sp.ssd_product_id)
                FROM ssd_product sp
                LEFT JOIN ssd_product_resource spr ON sp.ssd_product_id = spr.ssd_product_id
                WHERE 1=1

                <if test="productName != null and productName != ''">
                        AND sp.product_name LIKE '%${productName}%'
                </if>
                <if test="ssdProductId != null and ssdProductId != ''">
                        AND sp.ssd_product_id = #{ssdProductId}
                </if>
                <if test="connectorType != null and connectorType != ''">
                        AND sp.connector_type = #{connectorType}
                </if>
                <if test="productSize != null and productSize != ''">
                        AND sp.product_size = #{productSize}
                </if>
                <if test="status != null and status > 0">
                        AND sp.STATUS = #{status}
                </if>
                <if test="ssdProductTypeId != null and ssdProductTypeId != 0">
                        AND sp.ssd_product_type_id = #{ssdProductTypeId}
                </if>

        </select>

        <select id="getSubProductCount" resultType="integer">
                SELECT
                        COUNT(DISTINCT ssd_product_id)
                FROM
                        ssd_product
                WHERE
                        1 = 1
                  AND ssd_product.STATUS = 2
                  AND ssd_product.ssd_product_type_id = #{ssdProductTypeId}
        </select>

</mapper>
