package service

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

type VMDialysisSchedule 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"`
	PartitionId  int64 `gorm:"column:partition_id" json:"partition_id" form:"partition_id"`
	BedId        int64 `gorm:"column:bed_id" json:"bed_id" form:"bed_id"`
	PatientId    int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
	ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date" form:"schedule_date"`
	ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type" form:"schedule_type"`
	ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week" form:"schedule_week"`
	ModeId       int64 `gorm:"column:mode_id" json:"mode_id" form:"mode_id"`
	Status       int64 `gorm:"column:status" json:"status" form:"status"`
	CreatedTime  int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
	UpdatedTime  int64 `gorm:"column:updated_time" json:"updated_time" form:"updated_time"`

	MonitorPatients models.MonitorPatients `gorm:"ForeignKey:PatientId" json:"patient"`
	DeviceNumber    models.DeviceNumber    `gorm:"ForeignKey:BedId" json:"device_number"`
	DeviceZone      models.DeviceZone      `gorm:"ForeignKey:PartitionId" json:"device_zone"`
	TreatmentMode   models.TreatmentMode   `gorm:"ForeignKey:ModeId" json:"treatment_mode"`

	DialysisOrder            models.MonitorDialysisOrder  `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
	Prescription             models.DialysisPrescription  `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
	AssessmentBeforeDislysis models.PredialysisEvaluation `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_before_dislysis"`
	AssessmentAfterDislysis  AssessmentAfterDislysis      `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_after_dislysis"`
	MonitoringRecord         []models.MonitoringRecord    `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"monitoring_record"`

	//Patient         string          `gorm:"-" json:"patient" form:"patient"`
	//SchedualPatient SchedualPatient `gorm:"ForeignKey:PatientId"`
	//DeviceNumber    DeviceNumber    `gorm:"ForeignKey:BedId"`
	//DeviceZone      DeviceZone      `gorm:"ForeignKey:PartitionId"`
	//TreatmentMode   TreatmentMode   `gorm:"ForeignKey:ModeId"`
}

func (VMDialysisSchedule) TableName() string {
	return "xt_schedule"
}

type MonitorDialysisSchedule struct {
	ID           int64 `gorm:"column:id" json:"id"`
	UserOrgId    int64 `gorm:"column:user_org_id" json:"user_org_id"`
	PartitionId  int64 `gorm:"column:partition_id" json:"partition_id"`
	BedId        int64 `gorm:"column:bed_id" json:"bed_id"`
	PatientId    int64 `gorm:"column:patient_id" json:"patient_id"`
	ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
	ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
	ModeId       int64 `gorm:"column:mode_id" json:"mode_id"`
	Status       int64 `gorm:"column:status" json:"status"`

	MonitorPatients models.MonitorPatients `gorm:"ForeignKey:PatientId" json:"patient"`
	DeviceNumber    models.DeviceNumber    `gorm:"ForeignKey:BedId" json:"device_number"`
	DeviceZone      models.DeviceZone      `gorm:"ForeignKey:PartitionId" json:"device_zone"`
	TreatmentMode   models.TreatmentMode   `gorm:"ForeignKey:ModeId" json:"treatment_mode"`

	DialysisOrder            models.MonitorDialysisOrder    `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
	Prescription             models.DialysisPrescription    `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
	AssessmentBeforeDislysis models.PredialysisEvaluation   `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_before_dislysis"`
	AssessmentAfterDislysis  models.AssessmentAfterDislysis `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_after_dislysis"`
	MonitoringRecord         []models.MonitoringRecord      `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"monitoring_record"`
}

func (MonitorDialysisSchedule) TableName() string {
	return "xt_schedule"
}

func GetDialysisParametersByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64, schedulDate int64) ([]*VMDialysisSchedule, error, int64) {
	fmt.Println("scheduletye ======================", schedulType)
	var patients []*models.Patients
	getPatientErr := readDb.Model(&models.Patients{}).Where("status = 1 AND user_org_id = ? AND (name like ? OR dialysis_no like ?)", orgID, "%"+keyword+"%", "%"+keyword+"%").Find(&patients).Error
	if getPatientErr != nil {
		return nil, getPatientErr, 0
	}
	patientIDs := make([]int64, len(patients))
	for index, patient := range patients {
		patientIDs[index] = patient.ID
	}

	db := readDb.
		Model(&VMDialysisSchedule{}).
		Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
		// Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
		Preload("TreatmentMode", "status = 1").
		Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, schedulDate).
		Preload("AssessmentBeforeDislysis", "status = 1 AND  user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
		Preload("AssessmentAfterDislysis", "status = 1 AND  user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
		Preload("MonitoringRecord", "status = 1 AND  user_org_id = ? AND monitoring_date = ?", orgID, schedulDate).
		Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, schedulDate).
		Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
	db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
	if schedulType > 0 {
		db = db.Where("schedule_type = ?", schedulType)
	}
	if partitionType > 0 {
		db = db.Joins("inner join xt_device_number as d_n on d_n.id = xt_schedule.bed_id and d_n.zone_id = ? and d_n.status = 1", partitionType)
	}
	if schedulDate > 0 {
		db = db.Where("schedule_date = ?", schedulDate)
	}
	var schedules []*VMDialysisSchedule
	total := int64(0)
	err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
	return schedules, err, total
}

func GetDialysisParameter(orgID int64, schedulDate int64, schedulType int64, partitionType int64, page int64, limit int64) (schedule []*MonitorDialysisSchedule, err error, total int64) {
	db := readDb.
		Model(&MonitorDialysisSchedule{}).
		Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
		Preload("TreatmentMode", "status = 1").
		Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, schedulDate).
		Preload("AssessmentBeforeDislysis", "status = 1 AND  user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
		Preload("AssessmentAfterDislysis", "status = 1 AND  user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
		Preload("MonitoringRecord", "status = 1 AND  user_org_id = ? AND monitoring_date = ?", orgID, schedulDate).
		Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, schedulDate).
		Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
	db = db.Where("xt_schedule.status = 1 AND user_org_id = ?", orgID)
	if schedulDate > 0 {
		db = db.Where("schedule_date = ?", schedulDate)
	}
	if schedulType > 0 {
		db = db.Where("schedule_type = ?", schedulType)
	}
	if partitionType > 0 {
		db = db.Joins("inner join xt_device_number on xt_device_number.id = xt_schedule.bed_id and xt_device_number.zone_id = ? and xt_device_number.status = 1", partitionType)
	}
	offset := (page - 1) * limit
	err = db.Count(&total).Offset(offset).Limit(limit).Order("bed_id desc").Find(&schedule).Error
	return schedule, err, total
}

func GetDialysisBatchParameters(schIDs []string, orgID int64) (schedule []*models.DialysisParameter, err error) {

	db := readDb.
		Model(&models.DialysisSchedule{}).
		Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
		// Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
		Preload("TreatmentMode", "status = 1").
		Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
		Preload("AssessmentBeforeDislysis", "status = 1 AND  user_org_id = ?", orgID).
		Preload("AssessmentAfterDislysis", "status = 1 AND  user_org_id = ?", orgID).
		Preload("MonitoringRecord", "status = 1 AND  user_org_id = ?", orgID).
		Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
		Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
	db = db.Where("xt_schedule.status = 1 AND id in (?)", schIDs)
	var schedules []*models.DialysisParameter
	err = db.Order("schedule_date desc").Find(&schedules).Error
	return schedules, err
}

