noticeSetService.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package service
  2. import (
  3. "fmt"
  4. systemDao "iot_manager_service/app/system/dao"
  5. systemService "iot_manager_service/app/system/service"
  6. "iot_manager_service/app/warn/dao"
  7. "iot_manager_service/app/warn/model"
  8. "strconv"
  9. "strings"
  10. )
  11. var NoticeSetService = new(noticeSetService)
  12. type noticeSetService struct{}
  13. func (s noticeSetService) GetList(tenantId string, searchValue string) ([]model.NoticeSetRecords, error) {
  14. code := dao.ViewsAllCode{TenantId: tenantId}
  15. deviceTypeGroups, _ := code.GetGroupByDeviceType(searchValue)
  16. var deviceTypeGroupsNew []model.NoticeSetRecords
  17. noteiceSets := s.getNoteiceSets(tenantId)
  18. for _, group := range deviceTypeGroups {
  19. var deviceNew []model.NoticeSetRecords
  20. code.DeviceType = group.DeviceType
  21. devices, _ := code.GetDatasByDeviceType(searchValue)
  22. for _, device := range devices {
  23. key := tenantId + strconv.Itoa(3) + strconv.Itoa(device.DeviceType) + strconv.Itoa(device.DeviceId)
  24. set := noteiceSets[key]
  25. deviceNew = append(deviceNew, model.NoticeSetRecords{
  26. Rtype: 3,
  27. RtypeName: device.DeviceName + "(" + device.Sn + ")",
  28. DeviceType: device.DeviceType,
  29. DeviceId: device.DeviceId,
  30. ID: device.Sn,
  31. DevUserNoticeModesName: set.DevUserNoticeModesName,
  32. BusinessNoticeModesName: set.BusinessNoticeModesName,
  33. DevUserIdsName: set.DevUserIdsName,
  34. BusinessUserIdsName: set.BusinessUserIdsName,
  35. DevUserIds: set.DevUserIds,
  36. BusinessUserIds: set.BusinessUserIds,
  37. })
  38. }
  39. key := (tenantId) + strconv.Itoa(2) + strconv.Itoa(group.DeviceType) + strconv.Itoa(999999)
  40. set := noteiceSets[key]
  41. deviceTypeGroupsNew = append(deviceTypeGroupsNew, model.NoticeSetRecords{
  42. Rtype: 2,
  43. RtypeName: group.DeviceTypeName + "(" + strconv.Itoa(len(deviceNew)) + "台)",
  44. Children: deviceNew,
  45. DeviceType: group.DeviceType,
  46. DeviceId: 0,
  47. ID: strconv.Itoa(group.DeviceType),
  48. DevUserNoticeModesName: set.DevUserNoticeModesName,
  49. BusinessNoticeModesName: set.BusinessNoticeModesName,
  50. DevUserIdsName: set.DevUserIdsName,
  51. BusinessUserIdsName: set.BusinessUserIdsName,
  52. DevUserIds: set.DevUserIds,
  53. BusinessUserIds: set.BusinessUserIds,
  54. })
  55. }
  56. key := tenantId + strconv.Itoa(1) + strconv.Itoa(0) + strconv.Itoa(999999)
  57. set := noteiceSets[key]
  58. data := []model.NoticeSetRecords{
  59. {
  60. Rtype: 0,
  61. RtypeName: "全部设备",
  62. Children: deviceTypeGroupsNew,
  63. DeviceType: 0,
  64. DeviceId: 0,
  65. ID: "999999",
  66. DevUserNoticeModesName: set.DevUserNoticeModesName,
  67. BusinessNoticeModesName: set.BusinessNoticeModesName,
  68. DevUserIdsName: set.DevUserIdsName,
  69. BusinessUserIdsName: set.BusinessUserIdsName,
  70. DevUserIds: set.DevUserIds,
  71. BusinessUserIds: set.BusinessUserIds,
  72. },
  73. }
  74. return data, nil
  75. }
  76. func (s noticeSetService) Update(post model.NoticeUpdateData, tenantId string) error {
  77. rtype := post.Rtype
  78. if rtype == 0 {
  79. rtype = 1
  80. }
  81. deviceId := 999999
  82. if rtype == 3 {
  83. deviceId = post.DeviceID
  84. }
  85. set := dao.NoticeSet{
  86. TenantId: tenantId,
  87. DeviceType: post.DeviceType,
  88. DeviceId: deviceId,
  89. DeviceSn: post.ID,
  90. RType: rtype,
  91. DevUserNoticeModes: post.NoticeOperationType,
  92. BusinessNoticeModes: post.NoticeBusinessType,
  93. DevUserNoticeModesName: post.ShowOperationTypeName,
  94. BusinessNoticeModesName: post.ShowBusinessTypeName,
  95. DevUserIds: post.NoticeOperationMan,
  96. BusinessUserIds: post.NoticeBusinessMan,
  97. DevUserIdsName: s.getUserName(post.NoticeOperationMan),
  98. BusinessUserIdsName: s.getUserName(post.NoticeBusinessMan),
  99. }
  100. return set.UpdateOrSave()
  101. }
  102. func (s noticeSetService) getNoteiceSets(tenantId string) map[string]dao.NoticeSet {
  103. noticeSet := dao.NoticeSet{TenantId: tenantId}
  104. list, _ := noticeSet.GetList()
  105. news := make(map[string]dao.NoticeSet)
  106. for _, set := range list {
  107. key := set.TenantId + strconv.Itoa(set.RType) + strconv.Itoa(set.DeviceType) + strconv.Itoa(set.DeviceId)
  108. fmt.Printf("xxxkey = %v", key)
  109. set.BusinessUserIdsName = strings.ReplaceAll(set.BusinessUserIdsName, " | ", ",")
  110. set.DevUserIdsName = strings.ReplaceAll(set.DevUserIdsName, " | ", ",")
  111. news[key] = set
  112. }
  113. return news
  114. }
  115. func (s noticeSetService) GetById(id string) (*model.NoticeSetDetail, error) {
  116. noticeSet := dao.NoticeSet{DeviceSn: id}
  117. data, err := noticeSet.GetById()
  118. if err != nil {
  119. return nil, err
  120. }
  121. detail := &model.NoticeSetDetail{
  122. ID: data.DeviceSn,
  123. RidType: data.RType,
  124. NoticeOperationType: data.DevUserNoticeModes,
  125. NoticeBusinessType: data.BusinessNoticeModes,
  126. NoticeOperationMan: data.DevUserIds,
  127. NoticeBusinessMan: data.BusinessUserIds,
  128. }
  129. return detail, nil
  130. }
  131. func (s noticeSetService) UserList(account string, name string, current int, size int) ([]model.NoticeSetDUserList, error) {
  132. var list []model.NoticeSetDUserList
  133. userList, _, _ := systemService.UserService.List(account, name, current, size)
  134. for _, user := range userList {
  135. idStr := strconv.Itoa(int(user.ID))
  136. list = append(list, model.NoticeSetDUserList{ID: idStr, Name: user.Name})
  137. }
  138. return list, nil
  139. }
  140. func (s noticeSetService) getUserName(ids string) string {
  141. split := strings.Split(ids, ",")
  142. fm := make(map[string]int)
  143. for i, v := range split {
  144. fm[v] = i
  145. }
  146. //<<<<<<< HEAD
  147. userList, _, _ := systemService.UserService.List("", "", 0, 1000)
  148. //new := []string{}
  149. //=======
  150. //userList, _ := systemService.UserService.List("", "", 0, 1000)
  151. var nameList []string
  152. //>>>>>>> fdc014add9a1761732ad176375e06591f726e001
  153. for _, detail := range userList {
  154. id := strconv.Itoa(int(detail.ID))
  155. if _, ok := fm[id]; ok {
  156. nameList = append(nameList, detail.Name)
  157. }
  158. }
  159. return strings.Join(nameList, ",")
  160. }
  161. func (s noticeSetService) Remove(id string, tenantId string) error {
  162. noticeSet := dao.NoticeSet{DeviceSn: id, TenantId: tenantId}
  163. return noticeSet.Delete()
  164. }
  165. // GetReceiveList 查出接收人
  166. func (s noticeSetService) GetReceiveList(deviceType int, sn string, armSource int) (map[string]model.ReceiveList, error) {
  167. list, _ := dao.NoticeSet{}.GetNoticeList(deviceType, sn)
  168. receives := make(map[string]model.ReceiveList)
  169. for _, set := range list {
  170. userIds := set.BusinessUserIds //接收用户
  171. noticeModes := set.BusinessNoticeModes //接收的通知方式
  172. if armSource == 1 {
  173. userIds = set.DevUserIds
  174. noticeModes = set.DevUserNoticeModes
  175. }
  176. usersArr := strings.Split(userIds, ",")
  177. noticeModesArr := strings.Split(noticeModes, ",")
  178. for _, userId := range usersArr {
  179. var user systemDao.User
  180. parseInt, _ := strconv.ParseInt(userId, 10, 64)
  181. user.ID = parseInt
  182. user.GetUser()
  183. for _, noticeModeStr := range noticeModesArr {
  184. noticeModeNum, _ := strconv.ParseInt(noticeModeStr, 10, 64)
  185. account := user.Email
  186. if noticeModeStr == "1" {
  187. account = user.Phone
  188. }
  189. receives[userId+noticeModeStr] = model.ReceiveList{
  190. NoticeSetId: int(set.ID),
  191. UserName: user.Name,
  192. NoticeMode: int(noticeModeNum),
  193. NoticeReceiveAccount: account,
  194. }
  195. }
  196. }
  197. }
  198. return receives, nil
  199. }