package mobile_api_controllers

import (
	"encoding/json"
	"fmt"
	"strconv"
	"time"

	"XT_New/enums"
	"XT_New/models"
	"XT_New/service"
	"XT_New/utils"
	"github.com/jinzhu/gorm"
)

// /m/api/monitor/add [post]
// @param patient_id:int
// @param order_id?:int 透析记录 ID
// @param date?:int 日期(10位时间戳)
// @param operate_time?:int 实际测量日期(10位时间戳)
// @param time?:string 时间(hh:mm) 废弃
// @param pulse_frequency?:float 脉率(P)(次/min)
// @param breathing_rated?:float 呼吸频率(R)(次/min)
// @param systolic_bp?:float 收缩压
// @param diastolic_bp?:float 舒张压
// @param bp_type?:int 血压测量类型
// @param blood_flow_volume?:float 血流量(ml/min)
// @param venous_pressure?:float 静脉压
// @param venous_pressure_unit?:string 静脉压单位
// @param arterial_pressure?:float 动脉压
// @param transmembrane_pressure?:float 跨膜压
// @param transmembrane_pressure_unit?:string 跨膜压单位
// @param ultrafiltration_rate?:float 超滤率(ml/h)
// @param ultrafiltration_volume?:float 超滤量(ml)
// @param sodium_concentration?:float 钠浓度(mmol/L)
// @param dialysate_temperature?:float 透析液温度(℃)
// @param replacement_rate?:float 置换率(ml/min)
// @param displacement_quantity?:float 置换量(L)
// @param ktv?:float KT/V(在线)
// @param symptom?:string 症状
// @param dispose?:string 处理
// @param result?:string 结果
// @param monitoring_nurse?:int 监测人
func (this *DialysisAPIController) AddMonitorRecord() {
	patientID, _ := this.GetInt64("patient_id")
	if patientID <= 0 {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	orderID, _ := this.GetInt64("order_id")
	if orderID < 0 {
		orderID = 0
	}
	date, _ := this.GetInt64("date")
	operateTime, _ := this.GetInt64("operate_time")

	// recordTime := this.GetString("time")
	if date <= 0 || operateTime <= 0 {
		date = 0
		operateTime = 0
		// recordTime = ""
	}
	fmt.Println(date)
	fmt.Println(operateTime)

	pulseFrequency, _ := this.GetFloat("pulse_frequency")
	//breathingRated, _ := this.GetFloat("breathing_rated")
	breathingRated := this.GetString("breathing_rated")
	systolicBP, _ := this.GetFloat("systolic_bp")
	diastolicBP, _ := this.GetFloat("diastolic_bp")
	BPType, _ := this.GetInt("bp_type")
	bloodFlowVolume, _ := this.GetFloat("blood_flow_volume")
	venousPressure, _ := this.GetFloat("venous_pressure")
	venousPressureType, _ := this.GetInt64("venous_pressure_type", 1)
	arterialPressure, _ := this.GetFloat("arterial_pressure")
	arterialPressureType, _ := this.GetInt64("arterial_pressure_type", 1)
	transmembranePressure, _ := this.GetFloat("transmembrane_pressure", 0)
	transmembranePressureType, _ := this.GetInt64("transmembrane_pressure_type", 1)
	ultrafiltrationRate, _ := this.GetFloat("ultrafiltration_rate")
	ultrafiltrationVolume, _ := this.GetFloat("ultrafiltration_volume")
	sodiumConcentration, _ := this.GetFloat("sodium_concentration")
	dialysateTemperature, _ := this.GetFloat("dialysate_temperature")
	temperature, _ := this.GetFloat("temperature")
	replacementRate, _ := this.GetFloat("replacement_rate")
	displacementQuantity, _ := this.GetFloat("displacement_quantity")
	ktv, _ := this.GetFloat("ktv")

	symptom := this.GetString("symptom")
	dispose := this.GetString("dispose")
	conductivity, _ := this.GetFloat("conductivity")
	displacement_flow_quantity, _ := this.GetFloat("displacement_flow_quantity")
	blood_oxygen_saturation := this.GetString("blood_oxygen_saturation")
	result := this.GetString("result")
	monitoringNurse, _ := this.GetInt64("monitoring_nurse")
	heparin, _ := this.GetFloat("heparin")
	dialysate_flow, _ := this.GetFloat("dialysate_flow")
	urr := this.GetString("urr")
	blood_sugar, _ := this.GetFloat("blood_sugar")
	monitor_anticoagulant, _ := this.GetInt64("monitor_anticoagulant")
	monitor_anticoagulant_value := this.GetString("monitor_anticoagulant_value")
	heparin_amount, _ := this.GetFloat("heparin_amount")
	blood_pressure_monitoring_site, _ := this.GetInt64("blood_pressure_monitoring_site")
	complication, _ := this.GetInt64("complication")
	accumulated_blood_volume, _ := this.GetFloat("accumulated_blood_volume")
	blood_temperature, _ := this.GetFloat("blood_temperature")
	urea_monitoring, _ := this.GetFloat("urea_monitoring")
	blood_thickness, _ := this.GetFloat("blood_thickness")
	blood_monitor, _ := this.GetFloat("blood_monitor")
	dehydration, _ := this.GetFloat("dehydration")
	filter_pressure := this.GetString("filter_pressure")
	replacement_speed, _ := this.GetFloat("replacement_speed")
	dicarbonate, _ := this.GetFloat("dicarbonate")
	is_pressure := this.GetString("is_pressure")
	heparin_one := this.GetString("heparin_one")
	ultrafiltration_rate_one := this.GetString("ultrafiltration_rate_one")

	monitor_diastolic_blood_pressure_one := this.GetString("monitor_diastolic_blood_pressure_one")
	monitor_systolic_blood_pressure_one := this.GetString("monitor_systolic_blood_pressure_one")
	monitor_pulse_frequency_one := this.GetString("monitor_pulse_frequency_one")
	blood_flow_volume_one := this.GetString("blood_flow_volume_one")
	venous_pressure_one := this.GetString("venous_pressure_one")
	arterial_pressure_one := this.GetString("arterial_pressure_one")
	transmembrane_pressure_one := this.GetString("transmembrane_pressure_one")
	ultrafiltration_volume_one := this.GetString("ultrafiltration_volume_one")
	sodium_concentration_one := this.GetString("sodium_concentration_one")
	dialysate_temperature_one := this.GetString("dialysate_temperature_one")
	replacement_rate_one := this.GetString("replacement_rate_one")
	displacement_quantity_one := this.GetString("displacement_quantity_one")
	ktv_one := this.GetString("ktv_one")
	urr_one := this.GetString("urr_one")
	conductivity_one := this.GetString("conductivity_one")
	displacement_flow_quantity_one := this.GetString("displacement_flow_quantity_one")
	blood_oxygen_saturation_one := this.GetString("blood_oxygen_saturation_one")
	dialysate_flow_one := this.GetString("dialysate_flow_one")
	blood_sugar_one := this.GetString("blood_sugar_one")
	monitor_temperature_one := this.GetString("monitor_temperature_one")
	pulse_frequency_one := this.GetString("pulse_frequency_one")
	blood_temperature_one := this.GetString("blood_temperature_one")
	urea_monitoring_one := this.GetString("urea_monitoring_one")
	blood_thickness_one := this.GetString("blood_thickness_one")
	blood_monitor_one := this.GetString("blood_monitor_one")
	replacement_speed_one := this.GetString("replacement_speed_one")
	dicarbonate_one := this.GetString("dicarbonate_one")
	accumulated_blood_volume_one := this.GetString("accumulated_blood_volume_one")
	adminInfo := this.GetMobileAdminUserInfo()
	patient, getPatientErr := service.MobileGetPatientById(adminInfo.Org.Id, patientID)
	if getPatientErr != nil {
		this.ErrorLog("获取患者信息失败:%v", getPatientErr)
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	} else if patient == nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
		return
	}

	// 查询信息规挡的设置天数

	infor, _ := service.GetDialysisInformationSetting(adminInfo.Org.Id)
	if infor.ID > 0 && infor.WeekDay > 0 {

		var cha_time int64

		timeNowStr := time.Now().Format("2006-01-02")
		timeNewDate, _ := utils.ParseTimeStringToTime("2006-01-02", timeNowStr)

		//今日的日期减去设置的日期
		cha_time = timeNewDate.Unix() - infor.WeekDay*86400

		if cha_time >= date {
			//查询审核是否允许
			infor, _ := service.GetDialysisInformationByRecordDate(patientID, date, adminInfo.Org.Id, 7)
			//申请状态不允许的情况 拒绝修改
			if infor.ApplicationStatus != 1 {
				this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInformationExist)
				return
			}

		}
	}
	record := models.MonitoringRecord{
		UserOrgId:       adminInfo.Org.Id,
		PatientId:       patientID,
		DialysisOrderId: orderID,
		MonitoringDate:  date,
		OperateTime:     operateTime,
		// MonitoringTime:            recordTime,
		PulseFrequency:                   pulseFrequency,
		BreathingRate:                    breathingRated,
		SystolicBloodPressure:            systolicBP,
		DiastolicBloodPressure:           diastolicBP,
		BloodPressureType:                int64(BPType),
		BloodFlowVolume:                  bloodFlowVolume,
		VenousPressure:                   venousPressure,
		VenousPressureType:               venousPressureType,
		ArterialPressure:                 arterialPressure,
		ArterialPressureType:             arterialPressureType,
		TransmembranePressure:            transmembranePressure,
		TransmembranePressureType:        transmembranePressureType,
		UltrafiltrationRate:              ultrafiltrationRate,
		UltrafiltrationVolume:            ultrafiltrationVolume,
		SodiumConcentration:              sodiumConcentration,
		DialysateTemperature:             dialysateTemperature,
		Temperature:                      temperature,
		ReplacementRate:                  replacementRate,
		DisplacementQuantity:             displacementQuantity,
		Ktv:                              ktv,
		Symptom:                          symptom,
		Dispose:                          dispose,
		Result:                           result,
		MonitoringNurse:                  monitoringNurse,
		Status:                           1,
		CreatedTime:                      time.Now().Unix(),
		UpdatedTime:                      time.Now().Unix(),
		Conductivity:                     conductivity,
		DisplacementFlowQuantity:         displacement_flow_quantity,
		BloodOxygenSaturation:            blood_oxygen_saturation,
		Creator:                          adminInfo.AdminUser.Id,
		Modify:                           0,
		Heparin:                          heparin,
		DialysateFlow:                    dialysate_flow,
		Urr:                              urr,
		BloodSugar:                       blood_sugar,
		MonitorAnticoagulant:             monitor_anticoagulant,
		MonitorAnticoagulantValue:        monitor_anticoagulant_value,
		BloodPressureMonitoringSite:      blood_pressure_monitoring_site,
		Complication:                     complication,
		AccumulatedBloodVolume:           accumulated_blood_volume,
		BloodTemperature:                 blood_temperature,
		UreaMonitoring:                   urea_monitoring,
		BloodThickness:                   blood_thickness,
		BloodMonitor:                     blood_monitor,
		HeparinAmount:                    heparin_amount,
		Dehydration:                      dehydration,
		FilterPressure:                   filter_pressure,
		Dicarbonate:                      dicarbonate,
		ReplacementSpeed:                 replacement_speed,
		IsPressure:                       is_pressure,
		HeparinOne:                       heparin_one,
		UltrafiltrationRateOne:           ultrafiltration_rate_one,
		MonitorDiastolicBloodPressureOne: monitor_diastolic_blood_pressure_one,
		MonitorSystolicBloodPressureOne:  monitor_systolic_blood_pressure_one,
		MonitorPulseFrequencyOne:         monitor_pulse_frequency_one,
		BloodFlowVolumeOne:               blood_flow_volume_one,
		VenousPressureOne:                venous_pressure_one,
		ArterialPressureOne:              arterial_pressure_one,
		TransmembranePressureOne:         transmembrane_pressure_one,
		UltrafiltrationVolumeOne:         ultrafiltration_volume_one,
		SodiumConcentrationOne:           sodium_concentration_one,
		DialysateTemperatureOne:          dialysate_temperature_one,
		ReplacementRateOne:               replacement_rate_one,
		DisplacementFlowQuantityOne:      displacement_flow_quantity_one,
		KtvOne:                           ktv_one,
		UrrOne:                           urr_one,
		ConductivityOne:                  conductivity_one,
		DisplacementQuantityOne:          displacement_quantity_one,
		BloodOxygenSaturationOne:         blood_oxygen_saturation_one,
		DialysateFlowOne:                 dialysate_flow_one,
		BloodSugarOne:                    blood_sugar_one,
		MonitorTemperatureOne:            monitor_temperature_one,
		PulseFrequencyOne:                pulse_frequency_one,
		BloodTemperatureOne:              blood_temperature_one,
		UreaMonitoringOne:                urea_monitoring_one,
		BloodThicknessOne:                blood_thickness_one,
		BloodMonitorOne:                  blood_monitor_one,
		ReplacementSpeedOne:              replacement_speed_one,
		DicarbonateOne:                   dicarbonate_one,
		AccumulatedBloodVolumeOne:        accumulated_blood_volume_one,
	}

	err := service.CreateMonitor(&record)
	//记录日志
	byterequest, _ := json.Marshal(record)
	monitorRecordLog := models.XtMonitorRecordLog{
		RecordDate:  record.MonitoringDate,
		PatientId:   record.PatientId,
		Module:      1,
		AdminUserId: adminInfo.AdminUser.Id,
		Ctime:       time.Now().Unix(),
		Mtime:       0,
		Status:      1,
		UserOrgId:   record.UserOrgId,
		ErrLog:      string(byterequest),
		Source:      "手机端新增血压",
	}

	service.CreateMonitorRecordLog(monitorRecordLog)

	finish := models.XtDialysisFinish{
		IsFinish:   1,
		UserOrgId:  record.UserOrgId,
		Status:     1,
		Ctime:      time.Now().Unix(),
		Mtime:      0,
		Module:     7,
		RecordDate: record.MonitoringDate,
		Sourse:     1,
		PatientId:  patientID,
	}

	dialysisFinish, err := service.GetDialysisFinish(record.UserOrgId, record.MonitoringDate, 7, patientID)
	if dialysisFinish.ID == 0 {
		service.CreateDialysisFinish(finish)
	}
	key := strconv.FormatInt(adminInfo.Org.Id, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(date, 10) + ":monitor_records"
	redis := service.RedisClient()
	//清空key 值
	redis.Set(key, "", time.Second)
	keyOne := strconv.FormatInt(adminInfo.Org.Id, 10) + ":" + strconv.FormatInt(date, 10) + ":monitor_record_list_all"
	redis.Set(keyOne, "", time.Second)
	defer redis.Close()
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMonitorCreate)
		return
	}

	this.ServeSuccessJSON(map[string]interface{}{
		"monitor": record,
	})
}

