package new_mobile_api_controllers

import (
	"XT_New/enums"
	"XT_New/models"
	"XT_New/service"
	"XT_New/utils"
	"encoding/json"
	"fmt"
	"github.com/jinzhu/gorm"
	"strconv"
	"strings"
	"time"
)

type NewDialysisApiController struct {
	NewMobileBaseAPIAuthController
}

func (this *NewDialysisApiController) GetIllnesslist() {

	illnesslist, err := service.GetIllnessListTwo()
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"illnesslist": illnesslist,
	})
}

func (this *NewDialysisApiController) GetPatient() {
	adminInfo := this.GetMobileAdminUserInfo()
	orgid := adminInfo.Org.Id
	page, _ := this.GetInt64("page")
	limit, _ := this.GetInt64("limit")
	patient, total, err := service.GetBloodDialysisPatient(orgid, page, limit)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"patient": patient,
		"total":   total,
	})
}

func (this *NewDialysisApiController) GetAllPatient() {
	adminInfo := this.GetMobileAdminUserInfo()
	orgid := adminInfo.Org.Id
	page, _ := this.GetInt64("page")
	fmt.Print("page", page)
	limit, _ := this.GetInt64("limit")
	fmt.Print("limit", limit)
	patient, err := service.GetAllPatient(orgid)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"patient": patient,
	})
}

func (this *NewDialysisApiController) GetGeneratedialysisno() {
	adminInfo := this.GetMobileAdminUserInfo()
	orgid := adminInfo.Org.Id
	dialysisNo := service.ChechLastDialysisNoTwo(orgid)
	//fmt.Print("机构ID",adminUserInfo.CurrentOrgId)
	if dialysisNo == 0 {
		dialysisNo = 1
	} else {
		dialysisNo++
	}
	no := strconv.FormatInt(dialysisNo, 10)

	rep := 3 - len(no)

	if rep > 0 {
		no = strings.Repeat("0", rep) + no
	}

	this.ServeSuccessJSON(map[string]interface{}{
		"no": no,
	})
	return
}

func (this *NewDialysisApiController) GetBloodPatientInfo() {
	adminInfo := this.GetMobileAdminUserInfo()
	orgId := adminInfo.Org.Id
	fmt.Print("机构ID", orgId)
	phone := this.GetString("phone")
	_, errcode := service.GetBloodPatientInfo(orgId, phone)
	fmt.Print("errcode-----------------", errcode)
	if errcode == gorm.ErrRecordNotFound {
		returnData := make(map[string]interface{}, 0)
		returnData["msg"] = "ok"
		this.ServeSuccessJSON(returnData)
		return
	} else if errcode == nil {
		return
	} else {

	}
}

func (this *NewDialysisApiController) GetSlowPatientInfo() {
	adminInfo := this.GetMobileAdminUserInfo()
	orgId := adminInfo.Org.Id
	fmt.Print("机构ID", orgId)
	phone := this.GetString("phone")
	fmt.Print("phone", phone)
	_, errcode := service.GetSlowPatientInfo(orgId, phone)
	fmt.Print("errcode -----------------------", errcode)
	if errcode == gorm.ErrRecordNotFound {
		returnData := make(map[string]interface{}, 0)
		returnData["msg"] = "ok"
		this.ServeSuccessJSON(returnData)
		return
	} else if errcode == nil {
		return
	} else {

	}

}

func (this *NewDialysisApiController) GetMemberpatientInfo() {
	adminInfo := this.GetMobileAdminUserInfo()
	orgId := adminInfo.Org.Id
	fmt.Print("机构ID", orgId)
	phone := this.GetString("phone")
	fmt.Print("phone", phone)
	_, errcode := service.GetMemberPatientInfo(orgId, phone)
	fmt.Print("errcode --------------", errcode)
	if errcode == gorm.ErrRecordNotFound {
		returnData := make(map[string]interface{}, 0)
		returnData["msg"] = "ok"
		this.ServeSuccessJSON(returnData)
		return
	} else if errcode == nil {
		return
	} else {

	}
}

