完成社会服务应用层
This commit is contained in:
@@ -5,6 +5,8 @@ package socialService
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/social_service/internal/model"
|
||||
@@ -30,6 +32,55 @@ func NewCreateSocialServiceLogic(ctx context.Context, cfg *config.Config, model
|
||||
}
|
||||
|
||||
func (l *CreateSocialServiceLogic) CreateSocialService(req *types.CreateSocialServiceReq) (resp *types.CreateSocialServiceResp, err error) {
|
||||
|
||||
return
|
||||
// 1. 初始化数据库模型结构体,映射请求参数
|
||||
data := &model.SocialService{
|
||||
Title: req.Title,
|
||||
Subtitle: req.Subtitle,
|
||||
CoverUrl: req.CoverUrl,
|
||||
Intro: req.Intro,
|
||||
Content: stringToNullString(req.Content),
|
||||
ImageEditors: req.ImageEditors,
|
||||
TextEditors: req.TextEditors,
|
||||
ChiefEditor: req.ChiefEditor,
|
||||
Proofreaders: req.Proofreaders,
|
||||
Reviewers: req.Reviewers,
|
||||
PublishTime: time.Now(), // 显式设置当前时间(与数据库默认值一致,双重保障)
|
||||
// UpdateTime 无需手动设置:数据库会通过 ON UPDATE CURRENT_TIMESTAMP 自动维护
|
||||
}
|
||||
|
||||
// 2. 调用 model 插入数据到数据库
|
||||
result, err := l.model.Insert(context.Background(), data)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("创建社会服务失败, err=%v, req=%+v", err, req)
|
||||
return &types.CreateSocialServiceResp{
|
||||
Code: 500,
|
||||
Msg: "创建失败:数据库插入异常",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 3. 获取插入后的主键 ID
|
||||
newId, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
l.Logger.Errorf("获取新增社会服务ID失败, err=%v", err)
|
||||
return &types.CreateSocialServiceResp{
|
||||
Code: 500,
|
||||
Msg: "创建失败:无法获取新记录ID",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 4. 构造成功响应
|
||||
return &types.CreateSocialServiceResp{
|
||||
Code: 0,
|
||||
Msg: "创建成功",
|
||||
Data: types.CreateSocialServiceData{
|
||||
Id: newId,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func stringToNullString(s string) sql.NullString {
|
||||
if s == "" {
|
||||
return sql.NullString{String: "", Valid: false} // 空字符串 -> 数据库 NULL
|
||||
}
|
||||
return sql.NullString{String: s, Valid: true} // 非空字符串 -> 数据库存储该值
|
||||
}
|
||||
|
||||
@@ -30,7 +30,28 @@ func NewDeleteSocialServiceLogic(ctx context.Context, cfg *config.Config, model
|
||||
}
|
||||
|
||||
func (l *DeleteSocialServiceLogic) DeleteSocialService(req *types.DeleteSocialServiceReq) (resp *types.DeleteSocialServiceResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
// 1. 二次校验 ID 有效性(请求层已校验,此处可选增强)
|
||||
if req.Id <= 0 {
|
||||
l.Logger.Errorf("删除社会服务失败:无效的ID,id=%d", req.Id)
|
||||
return &types.DeleteSocialServiceResp{
|
||||
Code: 400,
|
||||
Msg: "删除失败:ID无效",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 2. 调用 model 执行逻辑删除(更新 is_delete=1)
|
||||
err = l.model.Delete(context.Background(), req.Id)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("删除社会服务失败,id=%d, err=%v", req.Id, err)
|
||||
return &types.DeleteSocialServiceResp{
|
||||
Code: 500,
|
||||
Msg: "删除失败:数据库更新异常",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 3. 构造成功响应
|
||||
return &types.DeleteSocialServiceResp{
|
||||
Code: 0,
|
||||
Msg: "删除成功(逻辑删除)",
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ package socialService
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/social_service/internal/model"
|
||||
@@ -30,7 +31,68 @@ func NewGetSocialServiceLogic(ctx context.Context, cfg *config.Config, model mod
|
||||
}
|
||||
|
||||
func (l *GetSocialServiceLogic) GetSocialService(req *types.GetSocialServiceReq) (resp *types.GetSocialServiceResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
// 1. 二次校验ID有效性
|
||||
if req.Id <= 0 {
|
||||
l.Logger.Errorf("查询社会服务详情失败:无效ID,id=%d", req.Id)
|
||||
return &types.GetSocialServiceResp{
|
||||
Code: 400,
|
||||
Msg: "查询失败:ID无效",
|
||||
Data: types.SocialService{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 2. 调用model查询详情(已过滤is_delete=1的记录)
|
||||
modelData, err := l.model.FindOne(context.Background(), req.Id)
|
||||
if err != nil {
|
||||
// 处理「记录不存在」和「数据库异常」两种错误
|
||||
if err == model.ErrNotFound {
|
||||
l.Logger.Errorf("查询社会服务详情失败:记录不存在,id=%d", req.Id)
|
||||
return &types.GetSocialServiceResp{
|
||||
Code: 404,
|
||||
Msg: "查询失败:未找到该社会服务记录",
|
||||
Data: types.SocialService{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 数据库查询异常
|
||||
l.Logger.Errorf("查询社会服务详情失败,id=%d, err=%v", req.Id, err)
|
||||
return &types.GetSocialServiceResp{
|
||||
Code: 500,
|
||||
Msg: "查询失败:数据库查询异常",
|
||||
Data: types.SocialService{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 3. 转换 model 类型到 types 类型(处理 sql.NullString 转 string)
|
||||
respData := types.SocialService{
|
||||
Id: modelData.Id,
|
||||
Title: modelData.Title, // Title是非空字段,直接赋值
|
||||
Subtitle: modelData.Subtitle,
|
||||
CoverUrl: modelData.CoverUrl,
|
||||
Intro: modelData.Intro,
|
||||
Content: nullStringToString(modelData.Content),
|
||||
ImageEditors: modelData.ImageEditors,
|
||||
TextEditors: modelData.TextEditors,
|
||||
ChiefEditor: modelData.ChiefEditor,
|
||||
Proofreaders: modelData.Proofreaders,
|
||||
Reviewers: modelData.Reviewers,
|
||||
// 时间类型转换:time.Time -> string(格式:yyyy-MM-dd HH:mm:ss)
|
||||
PublishTime: modelData.PublishTime.Format("2006-01-02 15:04:05"),
|
||||
UpdateTime: modelData.UpdateTime.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
|
||||
// 4. 构造成功响应
|
||||
return &types.GetSocialServiceResp{
|
||||
Code: 0,
|
||||
Msg: "查询成功",
|
||||
Data: respData,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 辅助函数:sql.NullString 转 string(适配 model 到 types 的类型转换)
|
||||
func nullStringToString(ns sql.NullString) string {
|
||||
if ns.Valid {
|
||||
return ns.String
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -30,7 +30,62 @@ func NewListSocialServiceLogic(ctx context.Context, cfg *config.Config, model mo
|
||||
}
|
||||
|
||||
func (l *ListSocialServiceLogic) ListSocialService(req *types.ListSocialServiceReq) (resp *types.ListSocialServiceResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
// 1. 分页参数计算(已通过 validate 校验 page≥1,pageSize≥1且≤100)
|
||||
// 2. 调用 model 统计未删除的总条数(用于前端分页)
|
||||
total, err := l.model.Count(context.Background())
|
||||
if err != nil {
|
||||
l.Logger.Errorf("统计社会服务总条数失败, err=%v", err)
|
||||
return &types.ListSocialServiceResp{
|
||||
Code: 500,
|
||||
Msg: "查询失败:统计总条数异常",
|
||||
Data: types.ListSocialServiceData{
|
||||
Total: 0,
|
||||
List: []types.SocialService{},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 3. 调用 model 分页查询当前页数据(过滤 is_delete=0,按ID倒序)
|
||||
modelList, err := l.model.FindPage(context.Background(), req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("分页查询社会服务列表失败, page=%d, pageSize=%d, err=%v", req.Page, req.PageSize, err)
|
||||
return &types.ListSocialServiceResp{
|
||||
Code: 500,
|
||||
Msg: "查询失败:分页数据查询异常",
|
||||
Data: types.ListSocialServiceData{
|
||||
Total: total, // 即使列表查询失败,总条数仍返回(便于前端感知数据总量)
|
||||
List: []types.SocialService{},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 4. model 数据转换为 types 响应数据(处理 sql.NullString + 时间格式)
|
||||
var respList []types.SocialService
|
||||
for _, item := range modelList {
|
||||
respList = append(respList, types.SocialService{
|
||||
Id: item.Id,
|
||||
Title: item.Title,
|
||||
Subtitle: item.Subtitle,
|
||||
CoverUrl: item.CoverUrl,
|
||||
Intro: item.Intro,
|
||||
Content: nullStringToString(item.Content),
|
||||
ImageEditors: item.ImageEditors,
|
||||
TextEditors: item.TextEditors,
|
||||
ChiefEditor: item.ChiefEditor,
|
||||
Proofreaders: item.Proofreaders,
|
||||
Reviewers: item.Reviewers,
|
||||
PublishTime: item.PublishTime.Format("2006-01-02 15:04:05"),
|
||||
UpdateTime: item.UpdateTime.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
// 5. 构造成功响应
|
||||
return &types.ListSocialServiceResp{
|
||||
Code: 0,
|
||||
Msg: "查询成功",
|
||||
Data: types.ListSocialServiceData{
|
||||
Total: total, // 总条数(用于前端分页控件)
|
||||
List: respList, // 当前页数据
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ package socialService
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/social_service/internal/model"
|
||||
@@ -30,6 +31,83 @@ func NewUpdateSocialServiceLogic(ctx context.Context, cfg *config.Config, model
|
||||
}
|
||||
|
||||
func (l *UpdateSocialServiceLogic) UpdateSocialService(req *types.UpdateSocialServiceReq) (resp *types.UpdateSocialServiceResp, err error) {
|
||||
|
||||
return
|
||||
// 1. 校验ID有效性
|
||||
if req.Id <= 0 {
|
||||
l.Logger.Errorf("更新社会服务失败:无效ID,id=%d", req.Id)
|
||||
return &types.UpdateSocialServiceResp{
|
||||
Code: 400,
|
||||
Msg: "更新失败:ID无效",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 2. 查询原记录(original的字段类型为model.SocialService,含sql.NullString)
|
||||
original, err := l.model.FindOne(context.Background(), req.Id)
|
||||
if err != nil {
|
||||
if err == model.ErrNotFound {
|
||||
l.Logger.Errorf("更新社会服务失败:记录不存在,id=%d", req.Id)
|
||||
return &types.UpdateSocialServiceResp{
|
||||
Code: 404,
|
||||
Msg: "更新失败:未找到该记录",
|
||||
}, nil
|
||||
}
|
||||
l.Logger.Errorf("查询原记录失败,id=%d, err=%v", req.Id, err)
|
||||
return &types.UpdateSocialServiceResp{
|
||||
Code: 500,
|
||||
Msg: "更新失败:查询原记录异常",
|
||||
}, nil
|
||||
}
|
||||
|
||||
updateData := &model.SocialService{
|
||||
Id: req.Id,
|
||||
// string类型字段:用处理string的辅助函数
|
||||
Title: updateStringField(req.Title, original.Title),
|
||||
Subtitle: updateStringField(req.Subtitle, original.Subtitle),
|
||||
CoverUrl: updateStringField(req.CoverUrl, original.CoverUrl),
|
||||
Intro: updateStringField(req.Intro, original.Intro),
|
||||
ImageEditors: updateStringField(req.ImageEditors, original.ImageEditors),
|
||||
TextEditors: updateStringField(req.TextEditors, original.TextEditors),
|
||||
ChiefEditor: updateStringField(req.ChiefEditor, original.ChiefEditor),
|
||||
Proofreaders: updateStringField(req.Proofreaders, original.Proofreaders),
|
||||
Reviewers: updateStringField(req.Reviewers, original.Reviewers),
|
||||
// 特殊处理Content(sql.NullString类型)
|
||||
Content: updateNullStringField(req.Content, original.Content),
|
||||
// 保留原发布时间和删除状态
|
||||
PublishTime: original.PublishTime,
|
||||
IsDelete: original.IsDelete,
|
||||
}
|
||||
|
||||
// 4. 执行更新
|
||||
err = l.model.Update(context.Background(), updateData)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("更新社会服务失败,id=%d, err=%v", req.Id, err)
|
||||
return &types.UpdateSocialServiceResp{
|
||||
Code: 500,
|
||||
Msg: "更新失败:数据库操作异常",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 5. 返回成功响应
|
||||
return &types.UpdateSocialServiceResp{
|
||||
Code: 0,
|
||||
Msg: "更新成功",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func updateStringField(newVal, originalVal string) string {
|
||||
if newVal != "" {
|
||||
return newVal // 传入新值则更新
|
||||
}
|
||||
return originalVal // 未传入则保留原值
|
||||
}
|
||||
|
||||
// 辅助函数:处理sql.NullString类型字段的更新(仅适用于Content)
|
||||
// newVal:前端传入的新值(string,可能为空)
|
||||
// originalVal:原记录的值(sql.NullString,数据库存储值)
|
||||
func updateNullStringField(newVal string, originalVal sql.NullString) sql.NullString {
|
||||
if newVal != "" {
|
||||
// 传入新值:转换为有效的sql.NullString
|
||||
return sql.NullString{String: newVal, Valid: true}
|
||||
}
|
||||
// 未传入新值:保留原记录的sql.NullString(可能有效或无效)
|
||||
return originalVal
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ type (
|
||||
FindOne(ctx context.Context, id int64) (*SocialService, error)
|
||||
Update(ctx context.Context, data *SocialService) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
FindPage(ctx context.Context, page, pageSize int) ([]*SocialService, error)
|
||||
Count(ctx context.Context) (int64, error)
|
||||
}
|
||||
|
||||
defaultSocialServiceModel struct {
|
||||
@@ -69,7 +71,7 @@ func (m *defaultSocialServiceModel) Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
func (m *defaultSocialServiceModel) FindOne(ctx context.Context, id int64) (*SocialService, error) {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", socialServiceRows, m.table)
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and `is_delete` = 0 limit 1", socialServiceRows, m.table)
|
||||
var resp SocialService
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
|
||||
switch err {
|
||||
@@ -94,6 +96,25 @@ func (m *defaultSocialServiceModel) Update(ctx context.Context, data *SocialServ
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultSocialServiceModel) Count(ctx context.Context) (int64, error) {
|
||||
query := fmt.Sprintf("select count(*) from %s where `is_delete` = 0", m.table)
|
||||
var total int64
|
||||
err := m.conn.QueryRowCtx(ctx, &total, query)
|
||||
return total, err
|
||||
}
|
||||
|
||||
// 新增:分页查询未删除的社会服务列表
|
||||
func (m *defaultSocialServiceModel) FindPage(ctx context.Context, page, pageSize int) ([]*SocialService, error) {
|
||||
offset := (page - 1) * pageSize // 分页偏移量计算
|
||||
query := fmt.Sprintf("select %s from %s where `is_delete` = 0 order by `id` desc limit ?, ?", socialServiceRows, m.table)
|
||||
var list []*SocialService
|
||||
err := m.conn.QueryRowsCtx(ctx, &list, query, offset, pageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (m *defaultSocialServiceModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user