package service

import (
	"XT_New/models"
	"github.com/jinzhu/gorm"
)

type HospitalPatient struct {
	ID                     int64                         `gorm:"column:id" json:"id" form:"id"`
	UserOrgId              int64                         `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
	Name                   string                        `gorm:"column:name" json:"name" form:"name"`
	Status                 int64                         `gorm:"column:status" json:"status" form:"status"`
	IdCardNo               string                        `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
	ScheduleType           int64                         `gorm:"column:schedule_type" json:"schedule_type" form:"schedule_type"`
	Schedule               Schedule                      `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"schedule"`
	HisHospitalCheckRecord models.HisHospitalCheckRecord `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"his_patient"`
	VMHisPrescriptionInfo  VMHisPrescriptionInfo         `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"info"`
	HisHospitalOrder       models.HisHospitalOrder       `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"order"`
}

func (HospitalPatient) TableName() string {
	return "xt_patients"
}

type HospitalPatientTWo struct {
	ID        int64  `gorm:"column:id" json:"id" form:"id"`
	UserOrgId int64  `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
	Name      string `gorm:"column:name" json:"name" form:"name"`
	Status    int64  `gorm:"column:status" json:"status" form:"status"`
	IdCardNo  string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
}

func (HospitalPatientTWo) TableName() string {
	return "xt_patients"
}

type HisHospitalOrder struct {
	ID                     int64                         `gorm:"column:id" json:"id" form:"id"`
	UserOrgId              int64                         `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
	HisPatientId           int64                         `gorm:"column:his_patient_id" json:"his_patient_id" form:"his_patient_id"`
	SettleAccountsDate     int64                         `gorm:"column:settle_accounts_date" json:"settle_accounts_date" form:"settle_accounts_date"`
	Ctime                  int64                         `gorm:"column:ctime" json:"ctime" form:"ctime"`
	Mtime                  int64                         `gorm:"column:mtime" json:"mtime" form:"mtime"`
	Status                 int64                         `gorm:"column:status" json:"status" form:"status"`
	Number                 string                        `gorm:"column:number" json:"number" form:"number"`
	PatientId              int64                         `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
	MedfeeSumamt           float64                       `gorm:"column:medfee_sumamt" json:"medfee_sumamt" form:"medfee_sumamt"`
	OrderStatus            float64                       `gorm:"column:order_status" json:"order_status" form:"order_status"`
	PayWay                 int64                         `gorm:"column:pay_way" json:"pay_way" form:"pay_way"`
	PayPrice               float64                       `gorm:"column:pay_price" json:"pay_price" form:"pay_price"`
	PayCardNo              string                        `gorm:"column:pay_card_no" json:"pay_card_no" form:"pay_card_no"`
	DiscountPrice          float64                       `gorm:"column:discount_price" json:"discount_price" form:"discount_price"`
	PreferentialPrice      float64                       `gorm:"column:preferential_price" json:"preferential_price" form:"preferential_price"`
	RealityPrice           float64                       `gorm:"column:reality_price" json:"reality_price" form:"reality_price"`
	FoundPrice             float64                       `gorm:"column:found_price" json:"found_price" form:"found_price"`
	MedicalInsurancePrice  float64                       `gorm:"column:medical_insurance_price" json:"medical_insurance_price" form:"medical_insurance_price"`
	PrivatePrice           float64                       `gorm:"column:private_price" json:"private_price" form:"private_price"`
	Creator                int64                         `gorm:"column:creator" json:"creator" form:"creator"`
	MdtrtId                string                        `gorm:"column:mdtrt_id" json:"mdtrt_id" form:"mdtrt_id"`
	MzNumber               string                        `gorm:"column:mz_number" json:"mz_number" form:"mz_number"`
	FaPiaoCode             string                        `gorm:"column:fa_piao_code" json:"fa_piao_code" form:"fa_piao_code"`
	FaPiaoNumber           string                        `gorm:"column:fa_piao_number" json:"fa_piao_number" form:"fa_piao_number"`
	IsPre                  int64                         `gorm:"column:is_pre" json:"is_pre" form:"is_pre"`
	Diagnosis              string                        `gorm:"column:diagnosis" json:"diagnosis" form:"diagnosis"`
	Patient                models.Patients               `gorm:"ForeignKey:ID;AssociationForeignKey:PatientId" json:"patient"`
	HisHospitalCheckRecord models.HisHospitalCheckRecord `gorm:"ForeignKey:Number;AssociationForeignKey:MdtrtId" json:"his_patient"`
}

func (HisHospitalOrder) TableName() string {
	return "his_order"
}

func GetHisHospitalPatientList(org_id int64, record_date int64) (patients []*HospitalPatient, err error) {
	db := readDb.Model(&HospitalPatient{}).Where("xt_patients.user_org_id = ? AND xt_patients.status = 1", org_id)
	db = db.Joins("join his_hospital_check_record as record ON record.patient_id = xt_patients.id AND record.status = 1 AND record.user_org_id = ? AND record.in_hospital_status = 1 AND record.record_date = ? ", org_id, record_date)
	db = db.Preload("HisHospitalCheckRecord", func(db *gorm.DB) *gorm.DB {
		return db.Where("user_org_id = ? AND status = 1  AND in_hospital_status = 1 AND out_hospital_status = 0", org_id).Order("id desc")
	})
	err = db.Preload("VMHisPrescriptionInfo", "user_org_id = ? AND status = 1 AND record_date = ? AND p_type = 1", org_id, record_date).Group("id").Find(&patients).Error
	return
}

func GetHisHospitalSchPatientList(org_id int64, record_date int64, sch_type int64) (patients []*HospitalPatient, err error) {
	if sch_type > 0 {
		db := readDb.Model(&HospitalPatient{}).Where("xt_patients.user_org_id = ? AND xt_patients.status = 1", org_id)
		db = db.Joins("join xt_schedule as sch ON sch.patient_id = xt_patients.id AND sch.status = 1 AND sch.user_org_id = ? AND sch.schedule_date = ? AND sch.schedule_type = ? ", org_id, record_date, sch_type)
		db = db.Preload("HisHospitalCheckRecord", func(db *gorm.DB) *gorm.DB {
			return db.Where("user_org_id = ? AND status = 1  AND in_hospital_status = 1 AND out_hospital_status = 0", org_id).Order("id desc")
		})
		err = db.Preload("VMHisPrescriptionInfo", "user_org_id = ? AND status = 1 AND record_date = ? AND p_type = 1", org_id, record_date).Group("id").Find(&patients).Error
		return

	} else {
		db := readDb.Model(&HospitalPatient{}).Where("xt_patients.user_org_id = ? AND xt_patients.status = 1", org_id)
		db = db.Joins("join xt_schedule as sch ON sch.patient_id = xt_patients.id AND sch.status = 1 AND sch.user_org_id = ? AND sch.schedule_date = ?  ", org_id, record_date)
		db = db.Preload("HisHospitalCheckRecord", func(db *gorm.DB) *gorm.DB {
			return db.Where("user_org_id = ? AND status = 1  AND in_hospital_status = 1 AND out_hospital_status = 0", org_id).Order("id desc")
		})
		err = db.Preload("VMHisPrescriptionInfo", "user_org_id = ? AND status = 1 AND record_date = ? AND p_type = 1", org_id, record_date).Group("id").Find(&patients).Error
		return

	}

}

func GetNewAllUnChargeHisHospitalPatientList(org_id int64, record_date int64) (patients []*models.HisHospitalCheckRecord, err error) {
	db := readDb.Model(&models.HisHospitalCheckRecord{}).Where("his_hospital_check_record.user_org_id = ? AND his_hospital_check_record.status = 1 AND his_hospital_check_record.in_hospital_status = 1", org_id)
	db = db.Preload("HisHospitalOrder", "user_org_id = ? AND status = 1", org_id)
	err = db.Group("number").Find(&patients).Error
	return
}

func GetNewAllChargeHisHospitalPatientList(org_id int64, record_date int64) (orders []*HisHospitalOrder, err error) {
	db := readDb.Model(&HisHospitalOrder{}).Where("his_order.user_org_id = ? AND his_order.status = 1 AND his_order.settle_accounts_date = ? AND his_order.p_type = 1 AND his_order.order_status = 2", org_id, record_date)
	db = db.Preload("Patient", "status = 1")
	db = db.Preload("HisHospitalCheckRecord", func(db *gorm.DB) *gorm.DB {
		return db.Where("status = 1 AND user_org_id = ? AND in_hospital_status = 1", org_id)
	})
	err = db.Find(&orders).Error
	return
}

func GetNewHisHospitalPatientInfo(his_patient_id int64) (info models.HisHospitalCheckRecord, err error) {
	err = readDb.Model(&models.HisHospitalCheckRecord{}).Where("id = ? AND status = 1 ", his_patient_id).First(&info).Error
	return
}

func GetUnChargeHisHospitalPrescriptionFive(org_id int64, patient_id int64, his_patient_id int64, record_date int64) (prescription []*models.HisPrescription, err error) {
	err = readDb.Model(&models.HisPrescription{}).
		Preload("HisAdditionalCharge", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("XtHisAddtionConfig", "status=1")
		}).
		Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("Drug", "status=1")
		}).
		Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1").Preload("XtHisProjectTeam", "status = 1")
		}).
		Where("user_org_id = ? AND status = 1 AND record_date = ? AND patient_id = ? AND order_status <> 2 AND order_status <> 3", org_id, record_date, patient_id).
		Find(&prescription).Error
	return
}

func GetChargeHisHospitalPrescriptionFive(org_id int64, patient_id int64, his_patient_id int64, record_date int64) (prescription []*models.HisPrescription, err error) {
	err = readDb.Model(&models.HisPrescription{}).
		Preload("HisAdditionalCharge", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("XtHisAddtionConfig", "status=1")
		}).
		Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("Drug", "status=1")
		}).
		Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1").Preload("XtHisProjectTeam", "status = 1")
		}).
		Where("user_org_id = ? AND status = 1 AND record_date = ? AND patient_id = ? AND order_status = 2 ", org_id, record_date, patient_id).
		Find(&prescription).Error
	return
}

//
type VMHospitalPatient struct {
	ID                     int64                         `gorm:"column:id" json:"id" form:"id"`
	UserOrgId              int64                         `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
	Name                   string                        `gorm:"column:name" json:"name" form:"name"`
	Status                 int64                         `gorm:"column:status" json:"status" form:"status"`
	IdCardNo               string                        `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
	ScheduleType           int64                         `gorm:"column:schedule_type" json:"schedule_type" form:"schedule_type"`
	Schedule               Schedule                      `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"schedule"`
	HisHospitalCheckRecord models.HisHospitalCheckRecord `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"his_patient"`
	VMHisPrescriptionInfo  VMHisPrescriptionInfo         `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"info"`
}

func (VMHospitalPatient) TableName() string {
	return "xt_patients"
}

//
//
//func GetHisHospitalPatientPrescriptionList(org_id int64, record_date int64,page int64, limit int64) (patients []*HospitalPatient, err error) {
//	offset := (page - 1) * limit
//	db := readDb.Model(&HospitalPatient{}).Where("xt_patients.user_org_id = ? AND xt_patients.status = 1", org_id)
//	db = db.Joins("join his_hospital_check_record as record ON record.patient_id = xt_patients.id AND record.status = 1 AND record.user_org_id = ?", org_id)
//	db = db.Preload("HisHospitalCheckRecord", func(db *gorm.DB) *gorm.DB {
//		return db.Where("user_org_id = ? AND status = 1  AND in_hospital_status = 1 AND out_hospital_status = 0", org_id)
//	})
//	err = db.Preload("VMHisPrescriptionInfo", "user_org_id = ? AND status = 1 AND record_date = ? AND p_type = 1", org_id, record_date).Group("id").Find(&patients).Error
//	return
//}

//
//func GetHisHospitalPatientPrescriptionList(org_id int64, keywords string, record_date int64, page int64, limit int64) (patients []*VMSchedule, err error, total int64) {
//	offset := (page - 1) * limit
//	db := readDb.Model(&VMHospitalPatient{}).Where("user_org_id = ? AND status = 1", org_id)
//	if len(keywords) > 0 {
//	wkkk	keywords = "%" + keywords + "%"
//		db = db.Joins("JOIN xt_patients as p On xt_schedule.patient_id = p.id AND p.user_org_id = ? AND p.name like ?", org_id, keywords)
//		db = db.Joins("JOIN his_prescription_info as info On xt_schedule.patient_id = info.patient_id  AND info.user_org_id = ? AND  info.record_date = ? AND info.prescription_number like ? AND info.p_type == 1", org_id, record_date, keywords)
//	}
//	db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
//	db = db.Preload("HisPatient", "user_org_id = ? AND status = 1", org_id)
//	db = db.Preload("HisPrescription", "user_org_id = ? AND status = 1 AND p_type == 1", org_id).
//		Preload("VMHisPrescriptionInfo", "user_org_id = ? AND status = 1 AND p_type == 1", org_id)
//	db = db.Count(&total)
//	err = db.Limit(limit).Offset(offset).Find(&patients).Error
//	return
//}

type VMHisHospitalPrescriptionInfo struct {
	ID                 int64              `gorm:"column:id" json:"id" form:"id"`
	UserOrgId          int64              `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
	RecordDate         int64              `gorm:"column:record_date" json:"record_date" form:"record_date"`
	PatientId          int64              `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
	HisPatientId       int64              `gorm:"column:his_patient_id" json:"his_patient_id" form:"his_patient_id"`
	Status             int64              `gorm:"column:status" json:"status" form:"status"`
	Ctime              int64              `gorm:"column:ctime" json:"ctime" form:"ctime"`
	Mtime              int64              `gorm:"column:mtime" json:"mtime" form:"mtime"`
	Creator            int64              `gorm:"column:creator" json:"creator" form:"creator"`
	Modifier           int64              `gorm:"column:modifier" json:"modifier" form:"modifier"`
	Diagnosis          string             `gorm:"column:diagnosis" json:"diagnosis" form:"diagnosis"`
	RegisterType       int64              `gorm:"column:register_type" json:"register_type" form:"register_type"`
	Doctor             string             `gorm:"column:doctor" json:"doctor" form:"doctor"`
	Departments        string             `gorm:"column:departments" json:"departments" form:"departments"`
	SickHistory        string             `gorm:"column:sick_history" json:"sick_history" form:"sick_history"`
	PrescriptionNumber string             `gorm:"column:prescription_number" json:"prescription_number" form:"prescription_number"`
	BatchNumber        string             `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
	PrescriptionStatus int64              `gorm:"column:prescription_status" json:"prescription_status" form:"prescription_status"`
	DoctorId           int64              `gorm:"column:doctor_id" json:"doctor_id" form:"doctor_id"`
	DepartmentId       int64              `gorm:"column:department_id" json:"department_id" form:"department_id"`
	SickType           int64              `gorm:"column:sick_type" json:"sick_type" form:"sick_type"`
	Patients           Patients           `gorm:"ForeignKey:ID;AssociationForeignKey:PatientId" json:"patients"`
	HisPrescription    []*HisPrescription `gorm:"ForeignKey:PatientId,RecordDate;AssociationForeignKey:PatientId,RecordDate" json:"prescription"`
}