func (this *NewDialysisApiController) SavePatient() {
	adminInfo := this.GetMobileAdminUserInfo()
	orgid := adminInfo.Org.Id
	dataBody := make(map[string]interface{}, 0)
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
	fmt.Print("err", err)
	name := dataBody["name"].(string)
	fmt.Println("姓名", name)
	sex := int64(dataBody["sex"].(float64))
	fmt.Print("性别", sex)
	idCard := dataBody["idCard"].(string)
	fmt.Print("身份证", idCard)
	birthday := dataBody["birthday"].(string)
	births := strings.Split(birthday, "-")
	fmt.Print("生日", birthday)
	birYear, _ := strconv.Atoi(births[0])
	age := time.Now().Year() - birYear
	ages := int64(age)
	fmt.Println("age是多少", ages)
	timeLayout := "2006-01-02 15:04:05"
	theTime, err := utils.ParseTimeStringToTime(timeLayout, birthday+" 00:00:00")
	birth := theTime.Unix()
	fmt.Print(birth)
	phone := dataBody["phone"].(string)
	fmt.Print("电话号码", phone)
	patientType := dataBody["result"].([]interface{})
	fmt.Print("患者标签", patientType)
	dialysis := dataBody["dialysis"].(string)
	fmt.Print("透析号", dialysis)
	patientsoure := int64(dataBody["patientsoure"].(float64))
	fmt.Print("患者来源", patientsoure)
	lapseto := int64(dataBody["lapseto"].(float64))
	fmt.Print("留置状态", lapseto)
	contagions := dataBody["resultTwo"].([]interface{})
	fmt.Print("传染病", contagions)
	ids := make([]int64, 0)
	for _, contagion := range contagions {
		id, _ := strconv.ParseInt(contagion.(string), 10, 64)
		ids = append(ids, id)
	}
	fmt.Print("传染病2", contagions)
	is_infectious := int64(dataBody["is_infectious"].(float64))
	fmt.Print("传染病", is_infectious)
	adminssionNumber := dataBody["admissionNumber"].(string)
	fmt.Print("住院号", adminssionNumber)

	fistdate := dataBody["fistDate"].(string)
	fmt.Print("首次透析日期", fistdate)
	timeLayouts := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	times, err := time.ParseInLocation(timeLayouts+" 15:04:05", fistdate+" 00:00:00", loc)
	//times, err := utils.ParseTimeStringToTime(timeLayout, fistdate+"00:00:00")
	fisttime := times.Unix()
	fmt.Print(fisttime)

	diagonse := dataBody["diagnose"].(string)
	fmt.Print("诊断", diagonse)
	avatar := dataBody["avatar"].(string)
	fmt.Print("头像", avatar)
	//慢性病
	requipmentId := dataBody["requipmentId"].(string)
	fmt.Print("设备ID", requipmentId)
	slowContagions := dataBody["resultThree"].([]interface{})
	fmt.Print("慢性传染病", slowContagions)
	idss := make([]int64, 0)
	for _, contagion := range slowContagions {
		id, _ := strconv.ParseInt(contagion.(string), 10, 64)
		idss = append(ids, id)
	}
	slowDiseases := dataBody["resultFour"].([]interface{})
	idsss := make([]int64, 0)
	for _, contagion := range slowDiseases {
		id, _ := strconv.ParseInt(contagion.(string), 10, 64)
		idsss = append(ids, id)
	}
	fmt.Print("慢性病", slowDiseases)

	bloodPatient := int64(dataBody["bloodPatient"].(float64))
	fmt.Print("血透病人", bloodPatient)
	slowpatient := int64(dataBody["slowPatient"].(float64))
	fmt.Print("慢病病人", slowpatient)
	memberpatient := int64(dataBody["memberPatient"].(float64))
	fmt.Print("会员病人", memberpatient)
	//会员
	memberFistDate := dataBody["memberFistDate"].(string)
	fmt.Print("首次透析日期", memberFistDate)
	//membertimes, err := utils.ParseTimeStringToTime(timeLayout, memberFistDate+"00:00:00")
	membertimes, _ := time.ParseInLocation(timeLayouts+" 15:04:05", memberFistDate+" 00:00:00", loc)
	memtime := membertimes.Unix()
	fmt.Print("会员透析日期", memtime)
	patient_type := int64(dataBody["patientType"].(float64))
	fmt.Print("病种", patient_type)
	treatmentmethod := int64(dataBody["treatmentMethod"].(float64))
	fmt.Print("治疗方式", treatmentmethod)

	//如果是血透病人
	if bloodPatient == 1 {
		_, errcode := service.GetPatientData(phone, orgid)
		if errcode == gorm.ErrRecordNotFound {
			patients := models.Patients{
				Name:              name,
				Gender:            sex,
				Birthday:          birth,
				Age:               ages,
				Phone:             phone,
				Lapseto:           lapseto,
				AdmissionNumber:   adminssionNumber,
				FirstDialysisDate: fisttime,
				Diagnose:          diagonse,
				Source:            patientsoure,
				DialysisNo:        dialysis,
				UserOrgId:         orgid,
				Status:            1,
				CreatedTime:       time.Now().Unix(),
				Avatar:            avatar,
				IdCardNo:          idCard,
				IsInfectious:      is_infectious,
			}
			err := service.CreateOldPatient(&patients)
			fmt.Print("报错", err)
			patient, err := service.GetLastOldPatient(orgid)
			fmt.Print("病人ID", patient.ID)
			err = service.AddContagions(patient.ID, patient.CreatedTime, patient.UpdatedTime, ids)
			fmt.Println("添加传染病失败", err)

			patientsNew := models.XtPatientsNew{
				Name:              name,
				Gender:            sex,
				Birthday:          birth,
				Age:               ages,
				Phone:             phone,
				Lapseto:           lapseto,
				AdmissionNumber:   adminssionNumber,
				FirstDialysisDate: fisttime,
				Diagnose:          diagonse,
				Source:            patientsoure,
				DialysisNo:        dialysis,
				UserOrgId:         orgid,
				Status:            1,
				CreatedTime:       time.Now().Unix(),
				Avatar:            avatar,
				MemberPatients:    memberpatient,
				BloodPatients:     bloodPatient,
				SlowPatients:      slowpatient,
				BloodId:           patient.ID,
				IsInfectious:      is_infectious,
				IdCardNo:          idCard,
			}
			err = service.CreateNewPatient(&patientsNew)
			fmt.Print("报错", err)
			if err != nil {
				this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
				return
			}
			this.ServeSuccessJSON(map[string]interface{}{
				"patient": patientsNew,
			})
		} else if errcode == nil {
			fmt.Print("病人已存在")
			return
		} else {

		}
	}

	fmt.Print("slowpatient", slowpatient)
	//如果是慢病病人
	if slowpatient == 1 {
		_, errcodetwo := service.GetSlowPatientData(phone, orgid)
		fmt.Print("查询慢慢是吧", errcodetwo)
		if errcodetwo == gorm.ErrRecordNotFound {
			patients := models.CdmPatients{
				Name:        name,
				Gender:      sex,
				Birthday:    birth,
				Phone:       phone,
				Diagnose:    diagonse,
				Source:      patientsoure,
				UserOrgId:   orgid,
				Status:      1,
				CreatedTime: time.Now().Unix(),
				Avatar:      avatar,
				IdCardNo:    idCard,
			}
			errcodetwo := service.CreateCdmPatient(&patients)
			fmt.Print("报错", errcodetwo)
			slowPatient, errcodetwo := service.GetOldCdmPatient(orgid)
			errcodetwo = service.AddSlowContagions(slowPatient.ID, slowPatient.CreatedTime, slowPatient.UpdatedTime, idss, orgid)
			fmt.Print("添加慢病传染病失败", errcodetwo)
			errcodethree := service.AddSlowDiseases(slowPatient.ID, slowPatient.CreatedTime, slowPatient.UpdatedTime, idsss, orgid)
			fmt.Print("添加慢性病失败", errcodethree)

			_, errcodefour := service.GetLastNewSlowPatient(phone, orgid)
			if errcodefour == gorm.ErrRecordNotFound {
				patientsNew := models.XtPatientsNew{
					Name:              name,
					Gender:            sex,
					Birthday:          birth,
					Age:               ages,
					Phone:             phone,
					Lapseto:           lapseto,
					AdmissionNumber:   adminssionNumber,
					FirstDialysisDate: fisttime,
					Diagnose:          diagonse,
					Source:            patientsoure,
					DialysisNo:        dialysis,
					UserOrgId:         orgid,
					Status:            1,
					CreatedTime:       time.Now().Unix(),
					Avatar:            avatar,
					MemberPatients:    memberpatient,
					BloodPatients:     bloodPatient,
					SlowPatients:      slowpatient,
					SlowId:            slowPatient.ID,
					IsInfectious:      is_infectious,
					IdCardNo:          idCard,
					EquitmentId:       requipmentId,
				}
				err = service.CreateNewPatient(&patientsNew)
				if err != nil {
					this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
					return
				}
				this.ServeSuccessJSON(map[string]interface{}{
					"patient": patientsNew,
				})
			} else if errcodetwo == nil {
				fmt.Print("病人已存在")
				if memberpatient == 1 {
					fmt.Print("进来没有", memberpatient)
					_, errmembercode := service.GetMemberPatientInfo(orgid, phone)
					if errmembercode == gorm.ErrRecordNotFound {
						customer := models.SgjUserCustomer{
							Name:        name,
							UserOrgId:   orgid,
							Mobile:      phone,
							Gender:      sex,
							Birthday:    birth,
							IllDate:     memtime,
							Status:      1,
							CreatedTime: time.Now().Unix(),
							Avatar:      avatar,
							IllnessId:   patient_type,
							TreatType:   treatmentmethod,
						}
						errmembercode := service.CreateMemberPatient(&customer)
						fmt.Print("创建会员病人失败", errmembercode)
						_, errmembercodetwo := service.GetMemberNewPatient(phone, orgid)
						fmt.Print("错误", errmembercodetwo)
						//获取机构的最后一条数据
						memberPatient, errmembercode := service.GetLastMemberPatient(orgid)
						if errmembercodetwo == gorm.ErrRecordNotFound {
							patientsNew := models.XtPatientsNew{
								Name:              name,
								Gender:            sex,
								Birthday:          birth,
								Age:               ages,
								Phone:             phone,
								Lapseto:           lapseto,
								AdmissionNumber:   adminssionNumber,
								FirstDialysisDate: fisttime,
								Diagnose:          diagonse,
								Source:            patientsoure,
								DialysisNo:        dialysis,
								UserOrgId:         orgid,
								Status:            1,
								CreatedTime:       time.Now().Unix(),
								Avatar:            avatar,
								MemberPatients:    memberpatient,
								BloodPatients:     bloodPatient,
								SlowPatients:      slowpatient,
								MemberId:          memberPatient.ID,
								IsInfectious:      is_infectious,
								IdCardNo:          idCard,
								MemberFistdate:    memtime,
								MemberPatienttype: patient_type,
								MemberTreatement:  treatmentmethod,
							}
							err = service.CreateNewPatient(&patientsNew)
							fmt.Print("报错-----------------------", err)
							if err != nil {
								this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
								return
							}
							this.ServeSuccessJSON(map[string]interface{}{
								"patient": patientsNew,
							})
						} else if errmembercode == nil {
							fmt.Print("病人已存在")
							return
						} else {

						}
					} else if errmembercode == nil {
						fmt.Print("病人已存在")
						return
					} else {

					}
				}
				return
			} else {

			}

		} else if errcodetwo == nil {
			fmt.Print("病人已存在")
			return
		} else {

		}

	}

	if memberpatient == 1 {
		fmt.Print("进来没有", memberpatient)
		_, errmembercode := service.GetMemberPatientInfo(orgid, phone)
		if errmembercode == gorm.ErrRecordNotFound {
			customer := models.SgjUserCustomer{
				Name:        name,
				UserOrgId:   orgid,
				Mobile:      phone,
				Gender:      sex,
				Birthday:    birth,
				IllDate:     memtime,
				Status:      1,
				CreatedTime: time.Now().Unix(),
				Avatar:      avatar,
				IllnessId:   patient_type,
				TreatType:   treatmentmethod,
			}
			errmembercode := service.CreateMemberPatient(&customer)
			fmt.Print("创建会员病人失败", errmembercode)
			_, errmembercodetwo := service.GetMemberNewPatient(phone, orgid)
			fmt.Print("错误", errmembercodetwo)
			//获取机构的最后一条数据
			memberPatient, errmembercode := service.GetLastMemberPatient(orgid)
			if errmembercodetwo == gorm.ErrRecordNotFound {
				patientsNew := models.XtPatientsNew{
					Name:              name,
					Gender:            sex,
					Birthday:          birth,
					Age:               ages,
					Phone:             phone,
					Lapseto:           lapseto,
					AdmissionNumber:   adminssionNumber,
					FirstDialysisDate: fisttime,
					Diagnose:          diagonse,
					Source:            patientsoure,
					DialysisNo:        dialysis,
					UserOrgId:         orgid,
					Status:            1,
					CreatedTime:       time.Now().Unix(),
					Avatar:            avatar,
					MemberPatients:    memberpatient,
					BloodPatients:     bloodPatient,
					SlowPatients:      slowpatient,
					MemberId:          memberPatient.ID,
					IsInfectious:      is_infectious,
					IdCardNo:          idCard,
					MemberFistdate:    memtime,
					MemberPatienttype: patient_type,
					MemberTreatement:  treatmentmethod,
				}
				err = service.CreateNewPatient(&patientsNew)
				fmt.Print("报错-----------------------", err)
				if err != nil {
					this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
					return
				}
				this.ServeSuccessJSON(map[string]interface{}{
					"patient": patientsNew,
				})
			} else if errmembercode == nil {
				fmt.Print("病人已存在")
				return
			} else {

			}
		} else if errmembercode == nil {
			fmt.Print("病人已存在")
			return
		} else {

		}
	}

}

