package admin

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

type FinanceApi struct{}

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

func (fa *FinanceApi) QueryDailyExpenses(c *gin.Context) {
	v := c.Query("id")
	id, err := strconv.Atoi(v)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryDailyExpenses ====== " + err.Error())
		return
	}

	expenses, err := financeService.QueryDailyExpenses(id)
	if err != nil {
	}
	details, err := dao.QueryDailyFeeDetails(int(expenses.ID))
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryDailyExpenses ====== " + err.Error())
		return
	}
	response.OkWithData(details, c)
}

func (fa *FinanceApi) QueryExpensesGenre(c *gin.Context) {
	genre, err := financeService.QueryExpensesGenre()
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryExpensesGenre ====== " + err.Error())
		return
	}
	response.OkWithData(genre, c)
}

func (fa *FinanceApi) QueryProjectFeeGenre(c *gin.Context) {
	genre, err := financeService.QueryProjectFeeGenre()
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryProjectFeeGenre ====== " + err.Error())
		return
	}
	response.OkWithData(genre, c)
}

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

func (fa *FinanceApi) QueryBorrowing(c *gin.Context) {
	v := c.Query("id")
	id, err := strconv.Atoi(v)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryBorrowing ====== " + err.Error())
		return
	}
	borrowing, err := financeService.QueryBorrowing(id)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryBorrowing ====== " + err.Error())
		return
	}
	response.OkWithData(borrowing, c)
}

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

func (fa *FinanceApi) QueryProjectFinance(c *gin.Context) {
	code := c.Query("code")
	finance, err := financeService.QueryProjectFinance(code)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryProjectFinance ====== " + err.Error())
		return
	}
	response.OkWithData(finance, c)
}

func (fa *FinanceApi) QueryReserveBalance(c *gin.Context) {
	balance, err := financeService.QueryReserveBalance()
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryReserveBalance ====== " + err.Error())
		return
	}
	response.OkWithData(balance, c)
}

func (fa *FinanceApi) CreateDailyExpenses(c *gin.Context) {
	var expenses dao.DailyExpenses
	err := c.ShouldBindJSON(&expenses)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateDailyExpenses ====== " + err.Error())
		return
	}
	err = financeService.CreateDailyExpenses(expenses)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateDailyExpenses ====== " + err.Error())
		return
	}
	response.OkWithMessage("新增成功", c)
}

func (fa *FinanceApi) CreateExpensesGenre(c *gin.Context) {
	var expensesGenre dao.ExpensesGenre
	err := c.ShouldBindJSON(&expensesGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateExpensesGenre ====== " + err.Error())
		return
	}
	err = financeService.CreateExpensesGenre(expensesGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateExpensesGenre ====== " + err.Error())
		return
	}
	response.OkWithMessage("新增成功", c)
}

func (fa *FinanceApi) CreateProjectFeeGenre(c *gin.Context) {
	var projectFeeGenre dao.ProjectFeeGenre
	err := c.ShouldBindJSON(&projectFeeGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateProjectFeeGenre ====== " + err.Error())
		return
	}
	err = financeService.CreateProjectFeeGenre(projectFeeGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateProjectFeeGenre ====== " + err.Error())
		return
	}
	response.OkWithMessage("新增成功", c)
}

func (fa *FinanceApi) CreateBorrowing(c *gin.Context) {
	var borrowing dao.Borrowing
	err := c.ShouldBindJSON(&borrowing)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateBorrowing ====== " + err.Error())
		return
	}
	err = financeService.CreateBorrowing(borrowing)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateBorrowing ====== " + err.Error())
		return
	}
	response.OkWithMessage("新增成功", c)
}

func (fa *FinanceApi) CreateDailyFile(c *gin.Context) {
	form, err := c.MultipartForm()
	if err != nil {
		response.FailWithMessage("失败", c)
		return
	}
	files := form.File["file"]

	genre := c.PostForm("genre")

	if len(genre) <= 0 {
		response.FailWithMessage("类型为空", c)
		return
	}

	id := c.PostForm("id")
	genreId, err := strconv.Atoi(genre)
	if err != nil {
		response.FailWithMessage("错误", c)
		global.GVA_LOG.Error("CreateDailyFile ====== " + err.Error())
		return
	}
	userId, err := strconv.Atoi(id)
	if err != nil {
		response.FailWithMessage("错误", c)
		global.GVA_LOG.Error("CreateDailyFile ====== " + err.Error())
		return
	}
	fileGenre, err := fileService.QueryFileGenreById(genreId)
	if err != nil {
		response.FailWithMessage("错误", c)
		global.GVA_LOG.Error("CreateDailyFile ====== " + err.Error())
		return
	}
	for _, file := range files {
		dst := file.Filename
		var path = "./uploads/file/" + id + "/" + fileGenre.Name + "/" + dst
		dailFile := dao.DailyFile{
			UserId: userId,
			Name:   dst,
			Genre:  genreId,
			Path:   path,
		}

		// gin 简单做了封装,拷贝了文件流
		if err := c.SaveUploadedFile(file, path); err != nil {
			response.FailWithMessage("上传失败", c)
			return
		}
		err = financeService.CreateDailyFile(dailFile)
		if err != nil {
			response.FailWithMessage("上传失败", c)
			return
		}
	}

	response.OkWithMessage("上传成功", c)
}

