<?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.iformall.mapper.WxRentContractMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.WxRentContract">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="merchant_id" jdbcType="BIGINT" property="merchantId"/>
		<result column="price" jdbcType="INTEGER" property="price"/>
		<result column="rental_start_date" jdbcType="TIMESTAMP" property="rentalStartDate"/>
		<result column="rental_end_date" jdbcType="TIMESTAMP" property="rentalEndDate"/>
		<result column="sign_date" jdbcType="TIMESTAMP" property="signDate"/>
		<result column="receive_period" jdbcType="INTEGER" property="receivePeriod"/>
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
		<result column="filepath" jdbcType="VARCHAR" property="filepath"/>
		<result column="status" jdbcType="INTEGER" property="status"/>
		<result column="contract_number" jdbcType="VARCHAR" property="contractNumber"/>
		<result column="deposit" jdbcType="INTEGER" property="deposit"/>
		<result column="pay_date" jdbcType="INTEGER" property="payDate"/>
		<result column="is_del" jdbcType="INTEGER" property="isDel"/>
		<result column="merchant_name" jdbcType="VARCHAR" property="merchantName"/>
		<result column="brand" jdbcType="VARCHAR" property="brand"/>
		<result column="business_id" jdbcType="INTEGER" property="businessId"/>
		<result column="shop_type" jdbcType="INTEGER" property="shopType"/>
		<result column="shop_id" jdbcType="BIGINT" property="shopId"/>
		<result column="updatetime" jdbcType="TIMESTAMP" property="updatetime"/>
		<result column="createtime" jdbcType="TIMESTAMP" property="createtime"/>
		<result column="link_person" jdbcType="VARCHAR" property="linkPerson"/>
		<result column="link_phone" jdbcType="VARCHAR" property="linkPhone"/>
		<result column="pay_account" jdbcType="VARCHAR" property="payAccount"/>
		<result column="filename" jdbcType="VARCHAR" property="filename"/>
		<result column="lease" jdbcType="INTEGER" property="lease"/>
		<result column="type" jdbcType="INTEGER" property="type"/>
		<result column="revenue" jdbcType="INTEGER" property="revenue"/>
		<result column="adjust_ratio" jdbcType="VARCHAR" property="adjustRatio"/>
		<result column="adjust_period" jdbcType="INTEGER" property="adjustPeriod"/>
		<result column="pay_ratio" jdbcType="INTEGER" property="payRatio"/>
		<result column="start_date" jdbcType="TIMESTAMP" property="startDate"/>
		<result column="end_date" jdbcType="TIMESTAMP" property="endDate"/>
		<result column="apply_status" jdbcType="INTEGER" property="applyStatus"/>
		<result column="bank_name" jdbcType="VARCHAR" property="bankName"/>
		<result column="rent_shop_type" jdbcType="INTEGER" property="rentShopType"/>
		<result column="fix_start_date" property="fixStartDate"/>
		<result column="fix_end_date" property="fixEndDate"/>
		<result column="business_type" property="businessType"/>
		<result column="receive_period" property="receivePeriod"/>
		<result column="rent_info" jdbcType="VARCHAR" property="rentInfo"/>
		<result column="file_names" jdbcType="VARCHAR" property="fileNames"/>
		<result column="price_unit" property="priceUnit"/>
		<result column="rent_price" property="rentPrice"/>
		<result column="subject_name" property="subjectName"/>
		<result column="rent_start_type" property="rentStartType"/><result column="type" jdbcType="INTEGER" property="type"/>
		<result column="revenue" jdbcType="INTEGER" property="revenue"/>
		<result column="adjust_ratio" jdbcType="VARCHAR" property="adjustRatio"/>
		<result column="adjust_period" jdbcType="INTEGER" property="adjustPeriod"/>
		<result column="pay_ratio" jdbcType="INTEGER" property="payRatio"/>
		<result column="start_date" jdbcType="TIMESTAMP" property="startDate"/>
		<result column="end_date" jdbcType="TIMESTAMP" property="endDate"/>
		<result column="apply_status" jdbcType="INTEGER" property="applyStatus"/>
		<result column="bank_name" jdbcType="VARCHAR" property="bankName"/>
		<result column="rent_shop_type" jdbcType="INTEGER" property="rentShopType"/>
		<result column="fix_start_date" property="fixStartDate"/>
		<result column="fix_end_date" property="fixEndDate"/>
		<result column="business_type" property="businessType"/>
		<result column="receive_period" property="receivePeriod"/>
		<result column="rent_info" jdbcType="VARCHAR" property="rentInfo"/>
		<result column="file_names" jdbcType="VARCHAR" property="fileNames"/>
		<result column="price_unit" property="priceUnit"/>
		<result column="rent_price" property="rentPrice"/>
		<result column="subject_name" property="subjectName"/>
		<result column="rent_start_type" property="rentStartType"/>

		<result column="rent_input_way" property="rentInputWay"/>
		<result column="adjust_ratio_way" property="adjustRatioWay"/>
		<result column="operation_type" property="operationType"/>
		<result column="late_pay_ratio" property="latePayRatio"/>
		<result column="late_pay_day" property="latePayDay"/>
		<result column="bus_discount_ratio" property="busDiscountRatio"/>
		<result column="bus_discount_time" property="busDiscountTime"/>

	</resultMap>

	<sql id="allColumns">
		`id`,`merchant_id`,`price`,`rental_start_date`,`rental_end_date`,`sign_date`,`receive_period`,
		`tenant_id`,`filepath`,`status`,`contract_number`,`deposit`,`pay_date`,`is_del`,`merchant_name`,
		`brand`,`business_id`,`shop_type`,`shop_id`,`updatetime`,`createtime`,`link_person`,`link_phone`,
		`pay_account`,`filename`,`lease`,`type`,`revenue`,`adjust_ratio`,`adjust_period`,`pay_ratio`,
		`start_date`,`end_date`,
		`apply_status`,`bank_name`,`rent_shop_type`,`fix_start_date`,`fix_end_date`,
		`business_type`,`rent_info`, `file_names`,price_unit,rent_price,`subject_name`,rent_start_type,
		`rent_input_way`,`adjust_ratio_way`,`operation_type`,late_pay_ratio,late_pay_day,bus_discount_ratio,bus_discount_time
	</sql>


	<sql id="dynamicWhereConditions">
		where 1 = 1
		<if test=" null != id ">and `id` = #{id}</if>
		<if test=" null != merchantId ">and `merchant_id` = #{merchantId}</if>
		<if test=" null != price ">and `price` = #{price}</if>
		<if test=" null != rentalStartDate ">and `rental_start_date` = #{rentalStartDate}</if>
		<if test=" null != rentalEndDate ">and `rental_end_date` = #{rentalEndDate}</if>
		<if test=" null != signDate ">and `sign_date` = #{signDate}</if>
		<if test=" null != receivePeriod ">and `receive_period` = #{receivePeriod}</if>
		<if test=" null != tenantId ">and `tenant_id` = #{tenantId}</if>
		<if test=" null != filepath ">and `filepath` = #{filepath}</if>
		<if test=" null != status ">and `status` = #{status}</if>
		<if test=" null != contractNumber ">and `contract_number` = #{contractNumber}</if>
		<if test=" null != deposit ">and `deposit` = #{deposit}</if>
		<if test=" null != payDate ">and `pay_date` = #{payDate}</if>
		<if test=" null != isDel ">and `is_del` = #{isDel}</if>
		<if test=" null != merchantName ">and `merchant_name` = #{merchantName}</if>
		<if test=" null != brand ">and `brand` = #{brand}</if>
		<if test=" null != businessId ">and `business_id` = #{businessId}</if>
		<if test=" null != shopType ">and `shop_type` = #{shopType}</if>
		<if test=" null != shopId ">and `shop_id` = #{shopId}</if>
		<if test=" null != updatetime ">and `updatetime` = #{updatetime}</if>
		<if test=" null != createtime ">and `createtime` = #{createtime}</if>
		<if test=" null != linkPerson ">and `link_person` = #{linkPerson}</if>
		<if test=" null != linkPhone ">and `link_phone` = #{linkPhone}</if>
		<if test=" null != payAccount ">and `pay_account` = #{payAccount}</if>
		<if test=" null != filename ">and `filename` = #{filename}</if>
		<if test=" null != lease ">and `lease` = #{lease}</if>
		<if test=" null != type ">and `type` = #{type}</if>
		<if test=" null != startDate ">and `start_date` = #{startDate}</if>
		<if test=" null != endDate ">and `end_date` = #{endDate}</if>
		<if test=" null != rentShopType ">and `rent_shop_type` = #{rentShopType}</if>
		<if test=" null != operationType ">and `operation_type` = #{operationType}</if>

		<if test=" null != ids ">
			and id in
			<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
				#{idItem}
			</foreach>
		</if>
		<if test=" null != sortColumns">order by ${sortColumns}</if>
	</sql>

	<select id="findList" parameterType="com.iformall.domain.po.WxRentContract" resultMap="BaseResultMap">
		select
		<include refid="allColumns"/>
		from wx_rent_contract
		<include refid="dynamicWhereConditions"/>
	</select>

	<select id="queryRentContractData" resultType="hashmap" parameterType="com.iformall.domain.po.WxRentContract">
		select rc.id,rc.merchant_name merchantName,rc.rental_start_date rentalStartDate,rc.rental_end_date rentalEndDate,rc.`status`,
		rc.price,rc.contract_number contractNumber,s.shop_number shopNumber,f.floor_name floorName,b.building_name buildingName,
		rc.lease,rc.filepath,rc.filename,rc.pay_account payAccount,rc.sign_date signDate,rc.merchant_id merchantId,rc.type,
		rc.start_date startDate,rc.end_date endDate,br.`name` brandName,rc.apply_status applyStatus,rc.bank_name
		bankName,rc.rent_shop_type rentShopType,rc.fix_start_date fixStartDate,rc.fix_end_date fixEndDate
		,rc.business_type businessType,rc.adjust_period adjustPeriod,rc.rent_info rentInfo, rc.file_names
		fileNames,rc.price_unit,rc.subject_name subjectName,rc.`rent_input_way` rentInputWay,rc.`adjust_ratio_way`
		adjustRatioWay,rc.bus_discount_ratio,rc.bus_discount_time
		from wx_rent_contract rc
		left join wx_shop s on rc.shop_id=s.id
		left join wx_mall_floor f on s.floor=f.id
		left join wx_mall_building b on s.building=b.id
		left join wx_brand br on rc.brand=br.id
		<where>
			rc.tenant_id=#{tenantId}
			<if test=" null != merchantName and merchantName!=''">and rc.`merchant_name` like concat('%',#{merchantName},'%')</if>
			<if test=" null != shopNumber and shopNumber!='' and null != rentShopType and rentShopType==1">
				and json_extract(rc.rent_info,'$[*].shopNumber') like concat('%',#{shopNumber},'%')
			</if>
			<if test=" null != shopNumber and shopNumber!='' and null != rentShopType and rentShopType==2">
				and s.`shop_number` like concat('%',#{shopNumber},'%')
			</if>
			<if test=" null != status ">and rc.`status` = #{status}</if>
			<if test=" null != shopId">and rc.`shop_id` = #{shopId}</if>
			<if test=" null != type ">and rc.`type` = #{type}</if>
			<if test=" null != rentShopType ">and rc.`rent_shop_type` = #{rentShopType}</if>
			<if test=" null != applyStatus ">and rc.`apply_status` = #{applyStatus}</if>
			<if test=" null != building and ''!= building">and b.id = #{building}</if>
			<if test=" null != floor and ''!= floor">and f.id = #{floor}</if>
			<if test=" null != operationType ">and `operation_type` = #{operationType}</if>
			<if test=" null != businessForRule and '' != businessForRule ">
				and rc.business_id in (${businessForRule})
			</if>
			<if test="null != rentShopType and 1 == rentShopType and  null != floorForRule and '' != floorForRule ">
				and rc.`id` in
				<foreach collection="tempShop" index="idx" separator=" union all " open="(" close=")">
					select id from wx_rent_contract where tenant_id=#{tenantId} and
					json_unquote(json_extract(json_extract(rent_info,'$[*].shopId'),concat('$[',#{idx},']'))) in(select
					id from wx_shop where tenant_id=#{tenantId} and floor in (${floorForRule}))
				</foreach>
			</if>
			<if test="null != rentShopType and 2 == rentShopType and  null != floorForRule and '' != floorForRule ">
				and rc.shop_id in (select id from wx_shop where tenant_id=#{tenantId} and floor in (${floorForRule}))
			</if>
			<if test=" null != merchantId">and rc.`merchant_id` = #{merchantId}</if>
		
		</where>
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
		<if test=" null == sortColumns"> order by rc.createtime desc,rc.id desc </if>

	</select>

	<select id="queryRentContractWaitSignStatus" resultType="int" parameterType="com.iformall.domain.po.WxRentContract">
		select count(id) from wx_rent_contract where tenant_id=#{tenantId} and status=#{status}
		<if test=" null != rentShopType ">and rent_shop_type = #{rentShopType}</if>
	</select>

	<select id="queryRentContractEndSoonStatus" resultType="int" parameterType="com.iformall.domain.po.WxRentContract">
		select count(id) from wx_rent_contract where tenant_id=#{tenantId} and status=4
		<if test=" null != rentShopType ">and rent_shop_type = #{rentShopType}</if>
	</select>

	<select id="queryRentContractPaidStatus" resultType="int" parameterType="com.iformall.domain.po.WxRentContract">
		select count(id) from wx_rent_contract where tenant_id=#{tenantId} and status in (2,3,4)
		<if test=" null != rentShopType ">and rent_shop_type = #{rentShopType}</if>
	</select>

	<select id="queryRentContractEndStatus" resultType="int" parameterType="com.iformall.domain.po.WxRentContract">
		select count(id) from wx_rent_contract where tenant_id=#{tenantId} and status!=0 and status!=1 and status!=6 and
		status!=7
		and rental_end_date &lt;now()
		<if test=" null != rentShopType ">and rent_shop_type = #{rentShopType}</if>

	</select>

	<update id="updateRentContractEndSoonStatus" parameterType="com.iformall.domain.po.WxRentContract">
		update wx_rent_contract set status=#{status} where tenant_id=#{tenantId} and status not in(0,1,5,6,7,8) and DATEDIFF(rental_end_date,now()) &lt;= 30 and rental_end_date > now()
	</update>

	<update id="updateRentContractPaidStatus" parameterType="com.iformall.domain.po.WxRentContract">
		update wx_rent_contract set status=#{status} where tenant_id=#{tenantId} and status not in(0,1,5,6,7,8) and rental_start_date &lt;= now() and rental_end_date >= now()
	</update>

	<update id="updateRentContractEndStatus" parameterType="com.iformall.domain.po.WxRentContract">
		update wx_rent_contract set status=#{status} where tenant_id=#{tenantId}  and status not in(0,1,5,6,7,8) and rental_end_date &lt;now()
	</update>

	<update id="updateRentWaitSignStatus" parameterType="com.iformall.domain.po.WxRentContract">
		update wx_rent_contract set status=#{status} where tenant_id=#{tenantId} and status not in(0,5,6,7) and merchant_id is null
	</update>

	<update id="updateApplyStatus" parameterType="com.iformall.domain.po.WxRentContract">
		update wx_rent_contract
		set apply_status=#{applyStatus}
		<if test=" null != businessType ">,business_type=#{businessType}</if>
		<if test=" null != status ">,status=#{status}</if>
		where id=#{id}
	</update>

	<update id="updateStatus" parameterType="com.iformall.domain.po.WxRentContract">
		update wx_rent_contract set status=#{status} where id=#{id}
	</update>

	<update id="updateRentInvalidStatus" parameterType="com.iformall.domain.po.WxRentContract">
		update wx_rent_contract set status=#{status} where shop_id in (
		select s.shop_id from (SELECT shop_id FROM wx_rent_contract WHERE tenant_id = #{tenantId} and status in(2,3,4)
		)s)  and status in(0,1) and status!=7 and tenant_id=#{tenantId} and rent_shop_type=2
	</update>

	<select id="getRentContractList" parameterType="com.iformall.domain.po.WxRentContract" resultType="com.iformall.domain.po.WxRentContract">
		select s.shop_id,s.shop_number,s.building_name building, s.floor_name floor,s.build_area,rc.* from (
		select s.id shop_id,ms.merchant_id,s.shop_number,b.building_name,f.floor_name,s.build_area,s.floor,s.building
		from wx_shop s
		left join wx_mall_building b  on s.building=b.id
		left join wx_mall_floor f on s.floor=f.id
		left join wx_merchant_shop ms on s.id=ms.shop_id
		where ms.is_del=0) s
		inner join (select * from wx_rent_contract where status in(2,3,4)) rc on s.merchant_id=rc.merchant_id
		where rc.tenant_id=#{tenantId}
		<if test=" null != shopNumber and ''!=shopNumber">
			and s.shop_number like concat('%', #{shopNumber},'%')
		</if>
		<if test=" null != floor and ''!=floor">
			and s.floor = #{floor}
		</if>
		<if test=" null != building and ''!=building">
			and s.building = #{building}

		</if>
		order by rc.id desc
	</select>

	<select id="updateStatusForUnsign" parameterType="com.iformall.domain.po.WxRentContract">
		update wx_rent_contract set status=1 where tenant_id=#{tenantId} and status=2 and merchant_id is null
	</select>

	<select id="getRentInvalidList" parameterType="com.iformall.domain.po.WxRentContract"
			resultType="com.iformall.domain.po.WxRentContract">
		select id from  wx_rent_contract where shop_id in (
		select s.shop_id from (SELECT shop_id FROM wx_rent_contract WHERE tenant_id = #{tenantId} and status in(2,3,4)
		)s)  and status in(0,1) and status!=7 and tenant_id=#{tenantId} and apply_status=1
	</select>

	<select id="queryOweCount" parameterType="com.iformall.domain.po.WxRentContract" resultType="Long">
		select count(*) from wx_rent_contract r,wx_bill_rent b
		where b.rent_contract_id = r.id and b.status = 1 and r.id = #{id}
	</select>

	<select id="getMerchants" parameterType="com.iformall.domain.po.WxRentContract"
			resultType="com.iformall.domain.po.WxRentContract">
		select * from wx_rent_contract where status in(2,3,4)
		and merchant_id not in(select merchant_id from wx_property_contract where status in(2,3,4))
		<if test=" null != rentShopType ">and rent_shop_type = #{rentShopType}</if>
	</select>

	<select id="selectRentContractCountByShopId" parameterType="com.iformall.domain.po.WxRentContract" resultType="int">
		select count(*) from wx_rent_contract where tenant_id=#{tenantId} and json_contains(json_extract(rent_info,'$[*].shopId'),concat('"',${shopId},'"')) and status in (2,3,4)
		<if test="id!=null">
			and id!=#{id}
		</if>
	</select>

	<select id="selectRentContractByShopId" parameterType="com.iformall.domain.po.WxRentContract"
			resultType="com.iformall.domain.po.WxRentContract">
		select * from wx_rent_contract where tenant_id=#{tenantId} and
		json_contains(json_extract(rent_info,'$[*].shopId'),concat('"',${shopId},'"')) and rent_shop_type =1
		<if test=" null != status ">and status=#{status} and status!=7</if>
	</select>

	<update id="updateInvalidContract" parameterType="com.iformall.domain.po.WxRentContract">
		update wx_rent_contract set status=#{status} where tenant_id=#{tenantId} and  id in(select r.id from (
		select id from wx_rent_contract where json_contains(json_extract(rent_info,'$[*].shopId'),concat('"',${shopId},'"'))
		and rent_shop_type=1 and status in(0,1) and status!=7)r)
	</update>

	<select id="selectRentContractByMerchantId" parameterType="com.iformall.domain.po.WxRentContract"
			resultType="com.iformall.domain.po.WxRentContract">
		select * from wx_rent_contract
		where status in(2,3,4)
		and merchant_id = #{merchantId}
	</select>

	<select id="hasContractNumber" parameterType="com.iformall.domain.po.WxRentContract"
			resultType="int">
		select count(*) from (
		select * from wx_rent_contract where tenant_id=#{tenantId}
		and contract_number=#{contractNumber}
		and status not in(5,6,7)
		union
		select * from wx_rent_contract where tenant_id=#{tenantId}
		and contract_number=#{contractNumber}
		and apply_status in(1,2)
		) r
		<where>
			<if test="id!=null and id!=''">
				r.id!=#{id}
			</if>
		</where>
	</select>

	<select id="getShopType" parameterType="com.iformall.domain.po.WxRentContract" resultType="int">
		select s.type from wx_rent_contract r left join wx_shop s on(s.id = r.`shop_id`)
		where r.id = #{id}
	</select>

	<resultMap id="contractMap" type="com.iformall.domain.vo.WxRentPropertyContractVo">
		<id column="id" property="id"/>
		<result column="contract_number" property="contractNumber"/>
		<result column="shop_number" property="shopNumber"/>
		<result column="merchant_name" property="merchantName"/>
		<result column="rent_info" property="rentInfo"/>
		<result column="rental_start_date" property="rentalStartDate"/>
		<result column="rental_end_date" property="rentalEndDate"/>
		<result column="rent_price" property="rentPrice"/>
		<result column="property_price" property="propertyPrice"/>
		<result column="type" property="type"/>
		<result column="status" property="status"/>
		<result column="apply_status" property="applyStatus"/>
		<result column="operation_type" property="operationType"/>
		<result column="rent_shop_type" property="rentShopType"/>
		<result column="merchant_id" property="merchantId"/>
		<result column="property_status" property="propertyStatus"/>
		<result column="rent_price_unit" property="rentPriceUnit"/>
		<result column="property_price_unit" property="propertyPriceUnit"/>
		<result column="bus_discount_time" property="busDiscountTime"/>
		<result column="bus_discount_ratio" property="busDiscountRatio"/>
	</resultMap>

	<select id="listContractVo" parameterType="com.iformall.domain.vo.WxRentPropertyContractVo" resultMap="contractMap">
		select rc.id,rc.merchant_name,rc.rental_start_date,rc.rental_end_date,rc.`status`,
		rc.price as rent_price,rc.contract_number,s.shop_number,rc.type,rc.apply_status,
		rc.rent_shop_type,rc.rent_info,pc.price as property_price,rc.merchant_id,pc.`status` as property_status,
		rc.price_unit as rent_price_unit,pc.price_unit as property_price_unit,
		rc.business_type businessType,rc.bus_discount_ratio,rc.bus_discount_time
		from wx_rent_contract rc left join wx_property_contract pc on rc.id=pc.rent_contract_id
		left join wx_shop s on rc.shop_id=s.id
		<where>
			rc.tenant_id=#{tenantId}
			<if test=" null != merchantName and merchantName!=''">and rc.`merchant_name` like
				concat('%',#{merchantName},'%')
			</if>
			<if test=" null != shopNumber and shopNumber!='' and null != rentShopType and rentShopType==1">
				and json_extract(rc.rent_info,'$[*].shopNumber') like concat('%',#{shopNumber},'%')
			</if>
			<if test=" null != shopNumber and shopNumber!='' and null != rentShopType and rentShopType==2">
				and s.`shop_number` like concat('%',#{shopNumber},'%')

			</if>
			<if test=" null != status ">and rc.`status` = #{status}</if>
			<if test=" null != type ">and rc.`type` = #{type}</if>
			<if test=" null != rentShopType ">
				and rc.`rent_shop_type` = #{rentShopType}
			</if>
			<if test=" null != applyStatus ">and rc.`apply_status` = #{applyStatus}</if>
			<if test=" null != operationType ">and rc.`operation_type` = #{operationType}</if>
			<if test=" null != businessForRule and '' != businessForRule ">
				and rc.business_id in (${businessForRule})
			</if>
			
			<if test="null != rentShopType and 1 == rentShopType and  null != floorForRule and '' != floorForRule ">
				and rc.`id` in
				<foreach collection="tempShop" index="idx" separator=" union all " open="(" close=")">
					select id from wx_rent_contract where tenant_id=#{tenantId} and
					json_unquote(json_extract(json_extract(rent_info,'$[*].shopId'),concat('$[',#{idx},']'))) in(select
					id from wx_shop where tenant_id=#{tenantId} and floor in (${floorForRule}))
				</foreach>
			</if>
			<if test="null != rentShopType and 2 == rentShopType and  null != floorForRule and '' != floorForRule ">
				and rc.shop_id in (select id from wx_shop where tenant_id=#{tenantId} and floor in (${floorForRule}))
			</if>
		
		</where>
		<if test=" null != sortColumns">order by ${sortColumns}</if>
		<if test=" null == sortColumns">order by rc.createtime desc,rc.id desc</if>
	</select>

	<select id="getByBill" parameterType="com.iformall.domain.po.WxBillRent" resultMap="BaseResultMap">
		select * from wx_rent_contract where id in(
		select rent_contract_id from wx_bill_rent id = #{id}
		)
	</select>
	
	<select id="getShopCountMax" resultType="int">
		select max(json_length(rent_info)) from wx_rent_contract
	</select>

	<select id="queryContractByBus" resultType="hashmap" parameterType="com.iformall.domain.po.WxBusiness">
		select 1 type,c.`rental_start_date`,c.`rental_end_date`,c.id id from wx_merchant m left join wx_rent_contract c on(m.id = c.merchant_id)
		where m.business_id = #{id} and c.price is not null and c.`rental_start_date` is not null and c.`rental_end_date` is not null
		union all
		select 2 type,c.`rental_start_date`,c.`rental_end_date`,c.id id from wx_merchant m left join wx_property_contract c on(m.id = c.merchant_id)
		where m.business_id = #{id} and c.price is not null and c.`rental_start_date` is not null and c.`rental_end_date` is not null
	</select>
	<select id="selectRentContractByShopIds" resultType="com.iformall.domain.po.WxRentContract">
		SELECT rc.id,rc.rental_start_date,rc.rental_end_date,rc.`shop_id`,rc.`rent_shop_type`,rc.`rent_info` FROM
		`wx_rent_contract` rc
		WHERE tenant_id = #{tenantId}
		AND rc.`status` IN (2,3,4) AND status!=7
		AND (	(rc.`rent_shop_type`=2 AND rc.`shop_id` IN
				<foreach collection="shopIds" index="index" item="idItem" open="(" separator="," close=")">
					#{idItem}
				</foreach>
		    )
			OR (rc.`rent_shop_type`=1 AND rc.`rent_info` REGEXP #{shopIdsRegexp})
		)
	</select>

	<select id="selectContractCountByShopId" parameterType="com.iformall.domain.po.WxRentContract" resultType="int">
		SELECT count(*)
		FROM wx_rent_contract
		WHERE tenant_id = #{tenantId}
		  AND `status` = #{status} AND status!=7
		  AND (
					`rent_shop_type` = 1 AND
					json_contains(json_extract(rent_info, '$[*].shopId'), concat('"', #{shopId}, '"'))
				OR
					`rent_shop_type` = 2 AND `shop_id` = #{shopId}
			)
	</select>
</mapper>