func (VMHisHospitalPrescriptionInfo) TableName() string {
	return "his_prescription_info"
}

func GetHisHospitalPatientPrescriptionList(org_id int64, keywords string, record_date int64, page int64, limit int64) (patients []*VMHisHospitalPrescriptionInfo, err error, total int64) {
	offset := (page - 1) * limit
	db := readDb.Model(&VMHisHospitalPrescriptionInfo{}).Where("his_prescription_info.user_org_id = ? AND his_prescription_info.status = 1 AND his_prescription_info.record_date = ? AND his_prescription_info.p_type = 1 ", org_id, record_date)
	if len(keywords) > 0 {
		keywords = "%" + keywords + "%"
		db = db.Joins("JOIN xt_patients as p On his_prescription_info.patient_id = p.id AND p.user_org_id = ? AND p.name like ?", org_id, keywords)
	}
	db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
	db = db.Preload("HisPrescription", "user_org_id = ? AND status = 1 AND p_type = 1", org_id)
	db = db.Count(&total)
	err = db.Limit(limit).Offset(offset).Find(&patients).Error
	return
}

func CreateHospitalRecord(record *models.HisHospitalCheckRecord) (err error) {
	err = writeDb.Save(&record).Error
	return
}

func GetInHospitalRecord(id int64) (record models.HisHospitalCheckRecord, err error) {
	err = readDb.Model(&models.HisHospitalCheckRecord{}).Where("id = ? AND status = 1", id).First(&record).Error
	return
}

