<?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.SpuPriceLogMapper">
  <resultMap id="spuPriceLogMap" type="com.tmerclub.cloud.product.model.SpuPriceLog">
    <id column="id" property="id" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="spu_id" property="spuId"/>
    <result column="sku_id" property="skuId"/>
    <result column="before_price" property="beforePrice"/>
    <result column="after_price" property="afterPrice"/>
    <result column="price" property="price"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `id`,`before_price`,`after_price`,`price`
  </sql>
  <select id="getById" resultMap="spuPriceLogMap">
    select <include refid="Vo_Column_List"/> ,`create_time`,  `update_time`, `spu_id`, `sku_id`from spu_price_log where id = #{id}
  </select>
  <select id="pageByParam" resultType="com.tmerclub.cloud.product.vo.SpuPriceLogVO">
    select <include refid="Vo_Column_List"/>, spl.`create_time`,  spl.`update_time`, spl.`spu_id`, spl.`sku_id`, sp.spu_name, sk.party_code, skl.sku_name, s.main_img_url
    from spu_price_log  spl
    join spu_lang sp on spl.spu_id = sp.spu_id
    join spu s on s.spu_id = spl.spu_id AND s.`status` != -1
    join sku sk on spl.sku_id = sk.sku_id
    left join sku_lang skl on  skl.sku_id = spl.sku_id AND skl.`lang` =1
    <where>
        sp.lang = 1
      <if test="spuPriceLogDTO.prodKey != null and spuPriceLogDTO.prodKey != ''">
        and
        (sp.spu_name like CONCAT("%",#{spuPriceLogDTO.prodKey},"%")
        or sk.party_code like CONCAT("%",#{spuPriceLogDTO.prodKey},"%") )
      </if>
      <if test="spuPriceLogDTO.createStartTime != null">
        and spl.create_time &gt;= #{spuPriceLogDTO.createStartTime}
      </if>
      <if test="spuPriceLogDTO.createEndTime != null">
        and spl.create_time &lt;= #{spuPriceLogDTO.createEndTime}
      </if>
      <if test="spuPriceLogDTO.supplierId != null">
         and s.spu_id in (select spu_id from spu where supplier_id = #{spuPriceLogDTO.supplierId})
      </if>
    </where>
    order by
    <if test="spuPriceLogDTO.sort != null ">
      <if test="spuPriceLogDTO.sort == 1">
        sk.`party_code` ASC
      </if>
      <if test="spuPriceLogDTO.sort == 2">
       sk.`party_code` DESC
      </if>
      <if test="spuPriceLogDTO.sort == 3">
         spl.`before_price` ASC
      </if>
      <if test="spuPriceLogDTO.sort == 4">
         spl.`before_price` DESC
      </if>
      <if test="spuPriceLogDTO.sort == 5">
         spl.`after_price` ASC
      </if>
      <if test="spuPriceLogDTO.sort == 6">
        spl.`after_price` DESC
      </if>
      <if test="spuPriceLogDTO.sort == 7">
         spl.`price` ASC
      </if>
      <if test="spuPriceLogDTO.sort == 8">
         spl.`price` DESC
      </if>
    </if>
    <if test="spuPriceLogDTO.sort == null">
       spl.`id` desc
    </if>
  </select>

  <insert id="save">
    insert into spu_price_log (`spu_id`,`sku_id`,`before_price`,`after_price`,`price`)
    values (#{spuPriceLog.spuId},#{spuPriceLog.skuId},#{spuPriceLog.beforePrice},#{spuPriceLog.afterPrice},#{spuPriceLog.price});
  </insert>
  <insert id="insertBatch">
    <foreach collection="spuPriceLogList" item="spuPriceLog">
      insert into spu_price_log (`spu_id`,`sku_id`,`before_price`,`after_price`,`price`)
      values  (#{spuPriceLog.spuId},#{spuPriceLog.skuId},#{spuPriceLog.beforePrice},#{spuPriceLog.afterPrice},#{spuPriceLog.price});
    </foreach>
  </insert>
  <update id="update">
    update spu_price_log
    <set>
      <if test="spuPriceLog.spuId != null">
        `spu_id` = #{spuPriceLog.spuId},
      </if>
      <if test="spuPriceLog.skuId != null">
        `sku_id` = #{spuPriceLog.skuId},
      </if>
      <if test="spuPriceLog.beforePrice != null">
        `before_price` = #{spuPriceLog.beforePrice},
      </if>
      <if test="spuPriceLog.afterPrice != null">
        `after_price` = #{spuPriceLog.afterPrice},
      </if>
      <if test="spuPriceLog.price != null">
        `price` = #{spuPriceLog.price},
      </if>
    </set>
    where id = #{spuPriceLog.id}
  </update>
  <delete id="deleteById">
    delete from spu_price_log where id = #{id}
  </delete>

</mapper>
