golang办公工作流workflow利用js-ojus/flow做测试——系列二

golang语言的办公工作流的包介绍——系列一

golang办公工作流workflow利用js-ojus/flow做测试——系列二

golang办公流程引擎初体验js-ojus/flow——系列三

golang办公流程引擎初体验js-ojus/flow——系列四

它这个东西https://github.com/js-ojus/flow很有意思,没有任何example,完全靠蒙,或者看它test摸索吧,看它issue里别人的例子。

首先安装mysql

然后建立一个用户travis,并且密码为空

CREATE USER 'travis'@'localhost' IDENTIFIED BY ''";

然后还要新建一个数据库flow,选择utf8mb4,它源码的某个地方有讲。

——然后给用户travis设置flow这个数据库的权限

golang办公工作流workflow利用js-ojus/flow做测试——系列二_第1张图片

golang办公工作流workflow利用js-ojus/flow做测试——系列二_第2张图片

然后进入文件夹内,初始化和进行测试。

D:\gowork\src\github.com\js-ojus\flow>setup_db.sh -t

D:\gowork\src\github.com\js-ojus\flow>setup_blob_dirs.sh

D:\gowork\src\github.com\js-ojus\flow>go test
PASS
ok      github.com/js-ojus/flow 5.965s

D:\gowork\src\github.com\js-ojus\flow>

beego里使用:

package controllers

import (
	"database/sql"
	// "strings"
	// "testing"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	// "github.com/astaxie/beego"
	"github.com/js-ojus/flow"
	// _ "github.com/mattn/go-sqlite3"
	// "github.com/astaxie/beego/httplib"
	// "github.com/astaxie/beego/logs"
	"log"
)

type WFApi struct {
	flow.Document
}

// fatal1 expects a value and an error value as its arguments.
func fatal1(val1 interface{}, err error) interface{} {
	if err != nil {
		fmt.Println("%v", err)
	}
	return val1
}

// Initialise DB connection.
// func init() {
// gt = t

// Connect to the database.travis
// driver, connStr := "mysql", "root:root@/flow"
// tdb := fatal1(sql.Open(driver, connStr)).(*sql.DB)
// flow.RegisterDB(tdb)

// if tdb == nil {
// 	log.Fatal("given database handle is `nil`")
// }
// db = tdb

// return nil
// }

// func RegisterDB(sdb *sql.DB) error {
// 	if sdb == nil {
// 		log.Fatal("given database handle is `nil`")
// 	}
// 	db = sdb

// 	return nil
// }

