package workflow

import (
	"github.com/gin-gonic/gin"
	"server/dao"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
	"strconv"
)

type NodeApi struct{}

func (na *NodeApi) QueryAllNode(c *gin.Context) {
	nodes, err := nodeService.QueryAllNode()
	if err != nil {
		response.FailWithMessage("查询失败", c)
		global.GVA_LOG.Error("QueryAllNode ==" + err.Error())
		return
	}
	response.OkWithData(nodes, c)
}

func (na *NodeApi) QueryNodeList(c *gin.Context) {
	var info request.PageInfo
	err := c.ShouldBindJSON(&info)
	if err != nil {
		response.FailWithMessage("参数错误", c)
		global.GVA_LOG.Error("QueryNodeList ====== " + err.Error())
		return
	}
	list, total, err := nodeService.QueryNodeList(info)
	if err != nil {
		response.FailWithMessage("查询失败", c)
		global.GVA_LOG.Error("QueryNodeList ====== " + err.Error())
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     info.Page,
		PageSize: info.PageSize,
	}, "获取成功", c)
}

func (na *NodeApi) QueryNodeByApproverId(c *gin.Context) {
	id := c.Param("approverId")
	approverId, err := strconv.Atoi(id)
	if err != nil {
		response.FailWithMessage("参数错误", c)
		global.GVA_LOG.Error("QueryNodeByApproverId ====== " + err.Error())
		return
	}
	node, err := nodeService.QueryNodeByApproverId(approverId)
	if err != nil {
		response.FailWithMessage("查询失败", c)
		global.GVA_LOG.Error("QueryNodeByApproverId ====== " + err.Error())
		return
	}
	response.OkWithData(node, c)
}

func (na *NodeApi) CreateNode(c *gin.Context) {
	var node dao.Node
	err := c.ShouldBindJSON(&node)
	if err != nil {
		response.FailWithMessage("参数错误", c)
		global.GVA_LOG.Error("CreateNode ====== " + err.Error())
		return
	}
	err = nodeService.CreateNode(node)
	if err != nil {
		response.FailWithMessage("创建失败", c)
		global.GVA_LOG.Error("CreateNode ====== " + err.Error())
		return
	}
	response.OkWithMessage("创建成功", c)
}

func (na *NodeApi) UpdateNode(c *gin.Context) {
	var node dao.Node
	err := c.ShouldBindJSON(&node)
	if err != nil {
		response.FailWithMessage("参数错误", c)
		global.GVA_LOG.Error("UpdateNode ====== " + err.Error())
		return
	}
	err = nodeService.UpdateNode(node)
	if err != nil {
		response.FailWithMessage("更新失败", c)
		global.GVA_LOG.Error("UpdateNode ====== " + err.Error())
		return
	}
	response.OkWithMessage("更新成功", c)
}

func (na *NodeApi) DeleteNode(c *gin.Context) {
	var node dao.Node
	err := c.ShouldBindJSON(&node)
	if err != nil {
		response.FailWithMessage("参数错误", c)
		global.GVA_LOG.Error("UpdateNode ====== " + err.Error())
		return
	}
	err = nodeService.DeleteNode(node)
	if err != nil {
		response.FailWithMessage("删除失败", c)
		global.GVA_LOG.Error("DeleteNode ====== " + err.Error())
		return
	}
	response.OkWithMessage("删除成功", c)
}
