data_waring.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package model
  2. import (
  3. "time"
  4. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  5. )
  6. const DefaultUserId = 0
  7. type DataWarning struct {
  8. Id int64 `json:"id"`
  9. UserId int64 `json:"userId"`
  10. Kind string `json:"kind"`
  11. Name string `json:"name"`
  12. Description string `json:"description"`
  13. DataValue string `json:"dataValue"`
  14. DataUpdateAt int64 `json:"dataUpdateAt"`
  15. ConditionUpdateAt int64 `json:"conditionUpdateAt"`
  16. IsShow pasturePb.IsShow_Kind `json:"isShow"`
  17. CreatedAt int64 `json:"createdAt"`
  18. UpdatedAt int64 `json:"updatedAt"`
  19. }
  20. func (d *DataWarning) TableName() string {
  21. return "data_warning"
  22. }
  23. func NewDataWarningList(userId int64, req []*pasturePb.WarningDataSet, warningMap map[string]*DataWarning) []*DataWarning {
  24. res := make([]*DataWarning, 0)
  25. isAdd := make(map[string]bool)
  26. for _, v := range req {
  27. if _, ok := isAdd[v.Kind]; ok {
  28. continue
  29. }
  30. isAdd[v.Kind] = true
  31. defaultDataWarning := warningMap[v.Kind]
  32. res = append(res, NewDataWarning(userId, v.Kind, v.IsShow, defaultDataWarning))
  33. }
  34. return res
  35. }
  36. func NewDataWarning(userId int64, Kind string, isShow pasturePb.IsShow_Kind, defaultDataWarning *DataWarning) *DataWarning {
  37. return &DataWarning{
  38. UserId: userId,
  39. Kind: Kind,
  40. Name: defaultDataWarning.Name,
  41. Description: defaultDataWarning.Description,
  42. IsShow: isShow,
  43. }
  44. }
  45. type DataWarningSlice []*DataWarning
  46. func (d DataWarningSlice) ToPB() []*pasturePb.WarningDataShow {
  47. res := make([]*pasturePb.WarningDataShow, 0)
  48. for _, warningData := range d {
  49. if warningData.IsShow != pasturePb.IsShow_Ok {
  50. continue
  51. }
  52. dataUpdateTimeFormat := ""
  53. if warningData.DataUpdateAt > 0 {
  54. dataUpdateTimeFormat = time.Unix(warningData.DataUpdateAt, 0).Format(LayoutTime)
  55. }
  56. res = append(res, &pasturePb.WarningDataShow{
  57. Name: warningData.Name,
  58. Number: warningData.DataValue,
  59. Describe: warningData.Description,
  60. DataUpdateTimeFormat: dataUpdateTimeFormat,
  61. Kind: warningData.Kind,
  62. })
  63. }
  64. return res
  65. }