package logic

import (
	"context"

	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"
	"gorm.io/gorm"
)

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

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

func (l *DeleteUserLogic) DeleteUser(in *system.DeleteUserReq) (*system.CommonResp, error) {
	err := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("id IN ?", in.Ids).Delete(&sysModel.SundynixUser{}).Error; err != nil {
			return err
		}
		// 同时删除用户角色关联
		if err := tx.Where("user_id IN ?", in.Ids).Delete(&sysModel.SundynixUserRole{}).Error; err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	return &system.CommonResp{Code: 200, Msg: "success"}, nil
}
