package service import ( "BlogAdmin/third_party/database" "Common/consts" "Common/model/admin" "Common/model/blog" "Common/model/vo" "context" "errors" "strconv" "time" "github.com/google/uuid" "github.com/redis/go-redis/v9" "gorm.io/gorm" ) type articleService struct { } var ArticleService articleService func (*articleService) GetAdminArticle(id string) admin.AdminArticle { var article admin.AdminArticle database.GormTemplate.Table("blog_articles").Where("id = ?", id).First(&article) return article } func (*articleService) PageAdminArticle(page int, itemsPerPage int) vo.Page[admin.AdminArticle] { var content []admin.AdminArticle var totalElements int64 ctx := context.Background() database.GormTemplate.Table("blog_articles").Count(&totalElements) database.GormTemplate.Table("blog_articles"). Where("del != ?", 1). Offset((page - 1) * itemsPerPage). Limit(itemsPerPage). Order("create_time DESC"). Find(&content) for i, v := range content { id := v.Id view, _ := database.RedisTemplate.HGet(ctx, consts.REDIS_BLOG_VIEW_RECORD, id).Int() content[i].View = view } pre := int(totalElements) % itemsPerPage if pre > 0 { pre = 1 } var totalPages int = int(totalElements)/itemsPerPage + pre return vo.Page[admin.AdminArticle]{TotalElements: totalElements, TotalPages: totalPages, Number: page, Content: content} } func (*articleService) CreateArticle(articel *admin.AdminArticle) (string, error) { time := time.Now().UnixMilli() articleId := uuid.NewString() articel.Id = articleId articel.CreateTime = time articel.UpdateTime = time articel.Del = 0 articel.State = consts.ARTICLE_STATE_DRAFT err := database.WGorm(func(tx *gorm.DB) error { contentId := uuid.NewString() content := articel.Content commonContent := vo.CommonContent{Id: contentId, RelId: articleId, Content: content, State: consts.CONTENT_STATE_DOWN} err := tx.Table("common_contents").Create(commonContent).Error if err != nil { return err } articel.Content = "" err = tx.Table("blog_articles").Create(*articel).Error return err }) return articleId, err } func (*articleService) UpdateArticle(articel *admin.AdminArticle) (string, error) { time := time.Now().UnixMilli() articleId := articel.Id articel.UpdateTime = time err := database.WGorm(func(tx *gorm.DB) error { //更新Article tx.Table("blog_articles").Updates(articel) //更新Content err := tx.Table("common_contents").Where("rel_id", articleId).UpdateColumn("content", articel.Content).Error return err }) return articleId, err } func (*articleService) PublishArticle(id string) error { now := time.Now().UnixMilli() var article blog.BlogArticle err := database.GormTemplate.Table("blog_articles").Where("id = ?", id).First(&article).Error if err != nil { return err } if article.State != consts.ARTICLE_STATE_DRAFT { return errors.New("发布失败,文章状态不是起草状态,无法发布") } if article.PublishTime == 0 { article.PublishTime = now } article.State = consts.ARTICLE_STATE_PUBLISH err = database.WGorm(func(tx *gorm.DB) error { var txErr error = tx.Table("blog_articles").Updates(&article).Error if txErr != nil { return errors.New("文章更新错误") } // txErr = ContentService.UpdataState(id, consts.CONTENT_STATE_PUBLISH) txErr = tx.Table("common_contents").Where("rel_id = ?", id).UpdateColumn("state", consts.CONTENT_STATE_PUBLISH).Error if txErr != nil { return errors.New("文本状态更新错误") } content, txErr := ContentService.GetContentByDb(id) if txErr != nil { return errors.New("文本查询错误") } ctx := context.Background() txErr = database.RedisTemplate.ZAdd(ctx, consts.REDIS_BLOG_ARTICLE_LATEST, redis.Z{Score: float64(article.PublishTime), Member: &article}).Err() if txErr != nil { return errors.New("缓存错误") } txErr = database.RedisTemplate.Set(ctx, consts.REDIS_BLOG_CONTENT+id, content.Content, time.Duration(0)).Err() return txErr }) return err } func (*articleService) UnPublishArticle(id string) error { var article admin.AdminArticle err := database.GormTemplate.Table("blog_articles").Where("id = ?", id).First(&article).Error if err != nil { return err } if article.State != consts.CONTENT_STATE_PUBLISH { return errors.New("撤下失败,文章状态不是已发布状态,无法撤下") } err = database.WGorm(func(tx *gorm.DB) error { var txErr error ctx := context.Background() txErr = database.RedisTemplate.ZRemRangeByScore(ctx, consts.REDIS_BLOG_ARTICLE_LATEST, strconv.FormatFloat(float64(article.PublishTime), 'E', -1, 64), strconv.FormatFloat(float64(article.PublishTime), 'E', -1, 64), ).Err() // txErr = database.RedisTemplate.ZAdd(ctx, "blog:article:latest", redis.Z{Score: float64(article.PublishTime), Member: &article}).Err() if txErr != nil { return errors.New("缓存错误") } txErr = database.RedisTemplate.Del(ctx, consts.REDIS_BLOG_CONTENT+id).Err() if txErr != nil { return errors.New("缓存错误") } article.State = consts.ARTICLE_STATE_DRAFT // article.PublishTime = -1 txErr = tx.Table("blog_articles").Updates(&article).Error if txErr != nil { return errors.New("文章状态更新错误") } // txErr = ContentService.UpdataState(id, consts.CONTENT_STATE_DOWN) txErr = tx.Table("common_contents").Where("rel_id = ?", id).UpdateColumn("state", consts.CONTENT_STATE_DOWN).Error return txErr }) return err } func (*articleService) DelArticle(id string) error { // articel := ArticleService.GetAdminArticle(id) // err := database.GormTemplate.Table("blog_articles").Where("id = ?", id).First(&article).Error err := database.WGorm(func(tx *gorm.DB) error { txErr := tx.Table("blog_articles").Where("id = ?", id).UpdateColumn("del", 1).Error return txErr }) return err }