package workflow

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

type ApproverService struct {
}

func (as *ApproverService) QueryAllApprover() (approvers []dao.Approver, err error) {
	return dao.QueryAllApprover()
}

func (as *ApproverService) QueryApproverList(info request.PageInfo) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	approvers, total, err := dao.QueryApproverList(limit, offset)
	return approvers, total, err
}

func (as *ApproverService) CreateApprover(approver dao.Approver) error {
	return approver.CreateApprover()
}

func (as *ApproverService) UpdateApprover(approver dao.Approver) error {
	nodes, err := dao.QueryNodeByApproverId(int(approver.ID))
	if err != nil {
		return err
	}
	if len(nodes) > 0 {
		return fmt.Errorf("该数据被使用过")
	}

	return approver.UpdateApprover()
}

func (as *ApproverService) DeleteApprover(approver dao.Approver) error {
	nodes, err := dao.QueryNodeByApproverId(int(approver.ID))
	if err != nil {
		return err
	}
	if len(nodes) > 0 {
		return fmt.Errorf("该数据被使用")
	}
	return approver.DeleteApprover()
}
