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

        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="com.storlead.sales.pojo.entity.sku.SsdSku">
                <id column="ssd_sku_id" property="ssdSkuId" />
                <result column="ssd_product_type_id" property="ssdProductTypeId" />
                <result column="ssd_sku_code" property="ssdSkuCode" />
                <result column="connector_type" property="connectorType" />
                <result column="product_size" property="productSize" />
                <result column="connector_protocol" property="connectorProtocol" />
                <result column="core_chip_type" property="coreChipType" />
                <result column="product_level" property="productLevel" />
                <result column="flash_chip_type" property="flashChipType" />
                <result column="capacity" property="capacity" />
                <result column="open_physical_destroy" property="openPhysicalDestroy" />
                <result column="force_open_physical_destroy" property="forceOpenPhysicalDestroy" />
                <result column="open_logic_destroy" property="openLogicDestroy" />
                <result column="force_open_logic_destroy" property="forceOpenLogicDestroy" />
                <result column="open_aes_encrypt" property="openAesEncrypt" />
                <result column="force_open_aes_encrypt" property="forceOpenAesEncrypt" />
                <result column="open_rapid_backup" property="openRapidBackup" />
                <result column="force_open_rapid_backup" property="forceOpenRapidBackup" />
                <result column="datasheet_name" property="datasheetName" />
                <result column="datasheet_url" property="datasheetUrl" />
                <result column="material_pricing" property="materialPricing" />
                <result column="processing_pricing" property="processingPricing" />
                <result column="ssd_core_chip_id" property="ssdCoreChipId" />
                <result column="ssd_core_json" property="ssdCoreJson" />
                <result column="ssd_flash_chip_id" property="ssdFlashChipId" />
                <result column="ssd_flash_json" property="ssdFlashJson" />
                <result column="flash_chip_count" property="flashChipCount" />
                <result column="physical_destroy_price" property="physicalDestroyPrice" />
                <result column="logic_destroy_price" property="logicDestroyPrice" />
                <result column="aes_encrypt_price" property="aesEncryptPrice" />
                <result column="rapid_backup_price" property="rapidBackupPrice" />
                <result column="customer_rate" property="customerRate" />
                <result column="repeat_data" property="repeatData" />
                <result column="remark" property="remark" />
                <result column="status" property="status" />
                <result column="additional_surcharges" property="additionalSurcharges" />
        </resultMap>

        <resultMap id="listResultMap" extends="BaseResultMap" type="com.storlead.sales.pojo.vo.sku.SsdSkuVO">
                <result column="shipmentCount" property="shipmentCount"/>
        </resultMap>
        <select id="list" resultMap="listResultMap">
                SELECT
                SUM(IFNULL(viw_order.product_count,0)) AS shipmentCount,
                sku.*
                FROM ssd_sku sku
                LEFT JOIN
                (
                SELECT sod.ssd_product_id, sod.product_count, so.ssd_order_id,sod.ssd_sku_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_sku_id = sku.ssd_sku_id
                WHERE 1=1
                <if test="ssdSkuId !=null and ssdSkuId > 0">
                        AND sku.ssd_sku_id = #{ssdSkuId}
                </if>
                <if test="ssdSkuCode !=null and ssdSkuCode !='' ">
                        AND sku.ssd_sku_code = #{ssdSkuCode}
                </if>
                <if test="connectorType !=null and connectorType !='' ">
                        AND sku.connector_type = #{connectorType}
                </if>
                <if test="productSize !=null and productSize !='' ">
                        AND sku.product_size = #{productSize}
                </if>
                <if test="connectorProtocol !=null and connectorProtocol !='' ">
                        AND sku.connector_protocol = #{connectorProtocol}
                </if>
                <if test="coreChipType !=null and coreChipType !='' ">
                        AND sku.core_chip_type = #{coreChipType}
                </if>
                <if test="productLevel !=null and productLevel !='' ">
                        AND sku.product_level = #{productLevel}
                </if>
                <if test="flashChipType !=null and flashChipType !='' ">
                        AND sku.flash_chip_type = #{flashChipType}
                </if>
                <if test="capacity !=null and capacity !='' ">
                        AND sku.capacity = #{capacity}
                </if>
                <if test="status !=null and status > 0 ">
                        AND sku.status = #{status}
                </if>
                GROUP BY sku.ssd_sku_id


                <if test="pageIndex !=null  and pageSize != null">
                        LIMIT #{pageIndex}, #{pageSize}
                </if>

        </select>

        <!--SSD SKU 总计-->
        <select id="total" resultType="java.lang.Long">
                SELECT
                COUNT(DISTINCT sku.ssd_sku_id)
                FROM ssd_sku sku
                LEFT JOIN ssd_order_detail sod ON sod.ssd_sku_id = sku.ssd_sku_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
                <if test="ssdSkuId !=null and ssdSkuId > 0">
                        AND sku.ssd_sku_id = #{ssdSkuId}
                </if>
                <if test="ssdSkuCode !=null and ssdSkuCode !='' ">
                        AND sku.ssd_sku_code = #{ssdSkuCode}
                </if>
                <if test="connectorType !=null and connectorType !='' ">
                        AND sku.connector_type = #{connectorType}
                </if>
                <if test="productSize !=null and productSize !='' ">
                        AND sku.product_size = #{productSize}
                </if>
                <if test="connectorProtocol !=null and connectorProtocol !='' ">
                        AND sku.connector_protocol = #{connectorProtocol}
                </if>
                <if test="coreChipType !=null and coreChipType !='' ">
                        AND sku.core_chip_type = #{coreChipType}
                </if>
                <if test="productLevel !=null and productLevel !='' ">
                        AND sku.product_level = #{productLevel}
                </if>
                <if test="flashChipType !=null and flashChipType !='' ">
                        AND sku.flash_chip_type = #{flashChipType}
                </if>
                <if test="capacity !=null and capacity !='' ">
                        AND sku.capacity = #{capacity}
                </if>
                <if test="status !=null and status > 0 ">
                        AND sku.status = #{status}
                </if>
        </select>

        <!--SSD SKU 详情 映射-->
        <resultMap id="detailResultMap" extends="BaseResultMap" type="com.storlead.sales.pojo.vo.sku.SsdSkuVO">

                <result column="flash_chip_model" property="ssdFlashChipModel"/>
                <result column="core_chip_model" property="ssdCoreChipModel"/>


        </resultMap>

        <select id="detail" resultMap="detailResultMap">
                SELECT sku.*,
                       rate.ssd_sku_area_rate_id,
                       rate.min_count,
                       rate.max_count,
                       rate.rate,
                       cycle.ssd_product_cycle_id,
                       cycle.min_count       AS minCount,
                       cycle.max_count       AS maxCount,
                       cycle.estimate_day,

                       flash_chip.flash_chip_model,
                       core_chip.core_chip_model,

                       flash_price.min_count AS flashMinCount,
                       flash_price.max_count AS flashMaxCount,
                       flash_price.price     AS flashPrice,
                       chip_price.min_count  AS chipMinCount,
                       chip_price.max_count  AS chipMaxCount,
                       chip_price.price      AS chipPrice

                FROM ssd_sku sku
                             LEFT JOIN ssd_sku_area_rate rate ON sku.ssd_sku_id = rate.ssd_sku_id
                             LEFT JOIN ssd_sku_product_cycle cycle ON sku.ssd_sku_id = cycle.ssd_sku_id
                             LEFT JOIN ssd_flash_chip_info flash_chip ON sku.ssd_flash_chip_id = flash_chip.ssd_flash_chip_id
                             LEFT JOIN ssd_flash_chip_area_price flash_price ON flash_chip.ssd_flash_chip_id = flash_price.ssd_flash_chip_id
                             LEFT JOIN ssd_core_chip_info core_chip ON core_chip.ssd_core_chip_id = sku.ssd_core_chip_id
                             LEFT JOIN ssd_core_chip_area_price chip_price ON sku.ssd_core_chip_id = chip_price.ssd_core_chip_id
                WHERE sku.ssd_sku_id = #{ssdSkuId}
        </select>

        <!-- 通用查询映射结果 -->
        <resultMap id="MakeOrderSkuDetailResultMap" type="com.storlead.sales.pojo.vo.sku.MakeOrderSkuDetailVO">
                <id column="ssd_sku_id" property="ssdSkuId"/>
                <result column="ssd_flash_chip_id" property="ssdFlashShipId"/>
                <result column="ssd_core_chip_id" property="ssdCoreShipId"/>
                <result column="ssd_sku_code" property="ssdSkuCode"/>
                <result column="connector_type" property="connectorType"/>
                <result column="product_size" property="productSize"/>
                <result column="connector_protocol" property="connectorProtocol"/>
                <result column="core_chip_type" property="coreChipType"/>
                <result column="product_level" property="productLevel"/>
                <result column="flash_chip_type" property="flashChipType"/>
                <result column="capacity" property="capacity"/>
                <result column="flash_chip_count" property="flashChipCount"/>
                <result column="physical_destroy_price" property="physicalDestroyPrice"/>
                <result column="open_physical_destroy" property="openPhysicalDestroy"/>
                <result column="logic_destroy_price" property="logicDestroyPrice"/>
                <result column="open_logic_destroy" property="openLogicDestroy"/>
                <result column="aes_encrypt_price" property="aesEncryptPrice"/>
                <result column="open_aes_encrypt" property="openAesEncrypt"/>
                <result column="rapid_backup_price" property="rapidBackupPrice"/>
                <result column="open_rapid_backup" property="openRapidBackup"/>
                <result column="core_chip_price" property="coreChipPrice"/>
                <result column="flash_chip_price" property="flashChipPrice"/>
                <result column="material_pricing" property="materialPricing"/>
                <result column="processing_pricing" property="processingPricing"/>
                <result column="rate" property="rate"/>
                <result column="ssd_core_ship_id" property="ssdCoreShipId"/>
                <result column="ssd_flash_ship_id" property="ssdFlashShipId"/>
                <result column="estimate_day" property="estimateDay"/>
                <result column="datasheet_name" property="datasheetName"/>
                <result column="datasheet_url" property="datasheetUrl"/>
                <result column="force_open_physical_destroy" property="forceOpenPhysicalDestroy"/>
                <result column="force_open_logic_destroy" property="forceOpenLogicDestroy"/>
                <result column="force_open_aes_encrypt" property="forceOpenAesEncrypt"/>
                <result column="force_open_rapid_backup" property="forceOpenRapidBackup"/>
                <result column="additional_surcharges" property="additionalSurcharges"/>
        </resultMap>

        <select id="queryMakeOrderSkuDetailBy" resultMap="MakeOrderSkuDetailResultMap">
                SELECT
                ss.ssd_sku_id,
                ss.ssd_sku_code,
                ss.connector_type,
                ss.connector_protocol,
                ss.product_level,
                ss.product_size,
                ss.core_chip_type,
                ss.flash_chip_type,
                ss.capacity,
                ss.flash_chip_count,
                ss.material_pricing,
                ss.processing_pricing,
                ss.ssd_core_chip_id,
                ss.ssd_flash_chip_id,
                ss.open_physical_destroy,
                ss.datasheet_name,
                ss.datasheet_url,
                ss.additional_surcharges,
                ss.force_open_physical_destroy,
                ss.force_open_logic_destroy   ,
                ss.force_open_aes_encrypt     ,
                ss.force_open_rapid_backup    ,
                IF(ss.open_physical_destroy = 1, ss.physical_destroy_price, - 1) as physical_destroy_price,
                ss.open_logic_destroy,
                IF(ss.open_logic_destroy = 1, ss.logic_destroy_price ,-1)        as logic_destroy_price,
                ss.open_aes_encrypt,
                IF(ss.open_aes_encrypt = 1 ,ss.aes_encrypt_price, - 1)           as aes_encrypt_price,
                ss.open_rapid_backup,
                IF(ss.open_rapid_backup = 1,ss.rapid_backup_price, - 1)          as rapid_backup_price,
                scsa.price                                                       as core_chip_price,
                ssap.price                                                       as flash_chip_price,
                sar.rate,
                viw_cycle.estimate_day
                FROM ssd_sku ss
                LEFT JOIN ssd_sku_product_cycle sspc
                on ss.ssd_sku_id = sspc.ssd_sku_id
                and #{productCount} BETWEEN sspc.min_count and sspc.max_count
                LEFT JOIN ssd_core_chip_area_price scsa
                on scsa.ssd_core_chip_id = ss.ssd_core_chip_id
                and #{productCount} BETWEEN scsa.min_count and scsa.max_count
                LEFT JOIN ssd_flash_chip_area_price ssap
                on ssap.ssd_flash_chip_id = ss.ssd_flash_chip_id
                AND #{productCount} BETWEEN ssap.min_count and ssap.max_count
                LEFT JOIN ssd_sku_area_rate sar
                on sar.ssd_sku_id = ss.ssd_sku_id
                AND #{productCount} BETWEEN sar.min_count and sar.max_count
                LEFT JOIN (
                SELECT
                sspc.estimate_day ,
                sspc.ssd_sku_id
                FROM ssd_sku_product_cycle sspc
                where  sspc.min_count &lt;=#{productCount}
                and if(sspc.max_count = -1, #{productCount}+1, sspc.max_count) >= #{productCount}
                ) as viw_cycle on viw_cycle.ssd_sku_id = ss.ssd_sku_id
                where ss.status = 2
                <if test="ssdSkuId == null">
                        and ss.connector_type = #{connectorType}
                        and ss.connector_protocol = #{connectorProtocol}
                        and ss.product_level = #{productLevel}
                        and ss.product_size = #{productSize}
                        and ss.capacity = #{capacity}
                        and ss.core_chip_type = #{coreChipType}
                        and ss.flash_chip_type = #{flashChipType}
                </if>
                <if test="ssdSkuId != null">
                        and ss.ssd_sku_id = #{ssdSkuId}
                </if>
                <if test="ssdSkuCode != null and ssdSkuCode != ''">
                        and ss.ssd_sku_code = #{ssdSkuCode}
                </if>
                <if test="repeatData != null">
                        and ss.repeat_data = #{repeatData}
                </if>
                <if test="productCount != null and productCount >0">
                        and #{productCount} BETWEEN sspc.min_count and sspc.max_count
                        and #{productCount} BETWEEN scsa.min_count and scsa.max_count
                        AND #{productCount} BETWEEN ssap.min_count and ssap.max_count
                        AND #{productCount} BETWEEN sar.min_count and sar.max_count
                </if>

        </select>

        <!--获取字典编码 dict_code-->
        <select id="queryDataDictCodeList" resultType="java.lang.String">
                SELECT dict_code FROM data_dict where status = 1 and dict_type = #{dictType} order by sort_num asc
        </select>


</mapper>
