Skip to content

Commit

Permalink
feat: support variables
Browse files Browse the repository at this point in the history
  • Loading branch information
lzw5399 committed Mar 21, 2021
1 parent 05ba300 commit 9859599
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 22 deletions.
24 changes: 24 additions & 0 deletions src/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,23 @@ var doc = `{
}
},
"definitions": {
"model.InstanceVariable": {
"type": "object",
"properties": {
"name": {
"description": "变量名",
"type": "string"
},
"type": {
"description": "变量类型 1=int 2=string 3=bool 4=float64",
"type": "integer"
},
"value": {
"description": "变量值",
"type": "object"
}
}
},
"request.BatchSyncRoleUsersRequest": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -756,6 +773,13 @@ var doc = `{
"title": {
"description": "流程实例标题",
"type": "string"
},
"variables": {
"description": "变量",
"type": "array",
"items": {
"$ref": "#/definitions/model.InstanceVariable"
}
}
}
},
Expand Down
24 changes: 24 additions & 0 deletions src/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,23 @@
}
},
"definitions": {
"model.InstanceVariable": {
"type": "object",
"properties": {
"name": {
"description": "变量名",
"type": "string"
},
"type": {
"description": "变量类型 1=int 2=string 3=bool 4=float64",
"type": "integer"
},
"value": {
"description": "变量值",
"type": "object"
}
}
},
"request.BatchSyncRoleUsersRequest": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -736,6 +753,13 @@
"title": {
"description": "流程实例标题",
"type": "string"
},
"variables": {
"description": "变量",
"type": "array",
"items": {
"$ref": "#/definitions/model.InstanceVariable"
}
}
}
},
Expand Down
17 changes: 17 additions & 0 deletions src/docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
definitions:
model.InstanceVariable:
properties:
name:
description: 变量名
type: string
type:
description: 变量类型 1=int 2=string 3=bool 4=float64
type: integer
value:
description: 变量值
type: object
type: object
request.BatchSyncRoleUsersRequest:
properties:
roleUsersList:
Expand Down Expand Up @@ -61,6 +73,11 @@ definitions:
title:
description: 流程实例标题
type: string
variables:
description: 变量
items:
$ref: '#/definitions/model.InstanceVariable'
type: array
type: object
request.SyncRoleUsersRequest:
properties:
Expand Down
11 changes: 9 additions & 2 deletions src/model/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ type ProcessInstance struct {
IsDenied bool `gorm:"default:false" json:"isDenied" form:"isDenied"` // 是否被拒绝
State datatypes.JSON `gorm:"type:jsonb" json:"state" form:"state"` // 状态信息
RelatedPerson pq.Int64Array `gorm:"type:integer[]; default:array[]::integer[]" json:"relatedPerson" form:"relatedPerson"` // 工单所有处理人
UrgeCount int `gorm:"type:integer; default:0" json:"urgeCount" form:"urgeCount"` // 催办次数
UrgeLastTime time.Time `gorm:"type:timestamp" json:"urgeLastTime" form:"urgeLastTime"` // 上一次催促时间
TenantId int `gorm:"index" json:"tenantId" form:"tenantId"` // 租户id
Variables datatypes.JSON `gorm:"type:jsonb" json:"variables" form:"variables"` // 变量
UrgeCount int `gorm:"type:integer; default:0" json:"urgeCount" form:"urgeCount"` // 催办次数(暂不支持)
UrgeLastTime time.Time `gorm:"type:timestamp" json:"urgeLastTime" form:"urgeLastTime"` // 上一次催促时间(暂不支持)
}

type InstanceVariable struct {
Name string `json:"name"` // 变量名
Type int `json:"type"` // 变量类型 1=int 2=string 3=bool 4=float64
Value interface{} `json:"value"` // 变量值
}
7 changes: 5 additions & 2 deletions src/model/request/instancerequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"time"

"workflow/src/model"
"workflow/src/util"
)

type ProcessInstanceRequest struct {
Title string `json:"title" form:"title"` // 流程实例标题
ProcessDefinitionId int `json:"processDefinitionId" form:"processDefinitionId"` // 流程ID
Title string `json:"title" form:"title"` // 流程实例标题
ProcessDefinitionId int `json:"processDefinitionId" form:"processDefinitionId"` // 流程ID
Variables []model.InstanceVariable `json:"variables"` // 变量
}

