作者 tangxvhui

Merge branch 'test' into dev-tangxvhui

@@ -192,3 +192,11 @@ func CreateEvaluationItemUsedRepository(options map[string]interface{}) domain.E @@ -192,3 +192,11 @@ func CreateEvaluationItemUsedRepository(options map[string]interface{}) domain.E
192 } 192 }
193 return repository.NewEvaluationItemUsedRepository(transactionContext) 193 return repository.NewEvaluationItemUsedRepository(transactionContext)
194 } 194 }
  195 +
  196 +func CreatePermissionRepository(options map[string]interface{}) domain.PermissionRepository {
  197 + var transactionContext *pg.TransactionContext
  198 + if value, ok := options["transactionContext"]; ok {
  199 + transactionContext = value.(*pg.TransactionContext)
  200 + }
  201 + return repository.NewPermissionRepository(transactionContext)
  202 +}
  1 +package command
  2 +
  3 +import "github.com/beego/beego/v2/core/validation"
  4 +
  5 +type GetPermissionCommand struct {
  6 + CompanyId int64 `cname:"公司Id"`
  7 +}
  8 +
  9 +func (in *GetPermissionCommand) Valid(*validation.Validation) {
  10 +
  11 +}
  1 +package command
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/core/validation"
  5 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  6 +)
  7 +
  8 +type UpdatePermissionCommand struct {
  9 + CompanyId int64 `cname:"公司Id"`
  10 + OptHrScore int `cname:"上级修改人资综评分数" json:"optHrScore" valid:"Required"`
  11 + OptEvalScore int `cname:"上级修改360°综评分数" json:"optEvalScore" valid:"Required"`
  12 +}
  13 +
  14 +func (in *UpdatePermissionCommand) Valid(validation *validation.Validation) {
  15 + switch in.OptHrScore {
  16 + case domain.PermissionOff, domain.PermissionOn:
  17 + default:
  18 + validation.SetError("optHrScore", "修改人资综评分数参数错误")
  19 + return
  20 + }
  21 +
  22 + switch in.OptEvalScore {
  23 + case domain.PermissionOff, domain.PermissionOn:
  24 + default:
  25 + validation.SetError("optEvalScore", "修改360°综评分数参数错误")
  26 + return
  27 + }
  28 +}
  1 +package service
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
  6 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/permission/command"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  8 +)
  9 +
  10 +type PermissionService struct {
  11 +}
  12 +
  13 +func NewPermissionService() *PermissionService {
  14 + newPermissionService := &PermissionService{}
  15 + return newPermissionService
  16 +}
  17 +
  18 +func (rs *PermissionService) Update(in *command.UpdatePermissionCommand) (interface{}, error) {
  19 + transactionContext, err := factory.ValidateStartTransaction(in)
  20 + if err != nil {
  21 + return nil, err
  22 + }
  23 + defer func() {
  24 + transactionContext.RollbackTransaction()
  25 + }()
  26 + permissionRepository := factory.CreatePermissionRepository(map[string]interface{}{"transactionContext": transactionContext})
  27 + // 获取权限配置
  28 + _, permissions, err := permissionRepository.Find(map[string]interface{}{"companyId": in.CompanyId})
  29 + if err != nil {
  30 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  31 + }
  32 + if len(permissions) == 0 {
  33 + return nil, application.ThrowError(application.BUSINESS_ERROR, "权限配置数据错误")
  34 + }
  35 + permission := permissions[0]
  36 + permission.OptHrScore = in.OptHrScore
  37 + permission.OptEvalScore = in.OptEvalScore
  38 +
  39 + permission, err = permissionRepository.Insert(permission)
  40 + if err != nil {
  41 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  42 + }
  43 + if err := transactionContext.CommitTransaction(); err != nil {
  44 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  45 + }
  46 + return permission, nil
  47 +}
  48 +
  49 +func (rs *PermissionService) Get(in *command.GetPermissionCommand) (interface{}, error) {
  50 + transactionContext, err := factory.ValidateStartTransaction(in)
  51 + if err != nil {
  52 + return nil, err
  53 + }
  54 + defer func() {
  55 + transactionContext.RollbackTransaction()
  56 + }()
  57 + permissionRepository := factory.CreatePermissionRepository(map[string]interface{}{"transactionContext": transactionContext})
  58 +
  59 + // 获取权限配置
  60 + _, permissions, err := permissionRepository.Find(map[string]interface{}{"companyId": in.CompanyId})
  61 + if err != nil {
  62 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  63 + }
  64 + var permission *domain.Permission
  65 + if len(permissions) == 0 { // 不存在时,新增权限配置
  66 + value := &domain.Permission{
  67 + Id: 0,
  68 + CompanyId: in.CompanyId,
  69 + OptHrScore: domain.PermissionOff,
  70 + OptEvalScore: domain.PermissionOff,
  71 + }
  72 + permission, err = permissionRepository.Insert(value)
  73 + if err != nil {
  74 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  75 + }
  76 + } else {
  77 + permission = permissions[0]
  78 + }
  79 + if err := transactionContext.CommitTransaction(); err != nil {
  80 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  81 + }
  82 + return permission, nil
  83 +}
  1 +package domain
  2 +
  3 +import "time"
  4 +
  5 +const (
  6 + PermissionOff int = 1 // 权限开关-关闭
  7 + PermissionOn int = 2 // 权限开关-开启
  8 +)
  9 +
  10 +type Permission struct {
  11 + Id int64 `json:"id,string"`
  12 + CompanyId int64 `json:"companyId" comment:"公司ID" `
  13 + OptHrScore int `json:"optHrScore" comment:"上级是否可以修改人资综评分数"`
  14 + OptEvalScore int `json:"optEvalScore" comment:"上级是否可以修改360°综评分数"`
  15 + CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
  16 + UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
  17 + DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
  18 +}
  19 +
  20 +type PermissionRepository interface {
  21 + Insert(permission *Permission) (*Permission, error)
  22 + FindOne(queryOptions map[string]interface{}) (*Permission, error)
  23 + Find(queryOptions map[string]interface{}) (int64, []*Permission, error)
  24 + //FindByCompanyId(companyId int64) (*Permission, error)
  25 +}
