125 lines
2.4 KiB
Go
125 lines
2.4 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"slices"
|
|
"yintai-api/app/api/request"
|
|
"yintai-api/app/common/consts"
|
|
"yintai-api/app/common/dao"
|
|
err2 "yintai-api/app/common/err"
|
|
"yintai-api/app/common/model"
|
|
|
|
"github.com/spf13/cast"
|
|
)
|
|
|
|
type News struct{}
|
|
|
|
func (r *News) List(req request.NewsList2) (any, int64) {
|
|
req.Pagination()
|
|
lists, count, err := dao.NewNews().List(req.Lang, req.Title, req.Content, req.PublishTime, cast.ToInt64(req.CategoryID), cast.ToInt64(req.Pinned), req.Page, req.Size, req.Sort)
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
res := r.ListFormat(lists)
|
|
|
|
return res, count
|
|
}
|
|
|
|
func (r *News) One(lang string, id int64) map[string]any {
|
|
x, err := dao.NewNews().GetLang(id, lang)
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
if x == nil {
|
|
return nil
|
|
}
|
|
|
|
return x.Format()
|
|
}
|
|
|
|
func (r *News) Get(req request.NewsDelete) any {
|
|
res := make([]map[string]any, 0)
|
|
for _, lang := range consts.LangSupport {
|
|
data := r.One(lang, cast.ToInt64(req.ID))
|
|
if data == nil {
|
|
continue
|
|
}
|
|
|
|
res = append(res, data)
|
|
}
|
|
|
|
_ = dao.NewNews().Incr(cast.ToInt64(req.ID))
|
|
|
|
return res
|
|
}
|
|
|
|
func (r *News) PreNext(req request.PreNext) any {
|
|
news := make([]*model.NewsPage, 0)
|
|
var err error
|
|
if news, err = dao.NewNews().GetN(cast.ToInt64(req.CategoryID), req.Lang, 1, math.MaxInt32, req.Sort); err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
res := make([]any, 0)
|
|
for i, v := range news {
|
|
if v.News.ID == cast.ToInt64(req.ID) {
|
|
if i > 0 {
|
|
v := news[i-1]
|
|
res = append(res, r.preFormat(v))
|
|
}
|
|
|
|
if i+1 < len(news) {
|
|
v := news[i+1]
|
|
res = append(res, r.preFormat(v))
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func (r *News) preFormat(v *model.NewsPage) any {
|
|
res := make(map[string]any)
|
|
res["id"] = v.ID
|
|
res["title"] = v.Title
|
|
res["weight"] = v.Weight
|
|
res["lang"] = v.Lang
|
|
return res
|
|
}
|
|
|
|
func (r *News) ListFormat(lists []*model.NewsPage) any {
|
|
listLang := make([]string, 0)
|
|
for _, list := range lists {
|
|
if list == nil {
|
|
continue
|
|
}
|
|
listLang = append(listLang, fmt.Sprintf("%d_%s", list.ID, list.Lang))
|
|
}
|
|
|
|
res := make([]map[string]any, 0)
|
|
for _, list := range lists {
|
|
if list == nil {
|
|
continue
|
|
}
|
|
res = append(res, list.Format())
|
|
for _, lang := range consts.LangSupport {
|
|
langFormat := fmt.Sprintf("%d_%s", list.ID, lang)
|
|
if slices.Contains(listLang, langFormat) {
|
|
continue
|
|
}
|
|
|
|
data := r.One(lang, list.ID)
|
|
if data == nil {
|
|
continue
|
|
}
|
|
|
|
res = append(res, data)
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|