package admin

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

type ExpensesApi struct{}

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

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

func (ea *ExpensesApi) QuerySurplus(c *gin.Context) {
	surplus, err := expensesService.QuerySurplus()
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QuerySurplus ====== " + err.Error())
		return
	}
	response.OkWithData(surplus, c)
}

func (ea *ExpensesApi) CreateExpenses(c *gin.Context) {
	var expenses dao.Expenses
	err := c.ShouldBindJSON(&expenses)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateExpense ====== " + err.Error())
		return
	}
	err = expensesService.CreateExpenses(expenses)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateExpense ====== " + err.Error())
		return
	}
	response.OkWithMessage("新增成功", c)
}

func (ea *ExpensesApi) UpdateExpenses(c *gin.Context) {
	var expenses dao.Expenses
	err := c.ShouldBindJSON(&expenses)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateExpenses ====== " + err.Error())
		return
	}
	err = expensesService.UpdateExpenses(expenses)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateExpenses ====== " + err.Error())
		return
	}
	response.OkWithMessage("修改成功", c)
}

func (ea *ExpensesApi) DeleteExpenses(c *gin.Context) {
	var expenses dao.Expenses
	err := c.ShouldBindJSON(&expenses)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteExpenses ====== " + err.Error())
		return
	}
	err = expensesService.DeleteExpenses(expenses)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteExpenses ====== " + err.Error())
		return
	}
	response.OkWithMessage("修改成功", c)
}
