alarmTerminalService.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/device/dao"
  5. "iot_manager_service/app/device/model"
  6. "iot_manager_service/util"
  7. "time"
  8. )
  9. // 中间件管理服务
  10. var AlarmTerminalService = new(alarmTerminalService)
  11. type alarmTerminalService struct{}
  12. func (s *alarmTerminalService) Get(id int) (*dao.AlarmTerminal, *util.Errors) {
  13. // 创建查询实例
  14. device := &dao.AlarmTerminal{
  15. ID: id,
  16. }
  17. err := device.GetDevice()
  18. if err != nil {
  19. return nil, util.FailResponse(err.Error(), nil)
  20. }
  21. return device, nil
  22. }
  23. func (s *alarmTerminalService) CreateOrUpdate(userId int64, tenantId int, req dao.AlarmTerminal) *util.Errors {
  24. device := req
  25. device.TenantId = tenantId
  26. device.UpdateUser = userId
  27. device.UpdateTime = time.Now()
  28. if req.ID == 0 {
  29. device.CreateTime = time.Now()
  30. device.CreateUser = userId
  31. if device.IsExistedBySN() {
  32. fmt.Printf("Create IsExistedBySN \n")
  33. return util.ParamsInvalidResponse(model.RepeatedName, nil)
  34. }
  35. if err := device.Create(); err != nil {
  36. fmt.Printf("Create err = %s \n", err.Error())
  37. return util.FailResponse(err.Error(), nil)
  38. }
  39. return util.SuccessResponse(util.Succeeded, nil)
  40. }
  41. if device.IsExistedByNameAndCode() {
  42. fmt.Printf("Update IsExistedByNameAndCode \n")
  43. return util.ParamsInvalidResponse(model.RepeatedName, nil)
  44. }
  45. if err := device.Update(); err != nil {
  46. fmt.Printf("Update err = %s \n", err.Error())
  47. return util.FailResponse(err.Error(), nil)
  48. }
  49. //todo operation record
  50. return util.SuccessResponse(util.Succeeded, nil)
  51. }
  52. func (s *alarmTerminalService) List(poleGroupName string, current, size int) ([]dao.AlarmTerminal, *util.Errors) {
  53. // 创建查询实例
  54. device := &dao.AlarmTerminal{}
  55. offset := (current - 1) * size
  56. limit := size
  57. devices, err := device.GetDevices(offset, limit)
  58. if err != nil {
  59. return nil, util.FailResponse(err.Error(), nil)
  60. }
  61. return devices, nil
  62. }
  63. func (s *alarmTerminalService) Remove(userId int64, id int) *util.Errors {
  64. // 创建查询实例
  65. device := &dao.AlarmTerminal{
  66. ID: id,
  67. IsDeleted: 1,
  68. UpdateUser: userId,
  69. UpdateTime: time.Now(),
  70. }
  71. //todo
  72. // service.lampPoleService.CountRelation()
  73. //todo operation record
  74. err := device.Delete()
  75. if err != nil {
  76. return util.FailResponse(err.Error(), nil)
  77. }
  78. return nil
  79. }