<?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.OrderSelfStationMapper">
  <resultMap id="orderSelfStationMap" type="com.tmerclub.cloud.order.model.OrderSelfStation">
    <id column="order_self_station_id" property="orderSelfStationId" />
    <result column="update_time" property="updateTime"/>
    <result column="create_time" property="createTime"/>
    <result column="shop_id" property="shopId"/>
    <result column="order_id" property="orderId"/>
    <result column="station_id" property="stationId"/>
    <result column="station_user_mobile" property="stationUserMobile"/>
    <result column="station_user_name" property="stationUserName"/>
    <result column="station_time" property="stationTime"/>
    <result column="station_code" property="stationCode"/>
    <result column="station_address" property="stationAddress"/>
    <result column="station_phone" property="stationPhone"/>
  </resultMap>
  <resultMap id="stationOrderInfo" type="com.tmerclub.cloud.common.order.vo.OrderVO">
    <result column="order_id" jdbcType="VARCHAR" property="orderId"/>
    <result column="actual_total" jdbcType="DECIMAL" property="actualTotal"/>
    <result column="status" jdbcType="INTEGER" property="status"/>
    <result column="order_type" jdbcType="VARCHAR" property="orderType"/>
    <result column="dvy_id" jdbcType="VARCHAR" property="dvyId"/>
    <result column="shop_id" jdbcType="BIGINT" property="shopId"/>
    <result column="create_time" property="createTime"/>
    <result column="all_count" property="allCount"/>
    <result column="remarks" property="remarks"/>
    <result column="shop_name" property="shopName"/>
    <association property="orderSelfStationVO" javaType="com.tmerclub.cloud.common.order.vo.OrderSelfStationVO">
      <result column="station_id" property="stationId"/>
      <result column="station_user_mobile" property="stationUserMobile"/>
      <result column="station_user_name" property="stationUserName"/>
      <result column="station_time" property="stationTime"/>
      <result column="station_code" property="stationCode"/>
      <result column="station_address" property="stationAddress"/>
      <result column="station_phone" property="stationPhone"/>
    </association>
    <collection property="orderItems" ofType="com.tmerclub.cloud.common.order.vo.OrderItemVO">
      <result column="order_id" jdbcType="VARCHAR" property="orderId"/>
      <result column="pic" jdbcType="VARCHAR" property="pic"/>
      <result column="spu_id" jdbcType="BIGINT" property="spuId"/>
      <result column="spu_name" jdbcType="VARCHAR" property="spuName"/>
      <result column="count" jdbcType="INTEGER" property="count"/>
      <result column="use_score" jdbcType="INTEGER" property="useScore"/>
      <result column="price" jdbcType="DECIMAL" property="price"/>
      <result column="sku_name" jdbcType="VARCHAR" property="skuName"/>
      <result column="order_item_id" property="orderItemId"/>
      <result column="is_comm" property="isComm"/>
      <result column="comm_time" property="commTime"/>
    </collection>
  </resultMap>
  <sql id="Vo_Column_List">
    `order_self_station_id`,`update_time`,`create_time`,`shop_id`,`order_id`,`station_id`,`station_user_mobile`,`station_user_name`,`station_time`,`station_code`,`station_address`,`station_phone`
  </sql>
  <select id="list" resultType="com.tmerclub.cloud.common.order.vo.OrderSelfStationVO">
    select <include refid="Vo_Column_List"/> from order_self_station order by order_self_station_id desc
  </select>
  <select id="getById" resultType="com.tmerclub.cloud.common.order.vo.OrderSelfStationVO">
    select <include refid="Vo_Column_List"/> from order_self_station where order_self_station_id = #{id}
  </select>
  <insert id="save">
    insert into order_self_station (`order_self_station_id`,`shop_id`,`user_id`,`order_id`,`station_id`,`station_user_mobile`,`station_user_name`,`station_time`,`station_code`,`station_address`,`station_phone`)
    values (#{orderSelfStation.orderSelfStationId},#{orderSelfStation.shopId},#{orderSelfStation.userId},#{orderSelfStation.orderId},#{orderSelfStation.stationId},#{orderSelfStation.stationUserMobile},
            #{orderSelfStation.stationUserName},#{orderSelfStation.stationTime},#{orderSelfStation.stationCode},#{orderSelfStation.stationAddress},#{orderSelfStation.stationPhone});
  </insert>
  <insert id="saveBatch">
      insert into order_self_station (`order_self_station_id`,`shop_id`,`user_id`,`order_id`,`station_id`,`station_user_mobile`,`station_user_name`,`station_time`,`station_code`,`station_address`,`station_phone`)
      values
      <foreach collection="orderSelfStations" item="orderSelfStation" separator=",">
        (#{orderSelfStation.orderSelfStationId},#{orderSelfStation.shopId},#{orderSelfStation.userId},#{orderSelfStation.orderId},#{orderSelfStation.stationId},#{orderSelfStation.stationUserMobile},
        #{orderSelfStation.stationUserName},#{orderSelfStation.stationTime},#{orderSelfStation.stationCode},#{orderSelfStation.stationAddress},#{orderSelfStation.stationPhone})
      </foreach>
  </insert>
  <update id="update">
    update order_self_station
    <set>
      <if test="orderSelfStation.shopId != null">
        `shop_id` = #{orderSelfStation.shopId},
      </if>
      <if test="orderSelfStation.orderId != null">
        `order_id` = #{orderSelfStation.orderId},
      </if>
      <if test="orderSelfStation.stationId != null">
        `station_id` = #{orderSelfStation.stationId},
      </if>
      <if test="orderSelfStation.stationUserMobile != null">
        `station_user_mobile` = #{orderSelfStation.stationUserMobile},
      </if>
      <if test="orderSelfStation.stationUserName != null">
        `station_user_name` = #{orderSelfStation.stationUserName},
      </if>
      <if test="orderSelfStation.stationTime != null">
        `station_time` = #{orderSelfStation.stationTime},
      </if>
      <if test="orderSelfStation.stationCode != null">
        `station_code` = #{orderSelfStation.stationCode},
      </if>
      <if test="orderSelfStation.stationAddress != null">
        `station_address` = #{orderSelfStation.stationAddress},
      </if>
      <if test="orderSelfStation.stationPhone != null">
        `station_phone` = #{orderSelfStation.stationPhone},
      </if>
    </set>
    where order_self_station_id = #{orderSelfStation.orderSelfStationId}
  </update>
  <delete id="deleteById">
    delete from order_self_station where order_self_station_id = #{id}
  </delete>

  <select id="getStationUserInfo" resultType="com.tmerclub.cloud.order.model.OrderSelfStation">
    SELECT * FROM
      (
        SELECT order_self_station_id,station_user_name,station_user_mobile FROM order_self_station
        WHERE user_id = #{userId}
        ORDER BY create_time DESC
      ) AS item
    GROUP BY item.station_user_name,item.station_user_mobile
    ORDER BY item.id DESC
  </select>

  <select id="getStationByOrderId" resultType="com.tmerclub.cloud.common.order.vo.OrderSelfStationVO">
    SELECT *
    FROM `order_self_station`
    WHERE `order_id` = #{orderId}
  </select>
  <select id="getUserStation" resultType="com.tmerclub.cloud.common.order.vo.OrderSelfStationVO">
      SELECT oss.*
      FROM `order` o
      JOIN `order_self_station` oss ON o.`order_id` = oss.`order_id`
      WHERE o.`delivery_type` = 2 AND o.user_id = #{userId}
      ORDER BY update_time DESC
        LIMIT 0,3
  </select>
  <select id="getOrderSelfStationByStationIdOrShopId" resultType="com.tmerclub.cloud.common.order.vo.OrderSelfStationVO">
    SELECT oss.* FROM `order` o
                        JOIN `order_self_station` oss ON o.`order_id` = oss.`order_id`
    WHERE o.`delivery_type` = 2 AND o.user_id = #{userId} AND o.`status` = 2
    <if test=" stationId != null and stationId !='' ">
      AND oss.station_id = #{stationId}
    </if>
    <if test=" shopId != null and shopId !='' ">
      AND o.shop_id = #{shopId}
    </if>
    ORDER BY update_time DESC
  </select>


    <select id="listSelfStationByOrderIds" resultType="com.tmerclub.cloud.common.order.vo.OrderSelfStationVO">
      SELECT
        *
      FROM
        `order_self_station`
      WHERE
        `order_id` IN
          <foreach collection="orderIds" item="orderId" open="(" separator="," close=")">
            #{orderId}
          </foreach>
    </select>
</mapper>
