104 lines
3.1 KiB
Go
104 lines
3.1 KiB
Go
package biz
|
|
|
|
import (
|
|
errorcode "ai_scheduler/internal/data/error"
|
|
"ai_scheduler/internal/data/impl"
|
|
"ai_scheduler/internal/data/model"
|
|
"ai_scheduler/internal/entitys"
|
|
"ai_scheduler/tmpl/dataTemp"
|
|
"context"
|
|
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
// AdviceModelSupBiz 模型提供方配置业务逻辑(管理后台使用,按 adminId 隔离)
|
|
type AdviceModelSupBiz struct {
|
|
supImpl *impl.AiAdviceModelSupImpl
|
|
}
|
|
|
|
func NewAdviceModelSupBiz(supImpl *impl.AiAdviceModelSupImpl) *AdviceModelSupBiz {
|
|
return &AdviceModelSupBiz{supImpl: supImpl}
|
|
}
|
|
|
|
func (b *AdviceModelSupBiz) Add(ctx context.Context, req *entitys.AdvicerModelSupAddReq, adminId int32) (int32, error) {
|
|
if req.SupName == "" || req.Key == "" {
|
|
return 0, errorcode.ParamErr("sup_name or key is empty")
|
|
}
|
|
sup := &model.AiAdviceModelSup{
|
|
AdminID: adminId,
|
|
SupName: req.SupName,
|
|
SupWay: req.SupWay,
|
|
Key: req.Key,
|
|
URL: req.URL,
|
|
FileModel: req.FileModel,
|
|
JSONModel: req.JSONModel,
|
|
ChatModel: req.ChatModel,
|
|
Mode: req.Mode,
|
|
}
|
|
if sup.Mode == 0 {
|
|
sup.Mode = 1
|
|
}
|
|
if err := b.supImpl.Add(ctx, sup); err != nil {
|
|
return 0, err
|
|
}
|
|
return sup.SupID, nil
|
|
}
|
|
|
|
func (b *AdviceModelSupBiz) Update(ctx context.Context, req *entitys.AdvicerModelSupUpdateReq, adminId int32) error {
|
|
if req.SupID == 0 {
|
|
return errorcode.ParamErr("sup_id is empty")
|
|
}
|
|
if _, err := b.getOwned(ctx, req.SupID, adminId); err != nil {
|
|
return err
|
|
}
|
|
return b.supImpl.UpdateByKey(ctx, b.supImpl.PrimaryKey(), req.SupID, &model.AiAdviceModelSup{
|
|
SupName: req.SupName,
|
|
SupWay: req.SupWay,
|
|
Key: req.Key,
|
|
URL: req.URL,
|
|
FileModel: req.FileModel,
|
|
JSONModel: req.JSONModel,
|
|
ChatModel: req.ChatModel,
|
|
Mode: req.Mode,
|
|
})
|
|
}
|
|
|
|
func (b *AdviceModelSupBiz) List(ctx context.Context, req *entitys.AdvicerModelSupListReq, adminId int32) ([]model.AiAdviceModelSup, int64, error) {
|
|
list := make([]model.AiAdviceModelSup, 0)
|
|
cond := builder.NewCond()
|
|
cond = cond.And(builder.Eq{"admin_id": adminId})
|
|
if req.SupName != "" {
|
|
cond = cond.And(builder.Like{"sup_name", "%" + req.SupName + "%"})
|
|
}
|
|
page, err := b.supImpl.GetListToStruct(ctx, &cond, &dataTemp.ReqPageBo{Page: req.Page, Limit: req.PageSize}, &list, "")
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return list, page.Total, nil
|
|
}
|
|
|
|
func (b *AdviceModelSupBiz) Del(ctx context.Context, req *entitys.AdvicerModelSupDelReq, adminId int32) error {
|
|
if req.SupID == 0 {
|
|
return errorcode.ParamErr("sup_id is empty")
|
|
}
|
|
if _, err := b.getOwned(ctx, req.SupID, adminId); err != nil {
|
|
return err
|
|
}
|
|
return b.supImpl.DelByPrimary(ctx, b.supImpl.PrimaryKey(), req.SupID)
|
|
}
|
|
|
|
// getOwned 查询配置并校验归属当前管理员
|
|
func (b *AdviceModelSupBiz) getOwned(ctx context.Context, supId int32, adminId int32) (*model.AiAdviceModelSup, error) {
|
|
sup := &model.AiAdviceModelSup{}
|
|
if err := b.supImpl.GetByKey(ctx, b.supImpl.PrimaryKey(), supId, sup); err != nil {
|
|
return nil, err
|
|
}
|
|
if sup.SupID == 0 {
|
|
return nil, errorcode.ParamErr("model sup not found")
|
|
}
|
|
if sup.AdminID != adminId {
|
|
return nil, errorcode.ForbiddenErr("跨后台调用")
|
|
}
|
|
return sup, nil
|
|
}
|