func (this *NewDialysisApiController) GetPatientDetail() {
	id, _ := this.GetInt64("id")
	//获取病人详情信息
	detail, err := service.GetPatientDetailTwo(id)
	//获取传染病详情
	diseases := service.GetPatientDiseasesTwo(detail.BloodId)
	//获取慢病传染病
	slowDiseases, err := service.GetSlowPatientDiseases(detail.SlowId)
	//获取慢病慢性病
	cronicDiseases, err := service.GetCronicDiseases(detail.SlowId)
	//获取血透医嘱管理
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"patientDetail":  detail,
		"diseases":       diseases,
		"slowDiseases":   slowDiseases,
		"cronicDiseases": cronicDiseases,
	})
}

func (this *NewDialysisApiController) GetDoctorAdvices() {
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	adminInfo := this.GetMobileAdminUserInfo()
	orgid := adminInfo.Org.Id
	id, _ := this.GetInt64("id")
	fmt.Print("id", id)
	//跟据新表id获取老表id
	newPatientInfo, _ := service.GetPatientDetailTwo(id)
	//获取病人ID
	doctor_type, _ := this.GetInt64("type")
	fmt.Print("医嘱内型", doctor_type)
	start := this.GetString("startime")
	fmt.Print("start", start)
	startTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", start+" 00:00:00", loc)
	startime := startTimes.Unix()
	fmt.Print("开始时间", startime)
	end := this.GetString("endtime")
	fmt.Print("end", end)
	endTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", end+" 00:00:00", loc)
	endtime := endTimes.Unix()
	fmt.Print("结束时间", endtime)
	limit, _ := this.GetInt64("limit")
	page, _ := this.GetInt64("page")
	advice, total, err := service.GetNewDoctorAdvice(newPatientInfo.BloodId, doctor_type, startime, endtime, limit, page, orgid)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"advice": advice,
		"total":  total,
	})
}

