208 lines
4.2 KiB
Go
208 lines
4.2 KiB
Go
package service
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
"yintai-api/app/admin/request"
|
|
"yintai-api/app/common/consts"
|
|
"yintai-api/app/common/dao"
|
|
err2 "yintai-api/app/common/err"
|
|
"yintai-api/app/common/model"
|
|
"yintai-api/app/common/service"
|
|
"yintai-api/base/global"
|
|
|
|
"github.com/spf13/cast"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Document struct{}
|
|
|
|
func (r *Document) Create(req request.DocumentCreate) int64 {
|
|
m := new(model.Document)
|
|
m.Weight = cast.ToInt64(req.Weight)
|
|
m.CreateTime = time.Now().Unix()
|
|
|
|
trans := make([]*model.DocumentTranslations, 0)
|
|
for lang, tran := range req.Trans {
|
|
lang = strings.ToUpper(lang)
|
|
if !slices.Contains(consts.LangSupport, lang) {
|
|
continue
|
|
}
|
|
|
|
trans = append(trans, &model.DocumentTranslations{
|
|
DocumentID: 0,
|
|
Lang: lang,
|
|
Name: tran.FileName,
|
|
Title: tran.Title,
|
|
SubTitle: tran.SubTitle,
|
|
ImageTitle: "",
|
|
Description: tran.Desc,
|
|
StorageType: "qiniu",
|
|
StoragePath: service.NewQiniu().ObjectKey(tran.Path),
|
|
Cover: service.NewQiniu().ObjectKey(tran.Cover),
|
|
Extra: tran.Extra,
|
|
})
|
|
|
|
}
|
|
|
|
err := global.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
if err := dao.NewDocument(tx).Create(m); err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, tran := range trans {
|
|
tran.DocumentID = m.ID
|
|
if err := dao.NewDocumentTranslations(tx).Create(tran); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := BizCateCreate(tx, m.ID, cast.ToInt64(req.CategoryID), model.CateTypeFile); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
return m.ID
|
|
}
|
|
|
|
func (r *Document) Update(req request.DocumentUpdate) {
|
|
m, err := dao.NewDocument().Get(cast.ToInt64(req.ID))
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
if m == nil {
|
|
err2.NotEsxErr()
|
|
return
|
|
}
|
|
|
|
if weight := cast.ToInt64(req.Weight); weight != m.Weight {
|
|
m.Weight = weight
|
|
}
|
|
|
|
m.UpdateTime = time.Now().Unix()
|
|
trans := make([]*model.DocumentTranslations, 0)
|
|
for lang, tran := range req.Trans {
|
|
lang = strings.ToUpper(lang)
|
|
if !slices.Contains(consts.LangSupport, lang) {
|
|
continue
|
|
}
|
|
|
|
t, err := dao.NewDocumentTranslations().GetByMaster(m.ID, lang)
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
if t == nil {
|
|
t = new(model.DocumentTranslations)
|
|
}
|
|
if tran.Path != "" {
|
|
t.StoragePath = service.NewQiniu().ObjectKey(tran.Path)
|
|
}
|
|
if tran.FileName != "" {
|
|
t.Name = tran.FileName
|
|
}
|
|
if tran.SubTitle != "" {
|
|
t.SubTitle = tran.SubTitle
|
|
}
|
|
if tran.Cover != "" {
|
|
t.Cover = service.NewQiniu().ObjectKey(tran.Cover)
|
|
}
|
|
if tran.Desc != "" {
|
|
t.Description = tran.Desc
|
|
}
|
|
if tran.Extra != "" {
|
|
t.Extra = tran.Extra
|
|
}
|
|
|
|
trans = append(trans, t)
|
|
}
|
|
|
|
err = global.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
if err = dao.NewDocument(tx).Update(m); err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, t := range trans {
|
|
t.DocumentID = m.ID
|
|
if err = dao.NewDocumentTranslations(tx).Update(t); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if v := cast.ToInt64(req.CategoryID); v != 0 {
|
|
if err = BizCateUpdate(tx, m.ID, v, model.CateTypeFile); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
}
|
|
|
|
func (r *Document) Delete(req request.DocumentDelete) {
|
|
m, err := dao.NewDocument().Get(cast.ToInt64(req.ID))
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
if m == nil {
|
|
err2.NotEsxErr()
|
|
return
|
|
}
|
|
|
|
err = global.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
if err = dao.NewDocument(tx).Delete(m.ID); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = dao.NewDocumentTranslations(tx).Delete(m.ID); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
}
|
|
|
|
func (r *Document) List(req request.DocumentList) (any, int64) {
|
|
req.Pagination()
|
|
lists, count, err := dao.NewDocument().List(req.Lang, cast.ToInt64(req.Category), req.FileName, req.Title, req.SubTitle, req.Desc, req.Page, req.Size, req.Sort)
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
res := make([]map[string]any, 0)
|
|
for _, list := range lists {
|
|
res = append(res, list.Format())
|
|
}
|
|
|
|
return res, count
|
|
}
|
|
|
|
func (r *Document) Get(req request.DocumentDelete) any {
|
|
res := make(map[string]any)
|
|
for _, lang := range consts.LangSupport {
|
|
x, err := dao.NewDocument().GetLang(lang, cast.ToInt64(req.ID))
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
res[lang] = x.Format()
|
|
}
|
|
|
|
return res
|
|
}
|