<?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.zehong.system.mapper.TUserManageVillageMapper">
    
    <resultMap type="TUserManageVillage" id="TUserManageVillageResult">
        <result property="fVillageId"    column="f_village_id"    />
        <result property="fVillageName"    column="f_village_name"    />
        <result property="fRegion"    column="f_region"    />
        <result property="fTotalNumHouse"    column="f_total_num_house"    />
        <result property="fPipeGasUsers"    column="f_pipe_gas_users"    />
        <result property="fBottledGasUsers"    column="f_bottled_gas_users"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="remarks"    column="remarks"    />
        <result property="beyondEnterpriseId"    column="f_beyond_enterprise_id"    />
        <result property="beyondEnterpriseName"    column="enterprise_name"    />
    </resultMap>

    <resultMap type="TUserManageVillageThirdPartyVo" id="TUserManageVillageThirdPartyVoResult">
        <result property="fVillageId"    column="f_village_id"    />
        <result property="fVillageName"    column="f_village_name"    />
        <result property="fRegionName"    column="fRegionName"    />
        <result property="countyCode"    column="countyCode"    />
        <result property="fTotalNumHouse"    column="f_total_num_house"    />
        <result property="fPipeGasUsers"    column="f_pipe_gas_users"    />
        <result property="fBottledGasUsers"    column="f_bottled_gas_users"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="remarks"    column="remarks"    />
    </resultMap>

    <sql id="selectTUserManageVillageVo">
        select village.f_village_id,
               village.f_village_name,
               village.f_region,
               region.f_name as fRegionName,
               village.f_total_num_house,
               village.f_pipe_gas_users,
               village.f_bottled_gas_users,
               village.longitude,
               village.latitude,
               village.remarks,
               village.f_beyond_enterprise_id,
               enterprise.enterprise_name
        from t_user_manage_village village
        left join t_enterprise_info enterprise on village.f_beyond_enterprise_id = enterprise.enterprise_id
        left join t_county_level_region region on village.f_region = region.f_id
    </sql>

<!--    导出封装-->

    <resultMap type="TUserManageVillage" id="exportTUserManageVillageResult">
        <result property="fVillageId"    column="f_village_id"    />
        <result property="fVillageName"    column="f_village_name"    />
        <result property="fRegion"    column="f_region"    />
        <result property="fRegionName"    column="fRegionName"    />
        <result property="fTotalNumHouse"    column="f_total_num_house"    />
        <result property="fPipeGasUsers"    column="f_pipe_gas_users"    />
        <result property="fBottledGasUsers"    column="f_bottled_gas_users"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="remarks"    column="remarks"    />
        <result property="beyondEnterpriseId"    column="f_beyond_enterprise_id"    />
        <result property="beyondEnterpriseName"    column="beyondEnterpriseName"    />
    </resultMap>

    <sql id="exportTUserManageVillageVo">
        select village.f_village_id,
               village.f_village_name,
               village.f_region,
               region.f_name as fRegionName,
               village.f_beyond_enterprise_id,
               enterprise.enterprise_name as beyondEnterpriseName,
               village.f_total_num_house,
               village.f_pipe_gas_users,
               village.f_bottled_gas_users,
               village.longitude,
               village.latitude,
               village.remarks
        from t_user_manage_village village
        left join t_enterprise_info enterprise on village.f_beyond_enterprise_id = enterprise.enterprise_id
        left join t_county_level_region region on village.f_region = region.f_id
    </sql>