func (this *NewDialysisApiController) GetDryWeight() {
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	id, _ := this.GetInt64("id")
	fmt.Print("id", id)
	patient, _ := service.GetPatientDetailTwo(id)
	start := this.GetString("startime")
	startTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", start+" 00:00:00", loc)
	startime := startTimes.Unix()
	fmt.Print("startime", startime)
	end := this.GetString("endtime")
	endTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", end+" 00:00:00", loc)
	endtime := endTimes.Unix()
	fmt.Print("endtime", endtime)
	limit, _ := this.GetInt64("limit")
	fmt.Print("limit", limit)
	page, _ := this.GetInt64("page")
	fmt.Print("page", page)
	adminInfo := this.GetMobileAdminUserInfo()
	orgid := adminInfo.Org.Id
	dryweight, total, err := service.GetDryWeight(patient.BloodId, startime, endtime, limit, page, orgid)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"dryweight": dryweight,
		"total":     total,
	})
}

func (this *NewDialysisApiController) ToSearch() {
	adminUser := this.GetMobileAdminUserInfo()
	orgId := adminUser.Org.Id
	name := this.GetString("name")
	search, err := service.ToSearch(orgId, name)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"search": search,
	})
}

func (this *NewDialysisApiController) GetCourseManagement() {
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	id, _ := this.GetInt64("id")
	fmt.Print("id", id)
	patient, _ := service.GetPatientDetailTwo(id)
	start := this.GetString("startime")
	startTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", start+" 00:00:00", loc)
	startime := startTimes.Unix()
	fmt.Print("startime", startime)
	end := this.GetString("endtime")
	endTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", end+" 00:00:00", loc)
	endtime := endTimes.Unix()
	fmt.Print("endtime", endtime)
	limit, _ := this.GetInt64("limit")
	fmt.Print("limit", limit)
	page, _ := this.GetInt64("page")
	fmt.Print("page", page)
	adminInfo := this.GetMobileAdminUserInfo()
	orgid := adminInfo.Org.Id
	management, total, err := service.GetCourseManagement(patient.BloodId, startime, endtime, limit, page, orgid)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"coursemanagement": management,
		"total":            total,
	})
}

