fix version 1
This commit is contained in:
@@ -3,6 +3,7 @@ package method
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/feedback/internal/impl"
|
||||
@@ -13,6 +14,8 @@ import (
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// Modify 修改反馈记录
|
||||
@@ -31,33 +34,37 @@ func Modify(ctx context.Context, in *pb.ModifyRequest) (reply *pb.StatusReply, e
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 构建更新记录
|
||||
// 归属校验:仅本人或管理端可修改
|
||||
exist := new(models.FeedbackItem)
|
||||
if err = impl.DBService.Where("identity = ?", in.GetIdentity()).First(exist).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if !canAccess(auth, exist.PassportIdentity) {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// 构建更新字段:不改写 identity 与归属,避免记录"消失"或归属被转移
|
||||
record := &models.FeedbackItem{
|
||||
Std_IICUDS: types.Std_IICUDS{
|
||||
Identity: utils.UUID(),
|
||||
},
|
||||
Std_Passport: types.Std_Passport{
|
||||
PassportID: auth.ID, // 用户ID
|
||||
PassportIdentity: auth.Identity, // 用户身份标识
|
||||
},
|
||||
UserName: in.GetUserName(), // 用户名
|
||||
Email: in.GetEmail(), // 邮箱
|
||||
Phone: in.GetPhone(), // 手机号
|
||||
Status: in.GetStatus(), // 状态
|
||||
Title: in.GetTitle(), // 标题
|
||||
Content: in.GetContent(), // 内容
|
||||
Category: in.GetCategory(), // 分类
|
||||
Images: make([]models.FeedbackImage, 0, len(in.GetImages())), // 图片列表
|
||||
Accessories: make([]models.FeedbackAccessory, 0, len(in.GetAccessories())), // 附件列表
|
||||
UserName: in.GetUserName(), // 用户名
|
||||
Email: in.GetEmail(), // 邮箱
|
||||
Phone: in.GetPhone(), // 手机号
|
||||
Status: in.GetStatus(), // 状态
|
||||
Title: in.GetTitle(), // 标题
|
||||
Content: in.GetContent(), // 内容
|
||||
Category: in.GetCategory(), // 分类
|
||||
}
|
||||
|
||||
// 处理图片信息
|
||||
images := make([]models.FeedbackImage, 0, len(in.GetImages()))
|
||||
for _, v := range in.GetImages() {
|
||||
identity := v.GetIdentity()
|
||||
if identity == "" {
|
||||
identity = utils.UUID() // 生成新的图片ID
|
||||
}
|
||||
record.Images = append(record.Images, models.FeedbackImage{
|
||||
images = append(images, models.FeedbackImage{
|
||||
Std_IICUDS: types.Std_IICUDS{
|
||||
Identity: identity,
|
||||
},
|
||||
@@ -66,43 +73,56 @@ func Modify(ctx context.Context, in *pb.ModifyRequest) (reply *pb.StatusReply, e
|
||||
})
|
||||
}
|
||||
|
||||
// 处理附件信息
|
||||
// 处理附件信息:子表 identity 为唯一索引,必须逐个赋值,否则多条附件必然唯一键冲突
|
||||
accessories := make([]models.FeedbackAccessory, 0, len(in.GetAccessories()))
|
||||
for _, v := range in.GetAccessories() {
|
||||
record.Accessories = append(record.Accessories, models.FeedbackAccessory{
|
||||
identity := v.GetIdentity()
|
||||
if identity == "" {
|
||||
identity = utils.UUID() // 生成新的附件ID
|
||||
}
|
||||
accessories = append(accessories, models.FeedbackAccessory{
|
||||
Std_IICUDS: types.Std_IICUDS{
|
||||
Identity: identity,
|
||||
},
|
||||
ItemIdentity: in.GetIdentity(), // 关联的反馈记录ID
|
||||
Title: v.Title, // 附件标题
|
||||
FilePath: v.FilePath, // 附件文件路径
|
||||
})
|
||||
}
|
||||
|
||||
// 先删除关联的图片和附件
|
||||
err = impl.DBService.Where("item_identity = ?", in.GetIdentity()).Delete(&models.FeedbackImage{}).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = impl.DBService.Where("item_identity = ?", in.GetIdentity()).Delete(&models.FeedbackAccessory{}).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 更新主记录
|
||||
err = impl.DBService.Where("identity = ?", in.GetIdentity()).Updates(record).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 重新创建关联的图片和附件
|
||||
if len(record.Images) > 0 {
|
||||
err = impl.DBService.Create(&record.Images).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// 主表字段与子表重建放在同一事务内,任一步失败整体回滚
|
||||
err = impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 只更新主表标量字段;Omit 关联后由下面显式重建子表,避免同一次修改写入两遍
|
||||
if err := tx.Model(&models.FeedbackItem{}).
|
||||
Where("identity = ?", in.GetIdentity()).
|
||||
Omit(clause.Associations).
|
||||
Updates(record).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(record.Accessories) > 0 {
|
||||
err = impl.DBService.Create(&record.Accessories).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
// 先删除关联的图片和附件
|
||||
if err := tx.Where("item_identity = ?", in.GetIdentity()).Delete(&models.FeedbackImage{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("item_identity = ?", in.GetIdentity()).Delete(&models.FeedbackAccessory{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 重新创建关联的图片和附件
|
||||
if len(images) > 0 {
|
||||
if err := tx.Create(&images).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(accessories) > 0 {
|
||||
if err := tx.Create(&accessories).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
|
||||
Reference in New Issue
Block a user