// /m/api/monitor/edit [post]
// @param patient_id:int
// @param order_id?:int 透析记录 ID
// @param date?:int 日期(10位时间戳)
// @param operate_time?:int 实际测量日期(10位时间戳)
// @param time?:string 时间(hh:mm)废弃
// @param pulse_frequency?:float 脉率(P)(次/min)
// @param breathing_rated?:float 呼吸频率(R)(次/min)
// @param systolic_bp?:float 收缩压
// @param diastolic_bp?:float 舒张压
// @param bp_type?:int 血压测量类型
// @param blood_flow_volume?:float 血流量(ml/min)
// @param venous_pressure?:float 静脉压
// @param venous_pressure_unit?:string 静脉压单位
// @param arterial_pressure?:float 动脉压
// @param transmembrane_pressure?:float 跨膜压
// @param transmembrane_pressure_unit?:string 跨膜压单位
// @param ultrafiltration_rate?:float 超滤率(ml/h)
// @param ultrafiltration_volume?:float 超滤量(ml)
// @param sodium_concentration?:float 钠浓度(mmol/L)
// @param dialysate_temperature?:float 透析液温度(℃)
// @param replacement_rate?:float 置换率(ml/min)
// @param displacement_quantity?:float 置换量(L)
// @param ktv?:float KT/V(在线)
// @param symptom?:string 症状
// @param dispose?:string 处理
// @param result?:string 结果
// @param monitoring_nurse?:int 监测人
func (this *DialysisAPIController) EditMonitorRecord() {
	patientID, _ := this.GetInt64("patient_id")
	if patientID <= 0 {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	id, _ := this.GetInt64("id")
	if id <= 0 {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	orderID, _ := this.GetInt64("order_id")
	if orderID < 0 {
		orderID = 0
	}
	date, _ := this.GetInt64("date")
	operateTime, _ := this.GetInt64("operate_time")
	if date <= 0 {
		date = 0
		operateTime = 0
	}
	pulseFrequency, _ := this.GetFloat("pulse_frequency")
	//breathingRated, _ := this.GetFloat("breathing_rated")
	breathingRated := this.GetString("breathing_rated")
	systolicBP, _ := this.GetFloat("systolic_bp")
	diastolicBP, _ := this.GetFloat("diastolic_bp")
	BPType, _ := this.GetInt("bp_type")
	bloodFlowVolume, _ := this.GetFloat("blood_flow_volume")
	venousPressure, _ := this.GetFloat("venous_pressure")
	venousPressureType, _ := this.GetInt64("venous_pressure_type", 1)
	arterialPressure, _ := this.GetFloat("arterial_pressure")
	arterialPressureType, _ := this.GetInt64("arterial_pressure", 1)
	transmembranePressure, _ := this.GetFloat("transmembrane_pressure")
	transmembranePressureType, _ := this.GetInt64("transmembrane_pressure_type", 1)
	ultrafiltrationRate, _ := this.GetFloat("ultrafiltration_rate")
	ultrafiltrationVolume, _ := this.GetFloat("ultrafiltration_volume")
	sodiumConcentration, _ := this.GetFloat("sodium_concentration")
	dialysateTemperature, _ := this.GetFloat("dialysate_temperature")
	temperature, _ := this.GetFloat("temperature")
	replacementRate, _ := this.GetFloat("replacement_rate")
	displacementQuantity, _ := this.GetFloat("displacement_quantity")
	ktv, _ := this.GetFloat("ktv")
	symptom := this.GetString("symptom")
	dispose := this.GetString("dispose")
	result := this.GetString("result")
	blood_oxygen_saturation := this.GetString("blood_oxygen_saturation")
	monitoringNurse, _ := this.GetInt64("monitoring_nurse")
	conductivity, _ := this.GetFloat("conductivity")
	displacement_flow_quantity, _ := this.GetFloat("displacement_flow_quantity")
	heparin, _ := this.GetFloat("heparin")
	dialysate_flow, _ := this.GetFloat("dialysate_flow")
	urr := this.GetString("urr")
	blood_sugar, _ := this.GetFloat("blood_sugar")
	adminInfo := this.GetMobileAdminUserInfo()
	monitor_anticoagulant, _ := this.GetInt64("monitor_anticoagulant")
	monitor_anticoagulant_value := this.GetString("monitor_anticoagulant_value")
	blood_pressure_monitoring_site, _ := this.GetInt64("blood_pressure_monitoring_site")
	complication, _ := this.GetInt64("complication")
	accumulated_blood_volume, _ := this.GetFloat("accumulated_blood_volume")
	blood_temperature, _ := this.GetFloat("blood_temperature")
	urea_monitoring, _ := this.GetFloat("urea_monitoring")
	blood_thickness, _ := this.GetFloat("blood_thickness")
	blood_monitor, _ := this.GetFloat("blood_monitor")
	heparin_amount, _ := this.GetFloat("heparin_amount")
	dehydration, _ := this.GetFloat("dehydration")
	filter_pressure := this.GetString("filter_pressure")
	replacement_speed, _ := this.GetFloat("replacement_speed")
	dicarbonate, _ := this.GetFloat("dicarbonate")
	is_pressure := this.GetString("is_pressure")
	ultrafiltration_rate_one := this.GetString("ultrafiltration_rate_one")
	heparin_one := this.GetString("heparin_one")

	monitor_diastolic_blood_pressure_one := this.GetString("monitor_diastolic_blood_pressure_one")
	monitor_systolic_blood_pressure_one := this.GetString("monitor_systolic_blood_pressure_one")
	monitor_pulse_frequency_one := this.GetString("monitor_pulse_frequency_one")
	blood_flow_volume_one := this.GetString("blood_flow_volume_one")
	venous_pressure_one := this.GetString("venous_pressure_one")
	arterial_pressure_one := this.GetString("arterial_pressure_one")
	transmembrane_pressure_one := this.GetString("transmembrane_pressure_one")
	ultrafiltration_volume_one := this.GetString("ultrafiltration_volume_one")
	sodium_concentration_one := this.GetString("sodium_concentration_one")
	dialysate_temperature_one := this.GetString("dialysate_temperature_one")
	replacement_rate_one := this.GetString("replacement_rate_one")
	displacement_quantity_one := this.GetString("displacement_quantity_one")
	ktv_one := this.GetString("ktv_one")
	urr_one := this.GetString("urr_one")
	conductivity_one := this.GetString("conductivity_one")
	displacement_flow_quantity_one := this.GetString("displacement_flow_quantity_one")
	blood_oxygen_saturation_one := this.GetString("blood_oxygen_saturation_one")
	dialysate_flow_one := this.GetString("dialysate_flow_one")
	blood_sugar_one := this.GetString("blood_sugar_one")
	monitor_temperature_one := this.GetString("monitor_temperature_one")
	pulse_frequency_one := this.GetString("pulse_frequency_one")
	blood_temperature_one := this.GetString("blood_temperature_one")
	urea_monitoring_one := this.GetString("urea_monitoring_one")
	blood_thickness_one := this.GetString("blood_thickness_one")
	blood_monitor_one := this.GetString("blood_monitor_one")
	replacement_speed_one := this.GetString("replacement_speed_one")
	dicarbonate_one := this.GetString("dicarbonate_one")
	accumulated_blood_volume_one := this.GetString("accumulated_blood_volume_one")
	monitor, err := service.GetMonitor(adminInfo.Org.Id, patientID, id)
	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	}
	if monitor == nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMonitorNotExist)
		return
	}

	monitor.Conductivity = conductivity
	monitor.DialysisOrderId = orderID
	monitor.MonitoringDate = date
	monitor.OperateTime = operateTime
	monitor.PulseFrequency = pulseFrequency
	monitor.BreathingRate = breathingRated
	monitor.SystolicBloodPressure = systolicBP
	monitor.DiastolicBloodPressure = diastolicBP
	monitor.BloodPressureType = int64(BPType)
	monitor.BloodFlowVolume = bloodFlowVolume
	monitor.VenousPressure = venousPressure
	monitor.VenousPressureType = venousPressureType
	monitor.ArterialPressure = arterialPressure
	monitor.ArterialPressureType = arterialPressureType
	monitor.TransmembranePressure = transmembranePressure
	monitor.TransmembranePressureType = transmembranePressureType
	monitor.UltrafiltrationRate = ultrafiltrationRate
	monitor.UltrafiltrationVolume = ultrafiltrationVolume
	monitor.SodiumConcentration = sodiumConcentration
	monitor.DialysateTemperature = dialysateTemperature
	monitor.Temperature = temperature
	monitor.ReplacementRate = replacementRate
	monitor.DisplacementQuantity = displacementQuantity
	monitor.Ktv = ktv
	monitor.Symptom = symptom
	monitor.Dispose = dispose
	monitor.Result = result
	monitor.MonitoringNurse = monitoringNurse
	monitor.Status = 1
	monitor.UpdatedTime = time.Now().Unix()
	monitor.DisplacementFlowQuantity = displacement_flow_quantity
	monitor.BloodOxygenSaturation = blood_oxygen_saturation
	monitor.Modify = adminInfo.AdminUser.Id
	monitor.Heparin = heparin
	monitor.DialysateFlow = dialysate_flow
	monitor.Urr = urr
	monitor.BloodSugar = blood_sugar
	monitor.MonitorAnticoagulant = monitor_anticoagulant
	monitor.MonitorAnticoagulantValue = monitor_anticoagulant_value
	monitor.BloodPressureMonitoringSite = blood_pressure_monitoring_site
	monitor.Complication = complication
	monitor.AccumulatedBloodVolume = accumulated_blood_volume
	monitor.BloodMonitor = blood_monitor
	monitor.UreaMonitoring = urea_monitoring
	monitor.BloodThickness = blood_thickness
	monitor.BloodTemperature = blood_temperature
	monitor.HeparinAmount = heparin_amount
	monitor.Dehydration = dehydration
	monitor.FilterPressure = filter_pressure
	monitor.IsPressure = is_pressure
	monitor.UltrafiltrationRateOne = ultrafiltration_rate_one
	monitor.HeparinOne = heparin_one
	monitor.MonitorDiastolicBloodPressureOne = monitor_diastolic_blood_pressure_one
	monitor.MonitorSystolicBloodPressureOne = monitor_systolic_blood_pressure_one
	monitor.MonitorPulseFrequencyOne = monitor_pulse_frequency_one
	monitor.BloodFlowVolumeOne = blood_flow_volume_one
	monitor.VenousPressureOne = venous_pressure_one
	monitor.ArterialPressureOne = arterial_pressure_one
	monitor.TransmembranePressureOne = transmembrane_pressure_one
	monitor.UltrafiltrationVolumeOne = ultrafiltration_volume_one
	monitor.SodiumConcentrationOne = sodium_concentration_one
	monitor.DialysateTemperatureOne = dialysate_temperature_one
	monitor.ReplacementRateOne = replacement_rate_one
	monitor.DisplacementFlowQuantityOne = displacement_flow_quantity_one
	monitor.KtvOne = ktv_one
	monitor.UrrOne = urr_one
	monitor.ConductivityOne = conductivity_one
	monitor.DisplacementQuantityOne = displacement_quantity_one
	monitor.BloodOxygenSaturationOne = blood_oxygen_saturation_one
	monitor.DialysateFlowOne = dialysate_flow_one
	monitor.BloodSugarOne = blood_sugar_one
	monitor.MonitorTemperatureOne = monitor_temperature_one
	monitor.PulseFrequencyOne = pulse_frequency_one
	monitor.BloodTemperatureOne = blood_temperature_one
	monitor.UreaMonitoringOne = urea_monitoring_one
	monitor.BloodThicknessOne = blood_thickness_one
	monitor.BloodMonitorOne = blood_monitor_one
	monitor.ReplacementSpeedOne = replacement_speed_one
	monitor.DicarbonateOne = dicarbonate_one
	monitor.AccumulatedBloodVolumeOne = accumulated_blood_volume_one

	// 查询信息规挡的设置天数

	infor, _ := service.GetDialysisInformationSetting(monitor.MonitoringDate)
	if infor.ID > 0 && infor.WeekDay > 0 {

		var cha_time int64

		timeNowStr := time.Now().Format("2006-01-02")
		timeNewDate, _ := utils.ParseTimeStringToTime("2006-01-02", timeNowStr)

		//今日的日期减去设置的日期
		cha_time = timeNewDate.Unix() - infor.WeekDay*86400

		if cha_time >= monitor.MonitoringDate {
			//查询审核是否允许
			infor, _ := service.GetDialysisInformationByRecordDate(id, monitor.MonitoringDate, monitor.UserOrgId, 7)
			//申请状态不允许的情况 拒绝修改
			if infor.ApplicationStatus != 1 {
				this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInformationExist)
				return
			}

		}
	}
	monitor.ReplacementSpeed = replacement_speed
	monitor.Dicarbonate = dicarbonate
	err = service.UpdateMonitor(monitor)
	//记录日志
	byterequest, _ := json.Marshal(monitor)
	monitorRecordLog := models.XtMonitorRecordLog{
		RecordDate:  monitor.MonitoringDate,
		PatientId:   monitor.PatientId,
		Module:      3,
		AdminUserId: adminInfo.AdminUser.Id,
		Ctime:       time.Now().Unix(),
		Mtime:       0,
		Status:      1,
		UserOrgId:   monitor.UserOrgId,
		ErrLog:      string(byterequest),
		Source:      "手机端修改监测",
	}
	service.CreateMonitorRecordLog(monitorRecordLog)

	key := strconv.FormatInt(adminInfo.Org.Id, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(date, 10) + ":monitor_records"
	redis := service.RedisClient()
	//清空key 值
	redis.Set(key, "", time.Second)
	keyOne := strconv.FormatInt(adminInfo.Org.Id, 10) + ":" + strconv.FormatInt(date, 10) + ":monitor_record_list_all"
	redis.Set(keyOne, "", time.Second)

	defer redis.Close()

	if err != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMonitorUpdate)
		return
	}

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