@@ -49,6 +49,7 @@ func init() { @@ -49,6 +49,7 @@ func init() {
49 &models.EvaluationItemUsed{}, 49 &models.EvaluationItemUsed{},
50 &models.SummaryEvaluation{}, 50 &models.SummaryEvaluation{},
51 &models.SummaryEvaluationValue{}, 51 &models.SummaryEvaluationValue{},
  52 + &models.Permission{},
52 } 53 }
53 for _, model := range tables { 54 for _, model := range tables {
54 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ 55 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
  1 +package models
  2 +
  3 +import "time"
  4 +
  5 +type Permission struct {
  6 + tableName struct{} `comment:"配置权限" pg:"permission"`
  7 + Id int64 `comment:"ID" pg:"pk:id"`
  8 + CompanyId int64 `comment:"公司ID"`
  9 + OptHrScore int `comment:"上级是否可以修改人资综评分数"`
  10 + OptEvalScore int `comment:"上级是否可以修改360°综评分数"`
  11 + CreatedAt time.Time `comment:"创建时间"`
  12 + UpdatedAt time.Time `comment:"更新时间"`
  13 + DeletedAt *time.Time `comment:"删除时间"`
  14 +}
  1 +package repository
  2 +
  3 +import (
  4 + "errors"
  5 + "fmt"
  6 + "time"
  7 +
  8 + "github.com/go-pg/pg/v10"
  9 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  10 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
  12 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
  13 +)
  14 +
  15 +type PermissionRepository struct {
  16 + transactionContext *pgTransaction.TransactionContext
  17 +}
  18 +
  19 +func NewPermissionRepository(transactionContext *pgTransaction.TransactionContext) *PermissionRepository {
  20 + return &PermissionRepository{transactionContext: transactionContext}
  21 +}
  22 +
  23 +func (repo *PermissionRepository) TransformToDomain(m *models.Permission) domain.Permission {
  24 + return domain.Permission{
  25 + Id: m.Id,
  26 + CompanyId: m.CompanyId,
  27 + OptHrScore: m.OptHrScore,
  28 + OptEvalScore: m.OptEvalScore,
  29 + CreatedAt: m.CreatedAt.Local(),
  30 + UpdatedAt: m.UpdatedAt.Local(),
  31 + DeletedAt: m.DeletedAt,
  32 + }
  33 +}
  34 +
  35 +func (repo *PermissionRepository) TransformToModel(d *domain.Permission) models.Permission {
  36 + return models.Permission{
  37 + Id: d.Id,
  38 + CompanyId: d.CompanyId,
  39 + OptHrScore: d.OptHrScore,
  40 + OptEvalScore: d.OptEvalScore,
  41 + CreatedAt: d.CreatedAt,
  42 + UpdatedAt: d.UpdatedAt,
  43 + DeletedAt: d.DeletedAt,
  44 + }
  45 +}
  46 +
  47 +func (repo *PermissionRepository) nextIdentify() (int64, error) {
  48 + return utils.NewSnowflakeId()
  49 +}
  50 +
  51 +func (repo *PermissionRepository) Insert(d *domain.Permission) (*domain.Permission, error) {
  52 + var isCreate = d.Id == 0
  53 + if isCreate {
  54 + id, err := repo.nextIdentify()
  55 + if err != nil {
  56 + return d, err
  57 + }
  58 + d.Id = id
  59 + d.CreatedAt = time.Now()
  60 + d.UpdatedAt = d.CreatedAt
  61 + } else {
  62 + d.UpdatedAt = time.Now()
  63 + }
  64 + m := repo.TransformToModel(d)
  65 + tx := repo.transactionContext.PgTx
  66 + var err error
  67 + if isCreate {
  68 + _, err = tx.Model(&m).Returning("id").Insert()
  69 + } else {
  70 + _, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件
  71 + }
  72 + if err != nil {
  73 + return nil, err
  74 + }
  75 + d.Id = m.Id
  76 + return d, nil
  77 +}
  78 +
  79 +func (repo *PermissionRepository) FindOne(queryOptions map[string]interface{}) (*domain.Permission, error) {
  80 + tx := repo.transactionContext.PgTx
  81 + m := new(models.Permission)
  82 + query := tx.Model(m)
  83 + query.Where("deleted_at isnull")
  84 + if v, ok := queryOptions["id"]; ok {
  85 + query.Where("id=?", v)
  86 + }
  87 + if err := query.First(); err != nil {
  88 + if errors.Is(err, pg.ErrNoRows) {
  89 + return nil, fmt.Errorf("没有此资源")
  90 + } else {
  91 + return nil, err
  92 + }
  93 + }
  94 + u := repo.TransformToDomain(m)
  95 + return &u, nil
  96 +}
  97 +
  98 +func (repo *PermissionRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Permission, error) {
  99 + tx := repo.transactionContext.PgTx
  100 + var m []*models.Permission
  101 + query := tx.Model(&m).Where("deleted_at isnull")
  102 +
  103 + if companyId, ok := queryOptions["companyId"]; ok {
  104 + query.Where("company_id = ?", companyId)
  105 + }
  106 +
  107 + if v, ok := queryOptions["limit"].(int64); ok {
  108 + query.Limit(int(v))
  109 + }
  110 + if v, ok := queryOptions["offset"].(int64); ok {
  111 + query.Offset(int(v))
  112 + }
  113 +
  114 + count, err := query.SelectAndCount()
  115 + if err != nil {
  116 + return 0, nil, err
  117 + }
  118 + var arrays []*domain.Permission
  119 + for _, v := range m {
  120 + d := repo.TransformToDomain(v)
  121 + arrays = append(arrays, &d)
  122 + }
  123 + return int64(count), arrays, nil
  124 +}
  125 +
  126 +//func (repo *PermissionRepository) FindByCompanyId(companyId int64) (*domain.Permission, error) {
  127 +// tx := repo.transactionContext.PgTx
  128 +// m := new(models.Permission)
  129 +// query := tx.Model(m)
  130 +// query.Where("deleted_at isnull")
  131 +// query.Where("company_id = ?", companyId)
  132 +// if err := query.First(); err != nil {
  133 +// if errors.Is(err, pg.ErrNoRows) {
  134 +// return nil, fmt.Errorf("没有此资源")
  135 +// } else {
  136 +// return nil, err
  137 +// }
  138 +// }
  139 +// u := repo.TransformToDomain(m)
  140 +// return &u, nil
  141 +//}
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "github.com/linmadan/egglib-go/web/beego"
  6 + service "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/permission"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/permission/command"
  8 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
  9 +)
  10 +
  11 +type PermissionController struct {
  12 + beego.BaseController
  13 +}
  14 +
  15 +func (controller *PermissionController) UpdatePermission() {
  16 + ruService := service.NewPermissionService()
  17 + in := &command.UpdatePermissionCommand{}
  18 + if err := controller.Unmarshal(in); err != nil {
  19 + controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
  20 + } else {
  21 + ua := middlewares.GetUser(controller.Ctx)
  22 + in.CompanyId = ua.CompanyId
  23 + controller.Response(ruService.Update(in))
  24 + }
  25 +}
  26 +
  27 +func (controller *PermissionController) GetPermission() {
  28 + ruService := service.NewPermissionService()
  29 + in := &command.GetPermissionCommand{}
  30 + if err := controller.Unmarshal(in); err != nil {
  31 + controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
  32 + } else {
  33 + ua := middlewares.GetUser(controller.Ctx)
  34 + in.CompanyId = ua.CompanyId
  35 + controller.Response(ruService.Get(in))
  36 + }
  37 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "github.com/linmadan/egglib-go/web/beego/filters"
  6 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/controllers"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
  8 +)
  9 +
  10 +func init() {
  11 + ns := web.NewNamespace("/v1/permission",
  12 + web.NSBefore(filters.AllowCors(), middlewares.CheckAdminToken()),
  13 + web.NSRouter("/update", &controllers.PermissionController{}, "Put:UpdatePermission"),
  14 + web.NSRouter("/get", &controllers.PermissionController{}, "Post:GetPermission"),
  15 + )
  16 + web.AddNamespace(ns)
  17 +}