From 8450299cdc84e699aa1ab42fac44212a61967136 Mon Sep 17 00:00:00 2001 From: majun <3060162534@qq.com> Date: Fri, 13 Dec 2024 19:32:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E4=BD=9C=E4=B8=9A=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- link_homework/api/v1/homework/homework.go | 26 +++++ link_homework/internal/cmd/cmd.go | 2 + .../internal/controller/homework/homework.go | 42 ++++++++ link_homework/internal/logic/homework/homework.go | 115 +++++++++++++++++++++ .../model/entity/activity_interactive_group.go | 2 +- link_homework/internal/service/homework.go | 3 + link_homework/manifest/config/config.yaml | 2 - 7 files changed, 189 insertions(+), 3 deletions(-) diff --git a/link_homework/api/v1/homework/homework.go b/link_homework/api/v1/homework/homework.go index 09ad117..89feddd 100644 --- a/link_homework/api/v1/homework/homework.go +++ b/link_homework/api/v1/homework/homework.go @@ -1,6 +1,32 @@ package homework +import ( + "github.com/gogf/gf/v2/os/gtime" + "link_homework/internal/model/entity" +) + type GetHomeworkListReq struct { PageNo int `v:"required#页码不能为空" dc:"页码"` PageSize int `v:"required#页面大小不能为空" dc:"页面大小"` } + +type AddHomeworkReq struct { + Name string `v:"required#作业名称不能为空" dc:"作业名称"` + ClubType int `v:"required|min:1|max:9#作业所属不能为空|作业所属不存在|作业所属不存在" dc:"作业所属"` + ArticleId int `v:"required-without:LiveId|min:1#请选择关联的文章或直播|关联文章不存在" dc:"关联文章id"` + LiveId int `v:"required-without:ArticleId|min:1#请选择关联的文章或直播|关联直播不存在" dc:"关联直播id"` + StartDate *gtime.Time `v:"required#作业开始时间不能为空" dc:"作业开始时间"` + EndDate *gtime.Time `v:"required|after-equal:StartDate#作业结束时间不能为空|作业结束时间必须晚于作业开始时间" dc:"作业结束时间"` + Questions []*entity.ActivityInteractiveForm `v:"required#题目集不能为空" dc:"题目集"` +} + +type EditHomeworkReq struct { + Id int `v:"required#作业id不能为空" dc:"作业id"` + Name string `v:"required#作业名称不能为空" dc:"作业名称"` + ClubType int `v:"required|min:1|max:9#作业所属不能为空|作业所属不存在|作业所属不存在" dc:"作业所属"` + ArticleId int `v:"required-without:LiveId|min:1#请选择关联的文章或直播|关联文章不存在" dc:"关联文章id"` + LiveId int `v:"required-without:ArticleId|min:1#请选择关联的文章或直播|关联直播不存在" dc:"关联直播id"` + StartDate *gtime.Time `v:"required#作业开始时间不能为空" dc:"作业开始时间"` + EndDate *gtime.Time `v:"required|after-equal:StartDate#作业结束时间不能为空|作业结束时间必须晚于作业开始时间" dc:"作业结束时间"` + Questions []*entity.ActivityInteractiveForm `v:"required#题目集不能为空" dc:"题目集"` +} diff --git a/link_homework/internal/cmd/cmd.go b/link_homework/internal/cmd/cmd.go index 27c8e2b..f588a2f 100644 --- a/link_homework/internal/cmd/cmd.go +++ b/link_homework/internal/cmd/cmd.go @@ -42,6 +42,8 @@ var ( //gfToken.Middleware(ctx, group) //直接写接口,不用再分组 group.POST("/get-homework-list", homework.Homework().GetHomeworkList) + group.POST("/add-homework", homework.Homework().AddHomework) + group.POST("/edit-homework", homework.Homework().EditHomework) group.ALL("/get-article-list", article.Article().GetArticleList) group.ALL("/get-live-list", live.Live().GetLiveList) }) diff --git a/link_homework/internal/controller/homework/homework.go b/link_homework/internal/controller/homework/homework.go index 2e78101..fb13e64 100644 --- a/link_homework/internal/controller/homework/homework.go +++ b/link_homework/internal/controller/homework/homework.go @@ -34,3 +34,45 @@ func (c cHomework) GetHomeworkList(r *ghttp.Request) { Data: res, }) } + +func (c cHomework) AddHomework(r *ghttp.Request) { + var req *homework.AddHomeworkReq + if err := r.Parse(&req); err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + err := service.Homework().AddHomework(r.Context(), req) + if err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + r.Response.WriteJsonExit(dto.Result{ + Code: 200, + Message: "success", + }) +} + +func (c cHomework) EditHomework(r *ghttp.Request) { + var req *homework.EditHomeworkReq + if err := r.Parse(&req); err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + err := service.Homework().EditHomework(r.Context(), req) + if err != nil { + r.Response.WriteJsonExit(dto.Result{ + Code: 400, + Message: err.Error(), + }) + } + r.Response.WriteJsonExit(dto.Result{ + Code: 200, + Message: "success", + }) +} diff --git a/link_homework/internal/logic/homework/homework.go b/link_homework/internal/logic/homework/homework.go index 46e5a06..9c4fda1 100644 --- a/link_homework/internal/logic/homework/homework.go +++ b/link_homework/internal/logic/homework/homework.go @@ -2,8 +2,12 @@ package homework import ( "context" + "fmt" + "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gtime" + "link_homework/api/v1/homework" "link_homework/internal/dao" + "link_homework/internal/model/do" "link_homework/internal/model/entity" "link_homework/internal/service" ) @@ -34,3 +38,114 @@ func (s *sHomework) GetHomeworkList(ctx context.Context, pageNo int, pageSize in } return } + +func (s *sHomework) AddHomework(ctx context.Context, req *homework.AddHomeworkReq) (err error) { + status := 0 + if req.StartDate.Before(gtime.Now()) { + if req.EndDate.Before(gtime.Now()) { + return fmt.Errorf("作业时间已过期,无法添加!") + } else { + status = 1 + } + } + var Id int64 + if req.ArticleId == 0 { + Id, err = dao.ActivityInteractiveGroup.Ctx(ctx).Data(do.ActivityInteractiveGroup{ + Name: req.Name, + Status: status, + ClubType: req.ClubType, + LiveId: req.LiveId, + StartDate: req.StartDate, + EndDate: req.EndDate, + }).InsertAndGetId() + } else if req.LiveId == 0 { + Id, err = dao.ActivityInteractiveGroup.Ctx(ctx).Data(do.ActivityInteractiveGroup{ + Name: req.Name, + Status: status, + ClubType: req.ClubType, + ArticleId: req.ArticleId, + StartDate: req.StartDate, + EndDate: req.EndDate, + }).InsertAndGetId() + } else { + return fmt.Errorf("不能同时关联文章和直播!") + } + for _, v := range req.Questions { + _, err = dao.ActivityInteractiveForm.Ctx(ctx).Data(do.ActivityInteractiveForm{ + Name: req.Name, + Description: v.Description, + Content: v.Content, + Status: status, + Type: v.Type, + GroupId: Id, + }).Insert() + } + return +} + +func (s *sHomework) EditHomework(ctx context.Context, req *homework.EditHomeworkReq) (err error) { + var nowhomework *entity.ActivityInteractiveGroup + err = dao.ActivityInteractiveGroup.Ctx(ctx).WherePri(req.Id).Scan(&nowhomework) + if nowhomework.EndDate.Before(gtime.Now()) { + return fmt.Errorf("作业已结束,无法修改!") + } else { + flag := true + if nowhomework.StartDate.Before(gtime.Now()) { + flag = false + } + status := 0 + if req.StartDate.Before(gtime.Now()) { + if req.EndDate.Before(gtime.Now()) { + status = 2 + } else { + status = 1 + } + } + if req.ArticleId == 0 { + _, err = g.Model("activity_interactive_group").Data(g.Map{ + "name": req.Name, + "status": status, + "club_type": req.ClubType, + "article_id": nil, + "live_id": req.LiveId, + "start_date": req.StartDate, + "end_date": req.EndDate, + }).WherePri(req.Id).Update() + } else if req.LiveId == 0 { + _, err = g.Model("activity_interactive_group").Data(g.Map{ + "name": req.Name, + "status": status, + "club_type": req.ClubType, + "article_id": req.ArticleId, + "live_id": nil, + "start_date": req.StartDate, + "end_date": req.EndDate, + }).WherePri(req.Id).Update() + } else { + return fmt.Errorf("不能同时关联文章和直播!") + } + if flag { + _, err = dao.ActivityInteractiveForm.Ctx(ctx).Where("group_id", req.Id).Delete() + for _, v := range req.Questions { + _, err = dao.ActivityInteractiveForm.Ctx(ctx).Data(do.ActivityInteractiveForm{ + Name: req.Name, + Description: v.Description, + Content: v.Content, + Status: status, + Type: v.Type, + GroupId: req.Id, + }).Insert() + } + } else { + for _, v := range req.Questions { + _, err = dao.ActivityInteractiveForm.Ctx(ctx).Data(do.ActivityInteractiveForm{ + Name: req.Name, + Description: v.Description, + Content: v.Content, + Status: status, + }).WherePri(v.Id).Update() + } + } + return + } +} diff --git a/link_homework/internal/model/entity/activity_interactive_group.go b/link_homework/internal/model/entity/activity_interactive_group.go index a4c0b61..4b26dcb 100644 --- a/link_homework/internal/model/entity/activity_interactive_group.go +++ b/link_homework/internal/model/entity/activity_interactive_group.go @@ -20,7 +20,7 @@ type ActivityInteractiveGroup struct { LiveId int `json:"liveId" orm:"live_id" description:"关联直播id"` // 关联直播id StartDate *gtime.Time `json:"startDate" orm:"start_date" description:"作业开始时间"` // 作业开始时间 EndDate *gtime.Time `json:"endDate" orm:"end_date" description:"作业结束时间"` // 作业结束时间 - Article *FxArticle `json:"article" orm:"with:id=article_id" description:"关联文章"` // 关联文章 + Article *FxArticle `json:"article" description:"关联文章"` // 关联文章 Live *Live `json:"live" orm:"with:id=live_id" description:"关联直播"` // 关联直播 Count int `json:"count" description:"作业提交次数"` // 作业结束时间 } diff --git a/link_homework/internal/service/homework.go b/link_homework/internal/service/homework.go index 053f0f6..668dd1c 100644 --- a/link_homework/internal/service/homework.go +++ b/link_homework/internal/service/homework.go @@ -7,12 +7,15 @@ package service import ( "context" + "link_homework/api/v1/homework" "link_homework/internal/model/entity" ) type ( IHomework interface { GetHomeworkList(ctx context.Context, pageNo int, pageSize int) (homeworkList []*entity.ActivityInteractiveGroup, err error) + AddHomework(ctx context.Context, req *homework.AddHomeworkReq) (err error) + EditHomework(ctx context.Context, req *homework.EditHomeworkReq) (err error) } ) diff --git a/link_homework/manifest/config/config.yaml b/link_homework/manifest/config/config.yaml index 9ff0ff8..af0239e 100644 --- a/link_homework/manifest/config/config.yaml +++ b/link_homework/manifest/config/config.yaml @@ -1,8 +1,6 @@ # https://goframe.org/docs/web/server-config-file-template server: address: ":8080" - openapiPath: "/api.json" - swaggerPath: "/swagger" # https://goframe.org/docs/core/glog-config logger: