package admin_feature

import (
	"context"

	"xingfucha-server/app/main/api/internal/svc"
	"xingfucha-server/app/main/api/internal/types"
	"xingfucha-server/common/xerr"

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

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

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

func (l *AdminDeleteFeatureLogic) AdminDeleteFeature(req *types.AdminDeleteFeatureReq) (resp *types.AdminDeleteFeatureResp, err error) {
	// 1. 查询记录是否存在
	record, err := l.svcCtx.FeatureModel.FindOne(l.ctx, req.Id)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
			"查找功能失败, err: %v, id: %d", err, req.Id)
	}

	// 2. 执行软删除
	err = l.svcCtx.FeatureModel.DeleteSoft(l.ctx, nil, record)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
			"删除功能失败, err: %v, id: %d", err, req.Id)
	}

	// 3. 返回结果
	return &types.AdminDeleteFeatureResp{Success: true}, nil
}
