Files
full/module/social/group/internal/logic/basic/modify.go

73 lines
1.9 KiB
Go
Raw Normal View History

package basic
import (
"context"
"time"
2026-08-10 11:42:45 +08:00
"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
}
2026-09-22 21:15:34 +08:00
if in.GetIdentity() == "" {
return nil, errcode.ErrInvalidArgument
}
2026-09-22 21:15:34 +08:00
// 校验群组存在,且操作者必须是创建者
var group models.GroupBasic
if err = impl.DBService.Where("identity=?", in.Identity).First(&group).Error; err != nil {
printer.Error(err.Error())
2026-09-22 21:15:34 +08:00
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
}