// /m/api/monitor/delete [post]
// @param record_id:int
// @param patient_id:int
func (this *DialysisAPIController) DeleteMonitor() {
	recordID, _ := this.GetInt64("record_id")
	patientID, _ := this.GetInt64("patient_id")
	if recordID <= 0 || patientID <= 0 {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	adminInfo := this.GetMobileAdminUserInfo()
	patient, getPatientErr := service.MobileGetPatientById(adminInfo.Org.Id, patientID)
	if getPatientErr != nil {
		this.ErrorLog("获取患者信息失败:%v", getPatientErr)
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	} else if patient == nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
		return
	}

	monitor, getMonitorErr := service.GetMonitor(adminInfo.Org.Id, patientID, recordID)
	if getMonitorErr != nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	}

	// 查询信息规挡的设置天数

	infor, _ := service.GetDialysisInformationSetting(adminInfo.Org.Id)
	if infor.ID > 0 && infor.WeekDay > 0 {

		var cha_time int64

		timeNowStr := time.Now().Format("2006-01-02")
		timeNewDate, _ := utils.ParseTimeStringToTime("2006-01-02", timeNowStr)

		//今日的日期减去设置的日期
		cha_time = timeNewDate.Unix() - infor.WeekDay*86400

		if cha_time >= monitor.MonitoringDate {
			//查询审核是否允许
			infor, _ := service.GetDialysisInformationByRecordDate(monitor.PatientId, monitor.MonitoringDate, monitor.UserOrgId, 7)
			//申请状态不允许的情况 拒绝修改
			if infor.ApplicationStatus != 1 {
				this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInformationExist)
				return
			}

		}
	}
	err := service.DisableMonitor(adminInfo.Org.Id, patientID, recordID, adminInfo.AdminUser.Id)

	//记录日志
	byterequest, _ := json.Marshal(monitor)
	recordLog := models.XtMonitorRecordLog{
		RecordDate:  monitor.MonitoringDate,
		PatientId:   monitor.PatientId,
		Module:      4,
		AdminUserId: adminInfo.AdminUser.Id,
		Ctime:       time.Now().Unix(),
		Mtime:       0,
		Status:      1,
		UserOrgId:   monitor.UserOrgId,
		ErrLog:      string(byterequest),
		Source:      "手机端删除监测",
	}
	service.CreateMonitorRecordLog(recordLog)

	orgid := this.GetMobileAdminUserInfo().Org.Id
	redis := service.RedisClient()
	key := strconv.FormatInt(adminInfo.Org.Id, 10) + ":" + strconv.FormatInt(monitor.MonitoringDate, 10) + ":monitor_record_list_all"
	redis.Set(key, "", time.Second)
	keyOne := strconv.FormatInt(orgid, 10) + ":" + strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(monitor.MonitoringDate, 10) + ":monitor_records"
	redis.Set(keyOne, "", time.Second)
	redis.Close()

	if err != nil {
		this.ErrorLog("删除透析监测记录失败:%v", err)
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBDelete)
		return
	}
	this.ServeSuccessJSON(map[string]interface{}{
		"record_id": monitor.ID,
	})
}

