完成社会服务的增删改查
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user