optoSensoService.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/device/dao"
  5. "iot_manager_service/util"
  6. "time"
  7. )
  8. var OptoSensorService = new(optoSensorService)
  9. type optoSensorService struct{}
  10. func (s *optoSensorService) Get(id int) (*dao.OptoSensor, *util.Errors) {
  11. // 创建查询实例
  12. device := &dao.OptoSensor{
  13. ID: id,
  14. }
  15. err := device.GetDevice()
  16. if err != nil {
  17. return nil, util.FailResponse(err.Error(), nil)
  18. }
  19. return device, nil
  20. }
  21. func (s *optoSensorService) CreateOrUpdate(req dao.OptoSensor) *util.Errors {
  22. // 创建查询实例
  23. device := req
  24. device.UpdateUser = "TODO" // todo: 使用登录态
  25. device.UpdateTime = time.Now()
  26. if req.ID == 0 {
  27. device.CreateTime = time.Now()
  28. device.CreateUser = "TODO" // todo: 使用登录态
  29. if device.IsExistedBySN() {
  30. fmt.Printf("Create GetDeviceID err \n")
  31. return util.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  32. }
  33. if err := device.Create(); err != nil {
  34. fmt.Printf("Create err = %s \n", err.Error())
  35. return util.FailResponse(err.Error(), nil)
  36. }
  37. return util.SuccessResponse(util.Succeeded, nil)
  38. }
  39. if device.IsExistedByNameAndCode() {
  40. return util.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  41. }
  42. if err := device.Update(); err != nil {
  43. fmt.Printf("Update err = %s \n", err.Error())
  44. return util.FailResponse(err.Error(), nil)
  45. }
  46. //todo operation record
  47. return util.SuccessResponse(util.Succeeded, nil)
  48. }
  49. func (s *optoSensorService) List(searchValue string, current, size int) ([]dao.OptoSensor, *util.Errors) {
  50. device := dao.OptoSensor{}
  51. if searchValue != "" {
  52. device.SN = searchValue
  53. device.Name = searchValue
  54. }
  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 *optoSensorService) Remove(id int) *util.Errors {
  64. // 创建查询实例
  65. device := &dao.OptoSensor{
  66. ID: id,
  67. IsDeleted: 1,
  68. UpdateUser: "TODO", // todo 使用登录态
  69. UpdateTime: time.Now(),
  70. }
  71. //todo
  72. // service.transformerService.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. }
  80. func (s *optoSensorService) GetList() ([]*dao.OptoSensor, *util.Errors) {
  81. device := &dao.OptoSensor{
  82. TenantId: "000000", // todo 使用登录态
  83. IsDeleted: 0,
  84. }
  85. devices, err := device.GetAllDevices()
  86. if err != nil {
  87. return nil, util.FailResponse(err.Error(), nil)
  88. }
  89. return devices, nil
  90. }