func GetWareHouseOutList(startime int64, endtime int64, orgid int64) (warehouse []*models.XtWarehouseOutInfo, err error) {

	db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.sys_record_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.sys_record_time<=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.org_id = ?", orgid)
	}
	err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.count,x.warehouse_out_order_number,x.sys_record_time,s.specification_name,t.type_name").Joins("left join xt_good_information as s on s.id = x.good_id").Where("s.status = 1 and s.org_id = ?", orgid).Joins("left join xt_goods_type as t on t.id = x.good_type_id").Order("x.id desc").Scan(&warehouse).Error
	return warehouse, err
}

func GetAllMaterial(startime int64, endtime int64, orgid int64) (reducedetail []*models.XtAutomaticReduceDetail, err error) {

	db := XTReadDB().Table("xt_automatic_reduce_detail as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.record_time>=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.record_time<=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.org_id = ?", orgid)
	}
	err = db.Select("x.patient_id,x.record_time,x.good_id,t.type_name").Joins("left join xt_goods_type as t on t.id = x.good_type_id").Group("x.good_type_id").Scan(&reducedetail).Error
	return reducedetail, err
}

func GetCollectList(limit int64, page int64, partitionType int64, schedulType int64, schedulDate int64, orgID int64, keyword string) ([]*models.DialysisParameter, error, int64) {

	var patients []*models.Patients
	getPatientErr := readDb.Model(&models.Patients{}).Where("status = 1 AND user_org_id = ? AND (name like ? OR dialysis_no like ?)", orgID, "%"+keyword+"%", "%"+keyword+"%").Find(&patients).Error
	if getPatientErr != nil {
		return nil, getPatientErr, 0
	}
	patientIDs := make([]int64, len(patients))
	for index, patient := range patients {
		patientIDs[index] = patient.ID
	}

	db := readDb.
		Model(&models.DialysisSchedule{}).
		Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
		// Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
		Preload("TreatmentMode", "status = 1").
		Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
		Preload("AssessmentBeforeDislysis", "status = 1 AND  user_org_id = ?", orgID).
		Preload("AssessmentAfterDislysis", "status = 1 AND  user_org_id = ?", orgID).
		Preload("MonitoringRecord", "status = 1 AND  user_org_id = ?", orgID).
		Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
		Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
	db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
	if schedulType > 0 {
		db = db.Where("schedule_type = ?", schedulType)
	}
	if partitionType > 0 {
		db = db.Joins("inner join xt_device_number as d_n on d_n.id = xt_schedule.bed_id and d_n.zone_id = ? and d_n.status = 1", partitionType)
		// db = db.Where("partition_id = ?", partitionType)
	}
	if schedulDate > 0 {
		db = db.Where("schedule_date = ?", schedulDate)
	}
	var schedules []*models.DialysisParameter
	total := int64(0)
	err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
	return schedules, err, total
}

func GetDialysisConsumables(startime int64, endtime int64, orgid int64) (reducedetail []*models.XtAutomaticReduceDetail, err error) {

	db := XTReadDB().Table("xt_automatic_reduce_detail as x").Where("x.status = 1")
	table := XTReadDB().Table("xt_warehouse_out_info as f").Where("f.status = 1 and f.org_id = ?", orgid)
	fmt.Println(table)
	if startime > 0 {
		db = db.Where("x.record_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.record_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.org_id =?", orgid)
	}
	err = db.Select("x.patient_id,x.count,x.good_id,x.good_type_id,t.specification_name,s.type_name").Joins("left join xt_good_information as t on t.id = x.good_id").Where("t.org_id = ? and t.status = 1", orgid).Joins("left join xt_goods_type as s on s.id = x.good_type_id").Joins("left join xt_warehouse_out_info as f on f.id = x.warehouse_out_id").Scan(&reducedetail).Error
	return reducedetail, err
}

