<?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="io.renren.modules.app.dao.FwxxDao">


	<select id="queryByPage" parameterType="io.renren.modules.app.entity.FwxxEntity"
			resultType="io.renren.modules.app.entity.FwxxEntity">

		select * from fwxx where 1=1

		<if test=" id!=null and id!='' ">
			and id like CONCAT(#{id},"%")
		</if>
		<if test=" cczh!=null and cczh!='' ">
			and cczh like CONCAT(#{cczh},"%")
		</if>
		<if test=" fwbm!=null and fwbm!='' ">
			and fwbm like CONCAT(#{fwbm},"%")
		</if>
		<if test=" dhbm!=null and dhbm!='' ">
			and dhbm like CONCAT(#{dhbm},"%")
		</if>
		<if test=" mphm!=null and mphm!='' ">
			and mphm like CONCAT(#{mphm},"%")
		</if>
		<if test=" lc!=null and lc!='' ">
			and lc like CONCAT(#{lc},"%")
		</if>
		<if test=" hx!=null and hx!='' ">
			and hx like CONCAT(#{hx},"%")
		</if>
		<if test=" fwxz!=null and fwxz!='' ">
			and fwxz like CONCAT(#{fwxz},"%")
		</if>
		<if test=" ssdj!=null and ssdj!='' ">
			and ssdj like CONCAT(#{ssdj},"%")
		</if>
		<if test=" mj!=null and mj!='' ">
			and mj like CONCAT(#{mj},"%")
		</if>
		<if test=" fwje!=null and fwje!='' ">
			and fwje like CONCAT(#{fwje},"%")
		</if>
		<if test=" gfrq!=null and gfrq!='' ">
			and gfrq like CONCAT(#{gfrq},"%")
		</if>
		<if test=" lfmj!=null and lfmj!='' ">
			and lfmj like CONCAT(#{lfmj},"%")
		</if>
		<if test=" lfdj!=null and lfdj!='' ">
			and lfdj like CONCAT(#{lfdj},"%")
		</if>
		<if test=" lfje!=null and lfje!='' ">
			and lfje like CONCAT(#{lfje},"%")
		</if>
		<if test=" zjk!=null and zjk!='' ">
			and zjk like CONCAT(#{zjk},"%")
		</if>
		<if test=" hxt!=null and hxt!='' ">
			and hxt like CONCAT(#{hxt},"%")
		</if>
		<if test=" xm!=null and xm!='' ">
			and xm like CONCAT(#{xm},"%")
		</if>
		<if test=" sfzh!=null and sfzh!='' ">
			and sfzh like CONCAT(sfzh,"%")
		</if>
		<if test=" lxdh!=null and lxdh!='' ">
			and lxdh like CONCAT(#{lxdh},"%")
		</if>
		<if test=" jjze!=null and jjze!='' ">
			and jjze like CONCAT(#{jjze},"%")
		</if>
		<if test=" grje!=null and grje!='' ">
			and grje like CONCAT(#{grje},"%")
		</if>
		<if test=" dwje!=null and dwje!='' ">
			and dwje like CONCAT(#{dwje},"%")
		</if>
		<if test=" dqye!=null and dqye!='' ">
			and dqye like CONCAT(#{dqye},"%")
		</if>
		<if test=" sfsc!=null and sfsc!='' ">
			and sfsc like CONCAT(#{sfsc},"%")
		</if>
		<if test=" mh!=null and mh!='' ">
			and mh like CONCAT(#{mh},"%")
		</if>
		<if test=" grjjl!=null and grjjl!='' ">
			and grjjl like CONCAT(#{grjjl},"%")
		</if>
		<if test=" jsbz!=null and jsbz!='' ">
			and jsbz like CONCAT(#{jsbz},"%")
		</if>
		<if test=" gzdbm!=null and gzdbm!='' ">
			and gzdbm like CONCAT(#{gzdbm},"%")
		</if>
		<if test=" ggsy!=null and ggsy!='' ">
			and ggsy like CONCAT(#{ggsy},"%")
		</if>
		<if test=" zqje!=null and zqje!='' ">
			and zqje like CONCAT(#{zqje},"%")
		</if>
		<if test=" ljlx!=null and ljlx!='' ">
			and ljlx like CONCAT(#{ljlx},"%")
		</if>
		<if test=" ncye!=null and ncye!='' ">
			and ncye like CONCAT(#{ncye},"%")
		</if>
		<if test=" xhje!=null and xhje!='' ">
			and xhje like CONCAT(#{xhje},"%")
		</if>
		<if test=" xhjx!=null and xhjx!='' ">
			and xhjx like CONCAT(#{xhjx},"%")
		</if>
		<if test=" htbh!=null and htbh!='' ">
			and htbh like CONCAT(#{htbh},"%")
		</if>
		<if test=" fpbh!=null and fpbh!='' ">
			and fpbh like CONCAT(#{fpbh},"%")
		</if>
		<if test=" zjlb!=null and zjlb!='' ">
			and zjlb like CONCAT(#{zjlb},"%")
		</if>
		<if test=" dz!=null and dz!='' ">
			and dz like CONCAT(#{dz},"%")
		</if>
		<if test=" bz!=null and bz!='' ">
			and bz like CONCAT(#{bz},"%")
		</if>
		<if test=" jcbzbm!=null and jcbzbm!='' ">
			and jcbzbm like CONCAT(#{jcbzbm},"%")
		</if>
		<if test=" fwyt!=null and fwyt!='' ">
			and fwyt like CONCAT(#{fwyt},"%")
		</if>
		<if test=" spzt!=null and spzt!='' ">
			and spzt like CONCAT(#{spzt},"%")
		</if>
		<if test=" yzkm!=null and yzkm!='' ">
			and yzkm like CONCAT(#{yzkm},"%")
		</if>
	</select>


	<!-- 新增 -->
	<insert id="insertFwxx" parameterType="io.renren.modules.app.entity.FwxxEntity">
		INSERT INTO
		fwxx(dhbm,cczh,fwbm,mphm,lc,hx,fwxz,mj,mh,bz,fwyt,yzkm,sfjq,jsbz)
		VALUES
		(#{dhbm},#{cczh},#{fwbm},#{mphm},#{lc},#{hx},#{fwxz},#{mj},#{fwdybm},#{bz},#{fwyt},#{yzkm},'0','0')

		<!-- INSERT INTO fwxx(dhbm,cczh,fwbm,mphm,lc,hx,fwxz,ssdj,mj,fwje,gfrq,lfmj,lfdj,lfje,zjk,hxt,xm,sfzh,lxdh,jjze,grje,dwje,dqye,sfsc,mh,grjjl,jsbz,gzdbm,ggsy,zqje,ljlx,ncye,xhje,xhjx,htbh,fpbh,zjlb,dz,bz,jcbzbm,fwyt,spzt,yzkm,yztk) 
			VALUES (#{dhbm},#{cczh},#{fwbm},#{mphm},#{lcs},#{hx},#{fwxz},#{ssdj},#{mj},#{fwje},#{gfrq},#{lfmj},#{lfdj},#{lfje},#{zjk},#{hxt},#{xm},#{sfzh},#{lxdh},#{jjze},#{grje},#{dwje},#{dqye},#{sfsc},#{fwdybm},#{grjjl},#{jsbz},#{gzdbm},#{ggsy},#{zqje},#{ljlx},#{ncye},#{xhje},#{xhjx},#{htbh},#{fpbh},#{zjlb},#{dz},#{bz},#{jcbzbm},#{fwyt},#{spzt},#{yzkm},#{yztk}) -->
	</insert>

	<insert id="PLINSERTFwxx" parameterType="io.renren.modules.app.entity.FwxxEntity">
		INSERT INTO
			fwxx(dhbm,cczh,fwbm,mphm,lc,hx,fwxz,ssdj,mj,fwje,gfrq,lfmj,lfdj,lfje,zjk,hxt,xm,sfzh,lxdh,jjze,grje,dwje,dqye,sfsc,mh,grjjl,jsbz,gzdbm,ggsy,zqje,ljlx,ncye,xhje,xhjx,htbh,fpbh,zjlb,dz,bz,jcbzbm,fwyt,spzt,yzkm)
		VALUES
			(#{dhbm},#{cczh},#{fwbm},#{mphm},#{lcs},#{hx},#{fwxz},#{ssdj},#{mj},#{fwje},#{gfrq},#{lfmj},#{lfdj},#{lfje},#{zjk},#{hxt},#{xm},#{sfzh},#{lxdh},#{jjze},#{grje},#{dwje},#{dqye},#{sfsc},#{fwdybm},#{grjjl},#{jsbz},#{gzdbm},#{ggsy},#{zqje},#{ljlx},#{ncye},#{xhje},#{xhjx},#{htbh},#{fpbh},#{zjlb},#{dz},#{bz},#{jcbzbm},#{fwyt},#{spzt},#{yzkm})
	</insert>

	<!-- 修改 -->
	<update id="updateFwxx" parameterType="io.renren.modules.app.entity.FwxxEntity">
		update fwxx set
						dhbm=#{dhbm},cczh=#{cczh},fwbm=#{fwbm},mphm=#{mphm},lc=#{lc},hx=#{hx},fwxz=#{fwxz},ssdj=#{ssdj},
						mj=#{mj},fwje=#{fwje},gfrq=#{gfrq},xm=#{xm},
						sfzh=#{sfzh},lxdh=#{lxdh},grjjl=#{grjjl},
						jsbz=#{jsbz},gzdbm='0001',
						htbh=#{htbh},fpbh=#{fpbh},zjlb=#{zjlb},dz=#{dz},bz=#{bz},fwyt=#{fwyt},dwjjl=#{dwjjl}
		where fwbm=#{fwbm}
	</update>

	<!-- 单个查询 -->
	<select id="selectByXxId" parameterType="io.renren.modules.app.entity.FwxxEntity"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		SELECT
		distinct
		f.dhbm,f.xm,z.dhmc,x.xmmc,s.dwmc,s.dwbm,f.cczh,f.fwbm,f.mphm,f.lc,f.hx,f.fwxz,d.value,f.ssdj,f.mj,f.fwje,f.gfrq,f.sfyj,f.lfmj,f.lfdj,f.lfje,f.zjk,f.hxt,f.xm,f.sfzh,f.lxdh,f.jjze,f.grje,f.dwje,f.dqye,f.sfsc,f.mh,f.grjjl,f.jsbz,f.gzdbm,f.ggsy,f.zqje,f.ljlx,f.ncye,f.xhje,f.xhjx,f.htbh,f.fpbh,f.zjlb,f.dz,f.bz,f.jcbzbm,f.fwyt,f.spzt,f.yzkm
		FROM fwxx f,zhxx z,xm x,xqxx q,sfdw s,sys_dict d WHERE f.dhbm=z.dhbm
		and f.fwxz=d.id and
		z.xmbm=x.xmbm and x.dwbm=s.dwbm
		<if test=" fwbm != null and fwbm != '' ">
			and fwbm = #{fwbm}
		</if>
	</select>

	<!-- 获取最新的房屋编码 -->
	<select id="querydhbm" resultType="string">
		SELECT fwbm FROM fwxx ORDER BY
			fwbm DESC LIMIT 1
	</select>

	<!-- 查询户数和楼层数 -->
	<select id="queryDyxx" parameterType="io.renren.modules.app.entity.FwxxEntity"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		SELECT a.DHBM,b.DHMC,a.FWDYBM,a.DYMC,a.HS,a.LCS
		FROM DYXX a left join
		ZHXX b on a.dhbm = b.dhbm WHERE 1=1
		<if test=" fwdybm != null and fwdybm != '' ">
			and FWDYBM = #{fwdybm}
		</if>
	</select>

	<!-- 新增小区集合 -->
	<select id="selectxq" parameterType="java.util.Map"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		select XQBM,XQMC from XQXX where XQBM in (select XQBM from
			XM where XMBM in
					 (select XMBM from ZHXX where DHBM in (select DHBM from
						 DYXX where
															   FWDYBM not in (select MH from FWXX))))
	</select>

	<!-- 新增项目集合 -->
	<select id="selectxm" parameterType="java.util.Map"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		select XMBM,XMMC from XM where 1=1
		<if test=" xqbm != null and xqbm != '' ">
			and XQBM = #{xqbm}
		</if>
		and XMBM in (select XMBM from ZHXX where DHBM in (select DHBM from
		DYXX where FWDYBM not in (select MH from FWXX)))
	</select>

	<!-- 新增栋号集合 -->
	<select id="selectlz" parameterType="java.util.Map"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		select DHBM,DHMC from ZHXX where 1=1
		<if test=" xmbm != null and xmbm != '' ">
			and XMBM = #{xmbm}
		</if>
		and DHBM in (select DHBM from DYXX where FWDYBM not in (select MH from
		FWXX))
	</select>

	<!-- 新增单元集合 -->
	<select id="selectdy" parameterType="java.util.Map"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		select fwdybm,DYMC,HS from DYXX where 1=1
		<if test=" dhbm != null and dhbm != '' ">
			and DHBM = #{dhbm}
		</if>
		and FWDYBM not in(select MH from FWXX) order by fwdybm
	</select>

	<!--户型 -->
	<select id="hx" parameterType="java.util.Map"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		SELECT id AS hx,VALUE AS hxmc FROM sys_dict WHERE TYPE
															  ='04'
	</select>

	<!-- 房屋性质 -->
	<select id="fmxz" parameterType="java.util.Map"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		SELECT id AS fwxz,VALUE AS fwxzmc FROM sys_dict WHERE TYPE
																  ='01'
	</select>

	<!-- 查询单元信息 -->
	<select id="queryFwjhDy" parameterType="io.renren.modules.app.entity.FwxxEntity"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		SELECT dymc,fwdybm FROM DYXX WHERE dhbm = #{dhbm}
	</select>

	<select id="queryByMhList" parameterType="string"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		SELECT * FROM fwxx WHERE 1 = 1
		<choose>
			<when test="mhList != null and mhList.size() > 0">
				and mh in
				<foreach collection="mhList" item="mh" open="(" separator=","  close=")">
					#{mh}
				</foreach>
			</when>
			<otherwise>
				and 1 = 2
			</otherwise>
		</choose>
	</select>

	<!-- 查询房屋信息 -->
	<select id="queryFwjhFw" parameterType="java.util.Map"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		SELECT mphm,fwbm,lc,sfyj,sfsc FROM fwxx WHERE mh =
													  #{fwdybm} AND lc =
																	#{lc}
	</select>

	<!-- 根据单元编码查询房屋楼层数集合 -->
	<select id="queryDylcs" parameterType="java.util.Map"
			resultType="java.lang.Integer">
		SELECT lc FROM fwxx WHERE mh = #{fwdybm} GROUP BY lc order
			by lc desc
	</select>

	<!-- 根据栋号获取信息 -->
	<select id="queryDhsj" parameterType="io.renren.modules.app.entity.FwxxEntity"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		select YZKM from ZHXX where dhbm=#{dhbm}
	</select>

	<select id="selectfwdjbh" parameterType="java.lang.String"
			resultType="io.renren.modules.app.entity.YwpzEntity">
		select djbh from ywpzmx where fwbm=#{id} and case
			LENGTH(djbh) when 20 then SUBSTR(djbh FROM 1 FOR 2)='03' when 17 then
				SUBSTR(djbh FROM 5 FOR 2)='03' else '' end
	</select>

	<select id="selectshzt" parameterType="java.lang.String"
			resultType="io.renren.modules.app.entity.YwpzEntity">
		select * from ywpz where djbh=#{id}
	</select>

	<select id="selectshztByFwbmList" parameterType="java.lang.String"
			resultType="io.renren.modules.app.entity.YwpzEntity">
		SELECT
		ywpz.*, ywpzmx.fwbm
		FROM
		ywpzmx ywpzmx
		JOIN ywpz ywpz ON ywpz.djbh = ywpzmx.djbh
		WHERE 1 = 1
		<if test="fwbmList != null and fwbmList.size() > 0">
			and ywpzmx.fwbm in
			<foreach collection="fwbmList" item="fwbm" open="(" separator="," close=")">
				#{fwbm}
			</foreach>
		</if>
		AND CASE LENGTH(ywpzmx.djbh)
		WHEN 20 THEN
		SUBSTR(ywpzmx.djbh FROM 1 FOR 2) = '03'
		WHEN 17 THEN
		SUBSTR(ywpzmx.djbh FROM 5 FOR 2) = '03'
		ELSE
		''
		END
		GROUP BY ywpz.djbh
	</select>

	<select id="selectmhxq" parameterType="java.lang.String"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		select xqbm,xqmc from xqxx where 1=1
		<if test=" _pt!=null and _pt!=''">
			and xqmc like '%${_pt}%' or xqbm like '%${_pt}%'
		</if>
	</select>

	<update id="updatezhxxhs" parameterType="io.renren.modules.app.entity.ZhxxEntity">
		update zhxx set hs=#{hs}
		where dhbm=#{dhbm}
	</update>

	<select id="selectzhxxhs" parameterType="java.lang.String"
			resultType="io.renren.modules.app.entity.ZhxxEntity">
		select hs from zhxx where dhbm=#{id}
	</select>

	<select id="selectfwxx" parameterType="java.lang.String"
			resultType="io.renren.modules.app.entity.FwxxEntity">
		select sfyj from fwxx where fwbm=#{id}
	</select>

	<select id="selectfwdjxx" parameterType="java.lang.String"
			resultType="io.renren.modules.app.entity.YwpzEntity">
		select y.djbh,y.djrq,h.yhmc,j.billname,j.billcode,y.je,y.zph,y.bz from
																			  ywpz y,h_jsfs j,yhxx h where y.bank_code=h.yhbm and j.billcode=y.jsfs
																									   and y.djbh in(select djbh from ywpzmx where (SUBSTR(djbh FROM 1 FOR
		2)='03' or SUBSTR(djbh FROM 5 FOR 2)='03') and fwbm=#{id}) order by y.djrq desc

	</select>

	<select id="selectfwsfjq" parameterType="java.lang.String" resultType="io.renren.modules.app.entity.YwpzmxEntity">
		select sum(je) je from ywpzmx where (SUBSTR(djbh FROM 5 FOR 2)='03' || SUBSTR(djbh FROM 1 FOR 2)='03') and fwbm=#{id}
	</select>

</mapper>