package service

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

func GetClassName(orgid int64, class_name string) (*models.DoctorSchedules, error) {

	schedules := models.DoctorSchedules{}
	err := XTReadDB().Model(&schedules).Where("user_org_id = ? and status = 1 and class_name = ?", orgid, class_name).Find(&schedules).Error
	if err == gorm.ErrRecordNotFound {
		return nil, err
	}
	if err != nil {
		return nil, err
	}
	return &schedules, nil
}

func CreateDotorSchedule(schedules *models.DoctorSchedules) error {

	err := XTWriteDB().Create(&schedules).Error
	return err
}

func GetDoctorScheduleByOrgId(orgid int64) (*models.DoctorSchedules, error) {
	schedules := models.DoctorSchedules{}
	err := XTReadDB().Model(&schedules).Where("user_org_id = ? and status = 1", orgid).Find(&schedules).Error
	if err == gorm.ErrRecordNotFound {
		return nil, err
	}
	if err != nil {
		return nil, err
	}
	return &schedules, nil
}

func GetDoctorScheduleList(orgid int64, page int64, limit int64) (schedules []*models.DoctorSchedules, total int64, err error) {
	offset := (page - 1) * limit
	err = XTReadDB().Model(&schedules).Where("user_org_id = ? and status =1", orgid).Count(&total).Offset(offset).Limit(limit).Find(&schedules).Error
	return schedules, total, err
}

func GetScheduleDetail(id int64) (models.DoctorSchedules, error) {
	schedules := models.DoctorSchedules{}
	err := XTReadDB().Model(&schedules).Where("id=? and status = 1", id).Find(&schedules).Error
	return schedules, err
}

func GetClassNameIsExsit(classname string, orgid int64, id int64) (schedule models.DoctorSchedules, err error) {

	err = XTReadDB().Model(&schedule).Where("class_name = ? and user_org_id =? and id <> ?", classname, orgid, id).First(&schedule).Error
	return schedule, err
}

func UpdateScheduleList(id int64, schedules *models.DoctorSchedules) error {

	err := XTReadDB().Model(&schedules).Where("id=?", id).Updates(map[string]interface{}{"class_name": schedules.ClassName, "class_attributes": schedules.ClassAttributes, "timeone_start": schedules.TimeoneStart, "timeone_type": schedules.TimeoneType, "timeone_end": schedules.TimeoneEnd, "timetwo_start": schedules.TimetwoStart, "timetwo_type": schedules.TimetwoType, "timetwo_end": schedules.TimetwoEnd, "work_time": schedules.WorkTime, "remarks": schedules.Remarks, "minute": schedules.Minute, "mtime": time.Now().Unix()}).Error
	return err
}

func GetStaffScheduleByScheduleType(id int64, orgid int64) (*models.StaffSchedule, error) {
	schedule := models.StaffSchedule{}
	err := XTReadDB().Model(&schedule).Where("schedule_type = ? and user_org_id = ? and status = 1", id, orgid).Find(&schedule).Error
	if err == gorm.ErrRecordNotFound {
		return nil, err
	}
	if err != nil {
		return nil, err
	}
	return &schedule, nil
}

func DeleteScheduleById(id int64) error {
	schedules := models.DoctorSchedules{}
	err := XTWriteDB().Model(&schedules).Where("id=?", id).Updates(map[string]interface{}{"status": 0}).Error
	return err
}

func GetDoctorList(orgid int64, appid int64) (role []*models.XTSgjUserAdminRole, err error) {

	err = UserReadDB().Model(&role).Where("org_id = ? and app_id = ? and (user_type = 2 or user_type = 3) and status = 1 and is_sort = 1", orgid, appid).Order("user_type desc").Find(&role).Error
	return role, err
}

func GetAllDoctorList(orgid int64, appid int64) (role []*models.XTSgjUserAdminRole, err error) {

	err = UserReadDB().Model(&role).Where("org_id =? and app_id = ? and user_type = 2 and status = 1", orgid, appid).Order("sort desc").Find(&role).Error
	return role, err
}

