- package adapters
-
- import (
- "context"
- "github.com/pkg/errors"
- "github.com/zeromicro/go-zero/core/logx"
- "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
- "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
- "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
- "gorm.io/gorm"
- "time"
- )
-
- type CreateAdapterLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
-
- func NewCreateAdapterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateAdapterLogic {
- return &CreateAdapterLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx,
- }
- }
-
- func (l *CreateAdapterLogic) CreateAdapter(req *types.AdapterCreateReq) (resp *types.AdapterResp, err error) {
- adapter := types.AdapterInfo{}
- existAdapter := types.AdapterResp{}
- resp = &types.AdapterResp{}
- utils.Convert(req, &adapter)
- //check name
- exist := l.svcCtx.DbEngin.Table("t_adapter").Where("name = ?", req.Name).First(&existAdapter).Error
- if !errors.Is(exist, gorm.ErrRecordNotFound) {
- resp = &existAdapter
- return resp, errors.New("name already exists")
- }
- //check type
- var arr = [...]string{"0", "1", "2"}
- found := false
- for _, str := range arr {
- if str == req.Type {
- found = true
- break
- }
- }
- if found == false {
- return nil, errors.New("type not found")
- }
-
- //check resourceTypeDict
- sql := `select t_dict_item.item_value
- from t_dict
- join t_dict_item on t_dict.id = t_dict_item.dict_id
- where dict_code = 'adapter_type' and item_value = ?
- and t_dict_item.parent_id != 0`
- err = l.svcCtx.DbEngin.Raw(sql, req.ResourceType).First(&types.DictItemInfo{}).Error
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return nil, errors.New("resourceType error, please check!")
- }
- adapter.Id = utils.GenSnowflakeIDStr()
- adapter.CreateTime = time.Now().Format("2006-01-02 15:04:05")
- result := l.svcCtx.DbEngin.Table("t_adapter").Create(&adapter)
- if result.Error != nil {
- return resp, result.Error
- }
-
- _ = l.svcCtx.DbEngin.Table("t_adapter").Where("name = ?", req.Name).First(&existAdapter).Error
- resp = &existAdapter
- return resp, nil
- }
|