package logic

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	sysModel "sundynix-micro-go/app/system/model"
	"sundynix-micro-go/app/system/rpc/internal/svc"
	"sundynix-micro-go/app/system/rpc/system"
)

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

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

func (l *UpdateDictLogic) UpdateDict(in *system.DictUpdateReq) (*system.CommonResp, error) {
	if err := l.svcCtx.DB.Model(&sysModel.SundynixDict{}).Where("id = ?", in.Id).Updates(map[string]interface{}{
		"type": in.Type, "label": in.Label, "value": in.Value, "sort": in.Sort, "desc": in.Desc,
	}).Error; err != nil {
		return nil, fmt.Errorf("更新字典失败")
	}
	return &system.CommonResp{Code: 200, Msg: "success"}, nil
}