func GetAllNurseList(orgid int64, appid int64) (role []*models.XTSgjUserAdminRole, err error) {

	err = UserReadDB().Model(&role).Where("org_id = ? and app_id = ? and user_type =3 and status =1", orgid, appid).Order("sort desc").Find(&role).Error
	return role, err
}

func GetScheduleList(orgid int64) (schedule []*models.DoctorSchedules, err error) {

	err = XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1", orgid).Find(&schedule).Error
	return schedule, err
}

func GetScheduleListDetail(doctor_id int64, orgid int64, scheduledate int64) (*models.StaffSchedule, error) {
	schedule := models.StaffSchedule{}
	err := XTReadDB().Model(&schedule).Where("doctor_id = ? and user_org_id = ? and status =1 and schedule_date = ?", doctor_id, orgid, scheduledate).Find(&schedule).Error
	if err == gorm.ErrRecordNotFound {
		return nil, err
	}
	if err != nil {
		return nil, err
	}
	return &schedule, nil
}

func AddSchedule(schedule *models.StaffSchedule) error {

	err := XTWriteDB().Create(&schedule).Error
	return err
}

func UpdateStaffList(schedule *models.StaffSchedule, id int64) error {
	err := XTReadDB().Model(&schedule).Where("id=?", id).Updates(map[string]interface{}{"doctor_id": schedule.DoctorId, "doctor_type": schedule.DoctorType, "schedule_type": schedule.ScheduleType, "schedule_week": schedule.ScheduleWeek, "schedule_date": schedule.ScheduleDate, "mtime": time.Now().Unix()}).Error
	return err
}

func GetStaffScheduleList(orgid int64, startime int64, endtime int64) (schedule []*models.XtStaffSchedule, err error) {

	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	err = db.Group("x.id").Select("x.id,x.doctor_id,x.doctor_type,x.schedule_type,x.schedule_week,x.user_org_id,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.admin_user_id,r.user_type,s.class_attributes,r.sort,r.is_sort,s.work_time,s.minute").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 and r.is_sort = 1", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func GetStaffScheduleListTwo(orgid int64, startime int64, endtime int64) (schedule []*models.StaffSchedule, err error) {

	err = XTReadDB().Model(&schedule).Where("user_org_id = ? and start_time>=? and end_time<=? and status = 1", orgid, startime, endtime).Find(&schedule).Error
	return schedule, err
}

func GetNextWeekSchedule(orgid int64, startime int64, endtime int64) (*models.StaffSchedule, error) {
	schedule := models.StaffSchedule{}
	err := XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1 and start_time >=? and end_time<=?", orgid, startime, endtime).Find(&schedule).Error
	if err == gorm.ErrRecordNotFound {
		return nil, err
	}
	if err != nil {
		return nil, err
	}
	return &schedule, nil
}

func GetScheduleByDoctorId(doctorid int64, startime int64, endtime int64, orgid int64) (schedule []*models.XtStaffSchedule, err error) {
	fmt.Println(doctorid, startime, endtime)
	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	if doctorid == 0 {
		db = db.Where("x.doctor_type = 2 or x.doctor_type = 3")
	}
	if doctorid > 0 {
		db = db.Where("x.doctor_type = ?", doctorid)
	}
	err = db.Group("x.id").Select("x.id,x.doctor_id,x.doctor_type,x.schedule_type,x.schedule_week,x.user_org_id,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.admin_user_id,s.class_attributes,r.sort,r.is_sort").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ?", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func ToSearchSeacheduleList(keywords string, startime int64, endtime int64, orgid int64) (schedule []*models.XtStaffSchedule, err error) {
	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	likeKey := "%" + keywords + "%"
	//if(doctorid >0){
	//  db = db.Where("x.doctor_id = ?", doctorid)
	//}
	err = db.Group("x.id").Select("x.id,x.doctor_id,x.doctor_type,x.schedule_type,x.schedule_week,x.user_org_id,x.start_time,x.schedule_date,x.end_time,r.user_type,r.user_name,s.class_name,r.admin_user_id,s.class_attributes,r.sort,r.is_sort").Joins("left join sgj_users.sgj_user_admin_role AS r ON r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type and s.user_org_id = ?", orgid).Where("r.org_id = ? and r.status = 1 and r.is_sort = 1  and r.user_name like ?", orgid, likeKey).Scan(&schedule).Error
	return schedule, err
}

