<?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.liquidnet.service.kylin.mapper.KylinFieldsMapper">

    <resultMap id="FieldMisListResult" type="com.liquidnet.service.kylin.dao.FieldMisListDao">
        <result column="fields_id" property="fieldsId"/>
        <result column="name" property="name"/>
        <result column="cover_img" property="coverImg"/>
        <result column="city_name" property="cityName"/>
        <result column="address" property="address"/>
        <result column="province_name" property="provinceName"/>
        <result column="district_name" property="districtName"/>
        <result column="valid_status" property="validStatus"/>
        <result column="change_status" property="changeStatus"/>
        <result column="is_display" property="isDisplay"/>
        <result column="is_business" property="isBusiness"/>
    </resultMap>

    <resultMap id="FieldMisDetailsResult" type="com.liquidnet.service.kylin.dao.FieldMisDetailsDao">
        <result column="fields_id" property="fieldsId"/>
        <result column="name" property="name"/>
        <result column="name_eng" property="nameEng"/>
        <result column="cover_img" property="coverImg"/>
        <result column="latitude" property="latitude"/>
        <result column="longitude" property="longitude"/>
        <result column="address" property="address"/>
        <result column="phone" property="phone"/>
        <result column="refuse_message" property="refuseMessage"/>
        <result column="city_name" property="cityName"/>
        <result column="province_name" property="provinceName"/>
        <result column="district_name" property="districtName"/>
        <result column="city_id" property="cityId"/>
        <result column="province_id" property="provinceId"/>
        <result column="district_id" property="districtId"/>
        <result column="valid_status" property="validStatus"/>
        <result column="change_status" property="changeStatus"/>
        <result column="is_display" property="isDisplay"/>
        <result column="is_business" property="isBusiness"/>
    </resultMap>

    <!-- 列表封装 -->
    <sql id="Base_Column_List">
		f.fields_id,
		f.name,
		f.cover_img,
		f.address,
		f.city_name,
		f.province_name,
		f.district_name,
		fs.valid_status,
		fs.change_status,
		fs.is_display,
        fs.is_business
	</sql>

    <!-- 详情封装 -->
    <sql id="Base_Column_Details">
		f.fields_id,
		f.name,
		f.name_eng,
		f.cover_img,
		f.latitude,
		f.longitude,
		f.address,
		f.city_id,
		f.city_name,
		f.province_id,
		f.province_name,
		f.district_id,
		f.district_name,
		f.phone,
		f.refuse_message,
		fs.valid_status,
		fs.change_status,
		fs.is_display,
        fs.is_business
	</sql>

    <!-- mis场地列表 -->
    <select id="misSearchFields" parameterType="java.util.Map" resultMap="FieldMisDetailsResult">
        SELECT
        <include refid="Base_Column_List"/>
        FROM kylin_fields as f
        LEFT JOIN kylin_field_status as fs ON f.fields_id=fs.field_id
        <where>
            1 = 1
            <if test="name != ''">
                AND f.name LIKE concat('%', #{name}, '%')
            </if>
            <if test="provinceId != 0">
                AND f.province_id=#{provinceId}
            </if>
            <if test="cityId != 0">
                AND f.city_id=#{cityId}
            </if>
            <if test="districtId != 0">
                AND f.district_id=#{districtId}
            </if>
            <if test="validStatus != -1">
                AND fs.valid_status=#{validStatus}
            </if>
            <if test="isDisplay != -1">
                AND fs.is_display=#{isDisplay}
            </if>
            <if test="isBusiness != -1">
                AND fs.is_business=#{isBusiness}
            </if>
            <if test="changeStatus != -1">
                AND fs.change_status=#{changeStatus}
            </if>
        </where>
        <if test="orderType == 1">
            ORDER BY f.created_at
        </if>
        <if test="orderType == 2">
            ORDER BY f.updated_at
        </if>
        LIMIT #{page},#{size}
    </select>

    <!-- mis场地列表数量 -->
    <select id="misSearchFieldsCount" parameterType="java.util.Map" resultType="long">
        SELECT COUNT(0)
        FROM kylin_fields as f
        LEFT JOIN kylin_field_status as fs ON f.fields_id=fs.field_id
        <where>
            1 = 1
            <if test="name != ''">
                AND f.name LIKE concat('%', #{name}, '%')
            </if>
            <if test="provinceId != 0">
                AND f.province_id=#{provinceId}
            </if>
            <if test="cityId != 0">
                AND f.city_id=#{cityId}
            </if>
            <if test="districtId != 0">
                AND f.district_id=#{districtId}
            </if>
            <if test="validStatus != -1">
                AND fs.valid_status=#{validStatus}
            </if>
            <if test="isDisplay != -1">
                AND fs.is_display=#{isDisplay}
            </if>
            <if test="isBusiness != -1">
                AND fs.is_business=#{isBusiness}
            </if>
            <if test="changeStatus != -1">
                AND fs.change_status=#{changeStatus}
            </if>
        </where>
        <if test="orderType == 1">
            ORDER BY f.created_at
        </if>
    </select>

    <!-- mis场地详情 -->
    <select id="misFieldsDetails" parameterType="java.lang.String" resultMap="FieldMisDetailsResult">
        SELECT
        <include refid="Base_Column_Details"/>
        FROM kylin_fields as f
        LEFT JOIN kylin_field_status as fs ON f.fields_id=fs.field_id
        <where>
            f.fields_id=#{fieldsId}
        </where>
    </select>
</mapper>
