<?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.tmerclub.cloud.product.mapper.SkuMapper">
  <resultMap id="skuMap" type="com.tmerclub.cloud.product.model.Sku">
    <id column="sku_id" property="skuId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="spu_id" property="spuId"/>
    <result column="img_url" property="imgUrl"/>
    <result column="price_fee" property="priceFee"/>
    <result column="market_price_fee" property="marketPriceFee"/>
    <result column="status" property="status"/>
    <result column="party_code" property="partyCode"/>
    <result column="model_id" property="modelId"/>
    <result column="weight" property="weight"/>
    <result column="volume" property="volume"/>
    <result column="score_fee" property="scoreFee"/>
    <result column="stock_warning" property="stockWarning"/>
  </resultMap>
  <resultMap id="skuVOMap" type="com.tmerclub.cloud.common.product.vo.SkuVO">
    <result column="sku_id" property="skuId"/>
    <result column="spu_id" property="spuId"/>
    <result column="img_url" property="imgUrl"/>
    <result column="price_fee" property="priceFee"/>
    <result column="market_price_fee" property="marketPriceFee"/>
    <result column="status" property="status"/>
    <result column="score_fee" property="scoreFee"/>
    <result column="party_code" property="partyCode"/>
    <result column="model_id" property="modelId"/>
    <result column="weight" property="weight"/>
    <result column="volume" property="volume"/>
    <result column="supplier_sku_id" property="supplierSkuId"/>
    <result column="stock" property="stock"/>
    <result column="stock_warning" property="stockWarning"/>
    <result column="spu_name" property="spuName"/>
    <result column="main_img_url" property="mainImgUrl"/>
    <collection property="skuLangList" ofType="com.tmerclub.cloud.common.product.vo.SkuLangVO">
      <id column="lang" property="lang"/>
      <result column="sku_name" property="skuName"/>
    </collection>
    <collection property="skuComboList" ofType="com.tmerclub.cloud.common.product.vo.SkuComboVO">
      <id column="sku_id" property="skuId"/>
      <result column="combo_sku_id" property="comboSkuId"/>
      <result column="combo_spu_id" property="comboSpuId"/>
      <result column="combo_count" property="comboCount"/>
    </collection>
  </resultMap>
  <resultMap id="skuAndAttrVOMap" type="com.tmerclub.cloud.common.product.vo.SkuVO" extends="com.tmerclub.cloud.product.mapper.SkuMapper.skuMap">
    <result column="stock" property="stock"/>
    <result column="main_img_url" property="mainImgUrl"/>
    <result column="img_url" property="imgUrl"/>
    <result column="supplier_sku_id" property="supplierSkuId"/>
    <result column="stock_warning" property="stockWarning"/>
    <result column="spu_mold" property="spuMold"/>
    <collection property="skuLangList" ofType="com.tmerclub.cloud.common.product.vo.SkuLangVO">
      <id column="lang" property="lang"/>
      <result column="sku_name" property="skuName"/>
      <result column="attrs" property="attrs"/>
    </collection>
<!--    <collection property="skuComboList" ofType="com.tmerclub.cloud.common.product.vo.SkuComboVO" columnPrefix="sc_">-->
<!--      <id column="sku_id" property="skuId"/>-->
<!--      <result column="combo_sku_id" property="comboSkuId"/>-->
<!--      <result column="combo_spu_id" property="comboSkuId"/>-->
<!--      <result column="combo_price" property="comboPrice"/>-->
<!--    </collection>-->
    <collection property="spuSkuAttrValues" ofType="com.tmerclub.cloud.common.product.vo.SpuSkuAttrValueVO">
      <id column="spu_sku_attr_id" property="spuSkuAttrId"/>
      <result column="attr_id" property="attrId"/>
      <result column="attr_value_id" property="attrValueId"/>
      <collection property="spuSkuAttrValueLangList" ofType="com.tmerclub.cloud.common.product.vo.SpuSkuAttrValueLangVO">
        <id column="attr_lang" property="lang"/>
        <result column="attr_name" property="attrName"/>
        <result column="attr_value_name" property="attrValueName"/>
      </collection>
    </collection>
  </resultMap>
  <resultMap id="spuAndSkuOrderDetailMap" type="com.tmerclub.cloud.common.product.vo.SkuAddrVO">
    <result column="sku_id" property="skuId" />
    <result column="spu_id" property="spuId"/>
    <result column="shop_id" property="shopId"/>
    <result column="spu_mold" property="spuMold"/>
    <result column="delivery_template_id" property="deliveryTemplateId"/>
    <result column="weight" property="weight"/>
    <result column="volume" property="volume"/>
  </resultMap>
  <sql id="Vo_Column_List">
    sku.`sku_id`,sku.`create_time`,sku.`update_time`,sku.`spu_id`,sku.`img_url`,sku.`price_fee`,sku.`market_price_fee`,sku.`status`
    ,sku.weight,sku.volume,sku.score_fee,sku.party_code,sku.model_id,sku.stock_warning,sku.supplier_sku_id
  </sql>

  <select id="listSkuWithAttrBySpuId" resultMap="skuAndAttrVOMap">
    SELECT <include refid="Vo_Column_List"/>,sl.lang,sl.sku_name,sl.attrs,spu.main_img_url,party_code,
    sav.spu_sku_attr_id,sav.`attr_id`,savl.`attr_name`,sav.`attr_value_id`,savl.`attr_value_name`,savl.lang attr_lang
