select crc.tenant_id, crc.id '合同ID', hhb.dept_id '房源所属门店ID', house_sd.name '房源所属门店', maintainer_sd.id '所属门店ID', maintainer_sd.name '所属门店', hhr.address '房源地址', mt_emp.name '合同维护人',
	cri.name '租客姓名', crc.begin_time '合同开始日期', crc.end_time '合同结束日期', 
	crc.quite_date '退租日期', crc.terminate_type '退租类型', 
	case when JSON_EXTRACT(crc.`cancel_info`, '$.paymentAccountType')=1 then '银联' 
	when JSON_EXTRACT(crc.`cancel_info`, '$.paymentAccountType')=2 then '支付宝' 
	when JSON_EXTRACT(crc.`cancel_info`, '$.paymentAccountType')=3 then '微信' end '退款途径',
	REPLACE(REPLACE(JSON_EXTRACT(crc.`cancel_info`, '$.paymentAccount'), '"', ''), 'null', '') '退款账号',
	REPLACE(REPLACE(JSON_EXTRACT(crc.`cancel_info`, '$.bankName'), '"', ''), 'null', '') '银行行号', 
	REPLACE(REPLACE(JSON_EXTRACT(crc.`cancel_info`, '$.bankNumber'), '"', ''), 'null', '') '开户银行',
	REPLACE(REPLACE(JSON_EXTRACT(crc.`cancel_info`, '$.reason'), '"', ''), 'null', '') '退租备注',
	REPLACE(REPLACE(JSON_EXTRACT(crc.`cancel_info`, '$.rejectName'), '"', ''), 'null', '') '退租操作人',
	REPLACE(REPLACE(JSON_EXTRACT(crc.`cancel_info`, '$.rejectTime'), '"', ''), 'null', '') '退租时间',
	_total.direction '费用方向',
	_total.kind '费用科目',
	_total.original_money '金额',
	_total.应退款金额 '应退款金额',
	_detail.info '财务退款备注',
	IF(_flow_status.avg_status is null, '-', IF(_flow_status.avg_status=1, '待审核', IF(_flow_status.avg_status=2, '审核通过', '部分审核通过'))) '审核状态',
	IF(_flow_status.avg_status is null, '未处理', IF(_flow_status.avg_status=2, '已退款', IF(_flow_status.avg_status>1, '已部分退款', '未退款'))) '退款信息'
from yuxin_contract.cont_renter_contract crc 
left join yuxin_house.hse_house_base hhb on hhb.is_delete=0 and hhb.id=crc.house_id
left join yuxin_setting.setting_department house_sd on house_sd.id=hhb.dept_id and house_sd.is_delete=0
left join yuxin_house.hse_house_room hhr on hhr.is_delete=0 and hhr.id=crc.house_id
left join yuxin_setting.setting_employee_dept maintainer_ed on maintainer_ed.emp_id=crc.maintainer_id and maintainer_ed.is_delete=0
left join yuxin_setting.setting_department maintainer_sd on maintainer_sd.id=maintainer_ed.dept_id and maintainer_sd.is_delete=0
left join yuxin_setting.setting_employee_info mt_emp on mt_emp.id=crc.maintainer_id and mt_emp.is_delete=0
left join yuxin_contract.cont_renter_info cri on cri.is_delete=0 and cri.customer_type=1 and cri.contract_id=crc.id
left join (
    select bd.biz_id, sd.name 'kind', IF(bd.fee_direction=1,'应收','应付') 'direction', SUM(bd.original_money) 'original_money',
	SUM(IF(bd.fee_direction = 2, 1, -1) * bd.original_money) '应退款金额'
    from yuxin_finance.fin_finance_bill_detail bd
	left join yuxin_setting.setting_dictionary sd on sd.id=bd.fee_subject_id
    where bd.is_valid=1 and bd.is_delete=0 and bd.biz_type=2
    group by bd.biz_id,sd.name,bd.fee_direction
) _total on _total.biz_id= crc.id
left join (
	select bd.biz_id, GROUP_CONCAT(CONCAT(sd.name, IF(bd.fee_direction=1,1,-1)*bd.original_money)) 'info'
	from yuxin_finance.fin_finance_bill_detail bd 
	left join yuxin_setting.setting_dictionary sd on sd.id=bd.fee_subject_id
	where bd.is_valid=1 and bd.is_delete=0 and bd.biz_type=2 and bd.fee_direction=2
	group by bd.biz_id 
) _detail on _detail.biz_id=crc.id
left join (
	select bf.biz_id, avg(bf.audit_status) 'avg_status'
	from yuxin_finance.fin_finance_bill_flow bf
	where bf.is_delete=0 and bf.biz_type=2 and bf.bill_type=2 
	group by bf.biz_id
) _flow_status on _flow_status.biz_id=crc.id
where crc.contract_status=3 and crc.is_delete=0