func DeleteStaffSchedule(orgid int64, startime int64, endtime int64) error {

	schedule := models.StaffSchedule{}
	err := XTWriteDB().Model(&schedule).Where("user_org_id = ? and start_time >= ? and end_time <=?", orgid, startime, endtime).Updates(map[string]interface{}{"status": 0}).Error
	return err
}

func GetContinueScheduleByOrgId(orgid int64) (*models.ContinueSchedule, error) {

	//schedules := models.DoctorSchedules{}
	//err := XTReadDB().Model(&schedules).Where("user_org_id = ? and status = 1", orgid).Find(&schedules).Error
	//if err == gorm.ErrRecordNotFound {
	//  return nil, err
	//}
	//if err != nil {
	//  return nil, err
	//}
	//return &schedules, nil
	schedule := models.ContinueSchedule{}
	err := XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1", orgid).Find(&schedule).Error
	if err == gorm.ErrRecordNotFound {
		return nil, err
	}
	if err != nil {
		return nil, err
	}
	return &schedule, nil
}

func UpdateContinusSchedule(schedule *models.ContinueSchedule, orgid int64) error {

	err := XTWriteDB().Model(&schedule).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"is_status": schedule.IsStatus, "mtime": time.Now().Unix()}).Error
	return err
}

func SaveNurseSort(role *models.App_Role, id int64) error {

	err := UserWriteDB().Model(&role).Where("id = ?", id).Updates(map[string]interface{}{"sort": role.Sort}).Error
	return err
}

func SaveIsSchedule(id int64, role *models.App_Role) error {

	err := UserWriteDB().Model(&role).Where("id=?", id).Updates(map[string]interface{}{"is_sort": role.IsSort}).Error
	return err
}

