package service

import (
	"XT_Admin_Api/models/function_models"
	"fmt"
)

//病人管理
func GetTodayInfo(to int64,ctime int64,page int64,limit int64)(patientinfo []function_models.PatientInfo,total int64,err error) {

	offset := (page - 1) * limit
	err = readUserDb.Raw("select * from sgj_users.sgj_user_org as u join sgj_xt.xt_patients as x on x.user_org_id = u.id where x.created_time >= ? AND x.created_time <= ?",to,ctime).Scan(&patientinfo).Error
	rows, err := readUserDb.Offset(offset).Limit(limit).Raw("select Count(1) from sgj_users.sgj_user_org as u join sgj_xt.xt_patients as x on x.user_org_id = u.id where x.created_time >= ? AND x.created_time <= ?", to, ctime).Rows()
	fmt.Println("rows是什么",rows)
	if rows.Next() {
		rows.Scan(&total)
	}
	fmt.Println("&total是什么",total)
	return patientinfo,total,err
}

   //透析处方
func GetTodayDialysisPatientsInfo(to int64,ctime int64,page int64,limit int64)(patientinfo []function_models.PatientInfo,total int64,err error)  {

	offset := (page - 1) * limit
	err = readUserDb.Raw("select * from sgj_users.sgj_user_org as u join sgj_xt.xt_dialysis_prescription as x on x.user_org_id = u.id JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ?",to,ctime).Scan(&patientinfo).Error
	rows, err := readUserDb.Offset(offset).Limit(limit).Raw("select Count(1) from sgj_users.sgj_user_org as u join sgj_xt.xt_dialysis_prescription as x on x.user_org_id = u.id  where x.created_time >= ? AND x.created_time <= ?", to, ctime).Rows()
	if rows.Next() {
		rows.Scan(&total)
	}

	return patientinfo,total,err
}


   //接诊评估
func GetTodayReceivePatientsInfo(to int64,ctime int64,page int64,limit int64)(patientinfo []function_models.PatientInfo,total int64,err error)  {

	offset := (page - 1) * limit
	err = readUserDb.Raw("select * from sgj_users.sgj_user_org as u join sgj_xt.xt_receive_treatment_asses as x on x.user_org_id = u.id JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ?",to,ctime).Scan(&patientinfo).Error
	rows, err := readUserDb.Offset(offset).Limit(limit).Raw("select Count(1) from sgj_users.sgj_user_org as u join sgj_xt.xt_receive_treatment_asses as x on x.user_org_id = u.id  where x.created_time >= ? AND x.created_time <= ?", to, ctime).Rows()
	if rows.Next() {
		rows.Scan(&total)
	}

	return patientinfo,total,err
}
// 透前评估
func GetTodayBeforDislysisInfo(to int64,ctime int64,page int64,limit int64)(patientinfo []function_models.PatientInfo,total int64,err error)  {
	offset := (page - 1) * limit
	err = readUserDb.Raw("select * from sgj_users.sgj_user_org as u join sgj_xt.xt_assessment_before_dislysis as x on x.user_org_id = u.id JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ?",to,ctime).Scan(&patientinfo).Error
	rows, err := readUserDb.Offset(offset).Limit(limit).Raw("select Count(1) from sgj_users.sgj_user_org as u join sgj_xt.xt_assessment_before_dislysis as x on x.user_org_id = u.id  where x.created_time >= ? AND x.created_time <= ?", to, ctime).Rows()
	if rows.Next() {
		rows.Scan(&total)
	}

	return patientinfo,total,err
}

 //零食医嘱
func GetTodayDoctorsOrderInfo(to int64,ctime int64,page int64,limit int64)(patientinfo []function_models.PatientInfo,total int64,err error)  {


	offset := (page - 1) * limit
	err = readUserDb.Raw("select * from sgj_users.sgj_user_org as u join sgj_xt.xt_doctor_advice as x on x.user_org_id = u.id JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ?",to,ctime).Scan(&patientinfo).Error
	rows, err := readUserDb.Offset(offset).Limit(limit).Raw("select Count(1) from sgj_users.sgj_user_org as u join sgj_xt.xt_doctor_advice as x on x.user_org_id = u.id  where x.created_time >= ? AND x.created_time <= ?", to, ctime).Rows()
	if rows.Next() {
		rows.Scan(&total)
	}

	return patientinfo,total,err
}
//双人核对
func GetTodayDoubleCheckInfo(to int64,ctime int64,page int64,limit int64)(patientinfo []function_models.PatientInfo,total int64,err error)  {

	offset := (page - 1) * limit
	err = readUserDb.Raw("select * from sgj_users.sgj_user_org as u join sgj_xt.xt_double_check as x on x.user_org_id = u.id JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ?",to,ctime).Scan(&patientinfo).Error
	rows, err := readUserDb.Offset(offset).Limit(limit).Raw("select Count(1) from sgj_users.sgj_user_org as u join sgj_xt.xt_double_check as x on x.user_org_id = u.id  where x.created_time >= ? AND x.created_time <= ?", to, ctime).Rows()
	if rows.Next() {
		rows.Scan(&total)
	}

	return patientinfo,total,err
}
//透析上机
func GetTodayDialysisMachinek(to int64,ctime int64,page int64,limit int64)(patientinfo []function_models.PatientInfo,total int64,err error)  {

	offset := (page - 1) * limit
	err = readUserDb.Raw("select * from sgj_users.sgj_user_org as u join sgj_xt.xt_dialysis_order as x on x.user_org_id = u.id JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ?",to,ctime).Scan(&patientinfo).Error
	rows, err := readUserDb.Offset(offset).Limit(limit).Raw("select Count(1) from sgj_users.sgj_user_org as u join sgj_xt.xt_dialysis_order as x on x.user_org_id = u.id  where x.created_time >= ? AND x.created_time <= ?", to, ctime).Rows()
	if rows.Next() {
		rows.Scan(&total)
	}

	return patientinfo,total,err
}

