<?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.order.mapper.OrderItemLangMapper">
  <resultMap id="orderItemLangMap" type="com.tmerclub.cloud.order.model.OrderItemLang">
    <result column="order_item_id" property="orderItemId" />
    <result column="lang" property="lang"/>
    <result column="spu_name" property="spuName"/>
    <result column="sku_name" property="skuName"/>
    <result column="supplier_spu_name" property="supplierSpuName"/>
    <result column="supplier_sku_name" property="supplierSkuName"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `order_item_id`,`lang`,`spu_name`,`sku_name`,`supplier_spu_name`,`supplier_sku_name`
  </sql>
  <select id="list" resultMap="orderItemLangMap">
    select <include refid="Vo_Column_List"/> from order_item_lang order by order_item_id desc
  </select>
  <select id="getByOrderItemId" resultMap="orderItemLangMap">
    select <include refid="Vo_Column_List"/> from order_item_lang where order_item_id = #{orderItemId}
  </select>

  <insert id="saveBatch">
    insert into order_item_lang (`order_item_id`,`lang`,`spu_name`,`sku_name`,`supplier_spu_name`,`supplier_sku_name`)
    values
    <foreach collection="orderItemLangList" item="orderItemLang" separator=",">
      (#{orderItemLang.orderItemId},#{orderItemLang.lang},#{orderItemLang.spuName},#{orderItemLang.skuName},#{orderItemLang.supplierSpuName},#{orderItemLang.supplierSkuName})
    </foreach>
  </insert>
  <insert id="saveCueOrderItemLang">
    insert into order_item_lang (
      `order_item_id`,
      `lang`,
      `spu_name`
    ) values (
        #{orderItemLang.orderItemId},
        #{orderItemLang.lang},
        #{orderItemLang.spuName}
    )
    </insert>
    <update id="update">
    update order_item_lang
    <set>
      <if test="orderItemLang.lang != null">
        `lang` = #{orderItemLang.lang},
      </if>
      <if test="orderItemLang.spuName != null">
        `spu_name` = #{orderItemLang.spuName},
      </if>
      <if test="orderItemLang.skuName != null">
        `sku_name` = #{orderItemLang.skuName},
      </if>
      <if test="orderItemLang.supplierSpuName != null">
        `spu_name` = #{orderItemLang.supplierSpuName},
      </if>
      <if test="orderItemLang.supplierSkuName != null">
        `sku_name` = #{orderItemLang.supplierSkuName},
      </if>
    </set>
    where order_item_id = #{orderItemLang.orderItemId}
  </update>
  <delete id="deleteById">
    delete from order_item_lang where order_item_id = #{orderItemId}
  </delete>
  <delete id="deleteBatchByIds">
    delete from order_item_lang where order_item_id  in
    <foreach collection="orderItemIds" item="orderItemId" open="(" separator="," close=")">
      #{orderItemId}
    </foreach>
  </delete>

  <select id="getLangListByOrderIds" resultType="com.tmerclub.cloud.common.order.vo.OrderItemLangVO">
    SELECT oil.order_item_id,oil.lang,oil.spu_name
    FROM order_item_lang oil
    JOIN order_item oi ON  oi.order_item_id = oil.order_item_id
    WHERE oi.order_id IN
      <foreach collection="orderIdList" item="orderId" open="(" close=")" separator=",">
        #{orderId}
      </foreach>
  </select>
  <select id="listOrderItemLangByIds" resultType="com.tmerclub.cloud.common.order.vo.OrderItemLangVO">
    SELECT <include refid="Vo_Column_List"/> FROM order_item_lang
    WHERE
      <foreach collection="orderItemIds" item="orderItemId" open="(" close=")" separator="OR">
        order_item_id = #{orderItemId}
      </foreach>
  </select>

  <select id="getListByOrderItemId" resultType="com.tmerclub.cloud.common.order.vo.OrderItemVO">
    SELECT oi.order_item_id, oil.sku_name, oi.create_time
    FROM order_item oi
    LEFT JOIN order_item_lang oil ON oi.order_item_id = oil.order_item_id
    WHERE oil.lang = #{lang} AND oi.order_item_id in
    <foreach collection="orderItemIds" item="orderItemId" open="(" close=")" separator=",">
      #{orderItemId}
    </foreach>
  </select>
</mapper>
