57 lines
2.0 KiB
Go
57 lines
2.0 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"gorm.io/gorm"
|
|
plantModel "sundynix-micro-go/app/plant/model"
|
|
"sundynix-micro-go/app/plant/rpc/internal/svc"
|
|
"sundynix-micro-go/app/plant/rpc/plant"
|
|
)
|
|
|
|
type CreateWikiLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewCreateWikiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateWikiLogic {
|
|
return &CreateWikiLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
|
}
|
|
|
|
func (l *CreateWikiLogic) CreateWiki(in *plant.CreateWikiReq) (*plant.CommonResp, error) {
|
|
if !errors.Is(l.svcCtx.DB.Where("name LIKE ?", "%"+in.Name+"%").First(&plantModel.Wiki{}).Error, gorm.ErrRecordNotFound) {
|
|
return nil, errors.New("植物已经存在")
|
|
}
|
|
classID := in.ClassId
|
|
if classID == "" && len(in.ClassIds) > 0 {
|
|
classID = in.ClassIds[0]
|
|
}
|
|
w := plantModel.Wiki{
|
|
Name: in.Name, LatinName: in.LatinName, Aliases: in.Aliases,
|
|
Genus: in.Genus, Difficulty: int(in.Difficulty), IsHot: int(in.IsHot),
|
|
GrowthHabit: in.GrowthHabit, LightIntensity: in.LightIntensity,
|
|
OptimalTempPeriod: in.OptimalTempPeriod, ClassID: classID,
|
|
DistributionArea: in.DistributionArea, LifeCycle: in.LifeCycle,
|
|
ReproductionMethod: in.ReproductionMethod, PestsDiseases: in.PestsDiseases,
|
|
LightType: in.LightType, Stem: in.Stem, Fruit: in.Fruit,
|
|
FoliageType: in.FoliageType, FoliageColor: in.FoliageColor,
|
|
FoliageShape: in.FoliageShape, Height: int(in.Height),
|
|
FloweringPeriod: in.FloweringPeriod, FloweringColor: in.FloweringColor,
|
|
FloweringShape: in.FloweringShape, FlowerDiameter: int(in.FloweringDiameter),
|
|
}
|
|
if err := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Create(&w).Error; err != nil {
|
|
return err
|
|
}
|
|
return replaceWikiRelations(tx, w.ID, in.ClassIds, in.OssIds, in.RelatedWikiIds)
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
go func() {
|
|
_, _ = NewSyncWikiVectorLogic(context.Background(), l.svcCtx).SyncWikiVector(&plant.SyncWikiVectorReq{WikiId: w.ID})
|
|
}()
|
|
return &plant.CommonResp{Code: 0, Msg: w.ID}, nil
|
|
}
|