package admin_api_controllers

import (
	"XT_Admin_Api/models/admin_models"
	"XT_Admin_Api/service"
	"XT_Admin_Api/utils"
	"time"
)

type AnalysisAPIController struct {
	AdminBaseAPIAuthController
}

func (this *AnalysisAPIController) HomeStatistics() {
	range_data := this.GetString("range_data")
	range_type, _ := this.GetInt64("range_type")
	var rangeDate *time.Time
	var parseDateErr error
	var rangeTimeStamp int64

	if len(range_data) > 0 {
		rangeDate, parseDateErr = utils.ParseTimeStringToTime("2006-01-02", range_data)
		if parseDateErr != nil {
			this.ErrorLog("日期(%v)解析错误：%v", range_data, parseDateErr)
			return
		}
		rangeTimeStamp = rangeDate.Unix()
	} else {
		rangeTimeStamp = 0
	}
	now := time.Now().Unix()

	newOrgCount, _ := service.GetRegistedOrgCount(rangeTimeStamp, now)
	totalOrg, _ := service.GetTotalOrgCount()
	activeOrgCount, _ := service.GetActiveOrgCount(rangeTimeStamp, now)
	totalActiveOrg, _ := service.GetActiveOrgTotalCount()
	activeAdminCount, _ := service.GetActiveAdminUserCount(rangeTimeStamp, now)
	adminTotalCount, _ := service.GetAdminUserTotalCount()
	patientCount, _ := service.GetNewPatientCount(rangeTimeStamp, now)
	patientTotalCount, _ := service.GetPatientTotalCount()

	var orgRegisterStatistics []*admin_models.Statistics
	var newPatientStatistics []*admin_models.Statistics
	var orgActiveAdminStatistics []*admin_models.Statistics
	var orgActiveStatistics []*admin_models.Statistics

	switch range_type {
	case 1:

		break

	case 2:
		orgRegisterStatistics, _ = service.GetYesterDayRegistedOrgCount()
		newPatientStatistics, _ = service.GetYesterDayNewPatientCount()
		orgActiveAdminStatistics, _ = service.GetYesterDayActiveAdminUserCount()
		orgActiveStatistics, _ = service.GetYesterDayActiveOrgCount()

		break
	case 3:
		orgRegisterStatistics, _ = service.GetWeekRegistedOrgCount()
		newPatientStatistics, _ = service.GetWeekNewPatientCount()
		orgActiveAdminStatistics, _ = service.GetWeekActiveAdminUserCount()
		orgActiveStatistics, _ = service.GetWeekActiveOrgCount()

		break
	case 4:
		orgRegisterStatistics, _ = service.GetMonthRegistedOrgCount()
		newPatientStatistics, _ = service.GetMonthNewPatientCount()
		orgActiveAdminStatistics, _ = service.GetMonthActiveAdminUserCount()
		orgActiveStatistics, _ = service.GetMonthActiveOrgCount()

		break
	case 5:
		orgRegisterStatistics, _ = service.GetThreeMonthRegistedOrgCount()
		newPatientStatistics, _ = service.GetThreeMonthNewPatientCount()
		orgActiveAdminStatistics, _ = service.GetThreeMonthActiveAdminUserCount()
		orgActiveStatistics, _ = service.GetThreeMonthActiveOrgCount()

		break
	case 6:
		orgRegisterStatistics, _ = service.GetSixMonthRegistedOrgCount()
		newPatientStatistics, _ = service.GetSixMonthNewPatientCount()
		orgActiveAdminStatistics, _ = service.GetSixMonthActiveAdminUserCount()
		orgActiveStatistics, _ = service.GetSixMonthActiveOrgCount()

		break
	case 7:
		orgRegisterStatistics, _ = service.GetYearRegistedOrgCount()
		newPatientStatistics, _ = service.GetYearNewPatientCount()
		orgActiveAdminStatistics, _ = service.GetYearActiveAdminUserCount()
		orgActiveStatistics, _ = service.GetYearActiveOrgCount()
		break
	}

	this.ServeSuccessJSON(map[string]interface{}{
		"org_count":                newOrgCount,
		"org_total_count":          totalOrg,
		"active_org_count":         activeOrgCount,
		"active_org_total_count":   totalActiveOrg,
		"active_admin_count":       activeAdminCount,
		"active_admin_total_count": adminTotalCount,
		"patient_count":            patientCount,
		"patient_total_count":      patientTotalCount,
		"org_count_list":           orgRegisterStatistics,
		"patient_count_list":       newPatientStatistics,
		"active_count_list":        orgActiveAdminStatistics,
		"active_org_list":          orgActiveStatistics,
	})

}
