voice_broadcast.go 14 KB

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