137 lines
4.4 KiB
Go
137 lines
4.4 KiB
Go
// errorcode.go Day 56:错误码维护(system_error_code 增改停用,A-5.x 运维
|
|
// 接口服务层)。表结构=v1 种子既有(UT:xxxx 体系:code 唯一/name/raw_value/
|
|
// action 处置策略/status 启停);本层只做 CRUD 校验,消费者归一化仍走
|
|
// Day55 纯函数词表(两域解耦:词表判三态,本表管运维语义)。
|
|
package report
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"go-sms/internal/model/entity"
|
|
errs "go-sms/internal/pkg/errors"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// 错误码处置策略合法域(与实体注释对齐:0 失败 1 重发 2 入审)。
|
|
var validActions = map[int8]bool{0: true, 1: true, 2: true}
|
|
|
|
// ErrCodeNotFound 错误码不存在(包装 ISC:404)。
|
|
var ErrCodeNotFound = errs.NotFound.WithMsg("错误码不存在")
|
|
|
|
// ErrorCodeService 错误码维护服务。
|
|
type ErrorCodeService struct{ db *gorm.DB }
|
|
|
|
// NewErrorCodeService 构造。
|
|
func NewErrorCodeService(db *gorm.DB) *ErrorCodeService { return &ErrorCodeService{db: db} }
|
|
|
|
// ErrorCodeQuery 列表筛选(分页;code 前缀/name 模糊/status 精确)。
|
|
type ErrorCodeQuery struct {
|
|
Page, PageSize int
|
|
Code string // 前缀匹配(UT:0 前缀检索)
|
|
Name string // 模糊
|
|
Status *int8 // 1 启用 0 停用(nil=全部)
|
|
}
|
|
|
|
// List 分页查询(code 升序稳定序)。
|
|
func (s *ErrorCodeService) List(ctx context.Context, q ErrorCodeQuery) ([]entity.SystemErrorCode, int64, error) {
|
|
tx := s.db.WithContext(ctx).Model(&entity.SystemErrorCode{})
|
|
if code := strings.TrimSpace(q.Code); code != "" {
|
|
tx = tx.Where("code LIKE ?", code+"%")
|
|
}
|
|
if name := strings.TrimSpace(q.Name); name != "" {
|
|
tx = tx.Where("name LIKE ?", "%"+name+"%")
|
|
}
|
|
if q.Status != nil {
|
|
tx = tx.Where("status = ?", *q.Status)
|
|
}
|
|
if q.Page <= 0 {
|
|
q.Page = 1
|
|
}
|
|
if q.PageSize <= 0 || q.PageSize > 200 {
|
|
q.PageSize = 50
|
|
}
|
|
var total int64
|
|
if err := tx.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
var rows []entity.SystemErrorCode
|
|
if err := tx.Order("code ASC").Offset((q.Page - 1) * q.PageSize).Limit(q.PageSize).
|
|
Find(&rows).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return rows, total, nil
|
|
}
|
|
|
|
// Create 新增(code 唯一;重复返回 ISC:冲突语义错误)。
|
|
func (s *ErrorCodeService) Create(ctx context.Context, code, name, rawValue string, action int8) (*entity.SystemErrorCode, error) {
|
|
code = strings.TrimSpace(code)
|
|
name = strings.TrimSpace(name)
|
|
if code == "" || len(code) > 16 {
|
|
return nil, errs.InvalidParam.WithMsg("code 必填且 ≤16 字符")
|
|
}
|
|
if name == "" || len(name) > 64 {
|
|
return nil, errs.InvalidParam.WithMsg("name 必填且 ≤64 字符")
|
|
}
|
|
if !validActions[action] {
|
|
return nil, errs.InvalidParam.WithMsg("action 非法(0 失败/1 重发/2 入审)")
|
|
}
|
|
var dup int64
|
|
if err := s.db.WithContext(ctx).Model(&entity.SystemErrorCode{}).
|
|
Where("code = ?", code).Count(&dup).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if dup > 0 {
|
|
return nil, errs.InvalidParam.WithMsg("错误码已存在: " + code)
|
|
}
|
|
row := entity.SystemErrorCode{
|
|
Code: code, Name: name, RawValue: strings.TrimSpace(rawValue),
|
|
Action: action, Status: 1,
|
|
}
|
|
if err := s.db.WithContext(ctx).Create(&row).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &row, nil
|
|
}
|
|
|
|
// Update 修改(name/raw_value/action/status;status=0 即停用——停用后
|
|
// 不再参与拦截/重发策略,历史记录保留)。
|
|
func (s *ErrorCodeService) Update(ctx context.Context, id int64, name, rawValue string, action int8, status int8) (*entity.SystemErrorCode, error) {
|
|
name = strings.TrimSpace(name)
|
|
if name == "" || len(name) > 64 {
|
|
return nil, errs.InvalidParam.WithMsg("name 必填且 ≤64 字符")
|
|
}
|
|
if !validActions[action] {
|
|
return nil, errs.InvalidParam.WithMsg("action 非法(0 失败/1 重发/2 入审)")
|
|
}
|
|
if status != 0 && status != 1 {
|
|
return nil, errs.InvalidParam.WithMsg("status 非法(1 启用/0 停用)")
|
|
}
|
|
res := s.db.WithContext(ctx).Model(&entity.SystemErrorCode{}).
|
|
Where("id = ?", id).
|
|
Updates(map[string]any{
|
|
"name": name, "raw_value": strings.TrimSpace(rawValue),
|
|
"action": action, "status": status,
|
|
})
|
|
if res.Error != nil {
|
|
return nil, res.Error
|
|
}
|
|
if res.RowsAffected == 0 {
|
|
var n int64
|
|
_ = s.db.WithContext(ctx).Model(&entity.SystemErrorCode{}).Where("id = ?", id).Count(&n).Error
|
|
if n == 0 {
|
|
return nil, ErrCodeNotFound
|
|
}
|
|
}
|
|
var row entity.SystemErrorCode
|
|
if err := s.db.WithContext(ctx).First(&row, id).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrCodeNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &row, nil
|
|
}
|