SELECT a.*,contract.ContractNo,contract.Id as ContractId,b.CourseId,IFNULL(C.CourseName,'') AS CourseName
,class.ClassName,class.ClassNo,class.Teacher_Id,teacher.TeacherName,b.OrderId,b.CreateTime as OrderTime,b.EnterID,b.PreferPrice,b.Income,b.Refund,b.PlatformTax,
(a.TotalHours-a.CompleteHours) as SurplusHours,gc.AbsenceNum,gc.LeaveNum,eventlog.EventlogNum,b.EffectStatus,b.JoinType,sog.Student_Id
FROM RB_Order_Guest AS A LEFT JOIN rb_order AS B ON A.OrderId=B.OrderId
LEFT JOIN rb_education_contract as contract on contract.GuestId=a.Id and contract.`Status`=0
LEFT JOIN rb_student_orderguest as sog on sog.GuestId=a.Id and sog.`Status`=0
LEFT JOIN rb_class as class on class.ClassId=b.ClassId
LEFT JOIN rb_course AS C ON (B.CourseId=C.CourseId AND B.CourseId>0 AND B.OrderType=1)
LEFT JOIN rb_teacher as teacher on teacher.TId=class.Teacher_Id
LEFT JOIN (SELECT OrderGuestId,
SUM(CASE CheckStatus WHEN 1 THEN 1 ELSE 0 END) as AbsenceNum,
SUM(CASE CheckStatus WHEN 2 THEN 1 ELSE 0 END) as LeaveNum
from rb_class_check where `Status`=0 and CheckStatus <>0 GROUP BY OrderGuestId) as gc on gc.OrderGuestId=a.Id
LEFT JOIN (SELECT StuId,COUNT(*) as EventlogNum from rb_student_eventlog where `Status`=0 GROUP BY StuId) as eventlog on eventlog.StuId=sog.Student_Id
WHERE 1=1 and A.Status=0 and class.Status=0 and class.ClassStatus <>4 and b.OrderState<>3
");
if(demodel.Group_Id>0)
{
builder.AppendFormat($@" AND A.{nameof(RB_Order_Guest_Extend.Group_Id)} ={demodel.Group_Id}");
}
if(demodel.School_Id>0)
{
builder.AppendFormat($@" AND A.{nameof(RB_Order_Guest_Extend.School_Id)} ={demodel.School_Id}");
}
if(demodel.CourseId>0)
{
builder.AppendFormat($@" AND B.{nameof(RB_Order_Guest_Extend.CourseId)} ={demodel.CourseId}");
}
if(demodel.ClassId>0)
{
builder.AppendFormat($@" AND B.{nameof(RB_Order_Guest_Extend.ClassId)} ={demodel.ClassId}");
}
if(!string.IsNullOrEmpty(demodel.GuestName))
{
builder.AppendFormat($@" AND A.{nameof(RB_Order_Guest_Extend.GuestName)} like '%{demodel.GuestName}%'");
}
if(!string.IsNullOrEmpty(demodel.ClassNo))
{
builder.AppendFormat($@" AND class.{nameof(RB_Order_Guest_Extend.ClassNo)} like '%{demodel.ClassNo}%'");
}
if(demodel.OrderId>0)
{
builder.AppendFormat($@" AND A.{nameof(RB_Order_Guest_Extend.OrderId)} ={demodel.OrderId}");
}
if(demodel.EnterID>0)
{
builder.AppendFormat($@" AND B.{nameof(RB_Order_Guest_Extend.EnterID)} ={demodel.EnterID}");
}
if(demodel.OrderTime.HasValue)
{
builder.AppendFormat($@" AND DATE_FORMAT(B.CreateTime,'%y-%m-%d')>=DATE_FORMAT('{demodel.OrderTime.Value.ToString("yyyy-MM-dd")}','%y-%m-%d')");
}
if(demodel.EndOrderTime.HasValue)
{
builder.AppendFormat($@" AND DATE_FORMAT(B.CreateTime,'%y-%m-%d')<=DATE_FORMAT('{demodel.EndOrderTime.Value.ToString("yyyy-MM-dd")}','%y-%m-%d')");
}
if(demodel.EffectStatus>0)
{
if(demodel.EffectStatus==1)
{
builder.AppendFormat($@" AND B.{nameof(RB_Order_Guest_Extend.EffectStatus)} =0");
}
else
{
builder.AppendFormat($@" AND B.{nameof(RB_Order_Guest_Extend.EffectStatus)} >0");
builder.AppendFormat($@" AND A.{nameof(RB_Order_Guest_Extend.GuestState)} ={demodel.GuestState}");
}
}
if(demodel.JoinType>0)
{
builder.AppendFormat($@" AND B.{nameof(RB_Order_Guest_Extend.JoinType)} ={demodel.JoinType}");