noticeSetService.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package service
  2. import (
  3. systemDao "iot_manager_service/app/system/dao"
  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 string, 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 := 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 := (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 := 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 string) 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: 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 string) map[string]dao.NoticeSet {
  102. noticeSet := dao.NoticeSet{TenantId: 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. set.BusinessUserIdsName = strings.ReplaceAll(set.BusinessUserIdsName, " | ", ",")
  108. set.DevUserIdsName = strings.ReplaceAll(set.DevUserIdsName, " | ", ",")
  109. news[key] = set
  110. }
  111. return news
  112. }
  113. func (s noticeSetService) GetById(id string) (*model.NoticeSetDetail, error) {
  114. noticeSet := dao.NoticeSet{DeviceSn: id}
  115. data, err := noticeSet.GetById()
  116. if err != nil {
  117. return nil, err
  118. }
  119. detail := &model.NoticeSetDetail{
  120. ID: data.DeviceSn,
  121. RidType: data.RType,
  122. NoticeOperationType: data.DevUserNoticeModes,
  123. NoticeBusinessType: data.BusinessNoticeModes,
  124. NoticeOperationMan: data.DevUserIds,
  125. NoticeBusinessMan: data.BusinessUserIds,
  126. }
  127. return detail, nil
  128. }
  129. func (s noticeSetService) UserList(account string, name string, current int, size int) ([]model.NoticeSetDUserList, error) {
  130. var list []model.NoticeSetDUserList
  131. userList, _, _ := systemService.UserService.List(account, name, current, size)
  132. for _, user := range userList {
  133. idStr := strconv.Itoa(int(user.ID))
  134. list = append(list, model.NoticeSetDUserList{ID: idStr, Name: user.Name})
  135. }
  136. return list, nil
  137. }
  138. func (s noticeSetService) getUserName(ids string) string {
  139. split := strings.Split(ids, ",")
  140. fm := make(map[string]int)
  141. for i, v := range split {
  142. fm[v] = i
  143. }
  144. //<<<<<<< HEAD
  145. userList, _, _ := systemService.UserService.List("", "", 0, 1000)
  146. //new := []string{}
  147. //=======
  148. //userList, _ := systemService.UserService.List("", "", 0, 1000)
  149. var nameList []string
  150. //>>>>>>> fdc014add9a1761732ad176375e06591f726e001
  151. for _, detail := range userList {
  152. id := strconv.Itoa(int(detail.ID))
  153. if _, ok := fm[id]; ok {
  154. nameList = append(nameList, detail.Name)
  155. }
  156. }
  157. return strings.Join(nameList, ",")
  158. }
  159. func (s noticeSetService) Remove(id string, tenantId string) error {
  160. noticeSet := dao.NoticeSet{DeviceSn: id, TenantId: tenantId}
  161. return noticeSet.Delete()
  162. }
  163. // GetReceiveList 查出接收人
  164. func (s noticeSetService) GetReceiveList(deviceType int, sn string, armSource int) (map[string]model.ReceiveList, error) {
  165. list, _ := dao.NoticeSet{}.GetNoticeList(deviceType, sn)
  166. receives := make(map[string]model.ReceiveList)
  167. for _, set := range list {
  168. userIds := set.BusinessUserIds //接收用户
  169. noticeModes := set.BusinessNoticeModes //接收的通知方式
  170. if armSource == 1 {
  171. userIds = set.DevUserIds
  172. noticeModes = set.DevUserNoticeModes
  173. }
  174. usersArr := strings.Split(userIds, ",")
  175. noticeModesArr := strings.Split(noticeModes, ",")
  176. for _, userId := range usersArr {
  177. var user systemDao.User
  178. parseInt, _ := strconv.Atoi(userId)
  179. user.ID = parseInt
  180. user.GetUser()
  181. for _, noticeModeStr := range noticeModesArr {
  182. noticeModeNum, _ := strconv.ParseInt(noticeModeStr, 10, 64)
  183. account := user.Email
  184. if noticeModeStr == "1" {
  185. account = user.Phone
  186. }
  187. receives[userId+noticeModeStr] = model.ReceiveList{
  188. NoticeSetId: int(set.ID),
  189. UserName: user.Name,
  190. NoticeMode: int(noticeModeNum),
  191. NoticeReceiveAccount: account,
  192. }
  193. }
  194. }
  195. }
  196. return receives, nil
  197. }