domain.go 1.9 KB
package domain

import "fmt"

const MaxQueryRow = 10000

// 班次
const (
	WorkOnFullDay = 1 //全天
	WorkOnDay     = 2 //白班
	WorkOnMidDay  = 4 //中班
	WorkOnNight   = 8 //夜班
)

const WorkOn = WorkOnFullDay | WorkOnDay | WorkOnMidDay | WorkOnNight

var (
	ErrorNotFound = fmt.Errorf("没有此资源")
)

// excel 编码
var (
	ExcelImportProduct = "MANUFACTURE_EXCEL_IMPORT_PRODUCT"
	ExcelExportProduct = "MANUFACTURE_EXCEL_EXPORT_PRODUCT"
)

/*****   1.数据权限  *****/
// DataAuthor 数据验证器
type DataAuthor interface {
	DataAuth(options OperateInfo, data AuthedData) error
}

// AuthedData 需要认证的数据
type AuthedData interface {
	// 数据所属组织
	BelongOrg() int64
}

// 验证参数
type OperateInfo struct {
	// 当前操作人
	UserId int
	// 当前公司
	CompanyId int
	// 当前登录的组织
	OrgId int
	// 菜单模块
	MenuCode string
	// 匹配多个组织
	OrgIds []int
}

func NewCheckOptions(optUser, org int) OperateInfo {
	return OperateInfo{
		UserId: optUser,
		OrgId:  org,
	}
}

func (info OperateInfo) Valid() bool {
	if info.UserId == 0 || info.CompanyId == 0 || info.OrgId == 0 {
		return false
	}
	return true
}

func (info OperateInfo) GetCompanyId(companyId int) int {
	if companyId != 0 {
		return companyId
	}
	return info.CompanyId
}

func (info OperateInfo) GetOrgId(orgId int) int {
	if orgId != 0 {
		return orgId
	}
	return info.OrgId
}

func (info OperateInfo) GetUserId(userId int) int {
	if userId != 0 {
		return userId
	}
	return info.UserId
}

func (info OperateInfo) String() string {
	return fmt.Sprintf("UserId: %v OrgId:%v CompanyId:%v", info.UserId, info.OrgId, info.CompanyId)
}

func Pagination(pageNumber, pageSize int) (offset int, limit int) {
	if pageSize != 0 {
		offset = (pageNumber - 1) * pageSize
		limit = pageSize
	}
	return
}

func ValidWorkOn(workOn int) error {
	if (workOn & WorkOn) == 0 {
		return fmt.Errorf("班次有误")
	}
	return nil
}