package controllers

import (
	"Xcx_New/enums"
	"Xcx_New/service"
	"Xcx_New/utils"
	"time"

	"github.com/astaxie/beego"
)

type QcdApiController struct {
	BaseAuthAPIController
}

func QcdApiRegistRouters() {
	beego.Router("/api/qcd/dialysistotaldata", &QcdApiController{}, "get:GetDialysisTotalData")
	beego.Router("/api/qcd/processindicatorsdata", &QcdApiController{}, "get:GetProcessIndicatorsData")
	beego.Router("/api/qcd/oicdata", &QcdApiController{}, "get:GetOICData")
	beego.Router("/api/qcd/oiqdata", &QcdApiController{}, "get:GetOIQData")
	beego.Router("/api/qcd/patotaldata", &QcdApiController{}, "get:GetPATotalData")
	beego.Router("/api/qcd/padialysisagedata", &QcdApiController{}, "get:GetPADialysisAgeData")
	beego.Router("/api/qcd/paweightdata", &QcdApiController{}, "get:GetPAWeightData")
	beego.Router("/api/qcd/pabloodpressuredata", &QcdApiController{}, "get:GetPABloodPressureData")
	beego.Router("/api/qcd/pacomplicationdata", &QcdApiController{}, "get:GetPAComplicationData")
	beego.Router("/api/qcd/painfectiousdiseasesdata", &QcdApiController{}, "get:GetPAInfectiousDiseasesData")
	beego.Router("/api/qcd/papersonlapsetdata", &QcdApiController{}, "get:GetPersonLapsetoData")
	beego.Router("/api/qcd/papersonweightdata", &QcdApiController{}, "get:GetPersonWeightData")
	beego.Router("/api/qcd/papersonbloodpressuredata", &QcdApiController{}, "get:GetPersonBloodPressureData")
	beego.Router("/api/qcd/papersonicdata", &QcdApiController{}, "get:GetPersonICData")
	beego.Router("/api/qcd/papersonadvicedata", &QcdApiController{}, "get:GetPersonAdviceData")
}

func (c *QcdApiController) GetDialysisTotalData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	lapseto, _ := c.GetInt64("lapseto", 0)
	statisticalMethod, _ := c.GetInt64("statistical_method", 0)
	modeID, _ := c.GetInt64("mode_id", 0)
	startTime := c.GetString("start_time", "")
	endTime := c.GetString("end_time", "")

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}

	adminUserInfo := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	var isStartTime bool
	var theStartTIme int64
	if len(startTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", startTime+" 00:00:00", loc)
		if err != nil {
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isStartTime = true
		theStartTIme = theTime.Unix()
	}
	var isEndTime bool
	var theEndtTIme int64
	if len(endTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", endTime+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isEndTime = true
		theEndtTIme = theTime.Unix()
	}

	data, ttd, total, _ := service.GetDialysisTotalData(adminUserInfo.CurrentOrgId, page, limit, lapseto, statisticalMethod, modeID, theStartTIme, theEndtTIme, isStartTime, isEndTime)

	c.ServeSuccessJSON(map[string]interface{}{
		"dtd":   data,
		"ttd":   ttd,
		"total": total,
	})
	return
}

func (c *QcdApiController) GetProcessIndicatorsData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	lapseto, _ := c.GetInt64("lapseto", 0)
	dialysisAge, _ := c.GetInt64("dialysis_age", 0)
	projectID, _ := c.GetInt64("project_id", 0)
	startTime := c.GetString("start_time", "")
	endTime := c.GetString("end_time", "")

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}

	adminUserInfo := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	var isStartTime bool
	var theStartTIme int64
	if len(startTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", startTime+" 00:00:00", loc)
		if err != nil {
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isStartTime = true
		theStartTIme = theTime.Unix()
	}
	var isEndTime bool
	var theEndtTIme int64
	if len(endTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", endTime+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isEndTime = true
		theEndtTIme = theTime.Unix()
	}

	inspections, references, patients, counts, total, err := service.GetProcessIndicatorsData(adminUserInfo.CurrentOrgId, page, limit, lapseto, dialysisAge, projectID, theStartTIme, theEndtTIme, isStartTime, isEndTime)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"inspections":     inspections,
		"references":      references,
		"patients":        patients,
		"unfinishedcount": counts[0].Count,
		"total":           total,
	})
	return
}

