Browse Source

我的语音播报列表计数

hsun 2 years ago
parent
commit
210318df4a

+ 25 - 0
controller/voice_broadcast/voice_broadcast.go

@@ -404,3 +404,28 @@ func MsgSend(c *gin.Context) {
 	}
 	response.Ok("操作成功", c)
 }
+
+// BroadcastDetail 获取语音播报详情
+// @Tags 语音播报模块
+// @Description 获取语音播报详情
+// @Param broadcast_id  query  int  true  "语音播报ID"
+// @Success 200 {object} voiceResp.Broadcast
+// @failure 400 {string} string "获取失败"
+// @Router /detail [get]
+func MyVoiceBroadcastListCount(c *gin.Context) {
+	var req request.BroadcastListCountReq
+	if err := c.Bind(&req); err != nil {
+		response.Fail("参数有误", c)
+		return
+	}
+	if req.AuthorId <= 0 {
+		response.Fail("参数有误", c)
+		return
+	}
+	resp, e := services.GetMyVoiceBroadcastListCount(req.AuthorId, req.SectionId)
+	if e != nil {
+		response.FailMsg("获取失败", "BroadcastDetail ErrMsg:"+e.Error(), c)
+		return
+	}
+	response.OkData("获取成功", resp, c)
+}

+ 5 - 0
models/request/voice_broadcast.go

@@ -43,3 +43,8 @@ type PublishBroadcastReq struct {
 	ImgUrl         string `json:"img_url" form:"img_url" description:"分享背景图"`
 	PrePublishTime string `json:"pre_publish_time" form:"pre_publish_time" description:"预发布时间"`
 }
+
+type BroadcastListCountReq struct {
+	AuthorId  int `json:"author_id" form:"author_id" description:"作者ID"`
+	SectionId int `json:"section_id" form:"section_id" description:"板块ID"`
+}

+ 6 - 0
models/response/voice_broadcast.go

@@ -41,3 +41,9 @@ type SectionList struct {
 	SectionName string
 	Status      int
 }
+
+type BroadcastListStatusCount struct {
+	Unpublished int `description:"未发布"`
+	Published   int `description:"已发布"`
+	All         int `description:"全部"`
+}

+ 15 - 0
models/tables/voice_broadcast/query.go

@@ -39,3 +39,18 @@ func GetPageListByCondition(condition string, pars []interface{}, pageIndex, pag
 		Scan(&list).Error
 	return
 }
+
+type VoiceBroadcastListStatusCount struct {
+	Num   int `json:"num" description:"计数"`
+	State int `json:"state" description:"状态:0-未发布 1-已发布"`
+}
+
+// GetVoiceBroadcastListStatusCount 获取语音播报列表计数-根据状态分组
+func GetVoiceBroadcastListStatusCount(condition string, pars []interface{}) (list []*VoiceBroadcastListStatusCount, err error) {
+	err = global.DEFAULT_MYSQL.Model(VoiceBroadcast{}).
+		Where(condition, pars...).
+		Select("COUNT(1) AS num, publish_state AS state").
+		Group("state").
+		Scan(&list).Error
+	return
+}

+ 1 - 0
routers/voice_broadcast.go

@@ -19,4 +19,5 @@ func InitVoiceBroadcast(r *gin.Engine) {
 	rGroup2.POST("/list", voice_broadcast.BroadcastList)
 	rGroup2.GET("/detail", voice_broadcast.BroadcastDetail)
 	rGroup2.POST("/msg_send", voice_broadcast.MsgSend)
+	rGroup2.GET("/list_count", voice_broadcast.MyVoiceBroadcastListCount)
 }

+ 32 - 0
services/voice_broadcast.go

@@ -436,3 +436,35 @@ func handleBroadcastItem(userId int, item *voice_broadcast.VoiceBroadcast, imgs
 	resp.Imgs = imgArr
 	return
 }
+
+// GetMyVoiceBroadcastListCount 获取我的语音播报列表计数
+func GetMyVoiceBroadcastListCount(authorId, sectionId int) (resp response.BroadcastListStatusCount, err error) {
+	condition := ` 1=1 `
+	var pars []interface{}
+	// 我的-非我的只能看到已发布
+	if authorId > 0 {
+		condition += ` AND author_id = ?`
+		pars = append(pars, authorId)
+	}
+	// 板块
+	if sectionId > 0 {
+		condition += ` AND section_id = ?`
+		pars = append(pars, sectionId)
+	}
+	counts, e := voice_broadcast.GetVoiceBroadcastListStatusCount(condition, pars)
+	if e != nil {
+		err = errors.New("获取语音播报列表计数失败, Err: " + e.Error())
+		return
+	}
+	for _, v := range counts {
+		if v.State == 0 {
+			resp.Unpublished = v.Num
+			continue
+		}
+		if v.State == 1 {
+			resp.Published = v.Num
+		}
+	}
+	resp.All = resp.Unpublished + resp.Published
+	return
+}