package workflow

import (
	"fmt"
	"server/dao"
	"server/model/common/request"
)

type NodeService struct{}

func (ns *NodeService) QueryAllNode() (nodes []dao.Node, err error) {
	return dao.QueryAllNode()
}

func (ns *NodeService) QueryNodeList(info request.PageInfo) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	nodes, total, err := dao.QueryNodeList(limit, offset)
	return nodes, total, err
}

func (ns *NodeService) QueryNodeById(id int) (node dao.Node, err error) {
	return dao.QueryNodeById(id)
}

func (ns *NodeService) QueryNodeByApproverId(approverId int) (nodes []dao.Node, err error) {
	return dao.QueryNodeByApproverId(approverId)
}

func (ns *NodeService) CreateNode(node dao.Node) error {
	return node.CreateNode()
}

func (ns *NodeService) UpdateNode(node dao.Node) error {
	nodes, err := ns.QueryNodeByApproverId(int(node.ID))
	if err != nil {
		return err
	}
	if len(nodes) > 0 {
		return fmt.Errorf("节点被使用过")
	}
	return node.UpdateNode()
}

func (ns *NodeService) DeleteNode(node dao.Node) error {
	nodes, err := ns.QueryNodeByApproverId(int(node.ID))
	if err != nil {
		return err
	}
	if len(nodes) > 0 {
		return fmt.Errorf("节点被使用")
	}
	return node.DeleteNode()
}