<!--    导出封装结束-->

    <select id="selectTUserManageVillageList" parameterType="TUserManageVillage" resultMap="TUserManageVillageResult">
        <include refid="selectTUserManageVillageVo"/>
        <where>  
            <if test="fVillageName != null  and fVillageName != ''"> and f_village_name like concat('%', #{fVillageName}, '%')</if>
            <if test="beyondEnterpriseId != null  and beyondEnterpriseId != '-2' and beyondEnterpriseId != ''"> and f_beyond_enterprise_id =  #{beyondEnterpriseId} </if>
        </where>
    </select>
    <select id="queryUserManageVillageForThirdParty" resultMap="TUserManageVillageThirdPartyVoResult">
        select village.f_village_id,
               village.f_village_name,
               region.f_name as fRegionName,
               region.f_county_code as countyCode,
               village.f_total_num_house,
               village.f_pipe_gas_users,
               village.f_bottled_gas_users,
               village.longitude,
               village.latitude,
               village.remarks
        from t_user_manage_village village
                 left join t_county_level_region region on village.f_region = region.f_id
    </select>

    <select id="exportUserManageVillageList" parameterType="TUserManageVillage" resultMap="exportTUserManageVillageResult">
        <include refid="exportTUserManageVillageVo"/>
        <where>
            <if test="fVillageName != null  and fVillageName != ''"> and village.f_village_name like concat('%', #{fVillageName}, '%')</if>
            <if test="beyondEnterpriseId != null  and beyondEnterpriseId != '-2' and beyondEnterpriseId != ''"> and f_beyond_enterprise_id =  #{beyondEnterpriseId} </if>
        </where>
    </select>
    <select id="queryByVillageNames" parameterType="list" resultMap="TUserManageVillageResult">
        <include refid="exportTUserManageVillageVo"/>
        where f_village_name in
        <foreach collection="list" close=")" separator="," open="(" item="item" index="i">
            #{item}
        </foreach>
    </select>

    <select id="queryByVillageIds" parameterType="list" resultMap="TUserManageVillageResult">
        <include refid="selectTUserManageVillageVo"></include>
        where village.f_village_id in
        <foreach collection="list" index="i" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>

    </select>

    <select id="selectTUserManageVillageById" parameterType="Long" resultMap="TUserManageVillageResult">
        <include refid="selectTUserManageVillageVo"/>
        where f_village_id = #{fVillageId}
    </select>

    <insert id="insertBatch" parameterType="list">
        insert into t_user_manage_village (f_village_name, f_region,f_total_num_house, f_pipe_gas_users, f_bottled_gas_users,
        longitude,latitude,remarks)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.fVillageName,jdbcType=VARCHAR}, #{item.fRegion}, #{item.fTotalNumHouse},
            #{item.fPipeGasUsers}, #{item.fBottledGasUsers},#{item.longitude},#{item.latitude},#{item.remarks}
            )
        </foreach>
    </insert>

    <insert id="insertTUserManageVillage" parameterType="TUserManageVillage" useGeneratedKeys="true" keyProperty="fVillageId">
        insert into t_user_manage_village
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fVillageName != null">f_village_name,</if>
            <if test="fRegion != null">f_region,</if>
            <if test="fTotalNumHouse != null">f_total_num_house,</if>
            <if test="fPipeGasUsers != null">f_pipe_gas_users,</if>
            <if test="fBottledGasUsers != null">f_bottled_gas_users,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="remarks != null">remarks,</if>
            <if test="beyondEnterpriseId != null">f_beyond_enterprise_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fVillageName != null">#{fVillageName},</if>
            <if test="fRegion != null">#{fRegion},</if>
            <if test="fTotalNumHouse != null">#{fTotalNumHouse},</if>
            <if test="fPipeGasUsers != null">#{fPipeGasUsers},</if>
            <if test="fBottledGasUsers != null">#{fBottledGasUsers},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="beyondEnterpriseId != null">#{beyondEnterpriseId},</if>
         </trim>
    </insert>

    <update id="updateTUserManageVillage" parameterType="TUserManageVillage">
        update t_user_manage_village
        <trim prefix="SET" suffixOverrides=",">
            <if test="fVillageName != null">f_village_name = #{fVillageName},</if>
            <if test="fRegion != null">f_region = #{fRegion},</if>
            <if test="fTotalNumHouse != null">f_total_num_house = #{fTotalNumHouse},</if>
            <if test="fPipeGasUsers != null">f_pipe_gas_users = #{fPipeGasUsers},</if>
            <if test="fBottledGasUsers != null">f_bottled_gas_users = #{fBottledGasUsers},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="beyondEnterpriseId != null">f_beyond_enterprise_id = #{beyondEnterpriseId},</if>
        </trim>
        where f_village_id = #{fVillageId}
    </update>

    <delete id="deleteTUserManageVillageById" parameterType="Long">
        delete from t_user_manage_village where f_village_id = #{fVillageId}
    </delete>

    <delete id="deleteTUserManageVillageByIds" parameterType="String">
        delete from t_user_manage_village where f_village_id in 
        <foreach item="fVillageId" collection="array" open="(" separator="," close=")">
            #{fVillageId}
        </foreach>
    </delete>
</mapper>