package admin_product

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

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

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

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

func (l *AdminGetProductDetailLogic) AdminGetProductDetail(req *types.AdminGetProductDetailReq) (resp *types.AdminGetProductDetailResp, 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. 构建响应
	resp = &types.AdminGetProductDetailResp{
		Id:          record.Id,
		ProductName: record.ProductName,
		ProductEn:   record.ProductEn,
		Description: record.Description,
		Notes:       record.Notes.String,
		CostPrice:   record.CostPrice,
		SellPrice:   record.SellPrice,
		CreateTime:  record.CreateTime.Format("2006-01-02 15:04:05"),
		UpdateTime:  record.UpdateTime.Format("2006-01-02 15:04:05"),
	}

	return resp, nil
}
