noticeSetService.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package service
  2. import (
  3. "fmt"
  4. systemService "iot_manager_service/app/system/service"
  5. "iot_manager_service/app/warn/dao"
  6. "iot_manager_service/app/warn/model"
  7. "strconv"
  8. "strings"
  9. )
  10. var NoticeSetService = new(noticeSetService)
  11. type noticeSetService struct{}
  12. func (s noticeSetService) GetList(tenantId int, searchValue string) ([]model.NoticeSetRecords, error) {
  13. code := dao.ViewsAllCode{TenantId: tenantId}
  14. deviceTypeGroups, _ := code.GetGroupByDeviceType(searchValue)
  15. var deviceTypeGroupsNew []model.NoticeSetRecords
  16. noteiceSets := s.getNoteiceSets(tenantId)
  17. for _, group := range deviceTypeGroups {
  18. var deviceNew []model.NoticeSetRecords
  19. code.DeviceType = group.DeviceType
  20. devices, _ := code.GetDatasByDeviceType(searchValue)
  21. for _, device := range devices {
  22. key := strconv.Itoa(tenantId) + strconv.Itoa(3) + strconv.Itoa(device.DeviceType) + strconv.Itoa(device.DeviceId)
  23. set := noteiceSets[key]
  24. deviceNew = append(deviceNew, model.NoticeSetRecords{
  25. Rtype: 3,
  26. RtypeName: device.DeviceName + "(" + device.Sn + ")",
  27. DeviceType: device.DeviceType,
  28. DeviceId: device.DeviceId,
  29. ID: device.Sn,
  30. DevUserNoticeModesName: set.DevUserNoticeModesName,
  31. BusinessNoticeModesName: set.BusinessNoticeModesName,
  32. DevUserIdsName: set.DevUserIdsName,
  33. BusinessUserIdsName: set.BusinessUserIdsName,
  34. DevUserIds: set.DevUserIds,
  35. BusinessUserIds: set.BusinessUserIds,
  36. })
  37. }
  38. key := strconv.Itoa(tenantId) + strconv.Itoa(2) + strconv.Itoa(group.DeviceType) + strconv.Itoa(999999)
  39. set := noteiceSets[key]
  40. deviceTypeGroupsNew = append(deviceTypeGroupsNew, model.NoticeSetRecords{
  41. Rtype: 2,
  42. RtypeName: group.DeviceTypeName + "(" + strconv.Itoa(len(deviceNew)) + "台)",
  43. Children: deviceNew,
  44. DeviceType: group.DeviceType,
  45. DeviceId: 0,
  46. ID: strconv.Itoa(group.DeviceType),
  47. DevUserNoticeModesName: set.DevUserNoticeModesName,
  48. BusinessNoticeModesName: set.BusinessNoticeModesName,
  49. DevUserIdsName: set.DevUserIdsName,
  50. BusinessUserIdsName: set.BusinessUserIdsName,
  51. DevUserIds: set.DevUserIds,
  52. BusinessUserIds: set.BusinessUserIds,
  53. })
  54. }
  55. key := strconv.Itoa(tenantId) + strconv.Itoa(1) + strconv.Itoa(0) + strconv.Itoa(999999)
  56. set := noteiceSets[key]
  57. data := []model.NoticeSetRecords{
  58. {
  59. Rtype: 0,
  60. RtypeName: "全部设备",
  61. Children: deviceTypeGroupsNew,
  62. DeviceType: 0,
  63. DeviceId: 0,
  64. ID: "999999",
  65. DevUserNoticeModesName: set.DevUserNoticeModesName,
  66. BusinessNoticeModesName: set.BusinessNoticeModesName,
  67. DevUserIdsName: set.DevUserIdsName,
  68. BusinessUserIdsName: set.BusinessUserIdsName,
  69. DevUserIds: set.DevUserIds,
  70. BusinessUserIds: set.BusinessUserIds,
  71. },
  72. }
  73. return data, nil
  74. }
  75. func (s noticeSetService) Update(post model.NoticeUpdateData, tenantId int) error {
  76. rtype := post.Rtype
  77. if rtype == 0 {
  78. rtype = 1
  79. }
  80. deviceId := 999999
  81. if rtype == 3 {
  82. deviceId = post.DeviceID
  83. }
  84. set := dao.NoticeSet{
  85. TenantId: strconv.Itoa(tenantId),
  86. DeviceType: post.DeviceType,
  87. DeviceId: deviceId,
  88. DeviceSn: post.ID,
  89. RType: rtype,
  90. DevUserNoticeModes: post.NoticeOperationType,
  91. BusinessNoticeModes: post.NoticeBusinessType,
  92. DevUserNoticeModesName: post.ShowOperationTypeName,
  93. BusinessNoticeModesName: post.ShowBusinessTypeName,
  94. DevUserIds: post.NoticeOperationMan,
  95. BusinessUserIds: post.NoticeBusinessMan,
  96. DevUserIdsName: s.getUserName(post.NoticeOperationMan),
  97. BusinessUserIdsName: s.getUserName(post.NoticeBusinessMan),
  98. }
  99. return set.UpdateOrSave()
  100. }
  101. func (s noticeSetService) getNoteiceSets(tenantId int) map[string]dao.NoticeSet {
  102. noticeSet := dao.NoticeSet{TenantId: strconv.Itoa(tenantId)}
  103. list, _ := noticeSet.GetList()
  104. news := make(map[string]dao.NoticeSet)
  105. for _, set := range list {
  106. key := set.TenantId + strconv.Itoa(set.RType) + strconv.Itoa(set.DeviceType) + strconv.Itoa(set.DeviceId)
  107. fmt.Printf("xxxkey = %v", key)
  108. set.BusinessUserIdsName = strings.ReplaceAll(set.BusinessUserIdsName, " | ", ",")
  109. set.DevUserIdsName = strings.ReplaceAll(set.DevUserIdsName, " | ", ",")
  110. news[key] = set
  111. }
  112. return news
  113. }
  114. func (s noticeSetService) GetById(id string) (*model.NoticeSetDetail, error) {
  115. noticeSet := dao.NoticeSet{DeviceSn: id}
  116. data, err := noticeSet.GetById()
  117. if err != nil {
  118. return nil, err
  119. }
  120. detail := &model.NoticeSetDetail{
  121. ID: data.DeviceSn,
  122. RidType: data.RType,
  123. NoticeOperationType: data.DevUserNoticeModes,
  124. NoticeBusinessType: data.BusinessNoticeModes,
  125. NoticeOperationMan: data.DevUserIds,
  126. NoticeBusinessMan: data.BusinessUserIds,
  127. }
  128. return detail, nil
  129. }
  130. func (s noticeSetService) UserList(account string, name string, current int, size int) ([]model.NoticeSetDUserList, error) {
  131. var list []model.NoticeSetDUserList
  132. userList, _ := systemService.UserService.List(account, name, current, size)
  133. for _, user := range userList {
  134. idStr := strconv.Itoa(int(user.ID))
  135. list = append(list, model.NoticeSetDUserList{ID: idStr, Name: user.Name})
  136. }
  137. return list, nil
  138. }
  139. func (s noticeSetService) getUserName(ids string) string {
  140. split := strings.Split(ids, ",")
  141. fm := make(map[string]int)
  142. for i, v := range split {
  143. fm[v] = i
  144. }
  145. userList, _ := systemService.UserService.List("", "", 0, 1000)
  146. new := []string{}
  147. for _, detail := range userList {
  148. id := strconv.Itoa(int(detail.ID))
  149. if _, ok := fm[id]; ok {
  150. new = append(new, detail.Name)
  151. }
  152. }
  153. return strings.Join(new, ",")
  154. }
  155. func (s noticeSetService) Remove(id string, tenant_id int) error {
  156. fmt.Printf("id = %v", id)
  157. noticeSet := dao.NoticeSet{DeviceSn: id, TenantId: strconv.Itoa(tenant_id)}
  158. return noticeSet.Delete()
  159. }