作者 郑周

1. 增加权限配置表

... ... @@ -192,3 +192,11 @@ func CreateEvaluationItemUsedRepository(options map[string]interface{}) domain.E
}
return repository.NewEvaluationItemUsedRepository(transactionContext)
}
func CreatePermissionRepository(options map[string]interface{}) domain.PermissionRepository {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewPermissionRepository(transactionContext)
}
... ...
package command
import "github.com/beego/beego/v2/core/validation"
type GetPermissionCommand struct {
CompanyId int64 `cname:"公司Id"`
}
func (in *GetPermissionCommand) Valid(*validation.Validation) {
}
... ...
package command
import (
"github.com/beego/beego/v2/core/validation"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type UpdatePermissionCommand struct {
CompanyId int64 `cname:"公司Id"`
OptHrScore int `cname:"上级修改人资综评分数" json:"optHrScore" valid:"Required"`
OptEvalScore int `cname:"上级修改360°综评分数" json:"optEvalScore" valid:"Required"`
}
func (in *UpdatePermissionCommand) Valid(validation *validation.Validation) {
switch in.OptHrScore {
case domain.PermissionOff, domain.PermissionOn:
default:
validation.SetError("optHrScore", "修改人资综评分数参数错误")
return
}
switch in.OptEvalScore {
case domain.PermissionOff, domain.PermissionOn:
default:
validation.SetError("optEvalScore", "修改360°综评分数参数错误")
return
}
}
... ...
package service
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/permission/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type PermissionService struct {
}
func NewPermissionService() *PermissionService {
newPermissionService := &PermissionService{}
return newPermissionService
}
func (rs *PermissionService) Update(in *command.UpdatePermissionCommand) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
permissionRepository := factory.CreatePermissionRepository(map[string]interface{}{"transactionContext": transactionContext})
// 获取权限配置
_, permissions, err := permissionRepository.Find(map[string]interface{}{"companyId": in.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(permissions) == 0 {
return nil, application.ThrowError(application.BUSINESS_ERROR, "权限配置数据错误")
}
permission := permissions[0]
permission.OptHrScore = in.OptHrScore
permission.OptEvalScore = in.OptEvalScore
permission, err = permissionRepository.Insert(permission)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return permission, nil
}
func (rs *PermissionService) Get(in *command.GetPermissionCommand) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
permissionRepository := factory.CreatePermissionRepository(map[string]interface{}{"transactionContext": transactionContext})
// 获取权限配置
_, permissions, err := permissionRepository.Find(map[string]interface{}{"companyId": in.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var permission *domain.Permission
if len(permissions) == 0 { // 不存在时,新增权限配置
value := &domain.Permission{
Id: 0,
CompanyId: in.CompanyId,
OptHrScore: domain.PermissionOff,
OptEvalScore: domain.PermissionOff,
}
permission, err = permissionRepository.Insert(value)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
} else {
permission = permissions[0]
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return permission, nil
}
... ...
package domain
import "time"
const (
PermissionOff int = 1 // 权限开关-关闭
PermissionOn int = 2 // 权限开关-开启
)
type Permission struct {
Id int64 `json:"id,string"`
CompanyId int64 `json:"companyId" comment:"公司ID" `
OptHrScore int `json:"optHrScore" comment:"上级是否可以修改人资综评分数"`
OptEvalScore int `json:"optEvalScore" comment:"上级是否可以修改360°综评分数"`
CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
}
type PermissionRepository interface {
Insert(permission *Permission) (*Permission, error)
FindOne(queryOptions map[string]interface{}) (*Permission, error)
Find(queryOptions map[string]interface{}) (int64, []*Permission, error)
//FindByCompanyId(companyId int64) (*Permission, error)
}
... ...
... ... @@ -49,6 +49,7 @@ func init() {
&models.EvaluationItemUsed{},
&models.SummaryEvaluation{},
&models.SummaryEvaluationValue{},
&models.Permission{},
}
for _, model := range tables {
err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
... ...
package models
import "time"
type Permission struct {
tableName struct{} `comment:"配置权限" pg:"permission"`
Id int64 `comment:"ID" pg:"pk:id"`
CompanyId int64 `comment:"公司ID"`
OptHrScore int `comment:"上级是否可以修改人资综评分数"`
OptEvalScore int `comment:"上级是否可以修改360°综评分数"`
CreatedAt time.Time `comment:"创建时间"`
UpdatedAt time.Time `comment:"更新时间"`
DeletedAt *time.Time `comment:"删除时间"`
}
... ...
package repository
import (
"errors"
"fmt"
"time"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
)
type PermissionRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func NewPermissionRepository(transactionContext *pgTransaction.TransactionContext) *PermissionRepository {
return &PermissionRepository{transactionContext: transactionContext}
}
func (repo *PermissionRepository) TransformToDomain(m *models.Permission) domain.Permission {
return domain.Permission{
Id: m.Id,
CompanyId: m.CompanyId,
OptHrScore: m.OptHrScore,
OptEvalScore: m.OptEvalScore,
CreatedAt: m.CreatedAt.Local(),
UpdatedAt: m.UpdatedAt.Local(),
DeletedAt: m.DeletedAt,
}
}
func (repo *PermissionRepository) TransformToModel(d *domain.Permission) models.Permission {
return models.Permission{
Id: d.Id,
CompanyId: d.CompanyId,
OptHrScore: d.OptHrScore,
OptEvalScore: d.OptEvalScore,
CreatedAt: d.CreatedAt,
UpdatedAt: d.UpdatedAt,
DeletedAt: d.DeletedAt,
}
}
func (repo *PermissionRepository) nextIdentify() (int64, error) {
return utils.NewSnowflakeId()
}
func (repo *PermissionRepository) Insert(d *domain.Permission) (*domain.Permission, error) {
var isCreate = d.Id == 0
if isCreate {
id, err := repo.nextIdentify()
if err != nil {
return d, err
}
d.Id = id
d.CreatedAt = time.Now()
d.UpdatedAt = d.CreatedAt
} else {
d.UpdatedAt = time.Now()
}
m := repo.TransformToModel(d)
tx := repo.transactionContext.PgTx
var err error
if isCreate {
_, err = tx.Model(&m).Returning("id").Insert()
} else {
_, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件
}
if err != nil {
return nil, err
}
d.Id = m.Id
return d, nil
}
func (repo *PermissionRepository) FindOne(queryOptions map[string]interface{}) (*domain.Permission, error) {
tx := repo.transactionContext.PgTx
m := new(models.Permission)
query := tx.Model(m)
query.Where("deleted_at isnull")
if v, ok := queryOptions["id"]; ok {
query.Where("id=?", v)
}
if err := query.First(); err != nil {
if errors.Is(err, pg.ErrNoRows) {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
}
}
u := repo.TransformToDomain(m)
return &u, nil
}
func (repo *PermissionRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Permission, error) {
tx := repo.transactionContext.PgTx
var m []*models.Permission
query := tx.Model(&m).Where("deleted_at isnull")
if companyId, ok := queryOptions["companyId"]; ok {
query.Where("company_id = ?", companyId)
}
if v, ok := queryOptions["limit"].(int64); ok {
query.Limit(int(v))
}
if v, ok := queryOptions["offset"].(int64); ok {
query.Offset(int(v))
}
count, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
var arrays []*domain.Permission
for _, v := range m {
d := repo.TransformToDomain(v)
arrays = append(arrays, &d)
}
return int64(count), arrays, nil
}
//func (repo *PermissionRepository) FindByCompanyId(companyId int64) (*domain.Permission, error) {
// tx := repo.transactionContext.PgTx
// m := new(models.Permission)
// query := tx.Model(m)
// query.Where("deleted_at isnull")
// query.Where("company_id = ?", companyId)
// if err := query.First(); err != nil {
// if errors.Is(err, pg.ErrNoRows) {
// return nil, fmt.Errorf("没有此资源")
// } else {
// return nil, err
// }
// }
// u := repo.TransformToDomain(m)
// return &u, nil
//}
... ...
package controllers
import (
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/web/beego"
service "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/permission"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/permission/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
)
type PermissionController struct {
beego.BaseController
}
func (controller *PermissionController) UpdatePermission() {
ruService := service.NewPermissionService()
in := &command.UpdatePermissionCommand{}
if err := controller.Unmarshal(in); err != nil {
controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
} else {
ua := middlewares.GetUser(controller.Ctx)
in.CompanyId = ua.CompanyId
controller.Response(ruService.Update(in))
}
}
func (controller *PermissionController) GetPermission() {
ruService := service.NewPermissionService()
in := &command.GetPermissionCommand{}
if err := controller.Unmarshal(in); err != nil {
controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
} else {
ua := middlewares.GetUser(controller.Ctx)
in.CompanyId = ua.CompanyId
controller.Response(ruService.Get(in))
}
}
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"github.com/linmadan/egglib-go/web/beego/filters"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/controllers"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
)
func init() {
ns := web.NewNamespace("/v1/permission",
web.NSBefore(filters.AllowCors(), middlewares.CheckAdminToken()),
web.NSRouter("/update", &controllers.PermissionController{}, "Put:UpdatePermission"),
web.NSRouter("/get", &controllers.PermissionController{}, "Post:GetPermission"),
)
web.AddNamespace(ns)
}
... ...