12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package model
- import (
- "encoding/json"
- "regexp"
- "strconv"
- "strings"
- pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
- )
- type EventSemeTimeFlow struct {
- Id int64 `json:"id"`
- SemeTimeId int64 `json:"seme_time_id"`
- CollateFlowData string `json:"collate_flow_data"`
- OriginalFlowData string `json:"original_flow_data"`
- CreatedAt int64 `json:"created_at"`
- UpdatedAt int64 `json:"updated_at"`
- }
- func (e *EventSemeTimeFlow) TableName() string {
- return "event_seme_time_flow"
- }
- func NewEventSemeTimeFlow(semeTimeId int64, req *pasturePb.SemeTimeRequest) *EventSemeTimeFlow {
- originalFlowData, _ := json.Marshal(req.Graph)
- collateFlowData := make([]*pasturePb.CollateFlowData, len(req.Graph.Nodes))
- for i, v := range req.Graph.Nodes {
- conditions := &pasturePb.FlowConditions{
- Day: 0,
- Hours: 0,
- }
- if i != len(req.Graph.Nodes)-1 && i < len(req.Graph.Edges) {
- edgesTextValue := req.Graph.Edges[i].Text.Value
- dayHours := strings.Split(edgesTextValue, "/")
- dayStr, hoursStr := "0", "0"
- if len(dayHours) > 0 && len(dayHours) == 1 {
- dayStr = regexp.MustCompile(`\d+`).FindString(dayHours[0])
- }
- if len(dayHours) > 0 && len(dayHours) == 2 {
- hoursStr = regexp.MustCompile(`\d+`).FindString(dayHours[1])
- }
- day, _ := strconv.Atoi(dayStr)
- hours, _ := strconv.Atoi(hoursStr)
- conditions.Day = int32(day)
- conditions.Hours = int32(hours)
- }
- newCollateFlowData := &pasturePb.CollateFlowData{
- Id: v.Id,
- Sort: int32(i + 1),
- NodeName: v.Text.Value,
- Conditions: conditions,
- }
- switch i {
- case 0:
- newCollateFlowData.NodeType = pasturePb.FlowerNodeType_Start
- case len(req.Graph.Nodes) - 1:
- newCollateFlowData.NodeType = pasturePb.FlowerNodeType_End
- default:
- newCollateFlowData.NodeType = pasturePb.FlowerNodeType_Middle
- }
- collateFlowData[i] = newCollateFlowData
- }
- collateFlowDataByte, _ := json.Marshal(collateFlowData)
- return &EventSemeTimeFlow{
- SemeTimeId: semeTimeId,
- CollateFlowData: string(collateFlowDataByte),
- OriginalFlowData: string(originalFlowData),
- }
- }
|