func (c *QcdApiController) GetOICData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	lapseto, _ := c.GetInt64("lapseto", 0)
	dialysisAge, _ := c.GetInt64("dialysis_age", 0)
	projectID, _ := c.GetInt64("project_id", 0)
	itemID, _ := c.GetInt64("item_id", 0)
	startTime := c.GetString("start_time", "")
	endTime := c.GetString("end_time", "")

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}

	adminUserInfo := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	var isStartTime bool
	var theStartTIme int64
	if len(startTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", startTime+" 00:00:00", loc)
		if err != nil {
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isStartTime = true
		theStartTIme = theTime.Unix()
	}
	var isEndTime bool
	var theEndtTIme int64
	if len(endTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", endTime+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isEndTime = true
		theEndtTIme = theTime.Unix()
	}

	inspections, references, patients, counts, total, err := service.GetOICData(adminUserInfo.CurrentOrgId, page, limit, lapseto, dialysisAge, projectID, itemID, theStartTIme, theEndtTIme, isStartTime, isEndTime)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"inspections":     inspections,
		"references":      references,
		"patients":        patients,
		"unfinishedcount": counts[0].Count,
		"total":           total,
	})
	return
}

func (c *QcdApiController) GetOIQData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	projectID, _ := c.GetInt64("project_id", 0)
	startTime := c.GetString("start_time", "")
	endTime := c.GetString("end_time", "")
	searchKey := c.GetString("search", "")

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}

	adminUserInfo := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	var isStartTime bool
	var theStartTIme int64
	if len(startTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", startTime+" 00:00:00", loc)
		if err != nil {
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isStartTime = true
		theStartTIme = theTime.Unix()
	}
	var isEndTime bool
	var theEndtTIme int64
	if len(endTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", endTime+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isEndTime = true
		theEndtTIme = theTime.Unix()
	}

	inspections, references, patients, total, err := service.GetOIQData(adminUserInfo.CurrentOrgId, page, limit, projectID, theStartTIme, theEndtTIme, isStartTime, isEndTime, searchKey)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"inspections": inspections,
		"references":  references,
		"patients":    patients,
		"total":       total,
	})
	return
}

func (c *QcdApiController) GetPATotalData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	startTime := c.GetString("start_time", "")
	endTime := c.GetString("end_time", "")
	searchKey := c.GetString("search", "")
	age, _ := c.GetInt64("age", 0)
	lapseto, _ := c.GetInt64("lapseto", 0)
	source, _ := c.GetInt64("source", 0)

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}

	adminUserInfo := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	var isStartTime bool
	var theStartTIme int64
	if len(startTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", startTime+" 00:00:00", loc)
		if err != nil {
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isStartTime = true
		theStartTIme = theTime.Unix()
	}
	var isEndTime bool
	var theEndtTIme int64
	if len(endTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", endTime+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isEndTime = true
		theEndtTIme = theTime.Unix()
	}

	patients, lapsetoOut, lapsetoIn, total, err := service.GetPATotalData(adminUserInfo.CurrentOrgId, page, limit, lapseto, source, age, theStartTIme, theEndtTIme, isStartTime, isEndTime, searchKey)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"patients":   patients,
		"total":      total,
		"lapsetoOut": lapsetoOut,
		"lapsetoIn":  lapsetoIn,
	})
	return
}

func (c *QcdApiController) GetPADialysisAgeData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	startTime := c.GetString("start_time", "")
	endTime := c.GetString("end_time", "")
	age, _ := c.GetInt64("age", 0)
	dialysisAge, _ := c.GetInt64("dialysis_age", 0)
	lapseto, _ := c.GetInt64("lapseto", 0)

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}

	adminUserInfo := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	var isStartTime bool
	var theStartTIme int64
	if len(startTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", startTime+" 00:00:00", loc)
		if err != nil {
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isStartTime = true
		theStartTIme = theTime.Unix()
	}
	var isEndTime bool
	var theEndtTIme int64
	if len(endTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", endTime+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isEndTime = true
		theEndtTIme = theTime.Unix()
	}

	patients, counts, total, err := service.GetPADialysisAgeData(adminUserInfo.CurrentOrgId, page, limit, lapseto, age, dialysisAge, theStartTIme, theEndtTIme, isStartTime, isEndTime)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"patients": patients,
		"counts":   counts,
		"total":    total,
	})
	return
}

