/*

 Copyright (c) [2023] [pcm]
 [pcm-coordinator] is licensed under Mulan PSL v2.
 You can use this software according to the terms and conditions of the Mulan PSL v2.
 You may obtain a copy of Mulan PSL v2 at:
          http://license.coscl.org.cn/MulanPSL2
 THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 See the Mulan PSL v2 for more details.

*/

package participantservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
	"gitlink.org.cn/JointCloud/pcm-coordinator/rpc/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/rpc/pcmCore"
)

type RegisterTenantLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterTenantLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterTenantLogic {
	return &RegisterTenantLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// registerTenant 注册租户信息
func (l *RegisterTenantLogic) RegisterTenant(in *pcmCore.TenantInfo) (*pcmCore.TenantResp, error) {
	// todo: add your logic here and delete this line
	tenantInfo := &models.ScTenantInfo{}
	utils.Convert(in, tenantInfo)
	tenantInfo.Id = utils.GenSnowflakeID()
	tx := l.svcCtx.DbEngin.Save(&tenantInfo)
	if tx.Error != nil {
		logx.Errorf("orm err:", tx.Error)
		return &pcmCore.TenantResp{
			Code: 500,
			Msg:  "save Tenant error " + tx.Error.Error(),
		}, nil
	}
	return &pcmCore.TenantResp{
		Code: 200,
		Msg:  "success",
	}, nil
}
