package logic

import (
	"context"
	"fmt"

	sysModel "sundynix-micro-go/app/system/model"
	"sundynix-micro-go/app/system/rpc/internal/svc"
	"sundynix-micro-go/app/system/rpc/system"

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

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

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

func (l *UpdateRoleLogic) UpdateRole(in *system.RoleUpdateReq) (*system.CommonResp, error) {
	if err := l.svcCtx.DB.Model(&sysModel.SundynixRole{}).Where("id = ?", in.Id).Updates(map[string]interface{}{
		"name": in.Name, "code": in.Code, "sort": in.Sort,
	}).Error; err != nil {
		return nil, fmt.Errorf("更新角色失败: %w", err)
	}
	return &system.CommonResp{Code: 200, Msg: "success"}, nil
}
