voice_broadcast.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_yb/global"
  7. "hongze/hongze_yb/models/response"
  8. admin2 "hongze/hongze_yb/models/tables/admin"
  9. "hongze/hongze_yb/models/tables/company_product"
  10. "hongze/hongze_yb/models/tables/sys_role_admin"
  11. "hongze/hongze_yb/models/tables/voice_broadcast"
  12. "hongze/hongze_yb/models/tables/voice_broadcast_img"
  13. "hongze/hongze_yb/models/tables/voice_broadcast_statistics"
  14. "hongze/hongze_yb/models/tables/voice_section"
  15. "hongze/hongze_yb/services/user"
  16. "hongze/hongze_yb/services/wechat"
  17. "hongze/hongze_yb/utils"
  18. "strings"
  19. "time"
  20. )
  21. // GetVoiceBroadcastList 获取语音播报列表
  22. func GetVoiceBroadcastList(pageIndex, pageSize, sectionId, broadcastId, authorId, mineStatus int, userInfo user.UserInfo) (resp []response.Broadcast, err error) {
  23. condition := ` 1=1`
  24. var pars []interface{}
  25. // 分享进来的指定语音播报
  26. if broadcastId > 0 {
  27. condition += ` AND broadcast_id = ? AND publish_state = 1`
  28. pars = append(pars, broadcastId)
  29. } else {
  30. // 我的-非我的只能看到已发布
  31. if authorId > 0 {
  32. condition += ` AND author_id = ?`
  33. pars = append(pars, authorId)
  34. // 我的语音播报状态: 0-未发布 1-已发布 2-全部
  35. if mineStatus != 2 {
  36. condition += ` AND publish_state = ?`
  37. pars = append(pars, mineStatus)
  38. }
  39. } else {
  40. condition += ` AND publish_state = 1`
  41. }
  42. // 板块
  43. if sectionId > 0 {
  44. condition += ` AND section_id = ?`
  45. pars = append(pars, sectionId)
  46. }
  47. }
  48. voiceList, e := voice_broadcast.GetPageListByCondition(condition, pars, pageIndex, pageSize)
  49. if e != nil {
  50. err = errors.New("获取语音播报列表失败, Err: " + e.Error())
  51. return
  52. }
  53. listLen := len(voiceList)
  54. if listLen == 0 {
  55. return
  56. }
  57. // 图片
  58. voiceIds := make([]int, 0)
  59. for i := 0; i < listLen; i++ {
  60. voiceIds = append(voiceIds, voiceList[i].BroadcastId)
  61. }
  62. imgList, e := voice_broadcast_img.GetVoiceImgListByVoiceIds(voiceIds)
  63. if e != nil {
  64. err = errors.New("获取语音播报列表图片失败, Err: " + e.Error())
  65. return
  66. }
  67. imgMap := make(map[int][]*voice_broadcast_img.YbVoiceBroadcastImg, 0)
  68. imgListLen := len(imgList)
  69. for i := 0; i < imgListLen; i++ {
  70. imgMap[imgList[i].BroadcastId] = append(imgMap[imgList[i].BroadcastId], imgList[i])
  71. }
  72. // 响应数据
  73. userId := int(userInfo.UserID)
  74. for i := 0; i < listLen; i++ {
  75. r := handleBroadcastItem(userId, voiceList[i], imgMap[voiceList[i].BroadcastId])
  76. resp = append(resp, r)
  77. }
  78. return
  79. }
  80. // GetVoiceAdminByUserInfo 判断当前用户是否为语音管理员
  81. func GetVoiceAdminByUserInfo(userInfo user.UserInfo) (ok bool, adminInfo *admin2.Admin, err error) {
  82. mobile := userInfo.Mobile
  83. var email string
  84. if mobile == "" {
  85. // 用户有可能是通过邮箱登录
  86. email = userInfo.Email
  87. if email == "" {
  88. return
  89. }
  90. }
  91. if userInfo.CompanyID != 16 {
  92. return
  93. }
  94. if mobile != "" {
  95. adminInfo, err = admin2.GetAdminByMobile(mobile)
  96. if err != nil {
  97. if err == utils.ErrNoRow {
  98. err = nil
  99. return
  100. }
  101. return
  102. }
  103. } else {
  104. adminInfo, err = admin2.GetAdminByEmail(email)
  105. if err != nil {
  106. if err == utils.ErrNoRow {
  107. err = nil
  108. return
  109. }
  110. return
  111. }
  112. }
  113. if adminInfo.Enabled != 1 {
  114. return
  115. }
  116. _, err = sys_role_admin.GetVoiceAdmin(int(adminInfo.AdminID))
  117. if err != nil && err != utils.ErrNoRow {
  118. return
  119. }
  120. if err == utils.ErrNoRow {
  121. ok = false
  122. return
  123. }
  124. ok = true
  125. return
  126. }
  127. func AddBroadcastRecord(userinfo user.UserInfo, source, broadcastId int) {
  128. var err error
  129. defer func() {
  130. if err != nil {
  131. global.LOG.Critical(fmt.Sprintf("AddBroadcastLog: userId=%d, err:%s", userinfo.UserID, err.Error()))
  132. }
  133. }()
  134. companyProduct, err := company_product.GetByCompany2ProductId(userinfo.CompanyID, 1)
  135. if err != nil {
  136. return
  137. }
  138. broadcast, err := voice_broadcast.GetBroadcastById(broadcastId)
  139. if err != nil {
  140. return
  141. }
  142. voiceBroadcastStatistics := voice_broadcast_statistics.VoiceBroadcastStatistics{
  143. CompanyId: companyProduct.CompanyID,
  144. CompanyName: companyProduct.CompanyName,
  145. UserId: int(userinfo.UserID),
  146. RealName: userinfo.RealName,
  147. Mobile: userinfo.Mobile,
  148. Email: userinfo.Email,
  149. CompanyStatus: companyProduct.Status,
  150. SellerId: companyProduct.SellerID,
  151. Source: source,
  152. BroadcastId: broadcastId,
  153. BroadcastName: broadcast.BroadcastName,
  154. SectionId: broadcast.SectionId,
  155. SectionName: broadcast.SectionName,
  156. VarietyId: broadcast.VarietyId,
  157. VarietyName: broadcast.VarietyName,
  158. AuthorId: broadcast.AuthorId,
  159. Author: broadcast.Author,
  160. PublishTime: broadcast.CreateTime,
  161. CreateTime: time.Now().Format(utils.FormatDateTime),
  162. }
  163. err = voiceBroadcastStatistics.AddBroadcastStatistics()
  164. if err != nil {
  165. return
  166. }
  167. return
  168. }
  169. // SendBroadcastMsg 推送语音播报消息
  170. func SendBroadcastMsg(broadcastId, userId int) (errMsg string, err error) {
  171. broadcast, e := voice_broadcast.GetBroadcastById(broadcastId)
  172. if e != nil {
  173. errMsg = "推送失败, 语音播报信息有误"
  174. err = errors.New("获取语音播报信息失败, Err: " + e.Error())
  175. return
  176. }
  177. if broadcast.PublishState != 1 {
  178. errMsg = "报告未发布, 不可推送"
  179. err = errors.New("报告未发布, 不可推送")
  180. return
  181. }
  182. if broadcast.AuthorId != userId {
  183. errMsg = "仅语音播报创建人可推送"
  184. err = errors.New("仅语音播报创建人可推送")
  185. return
  186. }
  187. if broadcast.MsgState != 0 {
  188. errMsg = "请勿重复推送"
  189. err = errors.New("请勿重复推送")
  190. return
  191. }
  192. // 更新语音播报信息
  193. updateCols := []string{"MsgState", "MsgTime"}
  194. broadcast.MsgState = 1
  195. broadcast.MsgTime = time.Now().Format(utils.FormatDateTime)
  196. if e = broadcast.Update(updateCols); e != nil {
  197. errMsg = "更新语音播报失败"
  198. err = errors.New("更新语音播报失败")
  199. return
  200. }
  201. // 推送模板消息
  202. go func() {
  203. _ = wechat.SendVoiceBroadcastWxMsg(broadcast.BroadcastId, broadcast.SectionName, broadcast.BroadcastName)
  204. }()
  205. // 推送客群消息
  206. go func() {
  207. _ = SendVoiceBroadcastToThs(broadcast)
  208. }()
  209. return
  210. }
  211. // CreateVoiceBroadcast 新增语音播报
  212. func CreateVoiceBroadcast(sectionId, varietyId, authorId, userId int, broadcastName, sectionName, varietyName, author, voiceSeconds, voiceSize, voiceUrl, imgs string) (resp response.Broadcast, err error) {
  213. nowTime := time.Now().Local()
  214. item := &voice_broadcast.VoiceBroadcast{
  215. BroadcastName: broadcastName,
  216. SectionId: sectionId,
  217. SectionName: sectionName,
  218. VarietyId: varietyId,
  219. VarietyName: varietyName,
  220. AuthorId: authorId,
  221. Author: author,
  222. VoiceUrl: voiceUrl,
  223. VoicePlaySeconds: voiceSeconds,
  224. VoiceSize: voiceSize,
  225. CreateTime: nowTime.Format(utils.FormatDateTime),
  226. ModifyTime: nowTime.Format(utils.FormatDateTime),
  227. }
  228. // 图片
  229. imgList := make([]*voice_broadcast_img.YbVoiceBroadcastImg, 0)
  230. if imgs != "" {
  231. imgArr := strings.Split(imgs, ",")
  232. imgLen := len(imgArr)
  233. for i := 0; i < imgLen; i++ {
  234. if imgArr[i] == "" {
  235. continue
  236. }
  237. imgList = append(imgList, &voice_broadcast_img.YbVoiceBroadcastImg{
  238. BroadcastId: item.BroadcastId,
  239. ImgUrl: imgArr[i],
  240. CreateTime: nowTime,
  241. })
  242. }
  243. }
  244. if e := voice_broadcast.CreateVoiceBroadcastAndImgs(item, imgList); e != nil {
  245. err = errors.New("新增语音播报及图片失败, Err: " + e.Error())
  246. return
  247. }
  248. resp = handleBroadcastItem(userId, item, imgList)
  249. return
  250. }
  251. // EditVoiceBroadcast 编辑语音播报
  252. func EditVoiceBroadcast(broadcastId, sectionId, varietyId, authorId, userId int, broadcastName, sectionName, varietyName, author, voiceSeconds, voiceSize, voiceUrl, imgs string) (resp response.Broadcast, err error) {
  253. if broadcastId <= 0 {
  254. return
  255. }
  256. item, e := voice_broadcast.GetBroadcastById(broadcastId)
  257. if e != nil {
  258. err = errors.New("语音播报信息有误")
  259. return
  260. }
  261. nowTime := time.Now().Local()
  262. updateCols := []string{"BroadcastName", "SectionId", "SectionName", "VarietyId", "VarietyName", "AuthorId", "Author", "VoiceUrl",
  263. "VoicePlaySeconds", "VoiceSize", "ModifyTime"}
  264. item.BroadcastName = broadcastName
  265. item.SectionId = sectionId
  266. item.SectionName = sectionName
  267. item.VarietyId = varietyId
  268. item.VarietyName = varietyName
  269. item.AuthorId = authorId
  270. item.Author = author
  271. item.VoiceUrl = voiceUrl
  272. item.VoicePlaySeconds = voiceSeconds
  273. item.VoiceSize = voiceSize
  274. item.ModifyTime = nowTime.Format(utils.FormatDateTime)
  275. // 图片
  276. imgList := make([]*voice_broadcast_img.YbVoiceBroadcastImg, 0)
  277. if imgs != "" {
  278. imgArr := strings.Split(imgs, ",")
  279. imgLen := len(imgArr)
  280. for i := 0; i < imgLen; i++ {
  281. if imgArr[i] == "" {
  282. continue
  283. }
  284. imgList = append(imgList, &voice_broadcast_img.YbVoiceBroadcastImg{
  285. BroadcastId: item.BroadcastId,
  286. ImgUrl: imgArr[i],
  287. CreateTime: nowTime,
  288. })
  289. }
  290. }
  291. if e := voice_broadcast.UpdateVoiceBroadcastAndImgs(item, updateCols, imgList); e != nil {
  292. err = errors.New("更新语音播报及图片失败, Err: " + e.Error())
  293. return
  294. }
  295. resp = handleBroadcastItem(userId, item, imgList)
  296. return
  297. }
  298. // PublishVoiceBroadcast 发布语音播报
  299. func PublishVoiceBroadcast(broadcastId, publishType int, prePublishTime string) (err error) {
  300. item, e := voice_broadcast.GetBroadcastById(broadcastId)
  301. if e != nil {
  302. err = errors.New("语音播报信息有误")
  303. return
  304. }
  305. if item.PublishState == 1 {
  306. err = errors.New("不可重复发布")
  307. return
  308. }
  309. // publishType: 0-仅发布 1-发布并推送 2-定时发布; 发布类型为定时发送时, 分享图时间取预发布时间
  310. updateCols := []string{"ImgUrl", "PublishState", "PublishTime", "PrePublishTime", "ModifyTime"}
  311. publishTime := time.Now().Local()
  312. if publishType == 2 {
  313. item.PublishState = 0
  314. item.PrePublishTime = prePublishTime
  315. publishTime, e = time.ParseInLocation(utils.FormatDateTime, prePublishTime, time.Local)
  316. if e != nil {
  317. err = errors.New("预发布时间有误, Err: " + e.Error())
  318. return
  319. }
  320. } else {
  321. // 非定时发布时重置预发布时间
  322. item.PublishState = 1
  323. item.PublishTime = publishTime.Format(utils.FormatDateTime)
  324. item.PrePublishTime = ""
  325. }
  326. // 分享背景图-取板块的图
  327. section, e := voice_section.GetVoiceSectionById(item.SectionId)
  328. if e != nil {
  329. err = errors.New("获取板块信息失败, Err: " + e.Error())
  330. return
  331. }
  332. shareTime := publishTime.Format(utils.FormatDate)
  333. shareImg, e := createVoiceBroadcastShareImg(section.ImgUrl, item.SectionName, shareTime)
  334. if e != nil {
  335. err = errors.New("生成分享图失败, Err: " + e.Error())
  336. return
  337. }
  338. item.ImgUrl = shareImg
  339. // 发布
  340. if e = item.Update(updateCols); e != nil {
  341. err = errors.New("发布语音播报失败, Err: " + e.Error())
  342. return
  343. }
  344. return
  345. }
  346. // VoiceBroadcastShareImgPars 语音播报分享图参数
  347. type VoiceBroadcastShareImgPars struct {
  348. BackgroundImg string `json:"background_img"`
  349. Title string `json:"title"`
  350. CreateTime string `json:"create_time"`
  351. }
  352. // createVoiceBroadcastShareImg 生成动态分享图
  353. func createVoiceBroadcastShareImg(baseImg, sectionName, createTime string) (shareImg string, err error) {
  354. pars := VoiceBroadcastShareImgPars{
  355. BackgroundImg: baseImg,
  356. Title: sectionName,
  357. CreateTime: createTime,
  358. }
  359. parsByte, e := json.Marshal(pars)
  360. if e != nil {
  361. err = e
  362. return
  363. }
  364. shareImg, e = GetDynamicShareImg(VoiceBroadcastShareImgSource, string(parsByte))
  365. if e != nil {
  366. err = e
  367. return
  368. }
  369. return
  370. }
  371. // GetVoiceBroadcastDetail 获取语音播报详情
  372. func GetVoiceBroadcastDetail(broadcastId, userId int) (detail response.Broadcast, err error) {
  373. item, e := voice_broadcast.GetBroadcastById(broadcastId)
  374. if e != nil {
  375. err = errors.New("获取语音播报详情失败, Err: " + e.Error())
  376. return
  377. }
  378. // 语音播报图片
  379. imgList, e := voice_broadcast_img.GetVoiceImgListByVoiceId(broadcastId)
  380. if e != nil {
  381. err = errors.New("获取语音播报图片失败, Err: " + e.Error())
  382. return
  383. }
  384. detail = handleBroadcastItem(userId, item, imgList)
  385. return
  386. }
  387. // handleBroadcastItem 语音播报响应数据处理
  388. func handleBroadcastItem(userId int, item *voice_broadcast.VoiceBroadcast, imgs []*voice_broadcast_img.YbVoiceBroadcastImg) (resp response.Broadcast) {
  389. if item == nil {
  390. return
  391. }
  392. resp.BroadcastId = item.BroadcastId
  393. resp.BroadcastName = item.BroadcastName
  394. resp.SectionId = item.SectionId
  395. resp.SectionName = item.SectionName
  396. resp.VarietyId = item.VarietyId
  397. resp.VarietyName = item.VarietyName
  398. resp.AuthorId = item.AuthorId
  399. resp.Author = item.Author
  400. resp.ImgUrl = item.ImgUrl
  401. resp.VoiceUrl = item.VoiceUrl
  402. resp.VoicePlaySeconds = item.VoicePlaySeconds
  403. resp.VoiceSize = item.VoiceSize
  404. resp.CreateTime = item.CreateTime
  405. resp.ModifyTime = item.ModifyTime
  406. resp.PublishState = item.PublishState
  407. resp.PublishTime = item.PublishTime
  408. resp.PrePublishTime = item.PrePublishTime
  409. // 是否为作者、是否可推送消息
  410. if userId == item.AuthorId {
  411. resp.IsAuthor = true
  412. if item.MsgState == 0 {
  413. resp.CouldSendMsg = true
  414. }
  415. }
  416. imgLen := len(imgs)
  417. imgArr := make([]string, 0)
  418. for i := 0; i < imgLen; i++ {
  419. imgArr = append(imgArr, imgs[i].ImgUrl)
  420. }
  421. resp.Imgs = imgArr
  422. return
  423. }
  424. // GetMyVoiceBroadcastListCount 获取我的语音播报列表计数
  425. func GetMyVoiceBroadcastListCount(authorId, sectionId int) (resp response.BroadcastListStatusCount, err error) {
  426. condition := ` 1=1 `
  427. var pars []interface{}
  428. // 我的-非我的只能看到已发布
  429. if authorId > 0 {
  430. condition += ` AND author_id = ?`
  431. pars = append(pars, authorId)
  432. }
  433. // 板块
  434. if sectionId > 0 {
  435. condition += ` AND section_id = ?`
  436. pars = append(pars, sectionId)
  437. }
  438. counts, e := voice_broadcast.GetVoiceBroadcastListStatusCount(condition, pars)
  439. if e != nil {
  440. err = errors.New("获取语音播报列表计数失败, Err: " + e.Error())
  441. return
  442. }
  443. for _, v := range counts {
  444. if v.State == 0 {
  445. resp.Unpublished = v.Num
  446. continue
  447. }
  448. if v.State == 1 {
  449. resp.Published = v.Num
  450. }
  451. }
  452. resp.All = resp.Unpublished + resp.Published
  453. return
  454. }