<?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.ComboSpuMapper">
  <resultMap id="comboSpuMap" type="com.tmerclub.cloud.common.product.vo.ComboSpuVO">
    <id column="combo_spu_id" property="comboSpuId" />
    <result column="combo_id" property="comboId"/>
    <result column="spu_id" property="spuId"/>
    <result column="combo_price" property="comboPrice"/>
    <result column="type" property="type"/>
    <result column="required" property="required"/>
    <result column="least_num" property="leastNum"/>
    <result column="status" property="status"/>
    <collection property="skuList" ofType="com.tmerclub.cloud.common.product.vo.ComboSpuSkuVO">
      <id column="sku_id" property="skuId"/>
      <result column="matching_price" property="matchingPrice"/>
      <result column="css_status" property="skuStatus"/>
    </collection>
  </resultMap>
  <sql id="Vo_Column_List">
    `combo_spu_id`,`combo_id`,`spu_id`,`combo_price`,`type`,`required`,`least_num`,`status`
  </sql>
  <sql id="Join_Column_List">
    cs.`combo_spu_id`,cs.`combo_id`,cs.`spu_id`,cs.`combo_price`,cs.`type`,cs.`required`,cs.`least_num`,cs.`status`
  </sql>
  <select id="list" resultMap="comboSpuMap">
    select <include refid="Vo_Column_List"/> from combo_spu order by combo_spu_id desc
  </select>
  <select id="getByComboSpuId" resultMap="comboSpuMap">
    select <include refid="Vo_Column_List"/> from combo_spu where combo_spu_id = #{comboSpuId}
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="comboSpuId">
    insert into combo_spu (`combo_id`,`spu_id`,`combo_price`,`type`,`required`,`least_num`,`status`)
    values (#{comboSpu.comboId},#{comboSpu.spuId},#{comboSpu.comboPrice},#{comboSpu.type},#{comboSpu.required},#{comboSpu.leastNum},#{comboSpu.status});
  </insert>
  <update id="update">
    update combo_spu
    <set>
      <if test="comboSpu.comboId != null">
        `combo_id` = #{comboSpu.comboId},
      </if>
      <if test="comboSpu.spuId != null">
        `spu_id` = #{comboSpu.spuId},
      </if>
      <if test="comboSpu.comboPrice != null">
        `combo_price` = #{comboSpu.comboPrice},
      </if>
      <if test="comboSpu.type != null">
        `type` = #{comboSpu.type},
      </if>
      <if test="comboSpu.required != null">
        `required` = #{comboSpu.required},
      </if>
      <if test="comboSpu.leastNum != null">
        `least_num` = #{comboSpu.leastNum},
      </if>
      <if test="comboSpu.status != null">
        `status` = #{comboSpu.status},
      </if>
    </set>
    where combo_spu_id = #{comboSpu.comboSpuId}
  </update>

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

  <update id="updateStatus">
    update combo_spu set status = #{comboSpu.status} where
    status != -1
    <if test="comboSpu.comboId != null">
      and combo_id = #{comboSpu.comboId}
    </if>
    <if test="comboSpu.spuId">
      and spu_id = #{comboSpu.spuId}
    </if>
  </update>

  <select id="matchingSpuByComboId" resultMap="comboSpuMap">
    select <include refid="Vo_Column_List"/> from combo_spu where combo_id = #{comboId} and type = 2 and status &gt; #{status}
  </select>

  <select id="getBySpuId" resultMap="comboSpuMap">
    select <include refid="Vo_Column_List"/> from combo_spu where spu_id = #{spuId} and combo_id = #{comboId} and status &gt; -1
  </select>

  <select id="getMainSpuByComboId" resultMap="comboSpuMap">
    select <include refid="Vo_Column_List"/> from combo_spu where combo_id = #{comboId} and type = 1 and status &gt;-1
  </select>

  <select id="getMainComboSpuId" resultMap="comboSpuMap">
    select * from combo_spu where combo_id = #{comboId} and type = 1 and status != -1
  </select>

  <select id="getMatchSpuByComboId" resultMap="comboSpuMap">
    select <include refid="Vo_Column_List"/> from combo_spu where combo_id = #{comboId} and type = 2 and status &gt;-1
  </select>

  <delete id="deleteBatch">
    delete from combo_spu where combo_spu_id in
    <foreach collection="comboSpuIdList" item="comboSpuId" open="(" separator="," close=")">
      #{comboSpuId}
    </foreach>
  </delete>

  <update id="batchUpdateByComboIdList">
  update combo_spu set status = #{status} where status != -1 and combo_id in
  <foreach collection="comboIdList" item="comboId" open="(" separator="," close=")">
    #{comboId}
  </foreach>
  </update>

  <select id="comboSpuByComboIds" resultMap="comboSpuMap">
    select <include refid="Vo_Column_List"/> from combo_spu where combo_id in
    <foreach collection="comboIdList" item="comboId" open="(" separator="," close=")">
      #{comboId}
    </foreach>
    and status &gt;-1
  </select>

  <update id="batchComboSpuStatus">
    update combo_spu set status = 0 where status = 1 and spu_id in
    <foreach collection="spuIdList" item="spuId" open="(" separator="," close=")">
      #{spuId}
    </foreach>
  </update>
  <select id="getComboSpuByComboId" resultMap="comboSpuMap">
    SELECT <include refid="Join_Column_List" />,
        css.sku_id,css.matching_price,css.status as css_status
    FROM combo_spu cs
    LEFT JOIN combo_spu_sku css ON cs.combo_spu_id = css.combo_spu_id AND css.status != -1
    WHERE cs.combo_id = #{comboId} AND cs.status != -1
  </select>
</mapper>
