<?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="cn.com.xingxi.store.data.mapper.WxaStoreProductMapper">

    <resultMap id="ProductResult" type="ProductDTO">
        <id property="productId"    column="prodId"/>
        <result property="orgProductId" column="orgProdId"/>
        <result property="prodName"    column="prodName"/>
        <result property="subTitle"    column="subTitle"/>
        <result property="prodType"    column="prodType"/>
        <result property="description"    column="prodContent"/>
        <association property="productPrice" select="selectMinProdPriceBy"
                     column="mercId=mercId,prodId=prodId"/>
        <association property="productVendorPrice" select="selectVendorMinProdPriceBy"
                     column="prodId=prodId"/>
        <collection property="imageUrls" select="selectProductImagesByProductId" column="prodId"/>
    </resultMap>

    <resultMap id="ProductAttrResult" type="ProductAttrDTO">
        <id property="id"    column="propId"/>
        <result property="label"    column="propName"/>
        <collection property="values" javaType="java.util.List" ofType="ProductAttrDTO">
            <result property="id"    column="ppvId"/>
            <result property="label"    column="prodPropValueName"/>
        </collection>
    </resultMap>

    <resultMap id="ProductSkuResult" type="ProductSkuDTO">
        <result property="id"    column="pprId"/>
        <result property="name"    column="pprName"/>
        <result property="attrs"    column="propIds"/>
<!--        <association property="price" select="selectProdPriceBy"-->
<!--                     column="mercId=mercId,prodId=prodId,pprId=pprId"/>-->
        <collection property="imageUrls" javaType="java.util.List" ofType="ImageDTO">
            <result property="imageId"    column="fileId"/>
            <result property="imageUrl"    column="fileUrl"/>
        </collection>
    </resultMap>


    <resultMap id="CategoryResult" type="CategoryDTO">
        <id property="categoryId"    column="prodkindId"/>
        <result property="categoryName"    column="prodkindName"/>
    </resultMap>


    <resultMap id="BrandResult" type="BrandDTO">
        <id property="brandId"    column="brandId"/>
        <result property="brandName"    column="brandName"/>
        <result property="logoImageId"    column="logoImageId"/>
        <result property="logoImageUrl"    column="logoImageUrl"/>
        <result property="brandImageId"    column="brandImageId"/>
        <result property="brandImageUrl"    column="brandImageUrl"/>
        <result property="brandDesc"    column="brandDesc"/>
<!--        <collection property="logoImageUrls" javaType="java.util.List" ofType="ImageDTO">-->
<!--            <id property="imageId"    column="logoFileId"/>-->
<!--            <result property="imageUrl"    column="logoFileUrl"/>-->
<!--        </collection>-->
    </resultMap>


    <resultMap id="ImagesResult" type="ImageDTO">
        <id property="imageId"    column="fileId"/>
        <result property="imageUrl"    column="fileUrl"/>
    </resultMap>


    <resultMap id="ProductDesignImageResult" type="ProductDesignImageDTO">
        <id property="designId"    column="prodDesignId"/>
        <result property="surfaceSeq"    column="surfaceSeq"/>
        <result property="designName" column="designName"/>
        <result property="suggestTech"    column="suggestTech"/>
        <result property="optionalTech"    column="optionalTech"/>
        <result property="point1X"    column="point1X"/>
        <result property="point1Y"    column="point1Y"/>
        <result property="point2X"    column="point2X"/>
        <result property="point2Y"    column="point2Y"/>
        <result property="printHeight"    column="printHeight"/>
        <result property="printWidth"    column="printWidth"/>
        <collection property="clearImgs" javaType="java.util.List" ofType="ImageDTO">
            <id property="imageId"    column="clearImgId"/>
            <result property="imageUrl"    column="clearImgUrl"/>
        </collection>
    </resultMap>

    <resultMap id="ProductOneResult" type="ProductDTO">
        <id property="productId"    column="prodId"/>
        <result property="orgProductId" column="orgProdId"/>
        <result property="prodName"    column="prodName"/>
        <result property="subTitle"    column="subTitle"/>
        <result property="prodType"    column="prodType"/>
        <result property="description"    column="prodContent"/>
        <result property="skuId"    column="pprId"/>
        <result property="propertyName"    column="pprName"/>
        <association property="productPrice" select="selectMinProdPriceBy"
                     column="mercId=mercId,prodId=prodId"/>
        <association property="productVendorPrice" select="selectVendorMinProdPriceBy"
                     column="prodId=prodId"/>
        <collection property="imageUrls" ofType="ImageDTO" javaType="java.util.List">
            <result property="imageId"    column="fileId"/>
            <result property="imageUrl"    column="fileUrl"/>
        </collection>
    </resultMap>


    <resultMap id="selectProductBySkuIdProductOneResult" type="ProductDTO">
        <id property="productId"    column="prodId"/>
        <result property="orgProductId" column="orgProdId"/>
        <result property="prodName"    column="prodName"/>
        <result property="subTitle"    column="subTitle"/>
        <result property="prodType"    column="prodType"/>
        <result property="description"    column="prodContent"/>
        <result property="skuId"    column="pprId"/>
        <result property="propertyName"    column="pprName"/>
        <result property="appStatus"    column="appStatus"/>
        <result property="brandId"    column="brandId"/>