func (c *QcdApiController) GetPAWeightData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	startTime := c.GetString("start_time", "")
	endTime := c.GetString("end_time", "")
	age, _ := c.GetInt64("age", 0)
	lapseto, _ := c.GetInt64("lapseto", 0)

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}

	adminUserInfo := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	var isStartTime bool
	var theStartTIme int64
	if len(startTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", startTime+" 00:00:00", loc)
		if err != nil {
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isStartTime = true
		theStartTIme = theTime.Unix()
	}
	var isEndTime bool
	var theEndtTIme int64
	if len(endTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", endTime+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isEndTime = true
		theEndtTIme = theTime.Unix()
	}

	weights, dryweight, weightincrease, afterweight, total, err := service.GetPAWeightData(adminUserInfo.CurrentOrgId, page, limit, lapseto, age, theStartTIme, theEndtTIme, isStartTime, isEndTime)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"weights":        weights,
		"dryweight":      dryweight,
		"weightincrease": weightincrease,
		"afterweight":    afterweight,
		"total":          total,
	})
	return
}

func (c *QcdApiController) GetPABloodPressureData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	startTime := c.GetString("start_time", "")
	endTime := c.GetString("end_time", "")
	lapseto, _ := c.GetInt64("lapseto", 0)
	dialysisStage, _ := c.GetInt64("dialysis_stage", 0)

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}

	adminUserInfo := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	var isStartTime bool
	var theStartTIme int64
	if len(startTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", startTime+" 00:00:00", loc)
		if err != nil {
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isStartTime = true
		theStartTIme = theTime.Unix()
	}
	var isEndTime bool
	var theEndtTIme int64
	if len(endTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", endTime+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isEndTime = true
		theEndtTIme = theTime.Unix()
	}

	bloodPressures, bps, total, err := service.GetPABloodPressureData(adminUserInfo.CurrentOrgId, page, limit, lapseto, dialysisStage, theStartTIme, theEndtTIme, isStartTime, isEndTime)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"blood_pressures": bloodPressures,
		"bps":             bps,
		"total":           total,
	})
	return
}

func (c *QcdApiController) GetPAComplicationData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	startTime := c.GetString("start_time", "")
	endTime := c.GetString("end_time", "")
	age, _ := c.GetInt64("age", 0)
	statisticalState, _ := c.GetInt64("statistical_state", 0)

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}

	adminUserInfo := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	var isStartTime bool
	var theStartTIme int64
	if len(startTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", startTime+" 00:00:00", loc)
		if err != nil {
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isStartTime = true
		theStartTIme = theTime.Unix()
	}
	var isEndTime bool
	var theEndtTIme int64
	if len(endTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", endTime+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isEndTime = true
		theEndtTIme = theTime.Unix()
	}

	complications, pcps, total, err := service.GetPAComplicationData(adminUserInfo.CurrentOrgId, page, limit, age, statisticalState, theStartTIme, theEndtTIme, isStartTime, isEndTime)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"complications": complications,
		"pcps":          pcps,
		"total":         total,
	})
	return
}

func (c *QcdApiController) GetPAInfectiousDiseasesData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	startTime := c.GetString("start_time", "")
	endTime := c.GetString("end_time", "")
	age, _ := c.GetInt64("age", 0)
	contagionType, _ := c.GetInt64("contagion_type", 0)
	lapseto, _ := c.GetInt64("lapseto", 0)

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}

	adminUserInfo := c.GetAdminUserInfo()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	var isStartTime bool
	var theStartTIme int64
	if len(startTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", startTime+" 00:00:00", loc)
		if err != nil {
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isStartTime = true
		theStartTIme = theTime.Unix()
	}
	var isEndTime bool
	var theEndtTIme int64
	if len(endTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", endTime+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isEndTime = true
		theEndtTIme = theTime.Unix()
	}

	patients, dsp, total, err := service.GetPAInfectiousDiseasesData(adminUserInfo.CurrentOrgId, page, limit, lapseto, age, contagionType, theStartTIme, theEndtTIme, isStartTime, isEndTime)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"patients": patients,
		"dsp":      dsp,
		"total":    total,
	})
	return
}

