package service

import (
	"XT_Admin_Api/models/function_models"
)

func GetTodayInfo(to int64,ctime int64,page int64,limit int64)(patients []function_models.XtPatients,total int64,err error) {
	db := readDb.Table("xt_patients as p").Where("p.status = 1")
	offset := (page - 1) * limit
	err = db.Where("status <> 0 AND created_time >= ? AND created_time <= ?", to, ctime).
		Count(&total).Order("p.created_time desc").Offset(offset).Limit(limit).
		Select("p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").
	Find(&patients).Error
    return patients,total,err
}

func GetTodayDialysisPatientsInfo(to int64,ctime int64,page int64,limit int64)(patients []function_models.XtPatients,total int64,err error)  {
	db := readDb.Table("xt_patients as p").Where("p.status = 1")
	offset := (page - 1) * limit
	err = db.Select("p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").
		Joins("join xt_dialysis_prescription as x On x.patient_id = p.id").Where("x.created_time >= ? AND x.created_time <= ?", to, ctime).Count(&total).Order("p.created_time desc").Offset(offset).Limit(limit).Find(&patients).Error
   return patients,total,err
}

func GetTodayReceivePatientsInfo(to int64,ctime int64,page int64,limit int64)(patients []function_models.XtPatients,total int64,err error)  {
	db := readDb.Table("xt_patients as p").Where("p.status = 1")
	offset := (page - 1) * limit
	err = db.Select("p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").
		Joins("join xt_receive_treatment_asses as x On x.patient_id = p.id").Where("x.created_time >= ? AND x.created_time <= ?", to, ctime).Count(&total).Order("p.created_time desc").Offset(offset).Limit(limit).Find(&patients).Error
	return patients,total,err
}

func GetTodayDoctorsOrderInfo(to int64,ctime int64,page int64,limit int64)(patients []function_models.XtPatients,total int64,err error)  {
	db := readDb.Table("xt_patients as p").Where("p.status = 1")
	offset := (page - 1) * limit
	err = db.Select("p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").
		Joins("join xt_doctor_advice as x On x.patient_id = p.id").Where("x.created_time >= ? AND x.created_time <= ?", to, ctime).Count(&total).Order("p.created_time desc").Offset(offset).Limit(limit).Find(&patients).Error
	return patients,total,err
}

func GetTodayDoubleCheckInfo(to int64,ctime int64,page int64,limit int64)(patients []function_models.XtPatients,total int64,err error)  {
	db := readDb.Table("xt_patients as p").Where("p.status = 1")
	offset := (page - 1) * limit
	err = db.Select("p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").
		Joins("join xt_double_check as x On x.patient_id = p.id").Where("x.created_time >= ? AND x.created_time <= ?", to, ctime).Count(&total).Order("p.created_time desc").Offset(offset).Limit(limit).Find(&patients).Error
	return patients,total,err
}

func GetTodayDialysisMachinek(to int64,ctime int64,page int64,limit int64)(patients []function_models.XtPatients,total int64,err error)  {
	db := readDb.Table("xt_patients as p").Where("p.status = 1")
	offset := (page - 1) * limit
	err = db.Select("p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").
		Joins("join xt_double_check as x On x.patient_id = p.id").Where("x.created_time >= ? AND x.created_time <= ?", to, ctime).Count(&total).Order("p.created_time desc").Offset(offset).Limit(limit).Find(&patients).Error
	return patients,total,err
}
