package workflow

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

type ProcessApi struct{}

func (pa *ProcessApi) QueryAllProcess(c *gin.Context) {
	process, err := processService.QueryAllProcess()
	if err != nil {
		response.FailWithMessage("查询失败", c)
		global.GVA_LOG.Error("QueryAllProcess ====== " + err.Error())
		return
	}
	response.OkWithData(process, c)
}

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

func (pa *ProcessApi) CreateProcess(c *gin.Context) {
	var process dao.Process
	err := c.ShouldBindJSON(&process)
	if err != nil {
		response.FailWithMessage("参数解析失败", c)
		global.GVA_LOG.Error("CreateProcess ====== " + err.Error())
		return
	}
	err = processService.CreateProcess(process)
	if err != nil {
		response.FailWithMessage("创建失败", c)
		global.GVA_LOG.Error("CreateProcess ====== " + err.Error())
		return
	}
	response.OkWithMessage("创建成功", c)
}

func (pa *ProcessApi) UpdateProcess(c *gin.Context) {
	var process dao.Process
	err := c.ShouldBindJSON(&process)
	if err != nil {
		response.FailWithMessage("参数解析失败", c)
		global.GVA_LOG.Error("UpdateProcess ====== " + err.Error())
		return
	}
	err = processService.UpdateProcess(process)
	if err != nil {
		response.FailWithMessage("更新失败", c)
		global.GVA_LOG.Error("UpdateProcess ====== " + err.Error())
		return
	}
	response.OkWithMessage("更新成功", c)
}

func (pa *ProcessApi) DeleteProcess(c *gin.Context) {
	var process dao.Process
	err := c.ShouldBindJSON(&process)
	if err != nil {
		response.FailWithMessage("参数解析失败", c)
		global.GVA_LOG.Error("UpdateProcess ====== " + err.Error())
		return
	}
	err = processService.DeleteProcess(process)
	if err != nil {
		response.FailWithMessage("删除失败", c)
		global.GVA_LOG.Error("DeleteProcess ====== " + err.Error())
		return
	}
	response.OkWithMessage("删除成功", c)
}