func (fa *FinanceApi) UpdateDailyExpenses(c *gin.Context) {
	var expenses dao.DailyExpenses
	err := c.ShouldBindJSON(&expenses)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateDailyExpenses ====== " + err.Error())
		return
	}
	err = financeService.UpdateDailyExpenses(expenses)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateDailyExpenses ====== " + err.Error())
		return
	}
	response.OkWithMessage("更新成功", c)
}

func (fa *FinanceApi) UpdateExpensesGenre(c *gin.Context) {
	var expensesGenre dao.ExpensesGenre
	err := c.ShouldBindJSON(&expensesGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateExpensesGenre ====== " + err.Error())
		return
	}
	err = financeService.UpdateExpensesGenre(expensesGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateExpensesGenre ====== " + err.Error())
		return
	}
	response.OkWithMessage("更新成功", c)
}

func (fa *FinanceApi) UpdateProjectFeeGenre(c *gin.Context) {
	var projectFeeGenre dao.ProjectFeeGenre
	err := c.ShouldBindJSON(&projectFeeGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateProjectFeeGenre ====== " + err.Error())
		return
	}
	err = financeService.UpdateProjectFeeGenre(projectFeeGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateProjectFeeGenre ====== " + err.Error())
		return
	}
	response.OkWithMessage("更新成功", c)
}

func (fa *FinanceApi) UpdateBorrowing(c *gin.Context) {
	var borrowing dao.Borrowing
	err := c.ShouldBindJSON(&borrowing)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateBorrowing ====== " + err.Error())
		return
	}
	err = financeService.UpdateBorrowing(borrowing)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateBorrowing ====== " + err.Error())
		return
	}
	response.OkWithMessage("更新成功", c)
}

func (fa *FinanceApi) DeleteDailyExpenses(c *gin.Context) {
	var expenses dao.DailyExpenses
	err := c.ShouldBindJSON(&expenses)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteDailyExpenses ====== " + err.Error())
		return
	}
	err = financeService.DeleteDailyExpenses(expenses)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteDailyExpenses ====== " + err.Error())
		return
	}
	response.OkWithMessage("删除成功", c)
}

func (fa *FinanceApi) DeleteExpensesGenre(c *gin.Context) {
	var expensesGenre dao.ExpensesGenre
	err := c.ShouldBindJSON(&expensesGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteExpensesGenre ====== " + err.Error())
		return
	}
	err = financeService.DeleteExpensesGenre(expensesGenre)
	if err != nil {
		if err.Error() == "费用类型正在被使用" {
			response.FailWithMessage("费用类型正在被使用", c)
			global.GVA_LOG.Error("DeleteExpensesGenre ====== " + err.Error())
			return
		}
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteExpensesGenre ====== " + err.Error())
		return
	}
	response.OkWithMessage("删除成功", c)
}

func (fa *FinanceApi) DeleteProjectFeeGenre(c *gin.Context) {
	var projectFeeGenre dao.ProjectFeeGenre
	err := c.ShouldBindJSON(&projectFeeGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteProjectFeeGenre ====== " + err.Error())
		return
	}
	err = financeService.DeleteProjectFeeGenre(projectFeeGenre)
	if err != nil {
		if err.Error() == "项目收费类型正在被使用" {
			response.FailWithMessage("项目收费类型正在被使用", c)
			global.GVA_LOG.Error("DeleteProjectFeeGenre ====== " + err.Error())
			return
		}
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteProjectFeeGenre ====== " + err.Error())
		return
	}
	response.OkWithMessage("删除成功", c)
}

func (fa *FinanceApi) DeleteBorrowing(c *gin.Context) {
	var borrowing dao.Borrowing
	err := c.ShouldBindJSON(&borrowing)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteBorrowing ====== " + err.Error())
		return
	}
	err = financeService.DeleteBorrowing(borrowing)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteBorrowing ====== " + err.Error())
		return
	}
	response.OkWithMessage("删除成功", c)
}

func (fa *FinanceApi) DeleteDailyFile(c *gin.Context) {
	var dailyFile dao.DailyFile
	err := c.ShouldBindJSON(&dailyFile)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteDailyFile ====== " + err.Error())
		return
	}

	err = os.Remove(dailyFile.Path)
	if err != nil {
		// 如果发生错误，打印错误信息
		response.FailWithMessage("删除文件时发生错误", c)
		global.GVA_LOG.Error("删除文件时发生错误: " + err.Error())
		return
	}

	err = financeService.DeleteDailyFile(dailyFile)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteDailyFile ====== " + err.Error())
		return
	}
	response.OkWithMessage("删除成功", c)
}
