refactor: reorganize modules and add Linux build tooling
This commit is contained in:
89
module/base/mgt/internal/logic/application/create.go
Normal file
89
module/base/mgt/internal/logic/application/create.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"bsm/full/module/base/mgt/internal/libs"
|
||||
"bsm/full/module/base/mgt/internal/models"
|
||||
"bsm/full/module/base/mgt/internal/types"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
coreTypes "git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Create(c *gin.Context) {
|
||||
var (
|
||||
request = types.ApplicationRequest{}
|
||||
data = models.MgtApplication{}
|
||||
)
|
||||
|
||||
// 解析JSON请求
|
||||
err := c.ShouldBindJSON(&request)
|
||||
if err != nil {
|
||||
infra.Response.Error(c, errcode.ErrJsonUnmarshal)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := libs.ValidateStruct(&request); err != nil {
|
||||
infra.Response.Error(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
// 判断应用是否已经存在(包括软删除的记录)
|
||||
db := models.DBService.Model(&models.MgtApplication{})
|
||||
if err = db.Where("title = ? or workspace = ?", request.Title, request.Workspace).First(&data).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
infra.Response.Error(c, errcode.ErrDB)
|
||||
return
|
||||
}
|
||||
// 未找到记录(包括软删除的),检查是否有软删除的记录
|
||||
var deletedData models.MgtApplication
|
||||
// 检查 workspace 是否被软删除(workspace 有唯一索引)
|
||||
if request.Workspace != "" {
|
||||
if err = models.DBService.Unscoped().Where("workspace = ?", request.Workspace).First(&deletedData).Error; err == nil {
|
||||
// 存在软删除的记录,先物理删除它,以便可以重新创建
|
||||
if err = models.DBService.Unscoped().Delete(&deletedData).Error; err != nil {
|
||||
infra.Response.Error(c, errcode.ErrDB)
|
||||
return
|
||||
}
|
||||
printer.Info("已物理删除软删除的应用记录: ID=%d, workspace=%s", deletedData.ID, deletedData.Workspace)
|
||||
}
|
||||
}
|
||||
// 检查 title 是否被软删除(如果 title 和 workspace 不同)
|
||||
if request.Title != "" && request.Title != request.Workspace {
|
||||
if err = models.DBService.Unscoped().Where("title = ?", request.Title).First(&deletedData).Error; err == nil {
|
||||
// 存在软删除的记录,先物理删除它
|
||||
if err = models.DBService.Unscoped().Delete(&deletedData).Error; err != nil {
|
||||
infra.Response.Error(c, errcode.ErrDB)
|
||||
return
|
||||
}
|
||||
printer.Info("已物理删除软删除的应用记录: ID=%d, title=%s", deletedData.ID, deletedData.Title)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 找到未删除的记录,说明应用已存在
|
||||
printer.Info("应用已存在: title=%s, workspace=%s", request.Title, request.Workspace)
|
||||
infra.Response.Error(c, errcode.ErrAlreadyExists)
|
||||
return
|
||||
}
|
||||
|
||||
// 新增应用
|
||||
var dataModel = models.MgtApplication{
|
||||
Std_IICUDS: coreTypes.Std_IICUDS{Identity: utils.UUID(), Status: 1},
|
||||
Title: request.Title,
|
||||
Workspace: request.Workspace,
|
||||
Description: request.Description,
|
||||
}
|
||||
if err = models.DBService.Model(&models.MgtApplication{}).Create(&dataModel).Error; err != nil {
|
||||
infra.Response.Error(c, errcode.ErrDB)
|
||||
return
|
||||
}
|
||||
|
||||
printer.Info("应用创建成功: ID=%d, workspace=%s", dataModel.ID, dataModel.Workspace)
|
||||
infra.Response.Success(c, types.IdResp{Id: dataModel.ID, Identity: dataModel.Identity})
|
||||
}
|
||||
89
module/base/mgt/internal/logic/application/del.go
Normal file
89
module/base/mgt/internal/logic/application/del.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"bsm/full/module/base/mgt/internal/libs"
|
||||
"bsm/full/module/base/mgt/internal/models"
|
||||
"bsm/full/module/base/mgt/internal/types"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Del(c *gin.Context) {
|
||||
var (
|
||||
request = types.ApplicationRequest{}
|
||||
)
|
||||
|
||||
err := c.ShouldBindJSON(&request)
|
||||
if err != nil {
|
||||
infra.Response.Error(c, errcode.ErrJsonUnmarshal)
|
||||
return
|
||||
}
|
||||
|
||||
if err := libs.ValidateStruct(&request); err != nil {
|
||||
infra.Response.Error(c, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
|
||||
if request.ID == 0 {
|
||||
printer.Error("删除应用参数异常: ID不能为空")
|
||||
infra.Response.Error(c, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
|
||||
appId := request.ID
|
||||
var dptIds []uint
|
||||
|
||||
if err := models.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Model(&models.MgtDepartment{}).Where("app_id = ?", appId).Pluck("id", &dptIds).Error; err != nil {
|
||||
printer.Error("查询应用部门异常: %v", err)
|
||||
return err
|
||||
}
|
||||
if len(dptIds) > 0 {
|
||||
if err := tx.Where("dpt_id in ?", dptIds).Delete(&models.MgtLinkUserDpt{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("dpt_id in ?", dptIds).Delete(&models.MgtLinkDptRole{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("dpt_id in ?", dptIds).Delete(&models.MgtLinkDptPmn{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("id in ?", dptIds).Delete(&models.MgtDepartment{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Where("app_id = ?", appId).Delete(&models.MgtLinkUserApp{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("app_id = ?", appId).Delete(&models.MgtLinkRoleApp{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("app_id = ?", appId).Delete(&models.MgtLinkUserPmn{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("app_id = ?", appId).Delete(&models.MgtLinkRolePmn{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("app_id = ?", appId).Delete(&models.MgtLinkDptPmn{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("app_id = ?", appId).Delete(&models.MgtPermission{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Delete(&models.MgtApplication{}, appId).Error; err != nil {
|
||||
printer.Error("删除应用异常: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
infra.Response.Error(c, errcode.ErrDB)
|
||||
return
|
||||
}
|
||||
|
||||
printer.Info("应用删除成功: ID=%d", request.ID)
|
||||
infra.Response.Success(c, types.IdResp{Id: request.ID})
|
||||
}
|
||||
51
module/base/mgt/internal/logic/application/detail.go
Normal file
51
module/base/mgt/internal/logic/application/detail.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"bsm/full/module/base/mgt/internal/libs"
|
||||
"bsm/full/module/base/mgt/internal/models"
|
||||
"bsm/full/module/base/mgt/internal/types"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Detail(c *gin.Context) {
|
||||
var (
|
||||
request = types.ApplicationRequest{}
|
||||
data = types.ApplicationRsp{}
|
||||
)
|
||||
|
||||
// 解析JSON请求
|
||||
err := c.ShouldBindJSON(&request)
|
||||
if err != nil {
|
||||
infra.Response.Error(c, errcode.ErrJsonUnmarshal)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := libs.ValidateStruct(&request); err != nil {
|
||||
infra.Response.Error(c, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
|
||||
if request.ID == 0 {
|
||||
printer.Error("应用ID不能为空")
|
||||
infra.Response.Error(c, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
|
||||
if err = models.DBService.Model(&models.MgtApplication{}).Where("id = ?", request.ID).First(&data).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
infra.Response.Error(c, errcode.ErrRecordNotFound)
|
||||
return
|
||||
}
|
||||
infra.Response.Error(c, errcode.ErrDB)
|
||||
return
|
||||
}
|
||||
|
||||
infra.Response.Success(c, data)
|
||||
}
|
||||
49
module/base/mgt/internal/logic/application/fetch.go
Normal file
49
module/base/mgt/internal/logic/application/fetch.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"bsm/full/module/base/mgt/internal/libs"
|
||||
"bsm/full/module/base/mgt/internal/logic/tool"
|
||||
"bsm/full/module/base/mgt/internal/models"
|
||||
"bsm/full/module/base/mgt/internal/types"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Fetch(c *gin.Context) {
|
||||
var (
|
||||
request = types.ApplicationRequest{}
|
||||
count int64 = 0
|
||||
data = make([]types.ApplicationRsp, 0)
|
||||
db = models.DBService.Model(&models.MgtApplication{})
|
||||
)
|
||||
|
||||
// 解析JSON请求
|
||||
err := c.ShouldBindJSON(&request)
|
||||
if err != nil {
|
||||
infra.Response.Error(c, errcode.ErrJsonUnmarshal)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := libs.ValidateStruct(&request); err != nil {
|
||||
infra.Response.Error(c, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
|
||||
if request.Keyword != "" {
|
||||
likeStr := "%" + request.Keyword + "%"
|
||||
db = db.Where("title like ? or code like ? ", likeStr)
|
||||
}
|
||||
if request.Status != 0 {
|
||||
db = db.Where("status = ? ", request.Status)
|
||||
}
|
||||
|
||||
page, size := tool.GetSizeAndPage(request.Page, request.Size)
|
||||
if err := db.Count(&count).Limit(size).Offset((page - 1) * size).Find(&data).Error; err != nil {
|
||||
infra.Response.Error(c, errcode.ErrDB)
|
||||
return
|
||||
}
|
||||
|
||||
infra.Response.Success(c, types.FetchResp{Data: data, Page: page, Size: size, Total: count})
|
||||
}
|
||||
132
module/base/mgt/internal/logic/application/fetch_other.go
Normal file
132
module/base/mgt/internal/logic/application/fetch_other.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"bsm/full/module/base/mgt/internal/libs"
|
||||
"bsm/full/module/base/mgt/internal/logic/tool"
|
||||
"bsm/full/module/base/mgt/internal/models"
|
||||
"bsm/full/module/base/mgt/internal/types"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 获取应用用户列表
|
||||
func UserFetch(c *gin.Context) {
|
||||
var (
|
||||
request = types.FetchBase{}
|
||||
data = models.MgtApplication{}
|
||||
db = models.DBService.Model(&models.MgtApplication{}).Select("id", "identity", "title")
|
||||
count int64 = 0
|
||||
)
|
||||
err := c.ShouldBindJSON(&request)
|
||||
if err != nil {
|
||||
infra.Response.Error(c, errcode.ErrJsonUnmarshal)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := libs.ValidateStruct(&request); err != nil {
|
||||
infra.Response.Error(c, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
|
||||
if request.Workspace == "" {
|
||||
printer.Error("Workspace不能为空")
|
||||
infra.Response.Error(c, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
if request.Status != 0 {
|
||||
db = db.Where("status = ?", request.Status)
|
||||
}
|
||||
|
||||
page, size := tool.GetSizeAndPage(request.Page, request.Size)
|
||||
if err := db.Where("workspace = ?", request.Workspace).Preload("Users", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Select("id", "identity", "name", "account", "phone", "email", "avatar").Count(&count).Limit(size).Offset((page - 1) * size)
|
||||
}).Find(&data).Error; err != nil {
|
||||
infra.Response.Error(c, errcode.ErrDB)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(c, types.FetchResp{Data: data, Page: page, Size: size, Total: count})
|
||||
}
|
||||
|
||||
// 获取应用角色列表
|
||||
func RoleFetch(c *gin.Context) {
|
||||
var (
|
||||
request = types.FetchBase{}
|
||||
data = make([]models.MgtApplication, 0)
|
||||
db = models.DBService.Model(&models.MgtApplication{}).Select("id", "identity", "title")
|
||||
count int64 = 0
|
||||
)
|
||||
err := c.ShouldBindJSON(&request)
|
||||
if err != nil {
|
||||
infra.Response.Error(c, errcode.ErrJsonUnmarshal)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := libs.ValidateStruct(&request); err != nil {
|
||||
infra.Response.Error(c, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
|
||||
if request.Workspace == "" {
|
||||
printer.Error("Workspace不能为空")
|
||||
infra.Response.Error(c, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
if request.Status != 0 {
|
||||
db = db.Where("status = ?", request.Status)
|
||||
}
|
||||
if request.ID != 0 {
|
||||
db = db.Where("id = ?", request.ID)
|
||||
}
|
||||
page, size := tool.GetSizeAndPage(request.Page, request.Size)
|
||||
if err := db.Where("workspace = ?", request.Workspace).Preload("Roles", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Select("id", "identity", "name")
|
||||
}).Count(&count).Limit(size).Offset((page - 1) * size).Find(&data).Error; err != nil {
|
||||
infra.Response.Error(c, errcode.ErrDB)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(c, types.FetchResp{Data: data, Page: page, Size: size, Total: count})
|
||||
}
|
||||
|
||||
// 获取应用权限列表
|
||||
func PermissionFetch(c *gin.Context) {
|
||||
var (
|
||||
request = types.FetchBase{}
|
||||
data = make([]models.MgtApplication, 0)
|
||||
db = models.DBService.Model(&models.MgtApplication{}).Select("id", "identity", "title")
|
||||
count int64 = 0
|
||||
)
|
||||
err := c.ShouldBindJSON(&request)
|
||||
if err != nil {
|
||||
infra.Response.Error(c, errcode.ErrJsonUnmarshal)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := libs.ValidateStruct(&request); err != nil {
|
||||
infra.Response.Error(c, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
|
||||
if request.Workspace == "" {
|
||||
printer.Error("Workspace不能为空")
|
||||
infra.Response.Error(c, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
if request.Status != 0 {
|
||||
db = db.Where("status = ?", request.Status)
|
||||
}
|
||||
if request.ID != 0 {
|
||||
db = db.Where("id = ?", request.ID)
|
||||
}
|
||||
page, size := tool.GetSizeAndPage(request.Page, request.Size)
|
||||
if err := db.Where("workspace = ?", request.Workspace).Preload("Permissions").Count(&count).Limit(size).Offset((page - 1) * size).Find(&data).Error; err != nil {
|
||||
infra.Response.Error(c, errcode.ErrDB)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(c, types.FetchResp{Data: data, Page: page, Size: size, Total: count})
|
||||
}
|
||||
71
module/base/mgt/internal/logic/application/modify.go
Normal file
71
module/base/mgt/internal/logic/application/modify.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"bsm/full/module/base/mgt/internal/libs"
|
||||
"bsm/full/module/base/mgt/internal/models"
|
||||
"bsm/full/module/base/mgt/internal/types"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Midify(c *gin.Context) {
|
||||
var (
|
||||
request = types.ApplicationRequest{}
|
||||
data = models.MgtApplication{}
|
||||
)
|
||||
|
||||
// 解析JSON请求
|
||||
err := c.ShouldBindJSON(&request)
|
||||
if err != nil {
|
||||
infra.Response.Error(c, errcode.ErrJsonUnmarshal)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := libs.ValidateStruct(&request); err != nil {
|
||||
infra.Response.Error(c, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
|
||||
if request.ID == 0 {
|
||||
printer.Error("更新应用参数异常: ID不能为空")
|
||||
infra.Response.Error(c, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
|
||||
// 检查workspace是否已被其他应用使用
|
||||
if request.Workspace != "" {
|
||||
if err := models.DBService.Model(&models.MgtApplication{}).Where("workspace=? and id<>?", request.Workspace, request.ID).First(&data).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
infra.Response.Error(c, errcode.ErrDB)
|
||||
return
|
||||
}
|
||||
}
|
||||
if data.ID != 0 {
|
||||
printer.Info("工作空间已被使用: workspace=%s", request.Workspace)
|
||||
infra.Response.Error(c, errcode.ErrAlreadyExists)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var dataModel = models.MgtApplication{
|
||||
Title: request.Title,
|
||||
Workspace: request.Workspace,
|
||||
Description: request.Description,
|
||||
}
|
||||
if request.Status != 0 {
|
||||
dataModel.Status = request.Status
|
||||
}
|
||||
if err := models.DBService.Model(&models.MgtApplication{}).Where("id = ?", request.ID).Updates(&dataModel).Error; err != nil {
|
||||
infra.Response.Error(c, errcode.ErrDB)
|
||||
return
|
||||
}
|
||||
|
||||
printer.Info("应用更新成功: ID=%d", request.ID)
|
||||
infra.Response.Success(c, types.IdResp{Id: request.ID, Identity: dataModel.Identity})
|
||||
}
|
||||
Reference in New Issue
Block a user