// /m/api/schedule/urgentinit [get]
func (this *DialysisAPIController) UrgentScheduleInitData() {
	schedule_type, _ := this.GetInt("type", 0)
	adminUserInfo := this.GetMobileAdminUserInfo()
	today := utils.ZeroHourTimeOfDay(time.Now())
	//获取所有病人且没有上机记录的病人
	patients, getPatientsErr := service.MobileGetAllPatientsForUrgentSchedule(adminUserInfo.Org.Id, today.Unix())
	if getPatientsErr != nil {
		this.ErrorLog("获取所有患者失败:%v", getPatientsErr)
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	}
	modes, getModesErr := service.MobileGetAllTrearmentModesForUrgentSchedule()
	if getModesErr != nil {
		this.ErrorLog("获取所有治疗模式失败:%v", getModesErr)
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	}
	deviceNumbers, getDeviceNumbersErr := service.GetAllAvaildDeviceNumbers(adminUserInfo.Org.Id, today.Unix(), schedule_type)
	if getDeviceNumbersErr != nil {
		this.ErrorLog("获取所有床位失败:%v", getDeviceNumbersErr)
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	}
	schedules, getSchedulesErr := service.MobileGetOtherSchedulesForUrgentSchedule(adminUserInfo.Org.Id, today.Unix(), schedule_type)
	if getSchedulesErr != nil {
		this.ErrorLog("获取所有排班失败:%v", getSchedulesErr)
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	}

	this.ServeSuccessJSON(map[string]interface{}{
		"patients":       patients,
		"modes":          modes,
		"device_numbers": deviceNumbers,
		"schedules":      schedules,
	})
}

