package service

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

func GetBatchDrugHisPrescription(ids []string, record_time int64, user_org_id int64) (patients []*models.PrintPatient, err error) {
	readDb.Model(&models.PrintPatient{}).Preload("HisPrintPatient", "status = 1 AND record_date = ?", record_time).
		Preload("HisPrintPrescription", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND type = 1 AND p_type = 2", user_org_id, record_time).
				Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
					return db.Where("status = 1 AND user_org_id = ?", user_org_id).Preload("Drug", "status=1")
				})
		}).
		Preload("HisPrescriptionInfoTwo", "status = 1 AND record_date = ?", record_time).
		Where("id in (?) AND user_org_id = ?", ids, user_org_id).Find(&patients)
	return
}

func GetBatchProjectHisPrescription(ids []string, record_time int64, user_org_id int64) (patients []*models.PrintPatient, err error) {
	readDb.Model(&models.PrintPatient{}).Preload("HisPrintPatient", "status = 1 AND record_date = ?", record_time).
		Preload("HisPrintPrescription", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND type <> 1 AND p_type = 2", user_org_id, record_time).
				Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
					return db.Where("his_prescription_project.status = 1 AND his_prescription_project.user_org_id = ?", user_org_id).Where("his_prescription_project.status = 1 ").Preload("HisProject", "status = 1").Preload("GoodInfo", "status=1")
				})
		}).
		Preload("HisPrescriptionInfoTwo", "status = 1 AND record_date = ?", record_time).
		Where("id in (?) AND user_org_id = ?  ", ids, user_org_id).Find(&patients)
	return

}

func GetBatchInspectionProjectHisPrescription(ids []string, record_time int64, user_org_id int64) (patients []*models.PrintPatient, err error) {
	readDb.Model(&models.PrintPatient{}).Preload("HisPrintPatient", "status = 1 AND record_date = ?", record_time).
		Preload("HisPrintPrescription", func(db *gorm.DB) *gorm.DB {
			return db.Where("status = 1 AND user_org_id = ? AND record_date = ? AND type <> 1 AND p_type = 2", user_org_id, record_time).
				Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
					return db.Joins("Join xt_his_project p On p.id = his_prescription_project.project_id  AND p.cost_classify = 3").Where("his_prescription_project.status = 1 ").Preload("XtHisProjectTeam", "status = 1").Preload("HisProject", "status = 1")
				})
		}).
		Preload("HisPrescriptionInfoTwo", "status = 1 AND record_date = ?", record_time).
		Where("id in (?) AND user_org_id = ?  ", ids, user_org_id).Find(&patients)
	return

}
