yintai2/app/admin/service/process.go

193 lines
3.8 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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/base/global"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"gorm.io/gorm"
)
type Process struct{}
func (r *Process) Create(req request.ProcessCreate) int64 {
m := &model.Process{CreateTime: time.Now().Unix()}
mt := make([]*model.ProcessTranslations, 0)
for lang, trans := range req.Translations {
lang = strings.ToUpper(lang)
if !slices.Contains(consts.LangSupport, lang) {
continue
}
t := new(model.ProcessTranslations)
// 图片处理,只提取object_key
t.Images = goutil.EncodeJSON(Images(trans.Images))
t.Lang = lang
t.Content = trans.Content
t.ProcessTime = trans.ProcessTime
t.Title = trans.Title
mt = append(mt, t)
}
err := global.GetDB().Transaction(func(tx *gorm.DB) error {
if err := dao.NewProcess(tx).Create(m); err != nil {
return err
}
for _, trans := range mt {
trans.ProcessID = m.ID
if err := dao.NewProcessTranslations(tx).Create(trans); err != nil {
return err
}
}
if err := BizCateCreate(tx, m.ID, cast.ToInt64(req.CategoryID), model.CateTypeProcess); err != nil {
return err
}
return nil
})
if err != nil {
err2.DbErr(err)
}
return m.ID
}
func (r *Process) Update(req request.ProcessUpdate) {
m, err := dao.NewProcess().Get(cast.ToInt64(req.ID))
if err != nil {
err2.DbErr(err)
}
if m == nil {
err2.NotEsxErr()
return
}
mt := make([]*model.ProcessTranslations, 0)
for lang, trans := range req.Translations {
lang = strings.ToUpper(lang)
if !slices.Contains(consts.LangSupport, lang) {
continue
}
t, err := dao.NewProcessTranslations().GetByMaster(m.ID, lang)
if err != nil {
err2.DbErr(err)
}
if t == nil {
t = new(model.ProcessTranslations)
}
if trans.Title != "" {
t.Title = trans.Title
}
if len(trans.Images) > 0 {
t.Images = goutil.EncodeJSON(Images(trans.Images))
}
if trans.Content != "" {
t.Content = trans.Content
}
if trans.ProcessTime != "" {
t.ProcessTime = trans.ProcessTime
}
mt = append(mt, t)
}
err = global.GetDB().Transaction(func(tx *gorm.DB) error {
m.UpdateTime = time.Now().Unix()
_ = dao.NewProcess(tx).Update(m)
for _, trans := range mt {
trans.ProcessID = m.ID
if err = dao.NewProcessTranslations(tx).Update(trans); err != nil {
return err
}
}
if cast.ToInt64(req.CategoryID) != 0 {
if err = BizCateUpdate(tx, m.ID, cast.ToInt64(req.CategoryID), model.CateTypeProcess); err != nil {
return err
}
}
return nil
})
if err != nil {
err2.DbErr(err)
}
return
}
func (r *Process) Delete(req request.ProcessDelete) {
m, err := dao.NewProcess().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.NewProcess().Delete(m.ID); err != nil {
err2.DbErr(err)
}
if err = dao.NewProcessTranslations().Delete(m.ID); err != nil {
err2.DbErr(err)
}
return nil
})
if err != nil {
err2.DbErr(err)
}
return
}
func (r *Process) List(req request.ProcessList) (any, int64) {
req.Pagination()
lists, count, err := dao.NewProcess().List(req.Lang, req.Title, req.Content, req.ProcessTime, cast.ToInt64(req.CategoryID), 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 *Process) Get(req request.ProcessDelete) any {
res := make(map[string]any)
for _, lang := range consts.LangSupport {
x, err := dao.NewProcess().GetLang(lang, cast.ToInt64(req.ID))
if err != nil {
err2.DbErr(err)
}
res[lang] = x.Format()
}
return res
}