mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-21 10:13:01 +00:00
refactor🎨 核心业务结构调整
This commit is contained in:
@@ -87,6 +87,7 @@ func (s *SysLoginLogControl) GetId() interface{} {
|
||||
type SysLoginLogById struct {
|
||||
Id int `uri:"id"`
|
||||
Ids []int `json:"ids"`
|
||||
common.ControlBy
|
||||
}
|
||||
|
||||
func (s *SysLoginLogById) GetId() interface{} {
|
||||
@@ -114,4 +115,4 @@ func (s *SysLoginLogById) Generate() *SysLoginLogById {
|
||||
|
||||
func (s *SysLoginLogById) GenerateM() (*models.SysLoginLog, error) {
|
||||
return &models.SysLoginLog{}, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"go-admin/app/admin/models"
|
||||
common "go-admin/common/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/api"
|
||||
|
||||
"go-admin/common/dto"
|
||||
@@ -36,7 +35,7 @@ func (m *SysPostSearch) Bind(ctx *gin.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// SysConfigControl 增、改使用的结构体
|
||||
// SysPostControl 增、改使用的结构体
|
||||
type SysPostControl struct {
|
||||
PostId int `uri:"id" comment:"id"` // id
|
||||
PostName string `form:"postName" comment:"名称"` // 名称
|
||||
@@ -44,39 +43,25 @@ type SysPostControl struct {
|
||||
Sort int `form:"sort" comment:"排序"` // 排序
|
||||
Status int `form:"status" comment:"状态"` // 状态
|
||||
Remark string `form:"remark" comment:"备注"` // 备注
|
||||
}
|
||||
|
||||
// Bind 映射上下文中的结构体数据
|
||||
func (s *SysPostControl) Bind(ctx *gin.Context) error {
|
||||
log := api.GetRequestLogger(ctx)
|
||||
err := ctx.ShouldBindUri(s)
|
||||
if err != nil {
|
||||
log.Debugf("ShouldBindUri error: %s", err.Error())
|
||||
return err
|
||||
}
|
||||
err = ctx.ShouldBindBodyWith(s, binding.JSON)
|
||||
if err != nil {
|
||||
log.Debugf("ShouldBind error: %s", err.Error())
|
||||
}
|
||||
var jsonStr []byte
|
||||
jsonStr, err = json.Marshal(s)
|
||||
if err != nil {
|
||||
log.Debugf("ShouldBind error: %s", err.Error())
|
||||
}
|
||||
ctx.Set("body", string(jsonStr))
|
||||
return err
|
||||
common.ControlBy
|
||||
}
|
||||
|
||||
// Generate 结构体数据转化 从 SysConfigControl 至 system.SysConfig 对应的模型
|
||||
func (s *SysPostControl) Generate() (*models.SysPost, error) {
|
||||
return &models.SysPost{
|
||||
PostId: s.PostId,
|
||||
PostName: s.PostName,
|
||||
PostCode: s.PostCode,
|
||||
Sort: s.Sort,
|
||||
Status: s.Status,
|
||||
Remark: s.Remark,
|
||||
}, nil
|
||||
func (s *SysPostControl) Generate(model *models.SysPost) {
|
||||
if s.PostId != 0 {
|
||||
model.PostId = s.PostId
|
||||
}
|
||||
model.PostName = s.PostName
|
||||
model.PostCode = s.PostCode
|
||||
model.Sort = s.Sort
|
||||
model.Status = s.Status
|
||||
model.Remark = s.Remark
|
||||
if s.ControlBy.UpdateBy != 0 {
|
||||
model.UpdateBy = s.UpdateBy
|
||||
}
|
||||
if s.ControlBy.CreateBy != 0 {
|
||||
model.CreateBy = s.CreateBy
|
||||
}
|
||||
}
|
||||
|
||||
// GetId 获取数据对应的ID
|
||||
@@ -84,15 +69,20 @@ func (s *SysPostControl) GetId() interface{} {
|
||||
return s.PostId
|
||||
}
|
||||
|
||||
// SysConfigById 获取单个或者删除的结构体
|
||||
// SysPostById 获取单个或者删除的结构体
|
||||
type SysPostById struct {
|
||||
Id int `uri:"id"`
|
||||
Ids []int `json:"ids"`
|
||||
common.ControlBy
|
||||
}
|
||||
|
||||
func (s *SysPostById) Generate() *SysPostById {
|
||||
cp := *s
|
||||
return &cp
|
||||
func (s *SysPostById) Generate(model *models.SysPost) {
|
||||
if s.ControlBy.UpdateBy != 0 {
|
||||
model.UpdateBy = s.UpdateBy
|
||||
}
|
||||
if s.ControlBy.CreateBy != 0 {
|
||||
model.CreateBy = s.CreateBy
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SysPostById) GetId() interface{} {
|
||||
@@ -119,4 +109,4 @@ func (s *SysPostById) Bind(ctx *gin.Context) error {
|
||||
|
||||
func (s *SysPostById) GenerateM() (*models.SysPost, error) {
|
||||
return &models.SysPost{}, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,7 @@ type SysUserControl struct {
|
||||
PostId int `json:"postId" comment:"岗位"`
|
||||
Remark string `json:"remark" comment:"备注"`
|
||||
Status string `json:"status" comment:"状态"`
|
||||
common.ControlBy
|
||||
}
|
||||
|
||||
func (s *SysUserControl) Bind(ctx *gin.Context) error {
|
||||
@@ -67,27 +68,22 @@ func (s *SysUserControl) Bind(ctx *gin.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SysUserControl) Generate() dto.Control {
|
||||
cp := *s
|
||||
return &cp
|
||||
}
|
||||
|
||||
func (s *SysUserControl) GenerateM() (common.ActiveRecord, error) {
|
||||
return &models.SysUser{
|
||||
UserId: s.UserId,
|
||||
Username: s.Username,
|
||||
Password: s.Password,
|
||||
NickName: s.NickName,
|
||||
Phone: s.Phone,
|
||||
RoleId: s.RoleId,
|
||||
Avatar: s.Avatar,
|
||||
Sex: s.Sex,
|
||||
Email: s.Email,
|
||||
DeptId: s.DeptId,
|
||||
PostId: s.PostId,
|
||||
Remark: s.Remark,
|
||||
Status: s.Status,
|
||||
}, nil
|
||||
func (s *SysUserControl) Generate(model *models.SysUser) {
|
||||
if s.UserId != 0 {
|
||||
model.UserId = s.UserId
|
||||
}
|
||||
model.Username = s.Username
|
||||
model.Password = s.Password
|
||||
model.NickName = s.NickName
|
||||
model.Phone = s.Phone
|
||||
model.RoleId = s.RoleId
|
||||
model.Avatar = s.Avatar
|
||||
model.Sex = s.Sex
|
||||
model.Email = s.Email
|
||||
model.DeptId = s.DeptId
|
||||
model.PostId = s.PostId
|
||||
model.Remark = s.Remark
|
||||
model.Status = s.Status
|
||||
}
|
||||
|
||||
func (s *SysUserControl) GetId() interface{} {
|
||||
@@ -96,6 +92,7 @@ func (s *SysUserControl) GetId() interface{} {
|
||||
|
||||
type SysUserById struct {
|
||||
dto.ObjectById
|
||||
common.ControlBy
|
||||
}
|
||||
|
||||
func (s *SysUserById) Generate() dto.Control {
|
||||
@@ -119,4 +116,4 @@ func (s *SysUserById) GenerateM() (common.ActiveRecord, error) {
|
||||
type PassWord struct {
|
||||
NewPassword string `json:"newPassword" binding:"required"`
|
||||
OldPassword string `json:"oldPassword" binding:"required"`
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"go-admin/app/admin/models"
|
||||
"go-admin/app/admin/service/dto"
|
||||
cDto "go-admin/common/dto"
|
||||
common "go-admin/common/models"
|
||||
)
|
||||
|
||||
type SysLoginLog struct {
|
||||
@@ -38,10 +37,7 @@ func (e *SysLoginLog) GetSysLoginLogPage(c *dto.SysLoginLogSearch, list *[]model
|
||||
// GetSysLoginLog 获取SysLoginLog对象
|
||||
func (e *SysLoginLog) GetSysLoginLog(d *dto.SysLoginLogById, model *models.SysLoginLog) error {
|
||||
var err error
|
||||
var data models.SysLoginLog
|
||||
|
||||
db := e.Orm.Model(&data).
|
||||
First(model, d.GetId())
|
||||
db := e.Orm.First(model, d.GetId())
|
||||
err = db.Error
|
||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
err = errors.New("查看对象不存在或无权查看")
|
||||
@@ -55,45 +51,12 @@ func (e *SysLoginLog) GetSysLoginLog(d *dto.SysLoginLogById, model *models.SysLo
|
||||
return nil
|
||||
}
|
||||
|
||||
// InsertSysLoginLog 创建SysLoginLog对象
|
||||
func (e *SysLoginLog) InsertSysLoginLog(model common.ActiveRecord) error {
|
||||
var err error
|
||||
var data models.SysLoginLog
|
||||
|
||||
err = e.Orm.Model(&data).
|
||||
Create(model).Error
|
||||
if err != nil {
|
||||
e.Log.Errorf("db error:%s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateSysLoginLog 修改SysLoginLog对象
|
||||
func (e *SysLoginLog) UpdateSysLoginLog(c common.ActiveRecord) error {
|
||||
var err error
|
||||
var data models.SysLoginLog
|
||||
|
||||
db := e.Orm.Model(&data).
|
||||
Where(c.GetId()).Updates(c)
|
||||
if db.Error != nil {
|
||||
e.Log.Errorf("db error:%s", err)
|
||||
return err
|
||||
}
|
||||
if db.RowsAffected == 0 {
|
||||
return errors.New("无权更新该数据")
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveSysLoginLog 删除SysLoginLog
|
||||
func (e *SysLoginLog) RemoveSysLoginLog(d *dto.SysLoginLogById, c common.ActiveRecord) error {
|
||||
func (e *SysLoginLog) RemoveSysLoginLog(c *dto.SysLoginLogById) error {
|
||||
var err error
|
||||
var data models.SysLoginLog
|
||||
|
||||
db := e.Orm.Model(&data).
|
||||
Where(d.Ids).Delete(c)
|
||||
db := e.Orm.Delete(&data, c.Ids)
|
||||
if db.Error != nil {
|
||||
err = db.Error
|
||||
e.Log.Errorf("Delete error: %s", err)
|
||||
@@ -104,4 +67,4 @@ func (e *SysLoginLog) RemoveSysLoginLog(d *dto.SysLoginLogById, c common.ActiveR
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ func (e *SysPost) GetSysPost(d *dto.SysPostById, model *models.SysPost) error {
|
||||
}
|
||||
|
||||
// InsertSysPost 创建SysPost对象
|
||||
func (e *SysPost) InsertSysPost(model *models.SysPost) error {
|
||||
func (e *SysPost) InsertSysPost(model *dto.SysPostControl) error {
|
||||
var err error
|
||||
var data models.SysPost
|
||||
|
||||
@@ -69,12 +69,13 @@ func (e *SysPost) InsertSysPost(model *models.SysPost) error {
|
||||
}
|
||||
|
||||
// UpdateSysPost 修改SysPost对象
|
||||
func (e *SysPost) UpdateSysPost(c *models.SysPost) error {
|
||||
func (e *SysPost) UpdateSysPost(c *dto.SysPostControl) error {
|
||||
var err error
|
||||
var data models.SysPost
|
||||
var model = models.SysPost{}
|
||||
e.Orm.First(&model, c.GetId())
|
||||
c.Generate(&model)
|
||||
|
||||
db := e.Orm.Model(&data).
|
||||
Where(c.GetId()).Updates(c)
|
||||
db := e.Orm.Save(&model)
|
||||
if db.Error != nil {
|
||||
e.Log.Errorf("db error:%s", err)
|
||||
return err
|
||||
|
||||
@@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"errors"
|
||||
"go-admin/app/admin/models"
|
||||
"go-admin/app/admin/service/dto"
|
||||
|
||||
log "github.com/go-admin-team/go-admin-core/logger"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg"
|
||||
@@ -11,7 +12,6 @@ import (
|
||||
|
||||
"go-admin/common/actions"
|
||||
cDto "go-admin/common/dto"
|
||||
common "go-admin/common/models"
|
||||
)
|
||||
|
||||
type SysUser struct {
|
||||
@@ -61,12 +61,11 @@ func (e *SysUser) GetSysUser(d cDto.Control, p *actions.DataPermission, model *m
|
||||
}
|
||||
|
||||
// InsertSysUser 创建SysUser对象
|
||||
func (e *SysUser) InsertSysUser(model common.ActiveRecord) error {
|
||||
func (e *SysUser) InsertSysUser(c *dto.SysUserControl) error {
|
||||
var err error
|
||||
var data models.SysUser
|
||||
|
||||
err = e.Orm.Model(&data).
|
||||
Create(model).Error
|
||||
c.Generate(&data)
|
||||
err = e.Orm.Create(&data).Error
|
||||
if err != nil {
|
||||
e.Log.Errorf("db error: %s", err)
|
||||
return err
|
||||
@@ -75,13 +74,12 @@ func (e *SysUser) InsertSysUser(model common.ActiveRecord) error {
|
||||
}
|
||||
|
||||
// UpdateSysUser 修改SysUser对象
|
||||
func (e *SysUser) UpdateSysUser(c common.ActiveRecord, p *actions.DataPermission) error {
|
||||
func (e *SysUser) UpdateSysUser(c *dto.SysUserControl, p *actions.DataPermission) error {
|
||||
var err error
|
||||
|
||||
db := e.Orm.Model(c).
|
||||
Scopes(
|
||||
actions.Permission(c.TableName(), p),
|
||||
).Where(c.GetId()).Updates(c)
|
||||
var model models.SysUser
|
||||
db := e.Orm.Scopes(
|
||||
actions.Permission(model.TableName(), p),
|
||||
).First(&model, c.GetId())
|
||||
if err = db.Error; err != nil {
|
||||
e.Log.Errorf("Service UpdateSysUser error: %s", err)
|
||||
return err
|
||||
@@ -90,18 +88,24 @@ func (e *SysUser) UpdateSysUser(c common.ActiveRecord, p *actions.DataPermission
|
||||
return errors.New("无权更新该数据")
|
||||
|
||||
}
|
||||
c.Generate(&model)
|
||||
err = e.Orm.Save(&model).Error
|
||||
if err != nil {
|
||||
e.Log.Errorf("Service UpdateSysUser error: %s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveSysUser 删除SysUser
|
||||
func (e *SysUser) RemoveSysUser(d cDto.Control, c common.ActiveRecord, p *actions.DataPermission) error {
|
||||
func (e *SysUser) RemoveSysUser(c *dto.SysUserById, p *actions.DataPermission) error {
|
||||
var err error
|
||||
var data models.SysUser
|
||||
|
||||
db := e.Orm.Model(&data).
|
||||
Scopes(
|
||||
actions.Permission(data.TableName(), p),
|
||||
).Where(d.GetId()).Delete(c)
|
||||
).Delete(&data, c.GetId())
|
||||
if err = db.Error; err != nil {
|
||||
e.Log.Errorf("Delete error: %s", err)
|
||||
return err
|
||||
@@ -157,8 +161,8 @@ func (e *SysUser) UpdateSysUserPwd(id int, oldPassword, newPassword string, p *a
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *SysUser) GetSysUserProfile(id int, user *models.SysUser, roles *[]models.SysRole, posts *[]models.SysPost) error {
|
||||
err := e.Orm.Preload("Dept").First(user, "user_id = ?", id).Error
|
||||
func (e *SysUser) GetSysUserProfile(c *dto.SysUserById, user *models.SysUser, roles *[]models.SysRole, posts *[]models.SysPost) error {
|
||||
err := e.Orm.Preload("Dept").First(user, c.GetId()).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -172,4 +176,4 @@ func (e *SysUser) GetSysUserProfile(id int, user *models.SysUser, roles *[]model
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user