<?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.SpuSupplierChangeLogMapper">
  <resultMap id="spuSupplierChangeLogMap" type="com.tmerclub.cloud.product.model.SpuSupplierChangeLog">
    <id column="spu_change_id" property="spuChangeId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="spu_id" property="spuId"/>
    <result column="change_type" property="changeType"/>
    <result column="remarks" property="remarks"/>
  </resultMap>

  <resultMap id="spuSupplierChangeLogVOMap" type="com.tmerclub.cloud.product.vo.SpuSupplierChangeLogVO">
    <id column="spu_change_id" property="spuChangeId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="spu_id" property="spuId"/>
    <result column="change_type" property="changeType"/>
    <result column="remarks" property="remarks"/>
    <result column="spu_code" property="spuCode"/>
    <result column="main_img_url" property="mainImgUrl"/>
    <result column="spu_name" property="spuName"/>
    <result column="supplier_name" property="supplierName"/>
    <result column="supplier_id" property="supplierId"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `spu_change_id`,`create_time`,`update_time`,`spu_id`,`change_type`,`remarks`
  </sql>
  <select id="getBySpuChangeId" resultMap="spuSupplierChangeLogMap">
    select <include refid="Vo_Column_List"/> from spu_supplier_change_log where spu_change_id = #{spuChangeId}
  </select>
  <insert id="save">
    insert into spu_supplier_change_log (`spu_id`,`change_type`)
    values (#{spuSupplierChangeLog.spuId},#{spuSupplierChangeLog.changeType});
  </insert>
    <insert id="insertBatch">
      insert into spu_supplier_change_log (`spu_id`,`change_type`)  values
      <foreach collection="spuIds" item="spuId" separator=",">
       ( #{spuId}, #{changeType} )
      </foreach>
    </insert>
  <insert id="saveBatch">
    insert into spu_supplier_change_log (`spu_id`,`change_type`)  values
    <foreach collection="spuSupplierChangeLogs" item="spu" separator=",">
      ( #{spu.spuId}, #{spu.changeType}  )
    </foreach>
  </insert>
  <update id="update">
    update spu_supplier_change_log
    <set>
      <if test="spuSupplierChangeLog.spuId != null">
        `spu_id` = #{spuSupplierChangeLog.spuId},
      </if>
      <if test="spuSupplierChangeLog.changeType != null">
        `change_type` = #{spuSupplierChangeLog.changeType},
      </if>
      <if test="spuSupplierChangeLog.remarks != null">
        `remarks` = #{spuSupplierChangeLog.remarks},
      </if>
    </set>
    where spu_change_id = #{spuSupplierChangeLog.spuChangeId}
  </update>
  <delete id="deleteById">
    delete from spu_supplier_change_log where spu_change_id = #{spuChangeId}
  </delete>

  <select id="listBySpuIds" resultMap="spuSupplierChangeLogVOMap">
    SELECT sc.spu_id,s.spu_code,s.main_img_url,sl.spu_name,sc.create_time AS update_time,sc.change_type,sc.spu_change_id,sc.remarks,s.supplier_id
    FROM spu_supplier_change_log sc
    JOIN spu s ON sc.spu_id = s.spu_id
    JOIN spu_lang sl ON s.spu_id = sl.spu_id
    WHERE sc.spu_id IN
        <foreach collection="spuIds" item="spuId" open="(" separator="," close=")">
          #{spuId}
        </foreach>
    AND sl.lang = 1
    ORDER BY sc.create_time DESC
  </select>

</mapper>
