<?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.xinelu.manage.mapper.communityinfo.CommunityInfoMapper">

    <resultMap type="CommunityInfo" id="CommunityInfoResult">
        <result property="id" column="id"/>
        <result property="areaCode" column="area_code"/>
        <result property="communityCode" column="community_code"/>
        <result property="communityName" column="community_name"/>
        <result property="communityLongitude" column="community_longitude"/>
        <result property="communityLatitude" column="community_latitude"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectCommunityInfoVo">
        select id,
               area_code,
               community_code,
               community_name,
               community_longitude,
               community_latitude,
               create_by,
               create_time,
               update_by,
               update_time
        from community_info
    </sql>

    <select id="selectCommunityInfoListD" parameterType="CommunityInfo" resultMap="CommunityInfoResult">
        <include refid="selectCommunityInfoVo"/>
        <where>
            <if test="areaCode != null  and areaCode != ''">
                and area_code = #{areaCode}
            </if>
            <if test="communityCode != null  and communityCode != ''">
                and community_code = #{communityCode}
            </if>
            <if test="communityName != null  and communityName != ''">
                and community_name like concat('%', #{communityName}, '%')
            </if>
            <if test="communityLongitude != null  and communityLongitude != ''">
                and community_longitude = #{communityLongitude}
            </if>
            <if test="communityLatitude != null  and communityLatitude != ''">
                and community_latitude = #{communityLatitude}
            </if>
        </where>
    </select>

    <select id="selectCommunityInfoByIdD" parameterType="Long"
            resultMap="CommunityInfoResult">
        <include refid="selectCommunityInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertCommunityInfo" parameterType="CommunityInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into community_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null">area_code,
            </if>
            <if test="communityCode != null">community_code,
            </if>
            <if test="communityName != null">community_name,
            </if>
            <if test="communityLongitude != null">community_longitude,
            </if>
            <if test="communityLatitude != null">community_latitude,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null">#{areaCode},
            </if>
            <if test="communityCode != null">#{communityCode},
            </if>
            <if test="communityName != null">#{communityName},
            </if>
            <if test="communityLongitude != null">#{communityLongitude},
            </if>
            <if test="communityLatitude != null">#{communityLatitude},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateCommunityInfo" parameterType="CommunityInfo">
        update community_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null">area_code =
                #{areaCode},
            </if>
            <if test="communityCode != null">community_code =
                #{communityCode},
            </if>
            <if test="communityName != null">community_name =
                #{communityName},
            </if>
            <if test="communityLongitude != null">community_longitude =
                #{communityLongitude},
            </if>
            <if test="communityLatitude != null">community_latitude =
                #{communityLatitude},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCommunityInfoById" parameterType="Long">
        delete
        from community_info
        where id = #{id}
    </delete>

    <delete id="deleteCommunityInfoByIds" parameterType="String">
        delete from community_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectCommunityInfoList"
            resultType="com.xinelu.manage.vo.community.CommunityInfoVO">
        select
        ci.id,
        ci.area_code,
        ci.community_code,
        ci.community_name,
        ci.community_longitude,
        ci.community_latitude,
        ci.create_by,
        ci.create_time,
        ci.update_by,
        ci.update_time,
        sa.area_name
        from community_info ci
        INNER JOIN sys_area sa ON sa.area_code = ci.area_code
        <where>
            <if test="areaCode != null  and areaCode != ''">
                and ci.area_code = #{areaCode}
            </if>
            <if test="communityCode != null  and communityCode != ''">
                and ci.community_code = #{communityCode}
            </if>
            <if test="communityName != null  and communityName != ''">
                and ci.community_name like concat('%', #{communityName}, '%')
            </if>
            <if test="communityLongitude != null  and communityLongitude != ''">
                and ci.community_longitude = #{communityLongitude}
            </if>
            <if test="communityLatitude != null  and communityLatitude != ''">
                and ci.community_latitude = #{communityLatitude}
            </if>
            <if test="areaName!= null  and communityLatitude != ''">
                and sa.area_name like concat('%'#{areaName},'%')
            </if>
        </where>
        ORDER BY ci.create_time DESC
    </select>

    <select id="selectCommunityInfoById" parameterType="Long"
            resultType="com.xinelu.manage.vo.community.CommunityInfoVO">
        select ci.id,
               ci.area_code,
               ci.community_code,
               ci.community_name,
               ci.community_longitude,
               ci.community_latitude,
               ci.create_by,
               ci.create_time,
               ci.update_by,
               ci.update_time,
               sa.area_name
        from community_info ci
                 INNER JOIN sys_area sa ON sa.area_code = ci.area_code
        where ci.id = #{id}
    </select>

    <insert id="insertCommunityInfoList" parameterType="java.util.List">
        insert into community_info
        (
        area_code,
        community_code,
        community_name,
        community_longitude,
        community_latitude,
        create_by,
        create_time
        )
        VALUE
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.areaCode},
            #{item.communityCode},
            #{item.communityName},
            #{item.communityLongitude},
            #{item.communityLatitude},
            #{item.createBy},
            #{item.createTime}
            )
        </foreach>
    </insert>
</mapper>