select b.name as name, b.id_no as pid, b.phone_no as mobile, '1' as queryReason, '1' as guaranteeType, '1' as loanPurpose, '99' as customType, '-1' as applyAmount, a.order_no as loanId
select
from xyqb_i_apply_quota_record a left join xyqb_user_i_user_detail b on a.user_id=b.user_id
a.id reqID
where a.created_at>=DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y-%m-%d')
,'A' opCode
and 1>2;
,DATE_FORMAT(now(),'%Y-%m-%dT%H:%i:%S') uploadTs
,'1' isFinTechAgencyBusiness
,DATE_FORMAT(case when i.created_at is null and h.created_at <=f.created_at then h.created_at
when i.created_at is null and h.created_at>f.created_at then f.created_at
else i.created_at end,'%Y-%m-%dT%H:%i:%S') as applyDate
,if(d.order_no is null,a.loan_application_history_id,d.order_no) applyId
,'1' applyType
,b.name name
,'1' idType
,b.id_no pid
,b.phone_no mobile
,b.email emailAddress
,case when j.education=1 then '10'
when j.education=2 then '20'
when j.education=3 then '30'
when j.education in (4,6) then '40'
when j.education=5 then '50'
when j.education in (7,8) then '60'
else '70' end eduBackground
,'500' degree
,'1' guaranteeType
,'1' loanPurpose
,'99' customType
,'-1' applyAmount
,'8' livingCondition
,case when j.marry_status=1 then '10'
when j.marry_status=2 then '20'
when j.marry_status=4 then '30'
when j.marry_status=3 then '40'
else '90' end marriageStatus
from xyqb_i_loan_application_manifest_history a
left join xyqb_user_i_user_detail b on a.user_id=b.user_id
left join xyqb_i_loan_account_ext c on a.loan_application_history_id=c.loan_id
left join xyqb_i_apply_quota_record d on c.order_no=d.order_no
left join xyqb_i_loan_submit_info f on a.loan_application_history_id=f.loan_id
left join xyqb_i_loan_application_history h on a.loan_application_history_id=h.id
left join xyqb_i_quota_credit i on d.order_no=i.order_no
left join xyqb_i_loan_base_info j on a.loan_application_history_id=j.loan_id
where a.funding_corp_id in (880,890)
and a.contract_term not in (2,3)
and a.transaction_status in (2,5)
and case when i.created_at is null and h.created_at <=f.created_at then h.created_at
when i.created_at is null and h.created_at>f.created_at then f.created_at
else i.created_at end >= #{startTime,jdbcType=VARCHAR}
and case when i.created_at is null and h.created_at <=f.created_at then h.created_at
when i.created_at is null and h.created_at>f.created_at then f.created_at
else i.created_at end < #{endTime,jdbcType=VARCHAR};
when min(i.dkye)=0 and if(DATEDIFF(a.next_deadline,a.repaid_at)>1,a.term_no,h.max_term) < f.max_term_no then '2'
when min(i.dkye)=0 and if(DATEDIFF(a.next_deadline,a.repaid_at)>1,a.term_no,h.max_term)= f.max_term_no then '3'
when f.yqze=0 and min(i.dkye)>0 then '1'
end as loanStatus
,if(f.yqze>0,'overdue','normal') termStatus
from (
select a.*,if(c.term_no is null,a.term_no,c.term_no) next_term
,if(c.deadline is null,a.deadline,c.deadline) next_deadline
,b.loan_paid_at
from xyqb_i_repayment_plan a
left join xyqb_i_loan_application_manifest_history b
on a.loan_application_history_id=b.loan_application_history_id and b.loan_paid_at < #{endTime,jdbcType=VARCHAR}
left join xyqb_i_repayment_plan c on a.loan_application_history_id=c.loan_application_history_id
and a.term_no=c.term_no-1
where a.repaid_at>= #{startTime,jdbcType=VARCHAR} and a.repaid_at < #{endTime,jdbcType=VARCHAR} and a.repayment_status=3
and b.funding_corp_id in (880,890)
and b.contract_term not in (2,3)
and b.loan_paid_at>='2019-10-25'
) a
left join xyqb_user_i_user_detail b on a.user_id=b.user_id
left join xyqb_i_loan_account_ext c on a.loan_application_history_id=c.loan_id
left join xyqb_i_apply_quota_record d on c.order_no=d.order_no
left join (
select a.loan_application_history_id
,sum(if(((a.repayment_status !=3 and DATEDIFF( #{endTime,jdbcType=VARCHAR},a.deadline)>=30) or (a.repayment_status =3 and a.repaid_at>= #{endTime,jdbcType=VARCHAR} and DATEDIFF(a.repaid_at,a.deadline)>=30)) and a.deadline <= #{endTime,jdbcType=VARCHAR},a.required_repayment,0)) yqze
,sum(if(a.repayment_status !=3 or (a.repayment_status =3 and a.repaid_at>= #{endTime,jdbcType=VARCHAR}),g.principal,0)) dkye
,min(if((a.repayment_status !=3 or (a.repayment_status =3 and a.repaid_at>= #{endTime,jdbcType=VARCHAR})) and a.deadline <= #{endTime,jdbcType=VARCHAR},a.deadline,null)) min_deadline
,max(a.term_no) max_term_no
from xyqb_i_repayment_plan a
left join xyqb_i_loan_application_manifest_history b
on a.loan_application_history_id=b.loan_application_history_id
left join xyqb_i_repayment_record g on a.id=g.repayment_plan_id
where b.funding_corp_id in (880,890)
group by a.loan_application_history_id) f on a.loan_application_history_id=f.loan_application_history_id
left join xyqb_i_repayment_record g on a.id=g.repayment_plan_id
left join xyqb_i_loan_application_manifest_history b
on a.loan_application_history_id=b.loan_application_history_id and b.loan_paid_at < #{endTime,jdbcType=VARCHAR}
where ((a.repayment_status !=3 and DATEDIFF( #{endTime,jdbcType=VARCHAR},a.deadline)>=30) or (a.repayment_status =3 and a.repaid_at>= #{endTime,jdbcType=VARCHAR} and DATEDIFF(a.repaid_at,a.deadline)>=30)) and
and floor(DATEDIFF( #{endTime,jdbcType=VARCHAR},a.deadline)/30)=DATEDIFF( #{endTime,jdbcType=VARCHAR},a.deadline)/30
and b.funding_corp_id in (880,890)
and b.transaction_status in (2,5)
and b.contract_term not in (2,3)
and b.loan_paid_at>='2019-10-25'
) a
left join xyqb_user_i_user_detail b on a.user_id=b.user_id
left join xyqb_i_loan_account_ext c on a.loan_application_history_id=c.loan_id
left join xyqb_i_apply_quota_record d on c.order_no=d.order_no
left join (select a.loan_application_history_id
,sum(if(((a.repayment_status !=3 and DATEDIFF( #{endTime,jdbcType=VARCHAR},a.deadline)>=30) or (a.repayment_status =3 and a.repaid_at>= #{endTime,jdbcType=VARCHAR} and DATEDIFF(a.repaid_at,a.deadline)>=30)) and a.deadline <= #{endTime,jdbcType=VARCHAR},a.required_repayment,0)) yqze
,sum(if(a.repayment_status !=3 or (a.repayment_status =3 and a.repaid_at>= #{endTime,jdbcType=VARCHAR}),if(b.loan_paid_at>='2018-01-01',g.principal,h.principle),0)) dkye
,min(if((a.repayment_status !=3 or (a.repayment_status =3 and a.repaid_at>= #{endTime,jdbcType=VARCHAR})) and a.deadline <= #{endTime,jdbcType=VARCHAR},a.deadline,null)) min_deadline
from xyqb_i_repayment_plan a
from xyqb_i_repayment_plan a
where 1>2;
left join xyqb_i_loan_application_manifest_history b
on a.loan_application_history_id=b.loan_application_history_id
left join xyqb_i_repayment_record g on a.id=g.repayment_plan_id
left join xyqb_i_plan_amount_detail h on a.id=h.plan_id
where b.funding_corp_id in (880,890)
group by a.loan_application_history_id) f on a.loan_application_history_id=f.loan_application_history_id
left join xyqb_i_repayment_record g on a.id=g.repayment_plan_id;