<?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.OrderVirtualInfoMapper">
  <resultMap id="orderVirtualInfoMap" type="com.tmerclub.cloud.order.model.OrderVirtualInfo">
    <id column="order_virtual_info_id" property="orderVirtualInfoId" />
    <result column="order_id" property="orderId"/>
    <result column="order_item_id" property="orderItemId"/>
    <result column="shop_id" property="shopId"/>
    <result column="station_id" property="stationId"/>
    <result column="write_off_code" property="writeOffCode"/>
    <result column="is_write_off" property="isWriteOff"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="write_off_time" property="writeOffTime"/>
    <result column="write_off_multiple_count" property="writeOffMultipleCount"/>
  </resultMap>

  <resultMap id="orderVirtualInfoVOMap" type="com.tmerclub.cloud.common.order.vo.OrderVirtualInfoVO">
    <id column="order_virtual_info_id" property="orderVirtualInfoId" />
    <result column="order_id" property="orderId"/>
    <result column="order_item_id" property="orderItemId"/>
    <result column="shop_id" property="shopId"/>
    <result column="station_id" property="stationId"/>
    <result column="write_off_code" property="writeOffCode"/>
    <result column="is_write_off" property="isWriteOff"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="write_off_time" property="writeOffTime"/>
    <result column="write_off_multiple_count" property="writeOffMultipleCount"/>
  </resultMap>

  <sql id="Vo_Column_List">
    `order_virtual_info_id`,`order_id`,`order_item_id`,`shop_id`,`station_id`,`write_off_code`,`is_write_off`,`create_time`,`update_time`,`write_off_time`,`write_off_multiple_count`
  </sql>
  <select id="list" resultMap="orderVirtualInfoMap">
    select <include refid="Vo_Column_List"/> from order_virtual_info order by order_virtual_info_id desc
  </select>
  <select id="getById" resultMap="orderVirtualInfoMap">
    select <include refid="Vo_Column_List"/> from order_virtual_info where order_virtual_info_id = #{id}
  </select>
  <insert id="save">
    insert into order_virtual_info (`order_virtual_info_id`,`order_id`,`shop_id`,`station_id`,`write_off_code`,`is_write_off`,`write_off_time`,`write_off_multiple_count`)
    values (#{orderVirtualInfo.orderVirtualInfoId}, #{orderVirtualInfo.orderId},#{orderVirtualInfo.shopId},#{orderVirtualInfo.stationId},#{orderVirtualInfo.writeOffCode},#{orderVirtualInfo.isWriteOff},#{orderVirtualInfo.writeOffTime},#{orderVirtualInfo.writeOffMultipleCount});
  </insert>
  <update id="update">
    update order_virtual_info
    <set>
      <if test="orderVirtualInfo.stationId != null">
        `station_id` = #{orderVirtualInfo.stationId},
      </if>
      <if test="orderVirtualInfo.isWriteOff != null">
        `is_write_off` = #{orderVirtualInfo.isWriteOff},
      </if>
      <if test="orderVirtualInfo.writeOffTime != null">
        `write_off_time` = #{orderVirtualInfo.writeOffTime},
      </if>
      <if test="orderVirtualInfo.writeOffMultipleCount != null">
        `write_off_multiple_count` = #{orderVirtualInfo.writeOffMultipleCount},
      </if>
    </set>
    where order_virtual_info_id = #{orderVirtualInfo.orderVirtualInfoId}
  </update>

  <update id="updateBatchByItemId">
    <foreach collection="orderVirtualInfos" item="orderVirtualInfo" separator=";">
      update order_virtual_info
      <set>
        <if test="orderVirtualInfo.orderId != null">
          `order_id` = #{orderVirtualInfo.orderId}
        </if>
      </set>
      where order_item_id = #{orderVirtualInfo.orderItemId}
    </foreach>
  </update>

  <delete id="deleteById">
    delete from order_virtual_info where order_virtual_info_id = #{id}
  </delete>

  <insert id="saveBatch">
    insert into order_virtual_info (`order_virtual_info_id`,`order_id`,`order_item_id`,`shop_id`,`station_id`,`write_off_code`,`is_write_off`,`write_off_time`,`write_off_multiple_count`)
    values
    <foreach collection="orderVirtualInfoList" item="orderVirtualInfo" separator=",">
      (#{orderVirtualInfo.orderVirtualInfoId}, #{orderVirtualInfo.orderId},#{orderVirtualInfo.orderItemId},#{orderVirtualInfo.shopId},#{orderVirtualInfo.stationId},#{orderVirtualInfo.writeOffCode},#{orderVirtualInfo.isWriteOff},#{orderVirtualInfo.writeOffTime},#{orderVirtualInfo.writeOffMultipleCount})
    </foreach>
  </insert>

  <select id="getByOrderIdAndStationId" resultMap="orderVirtualInfoVOMap">
    select <include refid="Vo_Column_List"/>  from order_virtual_info  where order_id = #{orderId}
    <if test="stationId != null">
        and station_id = #{stationId}
        and is_write_off = 1
    </if>
  </select>
  <select id="getByOrderIdAndShopId" resultMap="orderVirtualInfoVOMap">
    select <include refid="Vo_Column_List"/> from order_virtual_info
    where order_id = #{orderId}
    and shop_id = #{shopId}
    and is_write_off = #{isWriteOff}
  </select>
  <select id="getByOrderIdAndCode" resultMap="orderVirtualInfoVOMap">
    select <include refid="Vo_Column_List"/> from order_virtual_info
    where order_id = #{orderId}
    <if test="orderId != code">
      and write_off_code = #{code}
    </if>
  </select>

  <select id="countWriteOffByOrderId" resultType="int">
    select ifnull(count(*),0) as count from order_virtual_info where order_id = #{orderId} and is_write_off = 1
  </select>

  <select id="getByOrderId" resultType="com.tmerclub.cloud.common.order.vo.OrderVirtualInfoVO">
    SELECT ovi.write_off_code, ovi.is_write_off,
           IF(NOW() > o.write_off_end, 1, 0) AS is_expired
    FROM order_virtual_info ovi
           JOIN `order` o ON o.order_id = ovi.order_id
    WHERE ovi.order_id = #{orderId}
  </select>

  <select id="listByOrderItemIds" resultType="com.tmerclub.cloud.common.order.vo.OrderVirtualInfoVO">
    SELECT <include refid="Vo_Column_List"/>
    FROM order_virtual_info ovi
    WHERE ovi.order_item_id in
    <foreach collection="orderItemIds" item="orderItemId" open="(" separator="," close=")">
      #{orderItemId}
    </foreach>
  </select>

  <delete id="deleteByOrderItemId">
    delete from order_virtual_info
    WHERE order_item_id in
    <foreach collection="orderItemIds" item="orderItemId" open="(" separator="," close=")">
      #{orderItemId}
    </foreach>
  </delete>
</mapper>
