<?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.ruoyi.system.mapper.grid.GridHouseMapper">
  <resultMap id="BaseResultMap" type="com.ruoyi.system.domain.grid.GridHouse">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="wg_code" jdbcType="VARCHAR" property="wgCode" />
    <result column="fwlx_code" jdbcType="VARCHAR" property="fwlxCode" />
    <result column="house_name" jdbcType="VARCHAR" property="houseName" />
    <result column="fwzt_code" jdbcType="VARCHAR" property="fwztCode" />
    <result column="person_id_card" jdbcType="VARCHAR" property="personIdCard" />
    <result column="person_name" jdbcType="VARCHAR" property="personName" />
    <result column="person_phone" jdbcType="VARCHAR" property="personPhone" />
    <result column="house_address" jdbcType="VARCHAR" property="houseAddress" />
    <result column="create_time" jdbcType="VARCHAR" property="createTime" />
    <result column="update_time" jdbcType="VARCHAR" property="updateTime" />
    <result column="create_id" jdbcType="VARCHAR" property="createId" />
    <result column="update_id" jdbcType="VARCHAR" property="updateId" />
    <result column="is_valid" jdbcType="TINYINT" property="isValid" />
    <result column="longitude" jdbcType="VARCHAR" property="longitude" />
    <result column="latitude" jdbcType="VARCHAR" property="latitude" />
  </resultMap>
  <resultMap id="GridHouseExt" type="com.ruoyi.system.domain.grid.ext.GridHouseExt" extends="BaseResultMap">
    <result column="wgValue" jdbcType="VARCHAR" property="wgValue" />
    <result column="fwlxValue" jdbcType="VARCHAR" property="fwlxValue" />
    <result column="fwztValue" jdbcType="VARCHAR" property="fwztValue" />
    <result column="commonPeople" jdbcType="VARCHAR" property="commonPeople" />
    <result column="wgOtherCode" jdbcType="VARCHAR" property="wgOtherCode" />
    <result column="wgOtherCodeValue" jdbcType="VARCHAR" property="wgOtherCodeValue" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, wg_code, fwlx_code, house_name, fwzt_code, person_id_card, person_name, person_phone,
    house_address, create_time, update_time, create_id, update_id, is_valid,
    longitude, latitude
  </sql>
  <select id="selectByExample" parameterType="com.ruoyi.system.domain.grid.GridHouseExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from grid_house
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from grid_house
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from grid_house
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.ruoyi.system.domain.grid.GridHouseExample">
    delete from grid_house
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.ruoyi.system.domain.grid.GridHouse">
    insert into grid_house (id, wg_code, fwlx_code,
      house_name, fwzt_code, person_id_card,
      person_name, person_phone, house_address,
      create_time, update_time, create_id,
      update_id, is_valid,
      longitude, latitude)
    values (#{id,jdbcType=VARCHAR}, #{wgCode,jdbcType=VARCHAR}, #{fwlxCode,jdbcType=VARCHAR},
      #{houseName,jdbcType=VARCHAR}, #{fwztCode,jdbcType=VARCHAR}, #{personIdCard,jdbcType=VARCHAR},
      #{personName,jdbcType=VARCHAR}, #{personPhone,jdbcType=VARCHAR}, #{houseAddress,jdbcType=VARCHAR},
      #{createTime,jdbcType=VARCHAR}, #{updateTime,jdbcType=VARCHAR}, #{createId,jdbcType=VARCHAR},
      #{updateId,jdbcType=VARCHAR}, #{isValid,jdbcType=TINYINT},
      #{longitude,jdbcType=VARCHAR}, #{latitude,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.ruoyi.system.domain.grid.GridHouse" keyProperty="id" useGeneratedKeys="true">
    insert into grid_house
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="wgCode != null">
        wg_code,
      </if>
      <if test="fwlxCode != null">
        fwlx_code,
      </if>
      <if test="houseName != null">
        house_name,
      </if>
      <if test="fwztCode != null">
        fwzt_code,
      </if>
      <if test="personIdCard != null">
        person_id_card,
      </if>
      <if test="personName != null">
        person_name,
      </if>
      <if test="personPhone != null">
        person_phone,
      </if>
      <if test="houseAddress != null">
        house_address,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="createId != null">
        create_id,
      </if>
      <if test="updateId != null">
        update_id,
      </if>
      <if test="isValid != null">
        is_valid,
      </if>
      <if test="longitude != null">
        longitude,
      </if>
      <if test="latitude != null">
        latitude,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="wgCode != null">
        #{wgCode,jdbcType=VARCHAR},
      </if>
      <if test="fwlxCode != null">
        #{fwlxCode,jdbcType=VARCHAR},
      </if>
      <if test="houseName != null">
        #{houseName,jdbcType=VARCHAR},
      </if>
      <if test="fwztCode != null">
        #{fwztCode,jdbcType=VARCHAR},
      </if>
      <if test="personIdCard != null">
        #{personIdCard,jdbcType=VARCHAR},
      </if>
      <if test="personName != null">
        #{personName,jdbcType=VARCHAR},
      </if>
      <if test="personPhone != null">
        #{personPhone,jdbcType=VARCHAR},
      </if>
      <if test="houseAddress != null">
        #{houseAddress,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=VARCHAR},
      </if>
      <if test="createId != null">
        #{createId,jdbcType=VARCHAR},
      </if>
      <if test="updateId != null">
        #{updateId,jdbcType=VARCHAR},
      </if>
      <if test="isValid != null">
        #{isValid,jdbcType=TINYINT},
      </if>
      <if test="longitude != null">
        #{longitude,jdbcType=VARCHAR},
      </if>
      <if test="latitude != null">
        #{latitude,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.ruoyi.system.domain.grid.GridHouseExample" resultType="java.lang.Long">
    select count(*) from grid_house
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update grid_house
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=VARCHAR},
      </if>
      <if test="record.wgCode != null">
        wg_code = #{record.wgCode,jdbcType=VARCHAR},
      </if>
      <if test="record.fwlxCode != null">
        fwlx_code = #{record.fwlxCode,jdbcType=VARCHAR},
      </if>
      <if test="record.houseName != null">
        house_name = #{record.houseName,jdbcType=VARCHAR},
      </if>
      <if test="record.fwztCode != null">
        fwzt_code = #{record.fwztCode,jdbcType=VARCHAR},
      </if>
      <if test="record.personIdCard != null">
        person_id_card = #{record.personIdCard,jdbcType=VARCHAR},
      </if>
      <if test="record.personName != null">
        person_name = #{record.personName,jdbcType=VARCHAR},
      </if>
      <if test="record.personPhone != null">
        person_phone = #{record.personPhone,jdbcType=VARCHAR},
      </if>
      <if test="record.houseAddress != null">
        house_address = #{record.houseAddress,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=VARCHAR},
      </if>
      <if test="record.updateTime != null">
        update_time = #{record.updateTime,jdbcType=VARCHAR},
      </if>
      <if test="record.createId != null">
        create_id = #{record.createId,jdbcType=VARCHAR},
      </if>
      <if test="record.updateId != null">
        update_id = #{record.updateId,jdbcType=VARCHAR},
      </if>
      <if test="record.isValid != null">
        is_valid = #{record.isValid,jdbcType=TINYINT},
      </if>
      <if test="record.longitude != null">
        longitude = #{record.longitude,jdbcType=VARCHAR},
      </if>
      <if test="record.latitude != null">
        latitude = #{record.latitude,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update grid_house
    set id = #{record.id,jdbcType=VARCHAR},
      wg_code = #{record.wgCode,jdbcType=VARCHAR},
      fwlx_code = #{record.fwlxCode,jdbcType=VARCHAR},
      house_name = #{record.houseName,jdbcType=VARCHAR},
      fwzt_code = #{record.fwztCode,jdbcType=VARCHAR},
      person_id_card = #{record.personIdCard,jdbcType=VARCHAR},
      person_name = #{record.personName,jdbcType=VARCHAR},
      person_phone = #{record.personPhone,jdbcType=VARCHAR},
      house_address = #{record.houseAddress,jdbcType=VARCHAR},
      create_time = #{record.createTime,jdbcType=VARCHAR},
      update_time = #{record.updateTime,jdbcType=VARCHAR},
      create_id = #{record.createId,jdbcType=VARCHAR},
      update_id = #{record.updateId,jdbcType=VARCHAR},
      is_valid = #{record.isValid,jdbcType=TINYINT},
      longitude = #{record.longitude,jdbcType=VARCHAR},
      latitude = #{record.latitude,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.ruoyi.system.domain.grid.GridHouse">
    update grid_house
    <set>
      <if test="wgCode != null">
        wg_code = #{wgCode,jdbcType=VARCHAR},
      </if>
      <if test="fwlxCode != null">
        fwlx_code = #{fwlxCode,jdbcType=VARCHAR},
      </if>
      <if test="houseName != null">
        house_name = #{houseName,jdbcType=VARCHAR},
      </if>
      <if test="fwztCode != null">
        fwzt_code = #{fwztCode,jdbcType=VARCHAR},
      </if>
      <if test="personIdCard != null">
        person_id_card = #{personIdCard,jdbcType=VARCHAR},
      </if>
      <if test="personName != null">
        person_name = #{personName,jdbcType=VARCHAR},
      </if>
      <if test="personPhone != null">
        person_phone = #{personPhone,jdbcType=VARCHAR},
      </if>
      <if test="houseAddress != null">
        house_address = #{houseAddress,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=VARCHAR},
      </if>
      <if test="createId != null">
        create_id = #{createId,jdbcType=VARCHAR},
      </if>
      <if test="updateId != null">
        update_id = #{updateId,jdbcType=VARCHAR},
      </if>
      <if test="isValid != null">
        is_valid = #{isValid,jdbcType=TINYINT},
      </if>
      <if test="longitude != null">
        longitude = #{longitude,jdbcType=VARCHAR},
      </if>
      <if test="latitude != null">
        latitude = #{latitude,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.ruoyi.system.domain.grid.GridHouse">
    update grid_house
    set wg_code = #{wgCode,jdbcType=VARCHAR},
      fwlx_code = #{fwlxCode,jdbcType=VARCHAR},
      house_name = #{houseName,jdbcType=VARCHAR},
      fwzt_code = #{fwztCode,jdbcType=VARCHAR},
      person_id_card = #{personIdCard,jdbcType=VARCHAR},
      person_name = #{personName,jdbcType=VARCHAR},
      person_phone = #{personPhone,jdbcType=VARCHAR},
      house_address = #{houseAddress,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=VARCHAR},
      create_id = #{createId,jdbcType=VARCHAR},
      update_id = #{updateId,jdbcType=VARCHAR},
      is_valid = #{isValid,jdbcType=TINYINT},
      longitude = #{longitude,jdbcType=VARCHAR},
      latitude = #{latitude,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>

  <select id="countGridHouse" parameterType="java.lang.String" resultType="map">
    select a.name, count(b.id) as number from bs_common as a LEFT JOIN grid_house as b on a.code = b.fwlx_code and b.is_valid = 1
    <if test="wgCodeLikes != null and wgCodeLikes.size() > 0">
      and (
      <foreach collection="wgCodeLikes" item="wgCode" open="(" close=")" separator="or">
        b.wg_code like CONCAT(#{wgCode}, '%')
      </foreach>
      )
    </if>
    where a.parent_id = (select id from bs_common where code = 'fwlx') and a.is_valid = '1' GROUP BY a.code ORDER BY number desc, a.sort
  </select>

  <select id="countGridHouseByFwzt" parameterType="java.lang.String" resultType="map">
    select a.name, count(b.id) as number from bs_common as a LEFT JOIN grid_house as b on a.code = b.fwzt_code and b.is_valid = 1
    <if test="wgCodeLikes != null and wgCodeLikes.size() > 0">
      and (
      <foreach collection="wgCodeLikes" item="wgCode" open="(" close=")" separator="or">
        b.wg_code like CONCAT(#{wgCode}, '%')
      </foreach>
      )
    </if>
    where a.parent_id = (select id from bs_common where code = 'fwzt') and a.is_valid = '1' GROUP BY a.code ORDER BY number desc, a.sort
  </select>

  <select id="countGridHouseByGrid" parameterType="java.lang.String" resultType="map">
    select a.wg_name as name, count(b.id) as number from grid_region as a LEFT JOIN grid_house as b on b.wg_code like concat(a.wg_code, '%')
    and b.is_valid = 1
    where a.level = '2' and a.is_valid = '1' GROUP BY a.wg_code ORDER BY number desc, a.wg_code
  </select>

  <select id="pageHouse" resultMap="GridHouseExt" parameterType="com.ruoyi.system.domain.grid.vo.GridHouseSearchParam">
      select
        e.id,
        e.wg_code,
        e.fwlx_code,
        e.house_name,
        e.fwzt_code,
        e.person_id_card,
        e.person_name,
        e.person_phone,
        e.house_address,
        e.create_time,
        e.update_time,
        e.create_id,
        e.update_id,
        e.is_valid,
        e.longitude,
        e.latitude
        from grid_house e where e.is_valid = 1
      <if test="wsHouse.wgCode != null and wsHouse.wgCode != ''">
          and e.wg_code like concat(#{wsHouse.wgCode}, '%')
      </if>

      <if test="wsHouse.fwlxCode != null and wsHouse.fwlxCode != ''">
          and e.fwlx_code = #{wsHouse.fwlxCode}
      </if>

      <if test="wsHouse.fwztCode != null and wsHouse.fwztCode != ''">
          and e.fwzt_code = #{wsHouse.fwztCode}
      </if>

      <if test="wsHouse.houseName != null and wsHouse.houseName != ''">
          and e.house_name like concat('%', #{wsHouse.houseName}, '%')
      </if>

    <if test="wsHouse.wgCodeLikes != null and wsHouse.wgCodeLikes.size() > 0">
      and (
      <foreach collection="wsHouse.wgCodeLikes" item="wgCode" open="(" close=")" separator="or">
        e.wg_code like CONCAT(#{wgCode}, '%')
      </foreach>
      )
    </if>

      <if test="value != null and value != ''">
        and (e.person_name like  concat('%', #{value}, '%') or e.person_id_card like  concat('%', #{value}, '%') or e.house_name like  concat('%', #{value}, '%'))
      </if>
    order by e.create_time desc, e.house_name
  </select>
    <select id="countByParam" resultType="java.lang.Integer" parameterType="com.ruoyi.system.domain.grid.vo.GridHouseSearchParam">
      select count(1) from grid_house where is_valid = '1'
      <if test="wgCodeLikes != null and wgCodeLikes.size() > 0">
        and (
        <foreach collection="wgCodeLikes" item="wgCode" open="(" close=")" separator="or">
          wg_code like CONCAT(#{wgCode}, '%')
        </foreach>
        )
      </if>
    </select>

</mapper>
