<?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.gxwebsoft.shop.mapper.MerchantMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*
        <if test="param.lat != null">
            , ACOS(SIN(( #{param.lat} * 3.1415) / 180 ) * SIN((lat * 3.1415) / 180 ) +COS(( #{param.lat} * 3.1415) / 180
            ) * COS((lat * 3.1415) / 180 ) * COS(( #{param.lng} * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380 as
            distance
        </if>
        FROM shop_merchant a
        <where>
            <if test="param.merchantId != null">
                AND a.merchant_id = #{param.merchantId}
            </if>
            <if test="param.merchantCategoryId != null">
                AND a.merchant_category_id = #{param.merchantCategoryId}
            </if>
            <if test="param.merchantName != null">
                AND a.merchant_name LIKE CONCAT('%', #{param.merchantName}, '%')
            </if>
            <if test="param.merchantCode != null">
                AND a.merchant_code LIKE CONCAT('%', #{param.merchantCode}, '%')
            </if>
            <if test="param.type != null">
                AND a.type = #{param.type}
            </if>
            <if test="param.image != null">
                AND a.image LIKE CONCAT('%', #{param.image}, '%')
            </if>
            <if test="param.phone != null">
                AND a.phone LIKE CONCAT('%', #{param.phone}, '%')
            </if>
            <if test="param.realName != null">
                AND a.real_name LIKE CONCAT('%', #{param.realName}, '%')
            </if>
            <if test="param.shopType != null">
                AND a.shop_type LIKE CONCAT('%', #{param.shopType}, '%')
            </if>
            <if test="param.itemType != null">
                AND a.item_type LIKE CONCAT('%', #{param.itemType}, '%')
            </if>
            <if test="param.category != null">
                AND a.category LIKE CONCAT('%', #{param.category}, '%')
            </if>
            <if test="param.commission != null">
                AND a.commission = #{param.commission}
            </if>
            <if test="param.files != null">
                AND a.files LIKE CONCAT('%', #{param.files}, '%')
            </if>
            <if test="param.ownStore != null">
                AND a.own_store = #{param.ownStore}
            </if>
            <if test="param.recommend != null">
                AND a.recommend = #{param.recommend}
            </if>
            <if test="param.goodsReview != null">
                AND a.goods_review = #{param.goodsReview}
            </if>
            <if test="param.comments != null">
                AND a.comments LIKE CONCAT('%', #{param.comments}, '%')
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.status != null">
                AND a.status = #{param.status}
            </if>
            <if test="param.deleted != null">
                AND a.deleted = #{param.deleted}
            </if>
            <if test="param.deleted == null">
                AND a.deleted = 0
            </if>
            <if test="param.sortNumber != null">
                AND a.sort_number = #{param.sortNumber}
            </if>
            <if test="param.createTimeStart != null">
                AND a.create_time &gt;= #{param.createTimeStart}
            </if>
            <if test="param.createTimeEnd != null">
                AND a.create_time &lt;= #{param.createTimeEnd}
            </if>
            <if test="param.merchantIds != null">
                AND a.merchant_id IN
                <foreach collection="param.merchantIds" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param.merchantCategoryIds != null">
                AND a.merchant_category_id IN
                <foreach collection="param.merchantCategoryIds" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param.merchantCodes != null">
                AND a.merchant_code IN
                <foreach collection="param.merchantCodes" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param.keywords != null">
                AND (
                a.merchant_name LIKE CONCAT('%', #{param.keywords}, '%')
                OR a.phone LIKE CONCAT('%', #{param.keywords}, '%')
                OR a.real_name LIKE CONCAT('%', #{param.keywords}, '%')
                )
            </if>
        </where>
        <if test="param.orderBy != null &amp; param.orderBy == 'recommend'">
            ORDER BY a.recommend DESC, a.sort_number ASC, a.create_time DESC
        </if>
        <if test="param.orderBy != null &amp; param.lat != null &amp; param.orderBy == 'distance'">
            ORDER BY distance ASC
        </if>
    </sql>

    <!-- 分页查询 -->
    <select id="selectPageRel" resultType="com.gxwebsoft.shop.entity.Merchant">
        <include refid="selectSql"></include>
    </select>

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.shop.entity.Merchant">
        <include refid="selectSql"></include>
    </select>

</mapper>
