package workflow

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

type ApproverApi struct{}

func (aa *ApproverApi) QueryAllApprover(c *gin.Context) {
	approvers, err := approverService.QueryAllApprover()
	if err != nil {
		response.FailWithMessage("查询失败", c)
		global.GVA_LOG.Error("QueryAllApprover ==" + err.Error())
		return
	}
	response.OkWithData(approvers, c)
}

func (aa *ApproverApi) QueryApproverList(c *gin.Context) {
	var info request.PageInfo
	err := c.ShouldBindJSON(&info)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryApproverList ====== " + err.Error())
		return
	}
	list, total, err := approverService.QueryApproverList(info)
	if err != nil {
		response.FailWithMessage("查询失败", c)
		global.GVA_LOG.Error("QueryApproverList ====== " + err.Error())
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     info.Page,
		PageSize: info.PageSize,
	}, "获取成功", c)
}

func (aa *ApproverApi) CreateApprover(c *gin.Context) {
	var approver dao.Approver
	err := c.ShouldBindJSON(&approver)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateApprover ====== " + err.Error())
		return
	}
	err = approverService.CreateApprover(approver)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateApprover ====== " + err.Error())
		return
	}
	response.OkWithMessage("新增成功", c)
}

func (aa *ApproverApi) UpdateApprover(c *gin.Context) {
	var approver dao.Approver
	err := c.ShouldBindJSON(&approver)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateApprover ====== " + err.Error())
		return
	}
	err = approverService.UpdateApprover(approver)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateApprover ====== " + err.Error())
		return
	}
	response.OkWithMessage("修改成功", c)
}

func (aa *ApproverApi) DeleteApprover(c *gin.Context) {
	var approver dao.Approver
	err := c.ShouldBindJSON(&approver)
	if err != nil {
		response.FailWithMessage("参数错误", c)
		global.GVA_LOG.Error("DeleteApprover ====== " + err.Error())
		return
	}
	err = approverService.DeleteApprover(approver)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteApprover ====== " + err.Error())
		return
	}
	response.OkWithMessage("删除成功", c)
}
