package dictionary

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

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

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

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

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

func (l *EditDictLogic) EditDict(req *types.DictEditReq) (resp *types.DictResp, err error) {
	dict := &types.DictInfo{}
	result := l.svcCtx.DbEngin.Table("t_dict").First(&dict, req.Id)
	if errors.Is(result.Error, gorm.ErrRecordNotFound) {
		logx.Errorf("Dictionary editing failure. errors: %s", result.Error)
		return nil, errors.New("Dict does not exist")
	}
	utils.Convert(req, &dict)
	tx := l.svcCtx.DbEngin.Table("t_dict").Model(&dict).Updates(&dict)
	fmt.Println(tx)
	return
}
