diff --git a/api/v1/plant/enter.go b/api/v1/plant/enter.go index d09ceca..5c43209 100644 --- a/api/v1/plant/enter.go +++ b/api/v1/plant/enter.go @@ -4,10 +4,12 @@ import "sundynix-go/service" type ApiGroup struct { MyPlantApi + TopicApi PostApi } var ( plantService = service.GroupApp.PlantServiceGroup.MyPlantService + topicService = service.GroupApp.PlantServiceGroup.TopicService postService = service.GroupApp.PlantServiceGroup.PostService ) diff --git a/api/v1/plant/my_plant.go b/api/v1/plant/my_plant.go index 6a12dae..0baaa06 100644 --- a/api/v1/plant/my_plant.go +++ b/api/v1/plant/my_plant.go @@ -16,7 +16,7 @@ type MyPlantApi struct{} // AddPlant 添加植物 // @Tags 我的植物 // @Summary 添加植物 -// @Security ApiKeyAuth +// @Security BearerAuth // @accept json // @Produce application/json // @Param data body plantReq.CreateMyPlant true "创建植物" @@ -42,7 +42,7 @@ func (a *MyPlantApi) AddPlant(c *gin.Context) { // PlantPage 植物列表 // @Tags 我的植物 // @Summary 植物列表 -// @Security ApiKeyAuth +// @Security BearerAuth // @accept json // @Produce application/json // @Param data body request.PageInfo true "分页获取植物列表" @@ -72,7 +72,7 @@ func (a *MyPlantApi) PlantPage(c *gin.Context) { // PlantDetail 植物详情 // @Tags 我的植物 // @Summary ById植物详情 -// @Security ApiKeyAuth +// @Security BearerAuth // @Produce application/json // @Param id query string true "id" // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取ById成功"}" @@ -91,7 +91,7 @@ func (a *MyPlantApi) PlantDetail(c *gin.Context) { // UpdatePlant 修改植物 // @Tags 我的植物 // @Summary 修改ById植物 -// @Security ApiKeyAuth +// @Security BearerAuth // @accept json // @Produce application/json // @Param data body plantReq.UpdateMyPlant true "修改ById植物" @@ -116,7 +116,7 @@ func (a *MyPlantApi) UpdatePlant(c *gin.Context) { // TodayTask 今日任务 // @Tags 我的植物 // @Summary 今日任务 -// @Security ApiKeyAuth +// @Security BearerAuth // @accept json // @Produce application/json // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" @@ -137,7 +137,7 @@ func (a *MyPlantApi) TodayTask(c *gin.Context) { // CompleteTask plantReq.CompleteMyPlant // @Tags 我的植物 // @Summary 完成任务 -// @Security ApiKeyAuth +// @Security BearerAuth // @accept json // @Produce application/json // @Param data body plantReq.CompleteTask true "完成任务" @@ -162,12 +162,12 @@ func (a *MyPlantApi) CompleteTask(c *gin.Context) { // DeletePlants // @Tags 我的植物 // @Summary 删除植物 -// @Security ApiKeyAuth +// @Security BearerAuth // @accept json // @Produce application/json // @Param data body request.IdsReq true "删除植物" // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}" -// @Router /plant/delete/ [post] +// @Router /plant/deletePlant [post] func (a *MyPlantApi) DeletePlants(c *gin.Context) { var req request.IdsReq err := c.ShouldBindJSON(&req) @@ -186,12 +186,12 @@ func (a *MyPlantApi) DeletePlants(c *gin.Context) { // DeletePlans DeletePlants // @Tags 我的植物 // @Summary 删除任务 -// @Security ApiKeyAuth +// @Security BearerAuth // @accept json // @Produce application/json // @Param data body request.IdsReq true "删除植物" // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}" -// @Router /plant/deletePlant [post] +// @Router /plant/deletePlan [post] func (a *MyPlantApi) DeletePlans(c *gin.Context) { var req request.IdsReq err := c.ShouldBindJSON(&req) diff --git a/api/v1/plant/post.go b/api/v1/plant/post.go index 9e7b0f4..1635113 100644 --- a/api/v1/plant/post.go +++ b/api/v1/plant/post.go @@ -15,7 +15,7 @@ type PostApi struct{} // PublishPost 发布帖子 // @Tags 帖子 // @Summary 发布帖子 -// @Security ApiKeyAuth +// @Security BearerAuth // @accept json // @Produce application/json // @Param data body plantReq.CreatePost true "发布帖子" @@ -42,7 +42,7 @@ func (a *PostApi) PublishPost(c *gin.Context) { // PostPage 帖子列表 // @Tags 帖子 // @Summary 帖子列表 -// @Security ApiKeyAuth +// @Security BearerAuth // @accept json // @Produce application/json // @Param data body plantReq.PostPage true "分页获取帖子列表" @@ -73,7 +73,7 @@ func (a *PostApi) PostPage(c *gin.Context) { // LikePost 点赞帖子 // @Tags 帖子 // @Summary 点赞帖子 -// @Security ApiKeyAuth +// @Security BearerAuth // @Produce application/json // @Param id query string true "帖子id" // @Param type query string true "点赞类型 1 点赞 2 取消点赞" @@ -95,7 +95,7 @@ func (a *PostApi) LikePost(c *gin.Context) { // CommentPost 评论帖子 // @Tags 帖子 // @Summary 评论帖子 -// @Security ApiKeyAuth +// @Security BearerAuth // @accept json // @Produce application/json // @Param data body plantReq.CreateComment true "评论帖子" diff --git a/api/v1/plant/topic.go b/api/v1/plant/topic.go new file mode 100644 index 0000000..95893ca --- /dev/null +++ b/api/v1/plant/topic.go @@ -0,0 +1,152 @@ +package plant + +import ( + "sundynix-go/global" + "sundynix-go/model/commom/request" + "sundynix-go/model/commom/response" + plantReq "sundynix-go/model/plant/request" + + "github.com/gin-gonic/gin" + "go.uber.org/zap" +) + +type TopicApi struct{} + +// AddTopic 发布话题 +// @Tags 帖子话题 +// @Summary 发布话题 +// @Security BearerAuth +// @accept json +// @Produce application/json +// @Param data body plantReq.CreateTopic true "发布话题" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"发布成功"}" +// @Router /topic/add [post] +func (a *TopicApi) AddTopic(c *gin.Context) { + var req plantReq.CreateTopic + if err := c.ShouldBindJSON(&req); err != nil { + response.FailWithMsg("请求参数错误", c) + return + } + err := topicService.AddTopic(req) + if err != nil { + global.Logger.Error("添加话题失败", zap.Error(err)) + response.FailWithMsg("添加话题失败", c) + return + } + response.OkWithMsg("添加话题成功", c) +} + +// UpdateTopic +// @Tags 帖子话题 +// @Summary 修改话题 +// @Security BearerAuth +// @accept json +// @Produce application/json +// @Param data body plantReq.UpdateTopic true "修改话题" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"发布成功"}" +// @Router /topic/add [post] +func (a *TopicApi) UpdateTopic(c *gin.Context) { + var req plantReq.UpdateTopic + if err := c.ShouldBindJSON(&req); err != nil { + response.FailWithMsg("请求参数错误", c) + return + } + err := topicService.UpdateTopic(req) + if err != nil { + global.Logger.Error("修改话题失败", zap.Error(err)) + response.FailWithMsg("修改话题失败", c) + return + } + response.OkWithMsg("修改话题成功", c) +} + +// TopicPage 话题列表 +// @Tags 帖子话题 +// @Summary 话题分页 +// @Security BearerAuth +// @accept json +// @Produce application/json +// @Param data body request.PageInfo true "分页获取话题列表" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" +// @Router /topic/page [post] +func (a *TopicApi) TopicPage(c *gin.Context) { + var req request.PageInfo + if err := c.ShouldBindQuery(&req); err != nil { + response.FailWithMsg("请求参数错误", c) + return + } + list, total, err := topicService.TopicPage(req) + if err != nil { + global.Logger.Error("修改话题失败", zap.Error(err)) + response.FailWithMsg("修改话题失败", c) + return + } + response.OkWithData(response.PageResult{ + List: list, + Total: total, + Page: req.Current, + PageSize: req.PageSize, + }, c) +} + +// TopicList 话题列表 +// @Tags 帖子话题 +// @Summary 话题列表 +// @Security BearerAuth +// @Produce application/json +// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" +// @Router /topic/list [get] +func (a *TopicApi) TopicList(c *gin.Context) { + list, err := topicService.TopicList() + if err != nil { + global.Logger.Error("获取话题列表失败", zap.Error(err)) + response.FailWithMsg("获取话题列表失败", c) + return + } + response.OkWithData(response.ListResult{ + List: list, + }, c) +} + +// TopicDetail 话题详情 +// @Tags 帖子话题 +// @Summary 话题详情 +// @Security BearerAuth +// @Produce application/json +// @Param id query string true "id" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" +// @Router /topic/detail [get] +func (a *TopicApi) TopicDetail(c *gin.Context) { + id := c.Query("id") + topic, err := topicService.Detail(id) + if err != nil { + response.FailWithMsg("获取失败", c) + return + } + response.OkWithData(topic, c) + +} + +// DeleteTopic 删除植物 +// @Tags 帖子话题 +// @Summary 删除任务 +// @Security BearerAuth +// @accept json +// @Produce application/json +// @Param data body request.IdsReq true "删除话题" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}" +// @Router /topic/delete [post] +func (a *TopicApi) DeleteTopic(c *gin.Context) { + var req request.IdsReq + err := c.ShouldBindJSON(&req) + if err != nil { + response.FailWithMsg("请求参数错误", c) + return + } + err = topicService.DeleteTopics(req) + if err != nil { + global.Logger.Error("删除话题失败", zap.Error(err)) + response.FailWithMsg("删除话题失败", c) + } + response.OkWithMsg("删除话题成功", c) +} diff --git a/go.mod b/go.mod index 5b2675e..798a7b8 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,6 @@ require ( github.com/swaggo/swag v1.16.6 github.com/tencentyun/cos-go-sdk-v5 v0.7.70 github.com/wechatpay-apiv3/wechatpay-go v0.2.21 - github.com/xuri/excelize/v2 v2.10.0 go.uber.org/zap v1.27.0 golang.org/x/crypto v0.45.0 golang.org/x/sync v0.18.0 @@ -91,8 +90,6 @@ require ( github.com/pelletier/go-toml/v2 v2.2.4 // indirect github.com/philhofer/fwd v1.2.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect - github.com/richardlehane/mscfb v1.0.4 // indirect - github.com/richardlehane/msoleps v1.0.4 // indirect github.com/rs/xid v1.6.0 // indirect github.com/sagikazarmark/locafero v0.7.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect @@ -100,12 +97,9 @@ require ( github.com/spf13/cast v1.7.1 // indirect github.com/spf13/pflag v1.0.6 // indirect github.com/subosito/gotenv v1.6.0 // indirect - github.com/tiendc/go-deepcopy v1.7.1 // indirect github.com/tinylib/msgp v1.3.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.3.0 // indirect - github.com/xuri/efp v0.0.1 // indirect - github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/arch v0.21.0 // indirect golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 // indirect diff --git a/go.sum b/go.sum index 641ffb9..2cd1e9c 100644 --- a/go.sum +++ b/go.sum @@ -167,11 +167,6 @@ github.com/redis/go-redis/v9 v9.7.3 h1:YpPyAayJV+XErNsatSElgRZZVCwXX9QzkKYNvO7x0 github.com/redis/go-redis/v9 v9.7.3/go.mod h1:bGUrSggJ9X9GUmZpZNEOQKaANxSGgOEBRltRTZHSvrA= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= -github.com/richardlehane/mscfb v1.0.4 h1:WULscsljNPConisD5hR0+OyZjwK46Pfyr6mPu5ZawpM= -github.com/richardlehane/mscfb v1.0.4/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7gK3DypaEsUk= -github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg= -github.com/richardlehane/msoleps v1.0.4 h1:WuESlvhX3gH2IHcd8UqyCuFY5yiq/GR/yqaSM/9/g00= -github.com/richardlehane/msoleps v1.0.4/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= @@ -214,8 +209,6 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563/go.mod h1: github.com/tencentyun/cos-go-sdk-v5 v0.7.70 h1:gkBkSfrDvUg4ZIjwYAfjbNCCclen9LCRNHhBNz+yjEQ= github.com/tencentyun/cos-go-sdk-v5 v0.7.70/go.mod h1:STbTNaNKq03u+gscPEGOahKzLcGSYOj6Dzc5zNay7Pg= github.com/tencentyun/qcloud-cos-sts-sdk v0.0.0-20250515025012-e0eec8a5d123/go.mod h1:b18KQa4IxHbxeseW1GcZox53d7J0z39VNONTxvvlkXw= -github.com/tiendc/go-deepcopy v1.7.1 h1:LnubftI6nYaaMOcaz0LphzwraqN8jiWTwm416sitff4= -github.com/tiendc/go-deepcopy v1.7.1/go.mod h1:4bKjNC2r7boYOkD2IOuZpYjmlDdzjbpTRyCx+goBCJQ= github.com/tinylib/msgp v1.3.0 h1:ULuf7GPooDaIlbyvgAxBV/FI7ynli6LZ1/nVUNu+0ww= github.com/tinylib/msgp v1.3.0/go.mod h1:ykjzy2wzgrlvpDCRc4LA8UXy6D8bzMSuAF3WD57Gok0= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= @@ -224,12 +217,6 @@ github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= github.com/wechatpay-apiv3/wechatpay-go v0.2.21 h1:uIyMpzvcaHA33W/QPtHstccw+X52HO1gFdvVL9O6Lfs= github.com/wechatpay-apiv3/wechatpay-go v0.2.21/go.mod h1:A254AUBVB6R+EqQFo3yTgeh7HtyqRRtN2w9hQSOrd4Q= -github.com/xuri/efp v0.0.1 h1:fws5Rv3myXyYni8uwj2qKjVaRP30PdjeYe2Y6FDsCL8= -github.com/xuri/efp v0.0.1/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI= -github.com/xuri/excelize/v2 v2.10.0 h1:8aKsP7JD39iKLc6dH5Tw3dgV3sPRh8uRVXu/fMstfW4= -github.com/xuri/excelize/v2 v2.10.0/go.mod h1:SC5TzhQkaOsTWpANfm+7bJCldzcnU/jrhqkTi/iBHBU= -github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 h1:+C0TIdyyYmzadGaL/HBLbf3WdLgC29pgyhTjAT/0nuE= -github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= diff --git a/initialize/gorm.go b/initialize/gorm.go index 51af66f..82ef181 100644 --- a/initialize/gorm.go +++ b/initialize/gorm.go @@ -43,6 +43,7 @@ func MigrateTable() { plant.CarePlan{}, //植物养护计划 plant.CareTask{}, //植物养护任务 plant.CareRecord{}, //植物养护记录 + plant.Topic{}, //帖子话题 plant.Post{}, //帖子 plant.PostLike{}, //帖子点赞 plant.PostComment{}, //帖子评论 diff --git a/initialize/router.go b/initialize/router.go index dddb275..2cfa1a7 100644 --- a/initialize/router.go +++ b/initialize/router.go @@ -50,6 +50,7 @@ func Routers() { { //需要鉴权的路由 plantGroup.InitPlantRouter(NeedAuthGroup) // 植物相关 + plantGroup.InitTopicRouter(NeedAuthGroup) // 帖子话题 plantGroup.InitPostRouter(NeedAuthGroup) // 帖子相关 } diff --git a/model/plant/community_post_topic.go b/model/plant/community_post_topic.go new file mode 100644 index 0000000..e9f0120 --- /dev/null +++ b/model/plant/community_post_topic.go @@ -0,0 +1,14 @@ +package plant + +import ( + "sundynix-go/global" + "time" +) + +type Topic struct { + global.BaseModel + Title string `json:"title"` + StartTime time.Time `json:"start_time" gorm:"column:start_time;"` + EndTime time.Time `json:"end_time" gorm:"column:end_time;"` + Remark string `json:"remark"` +} diff --git a/model/plant/request/post.go b/model/plant/request/post.go index 26a8b44..aa23c63 100644 --- a/model/plant/request/post.go +++ b/model/plant/request/post.go @@ -1,6 +1,8 @@ package request -import common "sundynix-go/model/commom/request" +import ( + common "sundynix-go/model/commom/request" +) type CreatePost struct { Title string `json:"title"` // 标题 必须 diff --git a/model/plant/request/topic.go b/model/plant/request/topic.go new file mode 100644 index 0000000..0d601ef --- /dev/null +++ b/model/plant/request/topic.go @@ -0,0 +1,16 @@ +package request + +type CreateTopic struct { + Title string `json:"title"` + StartTime string `json:"start_time"` + EndTime string `json:"end_time"` + Remark string `json:"remark"` +} + +type UpdateTopic struct { + Id int `json:"id" binding:"required"` + Title string `json:"title"` + StartTime string `json:"start_time"` + EndTime string `json:"end_time"` + Remark string `json:"remark"` +} diff --git a/router/plant/enter.go b/router/plant/enter.go index 67add1d..5caefdd 100644 --- a/router/plant/enter.go +++ b/router/plant/enter.go @@ -4,11 +4,13 @@ import v1 "sundynix-go/api/v1" type RouterGroup struct { MyPlantRouter + TopicRouter PostRouter } // 初始化路由 var ( myPlantApi = v1.ApiGroupApp.PlantApiGroup.MyPlantApi + topicApi = v1.ApiGroupApp.PlantApiGroup.TopicApi postApi = v1.ApiGroupApp.PlantApiGroup.PostApi ) diff --git a/router/plant/post_router.go b/router/plant/post_router.go index 8f916f3..e435737 100644 --- a/router/plant/post_router.go +++ b/router/plant/post_router.go @@ -7,6 +7,7 @@ type PostRouter struct{} func (p *PostRouter) InitPostRouter(Router *gin.RouterGroup) { postRouter := Router.Group("post") { + // 帖子 postRouter.POST("publish", postApi.PublishPost) // 发布帖子 postRouter.POST("page", postApi.PostPage) // 帖子列表 postRouter.GET("like", postApi.LikePost) // 点赞或者取消点赞 diff --git a/router/plant/topic_router.go b/router/plant/topic_router.go new file mode 100644 index 0000000..b721a55 --- /dev/null +++ b/router/plant/topic_router.go @@ -0,0 +1,19 @@ +package plant + +import "github.com/gin-gonic/gin" + +type TopicRouter struct{} + +func (p *TopicRouter) InitTopicRouter(Router *gin.RouterGroup) { + topicRouter := Router.Group("topic") + { + //话题管理 + topicRouter.POST("/add", topicApi.AddTopic) // 添加话题 + topicRouter.POST("/update", topicApi.UpdateTopic) //修改话题 + topicRouter.POST("/page", topicApi.TopicPage) //分页 + topicRouter.GET("/list", topicApi.TopicList) + topicRouter.GET("/detail", topicApi.TopicDetail) + topicRouter.POST("/delete", topicApi.DeleteTopic) + + } +} diff --git a/service/plant/enter.go b/service/plant/enter.go index e1af4a6..b5f2768 100644 --- a/service/plant/enter.go +++ b/service/plant/enter.go @@ -2,5 +2,6 @@ package plant type ServiceGroup struct { MyPlantService + TopicService PostService } diff --git a/service/plant/my_plant.go b/service/plant/my_plant.go index 7a089bc..1c03e92 100644 --- a/service/plant/my_plant.go +++ b/service/plant/my_plant.go @@ -128,7 +128,6 @@ func (s *MyPlantService) UpdatePlant(req plantReq.UpdateMyPlant) error { } //2.修改计划 if len(req.CarePlans) > 0 { - //删除已经存在的计划 生成新的计划 for _, plan := range req.CarePlans { err = tx.Model(&plant.CarePlan{}).Where("id = ?", plan.Id).Updates(map[string]interface{}{ "icon": plan.Icon, diff --git a/service/plant/topic.go b/service/plant/topic.go new file mode 100644 index 0000000..31d3700 --- /dev/null +++ b/service/plant/topic.go @@ -0,0 +1,88 @@ +package plant + +import ( + "errors" + "sundynix-go/global" + common "sundynix-go/model/commom/request" + "sundynix-go/model/plant" + plantReq "sundynix-go/model/plant/request" + "time" + + "gorm.io/gorm" +) + +type TopicService struct{} + +var TopicServiceApp = new(TopicService) + +// AddTopic 发布话题 +func (s *TopicService) AddTopic(req plantReq.CreateTopic) error { + // 查询是否存在 + if !errors.Is(global.DB.Where("title = ?", req.Title).First(&plant.Topic{}).Error, gorm.ErrRecordNotFound) { + return errors.New("存在重复话题") + } + start, _ := time.Parse("2006-01-02 15:04:05", req.StartTime) + end, _ := time.Parse("2006-01-02 15:04:05", req.EndTime) + return global.DB.Create(&plant.Topic{ + Title: req.Title, + StartTime: start, + EndTime: end, + Remark: req.Remark, + }).Error +} + +// UpdateTopic 修改话题 +func (s *TopicService) UpdateTopic(req plantReq.UpdateTopic) error { + start, _ := time.Parse("2006-01-02 15:04:05", req.StartTime) + end, _ := time.Parse("2006-01-02 15:04:05", req.EndTime) + updateMap := map[string]interface{}{ + "title": req.Title, + "start_time": start, + "end_time": end, + "remark": req.Remark, + } + err := global.DB.Model(&plant.Topic{}).Where("id = ?", req.Id).Updates(updateMap).Error + return err +} + +// TopicPage 分页 +func (s *TopicService) TopicPage(req common.PageInfo) (list interface{}, total int64, err error) { + limit := req.PageSize + offset := req.PageSize * (req.Current - 1) + db := global.DB.Model(&plant.Topic{}) + var topics []*plant.Topic + err = db.Count(&total).Error + err = db.Limit(limit).Offset(offset).Order("created_at desc").Find(&topics).Error + return topics, total, err +} + +// TopicList 话题列表 +func (s *TopicService) TopicList() (list interface{}, err error) { + var topics []plant.Topic + err = global.DB.Order("created_at desc").Find(&topics).Error + return topics, err +} + +// Detail 话题详情 +func (s *TopicService) Detail(id string) (t plant.Topic, err error) { + var res plant.Topic + err = global.DB.Where("id = ?", id).First(&res).Error + if err != nil { + return res, err + } + return res, nil +} + +// DeleteTopics 删除话题 +func (s *TopicService) DeleteTopics(req common.IdsReq) error { + var topics []plant.Topic + err := global.DB.Where("id in (?)", req.Ids).Find(&topics).Error + if err != nil { + return err + } + err = global.DB.Unscoped().Delete(&plant.Topic{}).Error + if err != nil { + return err + } + return nil +}