func (this *NewDialysisApiController) DeleteCourseManagement() {
	id, _ := this.GetInt64("id")
	err := service.DeleteCouseManagement(id)
	fmt.Println("错误是什么", err)
	returnData := make(map[string]interface{}, 0)
	returnData["msg"] = "ok"
	this.ServeSuccessJSON(returnData)
	return
}

func (this *NewDialysisApiController) GetCouseManagentDetail() {
	id, _ := this.GetInt64("id")
	detail, err := service.GetCouseManagentDetail(id)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"couseDetail": detail,
	})
}

func (this *NewDialysisApiController) GetAllBloodDialysisPatient() {
	page, _ := this.GetInt64("page")
	fmt.Print("page", page)
	limit, _ := this.GetInt64("limit")
	fmt.Print("limit", limit)
	adminUserInfo := this.GetMobileAdminUserInfo()
	orgid := adminUserInfo.Org.Id
	patient, total, err := service.GetAllBloodDialysisPatient(orgid, page, limit)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"bloodpatient": patient,
		"total":        total,
	})
}

func (this *NewDialysisApiController) GetAllSlowPatent() {
	page, _ := this.GetInt64("page")
	fmt.Print("page", page)
	limit, _ := this.GetInt64("limit")
	fmt.Print("limit", limit)
	adminUserInfo := this.GetMobileAdminUserInfo()
	orgid := adminUserInfo.Org.Id
	patient, total, err := service.GetAllSlowPatient(orgid, page, limit)
	fmt.Print("err", err)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"slowpatient": patient,
		"total":       total,
	})
}

