package service

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

func GetAppVersionByAppType(apptype int64) (*models.AppVersion, error) {
	var version models.AppVersion
	err := readDb.Model(&models.AppVersion{}).Where("app_type=?", apptype).First(&version).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}

	if err != nil {
		return nil, err
	}
	return &version, nil
}

func GetAllAppOrg() ([]*models.OrgApp, error) {
	var app []*models.OrgApp
	err := readUserDb.Model(&models.OrgApp{}).Where("status = 1 AND org_id > 0").Group("org_id").Find(&app).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}

	if err != nil {
		return nil, err
	}
	return app, nil
}

func GetSystemApp() ([]*models.OrgApp, error) {
	var app []*models.OrgApp
	err := readDb.Model(&models.OrgApp{}).Where("status = 1 AND org_id = 0 ").Find(&app).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}

	if err != nil {
		return nil, err
	}
	return app, nil
}

func GetApp() ([]*models.OrgApp, error) {
	var app []*models.OrgApp
	err := readDb.Model(&models.OrgApp{}).Where("status = 1 AND org_id = 0").Find(&app).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}

	if err != nil {
		return nil, err
	}
	return app, nil
}

func GetAppByType(orgID int64, app_type int) (*models.OrgApp, error) {
	var apps models.OrgApp
	err := readUserDb.Where("app_type = ? AND org_id = ? AND status = 1", app_type, orgID).First(&apps).Error
	if err != nil {
		return nil, err
	}
	return &apps, nil
}

func CreateOrgApp(app *models.OrgApp) {
	writeUserDb.Create(&app)

}

func GetAllUserRole(org_id int64) (appRole []*models.App_Role) {
	if org_id == 0 {
		readUserDb.Model(&models.App_Role{}).Where("status = 1").Find(&appRole)

	} else {
		readUserDb.Model(&models.App_Role{}).Where("status = 1 AND org_id = ? ", org_id).Find(&appRole)

	}
	return
}

func GetAllUserRoleByUserTypeOne(org_id int) (appRole []*models.App_Role) {
	readUserDb.Model(&models.App_Role{}).Where("status = 1 AND user_type = 1").Find(&appRole)
	return
}

func GetAllUserRoleByUserTypeOther() (appRole []*models.App_Role) {
	//app, _ := GetOrgApp(int64(org_id), 3)

	//if org_id == 0 {
	readUserDb.Model(&models.App_Role{}).Where("status = 1 AND user_type > 1").Find(&appRole)
	//
	//} else {
	//	readUserDb.Model(&models.App_Role{}).Where("status = 1 AND org_id = ?  AND user_type > 1 AND app_id = ? ", org_id, app.Id).Find(&appRole)
	//
	//}
	return
}

func FindRoleByUserTypeOne(org_id int64) (role models.Role) {
	readUserDb.Model(&models.Role{}).Where("status = 1 AND org_id = ? AND is_system = 2 AND role_name = '医生'", org_id).First(&role)
	return
}

func FindRoleByUserTypeTwo(org_id int64) (role models.Role) {
	readUserDb.Model(&models.Role{}).Where("status = 1 AND org_id = ? AND is_system = 3 AND role_name = '护士'", org_id).First(&role)
	return
}

func GetAllRole() ([]*models.Role, error) {
	var app []*models.Role
	err := readUserDb.Model(&models.Role{}).Where("status = 1 AND org_id > 0").Group("org_id").Find(&app).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}
	return app, nil
}

func UpdateRoleIds(id int64, ids string) {
	writeUserDb.Model(&models.App_Role{}).Where("status = 1 AND id = ?", id).Updates(map[string]interface{}{"role_ids": ids, "mtime": time.Now().Unix()})

}

func GetOrgAppA(orgID int64, app_type int) (*models.OrgApp, error) {
	var apps models.OrgApp
	err := readUserDb.Where("app_type = ? AND org_id = ? AND status = 1", app_type, orgID).First(&apps).Error
	if err != nil {
		return nil, err
	}
	return &apps, nil
}

func GetOrgByIdB(orgID int64) (*models.Org, error) {
	var org models.Org
	err := readUserDb.Model(&models.Org{}).Where("id = ?", orgID).First(&org).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		} else {
			return nil, err
		}
	}
	return &org, nil
}

func GetOrgAppB(orgID int64, app_type int) (*models.OrgApp, error) {
	var apps models.OrgApp
	err := readUserDb.Where("app_type = ? AND org_id = ? AND status = 1", app_type, orgID).First(&apps).Error
	if err != nil {
		return nil, err
	}
	return &apps, nil
}

func CreateOrgRoleB(role *models.Role) (err error) {
	err = writeUserDb.Create(&role).Error
	return
}

func CreateRolePurviewB(purview *models.RolePurview) (err error) {
	err = writeUserDb.Create(&purview).Error
	return
}

func CreateFuncRolePurviewB(purview *models.SgjUserRoleFuncPurview) (err error) {
	err = writeUserDb.Create(&purview).Error
	return
}

func GetSystemRole(orgID int64) ([]*models.Role, error) {
	var roles []*models.Role
	err := readUserDb.Where(" org_id = ? AND status = 1 AND is_system > 1", orgID).First(&roles).Error
	if err != nil {
		return nil, err
	}
	return roles, nil
}
