package admin

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

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) QueryProjectFeeList(c *gin.Context) {
	var info request.SearchExpenses
	err := c.ShouldBindJSON(&info)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryProjectFeeList ====== " + err.Error())
		return
	}
	list, total, err := expensesService.QueryProjectFeeList(info)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryProjectFeeList ====== " + 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) QueryProjectMonthFee(c *gin.Context) {
	monthTime := c.Query("monthTime")
	yearTime := c.Query("yearTime")
	code := c.Query("code")
	fee, err := expensesService.QueryProjectMonthFee(monthTime, yearTime, code)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryProjectMonthFee ====== " + err.Error())
		return
	}
	response.OkWithData(fee, c)
}

func (ea *ExpensesApi) QueryMonthExpenses(c *gin.Context) {
	monthTime := c.Query("monthTime")
	yearTime := c.Query("yearTime")
	genre := c.Query("genre")
	genreId, _ := strconv.Atoi(genre)
	fee, err := expensesService.QueryMonthExpenses(monthTime, yearTime, genreId)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryMonthExpenses ====== " + err.Error())
		return
	}
	response.OkWithData(fee, 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) CreateProjectFee(c *gin.Context) {
	var projectFee dao.ProjectFee
	err := c.ShouldBindJSON(&projectFee)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateProjectFee ====== " + err.Error())
		return
	}
	err = expensesService.CreateProjectFee(projectFee)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateProjectFee ====== " + 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) UpdateProjectFee(c *gin.Context) {
	var projectFee dao.ProjectFee
	err := c.ShouldBindJSON(&projectFee)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateProjectFee ====== " + err.Error())
		return
	}
	err = expensesService.UpdateProjectFee(projectFee)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateProjectFee ====== " + 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)
}

func (ea *ExpensesApi) DeleteProjectFee(c *gin.Context) {
	var projectFee dao.ProjectFee
	err := c.ShouldBindJSON(&projectFee)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteProjectFee ====== " + err.Error())
		return
	}
	err = expensesService.DeleteProjectFee(projectFee)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteProjectFee ====== " + err.Error())
		return
	}
	response.OkWithMessage("删除成功", c)
}
