manholeCoverService.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package service
  2. import (
  3. "iot_manager_service/app/device/dao"
  4. "iot_manager_service/app/system/service"
  5. "iot_manager_service/util/common"
  6. "iot_manager_service/util/logger"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. var ManholeCoverService = new(manholeCoverService)
  12. type manholeCoverService struct{}
  13. func (s *manholeCoverService) Get(id int) (*dao.ManholeCover, *common.Errors) {
  14. // 创建查询实例
  15. device := &dao.ManholeCover{
  16. ID: id,
  17. }
  18. err := device.GetDevice()
  19. if err != nil {
  20. return nil, common.FailResponse(err.Error(), nil)
  21. }
  22. return device, nil
  23. }
  24. func (s *manholeCoverService) CreateOrUpdate(userId int, tenantId string, req dao.ManholeCover) *common.Errors {
  25. // 创建查询实例
  26. device := req
  27. device.TenantId = tenantId
  28. device.UpdateUser = userId
  29. device.UpdateTime = time.Now()
  30. if req.ID == 0 {
  31. device.CreateTime = time.Now()
  32. device.CreateUser = userId
  33. if device.IsExistedBySN() {
  34. logger.Logger.Errorf("Create GetDeviceID err \n")
  35. return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  36. }
  37. if err := device.Create(); err != nil {
  38. logger.Logger.Errorf("Create err = %s \n", err.Error())
  39. return common.FailResponse(err.Error(), nil)
  40. }
  41. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  42. common.DeviceTypeManholeCover, common.GetDeviceObject(device.ID, device.ManholeName), common.OperationSuccess)
  43. return common.SuccessResponse(common.Succeeded, nil)
  44. }
  45. if device.IsExistedByNameAndCode() {
  46. return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  47. }
  48. if err := device.Update(); err != nil {
  49. logger.Logger.Errorf("Update err = %s \n", err.Error())
  50. return common.FailResponse(err.Error(), nil)
  51. }
  52. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  53. common.DeviceTypeManholeCover, common.GetDeviceObject(device.ID, device.ManholeName), common.OperationSuccess)
  54. return common.SuccessResponse(common.Succeeded, nil)
  55. }
  56. func (s *manholeCoverService) List(searchValue string, current, size int) ([]dao.ManholeCover, int64, *common.Errors) {
  57. device := dao.ManholeCover{}
  58. if searchValue != "" {
  59. device.ManholeSn = searchValue
  60. device.ManholeName = searchValue
  61. }
  62. offset := (current - 1) * size
  63. limit := size
  64. devices, total, err := device.GetDevices(offset, limit)
  65. if err != nil {
  66. return nil, 0, common.FailResponse(err.Error(), nil)
  67. }
  68. return devices, total, nil
  69. }
  70. func (s *manholeCoverService) Remove(userId int, tenantId string, id int) *common.Errors {
  71. // 创建查询实例
  72. device := &dao.ManholeCover{
  73. ID: id,
  74. IsDeleted: 1,
  75. UpdateUser: userId,
  76. UpdateTime: time.Now(),
  77. }
  78. err := device.Delete()
  79. if err != nil {
  80. return common.FailResponse(err.Error(), nil)
  81. }
  82. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  83. common.DeviceTypeManholeCover, common.GetDeviceObject(device.ID, device.ManholeName), common.OperationSuccess)
  84. return nil
  85. }
  86. func (s *manholeCoverService) GetList(tenantId string) ([]*dao.ManholeCover, *common.Errors) {
  87. device := &dao.ManholeCover{
  88. TenantId: tenantId,
  89. IsDeleted: 0,
  90. }
  91. devices, err := device.GetAllDevices()
  92. if err != nil {
  93. return nil, common.FailResponse(err.Error(), nil)
  94. }
  95. return devices, nil
  96. }
  97. func (s *manholeCoverService) Setting(tenantId string, id int, threshold string) error {
  98. split := strings.Split(threshold, ",")
  99. leanAngle, _ := strconv.Atoi(split[0])
  100. batteryAlarm, _ := strconv.ParseFloat(split[1], 64)
  101. minTemp, _ := strconv.ParseFloat(split[2], 64)
  102. maxTemp, _ := strconv.ParseFloat(split[3], 64)
  103. device := &dao.ManholeCover{
  104. TenantId: tenantId,
  105. ID: id,
  106. LeanAngle: leanAngle,
  107. BatteryAlarm: batteryAlarm,
  108. MinTemp: minTemp,
  109. MaxTemp: maxTemp,
  110. }
  111. return device.Update()
  112. }
  113. func (s *manholeCoverService) DeviceCount() dao.DeviceStatus {
  114. var rsp dao.DeviceStatus
  115. d := dao.ManholeCover{}
  116. rsp.Set(d.DeviceCount1())
  117. return rsp
  118. }
  119. func (s *manholeCoverService) ListDevices() []dao.Device {
  120. d := dao.ManholeCover{}
  121. return d.ListDevices1()
  122. }