func (this *NewDialysisApiController) GetAllMemberPatient() {
	page, _ := this.GetInt64("page")
	fmt.Print("page", page)
	limit, _ := this.GetInt64("limit")
	fmt.Print("limit", limit)
	adminUserInfo := this.GetMobileAdminUserInfo()
	orgid := adminUserInfo.Org.Id
	patient, total, err := service.GetAllMemberPatient(orgid, page, limit)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"memberpatient": patient,
		"total":         total,
	})
}

func (this *NewDialysisApiController) DeleteDryWeight() {
	id, _ := this.GetInt64("id")
	err := service.DeleteDryWeight(id)
	fmt.Println("错误是什么", err)
	returnData := make(map[string]interface{}, 0)
	returnData["msg"] = "ok"
	this.ServeSuccessJSON(returnData)
	return
}

func (this *NewDialysisApiController) GetDryWeightDetail() {
	id, _ := this.GetInt64("id")
	fmt.Print("id", id)
	drydetail, err := service.GetDryWeightDetail(id)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"drydetail": drydetail,
	})
}

func (this *NewDialysisApiController) GetlongDialysisrecord() {
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	id, _ := this.GetInt64("id")
	fmt.Print("id", id)
	patient, _ := service.GetPatientDetailTwo(id)
	start := this.GetString("startime")
	startTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", start+" 00:00:00", loc)
	startime := startTimes.Unix()
	fmt.Print("startime", startime)
	end := this.GetString("endtime")
	endTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", end+" 00:00:00", loc)
	endtime := endTimes.Unix()
	fmt.Print("endtime", endtime)
	limit, _ := this.GetInt64("limit")
	fmt.Print("limit", limit)
	page, _ := this.GetInt64("page")
	fmt.Print("page", page)
	adminInfo := this.GetMobileAdminUserInfo()
	orgid := adminInfo.Org.Id
	dialysisrecord, total, err := service.GetlongDialysisrecord(patient.BloodId, startime, endtime, limit, page, orgid)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"dialysisrecord": dialysisrecord,
		"total":          total,
	})
}

