<?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.SkuComboMapper">
  <resultMap id="skuComboMap" type="com.tmerclub.cloud.product.model.SkuCombo">
    <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"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
  </resultMap>

  <resultMap id="shopCartItemVOMap" type="com.tmerclub.cloud.common.order.vo.ShopCartItemVO">
    <id column="cart_item_id" property="cartItemId"/>
    <result column="create_time" property="createTime"/>
    <result column="shop_id" property="shopId"/>
    <result column="spu_id" property="spuId"/>
    <result column="sku_id" property="skuId"/>
    <result column="category_id" property="categoryId"/>
    <result column="rate" property="rate"/>
    <result column="user_id" property="userId"/>
    <result column="main_sku_id" property="mainComboSkuId"/>
    <result column="main_spu_id" property="mainComboSpuId"/>
    <result column="spu_count" property="count"/>
    <result column="discount_id" property="discountId"/>
<!--    <result column="price_fee" property="priceFee"/>-->
    <result column="distribution_user_id" property="distributionUserId"/>
    <result column="is_checked" property="isChecked"/>
    <result column="delivery_mode" property="deliveryMode"/>
    <result column="party_code" property="partyCode"/>
    <result column="spu_code" property="spuCode"/>
    <result column="delivery_template_id" property="deliveryTemplateId"/>
    <result column="delivery_amount" property="deliveryAmount"/>
    <result column="supplier_delivery_template_id" property="deliveryTemplateId"/>
    <result column="sku_price_fee" property="skuPriceFee"/>
    <result column="score_price" property="scorePrice"/>
    <result column="is_collection" property="isCollection"/>
    <result column="img_url" property="imgUrl"/>
    <result column="supplier_id" property="supplierId"/>
    <result column="supplier_spu_type" property="supplierSpuType"/>
    <result column="sku_choice_status" property="skuChoiceStatus"/>
    <result column="supplier_delivery_type" property="supplierDeliveryType"/>
    <result column="measure_unit" property="measureUnit"/>
    <result column="supplier_price_fee" property="supplierPriceFee"/>
    <result column="weight" property="weight"/>
    <result column="volume" property="volume"/>
    <result column="combo_id" property="comboId"/>
    <result column="combo_count" property="comboCount"/>
    <result column="parent_cart_item_id" property="parentCartItemId"/>
    <result column="virtual_remark" property="virtualRemark"/>
    <result column="is_refund" property="isRefund"/>
    <result column="spu_mold" property="mold"/>
    <result column="supplier_spu_id" property="supplierSpuId"/>
    <result column="pre_sale_type" property="preSaleType"/>
    <result column="pre_sale_delivery_type" property="preSaleDeliveryType"/>
    <result column="pre_sale_delivery_time" property="preSaleDeliveryTime"/>
    <result column="write_off_num" property="writeOffNum"/>
    <result column="write_off_multiple_count" property="writeOffMultipleCount"/>
    <result column="write_off_time" property="writeOffTime"/>
    <result column="write_off_start" property="writeOffStart"/>
    <result column="write_off_end" property="writeOffEnd"/>
    <collection property="spuLangList" ofType="com.tmerclub.cloud.common.order.vo.OrderSpuLangVO">
      <id column="spu_lang" property="lang"/>
      <result column="spu_name" property="spuName"/>
    </collection>
    <collection property="skuLangList" ofType="com.tmerclub.cloud.common.order.vo.OrderSkuLangVO">
      <id column="sku_lang" property="lang"/>
      <result column="sku_name" property="skuName"/>
    </collection>
  </resultMap>

  <sql id="Vo_Column_List">
    `sku_id`,`combo_sku_id`,`combo_spu_id`,`combo_count`,`create_time`,`update_time`
  </sql>
  <select id="list" resultMap="skuComboMap">
    select <include refid="Vo_Column_List"/> from sku_combo order by sku_id desc
  </select>

  <select id="getCombSpusBySkuId" resultType="com.tmerclub.cloud.common.product.vo.SkuComboVO">
    select  sc.`sku_id`,sc.`combo_sku_id`,sc.`combo_spu_id`,sc.`combo_count`,sc.`create_time`,sc.`update_time`,
    sl.`spu_name`,skl.`sku_name`,sku.`price_fee`
    from sku_combo sc
    left join spu s on sc.combo_spu_id = s.spu_id
    LEFT JOIN spu_lang sl ON s.`spu_id` = sl.`spu_id` aND sl.lang = #{lang}
    left join sku sku on sc.combo_sku_id = sku.sku_id
    LEFT JOIN `sku_lang` skl ON skl.sku_id = sku.sku_id AND skl.lang = #{lang}
    where sc.sku_id in
    <foreach collection="skuIds" item="skuId" open="(" separator="," close=")">
        #{skuId}
    </foreach>
  </select>