//透析检测
func GetTodayDialysisExamination(to int64,ctime int64,page int64,limit int64)(patientinfo []function_models.PatientInfo,total int64,err error) {

	offset := (page - 1) * limit
	err = readUserDb.Raw("select * from sgj_users.sgj_user_org as u join sgj_xt.xt_monitoring_record as x on x.user_org_id = u.id JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ?",to,ctime).Scan(&patientinfo).Error
	rows, err := readUserDb.Offset(offset).Limit(limit).Raw("select Count(1) from sgj_users.sgj_user_org as u join sgj_xt.xt_monitoring_record as x on x.user_org_id = u.id  where x.created_time >= ? AND x.created_time <= ?", to, ctime).Rows()
	if rows.Next() {
		rows.Scan(&total)
	}

	return patientinfo,total,err
}

//透析下机
func GetTodayBeforOrpenetration(to int64,ctime int64,page int64,limit int64)(patientinfo []function_models.PatientInfo,total int64,err error)  {

	offset := (page - 1) * limit
	err = readUserDb.Raw("select * from sgj_users.sgj_user_org as u join sgj_xt.xt_dialysis_order as x on x.user_org_id = u.id JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ?",to,ctime).Scan(&patientinfo).Error
	rows, err := readUserDb.Offset(offset).Limit(limit).Raw("select Count(1) from sgj_users.sgj_user_org as u join sgj_xt.xt_dialysis_order as x on x.user_org_id = u.id  where x.created_time >= ? AND x.created_time <= ?", to, ctime).Rows()
	if rows.Next() {
		rows.Scan(&total)
	}

	return patientinfo,total,err
}

//透后评估
func GetTodayPostAssessment(to int64,ctime int64,page int64,limit int64)(patientinfo []function_models.PatientInfo,total int64,err error)  {
	offset := (page - 1) * limit
	err = readUserDb.Raw("select * from sgj_users.sgj_user_org as u join sgj_xt.xt_assessment_after_dislysis as x on x.user_org_id = u.id JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ?",to,ctime).Scan(&patientinfo).Error
	rows, err := readUserDb.Offset(offset).Limit(limit).Raw("select Count(1) from sgj_users.sgj_user_org as u join sgj_xt.xt_assessment_after_dislysis as x on x.user_org_id = u.id  where x.created_time >= ? AND x.created_time <= ?", to, ctime).Rows()
	if rows.Next() {
		rows.Scan(&total)
	}

	return patientinfo,total,err
}

func GetTodayEntrylist(to int64,ctime int64,limit int64,page int64)(patientinfo []function_models.PatientInfo,total int64, err error)  {
	 offset := (page - 1) * limit
	 err = readUserDb.Raw("select * from sgj_users.sgj_user_org as u join sgj_xt.xt_warehouse as x on x.org_id = u.id JOIN sgj_xt.xt_patients as p On p.id = x.creater where x.warehousing_time >= ? AND x.warehousing_time<= ?",to,ctime).Scan(&patientinfo).Error
	rows, err := readUserDb.Offset(offset).Limit(limit).Raw("select Count(1) from sgj_users.sgj_user_org as u join sgj_xt.xt_warehouse as x on x.org_id = u.id where x.warehousing_time >= ? AND x.warehousing_time <= ?", to, ctime).Rows()
	if rows.Next() {
		rows.Scan(&total)
	}

	return patientinfo,total,err
}

//func GetTodayLogout(apptype int64,oppratetype int64)(userorg []function_models.SgjUserOrg,err error) {
//
//	err = readUserDb.Raw("select * from sgj_users.sgj_user_org as u join sgj_users.sgj_user_admin_login_log as l on l.org_id = u.id where l.operate_type = ? AND l.app_type = ?", oppratetype, apptype).Scan(&userorg).Error
//	return userorg,err
//}