func (c *QcdApiController) GetPersonLapsetoData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	patientId, _ := c.GetInt64("patient_id", 0)

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}

	adminUserInfo := c.GetAdminUserInfo()

	patient, lapsetos, total, err := service.GetPersonLapsetoData(adminUserInfo.CurrentOrgId, patientId, page, limit)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"patient":  patient,
		"lapsetos": lapsetos,
		"total":    total,
	})
	return
}

func (c *QcdApiController) GetPersonWeightData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	patientId, _ := c.GetInt64("patient_id", 0)
	startTime := c.GetString("start_time", "")
	endTime := c.GetString("end_time", "")

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}
	var isStartTime bool
	var theStartTIme int64

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	if len(startTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", startTime+" 00:00:00", loc)
		if err != nil {
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isStartTime = true
		theStartTIme = theTime.Unix()
	}
	var isEndTime bool
	var theEndtTIme int64
	if len(endTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", endTime+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isEndTime = true
		theEndtTIme = theTime.Unix()
	}

	adminUserInfo := c.GetAdminUserInfo()

	patient, weight, pie, total, err := service.GetPersonWeightData(adminUserInfo.CurrentOrgId, patientId, page, limit, theStartTIme, theEndtTIme, isStartTime, isEndTime)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"patient": patient,
		"weight":  weight,
		"pie":     pie,
		"total":   total,
	})
	return
}

func (c *QcdApiController) GetPersonBloodPressureData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	patientId, _ := c.GetInt64("patient_id", 0)
	startTime := c.GetString("start_time", "")
	endTime := c.GetString("end_time", "")

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}
	var isStartTime bool
	var theStartTIme int64

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	if len(startTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", startTime+" 00:00:00", loc)
		if err != nil {
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isStartTime = true
		theStartTIme = theTime.Unix()
	}
	var isEndTime bool
	var theEndtTIme int64
	if len(endTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", endTime+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isEndTime = true
		theEndtTIme = theTime.Unix()
	}

	adminUserInfo := c.GetAdminUserInfo()

	patient, bps, lines, total, err := service.GetPersonBloodPressureData(adminUserInfo.CurrentOrgId, patientId, page, limit, theStartTIme, theEndtTIme, isStartTime, isEndTime)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"patient": patient,
		"bps":     bps,
		"lines":   lines,
		"total":   total,
	})
	return
}

func (c *QcdApiController) GetPersonICData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	patientId, _ := c.GetInt64("patient_id", 0)

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}
	adminUserInfo := c.GetAdminUserInfo()

	patient, inspections, references, total, err := service.GetPersonICData(adminUserInfo.CurrentOrgId, patientId, page, limit)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"patient":     patient,
		"inspections": inspections,
		"references":  references,
		"total":       total,
	})
	return
}

func (c *QcdApiController) GetPersonAdviceData() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	patientId, _ := c.GetInt64("patient_id", 0)
	adviceType, _ := c.GetInt64("advice_type", 0)
	medicalType, _ := c.GetInt64("medical_type", 0)
	startTime := c.GetString("start_time", "")
	endTime := c.GetString("end_time", "")

	if page <= 0 {
		page = 1
	}
	if limit <= 0 {
		limit = 10
	}
	var isStartTime bool
	var theStartTIme int64

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	if len(startTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", startTime+" 00:00:00", loc)
		if err != nil {
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isStartTime = true
		theStartTIme = theTime.Unix()
	}
	var isEndTime bool
	var theEndtTIme int64
	if len(endTime) > 0 {
		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", endTime+" 23:59:59", loc)
		if err != nil {
			utils.ErrorLog(err.Error())
			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
			return
		}
		isEndTime = true
		theEndtTIme = theTime.Unix()
	}

	adminUserInfo := c.GetAdminUserInfo()

	patient, advices, total, err := service.GetPersonAdviceData(adminUserInfo.CurrentOrgId, patientId, page, limit, adviceType, medicalType, theStartTIme, theEndtTIme, isStartTime, isEndTime)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	users, err := service.GetAllAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
	if err != nil {
		utils.ErrorLog(err.Error())
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	c.ServeSuccessJSON(map[string]interface{}{
		"patient": patient,
		"advices": advices,
		"users":   users,
		"total":   total,
	})
	return
}
