108 lines
2.9 KiB
Go
108 lines
2.9 KiB
Go
package biz
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"geo/internal/config"
|
|
"geo/internal/data/impl"
|
|
"geo/internal/data/model"
|
|
"geo/pkg"
|
|
"geo/tmpl/errcode"
|
|
"geo/utils/utils_oss"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type ProductBiz struct {
|
|
cfg *config.Config
|
|
productImpl *impl.ProductImpl
|
|
productSourceImpl *impl.ProductSourceImpl
|
|
oss *utils_oss.Client
|
|
}
|
|
|
|
func NewProductBiz(productImpl *impl.ProductImpl, productSourceImpl *impl.ProductSourceImpl, cfg *config.Config, oss *utils_oss.Client) *ProductBiz {
|
|
return &ProductBiz{
|
|
productImpl: productImpl,
|
|
productSourceImpl: productSourceImpl,
|
|
cfg: cfg,
|
|
oss: oss,
|
|
}
|
|
}
|
|
|
|
func (p *ProductBiz) GetProduct(ctx context.Context, productId int32) (*model.Product, error) {
|
|
var product model.Product
|
|
err := p.productImpl.GetByKey(ctx, p.productImpl.PrimaryKey(), productId, &product)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if product.ID == 0 {
|
|
return nil, errcode.NotFound("产品未找到")
|
|
}
|
|
return &product, err
|
|
}
|
|
|
|
func (p *ProductBiz) CreateAndUploadArticle(ctx context.Context, content string, product *model.Product) (string, error) {
|
|
if err := os.MkdirAll(p.cfg.Sys.MdDir, 0755); err != nil {
|
|
return "", err
|
|
}
|
|
now := time.Now().Unix()
|
|
fileName := fmt.Sprintf("article_%d_%d.md", product.ID, now)
|
|
mdAbs := filepath.Join(p.cfg.Sys.MdDir, fileName)
|
|
// 创建并写入文件
|
|
file, err := os.Create(mdAbs)
|
|
if err != nil {
|
|
return "", fmt.Errorf("创建文件失败: %w", err)
|
|
}
|
|
defer file.Close()
|
|
// 写入内容
|
|
if _, err := file.WriteString(content); err != nil {
|
|
return "", fmt.Errorf("写入文件失败: %w", err)
|
|
}
|
|
var imgs []string
|
|
if product.Imgs != "" {
|
|
imgs = strings.Split(product.Imgs, ",")
|
|
}
|
|
|
|
docxPath, err := pkg.Md2wordFix(mdAbs, p.cfg.Sys.MdDir, imgs)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
docxName := fmt.Sprintf("article_%d_%d.docx", product.ID, now)
|
|
docxAbs := filepath.Join(docxPath, docxName)
|
|
fileByte, err := pkg.ReadDocxToBytes(docxAbs)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
url, err := p.oss.UploadBytes(docxName, fileByte)
|
|
if err != nil {
|
|
return "", fmt.Errorf("上传文件失败: %w", err)
|
|
}
|
|
return url, nil
|
|
}
|
|
|
|
func (p *ProductBiz) AddSource(ctx context.Context, source *model.ProductSource) error {
|
|
err := p.productSourceImpl.Add(ctx, source)
|
|
return err
|
|
}
|
|
|
|
func (p *ProductBiz) SourceUpload(ctx context.Context, file []byte, fileName string) (string, error) {
|
|
url, err := p.oss.UploadBytes(fileName, file)
|
|
if err != nil {
|
|
return "", fmt.Errorf("上传文件失败: %w", err)
|
|
}
|
|
return url, nil
|
|
}
|
|
|
|
func (p *ProductBiz) UpdateSourceById(ctx context.Context, id int32, update *model.ProductSource) error {
|
|
|
|
return p.productSourceImpl.UpdateByKey(ctx, p.productSourceImpl.PrimaryKey(), id, update)
|
|
}
|
|
|
|
func (p *ProductBiz) DelSourceById(ctx context.Context, id int32) error {
|
|
|
|
return p.productSourceImpl.DeleteByKey(ctx, p.productSourceImpl.PrimaryKey(), id)
|
|
}
|