func GetInHospitalRecordByNumber(number string) (record models.HisHospitalCheckRecord, err error) {
	err = readDb.Model(&models.HisHospitalCheckRecord{}).Where("number = ? AND status = 1", number).First(&record).Error
	return
}

func GetLastHospitalRecord(id int64, user_org_id int64) (record models.HisHospitalCheckRecord, err error) {
	err = readDb.Model(&models.HisHospitalCheckRecord{}).Where("id = ? AND status = 1 AND user_org_id = ?", id, user_org_id).Last(&record).Error
	return
}

func GetLastHospitalRecordTwo(patient_id int64, user_org_id int64) (record models.HisHospitalCheckRecord, err error) {
	err = readDb.Model(&models.HisHospitalCheckRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ? AND in_hospital_status = 1 AND out_hospital_status = 0", patient_id, user_org_id).Last(&record).Error
	return
}

func FindDiagnoseByIds(ids []string) ([]*models.HisXtDiagnoseConfig, error) {
	dealer := []*models.HisXtDiagnoseConfig{}
	err := readDb.Model(&models.HisXtDiagnoseConfig{}).Where("id IN (?) AND status = 1", ids).Find(&dealer).Error
	return dealer, err
}

func FindSickById(id int64) (*models.OutpatientServiceSick, error) {
	outPatient := &models.OutpatientServiceSick{}
	err := readDb.Model(&models.OutpatientServiceSick{}).Where("id = ? AND status = 1", id).First(&outPatient).Error
	return outPatient, err
}

func GetHospitalMonthHisPrescription(org_id int64, patient_id int64, start_time int64, end_time int64) (prescription []*models.HisPrescription, err error) {
	err = readDb.Model(&models.HisPrescription{}).
		Preload("HisAdditionalCharge", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("XtHisAddtionConfig", "status=1")
		}).
		Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("Drug", "status=1")
		}).
		Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisProject").Preload("GoodInfo", "status=1")
		}).
		Where("user_org_id = ? AND record_date >= ? AND record_date <= ? AND patient_id = ? AND order_status <> 2 AND status = 1 AND p_type = 1 ", org_id, start_time, end_time, patient_id).
		Find(&prescription).Error
	return
}

