event_seme_time_flow.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package model
  2. import (
  3. "encoding/json"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  8. )
  9. type EventSemeTimeFlow struct {
  10. Id int64 `json:"id"`
  11. SemeTimeId int64 `json:"seme_time_id"`
  12. CollateFlowData string `json:"collate_flow_data"`
  13. OriginalFlowData string `json:"original_flow_data"`
  14. CreatedAt int64 `json:"created_at"`
  15. UpdatedAt int64 `json:"updated_at"`
  16. }
  17. func (e *EventSemeTimeFlow) TableName() string {
  18. return "event_seme_time_flow"
  19. }
  20. func NewEventSemeTimeFlow(semeTimeId int64, req *pasturePb.SemeTimeRequest) *EventSemeTimeFlow {
  21. originalFlowData, _ := json.Marshal(req.Graph)
  22. collateFlowData := make([]*pasturePb.CollateFlowData, len(req.Graph.Nodes))
  23. for i, v := range req.Graph.Nodes {
  24. conditions := &pasturePb.FlowConditions{
  25. Day: 0,
  26. Hours: 0,
  27. }
  28. if i != len(req.Graph.Nodes)-1 && i < len(req.Graph.Edges) {
  29. edgesTextValue := req.Graph.Edges[i].Text.Value
  30. dayHours := strings.Split(edgesTextValue, "/")
  31. dayStr, hoursStr := "0", "0"
  32. if len(dayHours) > 0 && len(dayHours) == 1 {
  33. dayStr = regexp.MustCompile(`\d+`).FindString(dayHours[0])
  34. }
  35. if len(dayHours) > 0 && len(dayHours) == 2 {
  36. hoursStr = regexp.MustCompile(`\d+`).FindString(dayHours[1])
  37. }
  38. day, _ := strconv.Atoi(dayStr)
  39. hours, _ := strconv.Atoi(hoursStr)
  40. conditions.Day = int32(day)
  41. conditions.Hours = int32(hours)
  42. }
  43. newCollateFlowData := &pasturePb.CollateFlowData{
  44. Id: v.Id,
  45. Sort: int32(i + 1),
  46. NodeName: v.Text.Value,
  47. Conditions: conditions,
  48. }
  49. switch i {
  50. case 0:
  51. newCollateFlowData.NodeType = pasturePb.FlowerNodeType_Start
  52. case len(req.Graph.Nodes) - 1:
  53. newCollateFlowData.NodeType = pasturePb.FlowerNodeType_End
  54. default:
  55. newCollateFlowData.NodeType = pasturePb.FlowerNodeType_Middle
  56. }
  57. collateFlowData[i] = newCollateFlowData
  58. }
  59. collateFlowDataByte, _ := json.Marshal(collateFlowData)
  60. return &EventSemeTimeFlow{
  61. SemeTimeId: semeTimeId,
  62. CollateFlowData: string(collateFlowDataByte),
  63. OriginalFlowData: string(originalFlowData),
  64. }
  65. }