2026-08-09 10:43:45 +08:00
|
|
|
|
// Package pages 提供页面相关的业务逻辑处理
|
|
|
|
|
|
// 包括页面的创建、修改、删除、查询等功能
|
|
|
|
|
|
package pages
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2026-08-09 12:41:08 +08:00
|
|
|
|
"bsm/full/module/base/cms/internal/impl"
|
|
|
|
|
|
"bsm/full/module/base/cms/internal/models"
|
|
|
|
|
|
"bsm/full/module/base/cms/internal/utils"
|
|
|
|
|
|
pb "bsm/full/module/base/cms/pb"
|
2026-08-09 10:43:45 +08:00
|
|
|
|
"git.apinb.com/bsm-sdk/core/errcode"
|
|
|
|
|
|
"git.apinb.com/bsm-sdk/core/service"
|
|
|
|
|
|
"git.apinb.com/bsm-sdk/core/types"
|
|
|
|
|
|
sdkUtils "git.apinb.com/bsm-sdk/core/utils"
|
|
|
|
|
|
"git.apinb.com/bsm-sdk/core/vars"
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Create 创建新页面
|
|
|
|
|
|
// 验证用户身份和页面数据,创建页面记录并关联标签、附件等信息
|
|
|
|
|
|
// 参数:
|
|
|
|
|
|
// - ctx: 请求上下文,包含用户身份信息
|
|
|
|
|
|
// - in: 页面数据,包含标题、内容、标签等
|
|
|
|
|
|
//
|
|
|
|
|
|
// 返回:
|
|
|
|
|
|
// - reply: 操作结果,包含页面ID和时间戳
|
|
|
|
|
|
// - err: 错误信息
|
|
|
|
|
|
func Create(ctx context.Context, in *pb.PagesItem) (reply *pb.StatusReply, err error) {
|
|
|
|
|
|
// 解析请求上下文,验证用户身份
|
|
|
|
|
|
_, err = service.ParseMetaCtx(ctx, nil)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 验证必填字段
|
|
|
|
|
|
if in.GetSiteIdentity() == "" || in.GetTitle() == "" || in.GetContent() == "" || in.GetKey() == "" {
|
|
|
|
|
|
return nil, errcode.ErrInvalidArgument
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 构建页面数据模型
|
|
|
|
|
|
var data = &models.CmsPages{
|
|
|
|
|
|
SiteIdentity: in.SiteIdentity,
|
|
|
|
|
|
Title: in.Title,
|
|
|
|
|
|
Key: utils.FormatKey(in.Key), // 格式化页面键值,确保唯一性
|
|
|
|
|
|
Description: in.Description,
|
|
|
|
|
|
CoverPath: in.CoverPath,
|
|
|
|
|
|
Content: in.Content,
|
|
|
|
|
|
HasAccessory: in.HasAccessory,
|
|
|
|
|
|
AccessoryIdentityArray: in.AccessoryIdentityArray,
|
|
|
|
|
|
TagsIdentityArray: in.TagsIdentityArray,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 生成页面唯一标识
|
|
|
|
|
|
data.Identity = sdkUtils.UUID()
|
|
|
|
|
|
|
|
|
|
|
|
// 保存页面数据,包括关联的标签、附件信息
|
|
|
|
|
|
err = AddPostPages(data, in.AccessoryData, in.TagsIdentityArray)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errcode.ErrDB
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 返回成功响应
|
|
|
|
|
|
return &pb.StatusReply{
|
|
|
|
|
|
Code: 0,
|
|
|
|
|
|
Message: vars.OK,
|
|
|
|
|
|
Details: data.Identity,
|
|
|
|
|
|
Timeseq: time.Now().UnixMilli(),
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AddPostPages 添加页面及其关联数据
|
|
|
|
|
|
// 使用事务确保数据一致性,同时创建页面、标签关联和附件信息
|
|
|
|
|
|
// 参数:
|
|
|
|
|
|
// - pages: 页面数据模型
|
|
|
|
|
|
// - accessory: 附件数据列表
|
|
|
|
|
|
// - tags: 标签标识列表
|
|
|
|
|
|
//
|
|
|
|
|
|
// 返回:
|
|
|
|
|
|
// - err: 错误信息
|
|
|
|
|
|
func AddPostPages(pages *models.CmsPages, accessory []*pb.AccessoryItem, tags []string) (err error) {
|
|
|
|
|
|
var (
|
|
|
|
|
|
accessoryPage = make([]models.CmsAccessory, 0) // 页面附件数据
|
|
|
|
|
|
TagsPage = make([]models.CmsRelateTags, 0) // 页面标签关联数据
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 构建标签关联数据
|
|
|
|
|
|
for _, val := range tags {
|
|
|
|
|
|
TagsPage = append(TagsPage, models.CmsRelateTags{
|
|
|
|
|
|
Identity: sdkUtils.ULID(),
|
|
|
|
|
|
PagesIdentity: pages.Identity,
|
|
|
|
|
|
TagsIdentity: val,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 使用事务确保数据一致性
|
|
|
|
|
|
return impl.DBService.Transaction(func(tx *gorm.DB) error {
|
|
|
|
|
|
// 创建页面记录
|
|
|
|
|
|
if err = tx.Create(pages).Error; err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建附件关联记录
|
|
|
|
|
|
if len(accessory) != 0 {
|
|
|
|
|
|
for _, v := range accessory {
|
|
|
|
|
|
accessoryPage = append(accessoryPage, models.CmsAccessory{
|
|
|
|
|
|
Std_Identity: types.Std_Identity{Identity: sdkUtils.UUID()},
|
|
|
|
|
|
PagesId: pages.ID,
|
|
|
|
|
|
PagesIdentity: pages.Identity,
|
|
|
|
|
|
FilePath: v.FilePath,
|
|
|
|
|
|
Title: v.Title,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := tx.Create(&accessoryPage).Error; err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建标签关联记录
|
|
|
|
|
|
if len(tags) != 0 {
|
|
|
|
|
|
if err := tx.Create(&TagsPage).Error; err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|