<?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="cn.com.tenlion.systembase.dao.keyplace.IKeyPlaceDao">

    <resultMap id="keyPlaceDTO" type="cn.com.tenlion.systembase.pojo.dtos.keyplace.KeyPlaceDTO">
        <result column="key_place_id" property="keyPlaceId"/>
        <result column="place_name" property="placeName"/>
        <result column="place_type" property="placeType"/>
        <result column="place_type_name" property="placeTypeName"/>
        <result column="detail_type" property="detailType"/>
        <result column="detail_type_name" property="detailTypeName"/>
        <result column="area_code" property="areaCode"/>
        <result column="area_name" property="areaName"/>
        <result column="address" property="address"/>
        <result column="link_man" property="linkMan"/>
        <result column="link_phone" property="linkPhone"/>
        <result column="phone" property="phone"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="grid" property="grid"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
        <result column="security_question" property="securityQuestion"/>
        <result column="security_question_name" property="securityQuestionName"/>
        <result column="security_question_code" property="securityQuestionCode"/>
    </resultMap>

    <resultMap id="keyPlaceBO" type="cn.com.tenlion.systembase.pojo.bos.keyplace.KeyPlaceBO">
        <result column="key_place_id" property="keyPlaceId"/>
        <result column="place_name" property="placeName"/>
        <result column="place_type" property="placeType"/>
        <result column="place_type_name" property="placeTypeName"/>
        <result column="detail_type" property="detailType"/>
        <result column="detail_type_name" property="detailTypeName"/>
        <result column="area_code" property="areaCode"/>
        <result column="area_name" property="areaName"/>
        <result column="address" property="address"/>
        <result column="link_man" property="linkMan"/>
        <result column="link_phone" property="linkPhone"/>
        <result column="phone" property="phone"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="keyPlacePO" type="cn.com.tenlion.systembase.pojo.pos.keyplace.KeyPlacePO">
        <result column="key_place_id" property="keyPlaceId"/>
        <result column="place_name" property="placeName"/>
        <result column="place_type" property="placeType"/>
        <result column="place_type_name" property="placeTypeName"/>
        <result column="detail_type" property="detailType"/>
        <result column="detail_type_name" property="detailTypeName"/>
        <result column="area_code" property="areaCode"/>
        <result column="area_name" property="areaName"/>
        <result column="address" property="address"/>
        <result column="link_man" property="linkMan"/>
        <result column="link_phone" property="linkPhone"/>
        <result column="phone" property="phone"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <!-- 新增重点场所 -->
    <insert id="save" parameterType="map">
        INSERT INTO data_key_place(
            key_place_id,
            place_name,
            place_type,
            place_type_name,
            detail_type,
            detail_type_name,
            area_code,
            area_name,
            address,
            link_man,
            link_phone,
            phone,
            longitude,
            latitude,
            grid,
            security_question,
            security_question_name,
            security_question_code,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{keyPlaceId},
            #{placeName},
            #{placeType},
            #{placeTypeName},
            #{detailType},
            #{detailTypeName},
            #{areaCode},
            #{areaName},
            #{address},
            #{linkMan},
            #{linkPhone},
            #{phone},
            #{longitude},
            #{latitude},
            #{grid},
            #{securityQuestion},
            #{securityQuestionName},
            #{securityQuestionCode},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除重点场所 -->
    <update id="remove" parameterType="map">
        UPDATE
            data_key_place
        SET
            gmt_modified = #{gmtModified},
            modifier = #{modifier},
            is_delete = 1
        WHERE
            key_place_id IN
        <foreach collection="keyPlaceIds" index="index" open="(" separator="," close=")">
            #{keyPlaceIds[${index}]}
        </foreach>
    </update>

    <!-- 删除重点场所（物理） -->
    <update id="delete" parameterType="map">
        DELETE FROM
            data_key_place
        WHERE
            key_place_id IN
        <foreach collection="keyPlaceIds" index="index" open="(" separator="," close=")">
            #{keyPlaceIds[${index}]}
        </foreach>
    </update>

    <!-- 修改重点场所 -->
    <update id="update" parameterType="map">
        UPDATE
            data_key_place
        SET
        <if test="placeName != null and placeName != ''">
            place_name = #{placeName},
        </if>
        <if test="placeType != null and placeType != ''">
            place_type = #{placeType},
        </if>
        <if test="placeTypeName != null and placeTypeName != ''">
            place_type_name = #{placeTypeName},
        </if>
        <if test="detailType != null and detailType != ''">
            detail_type = #{detailType},
        </if>
        <if test="detailTypeName != null and detailTypeName != ''">
            detail_type_name = #{detailTypeName},
        </if>
        <if test="areaCode != null and areaCode != ''">
            area_code = #{areaCode},
        </if>
        <if test="areaName != null and areaName != ''">
            area_name = #{areaName},
        </if>
        <if test="address != null and address != ''">
            address = #{address},
        </if>
        <if test="linkMan != null and linkMan != ''">
            link_man = #{linkMan},
        </if>
        <if test="linkPhone != null and linkPhone != ''">
            link_phone = #{linkPhone},
        </if>
        <if test="phone != null and phone != ''">
            phone = #{phone},
        </if>
        <if test="longitude != null and longitude != ''">
            longitude = #{longitude},
        </if>
        <if test="latitude != null and latitude != ''">
            latitude = #{latitude},
        </if>
        <if test="grid != null and grid != ''">
            grid = #{grid},
        </if>
        <if test="securityQuestion != null">
            security_question = #{securityQuestion},
        </if>
        <if test="securityQuestionName != null and securityQuestionName != ''">
            security_question_name = #{securityQuestionName},
        </if>
        <if test="securityQuestionCode != null and securityQuestionCode != ''">
            security_question_code = #{securityQuestionCode},
        </if>
            gmt_modified = #{gmtModified},
            modifier = #{modifier},
            key_place_id = key_place_id
        WHERE
            key_place_id = #{keyPlaceId}
    </update>

    <!-- 重点场所详情 -->
    <select id="get" parameterType="map" resultMap="keyPlaceDTO">
        SELECT
            t1.place_name,
            t1.place_type,
            t1.place_type_name,
            t1.detail_type,
            t1.detail_type_name,
            t1.area_code,
            t1.area_name,
            t1.address,
            t1.link_man,
            t1.link_phone,
            t1.phone,
            t1.longitude,
            t1.latitude,
            t1.grid,
            t1.security_question,
            t1.security_question_name,
            t1.security_question_code,
            t1.key_place_id
        FROM
            data_key_place t1
        WHERE
            t1.is_delete = 0
            <if test="keyPlaceId != null and keyPlaceId != ''">
        AND
            t1.key_place_id = #{keyPlaceId}
            </if>
    </select>

    <!-- 重点场所详情 -->
    <select id="getBO" parameterType="map" resultMap="keyPlaceBO">
        SELECT
            t1.key_place_id,
            t1.place_name,
            t1.place_type,
            t1.place_type_name,
            t1.detail_type,
            t1.detail_type_name,
            t1.area_code,
            t1.area_name,
            t1.address,
            t1.link_man,
            t1.link_phone,
            t1.phone,
            t1.longitude,
            t1.latitude,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            data_key_place t1
        WHERE
            t1.is_delete = 0
            <if test="keyPlaceId != null and keyPlaceId != ''">
        AND
            t1.key_place_id = #{keyPlaceId}
            </if>
    </select>

    <!-- 重点场所详情 -->
    <select id="getPO" parameterType="map" resultMap="keyPlacePO">
        SELECT
            t1.key_place_id,
            t1.place_name,
            t1.place_type,
            t1.place_type_name,
            t1.detail_type,
            t1.detail_type_name,
            t1.area_code,
            t1.area_name,
            t1.address,
            t1.link_man,
            t1.link_phone,
            t1.phone,
            t1.longitude,
            t1.latitude,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            data_key_place t1
        WHERE
            t1.is_delete = 0
            <if test="keyPlaceId != null and keyPlaceId != ''">
        AND
            t1.key_place_id = #{keyPlaceId}
            </if>
    </select>

    <!-- 重点场所列表 -->
    <select id="list" parameterType="map" resultMap="keyPlaceDTO">
        SELECT
            t1.key_place_id,
            t1.place_name,
            t1.place_type,
            t1.place_type_name,
            t1.detail_type,
            t1.detail_type_name,
            t1.area_code,
            t1.area_name,
            t1.address,
            t1.link_man,
            t1.link_phone,
            t1.phone,
            t1.longitude,
            t1.latitude,
            t1.grid,
            t1.security_question,
            t1.security_question_name,
            t1.security_question_code,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete,
            1
        FROM
            data_key_place t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.place_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="areaCode != null and areaCode != ''">
            AND t1.area_code = #{areaCode}
        </if>
        <if test="areaCodeLike != null and areaCodeLike != ''">
            AND t1.area_code LIKE CONCAT(#{areaCodeLike}, '%')
        </if>
        <if test="grid != null and grid != ''">
            AND t1.grid = #{grid}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="keyPlaceIds != null and keyPlaceIds.size > 0">
        AND
            t1.key_place_id IN
            <foreach collection="keyPlaceIds" index="index" open="(" separator="," close=")">
                #{keyPlaceIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 重点场所列表 -->
    <select id="listBO" parameterType="map" resultMap="keyPlaceBO">
        SELECT
            t1.key_place_id,
            t1.place_name,
            t1.place_type,
            t1.place_type_name,
            t1.detail_type,
            t1.detail_type_name,
            t1.area_code,
            t1.area_name,
            t1.address,
            t1.link_man,
            t1.link_phone,
            t1.phone,
            t1.longitude,
            t1.latitude,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            data_key_place t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="keyPlaceIds != null and keyPlaceIds.size > 0">
        AND
            t1.key_place_id IN
            <foreach collection="keyPlaceIds" index="index" open="(" separator="," close=")">
                #{keyPlaceIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 重点场所列表 -->
    <select id="listPO" parameterType="map" resultMap="keyPlacePO">
        SELECT
            t1.key_place_id,
            t1.place_name,
            t1.place_type,
            t1.place_type_name,
            t1.detail_type,
            t1.detail_type_name,
            t1.area_code,
            t1.area_name,
            t1.address,
            t1.link_man,
            t1.link_phone,
            t1.phone,
            t1.longitude,
            t1.latitude,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            data_key_place t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="keyPlaceIds != null and keyPlaceIds.size > 0">
        AND
            t1.key_place_id IN
            <foreach collection="keyPlaceIds" index="index" open="(" separator="," close=")">
                #{keyPlaceIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 重点场所统计  -->
    <select id="count" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            data_key_place t1
        WHERE
            t1.is_delete = 0
    </select>

</mapper>