<?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.SpuAttrValueMapper">
  <resultMap id="spuAttrValueMap" type="com.tmerclub.cloud.product.model.SpuAttrValue">
    <id column="spu_attr_value_id" property="spuAttrValueId" />
    <result column="spu_id" property="spuId"/>
    <result column="attr_id" property="attrId"/>
    <result column="attr_name" property="attrName"/>
    <result column="attr_value_id" property="attrValueId"/>
    <result column="attr_value_name" property="attrValueName"/>
  </resultMap>
  <resultMap id="SpuAttrValueVOMap" type="com.tmerclub.cloud.common.product.vo.SpuAttrValueVO">
    <id column="attr_id" property="attrId"/>
    <result column="attr_value_id" property="attrValueId"/>
    <result column="search_type" property="searchType"/>
    <collection property="spuAttrValueLangList" ofType="com.tmerclub.cloud.common.product.vo.SpuAttrValueLangVO">
      <id column="lang" property="lang"/>
      <result column="attr_name" property="attrName"/>
      <result column="attr_value_name" property="attrValueName"/>
      <result column="attr_desc" property="attrDesc"/>
    </collection>
  </resultMap>
  <sql id="Vo_Column_List">
    `spu_attr_value_id`,`spu_id`,`attr_id`,`attr_name`,`attr_value_id`,`attr_value_name`
  </sql>

  <insert id="batchSave">
    insert into spu_attr_value (`spu_id`,`lang`,`attr_id`,`attr_name`,`attr_value_id`,`attr_value_name`)values
    <foreach collection="spuAttrValues" item="spuAttrValue" separator=",">
      (#{spuAttrValue.spuId},#{spuAttrValue.lang},#{spuAttrValue.attrId},#{spuAttrValue.attrName},#{spuAttrValue.attrValueId},#{spuAttrValue.attrValueName})
    </foreach>
  </insert>

  <update id="deleteBySpuId">
    delete from spu_attr_value where spu_id = #{spuId}
  </update>

  <delete id="deleteByAttId">
    DELETE FROM spu_attr_value WHERE attr_id = #{attrId} AND attr_value_id IN
    <foreach collection="attrValueIds" item="attrValueId" open="(" close=")" separator=",">
      #{attrValueId}
    </foreach>
  </delete>

  <delete id="deleteByCategoryIds">
    DELETE FROM spu_attr_value WHERE attr_id = #{attrId} AND spu_id IN (SELECT spu_id FROM spu WHERE category_id IN
    <foreach collection="categoryIds" item="categoryId" open="(" close=")" separator=",">
      #{categoryId}
    </foreach>
    )
  </delete>

  <select id="getSpuAttrsBySpuId" resultMap="SpuAttrValueVOMap">
    SELECT sav.lang,sav.attr_id,sav.attr_name,sav.attr_value_id,sav.attr_value_name,sav.attr_desc,a.search_type
    FROM spu_attr_value sav
    JOIN attr a ON a.attr_id = sav.attr_id
    WHERE sav.spu_id = #{spuId}
  </select>

  <update id="batchUpdate">
    <foreach collection="spuAttrValues" item="spuAttrValue">
      update spu_attr_value
      set `attr_value_id` = #{spuAttrValue.attrValueId},`attr_value_name` = #{spuAttrValue.attrValueName}
      where spu_id = #{spuAttrValue.spuId} and attr_id = #{spuAttrValue.attrId} and lang = #{spuAttrValue.lang};
    </foreach>
  </update>
  <update id="batchUpdateSpuAttrValue">
    <foreach collection="spuAttrValues" item="spuAttrValue">
      update spu_attr_value
      <set>
        <if test="spuAttrValue.attrName != null">
          `attr_name` = #{spuAttrValue.attrName}
        </if>
        <if test="spuAttrValue.attrValueName != null">
          ,`attr_value_name` = #{spuAttrValue.attrValueName}
        </if>
      </set>
      where `attr_value_id` = #{spuAttrValue.attrValueId} and attr_id = #{spuAttrValue.attrId} and lang = #{spuAttrValue.lang};
    </foreach>
  </update>

  <delete id="deleteBatchBySpuIdAndAttrIds">
    delete from spu_attr_value where spu_id = #{spuId} and attr_id in
    <foreach collection="attrIds" item="attrId" open="(" close=")" separator=",">
      #{attrId}
    </foreach>
  </delete>

  <select id="spuIdListByAttrValueIds" resultType="java.lang.Long">
    select distinct spu_id from spu_attr_value where attr_value_id in
    <foreach collection="attrValueIds" item="attrValueId" open="(" close=")" separator=",">
      #{attrValueId}
    </foreach>
  </select>
</mapper>
