<?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.community.huiju.dao.ToCommunitiesMapper" >
  <resultMap id="BaseResultMap" type="com.community.huiju.model.ToCommunities" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="community_name" property="communityName" jdbcType="VARCHAR" />
    <result column="community_alias" property="communityAlias" jdbcType="VARCHAR" />
    <result column="province_id" property="provinceId" jdbcType="INTEGER" />
    <result column="city_id" property="cityId" jdbcType="INTEGER" />
    <result column="district_id" property="districtId" jdbcType="INTEGER" />
    <result column="longitude" property="longitude" jdbcType="VARCHAR" />
    <result column="latitude" property="latitude" jdbcType="VARCHAR" />
    <result column="user_name" property="userName" jdbcType="VARCHAR" />
    <result column="login_name" property="loginName" jdbcType="VARCHAR" />
    <result column="create_user" property="createUser" jdbcType="INTEGER" />
    <result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
    <result column="update_user" property="updateUser" jdbcType="INTEGER" />
    <result column="update_date" property="updateDate" jdbcType="TIMESTAMP" />
    <result column="province" property="province" jdbcType="VARCHAR" />
    <result column="city" property="city" jdbcType="VARCHAR" />
    <result column="district" property="district" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, community_name, community_alias, province_id, city_id, district_id, longitude,
    latitude,user_name,login_name,create_user,create_date,update_user,update_date
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from to_communities
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from to_communities
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.community.huiju.model.ToCommunities" >
    insert into to_communities (id, community_name, community_alias, 
      province_id, city_id, district_id, 
      longitude, latitude)
    values (#{id,jdbcType=INTEGER}, #{communityName,jdbcType=VARCHAR}, #{communityAlias,jdbcType=VARCHAR}, 
      #{provinceId,jdbcType=INTEGER}, #{cityId,jdbcType=INTEGER}, #{districtId,jdbcType=INTEGER}, 
      #{longitude,jdbcType=VARCHAR}, #{latitude,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.community.huiju.model.ToCommunities" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
    insert into to_communities
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="communityName != null" >
        community_name,
      </if>
      <if test="communityAlias != null" >
        community_alias,
      </if>
      <if test="provinceId != null" >
        province_id,
      </if>
      <if test="cityId != null" >
        city_id,
      </if>
      <if test="districtId != null" >
        district_id,
      </if>
      <if test="longitude != null" >
        longitude,
      </if>
      <if test="latitude != null" >
        latitude,
      </if>
      <if test="userName != null" >
        user_name,
      </if>
      <if test="loginName != null" >
        login_name,
      </if>
      <if test="createUser != null" >
        create_user,
      </if>
      <if test="createDate != null" >
        create_date,
      </if>
      <if test="updateUser != null" >
        update_user,
      </if>
      <if test="updateDate != null" >
        update_date,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="communityName != null" >
        #{communityName,jdbcType=VARCHAR},
      </if>
      <if test="communityAlias != null" >
        #{communityAlias,jdbcType=VARCHAR},
      </if>
      <if test="provinceId != null" >
        #{provinceId,jdbcType=INTEGER},
      </if>
      <if test="cityId != null" >
        #{cityId,jdbcType=INTEGER},
      </if>
      <if test="districtId != null" >
        #{districtId,jdbcType=INTEGER},
      </if>
      <if test="longitude != null" >
        #{longitude,jdbcType=VARCHAR},
      </if>
      <if test="latitude != null" >
        #{latitude,jdbcType=VARCHAR},
      </if>
      <if test="userName != null" >
        #{userName,jdbcType=VARCHAR},
      </if>
      <if test="loginName != null" >
        #{loginName,jdbcType=VARCHAR},
      </if>
      <if test="createUser != null" >
        #{createUser,jdbcType=INTEGER},
      </if>
      <if test="createDate != null" >
        #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null" >
        #{updateUser,jdbcType=INTEGER},
      </if>
      <if test="updateDate != null" >
        #{updateDate,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.community.huiju.model.ToCommunities" >
    update to_communities
    <set >
      <if test="communityName != null" >
        community_name = #{communityName,jdbcType=VARCHAR},
      </if>
      <if test="communityAlias != null" >
        community_alias = #{communityAlias,jdbcType=VARCHAR},
      </if>
      <if test="provinceId != null" >
        province_id = #{provinceId,jdbcType=INTEGER},
      </if>
      <if test="cityId != null" >
        city_id = #{cityId,jdbcType=INTEGER},
      </if>
      <if test="districtId != null" >
        district_id = #{districtId,jdbcType=INTEGER},
      </if>
      <if test="longitude != null" >
        longitude = #{longitude,jdbcType=VARCHAR},
      </if>
      <if test="latitude != null" >
        latitude = #{latitude,jdbcType=VARCHAR},
      </if>
      <if test="userName != null" >
        user_name = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="loginName != null" >
        login_name = #{loginName,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.community.huiju.model.ToCommunities" >
    update to_communities
    set community_name = #{communityName,jdbcType=VARCHAR},
      community_alias = #{communityAlias,jdbcType=VARCHAR},
      province_id = #{provinceId,jdbcType=INTEGER},
      city_id = #{cityId,jdbcType=INTEGER},
      district_id = #{districtId,jdbcType=INTEGER},
      longitude = #{longitude,jdbcType=VARCHAR},
      latitude = #{latitude,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="selectByCommunityName" resultMap="BaseResultMap" parameterType="com.community.huiju.model.ToCommunities" >
    SELECT
      c.id,
      c.community_name,
      c.community_alias,
      c.province_id,
      c.city_id,
      c.district_id,
      c.longitude,
      c.latitude,
      c.user_name,
      c.login_name,
      c.create_user,
      c.create_date,
      c.update_user,
      c.update_date,
      n.province,
      a.city,
      t.district,
      (select count(*) from tp_user where community_id=c.id and status=1) as propertyNum,
      (select count(*) from ta_user_verify where community_id=c.id and role_id=1 and verify_status=1) as verifyOwnerNum,
      (select count(*) from ta_user_verify where community_id=c.id and role_id=3 and verify_status=1) as verifyRelationNum,
      (select count(*) from ta_user_verify where community_id=c.id and role_id=2 and verify_status=1) as verifyTenantNum
    FROM
      to_communities c
    LEFT JOIN sys_nation n ON c.province_id = n.id
    LEFT JOIN sys_nation a ON c.city_id = a.id
    LEFT JOIN sys_nation t ON c.district_id = t.id
    where 1=1
    <if test="communityName != null and communityName != ''" >
      and c.community_name like concat('%',#{communityName,jdbcType=VARCHAR},'%')
    </if>
    <if test="id != null and id != ''" >
      and c.id like concat('%',#{id,jdbcType=INTEGER},'%')
    </if>
    <if test="provinceId != null and provinceId != ''" >
      and c.province_id = #{provinceId,jdbcType=INTEGER}
    </if>
    <if test="cityId != null and cityId != ''" >
      and c.city_id = #{cityId,jdbcType=INTEGER}
    </if>
    <if test="districtId != null and districtId != ''" >
      and c.district_id = #{districtId,jdbcType=INTEGER}
    </if>
    order by c.create_date desc
  </select>

  <select id="selectLongitude" resultMap="BaseResultMap" >
    SELECT
    c.id,
    c.community_name,
    c.community_alias,
    c.province_id,
    c.city_id,
    c.district_id,
    c.longitude,
    c.latitude,
    c.user_name,
    c.login_name,
    c.create_user,
    c.create_date,
    c.update_user,
    c.update_date
    FROM
    to_communities c
  </select>

</mapper>