<?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.warehouse.mapper.WarehouseMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap id="warehouseVOMap" type="com.tmerclub.cloud.warehouse.model.vo.WarehouseVO">
        <result property="warehouseId" column="warehouse_id"/>
        <result property="warehouseName" column="warehouse_name"/>
        <result property="warehouseStatus" column="warehouse_status"/>
        <result property="principalName" column="principal_name"/>
        <result property="principalPhone" column="principal_phone"/>
        <result property="provinceId" column="province_id"/>
        <result property="province" column="province"/>
        <result property="cityId" column="city_id"/>
        <result property="city" column="city"/>
        <result property="areaId" column="area_id"/>
        <result property="area" column="area"/>
        <result property="postCode" column="post_code"/>
        <result property="addr" column="addr"/>
        <result property="lng" column="lng"/>
        <result property="lat" column="lat"/>
        <result property="updateTime" column="update_time"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="Vo_Column_List">
        shop_id,warehouse_id,warehouse_name,warehouse_status,principal_name,principal_phone,province_id,province,city_id,city,area_id,area,post_code,addr,lng,lat,deleted,update_time,create_time
    </sql>

    <select id="list" resultMap="warehouseVOMap">
        SELECT
            <include refid="Vo_Column_List" />
        FROM
            warehouse
        <where>
            deleted = 0
            <if test="dto.shopId != null">
                AND shop_id = #{dto.shopId}
            </if>
            <if test="dto.warehouseName != null and dto.warehouseName != ''">
                AND warehouse_name LIKE CONCAT('%', #{dto.warehouseName}, '%')
            </if>
            <if test="dto.warehouseStatus != null">
                AND warehouse_status = #{dto.warehouseStatus}
            </if>
            <if test="dto.provinceId != null">
                AND province_id = #{dto.provinceId}
            </if>
            <if test="dto.cityId != null">
                AND city_id = #{dto.cityId}
            </if>
            <if test="dto.areaId != null">
                AND area_id = #{dto.areaId}
            </if>
            <if test="dto.postCode != null and dto.postCode != ''">
                AND post_code LIKE CONCAT('%', #{dto.postCode}, '%')
            </if>
            <if test="dto.principalName != null and dto.principalName != ''">
                AND principal_name LIKE CONCAT('%', #{dto.principalName}, '%')
            </if>
            <if test="dto.principalPhone != null and dto.principalPhone != ''">
                AND principal_phone LIKE CONCAT('%', #{dto.principalPhone}, '%')
            </if>
            <if test="dto.addr != null and dto.addr != ''">
                AND addr LIKE CONCAT('%', #{dto.addr}, '%')
            </if>
        </where>
        ORDER BY
            warehouse_id DESC
    </select>

    <select id="getById" resultMap="warehouseVOMap">
        SELECT
            <include refid="Vo_Column_List" />
        FROM
            warehouse
        WHERE
            warehouse_id = #{warehouseId}
    </select>
    <select id="getNearbyWarehouse" resultType="com.tmerclub.cloud.warehouse.model.vo.WarehouseVO">
        SELECT
            shop_id,
            warehouse_id,
            warehouse_name,
            warehouse_status,
            principal_name,
            principal_phone,
            province_id,
            province,
            city_id,
            city,
            area_id,
            area,
            post_code,
            addr,
            lng,
            lat,
            deleted,
            update_time,
            create_time,
            (6371 * ACOS(
                    COS(RADIANS(#{lat})) * COS(RADIANS(lat)) *
                    COS(RADIANS(lng) - RADIANS(#{lng})) +
                    SIN(RADIANS(#{lat})) * SIN(RADIANS(lat))
                    )) AS distance
        FROM
            warehouse
        ORDER BY
            distance
        LIMIT 1;
    </select>

    <insert id="save" useGeneratedKeys="true" keyProperty="warehouseId">
        INSERT INTO warehouse
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="shopId != null">shop_id,</if>
            <if test="warehouseName != null and warehouseName != ''">warehouse_name,</if>
            <if test="warehouseStatus != null">warehouse_status,</if>
            <if test="principalName != null and principalName != ''">principal_name,</if>
            <if test="principalPhone != null and principalPhone != ''">principal_phone,</if>
            <if test="provinceId != null">province_id,</if>
            <if test="province != null and province != ''">province,</if>
            <if test="cityId != null">city_id,</if>
            <if test="city != null and city != ''">city,</if>
            <if test="areaId != null">area_id,</if>
            <if test="area != null">area,</if>
            <if test="postCode != null">post_code,</if>
            <if test="addr != null and area != ''">addr,</if>
            <if test="lng != null">lng,</if>
            <if test="lat != null">lat,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="shopId != null">#{shopId},</if>
            <if test="warehouseName != null and warehouseName != ''">#{warehouseName},</if>
            <if test="warehouseStatus != null">#{warehouseStatus},</if>
            <if test="principalName != null and principalName != ''">#{principalName},</if>
            <if test="principalPhone != null and principalPhone != ''">#{principalPhone},</if>
            <if test="provinceId != null">#{provinceId},</if>
            <if test="province != null and province != ''">#{province},</if>
            <if test="cityId != null">#{cityId},</if>
            <if test="city != null and city != ''">#{city},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="area != null and area != ''">#{area},</if>
            <if test="postCode != null">#{postCode},</if>
            <if test="addr != null">#{addr},</if>
            <if test="lng != null">#{lng},</if>
            <if test="lat != null">#{lat},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="update">
        UPDATE warehouse
        <set>
            <if test="warehouseName != null and warehouseName != ''">
                warehouse_name = #{warehouseName},
            </if>
            <if test="warehouseStatus != null">
                warehouse_status = #{warehouseStatus},
            </if>
            <if test="principalName != null and principalName != ''">
                principal_name = #{principalName},
            </if>
            <if test="principalPhone != null and principalPhone != ''">
                principal_phone = #{principalPhone},
            </if>
            <if test="provinceId != null">
                province_id = #{provinceId},
            </if>
            <if test="province != null and province != ''">
                province = #{province},
            </if>
            <if test="cityId != null">
                city_id = #{cityId},
            </if>
            <if test="city != null and city != ''">
                city = #{city},
            </if>
            <if test="areaId != null">
                area_id = #{areaId},
            </if>
            <if test="area != null and area != ''">
                area = #{area},
            </if>
            <if test="postCode != null and postCode != ''">
                post_code = #{postCode},
            </if>
            <if test="addr != null and addr != ''">
                addr = #{addr},
            </if>
            <if test="lng != null">
                lng = #{lng},
            </if>
            <if test="lat != null">
                lat = #{lat},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
        </set>
        where warehouse_id = #{warehouseId}
    </update>
</mapper>