package service

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

func UpdateSysItemIdByID(sysInspection *models.InspectionReference) error {
	db := XTWriteDB()
	err := db.Model(&models.InspectionReference{}).Update(&sysInspection).Error
	return err
}

func GetSysInspectionList(orgID int64) (sysInspection []*models.InspectionReference,ownerInspection []*models.InspectionReference, err error) {
	db := XTReadDB()
	err = db.Model(&models.InspectionReference{}).Where("org_id= 0 and status=1").Find(&sysInspection).Error
	err = db.Model(&models.InspectionReference{}).Where("org_id= ? and status=1", orgID).Find(&ownerInspection).Error
	return
}

func GetSyncList(orgID int64, page, limit int64) (patients []*models.MiddleSyncInfo, total int64, err error) {
	//db := MiddleReadDB()
	db := MiddleReadDB()
	offset := (page - 1) * limit
	err = db.Model(&models.MiddleSyncInfo{}).Where("org_id=? ", orgID).Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
	return
}

func SaveInterface(interfaceinfo *models.MiddleInterface) error {
	db := MiddleWriteDB()
	if (interfaceinfo.ID > 0 ) {
		err := db.Save(&interfaceinfo).Error
		return err
	} else {
		err := db.Create(&interfaceinfo).Error
		return err
	}
}

func GetInterface(orgID int64) (interfaceinfo models.MiddleInterface, err error){
	db := MiddleReadDB()
	err = db.Model(&models.MiddleInterface{}).Where("org_id=? and status=1", orgID).First(&interfaceinfo).Error
	return
}

func GetIntegrationPatientList(orgID int64, page, limit int64) (patients []*models.VMPatients, total int64, err error) {
	//db := MiddleReadDB()
	db := readDb.Model(&models.VMPatients{}).Where("status=1")
	if orgID > 0 {
		db = db.Where("user_org_id=?", orgID)
	}
	offset := (page - 1) * limit
	err = db.Preload("VMHisPatients", func(db *gorm.DB) *gorm.DB {
		return readMiddleDb.Model(&models.VMHisPatients{}).Where("status = 1 AND user_org_id = ?", orgID)
	}).Preload("Contagions", "status = 1").Group("id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
	return
}

func UpdatePatientsHis(org_id int64, his_user_id string, id int64) (err error) {
	db := MiddleWriteDB()
	err = db.Model(&models.HisPatients{}).Where("user_org_id = ? AND id = ?", org_id, id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "his_user_id": his_user_id}).Error
	return
}

func CreatePatientsHis(patient *models.HisPatients) (err error) {
	db := MiddleWriteDB()
	err = db.Save(&patient).Error
	return
}

func FindVMPatientById(orgID int64, id int64) (patient models.VMPatients, err error) {
	db := XTReadDB()
	err = db.Model(&models.VMPatients{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
	return
}

func FindHisPatientByHisId(orgID int64, his_user_id string) (total int64, err error) {
	db := MiddleReadDB()
	err = db.Model(&models.HisPatients{}).Where("his_user_id = ? and user_org_id=? and status=1", his_user_id, orgID).Count(&total).Error
	return

}

func GetAdminUsers(orgID int64, appID int64, page int, limit int) (admins []*models.VMUserAdminRole, total int64, err error) {
	db := readUserDb.Model(&models.VMUserAdminRole{}).Where("status=1")
	if orgID > 0 {
		db = db.Where("org_id = ? AND app_id = ?", orgID, appID)
	}
	offset := (page - 1) * limit
	err = db.Preload("HisUserAdminRole", func(db *gorm.DB) *gorm.DB {
		return readMiddleDb.Model(&models.HisUserAdminRole{}).Where("status = 1 AND org_id = ? AND app_id = ?", orgID, appID)
	}).Preload("VMUserAdmin", "status = 1").Preload("VMUserRole", "status = 1 AND org_id = ? AND app_id = ?", orgID, appID).Count(&total).Offset(offset).Limit(limit).Find(&admins).Error
	return
}

func UpdateAdminsHis(org_id int64, his_user_id string, id int64) (err error) {
	db := MiddleWriteDB()
	err = db.Model(&models.HisUserAdminRole{}).Where("org_id = ? AND id = ?", org_id, id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "his_user_id": his_user_id}).Error
	return
}

func CreateAdminsHis(admin *models.HisUserAdminRole) (err error) {
	db := MiddleWriteDB()
	err = db.Save(&admin).Error
	return
}

func FindVMAdminRoleById(orgID int64, id int64, app_id int64) (admin models.VMUserAdminRole, err error) {
	db := UserReadDB()
	err = db.Model(&models.VMUserAdminRole{}).Where("admin_user_id = ? and org_id=?  and app_id = ? and status=1", id, orgID, app_id).First(&admin).Error
	return
}

func FindHisAdminByHisId(orgID int64, his_user_id string, app_id int64) (total int64, err error) {
	db := MiddleReadDB()
	err = db.Model(&models.HisUserAdminRole{}).Where("his_user_id = ? and org_id=? and app_id = ? and status=1", his_user_id, orgID, app_id).Count(&total).Error
	return

}
