voice_broadcast.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. package voice_broadcast
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "hongze/hongze_yb/controller/response"
  5. "hongze/hongze_yb/models/request"
  6. voiceResp "hongze/hongze_yb/models/response"
  7. "hongze/hongze_yb/models/tables/voice_broadcast"
  8. "hongze/hongze_yb/models/tables/voice_section"
  9. "hongze/hongze_yb/services"
  10. "hongze/hongze_yb/services/user"
  11. "hongze/hongze_yb/utils"
  12. "strconv"
  13. "strings"
  14. )
  15. // BroadcastList
  16. // @Description 语音播报列表
  17. // @Param page_index query int false "页码"
  18. // @Param page_size query int false "每页数量"
  19. // @Param broadcast_id query int false "语音播报ID(分享进来的时候筛选用)"
  20. // @Param section_id query int false "板块ID"
  21. // @Param author_id query int false "作者ID(我的语音播报列表)"
  22. // @Param mine_status query int false "语音播报状态:0-未发布 1-已发布 2-全部(我的语音播报列表)"
  23. // @Success 200 {object} []voiceResp.BroadcastListResp
  24. // @failure 400 {string} string "获取失败"
  25. // @Router /list [Post]
  26. func BroadcastList(c *gin.Context) {
  27. var req request.BroadcastListReq
  28. if err := c.Bind(&req); err != nil {
  29. response.Fail("参数有误", c)
  30. return
  31. }
  32. if req.PageIndex == 0 {
  33. req.PageIndex = 1
  34. }
  35. if req.PageSize == 0 {
  36. req.PageSize = utils.PageSize20
  37. }
  38. userinfo := user.GetInfoByClaims(c)
  39. list, err := services.GetVoiceBroadcastList(req.PageIndex, req.PageSize, req.SectionId, req.BroadcastId, req.AuthorId, req.MineStatus, userinfo)
  40. if err != nil {
  41. response.FailMsg("获取语音播报列表失败,"+err.Error(), "QuestionList ErrMsg:"+err.Error(), c)
  42. return
  43. }
  44. isVoiceAdmin, _, err := services.GetVoiceAdminByUserInfo(userinfo)
  45. if err != nil && err != utils.ErrNoRow {
  46. response.FailMsg("获取语音管理员信息失败", "QuestionList ErrMsg:"+err.Error(), c)
  47. return
  48. }
  49. var resp voiceResp.BroadcastListResp
  50. resp.List = list
  51. resp.IsVoiceAdmin = isVoiceAdmin
  52. response.OkData("获取成功", resp, c)
  53. }
  54. // AddBroadcast
  55. // @Description 新增语音播报
  56. // @Param broadcast_name query string true "语音标题"
  57. // @Param section_id query int true "板块ID"
  58. // @Param section_name query string true "板块名称"
  59. // @Param variety_id query int true "品种ID"
  60. // @Param variety_name query string true "品种名称"
  61. // @Param author_id query int true "作者ID"
  62. // @Param author query string true "作者名称"
  63. // @Param Imgs query string false "图片,英文逗号拼接"
  64. // @Param voice_seconds query string true "音频时长"
  65. // @Param voice_size query string true "音频大小"
  66. // @Param voice_url query string true "音频文件地址"
  67. // @Success 200 {string} string "发布成功"
  68. // @failure 400 {string} string "发布失败"
  69. // @Router /add [post]
  70. func AddBroadcast(c *gin.Context) {
  71. var req request.SaveBroadcastReq
  72. if err := c.ShouldBind(&req); err != nil {
  73. response.Fail("参数有误", c)
  74. return
  75. }
  76. // 参数校验
  77. if req.BroadcastName == "" {
  78. response.Fail("请输入标题", c)
  79. return
  80. }
  81. if req.SectionId <= 0 || req.SectionName == "" {
  82. response.Fail("请选择品种", c)
  83. return
  84. }
  85. if req.SectionId <= 0 || req.SectionName == "" {
  86. response.Fail("请选择板块", c)
  87. return
  88. }
  89. if req.VoiceUrl == "" || req.VoiceSeconds == "" || req.VoiceSize == "" {
  90. response.Fail("请上传音频", c)
  91. return
  92. }
  93. if req.Imgs != "" {
  94. imgList := strings.Split(req.Imgs, ",")
  95. if len(imgList) > 5 {
  96. response.Fail("最多插入五张图片", c)
  97. return
  98. }
  99. }
  100. userInfo := user.GetInfoByClaims(c)
  101. // 新增
  102. resp, e := services.CreateVoiceBroadcast(req.SectionId, req.VarietyId, req.AuthorId, req.BroadcastName, req.SectionName, req.VarietyName,
  103. req.Author, req.VoiceSeconds, req.VoiceSize, req.VoiceUrl, req.Imgs, userInfo)
  104. if e != nil {
  105. response.FailMsg("新增失败", e.Error(), c)
  106. return
  107. }
  108. response.OkData("操作成功", resp, c)
  109. }
  110. // EditBroadcast
  111. // @Description 编辑语音播报
  112. // @Param broadcast_id query int true "语音播报ID"
  113. // @Param broadcast_name query string true "语音标题"
  114. // @Param section_id query int true "板块ID"
  115. // @Param section_name query string true "板块名称"
  116. // @Param variety_id query int true "品种ID"
  117. // @Param variety_name query string true "品种名称"
  118. // @Param author_id query int true "作者ID"
  119. // @Param author query string true "作者名称"
  120. // @Param Imgs query string false "图片,英文逗号拼接"
  121. // @Param voice_seconds query string true "音频时长"
  122. // @Param voice_size query string true "音频大小"
  123. // @Param voice_url query string true "音频文件地址"
  124. // @Success 200 {string} string "发布成功"
  125. // @failure 400 {string} string "发布失败"
  126. // @Router /edit [post]
  127. func EditBroadcast(c *gin.Context) {
  128. var req request.SaveBroadcastReq
  129. if err := c.ShouldBind(&req); err != nil {
  130. response.Fail("参数有误", c)
  131. return
  132. }
  133. // 参数校验
  134. if req.BroadcastId <= 0 {
  135. response.Fail("参数有误", c)
  136. return
  137. }
  138. if req.BroadcastName == "" {
  139. response.Fail("请输入标题", c)
  140. return
  141. }
  142. if req.SectionId <= 0 || req.SectionName == "" {
  143. response.Fail("请选择品种", c)
  144. return
  145. }
  146. if req.SectionId <= 0 || req.SectionName == "" {
  147. response.Fail("请选择板块", c)
  148. return
  149. }
  150. if req.VoiceUrl == "" || req.VoiceSeconds == "" || req.VoiceSize == "" {
  151. response.Fail("请上传音频", c)
  152. return
  153. }
  154. if req.Imgs != "" {
  155. imgList := strings.Split(req.Imgs, ",")
  156. if len(imgList) > 5 {
  157. response.Fail("最多插入五张图片", c)
  158. return
  159. }
  160. }
  161. userInfo := user.GetInfoByClaims(c)
  162. // 编辑
  163. resp, e := services.EditVoiceBroadcast(req.BroadcastId, req.SectionId, req.VarietyId, req.AuthorId, req.BroadcastName, req.SectionName, req.VarietyName,
  164. req.Author, req.VoiceSeconds, req.VoiceSize, req.VoiceUrl, req.Imgs, userInfo)
  165. if e != nil {
  166. response.FailMsg("新增失败", e.Error(), c)
  167. return
  168. }
  169. response.OkData("操作成功", resp, c)
  170. }
  171. // PublishBroadcast
  172. // @Description 发布语音播报
  173. // @Param broadcast_id query int true "语音播报ID"
  174. // @Param publish_type query int true "发布类型: 1-发布 2-定时发布"
  175. // @Param pre_publish_time query string false "预发布时间"
  176. // @Success 200 {string} string "发布成功"
  177. // @failure 400 {string} string "发布失败"
  178. // @Router /publish [post]
  179. func PublishBroadcast(c *gin.Context) {
  180. var req request.PublishBroadcastReq
  181. if err := c.ShouldBind(&req); err != nil {
  182. response.Fail("参数有误", c)
  183. return
  184. }
  185. // 参数校验
  186. if req.BroadcastId <= 0 {
  187. response.Fail("参数有误", c)
  188. return
  189. }
  190. if req.PublishType <= 0 {
  191. response.Fail("请选择发布类型", c)
  192. return
  193. }
  194. if req.PublishType == 2 && req.PrePublishTime == "" {
  195. response.Fail("定时发布请选择发布时间", c)
  196. return
  197. }
  198. // 发布
  199. if e := services.PublishVoiceBroadcast(req.BroadcastId, req.PublishType, req.PrePublishTime); e != nil {
  200. response.FailMsg("发布失败", e.Error(), c)
  201. return
  202. }
  203. response.Ok("操作成功", c)
  204. }
  205. // SectionList
  206. // @Description 语音播报板块列表
  207. // @Success 200 {object} []voiceResp.VarietyList
  208. // @failure 400 {string} string "获取失败"
  209. // @Router /section/list [get]
  210. func SectionList(c *gin.Context) {
  211. sList, err := voice_section.GetVoiceSection()
  212. if err != nil {
  213. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  214. }
  215. vList, err := voice_section.GetVoiceVariety()
  216. if err != nil {
  217. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  218. }
  219. var sectionList []voiceResp.SectionList
  220. var varietyList []voiceResp.VarietyList
  221. var resp []voiceResp.VarietyList
  222. //var resp voiceResp.SectionListResp
  223. //for _, s := range sList {
  224. // section := voiceResp.SectionList{
  225. // SectionId: s.SectionId,
  226. // SectionName: s.SectionName,
  227. // Status: s.Status,
  228. // }
  229. // sectionList = append(sectionList, section)
  230. //}
  231. var newsList []*voice_section.VoiceSection
  232. //var bannedSectionList []*voice_section.VoiceSection
  233. //查找被禁用的板块ids
  234. var bannedIds []int
  235. for _, section := range sList {
  236. if section.Status == 0 {
  237. //bannedSectionList = append(bannedSectionList, section)
  238. bannedIds = append(bannedIds, section.SectionId)
  239. } else {
  240. newsList = append(newsList, section)
  241. }
  242. }
  243. //如果有被禁用的板块,去语音列表查找被禁用板块有没有语音
  244. var lists []*voice_broadcast.VoiceBroadcast
  245. if len(bannedIds) > 0 {
  246. lists, err = voice_section.GetVoiceSectionFromBroadcast(bannedIds)
  247. if err != nil {
  248. response.FailMsg("查询语音播报禁用板块失败", "GetVoiceSectionFromBroadcast, Err:"+err.Error(), c)
  249. }
  250. }
  251. //被禁用板块有语音,依然显示该板块
  252. if len(lists) > 0 {
  253. //清空切片,用新的
  254. newsList = newsList[0:0]
  255. bannedMap := make(map[int]int)
  256. for _, broadcast := range lists {
  257. bannedMap[broadcast.SectionId] = broadcast.SectionId
  258. }
  259. for _, section := range sList {
  260. _, ok := bannedMap[section.SectionId]
  261. if section.Status != 0 || ok {
  262. newsList = append(newsList, section)
  263. }
  264. }
  265. }
  266. for _, v := range vList {
  267. variety := voiceResp.VarietyList{
  268. VarietyId: v.VarietyId,
  269. VarietyName: v.VarietyName,
  270. }
  271. varietyList = append(varietyList, variety)
  272. }
  273. for _, v := range varietyList {
  274. for _, s := range newsList {
  275. if v.VarietyId == s.VarietyId {
  276. section := voiceResp.SectionList{
  277. ImgUrl: s.ImgUrl,
  278. SectionId: s.SectionId,
  279. SectionName: s.SectionName,
  280. Status: s.Status,
  281. }
  282. sectionList = append(sectionList, section)
  283. }
  284. }
  285. if len(sectionList) == 0 {
  286. continue
  287. }
  288. v.Children = sectionList
  289. resp = append(resp, v)
  290. sectionList = []voiceResp.SectionList{}
  291. }
  292. response.OkData("上传成功", resp, c)
  293. }
  294. // DelBroadcast
  295. // @Description 删除语音播报
  296. // @Param broadcast_id query int false "语音播报id"
  297. // @Success 200 {string} string "删除成功"
  298. // @failure 400 {string} string "删除失败"
  299. // @Router /delete [get]
  300. func DelBroadcast(c *gin.Context) {
  301. sbroadcastId := c.DefaultQuery("broadcast_id", "0")
  302. broadcastId, err := strconv.Atoi(sbroadcastId)
  303. if err != nil {
  304. response.FailMsg("转换id失败,请输入正确的id", "strconv.Atoi, Err:"+err.Error(), c)
  305. return
  306. }
  307. if broadcastId <= 0 {
  308. response.FailMsg("参数错误", "参数有误", c)
  309. return
  310. }
  311. userInfo := user.GetInfoByClaims(c)
  312. // 是否为内部员工
  313. ok, _, err := user.GetAdminByUserInfo(userInfo)
  314. if err != nil {
  315. response.FailMsg("您无权操作", "获取系统用户信息失败"+err.Error(), c)
  316. return
  317. }
  318. if !ok {
  319. response.FailMsg("您无权操作", "非公司内部员工", c)
  320. return
  321. }
  322. var item voice_broadcast.VoiceBroadcast
  323. item.BroadcastId = broadcastId
  324. err = item.DelVoiceBroadcast()
  325. if err != nil {
  326. response.FailMsg("删除语音播报失败", "DelVoiceBroadcast, Err:"+err.Error(), c)
  327. return
  328. }
  329. response.Ok("删除成功", c)
  330. }
  331. // AddStatistics
  332. // @Description 新增语音播报记录
  333. // @Param file query string true "音频文件"
  334. // @Success 200 {string} string "新增成功"
  335. // @failure 400 {string} string "新增失败"
  336. // @Router /statistics/add [post]
  337. func AddStatistics(c *gin.Context) {
  338. var req request.AddBroadcastStatisticsReq
  339. if err := c.Bind(&req); err != nil {
  340. response.Fail("参数有误", c)
  341. return
  342. }
  343. if req.BroadcastId <= 0 {
  344. response.Fail("参数有误", c)
  345. }
  346. userinfo := user.GetInfoByClaims(c)
  347. go services.AddBroadcastRecord(userinfo, req.Source, req.BroadcastId)
  348. response.Ok("新增记录成功", c)
  349. }
  350. // BroadcastDetail 获取语音播报详情
  351. // @Tags 语音播报模块
  352. // @Description 获取语音播报详情
  353. // @Param broadcast_id query int true "语音播报ID"
  354. // @Success 200 {object} voiceResp.Broadcast
  355. // @failure 400 {string} string "获取失败"
  356. // @Router /detail [get]
  357. func BroadcastDetail(c *gin.Context) {
  358. var req request.BroadcastDetailReq
  359. if err := c.Bind(&req); err != nil {
  360. response.Fail("参数有误", c)
  361. return
  362. }
  363. if req.BroadcastId <= 0 {
  364. response.Fail("参数有误", c)
  365. return
  366. }
  367. userInfo := user.GetInfoByClaims(c)
  368. resp, e := services.GetVoiceBroadcastDetail(req.BroadcastId, userInfo)
  369. if e != nil {
  370. response.FailMsg("获取失败", "BroadcastDetail ErrMsg:"+e.Error(), c)
  371. return
  372. }
  373. response.OkData("获取成功", resp, c)
  374. }
  375. // MsgSend 语音播报消息推送
  376. // @Tags 语音播报模块
  377. // @Description 语音播报消息推送
  378. // @Param broadcast_id query int true "语音播报ID"
  379. // @Success 200 {string} string "操作成功"
  380. // @failure 400 {string} string "操作失败"
  381. // @Router /msg_send [post]
  382. func MsgSend(c *gin.Context) {
  383. var req request.BroadcastMsgSendReq
  384. if err := c.Bind(&req); err != nil {
  385. response.Fail("参数有误", c)
  386. return
  387. }
  388. if req.BroadcastId <= 0 {
  389. response.Fail("参数有误", c)
  390. return
  391. }
  392. userInfo := user.GetInfoByClaims(c)
  393. errMsg, err := services.SendBroadcastMsg(req.BroadcastId, int(userInfo.UserID))
  394. if err != nil {
  395. response.FailMsg(errMsg, "MsgSend ErrMsg:"+err.Error(), c)
  396. return
  397. }
  398. response.Ok("操作成功", c)
  399. }
  400. // BroadcastDetail 获取语音播报详情
  401. // @Tags 语音播报模块
  402. // @Description 获取语音播报详情
  403. // @Param broadcast_id query int true "语音播报ID"
  404. // @Success 200 {object} voiceResp.Broadcast
  405. // @failure 400 {string} string "获取失败"
  406. // @Router /detail [get]
  407. func MyVoiceBroadcastListCount(c *gin.Context) {
  408. var req request.BroadcastListCountReq
  409. if err := c.Bind(&req); err != nil {
  410. response.Fail("参数有误", c)
  411. return
  412. }
  413. if req.AuthorId <= 0 {
  414. response.Fail("参数有误", c)
  415. return
  416. }
  417. resp, e := services.GetMyVoiceBroadcastListCount(req.AuthorId, req.SectionId)
  418. if e != nil {
  419. response.FailMsg("获取失败", "BroadcastDetail ErrMsg:"+e.Error(), c)
  420. return
  421. }
  422. response.OkData("获取成功", resp, c)
  423. }