Files
data-engine/service/dict/api_interface_service.go
2026-06-11 13:06:54 +08:00

194 lines
5.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package dict
import (
"context"
consts "dataengine/consts/api-feature"
"dataengine/dao/dict"
dto "dataengine/model/dto/dict"
entity "dataengine/model/entity/dict"
"errors"
"github.com/gogf/gf/v2/os/gtime"
)
type apiInterfaceService struct{}
// ApiInterface 接口服务
var ApiInterface = new(apiInterfaceService)
// Create 创建接口
func (s *apiInterfaceService) Create(ctx context.Context, req *dto.CreateApiInterfaceReq) (res *dto.CreateApiInterfaceRes, err error) {
_, err = DatasourcePlatform.GetOne(ctx, &dto.GetDatasourcePlatformReq{Id: req.PlatformId})
if err != nil {
return nil, errors.New("平台不存在")
}
// 检查接口编码在同一平台下是否重复
interfaces, _, err := dict.ApiInterface.List(ctx, &dto.ListApiInterfaceReq{
PlatformId: req.PlatformId,
Code: req.Code,
})
if err != nil {
return
}
if len(interfaces) > 0 {
return nil, errors.New("接口编码在该平台下已存在")
}
// 插入数据库
id, err := dict.ApiInterface.Insert(ctx, req)
if err != nil {
return
}
res = &dto.CreateApiInterfaceRes{
Id: id,
}
return
}
// List 获取接口列表
func (s *apiInterfaceService) List(ctx context.Context, req *dto.ListApiInterfaceReq) (res *dto.ListApiInterfaceRes, err error) {
apiList, total, err := dict.ApiInterface.List(ctx, req)
if err != nil {
return
}
platformIds := make([]int64, 0)
for _, item := range apiList {
if item.PlatformId > 0 {
platformIds = append(platformIds, item.PlatformId)
}
}
platformMap := make(map[int64]string)
if len(platformIds) > 0 {
res, err := DatasourcePlatform.List(ctx, &dto.ListDatasourcePlatformReq{})
if err == nil && res != nil {
for _, p := range res.List {
platformMap[p.Id] = p.PlatformName
}
}
}
list := make([]dto.ApiInterfaceItem, 0, len(apiList))
for _, item := range apiList {
platformName := ""
if name, ok := platformMap[item.PlatformId]; ok {
platformName = name
}
list = append(list, dto.ApiInterfaceItem{
Id: item.Id,
PlatformId: item.PlatformId,
PlatformName: platformName,
Name: item.Name,
Code: item.Code,
Url: item.Url,
Method: item.Method,
Status: item.Status,
StatusName: s.getStatusName(item.Status),
CreatedAt: safeUnix(item.CreatedAt),
UpdatedAt: safeUnix(item.UpdatedAt),
})
}
res = &dto.ListApiInterfaceRes{
List: list,
Total: total,
}
return
}
// GetOne 获取单个接口
func (s *apiInterfaceService) GetOne(ctx context.Context, req *dto.GetApiInterfaceReq) (res *dto.GetApiInterfaceRes, err error) {
apiInterface, err := dict.ApiInterface.GetOne(ctx, req)
if err != nil {
return
}
var platformName string
if apiInterface.PlatformId > 0 {
platform, _ := DatasourcePlatform.GetOne(ctx, &dto.GetDatasourcePlatformReq{Id: apiInterface.PlatformId})
if platform != nil {
platformName = platform.PlatformName
}
}
return &dto.GetApiInterfaceRes{
ApiInterface: apiInterface,
PlatformName: platformName,
}, nil
}
// Update 更新接口
func (s *apiInterfaceService) Update(ctx context.Context, req *dto.UpdateApiInterfaceReq) (err error) {
exist, err := dict.ApiInterface.GetOne(ctx, &dto.GetApiInterfaceReq{Id: req.Id})
if err != nil || exist == nil {
return errors.New("接口不存在")
}
if req.PlatformId > 0 && req.PlatformId != exist.PlatformId {
_, err := DatasourcePlatform.GetOne(ctx, &dto.GetDatasourcePlatformReq{Id: req.PlatformId})
if err != nil {
return errors.New("平台不存在")
}
}
if req.Code != "" && req.Code != exist.Code {
platformId := req.PlatformId
if platformId == 0 {
platformId = exist.PlatformId
}
interfaces, _, err := dict.ApiInterface.List(ctx, &dto.ListApiInterfaceReq{
PlatformId: platformId,
Code: req.Code,
})
if err != nil {
return err
}
if len(interfaces) > 0 {
return errors.New("接口编码在该平台下已存在")
}
}
_, err = dict.ApiInterface.Update(ctx, req)
return
}
// UpdateStatus 更新接口状态
func (s *apiInterfaceService) UpdateStatus(ctx context.Context, req *dto.UpdateApiInterfaceStatusReq) (err error) {
_, err = dict.ApiInterface.UpdateStatus(ctx, req.Id, req.Status.String())
return
}
// Delete 删除接口
func (s *apiInterfaceService) Delete(ctx context.Context, req *dto.DeleteApiInterfaceReq) (err error) {
_, err = dict.ApiInterface.Delete(ctx, req)
return
}
// GetByIds 根据ID列表获取接口
func (s *apiInterfaceService) GetByIds(ctx context.Context, ids []int64) (res []entity.ApiInterface, err error) {
return dict.ApiInterface.GetByIds(ctx, ids)
}
// getStatusName 获取状态名称
func (s *apiInterfaceService) getStatusName(status consts.PlatformStatus) string {
statusNames := map[consts.PlatformStatus]string{
consts.PlatformStatusActive: "启用",
consts.PlatformStatusInactive: "停用",
}
if name, ok := statusNames[status]; ok {
return name
}
return string(status)
}
// safeUnix 安全地从 *gtime.Time 获取 Unix 时间戳nil 返回 0
func safeUnix(t *gtime.Time) int64 {
if t == nil {
return 0
}
return t.Unix()
}