package admin

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

type ExpensesService struct{}

func (es *ExpensesService) QueryExpensesList(info request.SearchExpenses) ([]dao.Expenses, int64, error) {
	limit := info.PageInfo.PageSize
	offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
	return dao.QueryExpensesList(limit, offset, info.Genre, info.Reimburser, info.DayTime, info.MonthTime, info.YearTime)
}

func (es *ExpensesService) QueryProjectFeeList(info request.SearchExpenses) ([]dao.ProjectFee, int64, error) {
	limit := info.PageInfo.PageSize
	offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
	return dao.QueryProjectFeeList(limit, offset, info.Genre, info.Reimburser, info.ProjectCode, info.DayTime, info.MonthTime, info.YearTime)
}

func (es *ExpensesService) QueryExpensesSumByPeople(info request.SearchExpenses) ([]dao.ExpensesResult, int64, error) {
	limit := info.PageInfo.PageSize
	offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
	expenses, total, err := dao.QueryExpensesSumByPeople(limit, offset, info.Genre, info.Reimburser, info.DayTime, info.MonthTime, info.YearTime)
	for i, expens := range expenses {
		list, _, err := dao.QueryExpensesList(1000, 0, info.Genre, expens.Reimburser, info.DayTime, info.MonthTime, info.YearTime)
		if err != nil {
			return expenses, total, err
		}
		deps, err := dao.QueryDepById(expens.DepartmentId)
		if err != nil {
			return expenses, total, err
		}
		user, err := dao.QueryUserByUserId(uint(expens.Reimburser))
		if err != nil {
			return expenses, total, err
		}
		expenses[i].Expenses = list
		expenses[i].Department = deps
		expenses[i].SysUser = user

		expenses[i].DepositAmountSum = utils.AccuratePrice(expens.DepositAmountSum)
		expenses[i].ExpenditureAmountSum = utils.AccuratePrice(expens.ExpenditureAmountSum)

	}
	return expenses, total, err
}

func (es *ExpensesService) QuerySurplus() (interface{}, error) {
	return dao.QuerySurplus()
}

func (es *ExpensesService) QueryProjectMonthFee(monthTime, yearTime, code string) ([]dao.ProjectMonthFee, error) {
	return dao.QueryProjectMonthFee(monthTime, yearTime, code)
}

func (es *ExpensesService) QueryMonthExpenses(monthTime, yearTime string, genreId int) ([]dao.MonthExpenses, error) {
	return dao.QueryMonthExpenses(monthTime, yearTime, genreId)
}

func (es *ExpensesService) CreateExpenses(expenses dao.Expenses) error {
	return expenses.CreateExpenses()
}

func (es *ExpensesService) CreateProjectFee(fee dao.ProjectFee) error {
	return fee.CreateProjectFee()
}

func (es *ExpensesService) UpdateExpenses(expenses dao.Expenses) error {
	return expenses.UpdateExpenses()
}

func (es *ExpensesService) UpdateProjectFee(fee dao.ProjectFee) error {
	return fee.UpdateProjectFee()
}

func (es *ExpensesService) DeleteExpenses(expenses dao.Expenses) error {
	return expenses.DeleteExpenses()
}

func (es *ExpensesService) DeleteProjectFee(fee dao.ProjectFee) error {
	return fee.DeleteProjectFee()
}