func GetSettleHisHospitalPrescription(org_id int64, patient_id int64, his_patient_id int64, record_date int64) (prescription []*models.HisPrescription, err error) {
	err = readDb.Model(&models.HisPrescription{}).
		Preload("HisAdditionalCharge", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("XtHisAddtionConfig", "status=1")
		}).
		Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("Drug", "status=1")
		}).
		Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1").Preload("XtHisProjectTeam", "status = 1")
		}).
		Where("user_org_id = ? AND status = 1 AND record_date >= ? AND patient_id = ? AND order_status = 4 ", org_id, record_date, patient_id).
		Find(&prescription).Error
	return
}

func GetHisHospitalUnUploadPrescriptionDetailPatientList(org_id int64, record_date int64) (patients []*models.HisHospitalCheckRecord, err error) {
	db := readDb.Model(&models.HisHospitalCheckRecord{})
	db = db.Joins("inner join his_prescription pre On pre.patient_id = his_hospital_check_record.patient_id and pre.record_date = ? AND pre.p_type = 1 AND pre.user_org_id = ? AND pre.order_status <> 4 AND pre.order_status <> 2", record_date, org_id)
	db = db.Where("his_hospital_check_record.user_org_id = ? AND his_hospital_check_record.status = 1 AND his_hospital_check_record.in_hospital_status = 1", org_id)
	err = db.Group("number").Find(&patients).Error
	return
}

