package service

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

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

	//offset := (page - 1) * limit
	if admin.IsAdmin == 1 {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath   from sgj_users.sgj_user_org as u left join sgj_org_follow as f On f.org_id = u.id AND f.status = 1  left JOIN sgj_user_district as d On d.id = u.city left JoIN sgj_admin as a On a.id = f.admin_user_id AND  a.status = 1 left join sgj_xt.xt_patients as x on x.user_org_id = u.id where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1   GROUP BY x.user_org_id", to, ctime).Scan(&patientinfo).Error
	} else {
		err = readUserDb.Raw("select *, a.name as admin_name, d.namepath as namepath  from sgj_users.sgj_user_org as u  join sgj_org_follow as f On f.org_id = u.id AND f.status = 1 AND f.admin_user_id = ? left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1  left JOIN sgj_user_district as d On d.id = u.city left join sgj_xt.xt_patients as x on x.user_org_id = u.id where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1   GROUP BY x.user_org_id", admin.ID, 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)
	//defer rows.Close()
	//if rows.Next() {
	//	rows.Scan(&total)
	//}
	//fmt.Println("&total是什么", total)
	return patientinfo, total, err
}

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

	//offset := (page - 1) * limit

	if admin.IsAdmin == 1 {
		err = readUserDb.Raw("select * , a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city left join sgj_org_follow as f On f.org_id = u.id AND f.status = 1  left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 left join sgj_xt.xt_dialysis_prescription as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", to, ctime).Scan(&patientinfo).Error

	} else {

		err = readUserDb.Raw("select * , a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city  join sgj_org_follow as f On f.org_id = u.id  AND f.admin_user_id = ?  AND f.status = 1  left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 left join sgj_xt.xt_dialysis_prescription as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", admin.ID, 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()
	//defer rows.Close()
	//
	//if rows.Next() {
	//	rows.Scan(&total)
	//}

	return patientinfo, total, err
}

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

	//offset := (page - 1) * limit
	if admin.IsAdmin == 1 {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city  left join sgj_org_follow as f On f.org_id = u.id  AND f.status = 1 left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 left join sgj_xt.xt_receive_treatment_asses as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", to, ctime).Scan(&patientinfo).Error

	} else {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city   join sgj_org_follow as f On f.org_id = u.id  AND f.status = 1  AND f.admin_user_id = ? left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 left join sgj_xt.xt_receive_treatment_asses as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", admin.ID, 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()
	//defer rows.Close()
	//
	//if rows.Next() {
	//	rows.Scan(&total)
	//}

	return patientinfo, total, err
}

// 透前评估
func GetTodayBeforDislysisInfo(to int64, ctime int64, page int64, limit int64, admin *admin_models.AdminAccount) (patientinfo []function_models.PatientInfo, total int64, err error) {
	//offset := (page - 1) * limit
	if admin.IsAdmin == 1 {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city left join sgj_org_follow as f On f.org_id = u.id left AND f.status = 1  JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 left join sgj_xt.xt_assessment_before_dislysis as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", to, ctime).Scan(&patientinfo).Error

	} else {

		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city  join sgj_org_follow as f On f.org_id = u.id  AND  f.admin_user_id = ? AND f.status = 1  left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 left join sgj_xt.xt_assessment_before_dislysis as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ?  AND u.import <> 1 GROUP BY x.user_org_id", admin.ID, 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()
	//defer rows.Close()
	//
	//if rows.Next() {
	//	rows.Scan(&total)
	//}

	return patientinfo, total, err
}

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

	//offset := (page - 1) * limit

	if admin.IsAdmin == 1 {

		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city left join sgj_org_follow as f On f.org_id = u.id  AND f.status = 1 left JoIN sgj_admin as a On a.id = f.admin_user_id  AND a.status = 1 join sgj_xt.xt_doctor_advice as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ?  AND u.import <> 1 GROUP BY x.user_org_id", to, ctime).Scan(&patientinfo).Error

	} else {

		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city  join sgj_org_follow as f On f.org_id = u.id AND f.admin_user_id = ? AND f.status = 1    left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 join sgj_xt.xt_doctor_advice as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", admin.ID, 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()
	//defer rows.Close()
	//
	//if rows.Next() {
	//	rows.Scan(&total)
	//}

	return patientinfo, total, err
}

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

	//offset := (page - 1) * limit
	if admin.IsAdmin == 1 {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city left join sgj_org_follow as f On f.org_id = u.id AND f.status = 1  left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 join sgj_xt.xt_double_check as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", to, ctime).Scan(&patientinfo).Error

	} else {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city  join sgj_org_follow as f On f.org_id = u.id AND f.admin_user_id = ?  AND f.status = 1  left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 join sgj_xt.xt_double_check as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", admin.ID, 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()
	//defer rows.Close()
	//
	//if rows.Next() {
	//	rows.Scan(&total)
	//}

	return patientinfo, total, err
}

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

	//offset := (page - 1) * limit
	if admin.IsAdmin == 1 {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city left join sgj_org_follow as f On f.org_id = u.id  AND f.status = 1  left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 left join sgj_xt.xt_dialysis_order as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", to, ctime).Scan(&patientinfo).Error

	} else {

		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u  left JOIN sgj_user_district as d On d.id = u.city join sgj_org_follow as f On f.org_id = u.id AND f.admin_user_id = ? AND f.status = 1    left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 left join sgj_xt.xt_dialysis_order as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", admin.ID, 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()
	//defer rows.Close()
	//
	//if rows.Next() {
	//	rows.Scan(&total)
	//}

	return patientinfo, total, err
}

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

	//offset := (page - 1) * limit
	if admin.IsAdmin == 1 {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city left join sgj_org_follow as f On f.org_id = u.id AND f.status = 1  left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 join sgj_xt.xt_monitoring_record as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", to, ctime).Scan(&patientinfo).Error

	} else {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city join sgj_org_follow as f On f.org_id = u.id  AND f.admin_user_id = ? AND f.status = 1   left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 join sgj_xt.xt_monitoring_record as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", admin.ID, 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()
	//defer rows.Close()
	//
	//if rows.Next() {
	//	rows.Scan(&total)
	//}

	return patientinfo, total, err
}

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

	//offset := (page - 1) * limit
	if admin.IsAdmin == 1 {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city left join sgj_org_follow as f On f.org_id = u.id AND f.status = 1  left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 join sgj_xt.xt_dialysis_order as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", to, ctime).Scan(&patientinfo).Error

	} else {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city  join sgj_org_follow as f On f.org_id = u.id AND  f.admin_user_id = ?  AND f.status = 1 left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 join sgj_xt.xt_dialysis_order as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", admin.ID, 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()
	//defer rows.Close()
	//
	//if rows.Next() {
	//	rows.Scan(&total)
	//}

	return patientinfo, total, err
}

// 透后评估
func GetTodayPostAssessment(to int64, ctime int64, page int64, limit int64, admin *admin_models.AdminAccount) (patientinfo []function_models.PatientInfo, total int64, err error) {
	//offset := (page - 1) * limit
	if admin.IsAdmin == 1 {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city left join sgj_org_follow as f On f.org_id = u.id  AND f.status = 1  left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 join sgj_xt.xt_assessment_after_dislysis as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", to, ctime).Scan(&patientinfo).Error

	} else {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u  left JOIN sgj_user_district as d On d.id = u.city join sgj_org_follow as f On f.org_id = u.id AND f.admin_user_id = ? AND f.status = 1  left JoIN sgj_admin as a On a.id = f.admin_user_id AND a.status = 1 join sgj_xt.xt_assessment_after_dislysis as x on x.user_org_id = u.id left JOIN sgj_xt.xt_patients as p On p.id = x.patient_id  where x.created_time >= ? AND x.created_time <= ? AND u.import <> 1  GROUP BY x.user_org_id", admin.ID, 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()
	//defer rows.Close()
	//
	//if rows.Next() {
	//	rows.Scan(&total)
	//}

	return patientinfo, total, err
}

func GetTodayEntrylist(to int64, ctime int64, limit int64, page int64, admin *admin_models.AdminAccount) (patientinfo []function_models.PatientInfo, total int64, err error) {
	//offset := (page - 1) * limit
	if admin.IsAdmin == 1 {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city  join sgj_xt.xt_warehouse as x on x.org_id = u.id   LEFT JOIN  sgj_users.sgj_org_follow as f ON f.org_id = u.id AND f.status = 1   LEFT JOIN   sgj_users.sgj_admin as a on a.id = f.admin_user_id AND a.status = 1  where x.ctime >= ? AND x.ctime<= ? AND u.import <> 1  GROUP BY x.org_id", to, ctime).Scan(&patientinfo).Error

	} else {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city  join sgj_xt.xt_warehouse as x on x.org_id = u.id  JOIN  sgj_users.sgj_org_follow as f ON f.org_id = u.id AND f.admin_user_id = ? AND f.status = 1   LEFT JOIN   sgj_users.sgj_admin as a on a.id = f.admin_user_id AND a.status = 1  where x.ctime >= ? AND x.ctime<= ? AND u.import <> 1  GROUP BY x.org_id", admin.ID, 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.ctime >= ? AND x.ctime <= ?", to, ctime).Rows()
	//defer rows.Close()
	//
	//if rows.Next() {
	//	rows.Scan(&total)
	//}

	return patientinfo, total, err
}

func GetTodayExitlist(to int64, ctime int64, limit int64, page int64, admin *admin_models.AdminAccount) (patientinfo []function_models.PatientInfo, total int64, err error) {
	//offset := (page - 1) * limit
	if admin.IsAdmin == 1 {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u  left JOIN sgj_user_district as d On d.id = u.city join sgj_xt.xt_warehouse_out as x on x.org_id = u.id  LEFT JOIN  sgj_users.sgj_org_follow as f ON f.org_id = u.id  AND f.status = 1  LEFT JOIN   sgj_users.sgj_admin as a on a.id = f.admin_user_id AND a.status = 1   where x.ctime >= ? AND x.ctime<= ? AND u.import <> 1  GROUP BY x.org_id", to, ctime).Scan(&patientinfo).Error

	} else {
		err = readUserDb.Raw("select *, a.name as admin_name,  d.namepath as namepath  from sgj_users.sgj_user_org as u left JOIN sgj_user_district as d On d.id = u.city  join sgj_xt.xt_warehouse_out as x on x.org_id = u.id   JOIN  sgj_users.sgj_org_follow as f ON f.org_id = u.id  AND f.admin_user_id = ? AND f.status = 1    LEFT JOIN   sgj_users.sgj_admin as a on a.id = f.admin_user_id  AND a.status = 1  where x.ctime >= ? AND x.ctime<= ? AND u.import <> 1  GROUP BY x.org_id", admin.ID, 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_out as x on x.org_id = u.id where x.ctime >= ? AND x.ctime <= ?", to, ctime).Rows()
	//defer rows.Close()
	//
	//if rows.Next() {
	//	rows.Scan(&total)
	//}

	return patientinfo, total, err
}
