完成社会服务的增删改查

This commit is contained in:
2025-10-30 14:24:20 +08:00
parent 019c99238b
commit 9f566027dc
22 changed files with 867 additions and 76 deletions

View File

@@ -29,6 +29,8 @@ type (
FindOne(ctx context.Context, id int64) (*SocialServiceInternship, error)
Update(ctx context.Context, data *SocialServiceInternship) error
Delete(ctx context.Context, id int64) error
Count(ctx context.Context) (int64, error)
ListByPage(ctx context.Context, offset, limit int) ([]*SocialServiceInternship, error)
}
defaultSocialServiceInternshipModel struct {
@@ -62,13 +64,17 @@ func newSocialServiceInternshipModel(conn sqlx.SqlConn) *defaultSocialServiceInt
}
func (m *defaultSocialServiceInternshipModel) Delete(ctx context.Context, id int64) error {
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
// 软删除逻辑更新is_delete=1保留数据同时更新最后修改时间
query := fmt.Sprintf(
"UPDATE %s SET `is_delete` = 1, `update_time` = NOW() WHERE `id` = ? AND `is_delete` = 0",
m.table,
)
_, err := m.conn.ExecCtx(ctx, query, id)
return err
}
func (m *defaultSocialServiceInternshipModel) FindOne(ctx context.Context, id int64) (*SocialServiceInternship, error) {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", socialServiceInternshipRows, m.table)
query := fmt.Sprintf("select %s from %s where `id` = ? and `is_delete` = 0 limit 1", socialServiceInternshipRows, m.table)
var resp SocialServiceInternship
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
switch err {
@@ -93,6 +99,29 @@ func (m *defaultSocialServiceInternshipModel) Update(ctx context.Context, data *
return err
}
func (m *defaultSocialServiceInternshipModel) 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
}
// ListByPage分页查询未删除的记录按ID倒序最新在前
func (m *defaultSocialServiceInternshipModel) ListByPage(ctx context.Context, offset, limit int) ([]*SocialServiceInternship, error) {
query := fmt.Sprintf(
"select %s from %s where `is_delete` = 0 order by `id` desc limit ?, ?",
socialServiceInternshipRows, // 包内全局变量,直接引用(无前缀)
m.table,
)
// 修复点:切片类型去掉 model. 前缀当前在model包内直接使用结构体
var list []*SocialServiceInternship
err := m.conn.QueryRowsCtx(ctx, &list, query, offset, limit)
if err != nil {
return nil, err
}
return list, nil
}
func (m *defaultSocialServiceInternshipModel) tableName() string {
return m.table
}