package dictionary

import (
	"context"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/dictionarydetail"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/predicate"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"
	"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/code"
	"git.noahlan.cn/noahlan/ntool-biz/core/nstatus/msg"

	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
)

type DeleteDictionaryDetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeleteDictionaryDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteDictionaryDetailLogic {
	return &DeleteDictionaryDetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeleteDictionaryDetailLogic) DeleteDictionaryDetail(in *core.DeleteDictionaryDetailReq) (*core.BaseResp, error) {
	var predicates []predicate.DictionaryDetail
	if in.DictId != 0 {
		predicates = append(predicates, dictionarydetail.DictionaryIDEQ(in.DictId))
		goto Delete
	}
	if len(in.Ids) > 0 {
		predicates = append(predicates, dictionarydetail.IDIn(in.Ids...))
	}

Delete:
	_, err := l.svcCtx.DB.DictionaryDetail.Delete().Where(predicates...).Exec(l.ctx)
	if err != nil {
		return nil, hander.HandleEntErr(err, in)
	}

	return &core.BaseResp{
		Code: code.StatusOK,
		Msg:  msg.DeleteSuccess,
	}, nil
}
