package controllers

import (
	"Xcx_New/enums"
	"Xcx_New/models"
	"Xcx_New/service"
	"fmt"
	"github.com/astaxie/beego"
	"time"
)

type HisHospitalApiController struct {
	BaseAuthAPIController
}

func HisHospitalManagerApiRegistRouters() {

	beego.Router("/api/hishospitalpatient/list", &HisHospitalApiController{}, "get:GetHisHospitalPatientList")

	//beego.Router("/api/hishospitalprescription/list", &HisHospitalApiController{}, "get:GetHisHospitalPrescriptionList")

	beego.Router("/api/hospitalcharge/list", &HisHospitalApiController{}, "get:GetHisHospitalChargePatientList")
	beego.Router("/api/hospotalcharge/info", &HisHospitalApiController{}, "get:GetHisHospitalChargePatientInfo")

	beego.Router("/api/hospitalprescription/list", &HisHospitalApiController{}, "get:GetHisHospitalPrescriptionList")
	//beego.Router("/api/hishospitalprescription/info", &HisApiController{}, "get:GetHisHospitalPrescriptionInfo")

	//beego.Router("/api/hisprescription/create", &HisApiController{}, "post:CreateHisPrescription")
	//beego.Router("/api/hisprescription/edit", &HisApiController{}, "post:EditHisPrescription")

	//beego.Router("/api/doctorworkstation/casehistory/list", &HisApiController{}, "get:GetHisPatientCaseHistoryList")
	//beego.Router("/api/doctorworkstation/casehistory/get", &HisApiController{}, "get:GetHisPatientCaseHistory")
	//beego.Router("/api/doctorworkstation/casehistory/create", &HisApiController{}, "get:CreateHisPatientCaseHistory")

	//beego.Router("/api/doctorworkstation/casehistorytemplate/create", &HisApiController{}, "get:CreateCaseHistoryTemplate")
	//beego.Router("/api/doctorworkstation/casehistorytemplate/get", &HisApiController{}, "get:GetCaseHistoryTemplate")

	//beego.Router("/api/hisorder/list", &HisApiController{}, "get:GetHisOrderList")
	//beego.Router("/api/hisorder/get", &HisApiController{}, "get:GetHisOrder")

	//beego.Router("/api/register/get", &HisApiController{}, "get:GetRegisterInfo")
	//
	//beego.Router("/api/upload/get", &HisApiController{}, "get:GetUploadInfo")
	//
	//beego.Router("/api/refund/post", &HisApiController{}, "post:Refund")
	//
	//beego.Router("/api/medicalinsurance/config", &HisApiController{}, "get:GetMedicalInsuranceConfig")
	//
	//beego.Router("/api/doctor/list", &HisApiController{}, "get:GetAdminUsers")

	//beego.Router("/api/medicalinsurance/config", &HisApiController{}, "get:GetMedicalInsuranceConfig")
	//新增附加费用
	//beego.Router("/api/his/additionalcharge", &HisApiController{}, "Post:AdditionalCharge")
	//beego.Router("/api/additionalcharge/get", &HisApiController{}, "Get:GetAdditionalcharge")
	//beego.Router("/api/hisprescription/get", &HisApiController{}, "Get:GetLastOrNextHisPrescription")
	//beego.Router("/api/callhisprescription/get", &HisApiController{}, "Get:GetCallHisPrescription")
	//beego.Router("/api/dayprescription/get", &HisApiController{}, "get:GetHisDayPrescription")
	//beego.Router("/api/charge/list", &HisApiController{}, "get:GetHisChargePatientList")
	//beego.Router("/api/charge/info", &HisApiController{}, "get:GetHisChargePatientInfo")
	//beego.Router("/api/unregister/list", &HisApiController{}, "get:GetHisUnRegisterPatientList")
	//beego.Router("/api/orderdetail/get", &HisApiController{}, "get:GetAllOrderDetail")
	//beego.Router("/api/orderdetaicollect/get", &HisApiController{}, "get:GetAllOrderDetailCollect")
	//beego.Router("/api/cost/compare", &HisApiController{}, "get:GetMedicalInsuranceCostCompareList")
	//beego.Router("/api/record/list", &HisApiController{}, "get:GetPutOnRecordList")
	//
	//beego.Router("/api/comparedata/get", &HisApiController{}, "get:GetCompareData")
	//beego.Router("/api/fapiao/get", &HisApiController{}, "get:GetFaPiaoData")
	//
	//beego.Router("/api/incomestatistics/get", &HisApiController{}, "get:GetIncomeStatisticsData")
	//
	//beego.Router("/api/postprojectinformation", &HisApiController{}, "post:PostProjectInformation")

}

