93 lines
2.7 KiB
Go
93 lines
2.7 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"prompts-core/dao"
|
|
"prompts-core/model/dto"
|
|
"prompts-core/model/entity"
|
|
)
|
|
|
|
var Prompt = &promptService{}
|
|
|
|
type promptService struct{}
|
|
|
|
func (s *promptService) Create(ctx context.Context, req *dto.CreatePromptReq) (res *dto.CreatePromptRes, err error) {
|
|
// promptInfo 兜底校验:必须可序列化为 JSON
|
|
if req.PromptInfo == nil {
|
|
return nil, errors.New("promptInfo不能为空")
|
|
}
|
|
if _, err := json.Marshal(req.PromptInfo); err != nil {
|
|
return nil, errors.New("promptInfo不是合法JSON")
|
|
}
|
|
if req.ResponseJsonSchema == nil {
|
|
return nil, errors.New("responseJsonSchema不能为空")
|
|
}
|
|
if _, err := json.Marshal(req.ResponseJsonSchema); err != nil {
|
|
return nil, errors.New("responseJsonSchema不是合法JSON")
|
|
}
|
|
|
|
m := &entity.PromptConfig{
|
|
ModelTypeId: req.ModelTypeId,
|
|
ModelType: req.ModelType,
|
|
PromptInfo: req.PromptInfo,
|
|
ResponseJsonSchema: req.ResponseJsonSchema,
|
|
Enabled: 1,
|
|
Version: req.Version,
|
|
}
|
|
|
|
id, err := dao.Prompt.Insert(ctx, m)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.CreatePromptRes{ID: id}, nil
|
|
}
|
|
|
|
func (s *promptService) Update(ctx context.Context, req *dto.UpdatePromptReq) error {
|
|
data := map[string]any{}
|
|
if req.ModelTypeId != nil && *req.ModelTypeId > 0 {
|
|
data[entity.PromptConfigCol.ModelTypeId] = *req.ModelTypeId
|
|
}
|
|
if req.ModelType != nil && *req.ModelType != "" {
|
|
data[entity.PromptConfigCol.ModelType] = *req.ModelType
|
|
}
|
|
if req.PromptInfo != nil {
|
|
if _, err := json.Marshal(req.PromptInfo); err != nil {
|
|
return errors.New("promptInfo不是合法JSON")
|
|
}
|
|
data[entity.PromptConfigCol.PromptInfo] = req.PromptInfo
|
|
}
|
|
if req.ResponseJsonSchema != nil {
|
|
if _, err := json.Marshal(req.ResponseJsonSchema); err != nil {
|
|
return errors.New("responseJsonSchema不是合法JSON")
|
|
}
|
|
data[entity.PromptConfigCol.ResponseJsonSchema] = req.ResponseJsonSchema
|
|
}
|
|
if req.Enabled != nil {
|
|
data[entity.PromptConfigCol.Enabled] = *req.Enabled
|
|
}
|
|
if req.Version != nil {
|
|
data[entity.PromptConfigCol.Version] = *req.Version
|
|
}
|
|
if len(data) == 0 {
|
|
return errors.New("无可更新字段")
|
|
}
|
|
_, err := dao.Prompt.UpdateByID(ctx, req.ID, data)
|
|
return err
|
|
}
|
|
|
|
func (s *promptService) Delete(ctx context.Context, id int64) error {
|
|
_, err := dao.Prompt.DeleteByID(ctx, id)
|
|
return err
|
|
}
|
|
|
|
func (s *promptService) Get(ctx context.Context, id int64) (*entity.PromptConfig, error) {
|
|
return dao.Prompt.GetByID(ctx, id)
|
|
}
|
|
|
|
func (s *promptService) List(ctx context.Context, pageNum, pageSize int, modelTypeID *int, modelTypeLike string) (list []*entity.PromptConfig, total int64, err error) {
|
|
return dao.Prompt.List(ctx, pageNum, pageSize, modelTypeID, modelTypeLike)
|
|
}
|