package admin

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

type ProjectService struct{}

func (ps *ProjectService) QueryProjectList(info request.SearchProject) (list interface{}, total int64, err error) {
	limit := info.PageInfo.PageSize
	offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
	project, total, err := dao.QueryProjectList(limit, offset, info.State, info.Name, info.Time)

	for i, p := range project {
		collection, err := dao.QueryCollection(p.Code)
		if err != nil {
			return nil, total, err
		}
		project[i].Collection = collection
	}

	return project, total, err
}

func (ps *ProjectService) QueryProjectByCode(code string) (project dao.Project, err error) {
	project, err = dao.QueryProjectByCode(code)
	if err != nil {
		return
	}
	files, err := dao.QueryFilesByCode(code)
	if err != nil {
		return
	}
	project.Files = files
	return project, err
}

func (ps *ProjectService) QueryFileList(info request.SearchProjectFileList) (list interface{}, total int64, err error) {
	limit := info.PageInfo.PageSize
	offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
	files, total, err := dao.QueryFileList(limit, offset, info.Genre, info.Code, info.Name)
	return files, total, err
}

func (ps *ProjectService) QueryFilesByNameOrGenre(info request.SearchProjectFileList) ([]dao.ProjectFile, error) {
	return dao.QueryFilesByNameOrGenre(info.Genre, info.Code, info.Name)
}

func (ps *ProjectService) QueryAllProject() ([]dao.Project, error) {
	return dao.QueryAllProject()
}

func (ps *ProjectService) QueryProjectsInfo() (sum float64, totalProject, completeProject, unCompleteProject int, err error) {
	projects, err := dao.QueryAllProject()
	if err != nil {
		return
	}
	for _, project := range projects {
		totalProject++
		if project.State == 3 {
			completeProject++
		}
		sum = sum + project.ProjectPrice
	}
	unCompleteProject = totalProject - completeProject
	return
}

func (ps *ProjectService) QueryProjectState() ([]dao.ProjectState, error) {
	return dao.QueryProjectState()
}

func (ps *ProjectService) QueryWorkingHours(code string) ([]dao.ProjectWorkingHours, error) {
	return dao.QueryWorkingHours(code)
}

func (ps *ProjectService) QueryWorkingHoursList(info request.SearchWorkingHours) (list interface{}, total int64, err error) {
	limit := info.PageInfo.PageSize
	offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
	return dao.QueryWorkingHoursList(limit, offset, info.People, info.Code, info.Time, info.YearTime)
}

func (ps *ProjectService) QueryWorkingHoursSum(info request.SearchWorkingHours) (list interface{}, total int64, err error) {
	limit := info.PageInfo.PageSize
	offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
	return dao.QueryWorkingHoursSum(limit, offset, info.People, info.Code, info.Time, info.YearTime)
}

func (ps *ProjectService) QueryCollections(code string) ([]dao.Collection, error) {
	return dao.QueryCollection(code)
}

func (ps *ProjectService) QueryCollectionList(info request.SearchProjectList) (list interface{}, total int64, err error) {
	limit := info.PageInfo.PageSize
	offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
	return dao.QueryCollectionList(limit, offset, info.Code, info.Time)
}
func (ps *ProjectService) QueryReimbursementList(info request.SearchProjectList) (list interface{}, total int64, err error) {
	limit := info.PageInfo.PageSize
	offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
	reimbursements, total, err := dao.QueryReimbursementList(limit, offset, info.Code, info.Name, info.Time)
	if err != nil {
		return nil, 0, err
	}
	for i, reimbursement := range reimbursements {
		details, err := dao.QueryFeeDetails(int(reimbursement.ID))
		if err != nil {
			return nil, 0, err
		}
		reimbursements[i].FeeDetails = details
	}
	return reimbursements, total, err
}

func (ps *ProjectService) QueryReimbursement(code string) ([]dao.Reimbursement, error) {
	reimbursements, err := dao.QueryReimbursement(code)
	if err != nil {
		return nil, err
	}
	for i, reimbursement := range reimbursements {
		details, err := dao.QueryFeeDetails(int(reimbursement.ID))
		if err != nil {
			return nil, err
		}
		reimbursements[i].FeeDetails = details
	}
	return reimbursements, err
}

func (ps *ProjectService) QueryProjectSum(code string) (dao.ProjectSum, error) {
	return dao.QueryProjectSum(code)
}

func (ps *ProjectService) CreateProject(project dao.Project) error {
	return project.CreateProject()
}

func (ps *ProjectService) CreateProjectFile(projectFile dao.ProjectFile) error {
	return projectFile.CreateFile()
}

func (ps *ProjectService) CreateProjectState(projectState dao.ProjectState) error {
	return projectState.CreateProjectState()
}

func (ps *ProjectService) CreateOrUpdateWorkingHours(workingHours dao.ProjectWorkingHours) error {
	return workingHours.CreateWorkingHours()
}

func (ps *ProjectService) CreateCollection(collection dao.Collection) error {
	return collection.CreateCollection()
}

func (ps *ProjectService) CreateReimbursement(reimbursement dao.Reimbursement) error {
	return reimbursement.CreateReimbursement()
}

func (ps *ProjectService) UpdateProject(project dao.Project) error {
	if project.State == 3 {
		project.CollectionTime = time.Now().Format("2006-01-02 15:04:05")
	} else if project.State == 4 || project.State == 5 {
		project.EndTime = time.Now().Format("2006-01-02 15:04:05")
	}
	return project.UpdateProject()
}

func (ps *ProjectService) UpdateProjectState(projectState dao.ProjectState) error {
	return projectState.UpdateProjectState()
}

func (ps *ProjectService) UpdateWorkingHours(workingHours dao.ProjectWorkingHours) error {
	return workingHours.UpdateWorkingHours()
}

func (ps *ProjectService) UpdateCollection(collection dao.Collection) error {
	return collection.UpdateCollection()
}

func (ps *ProjectService) UpdateReimbursement(reimbursement dao.Reimbursement) error {
	return reimbursement.UpdateReimbursement()
}

func (ps *ProjectService) DeleteProject(code string) error {
	files, _ := dao.QueryFilesByCode(code)
	if len(files) > 0 {
		return fmt.Errorf("项目下存在文件")
	}
	collection, _ := dao.QueryCollection(code)
	if len(collection) > 0 {
		return fmt.Errorf("项目下存在收款记录")
	}
	project, _ := dao.QueryProjectByCode(code)
	expenses, _ := dao.QueryExpensesByProjectId(int(project.ID))
	if len(expenses) > 0 {
		return fmt.Errorf("项目下存在费用记录")
	}
	return dao.DeleteProject(code)
}

func (ps *ProjectService) DeleteProjectFile(id uint) error {
	return dao.DeleteFile(id)
}

func (ps *ProjectService) DeleteProjectState(projectState dao.ProjectState) error {
	return projectState.DeleteProjectState()
}

func (ps *ProjectService) DeleteWorkingHours(workingHours dao.ProjectWorkingHours) error {
	return workingHours.DeleteWorkingHours()
}

func (ps *ProjectService) DeleteCollection(collection dao.Collection) error {
	return collection.DeleteCollection()
}

func (ps *ProjectService) DeleteReimbursement(reimbursement dao.Reimbursement) error {
	return reimbursement.DeleteReimbursement()
}
