package dictionary

import (
	"context"
	"github.com/pkg/errors"
	tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"

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

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

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

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

func (l *GetDictItemLogic) GetDictItem(req *types.CId) (resp *types.DictItemResp, err error) {
	resp = &types.DictItemResp{}
	item := &types.DictItemInfo{}
	db := l.svcCtx.DbEngin.Table("t_dict_item").Where("id = ?", req.Id).First(&item)
	if db.Error != nil {
		logx.Errorf("err %v", db.Error.Error())
		return nil, errors.New("Dictionary item does not exist")
	}
	tool.Convert(item, &resp)
	return
}