func (c *HisHospitalApiController) GetHisHospitalPatientList() {
	record_date := c.GetString("record_date")
	types, _ := c.GetInt64("type", 0)

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", record_date+" 00:00:00", loc)
	if err != nil {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	recordDateTime := theTime.Unix()
	adminInfo := c.GetAdminUserInfo()
	var patients []*service.HospitalPatient
	tempPatients, _ := service.GetHisHospitalPatientList(adminInfo.CurrentOrgId, recordDateTime)

	for _, item := range tempPatients {
		if item.HisHospitalCheckRecord.ID > 0 && item.HisHospitalCheckRecord.InHospitalStatus == 1 && item.HisHospitalCheckRecord.OutHospitalStatus == 0 {
			patients = append(patients, item)

		}

	}

	var total_one int64
	var total_two int64

	for _, item := range patients {

		if item.VMHisPrescriptionInfo.ID == 0 {
			total_one = total_one + 1
		}
		if item.VMHisPrescriptionInfo.ID > 0 {
			total_two = total_two + 1
		}
	}

	adminUserInfo, _ := service.GetAdminUserInfoByID(adminInfo.CurrentOrgId, adminInfo.AdminUser.Id)
	doctors, _ := service.GetHisAdminUserDoctors(adminInfo.CurrentOrgId)
	department, _ := service.GetAllDepartMent(adminInfo.CurrentOrgId)

	if types == 0 {
		c.ServeSuccessJSON(map[string]interface{}{
			"list":       patients,
			"total_one":  total_one,
			"total_two":  total_two,
			"info":       adminUserInfo,
			"doctors":    doctors,
			"department": department,
		})

	} else if types == 1 { //未就诊
		var patientsOne []*service.HospitalPatient
		for _, item := range tempPatients {
			if item.VMHisPrescriptionInfo.ID == 0 {
				patientsOne = append(patientsOne, item)
			}
		}
		c.ServeSuccessJSON(map[string]interface{}{
			"list":       patientsOne,
			"total_one":  total_one,
			"total_two":  total_two,
			"info":       adminUserInfo,
			"doctors":    doctors,
			"department": department,
		})

	} else if types == 2 { //已就诊
		var patientsTwo []*service.HospitalPatient
		for _, item := range tempPatients {

			if item.VMHisPrescriptionInfo.ID > 0 {
				patientsTwo = append(patientsTwo, item)
			}
		}
		c.ServeSuccessJSON(map[string]interface{}{
			"list":       patientsTwo,
			"total_one":  total_one,
			"total_two":  total_two,
			"info":       adminUserInfo,
			"doctors":    doctors,
			"department": department,
		})
	}
}

func (c *HisHospitalApiController) GetHisHospitalChargePatientList() {
	record_date := c.GetString("record_date")
	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", record_date+" 00:00:00", loc)
	if err != nil {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	recordDateTime := theTime.Unix()
	adminInfo := c.GetAdminUserInfo()
	//tempPatients, _ := service.GetAllChargeHisPatientList(adminInfo.CurrentOrgId, "", recordDateTime)
	tempPatients, _ := service.GetNewAllChargeHisHospitalPatientList(adminInfo.CurrentOrgId, recordDateTime)

	var patients []*service.HospitalPatient
	for _, item := range tempPatients {
		fmt.Println(item.ID)
		if item.HisHospitalCheckRecord.ID > 0 && item.HisHospitalCheckRecord.InHospitalStatus == 1 &&
			item.HisHospitalCheckRecord.HisHospitalOrder.ID == 0 {
			patients = append(patients, item)
		}
	}

	c.ServeSuccessJSON(map[string]interface{}{
		"list":     patients,
		"list_two": patients,
	})
}

func (c *HisHospitalApiController) GetHisHospitalChargePatientInfo() {
	patient_id, _ := c.GetInt64("patient_id")
	his_patient_id, _ := c.GetInt64("his_patient_id")
	record_date := c.GetString("record_date")
	start_time := c.GetString("start_time")
	end_time := c.GetString("end_time")
	order_status, _ := c.GetInt64("type", 0)
	p_type, _ := c.GetInt64("p_type", 0)

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", record_date+" 00:00:00", loc)
	if err != nil {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	recordDateTime := theTime.Unix()

	startTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
	if err != nil {

	}
	startRecordDateTime := startTime.Unix()

	endTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
	if err != nil {

	}
	endRecordDateTime := endTime.Unix()

	admin := c.GetAdminUserInfo()
	his_patient_info, _ := service.GetNewHisHospitalPatientInfo(his_patient_id)
	xt_patient_info, _ := service.GetXTPatientInfo(admin.CurrentOrgId, patient_id)

	var prescriptions []*models.HisPrescription

	if order_status == 1 || order_status == 0 {
		prescriptions, _ = service.GetUnChargeHisHospitalPrescriptionFive(admin.CurrentOrgId, patient_id, his_patient_id, recordDateTime)
	} else if order_status == 2 {
		prescriptions, _ = service.GetChargeHisHospitalPrescriptionFive(admin.CurrentOrgId, patient_id, his_patient_id, recordDateTime)
	}

	var monthPrescriptions []*models.HisPrescription
	if order_status == 1 || order_status == 0 {
		monthPrescriptions, _ = service.GetUnChargeMonthHisPrescriptionThree(admin.CurrentOrgId, patient_id, startRecordDateTime, endRecordDateTime, p_type)
	} else if order_status == 2 {
		monthPrescriptions, _ = service.GetChargeMonthHisPrescriptionFour(admin.CurrentOrgId, patient_id, startRecordDateTime, endRecordDateTime, p_type)
	}
	case_history, _ := service.GetHisPatientCaseHistoryInfo(admin.CurrentOrgId, patient_id, recordDateTime)
	patientPrescriptionInfo, _ := service.FindPatientPrescriptionInfo(admin.CurrentOrgId, patient_id, recordDateTime, p_type)
	order, _ := service.GetNewHisOrder(admin.CurrentOrgId, his_patient_info.Number, patient_id)
	doctors, _ := service.GetHisAdminUserDoctors(admin.CurrentOrgId)
	//获取所有科室信息
	department, _ := service.GetAllDepartMent(admin.CurrentOrgId)

	c.ServeSuccessJSON(map[string]interface{}{
		"his_info":            his_patient_info,
		"xt_info":             xt_patient_info,
		"prescription":        prescriptions,
		"case_history":        case_history,
		"info":                patientPrescriptionInfo,
		"month_prescriptions": monthPrescriptions,
		"order":               order,
		"doctors":             doctors,
		"department":          department,
	})
	return

}

func (c *HisHospitalApiController) GetHisHospitalrescriptionList() {
	record_date := c.GetString("record_date")
	keywords := c.GetString("keywords")
	page, _ := c.GetInt64("page")
	limit, _ := c.GetInt64("limit")

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", record_date+" 00:00:00", loc)
	if err != nil {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	recordDateTime := theTime.Unix()
	adminInfo := c.GetAdminUserInfo()
	prescriptionOrder, err, total := service.GetHisPatientPrescriptionList(adminInfo.CurrentOrgId, keywords, recordDateTime, page, limit)

	//adminInfo := c.GetAdminUserInfo()
	//prescriptionOrder, err := service.GetHisPrescriptionOrderList(adminInfo.CurrentOrgId)
	//fmt.Println(prescriptionOrder)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"order": prescriptionOrder,
			"total": total,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	}
}

func (c *HisHospitalApiController) GetHisHospitalPrescriptionList() {
	record_date := c.GetString("record_date")
	keywords := c.GetString("keywords")
	page, _ := c.GetInt64("page")
	limit, _ := c.GetInt64("limit")

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")
	theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", record_date+" 00:00:00", loc)
	if err != nil {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	recordDateTime := theTime.Unix()
	adminInfo := c.GetAdminUserInfo()
	prescriptionOrder, err, total := service.GetHisHospitalPatientPrescriptionList(adminInfo.CurrentOrgId, keywords, recordDateTime, page, limit)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"order": prescriptionOrder,
			"total": total,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return
	}

}