feat: 百科
This commit is contained in:
@@ -1,5 +1,197 @@
|
||||
package plant
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/plant"
|
||||
plantReq "sundynix-go/model/plant/request"
|
||||
"sundynix-go/model/system"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type WikiService struct{}
|
||||
|
||||
var WikiServiceApp = new(WikiClassService)
|
||||
|
||||
// CreateWiki 创建百科
|
||||
func (s *WikiService) CreateWiki(req plantReq.CreateWiki) error {
|
||||
return global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
//1.先模糊查询name是否存在 如果存在 则返回错误
|
||||
if !errors.Is(tx.Where("name like ?", "%"+req.Name+"%").First(&plant.Wiki{}).Error, gorm.ErrRecordNotFound) {
|
||||
return errors.New("植物已经存在")
|
||||
}
|
||||
|
||||
//2.图片
|
||||
var ossList []*system.Oss
|
||||
err := tx.Where("id in ?", req.OssIds).Find(&ossList).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//3. 分类
|
||||
var classes []*plant.Class
|
||||
err = tx.Where("id in ?", req.ClassIds).Find(&classes).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//4. 相关的植物
|
||||
var relatedWiki []*plant.Wiki
|
||||
err = tx.Where("id in ?", req.RelatedWikiIds).Find(&relatedWiki).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//6.保存数据
|
||||
wiki := plant.Wiki{
|
||||
IsHot: req.IsHot,
|
||||
Name: req.Name,
|
||||
LatinName: req.LatinName,
|
||||
Aliases: req.Aliases,
|
||||
Difficulty: req.Difficulty,
|
||||
DistributionArea: req.DistributionArea,
|
||||
Stem: req.Stem,
|
||||
Fruit: req.Fruit,
|
||||
Genus: req.Genus,
|
||||
LifeCycle: req.LifeCycle,
|
||||
GrowthHabit: req.GrowthHabit,
|
||||
ReproductionMethod: req.ReproductionMethod,
|
||||
PestsDiseases: req.PestsDiseases,
|
||||
LightIntensity: req.LightIntensity,
|
||||
LightType: req.LightType,
|
||||
OptimalTempPeriod: req.OptimalTempPeriod,
|
||||
FoliageType: req.FoliageType,
|
||||
FoliageColor: req.FoliageColor,
|
||||
FoliageShape: req.FoliageShape,
|
||||
Height: req.Height,
|
||||
FloweringPeriod: req.FloweringPeriod,
|
||||
FloweringColor: req.FloweringColor,
|
||||
FloweringShape: req.FloweringShape,
|
||||
FlowerDiameter: req.FlowerDiameter,
|
||||
}
|
||||
err = tx.Create(&wiki).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//7.处理图片关系
|
||||
if len(ossList) > 0 {
|
||||
var relations []map[string]interface{}
|
||||
for _, oss := range ossList {
|
||||
relations = append(relations, map[string]interface{}{
|
||||
"wiki_id": wiki.Id,
|
||||
"oss_id": oss.Id,
|
||||
})
|
||||
}
|
||||
err = tx.Table("sundynix_wiki_oss").Create(relations).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
//8.处理分类关系
|
||||
if len(classes) > 0 {
|
||||
var classRelations []map[string]interface{}
|
||||
for _, class := range classes {
|
||||
classRelations = append(classRelations, map[string]interface{}{
|
||||
"wiki_id": wiki.Id,
|
||||
"class_id": class.Id,
|
||||
})
|
||||
}
|
||||
err = tx.Table("sundynix_wiki_class").Create(classRelations).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
//9.处理相关植物关系
|
||||
if len(relatedWiki) > 0 {
|
||||
var relatedWikiRelations []map[string]interface{}
|
||||
for _, item := range relatedWiki {
|
||||
relatedWikiRelations = append(relatedWikiRelations, map[string]interface{}{
|
||||
"wiki_id": wiki.Id,
|
||||
"related_wiki_id": item.Id,
|
||||
})
|
||||
}
|
||||
err = tx.Table("sundynix_wiki_related").Create(relatedWikiRelations).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateWiki 修改百科
|
||||
func (s *WikiService) UpdateWiki(req plantReq.UpdateWiki) error {
|
||||
updateData := map[string]interface{}{
|
||||
"name": req.Name,
|
||||
"latin_name": req.LatinName,
|
||||
"aliases": req.Aliases,
|
||||
"difficulty": req.Difficulty,
|
||||
"genus": req.Genus,
|
||||
"distribution_area": req.DistributionArea,
|
||||
"life_cycle": req.LifeCycle,
|
||||
"growth_habit": req.GrowthHabit,
|
||||
"pests_diseases": req.PestsDiseases,
|
||||
"light_intensity": req.LightIntensity,
|
||||
"light_type": req.LightType,
|
||||
"optimal_temp_period": req.OptimalTempPeriod,
|
||||
"stem": req.Stem,
|
||||
"fruit": req.Fruit,
|
||||
"foliage_type": req.FoliageType,
|
||||
"foliage_color": req.FoliageColor,
|
||||
"foliage_shape": req.FoliageShape,
|
||||
"height": req.Height,
|
||||
"flowering_period": req.FloweringPeriod,
|
||||
"flowering_color": req.FloweringColor,
|
||||
"flowering_shape": req.FloweringShape,
|
||||
"flower_diameter": req.FlowerDiameter,
|
||||
}
|
||||
err := global.DB.Model(&plant.Wiki{}).Where("id = ?", req.Id).Updates(updateData).Error
|
||||
return err
|
||||
}
|
||||
|
||||
// WikiPage 分页
|
||||
func (s *WikiService) WikiPage(req plantReq.WikiPage) (list interface{}, total int64, err error) {
|
||||
limit := req.PageSize
|
||||
offset := req.PageSize * (req.Current - 1)
|
||||
db := global.DB.Model(&plant.Wiki{}).Preload("ImgList", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("created_at desc").Limit(1)
|
||||
}).Preload("Classes", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("created_at desc")
|
||||
})
|
||||
var wikis []plant.Wiki
|
||||
if req.Name != "" {
|
||||
db = db.Where("name like ?", "%"+req.Name+"%")
|
||||
}
|
||||
if req.IsHot != nil {
|
||||
db = db.Where("is_hot = ?", *req.IsHot)
|
||||
}
|
||||
if len(req.ClassIdIs) > 0 {
|
||||
db = db.Joins("inner join sundynix_wiki_class on sundynix_wiki_class.class.id = sundynix_wiki.id").
|
||||
Where("sundynix_wiki_class.class_id IN (?)", req.ClassIdIs)
|
||||
}
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = db.Limit(limit).Offset(offset).Order("created_at desc").Find(&wikis).Error
|
||||
return wikis, total, err
|
||||
}
|
||||
|
||||
// Detail 详情
|
||||
func (s *WikiService) Detail(id string) (w plant.Wiki, err error) {
|
||||
var wiki plant.Wiki
|
||||
err = global.DB.Where("id = ?", id).
|
||||
Preload("Classes", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("created_at desc")
|
||||
}).
|
||||
Preload("ImgList", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("created_at desc")
|
||||
}).
|
||||
Preload("RelatedWiki", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("created_at desc")
|
||||
}).
|
||||
First(&wiki).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return wiki, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user