package dictionary

import (
	"context"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
	"time"

	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *AddDictLogic) AddDict(req *types.DictEditReq) (resp *types.DictResp, err error) {
	dict := &types.DictInfo{}
	dict.DictName = req.DictName
	dict.DictCode = req.DictCode
	dict.Type = req.Type
	dict.Description = req.Description
	dict.Id = utils.GenSnowflakeIDStr()
	dict.CreateTime = time.Now().Format("2006-01-02 15:04:05")
	dict.Status = req.Status
	result := l.svcCtx.DbEngin.Table("t_dict").Create(&dict)
	if result.Error != nil {
		logx.Errorf("Failed to create dictionary , errors: %s", result.Error)
		return nil, result.Error
	}
	return
}