<!--        <association property="productPrice" select="selectProdPriceBy"-->
<!--                     column="mercId=mercId,prodId=prodId,pprId=pprId"/>-->
        <collection property="imageUrls" ofType="ImageDTO" javaType="java.util.List">
            <result property="imageId"    column="fileId"/>
            <result property="imageUrl"    column="fileUrl"/>
        </collection>
    </resultMap>



    <select id="selectProductImagesByProductId" parameterType="Long" resultMap="ImagesResult">
        SELECT m_prod_picture.picImgId, t_attch_file.fileId, t_attch_file.fileUrl
        FROM m_prod_picture
        INNER JOIN t_attch_file ON m_prod_picture.picImgId = t_attch_file.fileId
        WHERE m_prod_picture.prodId = #{prodId}
        ORDER BY m_prod_picture.sort
    </select>

    <select id="selectProductList" parameterType="ProductDTO" resultMap="ProductResult">
        SELECT m_prod.prodId, m_merc_prod.orgProdId, m_prod.prodName, m_prod.prodType, m_prod.subTitle, m_merc_prod.mercId
        FROM m_merc_prod
        LEFT JOIN m_prod ON m_merc_prod.prodId = m_prod.prodId
        <where>
            m_merc_prod.supplyFlag = '1'
            AND m_merc_prod.shelfFlag = '1'
            AND m_merc_prod.delFlag = '0'
            AND m_merc_prod.mercId = #{mercId}
            <if test="categoryName != null and categoryName != ''">AND m_merc_prod.prodkindId = #{categoryName}</if>
            <if test="prodName != null and prodName != ''">AND (m_prod.prodName like concat('%',#{prodName},'%') OR m_prod.subTitle like concat('%',#{prodName},'%'))</if>
            <if test="brandId != null and brandId != ''">AND m_prod.brandId = #{brandId} </if>
            <if test="brandName != null and brandName != ''">
                AND m_prod.brandId IN
                <foreach collection="brandNames" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY m_merc_prod.prodSort
    </select>

    <select id="selectByProductId" resultMap="ProductOneResult">
        SELECT m_prod.prodId,m_merc_prod.orgProdId,  m_prod.prodName, m_prod.subTitle,
               m_prod.prodType, m_prod.prodContent,m_merc_prod.mercId,
               m_prod_picture.picImgId, t_attch_file.fileId,
               t_attch_file.fileUrl,
               m_merc_prod.mercId, m_prod.brandId
        FROM m_merc_prod
        LEFT JOIN m_prod ON m_merc_prod.prodId = m_prod.prodId
        INNER JOIN m_prod_picture ON m_prod_picture.prodId = m_prod.prodId AND m_prod_picture.delFlag = '0'
        INNER JOIN t_attch_file ON t_attch_file.fileId = m_prod_picture.picImgId
        WHERE m_merc_prod.supplyFlag = '1'
          AND m_merc_prod.shelfFlag = '1'
          AND m_merc_prod.delFlag = '0'
          AND m_merc_prod.mercId = #{mercId}
          AND m_merc_prod.prodId = #{productId}
        ORDER BY m_merc_prod.prodSort, m_prod_picture.sort
    </select>


    <select id="selectAttrByProductId" resultMap="ProductAttrResult">
        SELECT m_prod_prop.propId,m_prod_prop.propName,
               m_prod_prop_value.ppvId, m_prod_prop_value.prodPropValueName
        FROM m_merc_prod
        INNER JOIN m_prod_prop ON m_merc_prod.orgProdId = m_prod_prop.prodId
        INNER JOIN m_prod_prop_value ON m_prod_prop.prodId = m_prod_prop_value.prodId AND m_prod_prop_value.prodPropId = m_prod_prop.propId
        WHERE m_merc_prod.delFlag = '0'
          AND m_prod_prop.delFlag = '0'
          AND m_prod_prop_value.delFlag = '0'
          AND m_merc_prod.supplyFlag = '1'
          AND m_merc_prod.shelfFlag = '1'
          AND m_merc_prod.mercId = #{mercId}
          AND m_merc_prod.prodId = #{productId}
        ORDER BY m_prod_prop.sort, m_prod_prop_value.sort
    </select>

    <select id="selectSkusByProductId" resultMap="ProductSkuResult">
        SELECT m_merc_prod.mercId,m_merc_prod.prodId,
               m_prod_prop_relation.propIds,m_prod_prop_relation.pprId,
               m_prod_prop_relation.pprName
        FROM m_merc_prod
        INNER JOIN m_prod_prop_relation ON m_merc_prod.orgProdId = m_prod_prop_relation.prodId
        WHERE m_prod_prop_relation.delFlag = '0'
          AND m_merc_prod.prodId = #{productId}
          AND m_merc_prod.mercId = #{mercId}
    </select>

    <select id="selectBrandListByMercId" resultMap="BrandResult">
        SELECT m_brand.brandId,
               m_brand.brandName,
               m_brand.logoImgId,
               t_attch_file.fileUrl logoImageUrl,
               t_attch_file.fileId logoImageId,
               taf.fileUrl brandImageUrl,
               taf.fileId brandImageId,
               m_brand.brandDesc
        FROM m_merc_prod
         INNER JOIN m_prod ON m_merc_prod.orgProdId = m_prod.prodId
         INNER JOIN m_brand ON m_brand.brandId = m_prod.brandId
         LEFT JOIN t_attch_file ON m_brand.logoImgId = t_attch_file.fileId
         LEFT JOIN t_attch_file taf ON m_brand.brandImgId = taf.fileId
        WHERE m_merc_prod.supplyFlag = '1'
          AND m_merc_prod.shelfFlag = '1'
          AND m_merc_prod.delFlag = '0'
          AND m_brand.delFlag = '0'
          AND m_merc_prod.mercId = #{mercId}
          <if test="prodkindId != null and prodkindId != ''">
            AND m_merc_prod.prodkindId = #{prodkindId}
          </if>
        GROUP BY m_merc_prod.prodkindId, m_brand.brandId
        ORDER BY m_brand.sort, t_attch_file.sort,taf.sort
    </select>

    <select id="selectBrandById" parameterType="java.lang.String" resultMap="BrandResult">
        SELECT m_brand.brandId,
               m_brand.brandName,
               m_brand.logoImgId,
               t_attch_file.fileUrl logoImageUrl,
               t_attch_file.fileId logoImageId,
               taf.fileUrl brandImageUrl,
               taf.fileId brandImageId,
               m_brand.brandDesc
        FROM m_brand
         LEFT JOIN t_attch_file ON m_brand.logoImgId = t_attch_file.fileId
         LEFT JOIN t_attch_file taf ON m_brand.brandImgId = taf.fileId
        WHERE m_brand.delFlag = '0'
          AND m_brand.brandId = #{brandId}
    </select>


    <select id="selectCategoryListByMercId" parameterType="java.lang.String" resultMap="CategoryResult">
        SELECT m_prodkind.prodkindId, m_prodkind.prodkindName
        FROM m_merc_prod
        LEFT JOIN m_prodkind ON m_merc_prod.prodkindId = m_prodkind.prodkindId
        WHERE m_merc_prod.supplyFlag = '1'
          AND m_merc_prod.shelfFlag = '1'
          AND m_merc_prod.delFlag = '0'
          AND m_prodkind.delFlag = '0'
          AND m_merc_prod.mercId = #{mercId}
        GROUP BY m_merc_prod.prodkindId
        ORDER BY m_prodkind.kindSort
    </select>


    <select id="selectDesignListByMercId" resultMap="ProductDesignImageResult">
        SELECT m_prod_design.*, clearImg.fileUrl AS clearImgUrl
        FROM m_merc_prod
        INNER JOIN m_prod_design ON m_merc_prod.orgProdId = m_prod_design.prodId
        INNER JOIN t_attch_file clearImg ON m_prod_design.clearImgId = clearImg.fileId
        WHERE m_prod_design.delFlag = '0'
          AND m_merc_prod.mercId = #{mercId}
          AND m_prod_design.pprId = #{pprId}
    </select>


<!--    <select id="selectProdPriceBy" resultType="BigDecimal">-->
<!--        SELECT IFNULL(a.price, 0) + IFNULL(b.price, 0)-->
<!--        FROM (-->
<!--         SELECT SUM(m_prod_price.price) AS price-->
<!--         FROM m_prod_price-->
<!--         WHERE m_prod_price.delFlag = '0'-->
<!--           AND m_prod_price.costItemKey IN ('BLANK', 'MANCOST', 'EXPRESS')-->
<!--           AND m_prod_price.salesmanId = #{mercId}-->
<!--           AND m_prod_price.buyerRoleKey = 'CUSTOMER'-->
<!--           AND m_prod_price.prodId = #{prodId}-->
<!--           AND m_prod_price.pprId = #{pprId} ) a, (-->
<!--         SELECT m_prod_price.price-->
<!--         FROM m_prod_price-->
<!--         WHERE m_prod_price.delFlag = '0'-->
<!--           AND m_prod_price.costItemKey = 'PRINT'-->
<!--           AND m_prod_price.salesmanId = #{mercId}-->
<!--           AND m_prod_price.buyerRoleKey = 'CUSTOMER'-->
<!--           AND m_prod_price.prodId = #{prodId}-->
<!--           AND m_prod_price.pprId = #{pprId}-->
<!--           AND m_prod_price.surfaceQty = (-->
<!--             SELECT COUNT(1)-->
<!--             FROM m_prod_design-->
<!--             WHERE m_prod_design.pprId = m_prod_price.pprId-->
<!--               AND m_prod_design.prodId = m_prod_price.prodId-->
<!--         )) b-->
<!--    </select>-->


    <select id="selectProductPriceList" resultType="ProdPriceDTO">
        SELECT m_prod_price.*,  m_cost_item.* FROM m_cost_item
        INNER JOIN m_prod_price ON m_cost_item.costItemKey = m_prod_price.costItemKey
        WHERE m_prod_price.delFlag = '0'
          AND m_prod_price.buyerRoleKey = 'CUSTOMER'
          AND m_prod_price.salesmanId = #{mercId}
          AND m_prod_price.prodId = #{prodId}
          AND m_prod_price.pprId = #{pprId}
          and (case when m_cost_item.singleFlag = '0' then m_prod_price.surfaceQty = #{surfaceQty} else 1 end)
    </select>


    <select id="selectProductBySkuId" resultMap="selectProductBySkuIdProductOneResult">
        SELECT m_merc_prod.mercId,
               m_merc_prod.prodId,
               m_merc_prod.orgProdId,
               m_merc_prod.mercId,
               m_prod_prop_relation.propIds,
               m_prod_prop_relation.pprId,
               m_prod_prop_relation.pprName,
               t_attch_file.fileId,
               m_prod.prodName,
               t_attch_file.fileUrl,
               m_prod.prodType,
               m_prod.appStatus
        FROM m_merc_prod
        LEFT JOIN m_prod ON m_merc_prod.prodId = m_prod.prodId
        INNER JOIN m_prod_prop_relation ON m_merc_prod.orgProdId = m_prod_prop_relation.prodId
        INNER JOIN m_prod_picture ON m_prod_picture.prodId = m_merc_prod.prodId
        INNER JOIN t_attch_file ON t_attch_file.fileId = m_prod_picture.picImgId
        WHERE m_prod_prop_relation.delFlag = '0'
          AND m_merc_prod.prodId = #{prodId}
          AND m_merc_prod.mercId = #{mercId}
          AND m_prod_prop_relation.pprId = #{pprId}
    </select>


    <select id="selectMinProdPriceBy" resultType="BigDecimal">
        SELECT SUM(a.price)
        FROM (SELECT MIN(m_prod_price.price) AS price
         FROM m_cost_item
         INNER JOIN m_prod_price ON m_cost_item.costItemKey = m_prod_price.costItemKey
         WHERE m_prod_price.delFlag = '0'
           AND m_prod_price.buyerRoleKey = 'CUSTOMER'
           AND m_prod_price.surfaceQty = 0
           AND m_prod_price.salesmanId = #{mercId}
           AND m_prod_price.prodId = #{prodId}
         GROUP BY m_prod_price.costItemKey) a
    </select>

    <select id="selectVendorMinProdPriceBy" resultType="BigDecimal">
        SELECT SUM(a.price)
        FROM (SELECT MIN(m_prod_price.price) AS price
              FROM m_cost_item
                       INNER JOIN m_prod_price ON m_cost_item.costItemKey = m_prod_price.costItemKey
              WHERE m_prod_price.delFlag = '0'
                AND m_prod_price.buyerRoleKey = 'CUSTOMER'
--                 AND m_prod_price.surfaceQty = 0
                AND m_prod_price.salesmanRoleKey = 'VENDOR'
                AND m_prod_price.prodId = #{prodId}
              GROUP BY m_prod_price.costItemKey) a
    </select>


    <select id="selectYdtProductListByMercId" parameterType="Long" resultType="ProductDTO">
        <![CDATA[
            SELECT m_prod.prodId, m_merc_prod.orgProdId,
                m_prod.prodName, m_prod.prodType,
                m_prod.subTitle,
                m_merc_prod.mercId,
                mppr.pprName,
                mppr.pprId
            FROM m_merc_prod
            LEFT JOIN m_prod ON m_merc_prod.prodId = m_prod.prodId
            LEFT JOIN m_prod_prop_relation mppr ON mppr.prodId = m_prod.prodId
            WHERE m_merc_prod.supplyFlag = '1'
            AND m_merc_prod.shelfFlag = '1'
            AND m_merc_prod.delFlag = '0'
            AND m_prod.prodType <> 'CUSTOMIZABLE'
            AND m_merc_prod.mercId = #{mercId}
        ]]>
    </select>


</mapper>