func GetBatchCollection(orgID int64, schIDs []string) (schedules []*models.DialysisParameter, err error) {

	db := readDb.
		Model(&models.DialysisSchedule{}).
		Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
		// Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
		Preload("TreatmentMode", "status = 1").
		Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
		Preload("AssessmentBeforeDislysis", "status = 1 AND  user_org_id = ?", orgID).
		Preload("AssessmentAfterDislysis", "status = 1 AND  user_org_id = ?", orgID).
		Preload("MonitoringRecord", "status = 1 AND  user_org_id = ?", orgID).
		Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
		Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
	db = db.Where("xt_schedule.status = 1 AND id in (?)", schIDs)
	err = db.Order("schedule_date desc").Find(&schedules).Error
	return schedules, err
}

func GetAnticoagulantCount(startime int64, endtime int64, orgid int64) (prescriptionCount []*models.DialysisPrescriptionCount, err error) {

	db := XTReadDB().Table("xt_dialysis_prescription as x").Where("x.status = 1")
	if orgid > 0 {
		db = db.Where("x.user_org_id = ? and x.anticoagulant <> 0", orgid)
	}
	if startime > 0 {
		db = db.Where("x.record_date >= ?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.record_date<=?", endtime)
	}

	err = db.Select("x.patient_id,x.anticoagulant,x.record_date,count(id) as count").Group("x.anticoagulant").Scan(&prescriptionCount).Error
	return prescriptionCount, err
}

func GetDialysisTodaySchedulePatient(orgid int64, startime int64, page int64, limit int64) (schedule []*models.XtSchedule, err error) {
	offset := (page - 1) * limit
	err = XTReadDB().Model(&schedule).Where("user_org_id = ? and schedule_date = ? and status = 1", orgid, startime).Offset(offset).Limit(limit).Find(&schedule).Error
	return schedule, err
}

func GetToDayDialysisPrescription(patientid int64, orgid int64, startime int64) (*models.XtDialysisPrescription, error) {
	prescription := models.XtDialysisPrescription{}
	err := XTReadDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and record_date = ? and status = 1", patientid, orgid, startime).Find(&prescription).Error
	if err == gorm.ErrRecordNotFound {
		return nil, err
	}
	if err != nil {
		return nil, err
	}
	return &prescription, nil

}

func GetLastDialysisPrescription(patientid int64, orgid int64) (models.XtDialysisPrescription, error) {

	prescription := models.XtDialysisPrescription{}
	err = XTReadDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and status = 1", patientid, orgid).Last(&prescription).Error
	return prescription, err
}

func GetLastAssessmentBeforDialysis(patientid int64, orgid int64) (models.PredialysisEvaluation, error) {

	evaluation := models.PredialysisEvaluation{}
	err := XTReadDB().Model(&evaluation).Where("patient_id = ? and user_org_id = ? and status = 1", patientid, orgid).Last(&evaluation).Error
	return evaluation, err
}

func GetDialysisPrescriptionList(patientid int64, orgid int64, startime int64) (models.XtDialysisPrescription, error) {
	prescription := models.XtDialysisPrescription{}
	err := XTReadDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientid, orgid, startime).Find(&prescription).Error
	return prescription, err
}

func GetTodayAssessmentBeforDialysis(patientid int64, orgid int64, startime int64) (*models.PredialysisEvaluation, error) {

	evaluation := models.PredialysisEvaluation{}
	err := XTReadDB().Model(&evaluation).Where("patient_id = ? and user_org_id = ? and assessment_date = ? and status = 1", patientid, orgid, startime).Find(&evaluation).Error
	if err == gorm.ErrRecordNotFound {
		return nil, err
	}
	if err != nil {
		return nil, err
	}
	return &evaluation, nil
}

func GetAssessmentBeforDialysisByStartime(patientid int64, orgid int64, startime int64) (models.PredialysisEvaluation, error) {
	evaluation := models.PredialysisEvaluation{}
	err := XTReadDB().Model(&evaluation).Where("patient_id = ? and user_org_id = ? and assessment_date = ? and status = 1", patientid, orgid, startime).Find(&evaluation).Error
	return evaluation, err
}

