package dao

import "server/global"

type Approver struct {
	global.GVA_MODEL
	UserId                    int     `json:"userId" form:"userId" gorm:"comment:用户id"`
	User                      SysUser `json:"user" form:"user" gorm:"foreignKey:UserId;references:id;"`
	ApproverDescription       string  `json:"approverDescription" form:"approverDescription" gorm:"comment:审批人详情"`
	ApproverRightsDescription string  `json:"approverRightsDescription" form:"approverRightsDescription" gorm:"comment:审批人权限详情"`
}

func (Approver) TableName() string {
	return "approver"
}

func QueryAllApprover() (approvers []Approver, err error) {
	err = global.GVA_DB.Model(&Approver{}).Preload("User").Find(&approvers).Error
	return approvers, err
}

func QueryApproverList(limit, offset int) (approvers []Approver, total int64, err error) {
	// 创建db
	db := global.GVA_DB.Model(&Approver{})
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Order("id desc").Limit(limit).Offset(offset).Preload("User").Find(&approvers).Error
	return approvers, total, err
}

func QueryApproverByUserId(userId int) (approver Approver, err error) {
	err = global.GVA_DB.Model(&Approver{}).Where("user_id = ?", userId).Preload("User").First(&approver).Error
	return approver, err
}

func (a Approver) CreateApprover() error {
	return global.GVA_DB.Create(&a).Error
}

func (a Approver) UpdateApprover() error {
	return global.GVA_DB.Where("id = ?", a.ID).Updates(&a).Error
}

func (a Approver) DeleteApprover() error {
	return global.GVA_DB.Unscoped().Delete(&a).Error
}
