command_list.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package gate_command
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/database"
  7. "git.getensh.com/common/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "google.golang.org/grpc/status"
  10. "property-device/errors"
  11. dbmodel "property-device/model"
  12. pb_v1 "property-device/pb/v1"
  13. )
  14. func checkGateCommandListParam(req *pb_v1.GateCommandListRequest) error {
  15. switch {
  16. case req.DeviceId == 0:
  17. return status.Error(10003, "设备id不能为空")
  18. }
  19. if req.Page == 0 {
  20. req.Page = 1
  21. }
  22. if req.PageSize == 0 {
  23. req.PageSize = 10
  24. }
  25. return nil
  26. }
  27. func GateCommandList(ctx context.Context, req *pb_v1.GateCommandListRequest) (reply *pb_v1.GateCommandListReply, err error) {
  28. reply = &pb_v1.GateCommandListReply{}
  29. // 捕获各个task中的异常并返回给调用者
  30. defer func() {
  31. if r := recover(); r != nil {
  32. err = fmt.Errorf("%+v", r)
  33. e := &status.Status{}
  34. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  35. logger.Error("err",
  36. zap.String("system_err", err.Error()),
  37. zap.Stack("stacktrace"))
  38. }
  39. }
  40. }()
  41. err = checkGateCommandListParam(req)
  42. if err != nil {
  43. return nil, err
  44. }
  45. p := &dbmodel.TGateCommand{}
  46. where := [][2]interface{}{}
  47. where = dbmodel.WhereAdd(where, "device_id", req.DeviceId)
  48. where = dbmodel.WhereAdd(where, "code in", []int{1, 2})
  49. if req.Start > 0 {
  50. where = dbmodel.WhereAdd(where, "created_at >=", req.Start)
  51. }
  52. if req.End > 0 {
  53. where = dbmodel.WhereAdd(where, "created_at <", req.End)
  54. }
  55. if req.Status > 0 {
  56. where = dbmodel.WhereAdd(where, "status", req.Status)
  57. }
  58. reply.Page = req.Page
  59. reply.Total, err = p.Count(database.DB(), where, nil)
  60. if err != nil {
  61. return nil, errors.DataBaseError
  62. }
  63. if reply.Total == 0 {
  64. return reply, nil
  65. }
  66. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  67. if err != nil {
  68. return nil, errors.DataBaseError
  69. }
  70. reply.List = make([]*pb_v1.GateCommandItem, len(list))
  71. for i, v := range list {
  72. reply.List[i] = &pb_v1.GateCommandItem{
  73. DeviceId: v.DeviceId,
  74. Desc: v.Desc,
  75. // 1 待执行 2 执行中 3 执行完成
  76. Status: v.Status,
  77. CreatedAt: v.CreatedAt.Unix(),
  78. // 1 成功 2 失败
  79. ResultStatus: v.ResultStatus,
  80. // 结果描述
  81. ResultStatusDesc: v.ResultStatusDesc,
  82. Id: v.ID,
  83. }
  84. }
  85. return reply, nil
  86. }