package controllers

import (
	"XT_New/service"
	"github.com/astaxie/beego"
	"time"
)

type StatisticsApiController struct {
	BaseAuthAPIController
}

func StatisticsApiRegistRouters() {

	beego.Router("/api/statistisc/index", &StatisticsApiController{}, "get:GetStatistics")

}

func (c *StatisticsApiController) GetStatistics() {
	adminUserInfo := c.GetAdminUserInfo()

	// thisTime := time.Now()
	year, month, day := time.Now().Date()
	todayTime := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
	startYearTime := time.Date(year, 1, 1, 0, 0, 0, 0, time.Local)
	endYearTime := time.Date(year+1, 1, 1, 0, 0, 0, 0, time.Local)

	todayWeek := int(todayTime.Weekday())
	if todayWeek == 0 {
		todayWeek = 7
	}

	weekEnd := 7 - todayWeek
	weekStart := weekEnd - 6
	// endDay := todayTime.AddDate(0, 0, weekEnd)
	startDay := todayTime.AddDate(0, 0, weekStart)

	//患者总数
	patientCount := service.GetPatientCount(adminUserInfo.CurrentOrgId)

	//今日透析
	todayDialysisCount := service.GetDayDialysisCount(adminUserInfo.CurrentOrgId, todayTime.Unix())

	//本周透析
	weekDaylysisCount := service.GetTimebetweenDialysisCount(adminUserInfo.CurrentOrgId, startDay.Unix(), todayTime.Unix())

	//传染病
	diseaseCounts := service.GetPatientContagionCounts(adminUserInfo.CurrentOrgId)

	//性别分布
	genderCounts := service.GetPatientGenderCounts(adminUserInfo.CurrentOrgId)

	//年龄分布
	ageCounts := service.GetPatiendAgeBetweenCount(adminUserInfo.CurrentOrgId)

	//透析模式
	modeCounts := service.GetPatientDialysisModeBetweenCount(adminUserInfo.CurrentOrgId, startYearTime.Unix(), endYearTime.Unix())

	c.ServeSuccessJSON(map[string]interface{}{
		"patient_count":        patientCount,
		"today_dialysis_count": todayDialysisCount,
		"week_daylysis_count":  weekDaylysisCount,
		"disease_counts":       diseaseCounts,
		"gender_counts":        genderCounts,
		"age_counts":           ageCounts,
		"mode_counts":          modeCounts,
	})

}
