package dictionary

import (
	"context"
	"github.com/pkg/errors"

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

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

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

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

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