func GetScheduleListTotal(orgid int64, startime int64, endtime int64) (schedule []*models.CountStaffSchedule, err error) {

	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	err = db.Group("x.doctor_id,x.schedule_type").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,count(s.class_name) as count").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func GetTotalMinutes(orgid int64, startime int64, endtime int64) (schedule []*models.CountStaffSchedule, err error) {

	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,sum(s.minute) as totalminute").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 and s.class_attributes = 1", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func GetTotalMinutesOne(orgid int64, startime int64, endtime int64) (schedule []*models.CountStaffSchedule, err error) {
	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,sum(s.minute) as totalminute").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 ", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func GetTotalMinutesTwo(orgid int64, startime int64, endtime int64) (schedule []*models.CountStaffSchedule, err error) {
	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,sum(s.minute) as workminute").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 and s.class_attributes = 1", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func GetChartTotalMinutesOne(orgid int64, startime int64, endtime int64, keywords string) (schedule []*models.CountStaffSchedule, err error) {
	likeKey := "%" + keywords + "%"
	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,sum(s.minute) as totalminute").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Where("r.user_name like ?", likeKey).Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 ", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func GetChartTotalMinutesTwo(orgid int64, startime int64, endtime int64, keywords string) (schedule []*models.CountStaffSchedule, err error) {
	likeKey := "%" + keywords + "%"
	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,sum(s.minute) as workminute").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Where("r.user_name like ?", likeKey).Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 and s.class_attributes = 1", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func GetTotalWorkDay(orgid int64, startime int64, endtime int64) (schedule []*models.CountStaffSchedule, err error) {

	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,count(x.id) as count").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 and s.class_attributes = 1", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func GetTotalNoWorkDay(orgid int64, startime int64, endtime int64) (schedule []*models.CountStaffSchedule, err error) {

	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,count(x.id) as count").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 and s.class_attributes = 2", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func GetSearchScheduleListTotal(orgid int64, startime int64, endtime int64, keywords string) (schedule []*models.CountStaffSchedule, err error) {
	likeKey := "%" + keywords + "%"
	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	err = db.Group("x.doctor_id,x.schedule_type").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,count(s.class_name) as count").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Where("r.user_name like ?", likeKey).Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 ", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func GeSearchtTotalMinutes(orgid int64, startime int64, endtime int64, keywords string) (schedule []*models.CountStaffSchedule, err error) {
	likeKey := "%" + keywords + "%"
	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,sum(s.minute) as totalminute").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Where("r.user_name like ?", likeKey).Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1  and s.class_attributes = 1", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func GetSearchWorkDay(orgid int64, startime int64, endtime int64, keywords string) (schedule []*models.CountStaffSchedule, err error) {
	likeKey := "%" + keywords + "%"
	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,count(x.id) as count").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Where("r.user_name like ?", likeKey).Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 and s.class_attributes = 1", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func GetSearchWorkNoDay(orgid int64, startime int64, endtime int64, keywords string) (schedule []*models.CountStaffSchedule, err error) {
	likeKey := "%" + keywords + "%"
	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,count(x.id) as count").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Where("r.user_name like ?", likeKey).Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 and s.class_attributes = 2", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func ChagneScheduleListTotal(orgid int64, startime int64, endtime int64, doctortype int64) (schedule []*models.CountStaffSchedule, err error) {

	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	if doctortype == 0 {
		db = db.Where("x.doctor_type = 2 or x.doctor_type = 3")
	}
	if doctortype > 0 {
		db = db.Where("x.doctor_type = ?", doctortype)
	}
	err = db.Group("x.doctor_id,x.schedule_type").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,count(s.class_name) as count").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 ", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func ChangeScheduleMinute(orgid int64, startime int64, endtime int64, doctortype int64) (schedule []*models.CountStaffSchedule, err error) {

	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	if doctortype == 0 {
		db = db.Where("x.doctor_type = 2 or x.doctor_type = 3")
	}
	if doctortype > 0 {
		db = db.Where("x.doctor_type = ?", doctortype)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,sum(s.minute) as totalminute").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 and s.class_attributes = 1", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func ChangeWorkDay(orgid int64, startime int64, endtime int64, doctortype int64) (schedule []*models.CountStaffSchedule, err error) {

	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	if doctortype == 0 {
		db = db.Where("x.doctor_type = 2 or x.doctor_type = 3")
	}
	if doctortype > 0 {
		db = db.Where("x.doctor_type = ?", doctortype)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,count(x.id) as count").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 and s.class_attributes = 1", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func ChangeNoWorkDay(orgid int64, startime int64, endtime int64, doctortype int64) (schedule []*models.CountStaffSchedule, err error) {

	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	if doctortype == 0 {
		db = db.Where("x.doctor_type = 2 or x.doctor_type = 3")
	}
	if doctortype > 0 {
		db = db.Where("x.doctor_type = ?", doctortype)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,count(x.id) as count").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 and s.class_attributes = 2", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func ChartTotalMinute(orgid int64, startime int64, endtime int64, doctortype int64) (schedule []*models.CountStaffSchedule, err error) {
	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	if doctortype == 0 {
		db = db.Where("x.doctor_type = 2 or x.doctor_type = 3")
	}
	if doctortype > 0 {
		db = db.Where("x.doctor_type = ?", doctortype)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,sum(s.minute) as totalminute").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 ", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func ChartWorkDayMinute(orgid int64, startime int64, endtime int64, doctortype int64) (schedule []*models.CountStaffSchedule, err error) {

	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	if startime > 0 {
		db = db.Where("x.start_time >=?", startime)
	}
	if endtime > 0 {
		db = db.Where("x.end_time <=?", endtime)
	}
	if orgid > 0 {
		db = db.Where("x.user_org_id = ?", orgid)
	}
	if doctortype == 0 {
		db = db.Where("x.doctor_type = 2 or x.doctor_type = 3")
	}
	if doctortype > 0 {
		db = db.Where("x.doctor_type = ?", doctortype)
	}
	err = db.Group("x.doctor_id").Select("x.doctor_id,x.schedule_type,x.start_time,x.end_time,x.schedule_date,r.user_name,s.class_name,r.sort,r.is_sort,sum(s.minute) as workminute").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.doctor_id").Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Where("r.org_id = ? and r.status = 1 and s.user_org_id = ? and s.status = 1 and s.class_attributes = 1", orgid, orgid).Scan(&schedule).Error
	return schedule, err
}

func GetMySchedule(startime int64, endtime int64, orgid int64, creator int64) (schedule []*models.XtStaffSchedule, err error) {
	db := XTReadDB().Table("xt_staff_schedule as x").Where("x.status = 1")
	err = db.Group("x.id").Select("x.doctor_id,x.schedule_week,s.class_name").Where("x.start_time >=? and x.end_time<=? and x.user_org_id = ? and x.doctor_id = ?", startime, endtime, orgid, creator).Joins("left join xt_doctor_schedules as s on s.id = x.schedule_type").Scan(&schedule).Error
	return schedule, err
}

func GetAllMobileZone(orgid int64) (zone []*models.DeviceZone, err error) {

	err = XTReadDB().Model(&zone).Where("org_id = ? and status = 1", orgid).Find(&zone).Error
	return zone, err
}

func GetPatientSchedule(startime int64, zoneid int64, classtype int64, orgid int64) (schedule []*models.Schedule, err error) {

	db := XTReadDB().Table("xt_schedule as s")
	if zoneid > 0 {
		db = db.Where("s.partition_id = ?", zoneid)
	}
	if classtype > 0 {
		db = db.Where("s.schedule_type = ?", classtype)
	}
	err = db.Preload("DeviceZone", " status= 1").Preload("DeviceNumber", func(db *gorm.DB) *gorm.DB {
		return XTReadDB().Model(&models.DeviceNumber{}).Where("status = 1 AND org_id = ?", orgid).Order("sort asc")
	}).Preload("DialysisOrder", "status =1").Joins("JOIN xt_patients as p ON p.id = s.patient_id and p.lapseto = 1 and p.user_org_id =?", orgid).Where("s.user_org_id = ? and s.schedule_date = ? and s.status =1", orgid, startime).
		Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id,p.name as patient").Find(&schedule).Error
	return schedule, err
}

func DeleteSchedule(id int64) error {

	schedule := models.XtSchedule{}

	err := XTWriteDB().Model(&schedule).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
	return err
}

func GetSearchPatient(keywords string, orgid int64, startime int64) (schedule []*models.Schedule, err error) {

	likeKey := "%" + keywords + "%"
	db := XTReadDB().Table("xt_schedule as s")
	err = db.Preload("DeviceZone", " status= 1").Preload("DeviceNumber", "status= 1").
		Preload("DialysisOrder", "status =1").
		Joins("JOIN xt_patients as p ON p.id = s.patient_id And(p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).Where("s.user_org_id = ? and s.schedule_date = ? and s.status =1", orgid, startime).
		Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id,p.name as patient").Find(&schedule).Error
	return schedule, err
}

func GetStaffScheduleById(ids []string) (schedule []*models.XtSchedule, err error) {

	err = XTReadDB().Where("id in(?)", ids).Find(&schedule).Error
	return schedule, err

}

func UpdateStaffSchedule(nextid int64, schedule models.XtSchedule) error {

	err := XTWriteDB().Model(&schedule).Where("id = ?", nextid).Updates(map[string]interface{}{"patient_id": schedule.PatientId, "mode_id": schedule.ModeId}).Error
	return err
}

func UpdateStaffScheduleOne(nextid int64, schedule models.WeekSchedule) error {

	err := XTWriteDB().Model(&schedule).Where("id = ?", nextid).Updates(map[string]interface{}{"patient_id": schedule.PatientId, "mode_id": schedule.ModeId}).Error
	return err
}