func (i *ProcessInstanceRequest) ToProcessInstance(currentUserId uint, tenantId uint) model.ProcessInstance {
Expand All @@ -27,6 +29,7 @@ func (i *ProcessInstanceRequest) ToProcessInstance(currentUserId uint, tenantId
Title: i.Title,
ProcessDefinitionId: i.ProcessDefinitionId,
TenantId: int(tenantId),
Variables: util.MarshalToDbJson(i.Variables),
}
}

Expand Down
37 changes: 31 additions & 6 deletions src/service/instanceservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ func (i *instanceService) CreateProcessInstance(r *request.ProcessInstanceReques
processInstance = r.ToProcessInstance(currentUserId, tenantId)
processEngine *engine.ProcessEngine // 流程定义引擎
instanceEngine *engine.InstanceEngine // 流程实例引擎
condExprStatus bool
sourceEdges []map[string]interface{}
targetEdges []map[string]interface{}
)

// 检查变量是否合法
err = validateVariables(r.Variables)
if err != nil {
return nil, err
}

// 查询对应的流程模板
err = global.BankDb.
Where("id = ?", processInstance.ProcessDefinitionId).
Expand Down Expand Up @@ -93,6 +96,7 @@ func (i *instanceService) CreateProcessInstance(r *request.ProcessInstanceReques
// 排他网关
case "exclusiveGateway":
var sourceEdges []map[string]interface{}
var condExprStatus bool
sourceEdges, err = processEngine.GetEdge(comingNode["id"].(string), "source")
if err != nil {
return nil, err
Expand Down Expand Up @@ -137,12 +141,12 @@ func (i *instanceService) CreateProcessInstance(r *request.ProcessInstanceReques
// 并行网关
case "parallelGateway":
// 入口,判断
sourceEdges, err = processEngine.GetEdge(comingNode["id"].(string), "source")
sourceEdges, err := processEngine.GetEdge(comingNode["id"].(string), "source")
if err != nil {
return nil, fmt.Errorf("查询流转信息失败,%v", err.Error())
}

targetEdges, err = processEngine.GetEdge(comingNode["id"].(string), "target")
targetEdges, err := processEngine.GetEdge(comingNode["id"].(string), "target")
if err != nil {
return nil, fmt.Errorf("查询流转信息失败,%v", err.Error())
}
Expand Down Expand Up @@ -194,7 +198,6 @@ func (i *instanceService) CreateProcessInstance(r *request.ProcessInstanceReques

// 开启事务
err = global.BankDb.Transaction(func(tx *gorm.DB) error {

// 创建
err = tx.Create(&processInstance).Error
if err != nil {
Expand Down Expand Up @@ -472,6 +475,28 @@ func (i *instanceService) GetProcessTrain(pi *model.ProcessInstance, instanceId
return trainNodes, nil
}

// 检查变量是否合法
func validateVariables(variables []model.InstanceVariable) error {
checkedVariables := make(map[string]model.InstanceVariable, 0)
for _, v := range variables {
// 检查类型
isValidType := From([]int{1, 2, 3, 4}).AnyWith(func(i interface{}) bool {
return i.(int) == v.Type
})
if !isValidType {
return fmt.Errorf("当前变量:%s 的类型不合法,请检查", v.Name)
}

// 检查是否重名
if _, present := checkedVariables[v.Name]; present {
return fmt.Errorf("当前变量名:%s 重复, 请检查", v.Name)
}
checkedVariables[v.Name] = v
}

return nil
}

// 获取实例的某一个变量
//func (i *instanceService) GetVariable(r *request.GetVariableRequest) (*response.InstanceVariableResponse, error) {
// var str string
Expand Down
9 changes: 9 additions & 0 deletions src/util/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"encoding/json"

"gorm.io/datatypes"

"workflow/src/model"
)

func StringToMap(jsonStr string) (map[string]string, error) {
Expand Down Expand Up @@ -46,3 +48,10 @@ func MarshalToDbJson(m interface{}) datatypes.JSON {
func MarshalToString(m interface{}) string {
return string(MarshalToBytes(m))
}

func UnmarshalToInstanceVariables(m datatypes.JSON) []model.InstanceVariable {
var variables []model.InstanceVariable
_ = json.Unmarshal([]byte(m), &variables)

return variables
}
12 changes: 0 additions & 12 deletions src/util/errormessage.go

This file was deleted.

0 comments on commit 9859599

Please sign in to comment.