1358 lines
34 KiB
Go
1358 lines
34 KiB
Go
package approval
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"server/models"
|
|
"server/services/erp"
|
|
"server/services/wechatmini"
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
"github.com/beego/beego/v2/core/logs"
|
|
)
|
|
|
|
type Approver struct {
|
|
ID uint64
|
|
Name string
|
|
}
|
|
|
|
type SubmitInput struct {
|
|
Scope int8
|
|
Tid uint64
|
|
BizTid uint64
|
|
BizType string
|
|
FlowCode string
|
|
BizID uint64
|
|
BizTitle string
|
|
SubmitUID uint64
|
|
SubmitName string
|
|
DeptID uint64
|
|
FlowID uint64
|
|
FormData map[string]interface{}
|
|
}
|
|
|
|
type Node struct {
|
|
ID uint64
|
|
NodeKey string
|
|
NodeName string
|
|
NodeType int8
|
|
ApproverType int8
|
|
ApproverIDs string
|
|
MultiMode int8
|
|
AllowReturn int8
|
|
TimeoutHours int
|
|
Sort int
|
|
PrevKeys string
|
|
NextKeys string
|
|
ConditionExpr string
|
|
}
|
|
|
|
type Flow struct {
|
|
ID uint64
|
|
BizType string
|
|
FlowName string
|
|
IsDefault int8
|
|
Status int8
|
|
Nodes []Node
|
|
ByKey map[string]*Node
|
|
StartKey string
|
|
}
|
|
|
|
type ErrNoFlow struct {
|
|
BizType string
|
|
}
|
|
|
|
func (e *ErrNoFlow) Error() string {
|
|
return "未找到适用于该单据的审批流程"
|
|
}
|
|
|
|
type ErrNoApprover struct {
|
|
NodeName string
|
|
NodeKey string
|
|
}
|
|
|
|
func (e *ErrNoApprover) Error() string {
|
|
return fmt.Sprintf("节点「%s」未解析到有效审批人,请检查审批人配置", e.NodeName)
|
|
}
|
|
|
|
func tenantFlows(tid uint64, bizType string) ([]models.TenantApprovalFlow, error) {
|
|
qs := models.Orm.QueryTable(new(models.TenantApprovalFlow)).
|
|
Filter("tid", tid).
|
|
Filter("is_deleted", 0)
|
|
if bizType != "" {
|
|
qs = qs.Filter("biz_type", bizType)
|
|
}
|
|
var rows []models.TenantApprovalFlow
|
|
_, err := qs.OrderBy("-id").All(&rows)
|
|
return rows, err
|
|
}
|
|
|
|
func tenantFlowNodes(flowID uint64) ([]Node, error) {
|
|
var rows []models.TenantApprovalNode
|
|
if _, err := models.Orm.QueryTable(new(models.TenantApprovalNode)).
|
|
Filter("flow_id", flowID).
|
|
Filter("is_deleted", 0).
|
|
OrderBy("sort", "id").
|
|
All(&rows); err != nil && err != orm.ErrNoRows {
|
|
return nil, err
|
|
}
|
|
out := make([]Node, 0, len(rows))
|
|
for _, r := range rows {
|
|
out = append(out, Node{
|
|
ID: r.ID,
|
|
NodeKey: r.NodeKey,
|
|
NodeName: r.NodeName,
|
|
NodeType: r.NodeType,
|
|
ApproverType: r.ApproverType,
|
|
ApproverIDs: r.ApproverIDs,
|
|
MultiMode: r.MultiMode,
|
|
AllowReturn: r.AllowReturn,
|
|
TimeoutHours: r.TimeoutHours,
|
|
Sort: r.Sort,
|
|
PrevKeys: r.PrevKeys,
|
|
NextKeys: r.NextKeys,
|
|
ConditionExpr: r.ConditionExpr,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func platformFlows(bizType string) ([]models.PlatformApprovalFlow, error) {
|
|
qs := models.Orm.QueryTable(new(models.PlatformApprovalFlow)).Filter("is_deleted", 0)
|
|
if bizType != "" {
|
|
qs = qs.Filter("biz_type", bizType)
|
|
}
|
|
var rows []models.PlatformApprovalFlow
|
|
_, err := qs.OrderBy("-id").All(&rows)
|
|
return rows, err
|
|
}
|
|
|
|
func platformFlowNodes(flowID uint64) ([]Node, error) {
|
|
var rows []models.PlatformApprovalNode
|
|
if _, err := models.Orm.QueryTable(new(models.PlatformApprovalNode)).
|
|
Filter("flow_id", flowID).
|
|
Filter("is_deleted", 0).
|
|
OrderBy("sort", "id").
|
|
All(&rows); err != nil && err != orm.ErrNoRows {
|
|
return nil, err
|
|
}
|
|
out := make([]Node, 0, len(rows))
|
|
for _, r := range rows {
|
|
out = append(out, Node{
|
|
ID: r.ID,
|
|
NodeKey: r.NodeKey,
|
|
NodeName: r.NodeName,
|
|
NodeType: r.NodeType,
|
|
ApproverType: r.ApproverType,
|
|
ApproverIDs: r.ApproverIDs,
|
|
MultiMode: r.MultiMode,
|
|
AllowReturn: r.AllowReturn,
|
|
TimeoutHours: r.TimeoutHours,
|
|
Sort: r.Sort,
|
|
PrevKeys: r.PrevKeys,
|
|
NextKeys: r.NextKeys,
|
|
ConditionExpr: r.ConditionExpr,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func LoadFlow(scope int8, tid uint64, bizType string, flowID uint64) (*Flow, error) {
|
|
if scope == models.ApprovalScopePlatform {
|
|
var candidates []models.PlatformApprovalFlow
|
|
var err error
|
|
if flowID > 0 {
|
|
var one models.PlatformApprovalFlow
|
|
if e := models.Orm.QueryTable(new(models.PlatformApprovalFlow)).
|
|
Filter("id", flowID).Filter("is_deleted", 0).One(&one); e != nil {
|
|
return nil, &ErrNoFlow{BizType: bizType}
|
|
}
|
|
candidates = []models.PlatformApprovalFlow{one}
|
|
} else {
|
|
candidates, err = platformFlows(bizType)
|
|
}
|
|
if err != nil || len(candidates) == 0 {
|
|
return nil, &ErrNoFlow{BizType: bizType}
|
|
}
|
|
return buildPlatformFlow(pickPlatformFlow(candidates))
|
|
}
|
|
|
|
var candidates []models.TenantApprovalFlow
|
|
var err error
|
|
if flowID > 0 {
|
|
var one models.TenantApprovalFlow
|
|
if e := models.Orm.QueryTable(new(models.TenantApprovalFlow)).
|
|
Filter("id", flowID).Filter("tid", tid).Filter("is_deleted", 0).One(&one); e != nil {
|
|
return nil, &ErrNoFlow{BizType: bizType}
|
|
}
|
|
candidates = []models.TenantApprovalFlow{one}
|
|
} else {
|
|
candidates, err = tenantFlows(tid, bizType)
|
|
}
|
|
if err != nil || len(candidates) == 0 {
|
|
return nil, &ErrNoFlow{BizType: bizType}
|
|
}
|
|
return buildTenantFlow(pickTenantFlow(candidates))
|
|
}
|
|
|
|
func LoadFlowByCode(scope int8, tid uint64, code string) (*Flow, error) {
|
|
code = strings.TrimSpace(code)
|
|
if code == "" {
|
|
return nil, &ErrNoFlow{BizType: code}
|
|
}
|
|
if scope == models.ApprovalScopePlatform {
|
|
var one models.PlatformApprovalFlow
|
|
if err := models.Orm.QueryTable(new(models.PlatformApprovalFlow)).
|
|
Filter("flow_code", code).Filter("is_deleted", 0).One(&one); err != nil {
|
|
return nil, &ErrNoFlow{BizType: code}
|
|
}
|
|
return buildPlatformFlow(one)
|
|
}
|
|
var one models.TenantApprovalFlow
|
|
if err := models.Orm.QueryTable(new(models.TenantApprovalFlow)).
|
|
Filter("flow_code", code).Filter("tid", tid).Filter("is_deleted", 0).One(&one); err != nil {
|
|
return nil, &ErrNoFlow{BizType: code}
|
|
}
|
|
return buildTenantFlow(one)
|
|
}
|
|
|
|
func buildTenantFlow(f models.TenantApprovalFlow) (*Flow, error) {
|
|
nodes, err := tenantFlowNodes(f.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(nodes) == 0 {
|
|
return nil, &ErrNoFlow{BizType: f.BizType}
|
|
}
|
|
return assembleFlow(f.ID, f.BizType, f.FlowName, nodes), nil
|
|
}
|
|
|
|
func buildPlatformFlow(f models.PlatformApprovalFlow) (*Flow, error) {
|
|
nodes, err := platformFlowNodes(f.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(nodes) == 0 {
|
|
return nil, &ErrNoFlow{BizType: f.BizType}
|
|
}
|
|
return assembleFlow(f.ID, f.BizType, f.FlowName, nodes), nil
|
|
}
|
|
|
|
func assembleFlow(id uint64, bizType, name string, nodes []Node) *Flow {
|
|
f := &Flow{ID: id, BizType: bizType, FlowName: name, Nodes: nodes, ByKey: make(map[string]*Node, len(nodes))}
|
|
for i := range f.Nodes {
|
|
f.ByKey[f.Nodes[i].NodeKey] = &f.Nodes[i]
|
|
}
|
|
for i := range f.Nodes {
|
|
if f.Nodes[i].NodeType == models.ApprovalNodeStart {
|
|
f.StartKey = f.Nodes[i].NodeKey
|
|
break
|
|
}
|
|
}
|
|
if f.StartKey == "" {
|
|
for i := range f.Nodes {
|
|
if strings.TrimSpace(f.Nodes[i].PrevKeys) == "" {
|
|
f.StartKey = f.Nodes[i].NodeKey
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if f.StartKey == "" && len(f.Nodes) > 0 {
|
|
f.StartKey = f.Nodes[0].NodeKey
|
|
}
|
|
return f
|
|
}
|
|
|
|
func pickTenantFlow(rows []models.TenantApprovalFlow) models.TenantApprovalFlow {
|
|
for _, r := range rows {
|
|
if r.IsDefault == 1 && r.Status == 1 {
|
|
return r
|
|
}
|
|
}
|
|
for _, r := range rows {
|
|
if r.Status == 1 {
|
|
return r
|
|
}
|
|
}
|
|
return rows[0]
|
|
}
|
|
|
|
func pickPlatformFlow(rows []models.PlatformApprovalFlow) models.PlatformApprovalFlow {
|
|
for _, r := range rows {
|
|
if r.IsDefault == 1 && r.Status == 1 {
|
|
return r
|
|
}
|
|
}
|
|
for _, r := range rows {
|
|
if r.Status == 1 {
|
|
return r
|
|
}
|
|
}
|
|
return rows[0]
|
|
}
|
|
|
|
func parseIDs(raw string) []uint64 {
|
|
out := make([]uint64, 0, 4)
|
|
for _, part := range strings.Split(raw, ",") {
|
|
part = strings.TrimSpace(part)
|
|
if part == "" {
|
|
continue
|
|
}
|
|
if v, err := strconv.ParseUint(part, 10, 64); err == nil && v > 0 {
|
|
out = append(out, v)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func resolveApprovers(scope int8, node Node, in SubmitInput) ([]Approver, error) {
|
|
ids := parseIDs(node.ApproverIDs)
|
|
|
|
switch node.ApproverType {
|
|
case models.ApprovalApproverUser:
|
|
if scope == models.ApprovalScopePlatform {
|
|
return platformUsers(ids), nil
|
|
}
|
|
return tenantUsers(uint64(in.Tid), ids), nil
|
|
|
|
case models.ApprovalApproverRole:
|
|
if scope == models.ApprovalScopePlatform {
|
|
return platformRoleUsers(ids), nil
|
|
}
|
|
return tenantRoleUsers(uint64(in.Tid), ids), nil
|
|
|
|
case models.ApprovalApproverInitiator:
|
|
if in.SubmitUID == 0 {
|
|
return nil, nil
|
|
}
|
|
return []Approver{{ID: in.SubmitUID, Name: in.SubmitName}}, nil
|
|
|
|
case models.ApprovalApproverOrgLeader:
|
|
if len(ids) == 0 {
|
|
return nil, nil
|
|
}
|
|
return orgLeaders(in.Tid, ids), nil
|
|
|
|
case models.ApprovalApproverInitLeader:
|
|
if in.DeptID == 0 {
|
|
return nil, nil
|
|
}
|
|
return orgLeaders(in.Tid, []uint64{in.DeptID}), nil
|
|
|
|
case models.ApprovalApproverPosition:
|
|
if len(ids) == 0 {
|
|
return nil, nil
|
|
}
|
|
return positionUsers(uint64(in.Tid), ids), nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func resolveNotifyTargets(scope int8, node Node, in SubmitInput) ([]Approver, error) {
|
|
if !strings.Contains(node.ApproverIDs, ":") {
|
|
return resolveApprovers(scope, node, in)
|
|
}
|
|
typeOrder := make([]int8, 0, 4)
|
|
idsByType := make(map[int8][]uint64)
|
|
for _, part := range strings.Split(node.ApproverIDs, ",") {
|
|
part = strings.TrimSpace(part)
|
|
if part == "" {
|
|
continue
|
|
}
|
|
kv := strings.SplitN(part, ":", 2)
|
|
t, err := strconv.Atoi(strings.TrimSpace(kv[0]))
|
|
if err != nil || t < int(models.ApprovalApproverUser) || t > int(models.ApprovalApproverInitLeader) {
|
|
continue
|
|
}
|
|
at := int8(t)
|
|
if _, ok := idsByType[at]; !ok {
|
|
typeOrder = append(typeOrder, at)
|
|
}
|
|
if len(kv) == 2 {
|
|
if id, err := strconv.ParseUint(strings.TrimSpace(kv[1]), 10, 64); err == nil && id > 0 {
|
|
idsByType[at] = append(idsByType[at], id)
|
|
}
|
|
}
|
|
}
|
|
merged := make([]Approver, 0, 8)
|
|
for _, t := range typeOrder {
|
|
sub := Node{ApproverType: t, ApproverIDs: joinApproverIDs(idsByType[t])}
|
|
approvers, err := resolveApprovers(scope, sub, in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
merged = append(merged, approvers...)
|
|
}
|
|
return dedupeApprovers(merged), nil
|
|
}
|
|
|
|
func joinApproverIDs(ids []uint64) string {
|
|
parts := make([]string, 0, len(ids))
|
|
for _, id := range ids {
|
|
if id > 0 {
|
|
parts = append(parts, strconv.FormatUint(id, 10))
|
|
}
|
|
}
|
|
return strings.Join(parts, ",")
|
|
}
|
|
|
|
func tenantUsers(tid uint64, uids []uint64) []Approver {
|
|
if len(uids) == 0 {
|
|
return nil
|
|
}
|
|
var rows []models.SystemTenantUser
|
|
if _, err := models.Orm.QueryTable(new(models.SystemTenantUser)).
|
|
Filter("tid", tid).
|
|
Filter("uid__in", uids).
|
|
Filter("delete_time__isnull", true).
|
|
All(&rows); err != nil {
|
|
return nil
|
|
}
|
|
out := make([]Approver, 0, len(rows))
|
|
for _, r := range rows {
|
|
out = append(out, Approver{ID: r.Uid, Name: firstNonEmptyPtr(r.Name, r.Account)})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func tenantRoleUsers(tid uint64, roleIDs []uint64) []Approver {
|
|
if len(roleIDs) == 0 {
|
|
return nil
|
|
}
|
|
var rows []models.SystemTenantUser
|
|
if _, err := models.Orm.QueryTable(new(models.SystemTenantUser)).
|
|
Filter("tid", tid).
|
|
Filter("group_id__in", roleIDs).
|
|
Filter("delete_time__isnull", true).
|
|
All(&rows); err != nil {
|
|
return nil
|
|
}
|
|
out := make([]Approver, 0, len(rows))
|
|
for _, r := range rows {
|
|
out = append(out, Approver{ID: r.Uid, Name: firstNonEmptyPtr(r.Name, r.Account)})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func platformUsers(ids []uint64) []Approver {
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
var rows []models.AdminUser
|
|
if _, err := models.Orm.QueryTable(new(models.AdminUser)).
|
|
Filter("id__in", ids).
|
|
Filter("delete_time__isnull", true).
|
|
All(&rows); err != nil {
|
|
return nil
|
|
}
|
|
out := make([]Approver, 0, len(rows))
|
|
for _, r := range rows {
|
|
out = append(out, Approver{ID: r.ID, Name: approverName(r.Name, r.Account)})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func platformRoleUsers(roleIDs []uint64) []Approver {
|
|
if len(roleIDs) == 0 {
|
|
return nil
|
|
}
|
|
var users []models.AdminUser
|
|
if _, err := models.Orm.QueryTable(new(models.AdminUser)).
|
|
Filter("role_id__in", roleIDs).
|
|
Filter("delete_time__isnull", true).
|
|
All(&users); err != nil {
|
|
return nil
|
|
}
|
|
out := make([]Approver, 0, len(users))
|
|
for _, r := range users {
|
|
out = append(out, Approver{ID: r.ID, Name: approverName(r.Name, r.Account)})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func orgLeaders(tid uint64, orgIDs []uint64) []Approver {
|
|
var orgs []models.BackendOrganization
|
|
if _, err := models.Orm.QueryTable(new(models.BackendOrganization)).
|
|
Filter("id__in", orgIDs).
|
|
Filter("delete_time__isnull", true).
|
|
All(&orgs); err != nil {
|
|
return nil
|
|
}
|
|
leaderIDs := make([]uint64, 0, len(orgs))
|
|
for _, o := range orgs {
|
|
if o.LeaderID != nil && *o.LeaderID > 0 {
|
|
leaderIDs = append(leaderIDs, *o.LeaderID)
|
|
}
|
|
}
|
|
if len(leaderIDs) == 0 {
|
|
return nil
|
|
}
|
|
return tenantUsers(tid, leaderIDs)
|
|
}
|
|
|
|
func positionUsers(tid uint64, positionIDs []uint64) []Approver {
|
|
var positions []models.BackendPosition
|
|
if _, err := models.Orm.QueryTable(new(models.BackendPosition)).
|
|
Filter("id__in", positionIDs).
|
|
Filter("delete_time__isnull", true).
|
|
All(&positions); err != nil {
|
|
return nil
|
|
}
|
|
names := make([]string, 0, len(positions))
|
|
for _, p := range positions {
|
|
if strings.TrimSpace(p.PositionName) != "" {
|
|
names = append(names, p.PositionName)
|
|
}
|
|
}
|
|
if len(names) == 0 {
|
|
return nil
|
|
}
|
|
var employees []models.BackendEmployee
|
|
if _, err := models.Orm.QueryTable(new(models.BackendEmployee)).
|
|
Filter("tid", tid).
|
|
Filter("position__in", names).
|
|
Filter("delete_time__isnull", true).
|
|
All(&employees); err != nil {
|
|
return nil
|
|
}
|
|
accounts := make([]string, 0, len(employees))
|
|
for _, e := range employees {
|
|
if strings.TrimSpace(e.Account) != "" {
|
|
accounts = append(accounts, e.Account)
|
|
}
|
|
}
|
|
if len(accounts) == 0 {
|
|
return nil
|
|
}
|
|
var users []models.SystemTenantUser
|
|
if _, err := models.Orm.QueryTable(new(models.SystemTenantUser)).
|
|
Filter("tid", tid).
|
|
Filter("account__in", accounts).
|
|
Filter("delete_time__isnull", true).
|
|
All(&users); err != nil {
|
|
return nil
|
|
}
|
|
out := make([]Approver, 0, len(users))
|
|
for _, u := range users {
|
|
out = append(out, Approver{ID: u.Uid, Name: firstNonEmptyPtr(u.Name, u.Account)})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func approverName(name *string, account string) string {
|
|
if s := firstNonEmptyPtr(name); s != "" {
|
|
return s
|
|
}
|
|
return strings.TrimSpace(account)
|
|
}
|
|
|
|
func firstNonEmptyPtr(vals ...*string) string {
|
|
for _, v := range vals {
|
|
if v != nil && strings.TrimSpace(*v) != "" {
|
|
return strings.TrimSpace(*v)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func dedupeApprovers(in []Approver) []Approver {
|
|
seen := make(map[uint64]bool, len(in))
|
|
out := make([]Approver, 0, len(in))
|
|
for _, a := range in {
|
|
if a.ID == 0 || seen[a.ID] {
|
|
continue
|
|
}
|
|
seen[a.ID] = true
|
|
out = append(out, a)
|
|
}
|
|
return out
|
|
}
|
|
|
|
type AvailableFlow struct {
|
|
FlowID uint64 `json:"flow_id"`
|
|
FlowCode string `json:"flow_code"`
|
|
FlowName string `json:"flow_name"`
|
|
BizType string `json:"biz_type"`
|
|
}
|
|
|
|
func ListAvailableFlows(scope int8, tid uint64, bizType string, userOrgID uint64) ([]AvailableFlow, error) {
|
|
var path []uint64
|
|
if scope == models.ApprovalScopeTenant && userOrgID > 0 {
|
|
path = buildOrgPath(tid, userOrgID)
|
|
}
|
|
out := make([]AvailableFlow, 0)
|
|
if scope == models.ApprovalScopePlatform {
|
|
var rows []models.PlatformApprovalFlow
|
|
_, _ = models.Orm.QueryTable(new(models.PlatformApprovalFlow)).
|
|
Filter("biz_type", bizType).Filter("is_deleted", 0).Filter("status", 1).
|
|
OrderBy("id").All(&rows)
|
|
for _, f := range rows {
|
|
out = append(out, AvailableFlow{FlowID: f.ID, FlowCode: f.FlowCode, FlowName: f.FlowName, BizType: f.BizType})
|
|
}
|
|
return out, nil
|
|
}
|
|
var rows []models.TenantApprovalFlow
|
|
_, _ = models.Orm.QueryTable(new(models.TenantApprovalFlow)).
|
|
Filter("tid", tid).Filter("biz_type", bizType).Filter("is_deleted", 0).Filter("status", 1).
|
|
OrderBy("id").All(&rows)
|
|
for _, f := range rows {
|
|
if !deptMatch(f.ApplyDeptIDs, path) {
|
|
continue
|
|
}
|
|
out = append(out, AvailableFlow{FlowID: f.ID, FlowCode: f.FlowCode, FlowName: f.FlowName, BizType: f.BizType})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func deptMatch(csv string, path []uint64) bool {
|
|
csv = strings.TrimSpace(csv)
|
|
if csv == "" {
|
|
return true
|
|
}
|
|
if len(path) == 0 {
|
|
return false
|
|
}
|
|
inPath := make(map[uint64]bool, len(path))
|
|
for _, id := range path {
|
|
inPath[id] = true
|
|
}
|
|
for _, part := range strings.Split(csv, ",") {
|
|
if v, err := strconv.ParseUint(strings.TrimSpace(part), 10, 64); err == nil && inPath[v] {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func buildOrgPath(tid, orgID uint64) []uint64 {
|
|
if orgID == 0 {
|
|
return nil
|
|
}
|
|
var all []models.BackendOrganization
|
|
_, _ = models.Orm.QueryTable(new(models.BackendOrganization)).
|
|
Filter("tid", tid).Filter("delete_time__isnull", true).
|
|
All(&all, "id", "parent_id")
|
|
parent := make(map[uint64]uint64, len(all))
|
|
for _, o := range all {
|
|
parent[o.ID] = o.ParentID
|
|
}
|
|
out := []uint64{orgID}
|
|
seen := map[uint64]bool{orgID: true}
|
|
cur := orgID
|
|
for {
|
|
p, ok := parent[cur]
|
|
if !ok || p == 0 || seen[p] {
|
|
break
|
|
}
|
|
out = append(out, p)
|
|
seen[p] = true
|
|
cur = p
|
|
}
|
|
return out
|
|
}
|
|
|
|
func Submit(in SubmitInput) (uint64, error) {
|
|
var (
|
|
flow *Flow
|
|
err error
|
|
)
|
|
if strings.TrimSpace(in.FlowCode) != "" {
|
|
flow, err = LoadFlowByCode(in.Scope, in.Tid, in.FlowCode)
|
|
} else {
|
|
flow, err = LoadFlow(in.Scope, in.Tid, in.BizType, in.FlowID)
|
|
}
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
now := time.Now()
|
|
inst := models.ApprovalInstance{
|
|
Scope: in.Scope,
|
|
Tid: in.Tid,
|
|
BizTid: in.BizTid,
|
|
FlowID: flow.ID,
|
|
BizType: in.BizType,
|
|
BizID: in.BizID,
|
|
BizTitle: in.BizTitle,
|
|
SubmitUserID: in.SubmitUID,
|
|
SubmitUserName: in.SubmitName,
|
|
SubmitDeptID: in.DeptID,
|
|
SubmitTime: &now,
|
|
Status: models.ApprovalInstanceRunning,
|
|
}
|
|
if len(in.FormData) > 0 {
|
|
if b, e := json.Marshal(in.FormData); e == nil {
|
|
s := string(b)
|
|
inst.FormData = &s
|
|
}
|
|
}
|
|
instID, err := models.Orm.Insert(&inst)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
start := flow.ByKey[flow.StartKey]
|
|
hasApproval, firstApprovalName, firstApprovalKey := anyApprovalNode(flow)
|
|
notifyFn := func(n *Node) { fireNotifyTasks(uint64(instID), in, n) }
|
|
node, approvers := nextExecutable(flow, in.FormData, start, in, notifyFn)
|
|
if node == nil {
|
|
if hasApproval {
|
|
_, _ = models.Orm.QueryTable(new(models.ApprovalInstance)).
|
|
Filter("id", instID).Update(orm.Params{"is_deleted": 1, "status": models.ApprovalInstanceCanceled})
|
|
return 0, &ErrNoApprover{NodeName: firstApprovalName, NodeKey: firstApprovalKey}
|
|
}
|
|
finishInstance(uint64(instID), models.ApprovalInstanceApproved)
|
|
return uint64(instID), nil
|
|
}
|
|
|
|
if err := createTasks(uint64(instID), in, *node, approvers); err != nil {
|
|
return uint64(instID), err
|
|
}
|
|
if _, err := models.Orm.QueryTable(new(models.ApprovalInstance)).
|
|
Filter("id", instID).Update(orm.Params{"current_node": node.NodeKey}); err != nil {
|
|
return uint64(instID), err
|
|
}
|
|
return uint64(instID), nil
|
|
}
|
|
|
|
func anyApprovalNode(flow *Flow) (bool, string, string) {
|
|
for i := range flow.Nodes {
|
|
if flow.Nodes[i].NodeType == models.ApprovalNodeApproval {
|
|
return true, flow.Nodes[i].NodeName, flow.Nodes[i].NodeKey
|
|
}
|
|
}
|
|
return false, "", ""
|
|
}
|
|
|
|
func advanceFrom(flow *Flow, formData map[string]interface{}, cur *Node, onNotify ...func(*Node)) *Node {
|
|
visited := make(map[string]bool)
|
|
node := pickNext(flow, formData, cur)
|
|
for node != nil {
|
|
if visited[node.NodeKey] {
|
|
return nil
|
|
}
|
|
visited[node.NodeKey] = true
|
|
switch node.NodeType {
|
|
case models.ApprovalNodeApproval:
|
|
return node
|
|
case models.ApprovalNodeEnd:
|
|
return nil
|
|
case models.ApprovalNodeNotify:
|
|
if len(onNotify) > 0 && onNotify[0] != nil {
|
|
onNotify[0](node)
|
|
}
|
|
node = pickNext(flow, formData, node)
|
|
default:
|
|
node = pickNext(flow, formData, node)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func pickNext(flow *Flow, formData map[string]interface{}, node *Node) *Node {
|
|
if node == nil {
|
|
return nil
|
|
}
|
|
outs := splitKeys(node.NextKeys)
|
|
if len(outs) == 0 {
|
|
return nil
|
|
}
|
|
if node.NodeType != models.ApprovalNodeCondition {
|
|
return flow.ByKey[outs[0]]
|
|
}
|
|
var def *Node
|
|
for _, k := range outs {
|
|
t := flow.ByKey[k]
|
|
if t == nil {
|
|
continue
|
|
}
|
|
if strings.TrimSpace(t.ConditionExpr) == "" {
|
|
if def == nil {
|
|
def = t
|
|
}
|
|
continue
|
|
}
|
|
if evalCondition(t.ConditionExpr, formData) {
|
|
return t
|
|
}
|
|
}
|
|
return def
|
|
}
|
|
|
|
func nextExecutable(flow *Flow, formData map[string]interface{}, cur *Node, in SubmitInput, onNotify ...func(*Node)) (*Node, []Approver) {
|
|
node := advanceFrom(flow, formData, cur, onNotify...)
|
|
visited := make(map[string]bool)
|
|
for node != nil {
|
|
if visited[node.NodeKey] {
|
|
break
|
|
}
|
|
visited[node.NodeKey] = true
|
|
approvers, err := resolveApprovers(in.Scope, *node, in)
|
|
if err == nil {
|
|
approvers = dedupeApprovers(approvers)
|
|
if len(approvers) > 0 {
|
|
return node, approvers
|
|
}
|
|
}
|
|
node = advanceFrom(flow, formData, node, onNotify...)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func splitKeys(raw string) []string {
|
|
out := make([]string, 0, 4)
|
|
for _, p := range strings.Split(raw, ",") {
|
|
if s := strings.TrimSpace(p); s != "" {
|
|
out = append(out, s)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func instanceFormData(inst *models.ApprovalInstance) map[string]interface{} {
|
|
if inst == nil || inst.FormData == nil || strings.TrimSpace(*inst.FormData) == "" {
|
|
return nil
|
|
}
|
|
m := make(map[string]interface{})
|
|
if err := json.Unmarshal([]byte(*inst.FormData), &m); err != nil {
|
|
return nil
|
|
}
|
|
return m
|
|
}
|
|
|
|
func submitFromInstance(inst *models.ApprovalInstance, formData map[string]interface{}) SubmitInput {
|
|
return SubmitInput{
|
|
Scope: inst.Scope,
|
|
Tid: inst.Tid,
|
|
BizTid: inst.BizTid,
|
|
BizType: inst.BizType,
|
|
BizID: inst.BizID,
|
|
SubmitUID: inst.SubmitUserID,
|
|
SubmitName: inst.SubmitUserName,
|
|
DeptID: inst.SubmitDeptID,
|
|
FormData: formData,
|
|
}
|
|
}
|
|
|
|
func createTasks(instID uint64, in SubmitInput, node Node, approvers []Approver) error {
|
|
timeout := node.TimeoutHours
|
|
if timeout <= 0 {
|
|
timeout = 48
|
|
}
|
|
deadline := time.Now().Add(time.Duration(timeout) * time.Hour)
|
|
multiMode := node.MultiMode
|
|
if multiMode != models.ApprovalMultiAll {
|
|
multiMode = models.ApprovalMultiAny
|
|
}
|
|
|
|
tasks := make([]models.ApprovalTask, 0, len(approvers))
|
|
for _, a := range approvers {
|
|
tasks = append(tasks, models.ApprovalTask{
|
|
InstanceID: instID,
|
|
Scope: in.Scope,
|
|
Tid: in.Tid,
|
|
BizType: in.BizType,
|
|
BizID: in.BizID,
|
|
NodeKey: node.NodeKey,
|
|
NodeName: node.NodeName,
|
|
Sort: node.Sort,
|
|
ApproverID: a.ID,
|
|
ApproverName: a.Name,
|
|
MultiMode: multiMode,
|
|
Status: models.ApprovalTaskPending,
|
|
Deadline: &deadline,
|
|
})
|
|
}
|
|
_, err := models.Orm.InsertMulti(len(tasks), tasks)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if in.Scope == models.ApprovalScopeTenant {
|
|
for _, a := range approvers {
|
|
approverID, nodeName := a.ID, node.NodeName
|
|
go func(uid uint64, name string) {
|
|
res := wechatmini.PushTenantNotice(uid, "您有新的审批待办", "审批事项:"+name+",请及时处理。")
|
|
if res.Sent > 0 {
|
|
logs.Info("[wechat-mini] 审批待办已推送订阅消息: user=%d sent=%d", uid, res.Sent)
|
|
}
|
|
for _, e := range res.Errors {
|
|
logs.Info("[wechat-mini] 订阅消息推送失败: user=%d err=%v", uid, e)
|
|
}
|
|
}(approverID, nodeName)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func fireNotifyTasks(instID uint64, in SubmitInput, notifyNode *Node) {
|
|
recipients, _ := resolveNotifyTargets(in.Scope, *notifyNode, in)
|
|
recipients = dedupeApprovers(recipients)
|
|
if len(recipients) == 0 {
|
|
return
|
|
}
|
|
now := time.Now()
|
|
tasks := make([]models.ApprovalTask, 0, len(recipients))
|
|
for _, r := range recipients {
|
|
tasks = append(tasks, models.ApprovalTask{
|
|
InstanceID: instID,
|
|
Scope: in.Scope,
|
|
Tid: in.Tid,
|
|
BizType: in.BizType,
|
|
BizID: in.BizID,
|
|
NodeKey: notifyNode.NodeKey,
|
|
NodeName: notifyNode.NodeName,
|
|
Sort: notifyNode.Sort,
|
|
ApproverID: r.ID,
|
|
ApproverName: r.Name,
|
|
Status: models.ApprovalTaskNotified,
|
|
FinishTime: &now,
|
|
})
|
|
}
|
|
_, _ = models.Orm.InsertMulti(len(tasks), tasks)
|
|
}
|
|
|
|
func finishInstance(instID uint64, status int8) {
|
|
now := time.Now()
|
|
_, _ = models.Orm.QueryTable(new(models.ApprovalInstance)).
|
|
Filter("id", instID).Update(orm.Params{
|
|
"status": status,
|
|
"finish_time": now,
|
|
"update_time": now,
|
|
})
|
|
SyncBizResult(instID, status)
|
|
}
|
|
|
|
const (
|
|
reimburseStatusApproved int8 = 2
|
|
reimburseStatusRejected int8 = 3
|
|
reimburseStatusWithdrawn int8 = 4
|
|
)
|
|
|
|
const BizTypeLegacyOA = "oa"
|
|
|
|
func IsReimburseBizType(bizType string) bool {
|
|
switch strings.TrimSpace(bizType) {
|
|
case models.ApprovalBizReimburse, BizTypeLegacyOA:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func SyncBizResult(instID uint64, status int8) {
|
|
if instID == 0 {
|
|
return
|
|
}
|
|
var inst models.ApprovalInstance
|
|
if err := models.Orm.QueryTable(new(models.ApprovalInstance)).
|
|
Filter("id", instID).One(&inst); err != nil {
|
|
return
|
|
}
|
|
if inst.BizID == 0 {
|
|
return
|
|
}
|
|
if strings.HasPrefix(inst.BizType, "erp_") {
|
|
erp.SyncErpDocResult(inst.BizType, inst.BizID, bizTenantID(&inst), status)
|
|
return
|
|
}
|
|
if inst.BizType == models.ApprovalBizAttendanceFix {
|
|
SyncAttendanceFixResult(&inst, status)
|
|
return
|
|
}
|
|
if !IsReimburseBizType(inst.BizType) {
|
|
return
|
|
}
|
|
syncReimburseResult(&inst, status)
|
|
}
|
|
|
|
func bizTenantID(inst *models.ApprovalInstance) uint64 {
|
|
if inst.Scope == models.ApprovalScopePlatform && inst.BizTid != 0 {
|
|
return inst.BizTid
|
|
}
|
|
return inst.Tid
|
|
}
|
|
|
|
func syncReimburseResult(inst *models.ApprovalInstance, status int8) {
|
|
var bizStatus, action int8
|
|
switch status {
|
|
case models.ApprovalInstanceApproved:
|
|
bizStatus, action = reimburseStatusApproved, models.ApprovalActionApprove
|
|
case models.ApprovalInstanceRejected:
|
|
bizStatus, action = reimburseStatusRejected, models.ApprovalActionReject
|
|
case models.ApprovalInstanceWithdrawn:
|
|
bizStatus, action = reimburseStatusWithdrawn, models.ApprovalActionWithdraw
|
|
default:
|
|
return
|
|
}
|
|
|
|
tid := bizTenantID(inst)
|
|
if tid == 0 {
|
|
return
|
|
}
|
|
var row models.BackendReimbursement
|
|
if err := models.Orm.QueryTable(new(models.BackendReimbursement)).
|
|
Filter("id", inst.BizID).Filter("tid", tid).
|
|
Filter("is_deleted", 0).One(&row); err != nil {
|
|
return
|
|
}
|
|
if row.Status == bizStatus {
|
|
return
|
|
}
|
|
|
|
now := time.Now()
|
|
if _, err := models.Orm.QueryTable(new(models.BackendReimbursement)).
|
|
Filter("id", inst.BizID).Filter("tid", tid).
|
|
Update(orm.Params{"status": bizStatus, "update_time": now}); err != nil {
|
|
return
|
|
}
|
|
|
|
actorID, actorName, comment := lastApprovalActor(inst)
|
|
_, _ = models.Orm.Insert(&models.BackendApprovalRecord{
|
|
Tid: tid, UID: approvalUint64Ptr(actorID), ReimbursementID: inst.BizID,
|
|
ApproverID: actorID, ApproverName: actorName, Action: action,
|
|
Comment: approvalStringPtr(comment),
|
|
})
|
|
}
|
|
|
|
func lastApprovalActor(inst *models.ApprovalInstance) (uint64, string, string) {
|
|
var task models.ApprovalTask
|
|
if err := models.Orm.QueryTable(new(models.ApprovalTask)).
|
|
Filter("instance_id", inst.ID).
|
|
Filter("status__in", models.ApprovalTaskApproved, models.ApprovalTaskRejected).
|
|
OrderBy("-finish_time", "-id").One(&task); err == nil {
|
|
return task.ApproverID, task.ApproverName, task.Comment
|
|
}
|
|
return inst.SubmitUserID, inst.SubmitUserName, ""
|
|
}
|
|
|
|
func approvalUint64Ptr(v uint64) *uint64 {
|
|
if v == 0 {
|
|
return nil
|
|
}
|
|
return &v
|
|
}
|
|
|
|
func approvalStringPtr(s string) *string {
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
return &s
|
|
}
|
|
|
|
type ApproveInput struct {
|
|
ApproverID uint64
|
|
Pass bool
|
|
Comment string
|
|
BizTid uint64
|
|
}
|
|
|
|
type ApproveResult struct {
|
|
InstanceID uint64
|
|
InstanceStat int8
|
|
CurrentNode string
|
|
Finished bool
|
|
Message string
|
|
}
|
|
|
|
func Approve(taskID uint64, in ApproveInput) (*ApproveResult, error) {
|
|
var task models.ApprovalTask
|
|
if err := models.Orm.QueryTable(new(models.ApprovalTask)).
|
|
Filter("id", taskID).
|
|
Filter("is_deleted", 0).
|
|
One(&task); err != nil {
|
|
return nil, err
|
|
}
|
|
if task.Status != models.ApprovalTaskPending {
|
|
return nil, fmt.Errorf("该审批任务已处理")
|
|
}
|
|
if task.ApproverID != in.ApproverID {
|
|
return nil, fmt.Errorf("您无权审批该任务")
|
|
}
|
|
|
|
var inst models.ApprovalInstance
|
|
if err := models.Orm.QueryTable(new(models.ApprovalInstance)).
|
|
Filter("id", task.InstanceID).
|
|
Filter("is_deleted", 0).
|
|
One(&inst); err != nil {
|
|
return nil, err
|
|
}
|
|
if inst.Status != models.ApprovalInstanceRunning {
|
|
return nil, fmt.Errorf("该单据当前状态不可审批")
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
if !in.Pass {
|
|
_, err := models.Orm.QueryTable(new(models.ApprovalTask)).
|
|
Filter("id", taskID).
|
|
Update(orm.Params{
|
|
"status": models.ApprovalTaskRejected,
|
|
"action": models.ApprovalActionReject,
|
|
"comment": in.Comment,
|
|
"finish_time": now,
|
|
"update_time": now,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_, _ = models.Orm.QueryTable(new(models.ApprovalTask)).
|
|
Filter("instance_id", task.InstanceID).
|
|
Filter("node_key", task.NodeKey).
|
|
Filter("status", models.ApprovalTaskPending).
|
|
Filter("id__neq", taskID).
|
|
Update(orm.Params{"status": models.ApprovalTaskSkipped, "update_time": now})
|
|
|
|
finishInstance(task.InstanceID, models.ApprovalInstanceRejected)
|
|
return &ApproveResult{
|
|
InstanceID: task.InstanceID,
|
|
InstanceStat: models.ApprovalInstanceRejected,
|
|
Finished: true,
|
|
Message: "已驳回",
|
|
}, nil
|
|
}
|
|
|
|
_, err := models.Orm.QueryTable(new(models.ApprovalTask)).
|
|
Filter("id", taskID).
|
|
Update(orm.Params{
|
|
"status": models.ApprovalTaskApproved,
|
|
"action": models.ApprovalActionApprove,
|
|
"comment": in.Comment,
|
|
"finish_time": now,
|
|
"update_time": now,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if task.MultiMode == models.ApprovalMultiAny {
|
|
_, _ = models.Orm.QueryTable(new(models.ApprovalTask)).
|
|
Filter("instance_id", task.InstanceID).
|
|
Filter("node_key", task.NodeKey).
|
|
Filter("status", models.ApprovalTaskPending).
|
|
Update(orm.Params{"status": models.ApprovalTaskSkipped, "update_time": now})
|
|
} else {
|
|
remaining, _ := models.Orm.QueryTable(new(models.ApprovalTask)).
|
|
Filter("instance_id", task.InstanceID).
|
|
Filter("node_key", task.NodeKey).
|
|
Filter("status", models.ApprovalTaskPending).
|
|
Count()
|
|
if remaining > 0 {
|
|
return &ApproveResult{
|
|
InstanceID: task.InstanceID,
|
|
InstanceStat: models.ApprovalInstanceRunning,
|
|
CurrentNode: task.NodeKey,
|
|
Message: fmt.Sprintf("已通过,等待其余 %d 位审批人", remaining),
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
return advance(task.InstanceID, task.NodeKey, in)
|
|
}
|
|
|
|
func advance(instID uint64, currentKey string, in ApproveInput) (*ApproveResult, error) {
|
|
var inst models.ApprovalInstance
|
|
if err := models.Orm.QueryTable(new(models.ApprovalInstance)).
|
|
Filter("id", instID).Filter("is_deleted", 0).One(&inst); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
flow, err := LoadFlow(inst.Scope, inst.Tid, inst.BizType, inst.FlowID)
|
|
if err != nil {
|
|
finishInstance(instID, models.ApprovalInstanceApproved)
|
|
return &ApproveResult{
|
|
InstanceID: instID,
|
|
InstanceStat: models.ApprovalInstanceApproved,
|
|
Finished: true,
|
|
Message: "审批已通过",
|
|
}, nil
|
|
}
|
|
|
|
formData := instanceFormData(&inst)
|
|
si := submitFromInstance(&inst, formData)
|
|
cur := flow.ByKey[currentKey]
|
|
notifyFn := func(n *Node) { fireNotifyTasks(instID, si, n) }
|
|
node, approvers := nextExecutable(flow, formData, cur, si, notifyFn)
|
|
if node == nil {
|
|
finishInstance(instID, models.ApprovalInstanceApproved)
|
|
return &ApproveResult{
|
|
InstanceID: instID,
|
|
InstanceStat: models.ApprovalInstanceApproved,
|
|
Finished: true,
|
|
Message: "审批已全部通过",
|
|
}, nil
|
|
}
|
|
|
|
if err := createTasks(instID, si, *node, approvers); err != nil {
|
|
return nil, err
|
|
}
|
|
now := time.Now()
|
|
if _, err := models.Orm.QueryTable(new(models.ApprovalInstance)).
|
|
Filter("id", instID).Update(orm.Params{"current_node": node.NodeKey, "update_time": now}); err != nil {
|
|
return nil, err
|
|
}
|
|
return &ApproveResult{
|
|
InstanceID: instID,
|
|
InstanceStat: models.ApprovalInstanceRunning,
|
|
CurrentNode: node.NodeKey,
|
|
Message: "已流转至「" + node.NodeName + "」",
|
|
}, nil
|
|
}
|
|
|
|
func evalCondition(expr string, data map[string]interface{}) bool {
|
|
expr = strings.TrimSpace(expr)
|
|
if expr == "" {
|
|
return true
|
|
}
|
|
for _, orPart := range strings.Split(expr, "||") {
|
|
if evalAnd(orPart, data) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func evalAnd(part string, data map[string]interface{}) bool {
|
|
for _, atom := range strings.Split(part, "&&") {
|
|
if !evalAtom(atom, data) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
var condOps = []string{">=", "<=", "!=", "==", ">", "<"}
|
|
|
|
func evalAtom(s string, data map[string]interface{}) bool {
|
|
s = strings.TrimSpace(s)
|
|
if s == "" {
|
|
return true
|
|
}
|
|
for _, op := range condOps {
|
|
idx := strings.Index(s, op)
|
|
if idx <= 0 {
|
|
continue
|
|
}
|
|
lv := resolveCondVal(strings.TrimSpace(s[:idx]), data)
|
|
rv := parseCondLiteral(strings.TrimSpace(s[idx+len(op):]))
|
|
return compareCond(lv, op, rv)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func resolveCondVal(key string, data map[string]interface{}) interface{} {
|
|
if data == nil {
|
|
return nil
|
|
}
|
|
if v, ok := data[key]; ok {
|
|
return v
|
|
}
|
|
var cur interface{} = data
|
|
for _, seg := range strings.Split(key, ".") {
|
|
m, ok := cur.(map[string]interface{})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
v, ok := m[seg]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
cur = v
|
|
}
|
|
return cur
|
|
}
|
|
|
|
func parseCondLiteral(raw string) interface{} {
|
|
raw = strings.TrimSpace(raw)
|
|
if len(raw) >= 2 {
|
|
if (raw[0] == '"' || raw[0] == '\'') && raw[len(raw)-1] == raw[0] {
|
|
return raw[1 : len(raw)-1]
|
|
}
|
|
}
|
|
if n, err := strconv.ParseFloat(raw, 64); err == nil {
|
|
return n
|
|
}
|
|
return raw
|
|
}
|
|
|
|
func toFloat(v interface{}) (float64, bool) {
|
|
switch x := v.(type) {
|
|
case float64:
|
|
return x, true
|
|
case float32:
|
|
return float64(x), true
|
|
case int:
|
|
return float64(x), true
|
|
case int64:
|
|
return float64(x), true
|
|
case uint64:
|
|
return float64(x), true
|
|
case json.Number:
|
|
if f, e := x.Float64(); e == nil {
|
|
return f, true
|
|
}
|
|
case string:
|
|
if f, e := strconv.ParseFloat(strings.TrimSpace(x), 64); e == nil {
|
|
return f, true
|
|
}
|
|
}
|
|
return 0, false
|
|
}
|
|
|
|
func compareCond(l interface{}, op string, r interface{}) bool {
|
|
lf, lok := toFloat(l)
|
|
rf, rok := toFloat(r)
|
|
if lok && rok {
|
|
switch op {
|
|
case ">":
|
|
return lf > rf
|
|
case "<":
|
|
return lf < rf
|
|
case ">=":
|
|
return lf >= rf
|
|
case "<=":
|
|
return lf <= rf
|
|
case "==":
|
|
return lf == rf
|
|
case "!=":
|
|
return lf != rf
|
|
}
|
|
}
|
|
ls := condString(l)
|
|
rs := condString(r)
|
|
switch op {
|
|
case "==":
|
|
return ls == rs
|
|
case "!=":
|
|
return ls != rs
|
|
}
|
|
return false
|
|
}
|
|
|
|
func condString(v interface{}) string {
|
|
if v == nil {
|
|
return ""
|
|
}
|
|
switch x := v.(type) {
|
|
case string:
|
|
return x
|
|
case bool:
|
|
if x {
|
|
return "true"
|
|
}
|
|
return "false"
|
|
case float64:
|
|
return strconv.FormatFloat(x, 'f', -1, 64)
|
|
default:
|
|
return fmt.Sprintf("%v", v)
|
|
}
|
|
}
|
|
|
|
func SortNodes(nodes []Node) {
|
|
sort.SliceStable(nodes, func(i, j int) bool { return nodes[i].Sort < nodes[j].Sort })
|
|
}
|