<?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.delivery.mapper.StationMapper">
  <resultMap id="stationMap" type="com.tmerclub.cloud.delivery.model.Station">
    <id column="station_id" property="stationId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="shop_id" property="shopId"/>
    <result column="station_name" property="stationName"/>
    <result column="img_url" property="imgUrl"/>
    <result column="mobile_prefix" property="mobilePrefix"/>
    <result column="mobile" property="mobile"/>
    <result column="province_id" property="provinceId"/>
    <result column="province" property="province"/>
    <result column="city_id" property="cityId"/>
    <result column="city" property="city"/>
    <result column="area_id" property="areaId"/>
    <result column="area" property="area"/>
    <result column="post_code" property="postCode"/>
    <result column="addr" property="addr"/>
    <result column="lng" property="lng"/>
    <result column="lat" property="lat"/>
    <result column="time_info" property="timeInfo"/>
    <result column="account" property="account"/>
    <result column="stock_mode" property="stockMode"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `station_id`,`create_time`,`update_time`,`shop_id`,`station_name`,`img_url`,`mobile_prefix`,`mobile`,`province_id`,`province`,`city_id`,`city`,`area_id`,`area`,`post_code`,`addr`,`lng`,`lat`,`time_info`,`status`,`account`,`stock_mode`
  </sql>
  <select id="list" resultType="com.tmerclub.cloud.api.delivery.vo.StationVO">
    select <include refid="Vo_Column_List"/> from station order by station_id desc
  </select>
  <select id="getByStationId" resultType="com.tmerclub.cloud.api.delivery.vo.StationVO">
    SELECT
      <include refid="Vo_Column_List"/>
    FROM
      station
    WHERE
      station_id = #{stationId}
  </select>

  <select id="listStation" resultType="com.tmerclub.cloud.api.delivery.vo.StationVO">
    SELECT
      <include refid="Vo_Column_List"/>
    FROM
      station
    WHERE
      `status` != -1
      <if test="stationDTO.shopId != null">
        AND shop_id = #{stationDTO.shopId}
      </if>
      <if test="stationDTO.shopIds != null and stationDTO.shopIds.size() != 0">
        AND shop_id in
        <foreach collection="stationDTO.shopIds" item="shopId" open="(" separator="," close=")">
          #{shopId}
        </foreach>
      </if>
      <if test="stationDTO.stationName != null and stationDTO.stationName != ''">
        AND station_name LIKE CONCAT( '%', #{stationDTO.stationName}, '%' )
      </if>
      <if test="stationDTO.status != null">
        AND status = #{stationDTO.status}
      </if>
      <if test="stationDTO.account != null and stationDTO.account != ''">
        AND account LIKE CONCAT( '%', #{stationDTO.account}, '%' )
      </if>
      <if test="stationDTO.addr != null and stationDTO.addr != ''">
        AND CONCAT( province, city, area, addr ) LIKE CONCAT( '%', #{stationDTO.addr}, '%' )
      </if>
    <if test="stationDTO.stockMode != null">
      AND stock_mode = #{stationDTO.stockMode}
    </if>
    ORDER BY
      station_id DESC
  </select>

  <insert id="save">
    insert into station (`station_id`,`shop_id`,`station_name`,`img_url`,`mobile_prefix`,`mobile`,`province_id`,`province`,`city_id`,`city`,`area_id`,`area`,`post_code`,`addr`,`lng`,`lat`,`time_info`,`status`,`stock_mode`)
    values (#{station.stationId},#{station.shopId},#{station.stationName},#{station.imgUrl},#{station.mobilePrefix},#{station.mobile},#{station.provinceId},#{station.province},#{station.cityId},#{station.city},#{station.areaId},#{station.area},#{station.postCode},#{station.addr},#{station.lng},#{station.lat},#{station.timeInfo},#{station.status},#{station.stockMode});
  </insert>
  <update id="update">
    update station
    <set>
      <if test="station.shopId != null">
        `shop_id` = #{station.shopId},
      </if>
      <if test="station.stationName != null">
        `station_name` = #{station.stationName},
      </if>
      <if test="station.imgUrl != null">
        `img_url` = #{station.imgUrl},
      </if>
      <if test="station.mobilePrefix != null">
        `mobile_prefix` = #{station.mobilePrefix},
      </if>
      <if test="station.mobile != null">
        `mobile` = #{station.mobile},
      </if>
      <if test="station.provinceId != null">
        `province_id` = #{station.provinceId},
      </if>
      <if test="station.province != null">
        `province` = #{station.province},
      </if>
      <if test="station.cityId != null">
        `city_id` = #{station.cityId},
      </if>
      <if test="station.city != null">
        `city` = #{station.city},
      </if>
      <if test="station.areaId != null">
        `area_id` = #{station.areaId},
      </if>
      <if test="station.area != null">
        `area` = #{station.area},
      </if>
      <if test="station.postCode != null">
        `post_code` = #{station.postCode},
      </if>
      <if test="station.addr != null">
        `addr` = #{station.addr},
      </if>
      <if test="station.lng != null">
        `lng` = #{station.lng},
      </if>
      <if test="station.lat != null">
        `lat` = #{station.lat},
      </if>
      <if test="station.timeInfo != null">
        `time_info` = #{station.timeInfo},
      </if>
      <if test="station.status != null">
        `status` = #{station.status},
      </if>
      <if test="station.account != null">
        `account` = #{station.account}
      </if>
      <if test="station.stockMode != null">
        `stock_mode` = #{station.stockMode}
      </if>
    </set>
    where station_id = #{station.stationId}
  </update>

  <update id="logicDeleteById">
    UPDATE `station`
    SET `status` = -1
    WHERE
      station_id = #{stationId}
  </update>

  <delete id="deleteById">
    delete from station where station_id = #{stationId}
  </delete>

  <select id="getStationList" resultType="com.tmerclub.cloud.order.vo.StationDetailVO">
    SELECT
    *
    <if test="orderStationDTO.lat != null and orderStationDTO.lng != null" >
      , ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN((
      #{orderStationDTO.lat} * PI() / 180 - lat * PI() / 180
      ) / 2), 2) + COS(#{orderStationDTO.lat} * PI() / 180) * COS(lat * PI() / 180) * POW(SIN(
      (#{orderStationDTO.lng} * PI() / 180 - lng * PI() / 180) / 2),2 ) )) * 1000) AS distance
    </if>
    FROM
    station
    WHERE status = 1
    <if test="orderStationDTO.stationName != null and orderStationDTO.stationName !='' ">
      AND (station_name LIKE '%${orderStationDTO.stationName}%' or
           province LIKE '%${orderStationDTO.stationName}%' or
           city LIKE '%${orderStationDTO.stationName}%' or
           area LIKE '%${orderStationDTO.stationName}%' or
           addr LIKE '%${orderStationDTO.stationName}%')
    </if>
    <if test="orderStationDTO.stationId != null">
      AND station_id = #{orderStationDTO.stationId}
    </if>
    <if test="orderStationDTO.shopId != null">
      AND shop_id = #{orderStationDTO.shopId}
    </if>
    <if test="orderStationDTO.stationIds != null and orderStationDTO.stationIds.size() > 0">
      AND station_id in
      <foreach collection="orderStationDTO.stationIds" item="stationId" open="(" close=")" separator=",">
        #{stationId}
      </foreach>
    </if>
    <if test="orderStationDTO.lat != null and orderStationDTO.lng != null" >
      ORDER BY distance ASC
    </if>
    <if test="orderStationDTO.lat == null and orderStationDTO.lng == null" >
      ORDER BY create_time DESC
    </if>
    LIMIT #{page.begin},#{page.size}
  </select>

  <select id="getStationDistance" resultType="java.lang.Double">
    SELECT
    ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN((
    #{lat} * PI() / 180 - lat * PI() / 180
    ) / 2), 2) + COS(#{lat} * PI() / 180) * COS(lat * PI() / 180) * POW(SIN(
    (#{lng} * PI() / 180 - lng * PI() / 180) / 2),2 ) )) * 1000) AS distance
    FROM
    station
    WHERE
    status = 1
    AND station_id = #{stationId}
  </select>

  <select id="getStationListById" resultType="com.tmerclub.cloud.api.delivery.vo.StationVO">
    SELECT
    <include refid="Vo_Column_List"/>
    FROM
    station
    <where>
      <if test="stationIds != null and stationIds.size() != 0">
        station_id in
        <foreach collection="stationIds" item="stationId" open="(" separator="," close=")">
          #{stationId}
        </foreach>
      </if>
    </where>
  </select>
  <select id="listSimpleStation" resultType="com.tmerclub.cloud.order.vo.StationDetailVO">
    SELECT `station_id`,`stock_mode`
    FROM station where `status` = #{status} and shop_id = #{shopId}
  </select>
</mapper>
