Files
hldrCenter/server/internal/social_service_internship/internal/logic/socialServiceInternship/updatesocialserviceinternshiplogic.go

124 lines
4.4 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.
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package socialServiceInternship
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/JACKYMYPERSON/hldrCenter/config"
"github.com/JACKYMYPERSON/hldrCenter/internal/social_service_internship/internal/model"
"github.com/JACKYMYPERSON/hldrCenter/internal/social_service_internship/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateSocialServiceInternshipLogic struct {
logx.Logger
ctx context.Context
cfg *config.Config
model model.SocialServiceInternshipModel
}
func NewUpdateSocialServiceInternshipLogic(ctx context.Context, cfg *config.Config, model model.SocialServiceInternshipModel) *UpdateSocialServiceInternshipLogic {
return &UpdateSocialServiceInternshipLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
cfg: cfg,
model: model,
}
}
func (l *UpdateSocialServiceInternshipLogic) UpdateSocialServiceInternship(req *types.UpdateSocialServiceInternshipReq) (resp *types.UpdateSocialServiceInternshipResp, err error) {
// 1. 验证ID合法性路径参数必填且有效
if req.Id <= 0 {
return &types.UpdateSocialServiceInternshipResp{
Code: 400,
Msg: "实习项目ID不合法必须大于0",
}, nil
}
// 2. 查询原记录(确认存在且未被软删除)
oldInternship, err := l.model.FindOne(context.Background(), req.Id)
if err != nil {
if err == model.ErrNotFound {
return &types.UpdateSocialServiceInternshipResp{
Code: 404,
Msg: "未找到该社会服务实习项目(或已被删除)",
}, nil
}
fmt.Printf("查询待更新实习项目失败ID%d错误%v\n", req.Id, err)
return &types.UpdateSocialServiceInternshipResp{
Code: 500,
Msg: "查询项目信息失败,请稍后重试",
}, nil
}
// 3. 构造更新数据(增量更新:仅覆盖请求中提供的非空值,其余保留原记录)
updateData := &model.SocialServiceInternship{
Id: req.Id, // 主键ID用于SQL WHERE条件
// 基础字段:有新值则更新,无则保留原值
Title: getUpdateValue(req.Title, oldInternship.Title),
Subtitle: getUpdateValue(req.Subtitle, oldInternship.Subtitle),
CoverUrl: getUpdateValue(req.CoverUrl, oldInternship.CoverUrl),
Intro: getUpdateValue(req.Intro, oldInternship.Intro),
// 特殊字段string → sql.NullString适配Content字段类型
Content: getNullStringFromUpdate(req.Content, oldInternship.Content),
ImageEditors: getUpdateValue(req.ImageEditors, oldInternship.ImageEditors),
TextEditors: getUpdateValue(req.TextEditors, oldInternship.TextEditors),
ChiefEditor: getUpdateValue(req.ChiefEditor, oldInternship.ChiefEditor),
Proofreaders: getUpdateValue(req.Proofreaders, oldInternship.Proofreaders),
Reviewers: getUpdateValue(req.Reviewers, oldInternship.Reviewers),
// 时间字段:发布时间保留原值,更新时间强制设为当前时间
PublishTime: oldInternship.PublishTime, // 发布时间不随更新改变
UpdateTime: time.Now(), // 最后修改时间自动更新
IsDelete: oldInternship.IsDelete, // 保留删除状态(不允许通过更新接口修改)
}
// 4. 调用Model层执行更新
err = l.model.Update(context.Background(), updateData)
if err != nil {
fmt.Printf("更新社会服务实习项目失败ID%d错误%v\n", req.Id, err)
return &types.UpdateSocialServiceInternshipResp{
Code: 500,
Msg: "项目更新失败,请稍后重试",
}, nil
}
// 5. 构造成功响应
resp = &types.UpdateSocialServiceInternshipResp{
Code: 0,
Msg: "实习项目更新成功",
}
return resp, nil
}
// ------------------------------
// 辅助函数(复用自政府项目模块,无需重复开发)
// ------------------------------
// getUpdateValue处理普通字符串字段的增量更新
func getUpdateValue(newVal, oldVal string) string {
if newVal != "" {
return newVal // 有新值则使用新值
}
return oldVal // 无新值则保留原值
}
// getNullStringFromUpdate处理Content字段的增量更新string → sql.NullString
func getNullStringFromUpdate(newContent string, oldNull sql.NullString) sql.NullString {
if newContent != "" {
// 新值非空包装为有效sql.NullString
return sql.NullString{
String: newContent,
Valid: true,
}
}
// 新值为空保留原记录的sql.NullString状态可能是NULL或有效字符串
return oldNull
}