programService.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package service
  2. import (
  3. "iot_manager_service/app/multimedia/dao"
  4. "iot_manager_service/app/multimedia/model"
  5. "iot_manager_service/app/system/service"
  6. "iot_manager_service/util/common"
  7. "iot_manager_service/util/logger"
  8. "time"
  9. )
  10. var ProgramService = new(programService)
  11. type programService struct{}
  12. func (s *programService) Get(id int) (*dao.Program, *common.Errors) {
  13. Program := &dao.Program{
  14. ID: id,
  15. }
  16. err := Program.Get()
  17. if err != nil {
  18. return nil, common.FailResponse(err.Error(), nil)
  19. }
  20. return Program, nil
  21. }
  22. func (s *programService) List(tenantId int, searchValue string, current, size int) ([]model.ProgramDetail,
  23. *common.Errors) {
  24. Program := &dao.Program{
  25. TenantId: tenantId,
  26. }
  27. offset := (current - 1) * size
  28. limit := size
  29. if searchValue != "" {
  30. Program.Name = searchValue
  31. }
  32. programs, err := Program.GetPrograms(offset, limit)
  33. if err != nil {
  34. return nil, common.FailResponse(err.Error(), nil)
  35. }
  36. var rsp []model.ProgramDetail
  37. for _, pro := range programs {
  38. rsp = append(rsp, model.ProgramDetail{
  39. Program: pro,
  40. ResolutionName: service.DictService.GetResolutionName(tenantId, pro.Resolution),
  41. })
  42. }
  43. return rsp, nil
  44. }
  45. func (s *programService) Remove(userId int64, tenantId int, id int) *common.Errors {
  46. relation := &dao.ProgramRelation{
  47. ProgramId: id,
  48. TenantId: tenantId,
  49. IsDeleted: 1,
  50. }
  51. err := relation.Delete()
  52. if err != nil {
  53. return common.FailResponse(err.Error(), nil)
  54. }
  55. program := &dao.Program{
  56. ID: id,
  57. IsDeleted: 1,
  58. UpdateUser: userId,
  59. UpdateTime: time.Now(),
  60. }
  61. err = program.Delete()
  62. if err != nil {
  63. return common.FailResponse(err.Error(), nil)
  64. }
  65. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeInfoBar,
  66. common.DeviceTypeDefault, "", common.OperationSuccess)
  67. return nil
  68. }
  69. func (s *programService) GetList(tenantId int, name string) ([]dao.Program, *common.Errors) {
  70. Program := &dao.Program{
  71. TenantId: tenantId,
  72. Name: name,
  73. }
  74. libraries, err := Program.GetAll()
  75. if err != nil {
  76. return nil, common.FailResponse(err.Error(), nil)
  77. }
  78. return libraries, nil
  79. }
  80. func (s *programService) Submit(tenantId int, userId int64, req model.ReqProgramSubmit) *common.Errors {
  81. program := &dao.Program{
  82. Name: req.Name,
  83. Resolution: req.Resolution,
  84. Duration: req.Duration,
  85. FileSize: req.FileSize,
  86. ImgDuration: req.ImgDuration,
  87. Remarks: req.Remarks,
  88. TenantId: tenantId,
  89. CreateTime: time.Now(),
  90. CreateUser: userId,
  91. UpdateTime: time.Now(),
  92. UpdateUser: userId,
  93. IsDeleted: 0,
  94. }
  95. if err := program.Create(); err != nil {
  96. logger.Logger.Errorf("Create err = %s \n", err.Error())
  97. return common.FailResponse(err.Error(), nil)
  98. }
  99. libraryIds := common.StringToIntArray(req.LibraryIds)
  100. for index, libraryId := range libraryIds {
  101. relation := &dao.ProgramRelation{
  102. ProgramId: program.ID,
  103. LibraryId: libraryId,
  104. OrderNo: index + 1,
  105. TenantId: tenantId,
  106. IsDeleted: 0,
  107. }
  108. err := relation.Save()
  109. if err != nil {
  110. logger.Logger.Errorf("relation save err = %s \n", err.Error())
  111. _ = program.Remove()
  112. _ = relation.Remove()
  113. return common.FailResponse(err.Error(), nil)
  114. }
  115. }
  116. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeInfoBar,
  117. common.DeviceTypeDefault, program.Name, common.OperationSuccess)
  118. return common.SuccessResponse(common.Succeeded, nil)
  119. }
  120. func (s *programService) GetLibraryList(tenantId, programId int) ([]dao.Library, *common.Errors) {
  121. relation := &dao.ProgramRelation{
  122. ProgramId: programId,
  123. TenantId: tenantId,
  124. }
  125. relations, err := relation.GetByProgram()
  126. if err != nil {
  127. logger.Logger.Errorf("relation BatchCreate err = %s \n", err.Error())
  128. return nil, common.FailResponse(err.Error(), nil)
  129. }
  130. var libraryIds []int
  131. for _, relation := range relations {
  132. libraryIds = append(libraryIds, relation.LibraryId)
  133. }
  134. library := &dao.Library{TenantId: tenantId}
  135. libraries, err := library.GetLibrariesByIds(libraryIds)
  136. if err != nil {
  137. logger.Logger.Errorf("GetLibrariesByIds err = %s \n", err.Error())
  138. return nil, common.FailResponse(err.Error(), nil)
  139. }
  140. return libraries, nil
  141. }