func GetHisHospitalUploadPrescriptionDetailPatientList(org_id int64, record_date int64) (patients []*models.HisHospitalCheckRecord, err error) {
	db := readDb.Model(&models.HisHospitalCheckRecord{})
	db = db.Joins("inner join his_prescription pre On pre.patient_id = his_hospital_check_record.patient_id and pre.record_date = ? AND pre.p_type = 1 AND pre.user_org_id = ? AND pre.order_status = 4", record_date, org_id)
	db = db.Where("his_hospital_check_record.user_org_id = ? AND his_hospital_check_record.status = 1 AND his_hospital_check_record.in_hospital_status = 1", org_id)
	err = db.Group("number").Find(&patients).Error
	return
}

func GetUnUploadHisHospitalPrescription(org_id int64, patient_id int64, record_date int64) (prescription []*models.HisPrescription, err error) {
	err = readDb.Model(&models.HisPrescription{}).
		Preload("HisAdditionalCharge", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("XtHisAddtionConfig", "status=1")
		}).
		Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("Drug", "status=1")
		}).
		Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1").Preload("XtHisProjectTeam", "status = 1")
		}).
		Where("user_org_id = ? AND status = 1 AND record_date = ? AND patient_id = ? AND order_status <> 2 AND order_status <> 3 AND order_status<> 4 AND p_type = 1", org_id, record_date, patient_id).
		Find(&prescription).Error
	return
}