<!--  <select id="getBySkuId" resultMap="skuComboMap">-->
<!--    select <include refid="Vo_Column_List"/> from sku_combo where sku_id = #{skuId}-->
<!--  </select>f-->
  <select id="getCombSpusBySkuIds" resultType="com.tmerclub.cloud.common.product.vo.SkuComboVO">
    select CASE WHEN sku.img_url IS NULL OR sku.img_url = '' THEN s.main_img_url ELSE sku.img_url END AS img_url,sc.`sku_id`,sc.`combo_sku_id`,
    sc.`combo_spu_id`,sc.`combo_count`,sc.`create_time`,sc.`update_time`,sl.`spu_name`,skl.`sku_name`,sku.`price_fee`
    from sku_combo sc
    left join spu s on sc.combo_spu_id = s.spu_id
    LEFT JOIN spu_lang sl ON s.`spu_id` = sl.`spu_id` aND sl.lang = #{lang}
    left join sku sku on sc.combo_sku_id = sku.sku_id
    LEFT JOIN `sku_lang` skl ON skl.sku_id = sku.sku_id AND skl.lang = #{lang}
    where sc.sku_id in
    <foreach collection="skuIds" item="skuId" open="(" separator="," close=")">
      #{skuId}
    </foreach>
  </select>

  <select id="getCombSpusBySpuIds" resultType="com.tmerclub.cloud.common.product.vo.SkuComboVO">
    SELECT spu.spu_id,sc.`sku_id`,sc.`combo_sku_id`,sc.`combo_spu_id`,sc.`combo_count`
    FROM sku_combo sc
    LEFT JOIN sku sku ON sc.sku_id = sku.sku_id
    LEFT JOIN spu spu ON sku.spu_id = spu.spu_id
    WHERE spu.spu_id IN
    <foreach collection="spuIds" item="spuId" open="(" separator="," close=")">
      #{spuId}
    </foreach>
  </select>

  <select id="getCombSpusByComboSpuIdsOrSkuIds" resultType="long">
    SELECT DISTINCT sku.spu_id
    FROM sku_combo sc
    LEFT JOIN sku sku ON sc.sku_id = sku.sku_id
    LEFT JOIN spu spu ON sku.spu_id = spu.spu_id
    WHERE spu.status = 1
    <if test="skuIds != null and skuIds.size() != 0">
      and sc.combo_sku_id IN
      <foreach collection="skuIds" item="skuId" open="(" separator="," close=")">
          #{skuId}
      </foreach>
    </if>
    <if test="spuIds != null and spuIds.size() != 0">
      and sc.combo_spu_id IN
      <foreach collection="spuIds" item="spuId" open="(" separator="," close=")">
          #{spuId}
      </foreach>
    </if>
  </select>

  <select id="countDisableComboSpuBySpuId" resultType="int">
      SELECT IFNULL(COUNT(DISTINCT cp.spu_id),0)
      FROM sku_combo sc
      LEFT JOIN sku sku ON sc.sku_id = sku.sku_id
      LEFT JOIN spu cp ON sc.combo_spu_id = cp.spu_id
      LEFT JOIN sku ck ON sc.combo_sku_id = ck.sku_id
      WHERE sku.`spu_id` = #{spuId} AND (cp.supplier_delivery_type = 1 OR cp.status <![CDATA[ <> ]]> 1 OR ck.`status` <![CDATA[ <> ]]> 1 )
  </select>

  <select id="getCombShopItemsAndBySkuId" resultMap="shopCartItemVOMap">
    SELECT
      sc.`sku_id` AS main_sku_id,
      sc.`combo_count` AS spu_count,
      sc.combo_spu_id  as spu_id,
      sc.combo_sku_id  as sku_id
    FROM sku_combo sc
    where sc.sku_id in
    <foreach collection="skuIds" item="skuId" open="(" separator="," close=")">
      #{skuId}
    </foreach>
  </select>

  <insert id="saveBatch">
    insert into sku_combo (`sku_id`,`combo_sku_id`,`combo_spu_id`,`combo_count`) values
    <foreach collection="skuComboList" item="skuCombo" separator=",">
        (#{skuCombo.skuId},#{skuCombo.comboSkuId},#{skuCombo.comboSpuId},#{skuCombo.comboCount})
    </foreach>
  </insert>

  <update id="update">
    update sku_combo
    <set>
      <if test="skuCombo.comboSkuId != null">
        `combo_sku_id` = #{skuCombo.comboSkuId},
      </if>
    </set>
    where sku_id = #{skuCombo.skuId}
  </update>

  <delete id="deleteBySkuIds">
    delete from sku_combo where sku_id in
    <foreach collection="skuIds" item="skuId" separator="," open="(" close=")">
        #{skuId}
    </foreach>
  </delete>

  <delete id="deleteByComboSpuIds">
    delete from sku_combo where combo_spu_id in
    <foreach collection="spuIds" item="spuId" separator="," open="(" close=")">
        #{spuId}
    </foreach>
  </delete>

  <delete id="deleteByComboSkuIds">
    delete from sku_combo where combo_sku_id in
    <foreach collection="skuIds" item="skuId" separator="," open="(" close=")">
        #{skuId}
    </foreach>
  </delete>

</mapper>
