package service

import (
	"XT_New/models"
)

func GetDayDialysisCount(orgID, date int64) (total int64) {
	readDb.Model(&models.DialysisOrder{}).Where("user_org_id=? and dialysis_date=? and status=1", orgID, date).Count(&total)
	return
}

func GetTimebetweenDialysisCount(orgID, start, end int64) (total int64) {
	readDb.Model(&models.DialysisOrder{}).Where("user_org_id=? and dialysis_date>=? and dialysis_date<=? and status=1", orgID, start, end).Count(&total)
	return
}

type PatientContagionsCountStruct struct {
	DiseaseId int64 `json:"disease_id"`
	Count     int64 `json:"count"`
}

func GetPatientContagionCounts(orgID int64) (counts []*PatientContagionsCountStruct) {
	readDb.Table("xt_patients_infectious_diseases AS pid").Joins("JOIN xt_patients AS p ON p.id=pid.patient_id").Where("p.user_org_id=? and p.status=1 and pid.status=1", orgID).Select("pid.disease_id, count(pid.disease_id) as count").Group("pid.disease_id").Scan(&counts)
	return
}

type PatientGenderCountStruct struct {
	Gender int64 `json:"gender"`
	Count  int64 `json:"count"`
}

func GetPatientGenderCounts(orgID int64) (counts []*PatientGenderCountStruct) {
	readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID).Select("gender, count(gender) as count").Group("gender").Scan(&counts)
	return
}

type PatientAgeCountStruct struct {
	Age   int64 `json:"age"`
	Count int64 `json:"count"`
}

func GetPatiendAgeBetweenCount(orgID int64) (counts []*PatientAgeCountStruct) {
	readDb.Raw(`SELECT nnd AS 'age',COUNT(*) AS 'count' FROM(
	 SELECT
	 CASE
	  WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<10 THEN '10'
	  WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=10 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<20 THEN '20'
	  WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=20 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<30 THEN '30'
	  WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=30 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<40 THEN '40'
	  WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=40 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<50 THEN '50'
	  WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=50 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<60 THEN '60'
	  WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=60 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<70 THEN '70'
	  WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=70 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<80 THEN '80'
	  WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=80 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<90 THEN '90'
	  WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=90 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<100 THEN '100'
	  ELSE '-1'
	 END
	 AS nnd FROM xt_patients where user_org_id=? and status=1
	 )a GROUP BY nnd`, orgID).Scan(&counts)
	return
}

type DialysisModeCountStruct struct {
	Date      int64  `json:"date"`
	Mode      int64  `json:"mode"`
	Count     int64  `json:"count"`
	DateMonth string `json:"date_month"`
}

func GetPatientDialysisModeBetweenCount(orgID int64, start, end int64) (counts []*DialysisModeCountStruct) {
	readDb.Raw(`SELECT o.dialysis_date as date, p.mode_id as mode, count(p.mode_id) as count, FROM_UNIXTIME(o.dialysis_date, '%Y-%m') as date_month from xt_dialysis_order as o JOIN xt_dialysis_prescription as p ON p.record_date=o.dialysis_date WHERE o.user_org_id=? and p.user_org_id=? and o.status=1 and p.status=1 and o.dialysis_date>=? and o.dialysis_date<? group by FROM_UNIXTIME(o.dialysis_date, '%Y%m'), p.mode_id`, orgID, orgID, start, end).Scan(&counts)
	return
}
