package admin_product

import (
	"context"
	"bdrp-server/app/main/api/internal/svc"
	"bdrp-server/app/main/api/internal/types"
	"bdrp-server/common/xerr"

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

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

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

func (l *AdminDeleteProductLogic) AdminDeleteProduct(req *types.AdminDeleteProductReq) (resp *types.AdminDeleteProductResp, err error) {
	// 1. 查询记录是否存在
	record, err := l.svcCtx.ProductModel.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.ProductModel.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.AdminDeleteProductResp{Success: true}, nil
}
