<?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.WxBillAllMapper">


	<resultMap id="allVoResultMap" type="com.iformall.domain.vo.WxBillAllVo">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="tenantId" jdbcType="VARCHAR" property="tenantId" />
		<result column="merchantId" jdbcType="INTEGER" property="merchantId" />
		<result column="shopId" jdbcType="INTEGER" property="shopId" />

		<result column="billTypeValue" jdbcType="INTEGER" property="billTypeValue"/>
		<result column="billType" jdbcType="VARCHAR" property="billType"/>
		<result column="needPay" jdbcType="BIGINT" property="needPay"/>

		<result column="merchantName" jdbcType="VARCHAR" property="merchantName" />
		<result column="rentShopType" jdbcType="INTEGER" property="rentShopType"/>
		<result column="receiveDate" jdbcType="TIMESTAMP" property="receiveDate" />
		<result column="expiredDay" jdbcType="INTEGER" property="expiredDay" />
		<result column="status" jdbcType="INTEGER" property="status" />
		
		
		<result column="receivePay" jdbcType="BIGINT" property="receivePay"/>
		<result column="owe" jdbcType="BIGINT" property="owe"/>
		<result column="pay" jdbcType="BIGINT" property="pay"/>
		<result column="payDate" jdbcType="TIMESTAMP" property="payDate" />
		<result column="starttime" jdbcType="TIMESTAMP" property="starttime" />
		<result column="endtime" jdbcType="TIMESTAMP" property="endtime" />

		<result column="shopNumber" jdbcType="VARCHAR" property="shopNumber"/>
		<result column="manager" jdbcType="VARCHAR" property="manager"/>
		<result column="managerPhone" jdbcType="VARCHAR" property="managerPhone"/>
		
		<result column="shopInfo" jdbcType="VARCHAR" property="shopInfo"/>
		
		<result column="name" jdbcType="VARCHAR" property="name"/>
		<result column="comments" property="comments"/>
		
		<result column="price_detail" property="priceDetail"/>
		<result column="updatetime" jdbcType="TIMESTAMP" property="updatetime"/>

		<result column="freeze" property="freeze"/>
	</resultMap>


	<select id="list" parameterType="com.iformall.domain.vo.WxBillAll" resultMap="allVoResultMap">
		select
		bill.id,bill.tenant_id tenantId,bill.merchant_id merchantId,bill.shop_id shopId,
		bill.bill_type_value billTypeValue,bill.bill_type billType,bill.need_pay needPay,
		bill.receive_pay receivePay,bill.pay,bill.owe,bill.receive_date receiveDate,
		bill.pay_date payDate,expired_day expiredDay,bill.status,bill.starttime,
		bill.endtime,bill.name,bill.rent_shop_type rentShopType,m.name merchantName,
		s.shop_number shopNumber, s.manager, s.manager_phone managerPhone,bill.shop_info
		shopInfo,bill.comments,bill.price_detail,updatetime,bill.freeze
		from (
		select id,merchant_id,shop_id,tenant_id,'租金' name,1 bill_type_value,'租金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,starttime,
		endtime,rent_shop_type,case when shop_info is null then '[]' else shop_info end shop_info,comments,''
		price_detail,updatetime,freeze
		from wx_bill_rent where tenant_id=#{tenantId} and is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,'租赁押金' name,2 bill_type_value,'租赁押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,starttime,
		endtime,rent_shop_type,shop_info,NULL comments,'' price_detail,updatetime,freeze from wx_bill_rent_deposit where
		tenant_id=#{tenantId}
		union all
		select id,merchant_id,shop_id,tenant_id,'物业费' name,3 bill_type_value,'物业费'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,starttime,
		endtime,rent_shop_type,case when shop_info is null then '[]' else shop_info end shop_info,comments,''
		price_detail,updatetime,freeze
		from wx_bill_property where tenant_id=#{tenantId} and is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,'物业押金' name,4 bill_type_value,'物业押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,starttime,
		endtime,rent_shop_type,shop_info,NULL comments,'' price_detail,updatetime,freeze from wx_bill_property_deposit where
		tenant_id=#{tenantId}
		union all
		select id,merchant_id,shop_id,tenant_id,case when type=1 then '水费' when type=2 then '电费' else '空调费' end name,
		case when type=1 then 5 when type=2 then 6 else 9 end bill_type_value,
		case when type=1 then '水费' when type=2 then '电费' else '空调费' end bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,starttime,endtime,
		rent_shop_type,'[]' shop_info, NULL comments,price_detail,updatetime,freeze
		from wx_bill_daily where tenant_id=#{tenantId}
		union all
		select id,merchant_id,shop_id,tenant_id,name,7 bill_type_value,concat('其他费用-',name) as bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,starttime,endtime,
		rent_shop_type,'[]' shop_info,comments,'' price_detail,updatetime,freeze
		from wx_bill_other where tenant_id=#{tenantId}
		union all
		select id,merchant_id,shop_id,tenant_id,comments as name,8 bill_type_value,concat('其他押金-',comments) as
		bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,starttime,endtime,
		rent_shop_type,'[]' shop_info,comments,'' price_detail,updatetime,freeze
		from wx_bill_other_deposit where tenant_id=#{tenantId}
		) bill
		left join wx_merchant m on bill.merchant_id=m.id
		left join wx_shop s on bill.shop_id=s.id
		where 1=1
		<if test=" null != tenantId ">
			and bill.tenant_id=#{tenantId}
		</if>
		<if test=" null != month and ''!=month">
			and date_format(bill.receive_date,'%Y-%m')=#{month}
		</if>
		<if test=" null != merchantName and ''!=merchantName ">and m.`name` like concat('%',#{merchantName},'%')</if>
		<if test=" null != billTypeValue ">and bill.bill_type_value = #{billTypeValue}</if>
		<if test=" null != status ">and bill.`status` = #{status}</if>
		<if test=" null != rentShopType ">and bill.rent_shop_type = #{rentShopType}</if>
		<if test=" null != starttime and null!= endtime and '' != starttime and ''!= endtime ">
			and bill.receive_date between #{starttime} and #{endtime}
		</if>
		<if test=" null != merchantId and ''!=merchantId">
			and bill.merchant_id=#{merchantId}
		</if>
		<if test=" null != statusList ">
			and bill.`status` in
			<foreach collection="statusList" index="index" item="status" open="(" separator="," close=")">
				#{status}
			</foreach>
		</if>
		
		<if test=" null != typeList ">
			and bill.`bill_type_value` in
			<foreach collection="typeList" index="index" item="type" open="(" separator="," close=")">
				#{type}
			</foreach>
		</if>
		<if test=" null != freeze ">and bill.freeze = #{freeze}</if>
		<if test=" null != id ">and bill.id = #{id}</if>
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
		<if test=" null == sortColumns"> order by bill.receive_date,bill.id desc,bill.merchant_id,bill.status</if>

	</select>
	
	
	<select id="queryOweInfo" parameterType="com.iformall.domain.vo.WxBillAll" resultType="hashmap">
		

		select count(bill.id) owncount,IFNULL(sum(bill.owe),0) owe from (
		select id,merchant_id,shop_id,tenant_id,1 bill_type_value,'租金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_rent where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,2 bill_type_value,'租赁押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from
		wx_bill_rent_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,3 bill_type_value,'物业费'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from
		wx_bill_property where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,4 bill_type_value,'物业押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from
		wx_bill_property_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,5 bill_type_value,'水费' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_daily where
		type=1
		union all
		select id,merchant_id,shop_id,tenant_id,6 bill_type_value,'电费' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_daily where
		type=2
		union all
		select id,merchant_id,shop_id,tenant_id,9 bill_type_value,'空调费' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_daily where
		type=3
		union all
		select id,merchant_id,shop_id,tenant_id,7 bill_type_value,'其他费用' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_other
		union all
		select id,merchant_id,shop_id,tenant_id,8 bill_type_value,'其他押金' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_other_deposit
		) bill
		where  bill.tenant_id=#{tenantId} and bill.status=#{status}
		<if test=" null != starttime and null!= endtime ">
			and bill.receive_date between #{starttime} and #{endtime}
		</if>
		<if test=" null != month ">
			and date_format(bill.receive_date,'%Y-%m')=#{month}
		</if>
		<if test=" null != rentShopType ">
			and bill.rent_shop_type = #{rentShopType}
		</if>
		<if test=" null != billTypeList ">
			and bill.bill_type_value in
			<foreach collection="billTypeList" index="index" item="type" open="(" separator="," close=")">
				#{type}
			</foreach>
		</if>

	</select>
	
	<select id="queryPaidInfo" parameterType="com.iformall.domain.vo.WxBillAll" resultType="hashmap">
  
		select count(bill.id) paycount,IFNULL(sum(bill.pay),0) pay from (
		select id,merchant_id,shop_id,tenant_id,1 bill_type_value,'租金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_rent where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,2 bill_type_value,'租赁押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from
		wx_bill_rent_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,3 bill_type_value,'物业费'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from
		wx_bill_property where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,4 bill_type_value,'物业押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from
		wx_bill_property_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,5 bill_type_value,'水费' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_daily where
		type=1
		union all
		select id,merchant_id,shop_id,tenant_id,6 bill_type_value,'电费' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_daily where
		type=2
		union all
		select id,merchant_id,shop_id,tenant_id,9 bill_type_value,'空调费' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_daily where
		type=3
		union all
		select id,merchant_id,shop_id,tenant_id,7 bill_type_value,'其他费用' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_other
		union all
		select id,merchant_id,shop_id,tenant_id,8 bill_type_value,'其他押金' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_other_deposit
		) bill
		where bill.tenant_id=#{tenantId} and bill.status=#{status}
		<if test=" null != starttime and null!= endtime ">
			and bill.receive_date between #{starttime} and #{endtime}
		</if>
		<if test=" null != month ">
			and date_format(bill.receive_date,'%Y-%m')=#{month}
		</if>
		<if test=" null != rentShopType ">
			and bill.rent_shop_type = #{rentShopType}
		</if>
	</select>
	
	
	<select id="listData" resultType="hashmap" parameterType="com.iformall.domain.vo.WxBillAll">
		select bill.settle_number,bill.id,bill.merchant_id merchantId,bill.shop_id shopId,bill.bill_type_value billTypeValue,bill.bill_type billType,bill.need_pay needPay,
		bill.receive_pay receivePay,bill.pay,bill.owe,bill.receive_date receiveDate,bill.pay_date payDate,DATEDIFF(now(),bill.receive_date) expiredDay,bill.status,
		bill.tenant_id tenantId,m.name merchantName,s.shop_number shopNumber,bill.starttime,bill.endtime,bill.name,pb.pay_bill_status payBillStatus,
		pb.pay_time_end tradeTime,pb.transaction_id transactionId,pb.pay_amount payAmount,bill.rent_shop_type
		rentShopType,bill.price_detail priceDetail,bill.freeze from (
		select '' settle_number,id,merchant_id,shop_id,tenant_id,'租金' name,1 bill_type_value,'租金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,starttime,endtime,rent_shop_type,''
		price_detail,freeze
		from wx_bill_rent where is_preview = 0 and tenant_id=#{tenantId} and rent_contract_id in (select id from
		wx_rent_contract where status in
		(2,3,4))
		union all
		select '' settle_number,id,merchant_id,shop_id,tenant_id,'租赁押金' name,2 bill_type_value,'租赁押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,starttime,endtime,rent_shop_type,''
		price_detail,freeze
		from wx_bill_rent_deposit where tenant_id=#{tenantId} and rent_contract_id in (select id from wx_rent_contract
		where status in (2,3,4))
		union all
		select '' settle_number,id,merchant_id,shop_id,tenant_id,'物业费' name,3 bill_type_value,'物业费'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,starttime,endtime,rent_shop_type,''
		price_detail,freeze
		from wx_bill_property where is_preview = 0 and tenant_id=#{tenantId} and property_contract_id in (select id from
		wx_property_contract
		where status in(2,3,4))
		union all
		select '' settle_number,id,merchant_id,shop_id,tenant_id,'物业押金' name,4 bill_type_value,'物业押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,starttime,endtime,rent_shop_type,''
		price_detail,freeze
		from wx_bill_property_deposit where tenant_id=#{tenantId} and property_contract_id in (select id from
		wx_property_contract where status in
		(2,3,4))
		union all
		select '' settle_number,id,merchant_id,shop_id,tenant_id,case when type=1 then '水费' when type=2 then '电费' else '空调费' end name,
		case when type=1 then 5 when type=2 then 6 else 9 end bill_type_value,
		case when type=1 then '水费' when type=2 then '电费' else '空调费' end bill_type, 0 as need_pay,receive_pay,
		pay,owe,receive_date,pay_date,expired_day,status,starttime,endtime,rent_shop_type,price_detail,freeze
		from wx_bill_daily where tenant_id=#{tenantId}
		union all
		select '' settle_number,id,merchant_id,shop_id,tenant_id,name,7 bill_type_vaue,'其他费用' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type,''
		price_detail,freeze
		from wx_bill_other where tenant_id=#{tenantId}
		union all
		select '' settle_number,id,merchant_id,shop_id,tenant_id,comments as name,8 bill_type_value,'其他押金' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type,''
		price_detail,freeze
		from wx_bill_other_deposit where tenant_id=#{tenantId}

		union all
		select s.settle_number,s.id,s.merchant_id,'' shop_id,s.tenant_id,'结算单' name, 10 bill_type_value,'结算单' bill_type,0 need_pay,
		((select IFNULL(sum(bill.owe),0) from wx_bill_settle_bill bb left join
		(select id,merchant_id,shop_id,tenant_id,1 bill_type_value,'租金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_rent where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,2 bill_type_value,'租赁押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from
		wx_bill_rent_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,3 bill_type_value,'物业费'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from
		wx_bill_property where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,4 bill_type_value,'物业押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from
		wx_bill_property_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,5 bill_type_value,'水费' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_daily where
		type=1
		union all
		select id,merchant_id,shop_id,tenant_id,6 bill_type_value,'电费' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_daily where
		type=2
		union all
		select id,merchant_id,shop_id,tenant_id,9 bill_type_value,'空调费' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_daily where
		type=3
		union all
		select id,merchant_id,shop_id,tenant_id,7 bill_type_value,'其他费用' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_other
		union all
		select id,merchant_id,shop_id,tenant_id,8 bill_type_value,'其他押金' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_other_deposit
		union all
		select id,id merchant_id,'' shop_id,tenant_id,10 bill_type_value,'补贴' bill_type,0 as
		need_pay,subsidy receive_pay,0 pay,0 owe,'' receive_date,'' pay_date,'' expired_day,status,'' rent_shop_type from wx_merchant_subsidy
		) bill
		on(bb.bill_id=bill.id) where bb.settle_id = s.id and bb.type=0)
		-
		(select IFNULL(sum(bill.owe),0) from wx_bill_settle_bill bb left join
		(select id,merchant_id,shop_id,tenant_id,1 bill_type_value,'租金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_rent where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,2 bill_type_value,'租赁押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from
		wx_bill_rent_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,3 bill_type_value,'物业费'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from
		wx_bill_property where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,4 bill_type_value,'物业押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from
		wx_bill_property_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,5 bill_type_value,'水费' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_daily where
		type=1
		union all
		select id,merchant_id,shop_id,tenant_id,6 bill_type_value,'电费' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_daily where
		type=2
		union all
		select id,merchant_id,shop_id,tenant_id,9 bill_type_value,'空调费' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_daily where
		type=3
		union all
		select id,merchant_id,shop_id,tenant_id,7 bill_type_value,'其他费用' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_other
		union all
		select id,merchant_id,shop_id,tenant_id,8 bill_type_value,'其他押金' bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,rent_shop_type from wx_bill_other_deposit
		union all
		select id,id merchant_id,'' shop_id,tenant_id,10 bill_type_value,'补贴' bill_type,0 as
		need_pay,subsidy receive_pay,0 pay,subsidy owe,'' receive_date,'' pay_date,'' expired_day,status,'' rent_shop_type from wx_merchant_subsidy
		) bill
		on(bb.bill_id=bill.id) where bb.settle_id = s.id and bb.type=1
		)
		) receive_pay
		,0 pay,0 owe,s.createtime receive_date,'' pay_date,0 expired_day,status,'' starttime,'' endtime,'' rent_shop_type,'' price_detail, 0 freeze
		from wx_bill_settle s where s.tenant_id=#{tenantId} and s.status >0

		) bill
		left join wx_merchant m on bill.merchant_id=m.id
		left join wx_shop s on bill.shop_id=s.id
		left join wx_pay_bill pb on bill.id=pb.bill_id
		where 1=1

		<if test=" null != id ">
			and bill.id=#{id}
		</if>
		<if test=" null != tenantId ">
			and bill.tenant_id=#{tenantId}
		</if>
		<if test=" null != merchantId ">
			and bill.merchant_id=#{merchantId}
		</if>
		<if test=" null != statusList ">
			and bill.`status` in
			<foreach collection="statusList" index="index" item="status" open="(" separator="," close=")">
				#{status}
			</foreach>
		</if>
		
		<if test=" null != typeList ">
			and bill.`bill_type_value` in
			<foreach collection="typeList" index="index" item="type" open="(" separator="," close=")">
				#{type}
		</foreach>
		</if>
		<if test="null!=starttime and null!=endtime and ''!=starttime and ''!=endtime ">
			and DATE_FORMAT(bill.receive_date,'%Y-%m') between #{starttime} and #{endtime}
		</if>
		<if test=" null != status ">
			and bill.status=#{status}
		</if>
		<if test=" null != billTypeValue ">
			and bill.bill_type_value=#{billTypeValue}
		</if>
		<if test=" null != rentShopType ">
			and bill.rent_shop_type = #{rentShopType}
		</if>
		order by bill.merchant_id,bill.status desc,bill.receive_date desc
	</select>
	
	
	<select id="getWaitPayBill" resultType="hashmap">
		select bill.tenant_id tenantId,bill.merchant_id merchantId,m.name merchantName,bill.receive_date receiveDate,
			bill.need_pay needPay,m.link_phone linkPhone,app.appname,m.email,bill.bill_type billType from (
		select bill.bill_type,bill.tenant_id,bill.merchant_id,bill.receive_date,ifnull(sum(need_pay),0) need_pay from (
		select id,merchant_id,shop_id,tenant_id,'租金' name,1 bill_type_value,'租金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_rent where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,'租赁押金' name,2 bill_type_value,'租赁押金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_rent_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,'物业费' name,3 bill_type_value,'物业费'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_property where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,'物业押金' name,4 bill_type_value,'物业押金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_property_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,'水费' name,5 bill_type_value,'水费' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_daily where type=1
		union all
		select id,merchant_id,shop_id,tenant_id,'电费' name,6 bill_type_value,'电费' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_daily where type=2
	  	union all
		select id,merchant_id,shop_id,tenant_id,'空调费' name,9 bill_type_value,'空调费' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_daily where type=3
		union all
		select id,merchant_id,shop_id,tenant_id,name,7 bill_type_value,'其他费用' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_other
		union all
		select id,merchant_id,shop_id,tenant_id,name,8 bill_type_value,'其他押金' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_other_deposit
		) bill
		where bill.status not in(3,5,6)
		group by bill.tenant_id,bill.merchant_id,bill.receive_date) bill
		left join wx_merchant m on bill.merchant_id=m.id
		left join (select tenant_id,`name` appname from wx_appinfo app where type=1) app on bill.tenant_id=app.tenant_id
		where m.status=1 and DATEDIFF(bill.receive_date,now())=m.bill_setting
	</select>
	
	
	<select id="getOweBill" resultType="hashmap">
		select bill.tenant_id tenantId,bill.merchant_id merchantId,m.name merchantName,bill.owe,m.link_phone linkPhone,app.appname from (
		select bill.tenant_id,bill.merchant_id,sum(owe) owe from (
		select id,merchant_id,shop_id,tenant_id,'租金' name,1 bill_type_value,'租金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_rent where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,'租赁押金' name,2 bill_type_value,'租赁押金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_rent_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,'物业费' name,3 bill_type_value,'物业费'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_property where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,'物业押金' name,4 bill_type_value,'物业押金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_property_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,'水费' name,5 bill_type_value,'水费' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_daily where type=1
		union all
		select id,merchant_id,shop_id,tenant_id,'电费' name,6 bill_type_value,'电费' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_daily where type=2
		union all
		select id,merchant_id,shop_id,tenant_id,'空调费' name,9 bill_type_value,'空调费' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_daily where type=3
		union all
		select id,merchant_id,shop_id,tenant_id,name,7 bill_type_value,'其他费用' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_other
		union all
		select id,merchant_id,shop_id,tenant_id,name,8 bill_type_value,'其他押金' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_other_deposit
		) bill
		where DATEDIFF(bill.receive_date,now())&lt;0 and bill.status not in(3,5,6)
		group by bill.tenant_id,bill.merchant_id) bill
		left join wx_merchant m on bill.merchant_id=m.id
		left join (select tenant_id,`name` appname from wx_appinfo app where type=1) app on bill.tenant_id=app.tenant_id
		where m.status=1
	</select>

	<select id="getWaitPayBillForEmail" resultType="hashmap">
		select bill.tenant_id tenantId,bill.merchant_id merchantId,m.name merchantName,bill.receive_date receiveDate,
		bill.need_pay  needPay,m.link_phone linkPhone,app.appname,m.email,bill.bill_type billType,ws.manager,ws.manager_phone managerPhone  from (
		select bill.bill_type,bill.tenant_id,bill.merchant_id,bill.receive_date,ifnull(sum(need_pay),0) need_pay from (
		select id,merchant_id,shop_id,tenant_id,'租金' name,1 bill_type_value,'租金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_rent where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,'租赁押金' name,2 bill_type_value,'租赁押金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_rent_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,'物业费' name,3 bill_type_value,'物业费'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_property where is_preview = 0
		union
		select id,merchant_id,shop_id,tenant_id,'物业押金' name,4 bill_type_value,'物业押金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_property_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,'水费' name,5 bill_type_value,'水费' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_daily where type=1
		union all
		select id,merchant_id,shop_id,tenant_id,'电费' name,6 bill_type_value,'电费' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_daily where type=2
		union all
		select id,merchant_id,shop_id,tenant_id,'空调费' name,9 bill_type_value,'空调费' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_daily where type=3
		union all
		select id,merchant_id,shop_id,tenant_id,name,7 bill_type_value,'其他费用' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_other
		union all
		select id,merchant_id,shop_id,tenant_id,name,8 bill_type_value,'其他押金' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_other_deposit
		) bill
		where bill.status not in(3,5,6)
		group by bill.tenant_id,bill.merchant_id,bill.receive_date,bill.bill_type
		) bill
		left join wx_merchant m on bill.merchant_id=m.id
		left join (select tenant_id,`name` appname from wx_appinfo app where type=1) app on bill.tenant_id=app.tenant_id
		left join wx_merchant_shop wms on wms.merchant_id = m.id
		left join wx_shop ws on wms.shop_id = ws.id
		where m.status=1 and DATEDIFF(bill.receive_date,now())=m.bill_setting
	</select>

	<select id="getOweBillForEmail" resultType="hashmap">
		select bill.tenant_id tenantId,bill.merchant_id merchantId,bill.bill_type billType,bill.owe owe,m.link_phone managerPhone,m.`link_person` manager,m.email,app.appname,bill.receive_date receiveDate
		,DATEDIFF(now(),bill.receive_date) expiredDay  from (
		select bill.tenant_id,bill.merchant_id,bill.bill_type,sum(owe) owe,bill.receive_date,bill.expired_day from (

		select id,merchant_id,shop_id,tenant_id,'租金' name,1 bill_type_value,'租金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_rent where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,'租赁押金' name,2 bill_type_value,'租赁押金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_rent_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,'物业费' name,3 bill_type_value,'物业费'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_property where is_preview = 0
		union all
		select id,merchant_id,shop_id,tenant_id,'物业押金' name,4 bill_type_value,'物业押金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_property_deposit
		union all
		select id,merchant_id,shop_id,tenant_id,'水费' name,5 bill_type_value,'水费' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_daily where type=1
		union all
		select id,merchant_id,shop_id,tenant_id,'电费' name,6 bill_type_value,'电费' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_daily where type=2
		union all
		select id,merchant_id,shop_id,tenant_id,'空调费' name,9 bill_type_value,'空调费' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_daily where type=3
		union all
		select id,merchant_id,shop_id,tenant_id,name,7 bill_type_value,'其他费用' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_other
		union all
		select id,merchant_id,shop_id,tenant_id,name,8 bill_type_value,'其他押金' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_other_deposit

		) bill
		where DATEDIFF(bill.receive_date,now())&lt;0 and bill.status not in(3,5,6)
		group by bill.tenant_id,bill.merchant_id,bill.bill_type) bill
		left join wx_merchant m on bill.merchant_id=m.id
		left join (select tenant_id,`name` appname from wx_appinfo app where type=1) app on bill.tenant_id=app.tenant_id
		where m.status=1
	</select>

	<sql id="getOweBillAsPageConditions">
		and status !=6
		<if test="null != status ">
			and status = #{status}
		</if>
		<if test=" null != month and ''!=month">
			and date_format(receive_date,'%Y-%m')=#{month}
		</if>
		<if test=" null != starttime and ''!=starttime">
			and receive_date >= #{starttime}
		</if>
		<if test=" null != endtime and ''!=endtime">
			and receive_date &lt;= #{endtime}
		</if>
	</sql>

	<select id="getOweBillAsPage" parameterType="com.iformall.domain.vo.WxBillAll" resultType="hashmap">
		select bill.tenant_id tenantId,bill.merchant_id merchantId,m.name merchantName,bill.bill_type billType,
		m.link_phone managerPhone,m.`link_person` manager,m.email,
		bill.receive_date receiveDate,DATEDIFF(now(),bill.receive_date) expiredDay,

		round(bill.rentOwe/100,2) rentOwe,
		round(bill.propertyOwe/100,2) propertyOwe,
		round(bill.depositOwe/100,2) depositOwe,
		round(bill.otherOwe/100,2) otherOwe,
		round((bill.rentOwe+bill.propertyOwe+bill.depositOwe+bill.otherOwe)/100,2) totalOwe,

		<choose>
			<when test="1==filterHasPay">
				round((bill.rentReceivePay-bill.rentPay)/100,2) rentReceivePay,
				round((bill.propertyReceivePay-bill.propertyPay)/100,2) propertyReceivePay,
				round((bill.depositReceivePay-bill.depositPay)/100,2) depositReceivePay,
				round((bill.otherReceivePay-bill.otherPay)/100,2) otherReceivePay,
				round((bill.rentReceivePay+bill.propertyReceivePay+bill.depositReceivePay+bill.otherReceivePay
				-bill.rentPay-bill.propertyPay-bill.depositPay-bill.otherPay)/100,2) totalReceivePay
			</when>
			<otherwise>
				round(bill.rentReceivePay/100,2) rentReceivePay,
				round(bill.propertyReceivePay/100,2) propertyReceivePay,
				round(bill.depositReceivePay/100,2) depositReceivePay,
				round(bill.otherReceivePay/100,2) otherReceivePay,
				round((bill.rentReceivePay+bill.propertyReceivePay+bill.depositReceivePay+bill.otherReceivePay)/100,2) totalReceivePay
			</otherwise>
		</choose>

		from (

		select oweList.*,
		max(case oweList.bill_type when '租金' then oweList.owe else 0 end) rentOwe,
		max(case oweList.bill_type when '物业费' then oweList.owe else 0 end) propertyOwe,
		max(case oweList.bill_type when '欠缴押金' then oweList.owe else 0 end) depositOwe,
		max(case oweList.bill_type when '其他费用' then oweList.owe else 0 end) otherOwe,

		max(case oweList.bill_type when '租金' then oweList.receive_pay else 0 end) rentReceivePay,
		max(case oweList.bill_type when '物业费' then oweList.receive_pay else 0 end) propertyReceivePay,
		max(case oweList.bill_type when '欠缴押金' then oweList.receive_pay else 0 end) depositReceivePay,
		max(case oweList.bill_type when '其他费用' then oweList.receive_pay else 0 end) otherReceivePay,

		max(case oweList.bill_type when '租金' then oweList.pay else 0 end) rentPay,
		max(case oweList.bill_type when '物业费' then oweList.pay else 0 end) propertyPay,
		max(case oweList.bill_type when '欠缴押金' then oweList.pay else 0 end) depositPay,
		max(case oweList.bill_type when '其他费用' then oweList.pay else 0 end) otherPay
		from(
		select bill.tenant_id,bill.merchant_id,bill.bill_type,sum(owe) owe,bill.receive_date,bill.expired_day,sum(bill.receive_pay) receive_pay,sum(bill.pay) pay from (

		select id,merchant_id,shop_id,tenant_id,'租金' name,1 bill_type_value,'租金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_rent where tenant_id = #{tenantId} and is_preview = 0
		<include refid="getOweBillAsPageConditions"/>
		union all
		select id,merchant_id,shop_id,tenant_id,'物业费' name,3 bill_type_value,'物业费'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_property where tenant_id = #{tenantId} and is_preview = 0
		<include refid="getOweBillAsPageConditions"/>
		union all
		select res.id,res.merchant_id,res.shop_id,res.tenant_id,res.name,res.bill_type_value,res.bill_type,res.need_pay,
		res.receive_pay,res.pay,res.owe owe,res.receive_date,res.pay_date,res.expired_day,res.status,res.starttime,res.endtime,res.rent_shop_type from(
		select id,merchant_id,shop_id,tenant_id,'欠缴押金' name,2 bill_type_value,'欠缴押金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_rent_deposit where tenant_id = #{tenantId}
		<include refid="getOweBillAsPageConditions"/>
		union all
		select id,merchant_id,shop_id,tenant_id,'欠缴押金' name,4 bill_type_value,'欠缴押金'
		bill_type,need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type from wx_bill_property_deposit where tenant_id = #{tenantId}
		<include refid="getOweBillAsPageConditions"/>
		union all
		select id,merchant_id,shop_id,tenant_id,'欠缴押金' name,8 bill_type_value,'欠缴押金' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_other_deposit where tenant_id = #{tenantId}
		<include refid="getOweBillAsPageConditions"/>
		) res
		union all
		select res.id,res.merchant_id,res.shop_id,res.tenant_id,res.name,res.bill_type_value,res.bill_type,res.need_pay,
		res.receive_pay,res.pay,res.owe owe,res.receive_date,res.pay_date,res.expired_day,res.status,res.starttime,res.endtime,res.rent_shop_type from(
		select id,merchant_id,shop_id,tenant_id,'其他费用' name,5 bill_type_value,'其他费用' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_daily where tenant_id = #{tenantId} and type in(1,2,3)
		<include refid="getOweBillAsPageConditions"/>
		union all
		select id,merchant_id,shop_id,tenant_id,'其他费用' name,7 bill_type_value,'其他费用' bill_type,0 as
		need_pay,receive_pay,pay,owe,DATE_FORMAT(receive_date,'%Y-%m-%d') receive_date,pay_date,expired_day,status,'' starttime,'' endtime,rent_shop_type
		from wx_bill_other where tenant_id = #{tenantId}
		<include refid="getOweBillAsPageConditions"/>
		) res

		) bill
		where bill.status not in(3,5,6)
		group by bill.merchant_id,bill.bill_type

		) oweList group by oweList.merchant_id

		) bill
		left join wx_merchant m on bill.merchant_id=m.id
		where m.status=1 and m.tenant_id = #{tenantId}
		<if test=" null != merchantName and '' != merchantName">and m.name like concat('%', #{merchantName},'%')</if>
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
		<if test=" null == sortColumns"> order by totalOwe desc </if>
	</select>

	<select id="getReceiveAndPayBillAsPage" parameterType="com.iformall.domain.vo.WxBillAll" resultType="hashmap">
		select tt.* from (
		select m.tenant_id,m.id,m.name,ws.shop_number shopNumber,ws.type,m.link_person linkPerson,m.link_phone linkPhone,
		xb.title business,rc.rental_start_date rentalStartDate,rc.rental_end_date rentalEndDate,rc.rent_shop_type,
		ifnull(round(
		(select sum(bill.receive_pay) from (
		select merchant_id,receive_pay from wx_bill_rent where is_preview = 0 and status not in(3,6)
		union all
		select merchant_id,receive_pay from wx_bill_rent_deposit where status not in(3,6)
		union all
		select merchant_id,receive_pay from wx_bill_property where is_preview = 0 and status not in(3,6)
		union all
		select merchant_id,receive_pay from wx_bill_property_deposit where status not in(3,6)
		union all
		select merchant_id,receive_pay from wx_bill_daily where type in(1,2,3) and status not in(3,6)
		union all
		select merchant_id,receive_pay from wx_bill_other where status not in(3,6)
		union all
		select merchant_id,receive_pay from wx_bill_other_deposit where status not in(3,6)
		) bill where bill.merchant_id = m.id
		)/100,2),0) receivePay,
		ifnull(round(
		(select sum(ss.money) from (
		select ms.subsidy money,mm.id
		from wx_merchant_subsidy ms
		left join wx_coupon_order co on co.id = ms.coupon_order_id
		left join wx_coupon c on c.id = co.coupon_id
		left join wx_merchant mm on mm.id = ms.merchant_id
		left join wx_card_spend cs on cs.order_id = ms.order_id
		left join wx_c_user cu on cu.id = cs.owner_id
		where ms.order_type in(3,0) and ms.status = 0
		union all
		select receive_pay money,merchant_id id from wx_bill_rent_deposit where status = 3
		union all
		select receive_pay money,merchant_id id from wx_bill_property_deposit where status = 3
		union all
		select receive_pay money,merchant_id id from wx_bill_other_deposit where status = 3
		)ss where ss.id = m.id
		)/100,2),0) payOut,
		
		(ifnull(round(
		(select sum(bill.receive_pay) from (
		select merchant_id,receive_pay from wx_bill_rent where is_preview = 0 and status not in(3,6)
		union all
		select merchant_id,receive_pay from wx_bill_rent_deposit where status not in(3,6)
		union all
		select merchant_id,receive_pay from wx_bill_property where is_preview = 0 and status not in(3,6)
		union all
		select merchant_id,receive_pay from wx_bill_property_deposit where status not in(3,6)
		union all
		select merchant_id,receive_pay from wx_bill_daily where type in(1,2,3) and status not in(3,6)
		union all
		select merchant_id,receive_pay from wx_bill_other where status not in(3,6)
		union all
		select merchant_id,receive_pay from wx_bill_other_deposit where status not in(3,6)
		) bill where bill.merchant_id = m.id
		)/100,2),0)-
		ifnull(round(
		(select sum(ss.money) from (
		select ms.subsidy money,mm.id
		from wx_merchant_subsidy ms
		left join wx_coupon_order co on co.id = ms.coupon_order_id
		left join wx_coupon c on c.id = co.coupon_id
		left join wx_merchant mm on mm.id = ms.merchant_id
		left join wx_card_spend cs on cs.order_id = ms.order_id
		left join wx_c_user cu on cu.id = cs.owner_id
		where ms.order_type in(3,0) and ms.status = 0
		union all
		select receive_pay money,merchant_id id from wx_bill_rent_deposit where status = 3
		union all
		select receive_pay money,merchant_id id from wx_bill_property_deposit where status = 3
		union all
		select receive_pay money,merchant_id id from wx_bill_other_deposit where status = 3
		)ss where ss.id = m.id
		)/100,2),0)) balance
		
		from wx_merchant m
		left join (select ms.merchant_id,s.type,GROUP_CONCAT(s.shop_number) as shop_number
		from wx_merchant_shop ms inner join wx_shop s on ms.shop_id=s.id where ms.is_del=0
		group by ms.merchant_id) ws on m.id=ws.merchant_id
		left join wx_business xb on(m.business_id = xb.id)
		left join wx_rent_contract rc on(rc.merchant_id = m.id)
		where m.status=1
		) tt where 1=1
		<if test=" null != tenantId"> and tt.tenant_id = #{tenantId} </if>
		<if test=" null != merchantId"> and tt.id = #{merchantId} </if>
		<if test=" null != merchantName and ''!=merchantName">and tt.name like concat('%', #{merchantName},'%')</if>
		<if test=" null != rentShopType"> and tt.rent_shop_type = #{rentShopType} </if>
		and (receivePay >0 or payOut>0)
		<if test=" null != sortColumns"> order by ${sortColumns} </if>
		<if test=" null == sortColumns"> order by tt.id desc </if>
	</select>

	<select id="allDepositList" parameterType="com.iformall.domain.vo.WxBillAll" resultType="hashmap">
		select bill.*
		from (
		select id,merchant_id,shop_id,tenant_id,'租赁押金' name,2 bill_type_value,'租赁押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type,shop_info,NULL comments,'' price_detail,updatetime,freeze from wx_bill_rent_deposit where
		tenant_id=#{tenantId}
		union all
		select id,merchant_id,shop_id,tenant_id,'物业押金' name,4 bill_type_value,'物业押金'
		bill_type,need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,'' starttime,''
		endtime,rent_shop_type,shop_info,NULL comments,'' price_detail,updatetime,freeze from wx_bill_property_deposit where
		tenant_id=#{tenantId}
		union all
		select id,merchant_id,shop_id,tenant_id,comments as name,8 bill_type_value,concat('其他押金-',comments) as
		bill_type,0 as
		need_pay,receive_pay,pay,owe,receive_date,pay_date,expired_day,status,'' starttime,'' endtime,
		rent_shop_type,'[]' shop_info,comments,'' price_detail,updatetime,freeze
		from wx_bill_other_deposit where tenant_id=#{tenantId}
		) bill
		where 1=1
		<if test=" null != freeze"> and bill.freeze = #{freeze} </if>
		<if test=" null != status"> and bill.status = #{status} </if>
		<if test=" null != merchantId">and bill.merchant_id=#{merchantId}</if>
	</select>

</mapper>
