129 lines
3.3 KiB
Go
129 lines
3.3 KiB
Go
|
|
package services
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"platform/pkg/u"
|
||
|
|
"platform/web/core"
|
||
|
|
m "platform/web/models"
|
||
|
|
q "platform/web/queries"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"gorm.io/gen/field"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
var ArticleGroup = &articleGroupService{}
|
||
|
|
|
||
|
|
type articleGroupService struct{}
|
||
|
|
|
||
|
|
func (s *articleGroupService) All() (result []*m.ArticleGroup, err error) {
|
||
|
|
return q.ArticleGroup.
|
||
|
|
Order(q.ArticleGroup.Sort, q.ArticleGroup.CreatedAt).
|
||
|
|
Find()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *articleGroupService) Page(req *PageArticleGroupReq) (result []*m.ArticleGroup, count int64, err error) {
|
||
|
|
do := q.ArticleGroup.Where()
|
||
|
|
if req.Keyword != nil && *req.Keyword != "" {
|
||
|
|
do = do.Where(
|
||
|
|
q.ArticleGroup.Where(
|
||
|
|
q.ArticleGroup.Name.Like("%" + *req.Keyword + "%"),
|
||
|
|
).Or(
|
||
|
|
q.ArticleGroup.Code.Like("%" + *req.Keyword + "%"),
|
||
|
|
),
|
||
|
|
)
|
||
|
|
}
|
||
|
|
if req.Status != nil {
|
||
|
|
do = do.Where(q.ArticleGroup.Status.Eq(int(*req.Status)))
|
||
|
|
}
|
||
|
|
|
||
|
|
return q.ArticleGroup.
|
||
|
|
Where(do).
|
||
|
|
Order(q.ArticleGroup.Sort, q.ArticleGroup.CreatedAt).
|
||
|
|
FindByPage(req.GetOffset(), req.GetLimit())
|
||
|
|
}
|
||
|
|
|
||
|
|
type PageArticleGroupReq struct {
|
||
|
|
core.PageReq
|
||
|
|
Keyword *string `json:"keyword,omitempty"`
|
||
|
|
Status *m.ArticleGroupStatus `json:"status,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *articleGroupService) Create(data CreateArticleGroupData) error {
|
||
|
|
err := q.ArticleGroup.Create(&m.ArticleGroup{
|
||
|
|
Name: data.Name,
|
||
|
|
Code: data.Code,
|
||
|
|
Sort: u.Else(data.Sort, 0),
|
||
|
|
Status: u.Else(data.Status, m.ArticleGroupStatusEnabled),
|
||
|
|
})
|
||
|
|
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
||
|
|
return core.NewBizErr("文档分组编码已存在")
|
||
|
|
}
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
type CreateArticleGroupData struct {
|
||
|
|
Name string `json:"name" validate:"required"`
|
||
|
|
Code string `json:"code" validate:"required"`
|
||
|
|
Sort *int32 `json:"sort"`
|
||
|
|
Status *m.ArticleGroupStatus `json:"status"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *articleGroupService) Update(data UpdateArticleGroupData) error {
|
||
|
|
do := make([]field.AssignExpr, 0)
|
||
|
|
if data.Name != nil {
|
||
|
|
do = append(do, q.ArticleGroup.Name.Value(*data.Name))
|
||
|
|
}
|
||
|
|
if data.Code != nil {
|
||
|
|
do = append(do, q.ArticleGroup.Code.Value(*data.Code))
|
||
|
|
}
|
||
|
|
if data.Sort != nil {
|
||
|
|
do = append(do, q.ArticleGroup.Sort.Value(*data.Sort))
|
||
|
|
}
|
||
|
|
if data.Status != nil {
|
||
|
|
do = append(do, q.ArticleGroup.Status.Value(int(*data.Status)))
|
||
|
|
}
|
||
|
|
if len(do) == 0 {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
r, err := q.ArticleGroup.Where(q.ArticleGroup.ID.Eq(data.ID)).UpdateSimple(do...)
|
||
|
|
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
||
|
|
return core.NewBizErr("文档分组编码已存在")
|
||
|
|
}
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if r.RowsAffected == 0 {
|
||
|
|
return core.NewBizErr("文档分组状态已过期")
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
type UpdateArticleGroupData struct {
|
||
|
|
ID int32 `json:"id" validate:"required"`
|
||
|
|
Name *string `json:"name"`
|
||
|
|
Code *string `json:"code"`
|
||
|
|
Sort *int32 `json:"sort"`
|
||
|
|
Status *m.ArticleGroupStatus `json:"status"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *articleGroupService) Delete(id int32) error {
|
||
|
|
count, err := q.Article.Where(q.Article.GroupID.Eq(id)).Count()
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if count > 0 {
|
||
|
|
return core.NewBizErr("分组下仍有关联文章,无法删除")
|
||
|
|
}
|
||
|
|
|
||
|
|
r, err := q.ArticleGroup.Where(q.ArticleGroup.ID.Eq(id)).UpdateColumn(q.ArticleGroup.DeletedAt, time.Now())
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if r.RowsAffected == 0 {
|
||
|
|
return core.NewBizErr("文档分组状态已过期")
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|