func (this *NewDialysisApiController) GetRescuerecord() {
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	id, _ := this.GetInt64("id")
	fmt.Print("id", id)
	patient, _ := service.GetPatientDetailTwo(id)
	start := this.GetString("startime")
	startTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", start+" 00:00:00", loc)
	startime := startTimes.Unix()
	fmt.Print("startime", startime)
	end := this.GetString("endtime")
	endTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", end+" 00:00:00", loc)
	endtime := endTimes.Unix()
	fmt.Print("endtime", endtime)
	limit, _ := this.GetInt64("limit")
	fmt.Print("limit", limit)
	page, _ := this.GetInt64("page")
	fmt.Print("page", page)
	adminInfo := this.GetMobileAdminUserInfo()
	orgid := adminInfo.Org.Id
	rescuerecord, total, err := service.GetRescuerecord(patient.BloodId, startime, endtime, limit, page, orgid)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"rescuerecord": rescuerecord,
		"total":        total,
	})
}

func (this *NewDialysisApiController) GetScheduling() {
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	id, _ := this.GetInt64("id")
	fmt.Print("id", id)
	patient, _ := service.GetPatientDetailTwo(id)
	start := this.GetString("startime")
	startTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", start+" 00:00:00", loc)
	startime := startTimes.Unix()
	fmt.Print("startime", startime)
	end := this.GetString("endtime")
	endTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", end+" 00:00:00", loc)
	endtime := endTimes.Unix()
	fmt.Print("endtime", endtime)
	limit, _ := this.GetInt64("limit")
	fmt.Print("limit", limit)
	page, _ := this.GetInt64("page")
	fmt.Print("page", page)
	mode_type, _ := this.GetInt64("mode_types")
	fmt.Print("透析模式", mode_type)
	adminInfo := this.GetMobileAdminUserInfo()
	orgid := adminInfo.Org.Id
	scheduling, total, err := service.GetScheduling(patient.BloodId, startime, endtime, limit, page, orgid, mode_type)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"scheduling": scheduling,
		"total":      total,
	})
}

func (this *NewDialysisApiController) GetEducation() {
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	id, _ := this.GetInt64("id")
	fmt.Print("id", id)
	patient, _ := service.GetPatientDetailTwo(id)
	start := this.GetString("startime")
	startTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", start+" 00:00:00", loc)
	startime := startTimes.Unix()
	fmt.Print("startime", startime)
	end := this.GetString("endtime")
	endTimes, _ := time.ParseInLocation(timeLayout+" 15:04:05", end+" 00:00:00", loc)
	endtime := endTimes.Unix()
	fmt.Print("endtime", endtime)
	limit, _ := this.GetInt64("limit")
	fmt.Print("limit", limit)
	page, _ := this.GetInt64("page")
	fmt.Print("page", page)
	adminInfo := this.GetMobileAdminUserInfo()
	orgid := adminInfo.Org.Id
	education, total, err := service.GetEducation(patient.BloodId, startime, endtime, limit, page, orgid)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"education": education,
		"total":     total,
	})
}