// /m/api/schedule/urgentadd [post]
// @param patient_id:int
// @param schedule_type:int
// @param mode:int
// @param bed:int
func (this *DialysisAPIController) AddUrgentSchedule() {
	patientID, _ := this.GetInt64("patient_id")
	scheduleType, _ := this.GetInt64("schedule_type")
	modeID, _ := this.GetInt64("mode")
	bedID, _ := this.GetInt64("bed")
	if patientID <= 0 || scheduleType < 1 || scheduleType > 3 || modeID < 1 || modeID > 14 || bedID <= 0 {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	adminUserInfo := this.GetMobileAdminUserInfo()
	patient, getPatientErr := service.GetPatientByID(adminUserInfo.Org.Id, patientID)
	if getPatientErr != nil {
		this.ErrorLog("获取患者信息失败:%v", getPatientErr)
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	} else if patient == nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
		return
	}

	deviceNumber, getDeviceNumberErr := service.GetDeviceNumberByID(adminUserInfo.Org.Id, bedID)
	if getDeviceNumberErr != nil {
		this.ErrorLog("获取床位失败:%v", getDeviceNumberErr)
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	} else if deviceNumber == nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotExist)
		return
	}

	scheduleDate := utils.ZeroHourTimeOfDay(time.Now())
	weekday := int64(scheduleDate.Weekday())
	if weekday == 0 {
		weekday = 7
	}

	//判断这个人今天是否有排班
	daySchedule, dayScheduleErr := service.GetDayScheduleTwo(adminUserInfo.Org.Id, scheduleDate.Unix(), patientID)

	//判断当前时段该床位是否有排班
	schedule, err := service.GetDayScheduleByBedid(adminUserInfo.Org.Id, scheduleDate.Unix(), bedID, scheduleType)
	if dayScheduleErr == gorm.ErrRecordNotFound { //今天没有排班
		if err == gorm.ErrRecordNotFound { //空床位
			// 创建排班数据
			newSchedule := &models.Schedule{
				UserOrgId:    adminUserInfo.Org.Id,
				PartitionId:  deviceNumber.ZoneID,
				BedId:        deviceNumber.ID,
				PatientId:    patientID,
				ScheduleDate: scheduleDate.Unix(),
				ScheduleType: scheduleType,
				ScheduleWeek: weekday,
				ModeId:       modeID,
				Status:       1,
				CreatedTime:  time.Now().Unix(),
				UpdatedTime:  time.Now().Unix(),
				IsExport:     5,
			}
			createErr := service.CreateScheduleTwo(newSchedule)

			//记录日志
			byterequest, _ := json.Marshal(newSchedule)
			scheduleLog := models.XtScheduleLog{
				UserOrgId:   adminUserInfo.Org.Id,
				RecordDate:  scheduleDate.Unix(),
				Status:      1,
				PatientId:   patientID,
				Ctime:       time.Now().Unix(),
				Mtime:       0,
				ErrLog:      string(byterequest),
				Source:      "手机端新增排班",
				Module:      1,
				AdminUserId: adminUserInfo.AdminUser.Id,
			}

			service.CreateScheduleLog(scheduleLog)
			service.UpdateRepeatSchStatus(adminUserInfo.Org.Id, scheduleDate.Unix())
			redis := service.RedisClient()
			timeStr := time.Now().Format("2006-01-02")
			key := "scheduals_" + timeStr + "_" + strconv.FormatInt(adminUserInfo.Org.Id, 10)
			redis.Set(key, "", time.Second)
			//处方
			keyOne := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":prescriptions_list_all"
			redis.Set(keyOne, "", time.Second)

			//医嘱
			keyTwo := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":advice_list_all"
			redis.Set(keyTwo, "", time.Second)

			keySix := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":assessment_befores_list_all"
			redis.Set(keySix, "", time.Second)
			keyThree := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":assessment_after_dislysis_list_all"
			redis.Set(keyThree, "", time.Second)

			keyFour := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":monitor_record_list_all"
			redis.Set(keyFour, "", time.Second)

			keyFive := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":treatment_summarys_list_all"
			redis.Set(keyFive, "", time.Second)

			keySeven := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + strconv.FormatInt(scheduleDate.Unix(), 10) + ":dialysis_orders_list_all"
			redis.Set(keySeven, "", time.Second)
			redis.Close()
			if createErr != nil {
				this.ErrorLog("紧急排班失败:%v", createErr)
				this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateScheduleFail)
				return
			} else {
				this.ServeSuccessJSON(map[string]interface{}{
					"schedule": newSchedule,
					"patient":  patient,
				})
			}
		} else if err == nil {
			if schedule.ID > 0 && schedule.DialysisOrder.ID == 0 { //该床位有排班没上机记录
				this.ServeFailJSONWithSGJErrorCode(enums.ErrorSchedualcRepeatBed)
			} else if schedule.ID > 0 && schedule.DialysisOrder.ID > 0 { //有排班且有上机记录
				this.ServeFailJSONWithSGJErrorCode(enums.ErrorDialysisOrderRepeatBed)
				return
			}
		} else if err != nil {
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
			return
		}

	} else if dayScheduleErr == nil { //今天有排班

		if daySchedule.ID > 0 && daySchedule.DialysisOrder.ID <= 0 { //今天有排班但是没上机

			if err == gorm.ErrRecordNotFound { //空床位
				// 修改了床位逻辑
				daySchedule, _ := service.GetDayScheduleTwo(adminUserInfo.Org.Id, scheduleDate.Unix(), patientID)
				if daySchedule.ID > 0 {
					daySchedule.PartitionId = deviceNumber.ZoneID
					daySchedule.BedId = bedID
					daySchedule.ModeId = modeID
					daySchedule.ScheduleType = scheduleType
					daySchedule.UpdatedTime = time.Now().Unix()
					err := service.UpdateSchedule(&daySchedule)

					//记录日志
					byterequest, _ := json.Marshal(daySchedule)
					scheduleLog := models.XtScheduleLog{
						UserOrgId:   adminUserInfo.Org.Id,
						RecordDate:  scheduleDate.Unix(),
						Status:      1,
						PatientId:   patientID,
						Ctime:       time.Now().Unix(),
						Mtime:       0,
						ErrLog:      string(byterequest),
						Source:      "手机端修改排班",
						Module:      1,
						AdminUserId: adminUserInfo.AdminUser.Id,
					}
					service.CreateScheduleLog(scheduleLog)
					service.UpdateRepeatSchStatus(adminUserInfo.Org.Id, scheduleDate.Unix())

					redis := service.RedisClient()
					timeStr := time.Now().Format("2006-01-02")
					key := "scheduals_" + timeStr + "_" + strconv.FormatInt(adminUserInfo.Org.Id, 10)
					redis.Set(key, "", time.Second)
					//处方
					keyOne := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":prescriptions_list_all"
					redis.Set(keyOne, "", time.Second)

					//医嘱
					keyTwo := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":advice_list_all"
					redis.Set(keyTwo, "", time.Second)

					keySix := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":assessment_befores_list_all"
					redis.Set(keySix, "", time.Second)
					keyThree := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":assessment_after_dislysis_list_all"
					redis.Set(keyThree, "", time.Second)

					keyFour := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":monitor_record_list_all"
					redis.Set(keyFour, "", time.Second)

					keyFive := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":treatment_summarys_list_all"
					redis.Set(keyFive, "", time.Second)

					keySeven := strconv.FormatInt(adminUserInfo.Org.Id, 10) + ":" + ":dialysis_orders_list_all"
					redis.Set(keySeven, "", time.Second)
					redis.Close()
					if err != nil {
						this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
						return
					}
					this.ServeSuccessJSON(map[string]interface{}{
						"schedule": daySchedule,
						"patient":  patient,
					})
				}
			} else if err == nil { //该床位被人霸占
				if schedule.ID > 0 && schedule.DialysisOrder.ID == 0 { //霸占该床位的病人有排班没上机记录
					this.ServeFailJSONWithSGJErrorCode(enums.ErrorSchedualcRepeatBed)
					return
				} else if schedule.ID > 0 && schedule.DialysisOrder.ID > 0 { //霸占该床位的病人有排班且有上机记录
					this.ServeFailJSONWithSGJErrorCode(enums.ErrorDialysisOrderRepeatBed)
					return
				}
			} else if err != nil {
				this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
				return
			}

		} else if daySchedule.ID > 0 && daySchedule.DialysisOrder.ID > 0 { //今天有排班且上机
			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientDialysisOrder)
			return
		}
	}

}