package admin

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

type FinanceService struct{}

func (fs *FinanceService) QueryDailyExpensesList(info request.SearchDailyFee) (list interface{}, total int64, err error) {
	limit := info.PageInfo.PageSize
	offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
	dailyExpenses, total, err := dao.QueryDailyExpensesList(limit, offset, info.Genre, info.Name, info.Time, info.YearTime)

	for i, expenses := range dailyExpenses {
		feeDetails, err := dao.QueryDailyFeeDetails(int(expenses.ID))
		if err != nil {
			return nil, 0, err
		}
		dailyExpenses[i].DailyFeeDetails = feeDetails
	}

	return dailyExpenses, total, err
}

func (fs *FinanceService) QueryDailyExpenses(id int) (dao.DailyExpenses, error) {
	expenses, err := dao.QueryDailyExpenses(id)
	if err != nil {
		return dao.DailyExpenses{}, err
	}
	details, err := dao.QueryDailyFeeDetails(int(expenses.ID))
	if err != nil {
		return dao.DailyExpenses{}, err
	}
	expenses.DailyFeeDetails = details
	return expenses, err
}

func (fs *FinanceService) QueryExpensesGenre() ([]dao.ExpensesGenre, error) {
	return dao.QueryExpensesGenre()
}

func (fs *FinanceService) QueryProjectFeeGenre() ([]dao.ProjectFeeGenre, error) {
	return dao.QueryProjectFeeGenre()
}

func (fs *FinanceService) QueryBorrowingList(info request.SearchNameTime) (list interface{}, total int64, err error) {
	limit := info.PageInfo.PageSize
	offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
	borrowing, total, err := dao.QueryBorrowingList(limit, offset, info.Name, info.Time, info.YearTime)
	return borrowing, total, err
}

func (fs *FinanceService) QueryBorrowing(id int) (dao.Borrowing, error) {
	return dao.QueryBorrowing(id)
}

func (fs *FinanceService) QueryDailyFileList(info request.SearchIdNameGenre) (list interface{}, total int64, err error) {
	limit := info.PageInfo.PageSize
	offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
	dailyFiles, total, err := dao.QueryDailyFileList(limit, offset, info.Id, info.Genre, info.Name)
	return dailyFiles, total, err
}

func (fs *FinanceService) QueryProjectFinance(code string) (dao.Project, error) {
	project, err := dao.QueryProjectByCode(code)
	if err != nil {
		return dao.Project{}, err
	}
	collection, collectionTotal, err := dao.QueryCollectionList(8, 0, code, "")
	if err != nil {
		return dao.Project{}, err
	}

	reimbursements, reimbursementTotal, err := dao.QueryReimbursementList(8, 0, code, "", "")
	if err != nil {
		return dao.Project{}, err
	}
	for i, reimbursement := range reimbursements {
		details, err := dao.QueryFeeDetails(int(reimbursement.ID))
		if err != nil {
			return dao.Project{}, err
		}
		reimbursements[i].FeeDetails = details
	}

	workingHours, workingHoursTotal, err := dao.QueryWorkingHoursList(8, 0, 0, code, "", "")
	if err != nil {
		return dao.Project{}, err
	}

	fee, feeTotal, err := dao.QueryProjectFeeList(8, 0, 0, 0, code, "", "", "")
	if err != nil {
		return dao.Project{}, err
	}

	project.Collection = collection
	project.Reimbursement = reimbursements
	project.WorkingHours = workingHours
	project.ReimbursementTotal = reimbursementTotal
	project.CollectionTotal = collectionTotal
	project.WorkingHoursTotal = workingHoursTotal
	project.ProjectFee = fee
	project.ProjectFeeTotal = feeTotal

	return project, err
}

func (fs *FinanceService) QueryReserveBalance() (dao.Reserve, error) {
	return dao.QueryReserveBalance()
}

func (fs *FinanceService) CreateDailyExpenses(expenses dao.DailyExpenses) error {
	return expenses.CreateDailyExpenses()
}

func (fs *FinanceService) CreateExpensesGenre(genre dao.ExpensesGenre) error {
	return genre.CreateExpensesGenre()
}

func (fs *FinanceService) CreateProjectFeeGenre(genre dao.ProjectFeeGenre) error {
	return genre.CreateProjectFeeGenre()
}

func (fs *FinanceService) CreateBorrowing(borrowing dao.Borrowing) error {
	return borrowing.CreateBorrowing()
}

func (fs *FinanceService) CreateDailyFile(file dao.DailyFile) error {
	return file.CreateDailyFile()
}

func (fs *FinanceService) UpdateDailyExpenses(expenses dao.DailyExpenses) error {
	return expenses.UpdateDailyExpenses()
}

func (fs *FinanceService) UpdateExpensesGenre(genre dao.ExpensesGenre) error {
	return genre.UpdateExpensesGenre()
}

func (fs *FinanceService) UpdateProjectFeeGenre(genre dao.ProjectFeeGenre) error {
	return genre.UpdateProjectFeeGenre()
}

func (fs *FinanceService) UpdateBorrowing(borrowing dao.Borrowing) error {
	return borrowing.UpdateBorrowing()
}

func (fs *FinanceService) DeleteDailyExpenses(expenses dao.DailyExpenses) error {
	return expenses.DeleteDailyExpenses()
}

func (fs *FinanceService) DeleteExpensesGenre(genre dao.ExpensesGenre) error {
	expenses, _ := dao.QueryExpensesByGenre(int(genre.ID))
	if len(expenses) > 0 {
		return fmt.Errorf("费用类型正在被使用")
	}
	return genre.DeleteExpensesGenre()
}

func (fs *FinanceService) DeleteProjectFeeGenre(genre dao.ProjectFeeGenre) error {
	fee, _ := dao.QueryProjectFeeByGenre(int(genre.ID))
	if len(fee) > 0 {
		return fmt.Errorf("费用类型正在被使用")
	}
	return genre.DeleteProjectFeeGenre()
}

func (fs *FinanceService) DeleteBorrowing(borrowing dao.Borrowing) error {
	return borrowing.DeleteBorrowing()
}

func (fs *FinanceService) DeleteDailyFile(file dao.DailyFile) error {
	return file.DeleteDailyFile()
}