func WorkflowsCreate() {
	driver, connStr := "mysql", "root:root@/flow"
	tdb := fatal1(sql.Open(driver, connStr)).(*sql.DB)
	// flow.RegisterDB(tdb)

	if tdb == nil {
		log.Fatal("given database handle is `nil`")
	}
	db := tdb
	// var db *sql.DB
	tx, _ := db.Begin()

	// var tx *sql.Tx
	defer tx.Rollback()

	docType1, err := flow.DocTypes.New(tx, "EXAM:COMMON")
	if err != nil {
		fmt.Println(err)
	}

	docState1, err := flow.DocStates.New(tx, "Init") //初始化
	// docState2, err := flow.DocStates.New(tx, "EntrustCreate")   //委托创建
	// docState3, err := flow.DocStates.New(tx, "EntrustApproved") //委托审核
	// docState4, err := flow.DocStates.New(tx, "SampleHandon")    //样本交接
	// docState5, err := flow.DocStates.New(tx, "PrepareFinish")   //制备完成
	flow.DocStates.New(tx, "PrepareApproved") //制备审核
	flow.DocStates.New(tx, "TaskAssign")      //任务分配
	flow.DocStates.New(tx, "DataApproved")    //数据录入审核
	flow.DocStates.New(tx, "ReportGen")       //报告生成
	flow.DocStates.New(tx, "ReportApproved")  //报告审核

	/////
	docActionID1, _ := flow.DocActions.New(tx, "CreateEntrust", false)  //创建委托
	docActionID2, _ := flow.DocActions.New(tx, "ApproveEntrust", false) //审核委托
	docActionID3, _ := flow.DocActions.New(tx, "HandonSample", false)   //提交样本
	docActionID4, _ := flow.DocActions.New(tx, "FinishPrepare", true)   //完成制备
	docActionID5, _ := flow.DocActions.New(tx, "ApprovePrepare", true)  //审核制备
	docActionID6, _ := flow.DocActions.New(tx, "AssignTask", false)     //分配任务
	docActionID7, _ := flow.DocActions.New(tx, "ApproveData", false)    //审核数据
	docActionID8, _ := flow.DocActions.New(tx, "GenReport", false)      //生成报告
	docActionID9, _ := flow.DocActions.New(tx, "ApproveReport", true)   //审核报告

	workflowId, _ := flow.Workflows.New(tx, "Examination", docType1, docState1)
	flow.Workflows.SetActive(tx, workflowId, true)

	//创建Docments
	contextId, _ := flow.AccessContexts.New(tx, "Context")
	groupId, _ := flow.Groups.New(tx, "Examination", "exam")

	resUser, _ := tx.Exec(`INSERT INTO users_master(first_name, last_name, email, active)
		VALUES('admin', 'dashoo', '[email protected]', 1)`)
	uid, _ := resUser.LastInsertId()
	userID1 := flow.UserID(uid)

	flow.Groups.AddUser(tx, groupId, userID1)
	roleID1, _ := flow.Roles.New(tx, "administrator")
	flow.Roles.AddPermissions(tx, roleID1, docType1, []flow.DocActionID{docActionID1, docActionID2,
		docActionID3, docActionID4, docActionID5, docActionID6, docActionID7, docActionID8, docActionID9})

	docNewInput := flow.DocumentsNewInput{
		DocTypeID:       docType1,
		AccessContextID: contextId,
		GroupID:         groupId,
		Title:           "entrust flow",
		Data:            "eid: 111, entrustNo: 2222",
	}
	flow.Documents.New(tx, &docNewInput)

	// documentID1, err := flow.Documents.New(tx, &docNewInput)

	// Documents.setState(tx, docType1, documentID1, docState2, contextId)

	tx.Commit()
}

func (this *WFApi) WorkflowsNext() {

	// tx, _ := flow.db.Begin()
	var tx *sql.Tx
	defer tx.Rollback()

	/*docNewInput := DocumentsNewInput {
	  	DocTypeID: docType1,
	  	AccessContextID: contextId,
	  	GroupID: groupId,
	  	Title: "entrust flow",
	  	Data: "eid: 111, entrustNo: 2222",
	  }
	  documentID1, err := Documents.New(tx, &docNewInput)
	  fmt.Println("documentID1=", documentID1, err)*/

	docEventInput := flow.DocEventsNewInput{
		DocTypeID:   flow.DocTypeID(1),
		DocumentID:  flow.DocumentID(1),
		DocStateID:  flow.DocStateID(2),
		DocActionID: flow.DocActionID(2),
		GroupID:     flow.GroupID(1),
		Text:        "开始审批",
	}
	groupIds := []flow.GroupID{flow.GroupID(1)}
	myWorkflow, err := flow.Workflows.Get(flow.WorkflowID(1))
	docEvent1, err := flow.DocEvents.New(tx, &docEventInput)
	myDocEvent, err := flow.DocEvents.Get(docEvent1)
	newDocStateId, err := myWorkflow.ApplyEvent(tx, myDocEvent, groupIds)
	fmt.Println("newDocStateId=", newDocStateId, err)

	tx.Commit()
}

main.go里调用

func main() {

	controllers.WorkflowsCreate()
	beego.Run()
}

 类似这样而已。暂时到这了。

 

你可能感兴趣的:(golang,beego,标准管理系统,移动办公,信息化,工作流)