<?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.combo.mapper.ComboSpuSkuMapper">
  <resultMap id="comboSpuSkuMap" type="com.tmerclub.cloud.common.product.vo.ComboSpuSkuVO">
    <id column="combo_spu_id" property="comboSpuId" />
    <result column="sku_id" property="skuId"/>
    <result column="matching_price" property="matchingPrice"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `combo_spu_id`,`sku_id`,`matching_price`
  </sql>
  <select id="list" resultMap="comboSpuSkuMap">
    select <include refid="Vo_Column_List"/> from combo_spu_sku order by combo_spu_id desc
  </select>
  <select id="getByComboSpuId" resultMap="comboSpuSkuMap">
    select <include refid="Vo_Column_List"/> from combo_spu_sku where combo_spu_id = #{comboSpuId} and status = 1
  </select>

  <select id="getByComboSpuIdS" resultMap="comboSpuSkuMap">
    select <include refid="Vo_Column_List"/> from combo_spu_sku where combo_spu_id = #{comboSpuId}
  </select>

  <insert id="save">
    insert into combo_spu_sku (combo_spu_id,`sku_id`,`matching_price`) values
    <foreach collection="comboSpuSkuList" item="sku" separator=",">
      (#{comboSpuId},#{sku.skuId},#{sku.matchingPrice})
    </foreach>
  </insert>

  <update id="update">
    update combo_spu_sku
    <set>
      <if test="comboSpuSku.skuId != null">
        `sku_id` = #{comboSpuSku.skuId},
      </if>
      <if test="comboSpuSku.matchingPrice != null">
        `matching_price` = #{comboSpuSku.matchingPrice},
      </if>
    </set>
    where combo_spu_id = #{comboSpuSku.comboSpuId}
  </update>

  <delete id="delete">
    delete from combo_spu_sku where combo_spu_id =
      #{comboSpuId}
  </delete>

  <insert id="insertBatch">
    insert into combo_spu_sku (`combo_spu_id`,`sku_id`,`matching_price`) values
    <foreach collection="skuList" item="sku" separator=",">
      (#{comboSpuId},#{sku.skuId},#{sku.matchingPrice})
    </foreach>
  </insert>

  <select id="batchByComboSpuId" resultType="com.tmerclub.cloud.common.product.vo.ComboSpuSkuVO">
    select <include refid="Vo_Column_List"/> from combo_spu_sku where combo_spu_id in
    <foreach collection="comboSpuIds" item="comboSpuId" open="(" separator="," close=")">
      #{comboSpuId}
    </foreach>
  </select>
  <update id="batchUpdate">
    <foreach collection="comboSpuSkuList" item="comboSpuSku" separator=";">
      update combo_spu_sku set
      <if test="comboSpuSku.matchingPrice != null">
        matching_price = #{comboSpuSku.matchingPrice}
      </if>
      where
      sku_id = #{comboSpuSku.skuId}
      and combo_spu_id = #{comboSpuSku.comboSpuId}
    </foreach>

  </update>

  <select id="comboVoListComboAndSkuList" resultType="com.tmerclub.cloud.api.combo.vo.ComboSkuVo">
    SELECT c.combo_id,c.name,cp.least_num,cps.sku_id,cps.matching_price
    FROM combo c
    JOIN combo_spu cp ON c.combo_id = cp.combo_id
    JOIN combo_spu_sku cps ON cp.combo_spu_id = cps.combo_spu_id
    WHERE c.combo_id IN
    <foreach collection="comboIdList" item="comboId" open="(" close=")" separator=",">
      #{comboId}
    </foreach>
    AND cps.sku_id IN
    <foreach collection="skuIdList" item="skuId" open="(" close=")" separator=",">
      #{skuId}
    </foreach>
  </select>

  <select id="comboVoListByComboSpuId" resultType="com.tmerclub.cloud.common.product.vo.ComboSpuSkuVO">
    select  <include refid="Vo_Column_List"/> from combo_spu_sku where combo_spu_id = #{comboSpuId}
  </select>

<!--  <delete id="deleteBatch">-->
<!--    delete from combo_spu_sku where combo_spu_id in-->
<!--    <foreach collection="comboSpuIdList" item="comboSpuId" open="(" separator="," close=")">-->
<!--      #{comboSpuId}-->
<!--    </foreach>-->
<!--  </delete>-->
  <delete id="deleteBatch">
    update combo_spu_sku set status = -1 where combo_spu_id in
    <foreach collection="comboSpuIdList" item="comboSpuId" open="(" separator="," close=")">
      #{comboSpuId}
    </foreach>
  </delete>

  <select id="comboSpuSkuList" resultType="com.tmerclub.cloud.common.product.vo.ComboSpuSkuVO">
    select  <include refid="Vo_Column_List"/> from combo_spu_sku where combo_spu_id in
    <foreach collection="comboSpuIdList" item="comboSpuId" open="(" close=")" separator=",">
      #{comboSpuId}
    </foreach>
  </select>

  <select id="comboSpuSkuListByComboIdAndSpuId" resultType="com.tmerclub.cloud.common.product.vo.ComboSpuSkuVO">
    SELECT css.sku_id,css.matching_price FROM combo_spu cs JOIN combo_spu_sku css ON cs.combo_spu_id = css.combo_spu_id
    WHERE cs.combo_id = #{comboId} and cs.spu_id = #{spuId} AND cs.`status` > -1 and css.status = 1
  </select>

  <update id="batchUpdateByComboSpuIdList">
    update combo_spu_sku set status = #{status} where status != -1 and combo_spu_id in
    <foreach collection="comboSpuIdList" item="comboSpuId" open="(" separator="," close=")">
      #{comboSpuId}
    </foreach>
  </update>

  <delete id="deleteById">
    delete from combo_spu_sku where combo_spu_id = #{comboSpuId}
  </delete>
</mapper>