func GetAotoMaticReduceByPatientId(patientid int64, orgid int64, startime int64, endtime int64) (*models.BloodAutomaticReduceDetail, error) {

	detail := models.BloodAutomaticReduceDetail{}
	err := XTReadDB().Model(&detail).Where("patient_id = ? and org_id =? and record_time >=? and record_time <=? and status = 1", patientid, orgid, startime, endtime).Find(&detail).Error
	if err == gorm.ErrRecordNotFound {
		return nil, err
	}
	if err != nil {
		return nil, err
	}
	return &detail, nil
}

func GetMaticReduece(patientid int64, orgid int64) (models.BloodAutomaticReduceDetail, error) {

	detail := models.BloodAutomaticReduceDetail{}
	err := XTReadDB().Model(&detail).Where("patient_id = ? and org_id= ?  and status = 1", patientid, orgid).Last(&detail).Error

	return detail, err
}

func GetMaticeReduceByPatientId(patientid int64, orgid int64, startime int64, endtime int64) (models.BloodAutomaticReduceDetail, error) {

	detail := models.BloodAutomaticReduceDetail{}
	err := XTReadDB().Model(&detail).Where("patient_id = ? and org_id = ? and record_time >=? and record_time<=? and status =1", patientid, orgid, startime, endtime).Find(&detail).Error
	return detail, err
}

func PCGetDialysisGoodsTwo(orgID int64, scheduleDate int64, schedule_type int64, partition_id int64, page int64, limit int64, keywords string) ([]*MDialysisGoodsVM, error, int64) {
	var vms []*MDialysisGoodsVM
	var total int64

	db := readDb.
		Model(&models.Schedule{}).
		Select("patient_id,mode_id").
		Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
		Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
		Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
		Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
			return db.Preload("VMGoodInfo", "status = 1 AND org_id = ?   ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND user_org_id = ? AND record_date = ? AND count > 0 ", orgID, scheduleDate)
		}).
		Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
			return db.Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0)  ", orgID).Where("status = 1 AND org_id = ? AND record_time = ?  AND count > 0", orgID, scheduleDate)
		}).Where("xt_schedule.status = 1 AND xt_schedule.user_org_id = ?", orgID)

	if scheduleDate != 0 {
		db = db.Where("schedule_date = ?", scheduleDate)
	}
	if len(keywords) != 0 {
		keywords = "%" + keywords + "%"
		db = db.Joins("JOIN xt_patients AS patient ON patient.id=xt_schedule.patient_id AND patient.status = 1 AND patient.user_org_id = ? AND patient.name Like ?", orgID, keywords)
	} else {
		if schedule_type != 0 {
			db = db.Where("schedule_type = ?", schedule_type)
		}
		if partition_id != 0 {
			db = db.Where("partition_id = ?", partition_id)
		}

		db = db.Count(&total)
		offset := (page - 1) * limit

		db = db.Offset(offset).Limit(limit)

	}

	err := db.Find(&vms).Error

	return vms, err, total
}

func PCGetDialysisGoodsThree(orgID int64, scheduleDate int64) ([]*MDialysisGoodsVM, error) {

	var vms []*MDialysisGoodsVM

	db := readDb.
		Model(&models.Schedule{}).
		Select("patient_id,mode_id").
		Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
		Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
		Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
		Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
			return db.Preload("VMGoodInfo", "status = 1 AND org_id = ?   ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND user_org_id = ? AND record_date = ? AND count > 0 ", orgID, scheduleDate)
		}).
		Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
			return db.Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0)  ", orgID).Where("status = 1 AND org_id = ? AND record_time = ?  AND count > 0", orgID, scheduleDate)
		}).Where("xt_schedule.status = 1 AND xt_schedule.user_org_id = ?", orgID)

	if scheduleDate != 0 {
		db = db.Where("schedule_date = ?", scheduleDate)
	}

	err = db.Find(&vms).Error

	return vms, err
}