<!--    sc.sku_id as sc_sku_id,sc.combo_sku_id as sc_combo_sku_id,sc.combo_spu_id as sc_combo_spu_id,sc.combo_price as sc_combo_price-->
    FROM sku as sku
    LEFT JOIN spu as spu  ON sku.spu_id = spu.spu_id
<!--    LEFT JOIN `sku_combo` sc ON sc.`sku_id` = sc.`sku_id`-->
    LEFT JOIN `sku_lang` sl ON sl.`sku_id` = sku.`sku_id`
    LEFT JOIN `spu_sku_attr_value` sav ON sav.`sku_id` = sku.`sku_id`
    LEFT JOIN `spu_sku_attr_value_lang` savl ON savl.`spu_sku_attr_id` = sav.`spu_sku_attr_id`
    WHERE sku.`spu_id` = #{spuId} and sku.status <![CDATA[<>]]> -1
  </select>

  <insert id="saveBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="skuId">
    insert into sku (`spu_id`,`img_url`,`price_fee`,`market_price_fee`,`score_fee`,`party_code`,`model_id`,`status`,`weight`,`volume`,`supplier_sku_id`,`stock_warning`) values
    <foreach collection="skuList" item="sku" separator=",">
      (#{sku.spuId},#{sku.imgUrl},#{sku.priceFee},#{sku.marketPriceFee},#{sku.scoreFee},#{sku.partyCode},#{sku.modelId},#{sku.status},#{sku.weight},#{sku.volume},#{sku.supplierSkuId},#{sku.stockWarning})
    </foreach>
  </insert>

  <update id="update">
    update sku
    <set>
      <if test="sku.spuId != null">
        `spu_id` = #{sku.spuId},
      </if>
      <if test="sku.imgUrl != null">
        `img_url` = #{sku.imgUrl},
      </if>
      <if test="sku.priceFee != null">
        `price_fee` = #{sku.priceFee},
      </if>
      <if test="sku.marketPriceFee != null">
        `market_price_fee` = #{sku.marketPriceFee},
      </if>
      <if test="sku.scoreFee != null">
        `score_fee` = #{sku.scoreFee},
      </if>
      <if test="sku.status != null">
        `status` = #{sku.status},
      </if>
      <if test="sku.supplierSkuId != null">
        `supplier_sku_id` = #{sku.supplierSkuId},
      </if>
    </set>
    where sku_id = #{sku.skuId}
  </update>
  <delete id="deleteById">
    delete from sku where sku_id = #{skuId}
  </delete>

  <update id="updateBySpuId">
    update sku set status = -1 where spu_id = #{spuId}
  </update>

  <update id="updateBatch">
    <foreach collection="skus" separator=";" item="sku">
      update sku
      <set>
        update_time = now(),
        <if test="sku.imgUrl != null">
          `img_url` = #{sku.imgUrl},
        </if>
        <if test="sku.priceFee != null">
          `price_fee` = #{sku.priceFee},
        </if>
        <if test="sku.marketPriceFee != null">
          `market_price_fee` = #{sku.marketPriceFee},
        </if>
        <if test="sku.scoreFee != null">
          `score_fee` = #{sku.scoreFee},
        </if>
        <if test="sku.partyCode != null">
          `party_code` = #{sku.partyCode},
        </if>
        <if test="sku.modelId != null">
          `model_id` = #{sku.modelId},
        </if>
        <if test="sku.status != null">
          `status` = #{sku.status},
        </if>
        <if test="sku.weight != null">
          `weight` = #{sku.weight},
        </if>
        <if test="sku.volume != null">
          `volume` = #{sku.volume},
        </if>
        <if test="sku.stockWarning != null">
          `stock_warning` = #{sku.stockWarning},
        </if>
        <if test="sku.supplierSkuId != null">
          `supplier_sku_id` = #{sku.supplierSkuId},
        </if>
      </set>
      where sku_id = #{sku.skuId}
    </foreach>
  </update>
  <select id="getSkuBySkuId" resultMap="skuVOMap">
    select sku.`sku_id`,sku.`spu_id`,sku.`img_url`,sku.`price_fee`,sku.`market_price_fee`,sku.`status`,
           sku.weight,sku.volume,sku.score_fee,sku.party_code,sku.model_id,sl.lang,sl.sku_name,sl.attrs,sku.supplier_sku_id
    from sku sku
    left join sku_lang sl on sku.sku_id = sl.sku_id
    where sku.sku_id = #{skuId}
  </select>

  <select id="excelSkuList" resultMap="skuAndAttrVOMap">
    SELECT
      sku.spu_id,
      sku.sku_id,
      sl.lang,
      sl.sku_name,
      sku.price_fee,
      sku.market_price_fee,
      sku.party_code,
      sku.model_id,
      sku.weight,
      sku.volume,
      sku.stock_warning,
      sku.img_url,
      ssav.spu_sku_attr_id,
      ssavl.attr_name,
      ssavl.attr_value_name,
      ssavl.lang attr_lang,
      sku.supplier_sku_id,
      spu.spu_mold
    FROM sku sku
    LEFT JOIN sku_lang sl ON sku.sku_id = sl.sku_id
    LEFT JOIN spu_sku_attr_value ssav ON sku.sku_id = ssav.sku_id AND ssav.status != -1
    LEFT JOIN spu_sku_attr_value_lang ssavl ON ssavl.spu_sku_attr_id = ssav.spu_sku_attr_id
    LEFT JOIN spu spu ON sku.spu_id = spu.spu_id
    WHERE sku.status != -1 AND sku.spu_id IN
    <foreach collection="spuIds" item="spuId" open="(" close=")" separator=",">
      #{spuId}
    </foreach>
    GROUP BY ssavl.spu_sku_attr_id, sku.`sku_id`
  </select>


  <select id="listSkuBySpuIds" resultMap="skuAndAttrVOMap">
    SELECT sku.spu_id,sku.sku_id,sl.lang,sl.sku_name,sku.price_fee,sku.market_price_fee,sku.party_code,sku.model_id,sku.weight,sku.volume,
    ssav.spu_sku_attr_id,ssavl.attr_name,ssavl.attr_value_name,ssavl.lang attr_lang,sku.supplier_sku_id
    FROM sku sku
    LEFT JOIN sku_lang sl ON sku.sku_id = sl.sku_id
    LEFT JOIN spu_sku_attr_value ssav ON sku.sku_id = ssav.sku_id AND ssav.status != -1
    LEFT JOIN spu_sku_attr_value_lang ssavl ON ssavl.spu_sku_attr_id = ssav.spu_sku_attr_id
    WHERE sku.status != -1 AND sku.status != 0 AND sku.spu_id IN
    <foreach collection="spuIds" item="spuId" open="(" close=")" separator=",">
      #{spuId}
    </foreach>
    GROUP BY sku.`sku_id`
  </select>

  <select id="listSkuPriceByIds" resultMap="skuVOMap">
    select sku_id,price_fee from sku where sku_id in
    <foreach collection="skuIds" item="skuId" open="(" close=")" separator=",">
      #{skuId}
    </foreach>
  </select>
  <select id="listSpuDetailByIds" resultMap="spuAndSkuOrderDetailMap">
    select s.spu_id, s.spu_mold,s.shop_id, s.delivery_template_id, sk.volume, sk.weight, sk.sku_id
    from sku sk
    join spu s on s.spu_id = sk.spu_id
    where sk.sku_id in
    <foreach collection="skuIds" item="skuId" open="(" close=")" separator=",">
      #{skuId}
    </foreach>
  </select>

  <select id="getMinPriceFeeByIds" resultType="java.lang.Long">
    SELECT price_fee FROM sku WHERE sku_id IN
    <foreach collection="skuIds" item="skuId" open="(" separator="," close=")">
      #{skuId}
    </foreach>
    ORDER BY price_fee ASC LIMIT 1
  </select>

  <select id="getPriceFeeBySpuIdAndSupplierSkuId" resultType="java.lang.Long">
    SELECT price_fee FROM sku WHERE spu_id = #{spuId} AND supplier_sku_id = #{supplierSkuId}
  </select>

  <select id="getSkuIdBySupplierSkuId" resultType="java.lang.Long">
    SELECT sku_id FROM sku WHERE supplier_sku_id = #{supplierSkuId} AND spu_id = #{spuId}
  </select>


  <select id="listExportSku" resultType="com.tmerclub.cloud.common.product.vo.SkuVO">
    SELECT sp.`spu_id`, sl.`spu_name`,  skl.`sku_name`, sk.`sku_id`, sk.`party_code`,   sk.`price_fee`
    FROM spu sp
    JOIN sku sk ON sp.`spu_id` = sk.`spu_id` AND sp.`status` != -1
    JOIN spu_lang sl ON sl.`spu_id` = sp.`spu_id`
    left JOIN sku_lang skl ON skl.`sku_id` = sk.`sku_id` and skl.`lang` = 1
    <where>
      sl.`lang` =1
      <if test="product.spuId != null and product.spuId != ''">
        and sp.spu_id = #{product.spuId}
      </if>
      <if test="product.shopId != null">
        and sp.shop_id = #{product.shopId}
      </if>
      <if test="product.shopSecondaryCategoryId != null">
        and sp.shop_category_id = #{product.shopSecondaryCategoryId}
      </if>
      <if test="product.categoryId != null">
        and sp.category_id = #{product.categoryId}
      </if>
      <if test="product.spuName != null and product.spuName != ''">
        and sl.spu_name like CONCAT('%', #{product.spuName},'%')
      </if>
      <if test="product.partyCode != null and product.partyCode != ''">
        AND sk.party_code = #{product.partyCode}
      </if>
      <if test="product.skuIds != null and product.skuIds.size>0">
        and sk.sku_id in
        <foreach collection="product.skuIds" item="skuId" open="(" separator="," close=")">
          #{skuId}
        </foreach>
      </if>
    </where>
    GROUP BY sp.spu_id,sk.`sku_id`
    order by sk.sku_id asc
  </select>
  <select id="listSkuBySkuIds" resultMap="skuVOMap">
    select <include refid="Vo_Column_List" />,sl.lang,sl.sku_name,sl.attrs,sku.`supplier_sku_id`,pl.spu_name
    from sku sku
    left join sku_lang sl on sku.sku_id = sl.sku_id
    left join spu_lang pl on sku.spu_id = pl.spu_id and pl.lang = 1
    where sku.sku_id in
    <foreach collection="skuIdList" item="skuId" open="(" separator="," close=")">
      #{skuId}
    </foreach>
  </select>

  <select id="listByPartyCodeList" resultMap="skuVOMap">
    select sku.`sku_id`,sku.`spu_id`,sku.`img_url`,sku.`price_fee`,sku.`market_price_fee`,sku.`status`
    ,sku.weight,sku.volume,sku.score_fee,sku.party_code,sku.model_id,sl.lang,sl.sku_name,sl.attrs
    from sku sku
    left join spu spu on sku.spu_id = spu.spu_id
    left join sku_lang sl on sku.sku_id = sl.sku_id
    where spu.shop_id = -1 and
          sku.party_code in
    <foreach collection="partyCodeList" item="partyCode" open="(" separator="," close=")">
      #{partyCode}
    </foreach>
  </select>
    <select id="getSkuByPartyCodeAndShopId" resultType="com.tmerclub.cloud.api.product.dto.StockBillLogItemDTO">
      SELECT sk.sku_id, sk.party_code, sp.spu_id, sp.shop_id, spl.spu_name, sp.main_img_url, skl.sku_name
      FROM sku sk
             JOIN `spu` sp ON sk.spu_id = sp.spu_id
             JOIN `spu_lang` spl ON spl.spu_id = sp.spu_id AND spl.lang = 1
             LEFT JOIN `sku_lang` skl ON skl.sku_id = sk.sku_id AND skl.lang = 1
      WHERE sk.party_code = #{partyCode} AND sp.status != -1
      <if test="sysType == 1">
        AND sp.shop_id = #{shopId}
      </if>
      <if test="sysType == 3">
        AND sp.supplier_id = #{shopId}
      </if>
    </select>
    <select id="getBySpuId" resultType="com.tmerclub.cloud.common.product.vo.SkuVO">
      SELECT sp.`spu_id`, sl.`spu_name`, sp.`main_img_url`,  skl.`sku_name`, sk.`sku_id`, sk.`party_code`, sk.`img_url`,  sk.`price_fee`, sk.market_price_fee, sk.status
      FROM  sku sk
      JOIN spu  sp ON sp.`spu_id` = sk.`spu_id`
       JOIN spu_lang sl ON sl.`spu_id` = sp.`spu_id`
     left JOIN sku_lang skl ON skl.`sku_id` = sk.`sku_id`
      where
      sl.`lang` =1  and sk.status <![CDATA[<>]]> -1  and sp.spu_id = #{spuId}

    </select>
  <select id="listSkuPartyCodesByShopId" resultType="string">
    SELECT sk.`party_code`
    FROM spu sp
    JOIN sku sk ON sp.`spu_id` = sk.`spu_id` AND sp.`status` != -1
    <where>
      sk.status <![CDATA[<>]]> -1
      <if test="spuId != null">
          and sp.spu_id <![CDATA[<>]]> #{spuId}
      </if>
      <if test="sysType == 3">
          and sp.supplier_id = #{shopId} and shop_id = -1
      </if>
      <if test="sysType == 1">
          and sp.shop_id = #{shopId}
      </if>
    </where>
  </select>

  <select id="getSkuIdBySpuIdAndSupplierSkuId" resultType="java.lang.Long">
    SELECT sku_id FROM sku WHERE spu_id = #{spuId} AND supplier_sku_id = #{supplierSkuId}
  </select>

  <select id="listShopSkuPriceBySupplierSkuIds" resultMap="skuVOMap">
    SELECT s.sku_id,s.spu_id,s.supplier_sku_id,s.party_code,s.price_fee,s.img_url,sl.lang,sl.sku_name
    FROM sku s
    JOIN `spu` p ON p.`spu_id` = s.`spu_id` AND p.shop_id = #{shopId}
    LEFT JOIN `sku_lang` sl ON sl.`sku_id` = s.`sku_id`
    WHERE  p.supplier_id <![CDATA[<>]]>  0
    AND p.`status`  <![CDATA[<>]]>  -1
    AND s.supplier_sku_id IN
    <foreach collection="skuIds" item="skuId" open="(" separator="," close=")">
      #{skuId}
    </foreach>
  </select>

  <select id="getStockWarningNum" resultType="int">
    SELECT COUNT(1)
    FROM spu sp
    JOIN sku  sk ON sp.`spu_id` = sk.`spu_id` AND sk.`status` != -1
    LEFT JOIN sku su ON sk.supplier_sku_id = su.sku_id AND sp.supplier_delivery_type = 1
    WHERE sp.status != -1 AND sp.shop_id = #{product.shopId}
<!--    供应商商品且发货类型为供应商发货的商品sku，会再关联一次供应商的sku-->
<!--    1.普通商品库存预警判断：供应商id为空且库存预警状态为1-->
<!--    2.供应商商品库存预警判断：供应商库存预警状态为1-->
    AND ((su.sku_id IS NULL AND sk.stock_warning_status = 1)
    OR su.stock_warning_status = 1)
  </select>

  <select id="getSkuIdsBySupplierSkuId" resultType="java.lang.Long">
    SELECT sku_id FROM sku WHERE supplier_sku_id = #{supplierSkuId}
  </select>
  <select id="pageSku" resultType="com.tmerclub.cloud.common.product.vo.SkuVO">
    SELECT sp.`spu_id`, sl.`spu_name`, sp.`main_img_url`,  skl.`sku_name`, sk.`sku_id`, sk.`party_code`, sk.`img_url`,  sk.`price_fee`,  sk.status, sp.spu_mold
    FROM spu sp
    JOIN sku  sk ON sp.`spu_id` = sk.`spu_id` AND sk.`status` != -1
    JOIN spu_lang sl ON sl.`spu_id` = sp.`spu_id`
    left JOIN sku_lang skl ON skl.`sku_id` = sk.`sku_id` AND skl.`lang` =1
    <where>
      sl.`lang` =1  and sp.status != -1 and sp.spu_mold != 2
      <if test="product.spuId != null and product.spuId != ''">
        and sp.spu_id = #{product.spuId}
      </if>
      <if test="product.stockWarning != null">
        AND (((IFNULL(supplier_delivery_type,0) != 1 and sk.stock_warning_status = #{product.stockWarning}))
        OR (sp.supplier_delivery_type = 1 and (select stock_warning_status from sku where sku_id =  sk.`supplier_sku_id`) = #{product.stockWarning}))
      </if>
      <if test="product.shopId != null">
        and sp.shop_id = #{product.shopId}
      </if>
      <if test="product.shopSecondaryCategoryId != null">
        and sp.shop_category_id = #{product.shopSecondaryCategoryId}
      </if>
      <if test="product.categoryId != null">
        and sp.category_id = #{product.categoryId}
      </if>
      <if test="product.spuName != null and product.spuName != ''">
        and sl.spu_name like CONCAT('%', #{product.spuName},'%')
      </if>
      <if test="product.partyCode != null and product.partyCode != ''">
      and sk.party_code like CONCAT('%', #{product.partyCode},'%')
    </if>
      <if test="product.skuIds != null and product.skuIds.size>0">
        and sk.sku_id in
        <foreach collection="product.skuIds" item="skuId" open="(" separator="," close=")">
          #{skuId}
        </foreach>
      </if>
      <if test="product.brandIds != null and product.brandIds != '' ">
        and sp.brand_id in
        <foreach collection="product.brandIds.split(',')" index="index" item="brandId" open="(" separator="OR" close=")">
          #{brandId}
        </foreach>
      </if>
      <if test="product.skuCode != null and product.skuCode != ''">
        and sk.party_code like CONCAT('%', #{product.skuCode},'%')
      </if>
    </where>
    ORDER BY sk.sku_id asc
  </select>

  <select id="listSkuByPartyCodeAndShopId" resultType="com.tmerclub.cloud.common.product.vo.SkuVO">
    SELECT sk.*
    FROM spu sp
     JOIN sku sk ON sk.`spu_id` = sp.`spu_id`
    WHERE
      sk.`party_code` LIKE CONCAT('%',#{partyCode},'%')
      <if test="shopId != null">
        AND sp.`shop_id` = #{shopId}
      </if>
      <if test="supplierId != null">
        AND sp.`shop_id` = -1
        AND sp.`supplier_id` = #{supplierId}
      </if>
    ORDER BY sk.`sku_id` ASC
  </select>

  <select id="listSkuIdBySpuId" resultType="java.lang.Long">
    SELECT sku_id FROM `sku` WHERE spu_id = #{spuId} and `status` > -1
  </select>

  <select id="listSimpleSkuBySpuIds" resultType="com.tmerclub.cloud.common.product.vo.SkuVO">
    SELECT spu_id,sku_id,stock_warning,`status` FROM `sku` WHERE `status` > -1 and spu_id in
    <foreach collection="spuIds" item="spuId" open="(" separator="," close=")">
      #{spuId}
    </foreach>
  </select>

  <select id="listSupplierSkuIdBySpuId" resultType="com.tmerclub.cloud.common.product.vo.SkuVO">
    SELECT sku.sku_id,sku.supplier_sku_id
    FROM `sku`
    JOIN spu ON spu.spu_id = sku.spu_id
    WHERE spu.shop_id = #{shopId} AND spu.`status` <![CDATA[<>]]> -1 AND sku.status <![CDATA[<>]]> -1 AND spu.supplier_spu_id IN
    <foreach collection="spuIds" item="spuId" open="(" separator="," close=")">
      #{spuId}
    </foreach>
  </select>


  <select id="listSkuStock" resultType="com.tmerclub.cloud.product.model.SkuStock">
    SELECT * FROM sku_stock WHERE sku_id IN
    <foreach collection="skuIds" item="skuId" open="(" close=")" separator=",">
      #{skuId}
    </foreach>
  </select>

  <select id="listSkuIdByPartyCodesAndShopId" resultType="com.tmerclub.cloud.common.product.vo.SkuVO">
    SELECT s.sku_id, s.party_code
    FROM sku s
    JOIN spu sp ON sp.spu_id = s.spu_id
    WHERE
    s.`status` = 1
    AND sp.shop_id = #{shopId}
    AND sp.`status` != -1
    AND s.party_code IN
    <foreach collection="partyCodes" item="partyCode" open="(" separator="," close=")">
      #{partyCode}
    </foreach>
    GROUP BY
    s.sku_id
  </select>

  <select id="listBySpuIds" resultMap="skuVOMap">
    SELECT <include refid="Vo_Column_List"/>,sl.lang,sl.sku_name,sl.attrs,spu.main_img_url
    FROM sku as sku
    LEFT JOIN spu as spu  ON sku.spu_id = spu.spu_id
    LEFT JOIN `sku_lang` sl ON sl.`sku_id` = sku.`sku_id`
    WHERE sku.status <![CDATA[<>]]> -1 and sku.`spu_id` IN
    <foreach collection="spuIds" item="spuId" open="(" separator="," close=")">
      #{spuId}
    </foreach>
  </select>

  <update id="batchUpdateSkuWarning">
    <foreach collection="skuList" item="sku" separator=";">
      UPDATE `sku` SET stock_warning_status  = IF(stock_warning &lt;= #{sku.stock}, 0, 1) WHERE sku_id = #{sku.skuId}
    </foreach>
  </update>

  <select id="listAll" resultMap="skuVOMap">
    select sku_id, spu_id, `status` from `sku`
    </select>

  <select id="listSku" resultMap="skuVOMap">
    SELECT
      <include refid="Vo_Column_List" />,
      sl.lang,
      sl.sku_name,
      sl.attrs,
      sku.`supplier_sku_id`
    FROM
      sku sku
      LEFT JOIN sku_lang sl ON sku.sku_id = sl.sku_id
      <if test="skuDTO.status == null or skuDTO.shopId != null">
        LEFT JOIN spu spu ON spu.spu_id = sku.spu_id
      </if>
    <where>
      <if test="skuDTO.status == null">
        spu.`status` != -1
      </if>
      <if test="skuDTO.shopId != null">
        AND spu.shop_id = #{skuDTO.shopId}
      </if>
      <if test="skuDTO.skuIds != null and skuDTO.skuIds.size() > 0">
        AND sku.sku_id IN
          <foreach collection="skuDTO.skuIds" item="skuId" open="(" separator="," close=")">
            #{skuId}
          </foreach>
      </if>
      <if test="skuDTO.partyCodes != null and skuDTO.partyCodes.size() > 0">
        AND sku.party_code IN
          <foreach collection="skuDTO.partyCodes" item="partyCode" open="(" separator="," close=")">
            #{partyCode}
          </foreach>
      </if>
      <if test="skuDTO.supplierSkuIds != null and skuDTO.supplierSkuIds.size() > 0">
        AND sku.supplier_sku_id IN
          <foreach collection="skuDTO.supplierSkuIds" item="supplierSkuId" open="(" separator="," close=")">
            #{supplierSkuId}
          </foreach>
      </if>
    </where>
  </select>
    <select id="listSimpleSkuAndComboBySpuId" resultMap="skuVOMap">
      SELECT s.sku_id,s.supplier_sku_id,sc.combo_spu_id,sc.combo_sku_id,sc.combo_count
      FROM sku s
      LEFT JOIN sku_combo sc ON s.sku_id = sc.sku_id
      WHERE s.spu_id = #{spuId}
    </select>

    <select id="countNormalSkuBySkuIds" resultType="int">
      SELECT ifnull(count(*),0) FROM sku
      WHERE status  <![CDATA[ <> ]]> 1
      and sku_id IN
      <foreach collection="skuIds" item="skuId" open="(" separator="," close=")">
        #{skuId}
      </foreach>
    </select>
</mapper>