func (this *NewDialysisApiController) GetSchedulingDetail() {
	id, _ := this.GetInt64("id")
	detail, err := service.GetSchedulingDetail(id)
	patient, err := service.GetSchedulingTwo(id)
	fmt.Print("errr", err)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"schedulingDetail": detail,
		"patient":          patient,
	})
}

func (this *NewDialysisApiController) DeleteScheduling() {
	id, _ := this.GetInt64("id")
	fmt.Print("id", id)
	err := service.DeleteScheduling(id)
	fmt.Println("错误是什么", err)
	returnData := make(map[string]interface{}, 0)
	returnData["msg"] = "ok"
	this.ServeSuccessJSON(returnData)
	return
}

func (this *NewDialysisApiController) GetEducationDetail() {
	id, _ := this.GetInt64("id")
	fmt.Print("id----------", id)
	education, err := service.GetEducationDetailOne(id)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"education": education,
	})
}

func (this *NewDialysisApiController) DeleteEducation() {
	id, _ := this.GetInt64("id")
	fmt.Print("id", id)
	err := service.DeleteEducation(id)
	fmt.Println("错误是什么", err)
	returnData := make(map[string]interface{}, 0)
	returnData["msg"] = "ok"
	this.ServeSuccessJSON(returnData)
	return
}

func (this *NewDialysisApiController) GetRescueRecordDetail() {
	id, _ := this.GetInt64("id")
	fmt.Print("id========", id)
	recordDetail, err := service.GetRescueRecordDetail(id)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"recordDetail": recordDetail,
	})
}

func (this *NewDialysisApiController) DeleteRescueRecord() {
	id, _ := this.GetInt64("id")
	err := service.DeleteRescueRecord(id)
	fmt.Println("错误是什么", err)
	returnData := make(map[string]interface{}, 0)
	returnData["msg"] = "ok"
	this.ServeSuccessJSON(returnData)
	return
}

func (this *NewDialysisApiController) ToSearchePatient() {
	keyword := this.GetString("keyword")
	fmt.Print("关键字", keyword)
	adminUserinfo := this.GetMobileAdminUserInfo()
	orgid := adminUserinfo.Org.Id
	patient, err := service.ToSearchePatient(keyword, orgid)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"patient": patient,
	})
}

func (this *NewDialysisApiController) SearchallPatient() {
	keyword := this.GetString("keyword")
	fmt.Print("关键字", keyword)
	adminUser := this.GetMobileAdminUserInfo()
	orgid := adminUser.Org.Id
	patient, err := service.SearchallPatient(keyword, orgid)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"patient": patient,
	})
}

func (this *NewDialysisApiController) GetSearchallBlood() {
	keyword := this.GetString("keyword")
	fmt.Print("关键字", keyword)
	adminUser := this.GetMobileAdminUserInfo()
	orgid := adminUser.Org.Id
	bloodPatient, err := service.GetSearchallBlood(keyword, orgid)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"bloodPatient": bloodPatient,
	})
}

func (this *NewDialysisApiController) GetSlowSearchePatient() {
	keyword := this.GetString("keyword")
	fmt.Print("关键字", keyword)
	adminUser := this.GetMobileAdminUserInfo()
	orgid := adminUser.Org.Id
	slowPatient, err := service.GetSlowSearchePatient(keyword, orgid)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"slowPatient": slowPatient,
	})
}

func (this *NewDialysisApiController) GetMemberSearchPatent() {
	keyword := this.GetString("keyword")
	fmt.Print("关键字", keyword)
	adminUser := this.GetMobileAdminUserInfo()
	orgid := adminUser.Org.Id
	memberPatient, err := service.GetMemberSearchPatient(keyword, orgid)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"memberPatient": memberPatient,
	})
}
