优化结构
This commit is contained in:
@@ -3,16 +3,15 @@ package system
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"kra/internal/biz/system"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type positionRepo struct{ data Provider }
|
||||
|
||||
func NewPositionRepo(data Provider) biz.PositionRepo { return &positionRepo{data: data} }
|
||||
func NewPositionRepo(data Provider) system.PositionRepo { return &positionRepo{data: data} }
|
||||
|
||||
type positionPO struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
@@ -37,13 +36,13 @@ type userPositionPO struct {
|
||||
|
||||
func (userPositionPO) TableName() string { return "sys_user_positions" }
|
||||
|
||||
func posFromPO(po positionPO) *biz.Position {
|
||||
return &biz.Position{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, Name: po.Name, Code: po.Code, Sort: po.Sort, Status: po.Status, Remark: po.Remark}
|
||||
func posFromPO(po positionPO) *system.Position {
|
||||
return &system.Position{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, Name: po.Name, Code: po.Code, Sort: po.Sort, Status: po.Status, Remark: po.Remark}
|
||||
}
|
||||
func (r *positionRepo) CreatePosition(ctx context.Context, v *biz.Position) error {
|
||||
func (r *positionRepo) CreatePosition(ctx context.Context, v *system.Position) error {
|
||||
return r.data.DB().WithContext(ctx).Create(&positionPO{Name: v.Name, Code: v.Code, Sort: v.Sort, Status: v.Status, Remark: v.Remark}).Error
|
||||
}
|
||||
func (r *positionRepo) UpdatePosition(ctx context.Context, v *biz.Position) error {
|
||||
func (r *positionRepo) UpdatePosition(ctx context.Context, v *system.Position) error {
|
||||
return r.data.DB().WithContext(ctx).Model(&positionPO{}).Where("id = ?", v.ID).Updates(map[string]any{"name": v.Name, "code": v.Code, "sort": v.Sort, "status": v.Status, "remark": v.Remark}).Error
|
||||
}
|
||||
func (r *positionRepo) DeletePosition(ctx context.Context, id uint) error {
|
||||
@@ -59,14 +58,14 @@ func (r *positionRepo) DeletePosition(ctx context.Context, id uint) error {
|
||||
}
|
||||
return r.data.DB().WithContext(ctx).Delete(&positionPO{}, id).Error
|
||||
}
|
||||
func (r *positionRepo) FindPosition(ctx context.Context, id uint) (*biz.Position, error) {
|
||||
func (r *positionRepo) FindPosition(ctx context.Context, id uint) (*system.Position, error) {
|
||||
var po positionPO
|
||||
if err := r.data.DB().WithContext(ctx).First(&po, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return posFromPO(po), nil
|
||||
}
|
||||
func (r *positionRepo) ListPositions(ctx context.Context, page, size int, q *biz.PositionListFilter) ([]*biz.Position, int64, error) {
|
||||
func (r *positionRepo) ListPositions(ctx context.Context, page, size int, q *system.PositionListFilter) ([]*system.Position, int64, error) {
|
||||
db := r.data.DB().WithContext(ctx).Model(&positionPO{})
|
||||
if q != nil {
|
||||
if q.Name != "" {
|
||||
@@ -90,7 +89,7 @@ func (r *positionRepo) ListPositions(ctx context.Context, page, size int, q *biz
|
||||
if err := db.Order("sort").Limit(size).Offset(size * (page - 1)).Find(&pos).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out := make([]*biz.Position, 0, len(pos))
|
||||
out := make([]*system.Position, 0, len(pos))
|
||||
for _, po := range pos {
|
||||
out = append(out, posFromPO(po))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user