publishLibrariesService.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "gorm.io/gorm"
  7. "iot_manager_service/app/multimedia/dao"
  8. "iot_manager_service/app/multimedia/model"
  9. "iot_manager_service/app/system/service"
  10. "iot_manager_service/util/common"
  11. "iot_manager_service/util/logger"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. var PublishLibrariesService = new(publishLibrariesService)
  17. type publishLibrariesService struct{}
  18. /**
  19. TODO: 此接口都未对接边缘端
  20. */
  21. func (s *publishLibrariesService) List(tenantId string, searchValue string, current, size int, sysType int) ([]dao.PublishLibraries, int64,
  22. *common.Errors) {
  23. program := &dao.PublishLibraries{
  24. TenantId: tenantId,
  25. SysType: sysType,
  26. }
  27. offset := (current - 1) * size
  28. limit := size
  29. if searchValue != "" {
  30. program.Name = searchValue
  31. }
  32. list, total, err := program.GetList(offset, limit)
  33. if err != nil {
  34. return nil, 0, common.FailResponse(err.Error(), nil)
  35. }
  36. return list, total, nil
  37. }
  38. // Audit 素材发布
  39. func (s *publishLibrariesService) Audit(req model.ReqProgramAudit) error {
  40. if !common.MultimediaEfficacyTime(req.StartTime, req.EndTime, req.KsTime) {
  41. panic("任务时间已过,请重新定义时间。")
  42. }
  43. program := dao.Program{ID: req.ProgramLibrariesId}
  44. err := program.Get()
  45. if err != nil {
  46. return err
  47. }
  48. PublishLibrariesService.Create(&program, req)
  49. return nil
  50. }
  51. // SaveOrUpdate 保存
  52. func (s *publishLibrariesService) Create(program *dao.Program, req model.ReqProgramAudit) *common.Errors {
  53. remarks := "多媒体系统"
  54. if program.SysType == 1 {
  55. remarks = "广播系统"
  56. }
  57. infoIds := req.InfoId
  58. infoIds = strings.Trim(infoIds, ",")
  59. infoIds = strings.ReplaceAll(infoIds, "-1", "")
  60. infoIds = strings.ReplaceAll(infoIds, ",-1", "") //新id
  61. libraries := &dao.PublishLibraries{
  62. Name: program.Name,
  63. Resolution: program.Resolution,
  64. Duration: program.Duration,
  65. FileSize: program.FileSize,
  66. InfoId: infoIds,
  67. ImgDuration: program.ImgDuration,
  68. StartTime: req.StartTime,
  69. EndTime: req.EndTime,
  70. NotificationType: "0",
  71. CreateTime: time.Now(),
  72. CreateUser: program.CreateUser,
  73. CreateName: "",
  74. UpdateTime: time.Now(),
  75. UpdateUser: "",
  76. IsDeleted: 0,
  77. Status: 1,
  78. TenantId: program.TenantId,
  79. Remarks: remarks,
  80. SysType: program.SysType,
  81. KsTime: req.KsTime,
  82. JsTime: req.JsTime,
  83. FileId: program.ID,
  84. PlayTime: req.PlayTime,
  85. PlayInterval: req.PlayInterval,
  86. }
  87. libraries.CreateTime = time.Now()
  88. if err := libraries.Create(); err != nil {
  89. logger.Logger.Errorf("Create err = %s \n", err.Error())
  90. return common.FailResponse(err.Error(), nil)
  91. }
  92. //TODO: 日志保存
  93. return common.SuccessResponse(common.Succeeded, nil)
  94. }
  95. // GetProgram 取节目
  96. func (s *publishLibrariesService) GetProgram(id int) (int, error) {
  97. libraries := dao.PublishLibraries{ID: int64(id)}
  98. program, err := libraries.GetProgram()
  99. return program, err
  100. }
  101. func (s *publishLibrariesService) putPublishLibraries(programId int, publishLibraries dao.PublishLibraries) {
  102. }
  103. // UpdateStatus 状态变更
  104. func (s *publishLibrariesService) UpdateStatus(tenantId string, id int, status int, name string) (bool, error) {
  105. update := &dao.PublishLibraries{
  106. TenantId: tenantId,
  107. ID: int64(id),
  108. Status: status,
  109. Name: name,
  110. }
  111. err := update.Update()
  112. if err != nil {
  113. return false, err
  114. }
  115. return true, nil
  116. }
  117. func (s *publishLibrariesService) Remove(userId int64, tenantId string, id int) *common.Errors {
  118. // 创建查询实例
  119. device := &dao.PublishLibraries{
  120. ID: int64(id),
  121. IsDeleted: 1,
  122. UpdateUser: strconv.Itoa(int(userId)),
  123. UpdateTime: time.Now(),
  124. }
  125. err := device.Delete()
  126. if err != nil {
  127. return common.FailResponse(err.Error(), nil)
  128. }
  129. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.DeviceTypeInfoBoard,
  130. common.DeviceTypeBridge, common.GetDeviceObject(int(device.ID), device.Name), common.OperationSuccess)
  131. return nil
  132. }
  133. type CltLedLibraries struct {
  134. Type string `json:"type"` //文件类型 2:图片3:视频5:文本27:网页
  135. Name string `json:"name"` //节目名字
  136. Imageinfo Imageinfo `json:"imageinfo"` //图片或视频信息
  137. }
  138. type InEffect struct {
  139. Type string `json:"Type"` //翻页类型 2
  140. Time string `json:"Time"` ///翻页花费时间 1000为1秒
  141. }
  142. type Imageinfo struct {
  143. Urls []string `json:"urls"`
  144. Duration string `json:"duration"`
  145. InEffect InEffect `json:"inEffect"`
  146. }
  147. // LedPaying 组装当前led屏要播放的节目json
  148. func (s *publishLibrariesService) LedPaying(ledId int, playing string) string {
  149. libraries := dao.PublishLibraries{}
  150. librarie, err := libraries.GetProgramByLedId(ledId)
  151. if errors.Is(err, gorm.ErrRecordNotFound) {
  152. return ""
  153. }
  154. if err != nil {
  155. logger.Logger.Errorf("LedPaying err = %v \n", err)
  156. return ""
  157. }
  158. if librarie.ID < 1 {
  159. //没有新节目不更新
  160. return ""
  161. }
  162. program, _ := s.GetProgram(int(librarie.ID))
  163. relation := &dao.ProgramRelation{
  164. ProgramId: program,
  165. TenantId: librarie.TenantId,
  166. }
  167. relations, err := relation.GetByProgram()
  168. if errors.Is(err, gorm.ErrRecordNotFound) {
  169. return ""
  170. }
  171. if err != nil {
  172. logger.Logger.Errorf("LedPaying err = %s \n", err.Error())
  173. return ""
  174. }
  175. var libraryIds []int
  176. for _, relation := range relations {
  177. libraryIds = append(libraryIds, relation.LibraryId)
  178. }
  179. library := &dao.Library{TenantId: librarie.TenantId}
  180. getLibrariesByIds, _ := library.GetLibrariesByIds(libraryIds)
  181. if len(getLibrariesByIds) < 1 {
  182. logger.Logger.Errorf("无素材")
  183. return ""
  184. }
  185. var cltLedLibraries CltLedLibraries
  186. cltLedLibraries.Name = fmt.Sprintf("%v-%v", librarie.Name, librarie.ID)
  187. if strings.EqualFold(cltLedLibraries.Name, strings.ReplaceAll(playing, ".vsn", "")) {
  188. logger.Logger.Errorf("节目相同无需要更新")
  189. return ""
  190. }
  191. cltLedLibraries.Type = "2"
  192. if strings.Contains(getLibrariesByIds[0].MaterialAddress, "mp4") {
  193. cltLedLibraries.Type = "3"
  194. }
  195. var urls []string
  196. for _, getLibrariesById := range getLibrariesByIds {
  197. urls = append(urls, getLibrariesById.MaterialAddress)
  198. }
  199. cltLedLibraries.Imageinfo.Duration = strconv.Itoa(int(librarie.Duration))
  200. cltLedLibraries.Imageinfo.Urls = urls
  201. cltLedLibraries.Imageinfo.InEffect.Type = "2"
  202. //cltLedLibraries.Imageinfo.InEffect.Time = strconv.Itoa(relations[0].Duration)
  203. cltLedLibraries.Imageinfo.InEffect.Time = "1500"
  204. marshal, err := json.Marshal(cltLedLibraries)
  205. if err != nil {
  206. return ""
  207. }
  208. str := string(marshal)
  209. return str
  210. }
  211. type iPCastPayingMp3Info struct {
  212. Type int `json:"Type"`
  213. PlayTimes int `json:"PlayTimes"`
  214. FileIds []int `json:"FileIds"`
  215. TermIds []int `json:"TermIds"`
  216. }
  217. type iPCastPayingContentInfo struct {
  218. Content string `json:"Content"`
  219. Playtime int `json:"Playtime"`
  220. TermIds []int `json:"TermIds"`
  221. PlayVol int `json:"PlayVol"`
  222. PromptTone int `json:"PromptTone"`
  223. }
  224. func (s *publishLibrariesService) IPCastPaying(id int) (string, int) {
  225. publishLibraries := dao.PublishLibraries{}
  226. libraries, err := publishLibraries.GetProgramByIpCastId(id)
  227. if err != nil {
  228. logger.Logger.Errorf("IPCastPaying err = %v \n", err)
  229. return "", 0
  230. }
  231. if len(libraries) < 1 {
  232. //没有新节目不更新
  233. return "", 0
  234. }
  235. var librarie dao.PublishLibraries
  236. //判断当前到底要放哪个节目
  237. for _, library := range libraries {
  238. library.Ptype = 1
  239. if library.JsTime == "" {
  240. library.Ptype = 0
  241. }
  242. if library.Ptype == 1 && library.JsTime != "" {
  243. currTime := time.Now()
  244. parse, err := time.Parse("2006-01-02 15:04", fmt.Sprintf("%v %v", time.Now().Format("2006-01-02"), library.JsTime))
  245. if err != nil {
  246. logger.Logger.Errorf("IPCastPaying parse err = %v \n", err)
  247. continue
  248. }
  249. if parse.Before(currTime) {
  250. continue
  251. }
  252. } else {
  253. cTime := time.Now()
  254. kTime, err := time.Parse("2006-01-02 15:04", fmt.Sprintf("%v %v", time.Now().Format("2006-01-02"), library.KsTime))
  255. if err != nil {
  256. logger.Logger.Errorf("IPCastPaying kTime err = %v \n", err)
  257. }
  258. //这里要作限制 ,只有是当前时分 才操作
  259. if cTime.Minute() != kTime.Minute() || cTime.Hour() == kTime.Hour() {
  260. continue
  261. }
  262. }
  263. librarie = library
  264. }
  265. program, _ := s.GetProgram(int(librarie.ID))
  266. relation := &dao.ProgramRelation{
  267. ProgramId: program,
  268. TenantId: librarie.TenantId,
  269. }
  270. relations, err := relation.GetByProgram()
  271. if err != nil {
  272. logger.Logger.Errorf("IPCastPaying err = %s \n", err)
  273. return "", 0
  274. }
  275. var libraryIds []int
  276. for _, relation := range relations {
  277. libraryIds = append(libraryIds, relation.LibraryId)
  278. }
  279. library := &dao.Library{TenantId: librarie.TenantId}
  280. getLibrariesByIds, _ := library.GetLibrariesByIds(libraryIds)
  281. if len(getLibrariesByIds) < 1 {
  282. logger.Logger.Errorf("无素材")
  283. return "", 0
  284. }
  285. // 音频播放
  286. if librarie.Ptype == 1 {
  287. paytime := diffTime(librarie.KsTime, librarie.JsTime)
  288. if paytime < 0 {
  289. //logger.Logger.Errorf("过了播放结束时间啦 ")
  290. return "", 0
  291. }
  292. fileIds := []int{}
  293. for _, libs := range getLibrariesByIds {
  294. fid, _ := strconv.Atoi(libs.FileId)
  295. fileIds = append(fileIds, fid)
  296. }
  297. info := iPCastPayingMp3Info{
  298. Type: 1,
  299. PlayTimes: paytime,
  300. FileIds: fileIds,
  301. TermIds: []int{1, 2, 3, 4, 5, 6, 7, 8},
  302. }
  303. marshal, err := json.Marshal(info)
  304. if err != nil {
  305. logger.Logger.Errorf("IPCastPaying iPCastPayingMp3Info err = %v \n", err)
  306. return "", 0
  307. }
  308. return string(marshal), librarie.Ptype
  309. //文字播放
  310. } else {
  311. contentInfo := iPCastPayingContentInfo{
  312. Content: getLibrariesByIds[0].MaterialAddress,
  313. Playtime: librarie.PlayTime,
  314. TermIds: []int{1, 2, 3, 4, 5, 6, 7, 8},
  315. PlayVol: 50,
  316. PromptTone: librarie.PlayInterval,
  317. }
  318. marshal, err := json.Marshal(contentInfo)
  319. if err != nil {
  320. logger.Logger.Errorf("IPCastPaying contentInfo err = %v \n", err)
  321. return "", 0
  322. }
  323. return string(marshal), librarie.Ptype
  324. }
  325. }
  326. // 计算还有多久结束
  327. func diffTime(stime, etime string) int {
  328. //stimeT, err := time.Parse("2006-01-02 15:04", fmt.Sprintf("%v %v", time.Now().Format("2006-01-02"), stime))
  329. stimeT, err := time.Parse("2006-01-02 15:04", fmt.Sprintf("%v", time.Now().Format("2006-01-02 15:04")))
  330. if err != nil {
  331. return -1
  332. }
  333. etimeT, err2 := time.Parse("2006-01-02 15:04", fmt.Sprintf("%v %v", time.Now().Format("2006-01-02"), etime))
  334. if err2 != nil {
  335. return -1
  336. }
  337. diff := int(etimeT.Sub(stimeT).Seconds())
  338. return diff
  339. }