package article import ( "context" "fmt" "practice_ArticleVote_Go/api/article" "practice_ArticleVote_Go/internal/dao" "practice_ArticleVote_Go/internal/service" ) var ArticlecController = &cArticle{} type cArticle struct { service service.IArticle } func (c *cArticle) AddArticle(ctx context.Context, req *article.ArticleReq) (res *article.ArticleRes, err error) { var Aid int64 var Vid int64 type VoteOption struct { VoteId int64 OptionContent string OptionIndex int Status int } var VoteOptions []VoteOption // 创建文章 Aid, err = dao.Article.Ctx(ctx).Data(req).InsertAndGetId() if req.VoteStatus == 1 { req.ArticleId = Aid // 如果发起投票,创建投票活动 Vid, err = dao.VotePoll.Ctx(ctx).Data(req).InsertAndGetId() if err != nil { return nil, fmt.Errorf("创建投票活动失败: %w", err) } // 如果发起投票,创建投票选项 for _, option := range req.OptionList { VoteOptions = append(VoteOptions, VoteOption{ VoteId: Vid, OptionContent: option.OptionContent, OptionIndex: option.OptionIndex, Status: 1, }) } _, err = dao.VoteOption.Ctx(ctx).Data(VoteOptions).Insert() if err != nil { return nil, fmt.Errorf("创建投票选项失败: %w", err) } } if err != nil { return nil, fmt.Errorf("创建文章失败: %w", err) } return }