func GetUploadHisHospitalPrescription(org_id int64, patient_id int64, record_date int64) (prescription []*models.HisPrescription, err error) {
	err = readDb.Model(&models.HisPrescription{}).
		Preload("HisAdditionalCharge", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("XtHisAddtionConfig", "status=1")
		}).
		Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("Drug", "status=1")
		}).
		Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1").Preload("XtHisProjectTeam", "status = 1")
		}).
		Where("user_org_id = ? AND status = 1 AND record_date = ? AND patient_id = ? AND  order_status  <> 0 AND order_status <> 1 AND p_type = 1", org_id, record_date, patient_id).
		Find(&prescription).Error
	return
}

func GetMonthUnUploadHisHospitalPrescription(org_id int64, patient_id int64, start_time int64, end_time int64) (prescription []*models.HisPrescription, err error) {
	err = readDb.Model(&models.HisPrescription{}).
		Preload("HisAdditionalCharge", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("XtHisAddtionConfig", "status=1")
		}).
		Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("Drug", "status=1")
		}).
		Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1").Preload("XtHisProjectTeam", "status = 1")
		}).
		Where("user_org_id = ? AND status = 1 AND patient_id = ? AND  record_date <= ? AND record_date >= ?  AND order_status <> 2 AND order_status <> 3 AND order_status<> 4 AND p_type = 1", org_id, patient_id, end_time, start_time).
		Find(&prescription).Error
	return
}

func GetMonthUploadHisHospitalPrescription(org_id int64, patient_id int64, start_time int64, end_time int64) (prescription []*models.HisPrescription, err error) {
	err = readDb.Model(&models.HisPrescription{}).
		Preload("HisAdditionalCharge", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("XtHisAddtionConfig", "status=1")
		}).
		Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("Drug", "status=1")
		}).
		Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1").Preload("XtHisProjectTeam", "status = 1")
		}).
		Where("user_org_id = ? AND status = 1 AND patient_id = ? AND record_date <= ? AND  record_date >= ?  AND  order_status  <> 0 AND order_status <> 1 AND p_type = 1", org_id, patient_id, end_time, start_time).
		Find(&prescription).Error
	return
}

func GetAllMonthHisHospitalPrescription(org_id int64, patient_id int64, start_time int64, end_time int64) (prescription []*models.HisPrescription, err error) {
	err = readDb.Model(&models.HisPrescription{}).
		Preload("HisAdditionalCharge", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("XtHisAddtionConfig", "status=1")
		}).
		Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("Drug", "status=1")
		}).
		Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ?", org_id).Preload("HisOrderInfo", "status = 1").Preload("HisProject").Preload("GoodInfo", "status=1").Preload("XtHisProjectTeam", "status = 1")
		}).
		Where("user_org_id = ? AND status = 1 AND patient_id = ? AND record_date <= ? AND  record_date >= ? AND p_type = 1", org_id, patient_id, end_time, start_time).
		Find(&prescription).Error
	return
}
