<?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.ComboMapper">
  <resultMap id="comboMap" type="com.tmerclub.cloud.common.product.vo.ComboVO">
    <id column="combo_id" property="comboId" />
    <result column="name" property="name"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="start_time" property="startTime"/>
    <result column="end_time" property="endTime"/>
    <result column="main_spu_id" property="mainSpuId"/>
    <result column="price" property="price"/>
    <result column="sold_num" property="soldNum"/>
    <result column="shop_id" property="shopId"/>
    <result column="status" property="status"/>
    <collection property="matchingSpus" javaType="list" ofType="com.tmerclub.cloud.common.product.vo.ComboSpuVO">
      <id column="s_combo_spu_id" property="comboSpuId"/>
      <result column="scp_spu_id" property="spuId"/>
      <result column="s_type" property="type"/>
      <result column="s_least_num" property="leastNum"/>
      <result column="s_status" property="status"/>
      <result column="s_required" property="required"/>
      <result column="s_pic" property="pic"/>
      <result column="s_spu_status" property="spuStatus"/>
      <result column="s_spu_name" property="spuName"/>
      <result column="s_combo_price" property="comboPrice"/>
      <collection property="skuList" ofType="com.tmerclub.cloud.common.product.vo.ComboSpuSkuVO">
        <id column="s_sku_id" property="skuId"/>
        <result column="s_matching_price" property="matchingPrice"/>
      </collection>
    </collection>
  </resultMap>

  <resultMap id="comboWithSpuMap" type="com.tmerclub.cloud.combo.vo.ComboVO">
    <id column="combo_id" property="comboId" />
    <result column="name" property="name"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="start_time" property="startTime"/>
    <result column="end_time" property="endTime"/>
    <result column="status" property="status"/>
    <result column="sold_num" property="soldNum"/>
    <result column="price" property="price"/>
    <result column="main_spu_id" property="mainSpuId"/>
    <result column="shop_id" property="shopId"/>
    <collection property="matchingSpus" javaType="list" ofType="com.tmerclub.cloud.combo.model.ComboSpu">
      <id column="combo_spu_id" property="comboSpuId" />
      <result column="m_combo_id" property="comboId"/>
      <result column="m_spu_id" property="spuId"/>
      <result column="m_type" property="type"/>
      <result column="m_least_num" property="leastNum"/>
      <result column="m_status" property="status"/>
      <result column="m_required" property="required"/>
    </collection>
  </resultMap>

  <sql id="Vo_Column_List">
    `combo_id`,`name`,`create_time`,`update_time`,`start_time`,`end_time`,`main_spu_id`,`price`,`sold_num`,`shop_id`,`status`
  </sql>
  <select id="list" resultType="com.tmerclub.cloud.common.product.vo.ComboVO">
    select c.`combo_id`,c.`name`,c.`start_time`,c.`end_time`,c.`main_spu_id`,c.`price`,c.`sold_num`,c.`shop_id`,c.`status`,count(cs.combo_spu_id) as spu_count
    from combo c
    JOIN combo_spu cs ON c.combo_id = cs.combo_id AND cs.status > -1
    <where>
      <if test="comboDTO.shopId != null">
        and c.shop_id = #{comboDTO.shopId}
      </if>
      <if test="comboDTO.name != null and comboDTO.name != ''">
        and c.name like concat('%',#{comboDTO.name},'%')
      </if>
      <if test="comboDTO.status != null">
        and c.status = #{comboDTO.status}
      </if>
      <if test="comboDTO.mainSpuIdList != null">
        and c.main_spu_id in
        <foreach collection="comboDTO.mainSpuIdList" item="mainSpuIdList" open="(" separator="," close=")">
          #{mainSpuIdList}
        </foreach>
      </if>
    <if test="comboDTO.matchSpuIdList != null">
      AND c.main_spu_id IN (
      SELECT
      c.main_spu_id
      FROM
      combo c
      JOIN combo_spu cs ON c.combo_id = cs.combo_id
      WHERE
      cs.spu_id IN
      <foreach collection="comboDTO.matchSpuIdList" item="matchSpuIdList" open="(" separator="," close=")">
        #{matchSpuIdList}
      </foreach>
      AND cs.`status` > - 1
      )
    </if>
    AND c.`status` > -1
    </where>
    group by c.combo_id
    order by c.combo_id desc
  </select>
  <select id="getByComboId" resultMap="comboMap">
    select <include refid="Vo_Column_List"/> from combo where combo_id = #{comboId}
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="comboId">
    insert into combo (combo_id,`name`,`start_time`,`end_time`,`main_spu_id`,`price`,`sold_num`,`shop_id`,`status`)
    values (#{combo.comboId},#{combo.name},#{combo.startTime},#{combo.endTime},#{combo.mainSpuId},#{combo.price},#{combo.soldNum},#{combo.shopId},#{combo.status});
  </insert>
  <update id="update">
    update combo
    <set>
      <if test="combo.name != null">
        `name` = #{combo.name},
      </if>
      <if test="combo.startTime != null">
        `start_time` = #{combo.startTime},
      </if>
      <if test="combo.endTime != null">
        `end_time` = #{combo.endTime},
      </if>
      <if test="combo.mainSpuId != null">
        `main_spu_id` = #{combo.mainSpuId},
      </if>
      <if test="combo.price != null">
        `price` = #{combo.price},
      </if>
      <if test="combo.soldNum != null">
        `sold_num` = #{combo.soldNum},
      </if>
      <if test="combo.shopId != null">
        `shop_id` = #{combo.shopId},
      </if>
      <if test="combo.status != null">
        `status` = #{combo.status},
      </if>
    </set>
    where combo_id = #{combo.comboId}
  </update>

  <update id="updateComboStatus">
    update combo set status = #{status} where combo_id = #{comboId}
  </update>

  <update id="batchUpdateComboStatus">
    update combo set status = #{status} where combo_id in
    <foreach collection="comboIdList" item="comboId" open="(" separator="," close=")">
      #{comboId}
    </foreach>
  </update>

  <select id="listComboInfoBySpuId" resultMap="comboMap">
    SELECT  c.combo_id, c.name, c.shop_id, c.status FROM combo c
      JOIN combo_spu cs ON c.combo_id = cs.combo_id
      WHERE c.status = 1 AND c.shop_id = #{shopId} AND cs.spu_id = #{spuId}
  </select>

  <select id="comboVoListBySpuId" resultMap="comboMap">
    select c.`combo_id`,c.`name`,c.`status`,c.`price`,c.`start_time`,c.`end_time`,scp.combo_price as s_combo_price,
           scp.`combo_spu_id` as s_combo_spu_id,
           scp.spu_id as scp_spu_id,
           scp.`type` as s_type,
           scp.`least_num` as s_least_num,
           scp.`status` as s_status,
           scp.`required` as s_required
    from combo c
           join combo_spu scp on c.combo_id = scp.combo_id and c.status = scp.status
    where c.combo_id in (SELECT combo_id FROM `combo_spu` WHERE `type` = 1 AND `spu_id` = #{spuId} AND `status` = 1)
      and c.`status` = 1
  </select>

  <select id="listUnEndButNeedEndActivity" resultMap="comboMap">
    select combo_id, main_spu_id, `status`
    FROM combo
    WHERE `status` not in (-1, 0)
      and end_time &lt;= now()
  </select>

  <update id="comboSoldNumAdd">
    <foreach collection="comboList" item="combo" separator=";">
      update combo set sold_num = #{combo.soldNum} where combo_id = #{combo.comboId}
    </foreach>
  </update>

  <select id="comboIdByMainSpuId" resultMap="comboMap">
    select <include refid="Vo_Column_List"/> from combo where main_spu_id = #{spuId}
    <if test="status != null">
      and status = #{status}
    </if>
  </select>

  <select id="checkComboSpu" resultType="java.lang.Integer">
    SELECT count(*) FROM combo WHERE  `status` = 1 and shop_id = #{shopId} and combo_id IN
        (SELECT combo_id FROM combo_spu WHERE spu_id = #{spuId} AND status = 1)

  </select>

  <select id="notStartComboIdButStart" resultMap="comboMap">
    SELECT <include refid="Vo_Column_List"/> FROM combo WHERE start_time &lt; NOW() AND end_time &gt; NOW() AND `status` = 2
  </select>

  <select id="checkComboIsUsable" resultType="java.lang.Integer">
    SELECT count(*) FROM combo WHERE status != 1
    AND combo_id IN
    <foreach collection="comboIdList" item="comboId" open="(" separator="," close=")">
      #{comboId}
    </foreach>
  </select>

  <select id="getComboMainSpuIdBySpuId" resultType="java.lang.Long">
    SELECT
      main_spu_id
    FROM
      `combo`
    WHERE
      `status` = 1
      AND combo_id IN (
      SELECT
        combo_id
      FROM
        `combo_spu`
      WHERE
        `status` = 1
        AND spu_id = #{spuId}
        AND type = 2)
  </select>

  <select id="getComboByComboIdList" resultMap="comboMap">
    SELECT
      <include refid="Vo_Column_List"/>
    FROM
      `combo`
    WHERE
        combo_id IN
      <foreach collection="comboIdList" item="comboId" open="(" separator="," close=")">
        #{comboId}
      </foreach>
  </select>
</mapper>
