Files
full/module/social/group/internal/logic/basic/modify.go
2026-09-22 21:15:34 +08:00

73 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package basic
import (
"context"
"time"
"bsm/full/module/social/group/internal/impl"
"bsm/full/module/social/group/internal/models"
pb "bsm/full/module/social/group/pb"
"git.apinb.com/bsm-sdk/core/errcode"
"git.apinb.com/bsm-sdk/core/printer"
"git.apinb.com/bsm-sdk/core/service"
"git.apinb.com/bsm-sdk/core/vars"
)
// 修改群组信息
func Modify(ctx context.Context, in *pb.GroupItem) (reply *pb.DataStatusReply, err error) {
auth, err := service.ParseMetaCtx(ctx, nil)
if err != nil {
return nil, err
}
if in.GetIdentity() == "" {
return nil, errcode.ErrInvalidArgument
}
// 校验群组存在,且操作者必须是创建者
var group models.GroupBasic
if err = impl.DBService.Where("identity=?", in.Identity).First(&group).Error; err != nil {
printer.Error(err.Error())
return nil, errcode.ErrRecordNotFound
}
if group.PassportID != auth.ID {
return nil, errcode.ErrPermissionDenied
}
// 仅更新请求中确实提供的字段,避免零值覆盖;同时不再改写群 identity
updates := map[string]any{}
if v := in.GetName(); v != "" {
updates["name"] = v
}
if v := in.GetIntroduce(); v != "" {
updates["introduce"] = v
}
if v := in.GetAvatar(); v != "" {
updates["avatar"] = v
}
if v := in.GetBackground(); v != "" {
updates["background"] = v
}
if v := in.GetNotice(); v != "" {
updates["notice"] = v
}
// 布尔字段无法区分「未传」与「false」仅在为 true 时更新,避免误清空已有开关
if in.GetEnableSearchByNumber() {
updates["enable_search_by_number"] = true
}
if in.GetEnableSearchByName() {
updates["enable_search_by_name"] = true
}
if len(updates) > 0 {
if err = impl.DBService.Model(&models.GroupBasic{}).Where("identity=?", in.Identity).Updates(updates).Error; err != nil {
printer.Error(err.Error())
return nil, errcode.ErrDB
}
}
return &pb.DataStatusReply{
Data: vars.OK,
Timeseq: time.Now().UnixMilli(),
}, nil
}