package controllers

import (
	"XT_New/models"
	"XT_New/service"
	"XT_New/utils"
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego"
	"io/ioutil"
)

type GdybController struct {
	BaseAuthAPIController
}

func GdybRegistRouters() {
	beego.Router("/gdyb/one", &GdybController{}, "get:PostOne")
	beego.Router("/gdyb/two", &GdybController{}, "get:PostTwo")
	beego.Router("/gdyb/three", &GdybController{}, "get:PostThree")
	beego.Router("/gdyb/four", &GdybController{}, "get:PostFour")
	beego.Router("/gdyb/five", &GdybController{}, "post:PostFive")
	beego.Router("/gdyb/six", &GdybController{}, "get:PostSix")
	beego.Router("/gdyb/seven", &GdybController{}, "get:PostSeven")
	beego.Router("/gdyb/eight", &GdybController{}, "get:PostEight")
	beego.Router("/gdyb/nine", &GdybController{}, "get:PostNine")

	//beego.Router("/gdyb/tem", &GdybController{}, "get:PostTen")

}

//provinces, _ := service.GetDistrictsByUpid(0)21

type Result struct {
	ErrMsg      interface{} `json:"err_msg"`
	InfRefmsgid string      `json:"inf_refmsgid"`
	Infcode     int64       `json:"infcode"`
	Output      struct {
		Baseinfo struct {
			Age         float64 `json:"age"`
			Brdy        string  `json:"brdy"`
			Certno      string  `json:"certno"`
			Gend        string  `json:"gend"`
			Naty        string  `json:"naty"`
			PsnCertType string  `json:"psn_cert_type"`
			PsnName     string  `json:"psn_name"`
			PsnNo       string  `json:"psn_no"`
		} `json:"baseinfo"`
		Idetinfo []interface{} `json:"idetinfo"`
		Iinfo    []struct {
			Balc                   int64       `json:"balc"`
			CvlservFlag            string      `json:"cvlserv_flag"`
			EmpName                string      `json:"emp_name"`
			InsuplcAdmdvs          string      `json:"insuplc_admdvs"`
			Insutype               string      `json:"insutype"`
			PausInsuDansuplcAdmdvs string      `json:"paus_insu_dansuplc_admdvs"`
			PausInsuDate           interface{} `json:"paus_insu_date"`
			PsnInsuDate            string      `json:"psn_insu_date"`
			PsnInsuStas            string      `json:"psn_insu_stas"`
			PsnType                string      `json:"psn_type"`
		} `json:"iinfo"`
	} `json:"output"`
	RefmsgTime  string      `json:"refmsg_time"`
	RespondTime string      `json:"respond_time"`
	Signtype    interface{} `json:"signtype"`
	WarnInfo    interface{} `json:"warn_info"`
}

func (c *GdybController) PostOne() {
	certNo := c.GetString("cert_no")
	org_name := c.GetString("org_name")
	doctor := c.GetString("doctor")
	fixmedins_code := c.GetString("fixmedins_code")

	result := service.Gdyb1101(certNo, org_name, doctor, fixmedins_code)

	var dat map[string]interface{}
	if err := json.Unmarshal([]byte(result), &dat); err == nil {
	} else {
	}

	c.ServeSuccessJSON(map[string]interface{}{
		"pre": dat,
	})
}

func (c *GdybController) PostTwo() {
	psnNo := c.GetString("psn_no")
	insutype := c.GetString("insutype")
	certNo := c.GetString("cert_no")
	org_name := c.GetString("org_name")
	doctor := c.GetString("doctor")
	ipt_otp_no := c.GetString("ipt_otp_no")
	dept := c.GetString("dept")
	fixmedins_code := c.GetString("fixmedins_code")

	result := service.Gdyb2201(psnNo, insutype, certNo, org_name, doctor, ipt_otp_no, dept, fixmedins_code)

	var dat map[string]interface{}
	if err := json.Unmarshal([]byte(result), &dat); err == nil {
		//fmt.Println(dat["status"])
	} else {
	}

	c.ServeSuccessJSON(map[string]interface{}{
		"pre": dat,
	})
}

func (c *GdybController) PostThree() {
	psnNo := c.GetString("psn_no")
	mdtrtId := c.GetString("mdtrt_id")
	iptOtpNo := c.GetString("ipt_otp_no")
	org_name := c.GetString("org_name")
	doctor := c.GetString("doctor")

	result := service.Gdyb2202(psnNo, mdtrtId, iptOtpNo, org_name, doctor)
	c.ServeSuccessJSON(map[string]interface{}{
		"pre": result,
	})
}

func (c *GdybController) PostFour() {
	psnNo := c.GetString("psn_no")
	mdtrtId := c.GetString("mdtrt_id")
	doctor := c.GetString("doctor")
	department := c.GetString("department")
	diag := c.GetString("diag")
	org_name := c.GetString("org_name")
	med_type := c.GetString("med_type")
	doctor_id, _ := c.GetInt64("doctor_id", 0)
	fixmedins_code := c.GetString("fixmedins_code")

	result := service.Gdyb2203(psnNo, mdtrtId, doctor, department, diag, org_name, med_type, doctor_id, fixmedins_code)
	var dat map[string]interface{}
	if err := json.Unmarshal([]byte(result), &dat); err == nil {
	} else {
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"pre": dat,
	})
}

type All struct {
	HisPrescription HisPrescription
	mdtrt_id        string
	psn_no          string
}

type HisPrescription struct {
	ID                     int64                            `gorm:"column:id" json:"id" form:"id"`
	UserOrgId              int64                            `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
	RecordDate             int64                            `gorm:"column:record_date" json:"record_date" form:"record_date"`
	PatientId              int64                            `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
	HisPatientId           int64                            `gorm:"column:his_patient_id" json:"his_patient_id" form:"his_patient_id"`
	Status                 int64                            `gorm:"column:status" json:"status" form:"status"`
	Ctime                  int64                            `gorm:"column:ctime" json:"ctime" form:"ctime"`
	Mtime                  int64                            `gorm:"column:mtime" json:"mtime" form:"mtime"`
	Number                 string                           `gorm:"column:number" json:"number" form:"number"`
	Type                   int64                            `gorm:"column:type" json:"type" form:"type"`
	Doctor                 int64                            `gorm:"column:doctor" json:"doctor" form:"doctor"`
	Creator                int64                            `gorm:"column:creator" json:"creator" form:"creator"`
	Modifier               int64                            `gorm:"column:modifier" json:"modifier" form:"modifier"`
	HisDoctorAdviceInfo    []*models.HisDoctorAdviceInfo    `gorm:"ForeignKey:PatientId,RecordDate,PrescriptionId;AssociationForeignKey:PatientId,RecordDate,ID" json:"advices"`
	HisPrescriptionProject []*models.HisPrescriptionProject `gorm:"ForeignKey:PatientId,RecordDate,PrescriptionId;AssociationForeignKey:PatientId,RecordDate,ID" json:"project"`
}

func (c *GdybController) PostFive() {
	body, _ := ioutil.ReadAll(c.Ctx.Request.Body)
	var respJSON map[string]interface{}
	if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
		utils.ErrorLog("接口返回数据解析JSON失败: %v", err)
		return
	}
	userJSONBytes, _ := json.Marshal(respJSON["pre"])
	var hp []*models.HisPrescription
	if err := json.Unmarshal(userJSONBytes, &hp); err != nil {
		utils.ErrorLog("解析失败：%v", err)
		return
	}

	result := service.Gdyb2204(respJSON["psn_no"].(string), respJSON["mdtrt_id"].(string), hp, respJSON["chrg_bchno"].(string), respJSON["org_name"].(string), respJSON["doctor"].(string), respJSON["dept"].(string), respJSON["fixmedins_code"].(string))

	var dat map[string]interface{}
	if err := json.Unmarshal([]byte(result), &dat); err == nil {
		fmt.Println(dat)
	} else {
		fmt.Println(err)
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"pre": dat,
	})
}

func (c *GdybController) PostSix() {
	psnNo := c.GetString("psn_no")
	mdtrtId := c.GetString("mdtrt_id")
	chrgBchno := c.GetString("chrg_bchno")
	org_name := c.GetString("org_name")
	doctor := c.GetString("doctor")

	result := service.Gdyb2205(psnNo, mdtrtId, chrgBchno, org_name, doctor)

	c.ServeSuccessJSON(map[string]interface{}{
		"pre": result,
	})
}

func (c *GdybController) PostSeven() {
	psnNo := c.GetString("psn_no")
	mdtrtId := c.GetString("mdtrt_id")
	chrgBchno := c.GetString("chrg_bchno")
	certNo := c.GetString("cert_no")
	insutype := c.GetString("insutype")
	total := c.GetString("total")
	org_name := c.GetString("org_name")
	doctor := c.GetString("doctor")

	result := service.Gdyb2206(psnNo, mdtrtId, chrgBchno, certNo, insutype, total, org_name, doctor)
	var dat map[string]interface{}
	if err := json.Unmarshal([]byte(result), &dat); err == nil {
		fmt.Println(dat)
	} else {
		fmt.Println(err)
	}
	c.ServeSuccessJSON(map[string]interface{}{
		"pre": dat,
	})
}

func (c *GdybController) PostEight() {
	psnNo := c.GetString("psn_no")
	mdtrtId := c.GetString("mdtrt_id")
	chrgBchno := c.GetString("chrg_bchno")
	certNo := c.GetString("cert_no")
	insutype := c.GetString("insutype")
	total := c.GetString("total")
	org_name := c.GetString("org_name")
	doctor := c.GetString("doctor")
	fixmedins_code := c.GetString("fixmedins_code")

	result := service.Gdyb2207(psnNo, mdtrtId, chrgBchno, certNo, insutype, total, org_name, doctor, fixmedins_code)
	var dat map[string]interface{}
	if err := json.Unmarshal([]byte(result), &dat); err == nil {
		fmt.Println(dat)
	} else {
		fmt.Println(err)
	}

	c.ServeSuccessJSON(map[string]interface{}{
		"pre": dat,
	})
}

func (c *GdybController) PostNine() {
	psnNo := c.GetString("psn_no")
	mdtrtId := c.GetString("mdtrt_id")
	setlId := c.GetString("setl_id")
	org_name := c.GetString("org_name")
	doctor := c.GetString("doctor")

	result := service.Gdyb2208(psnNo, mdtrtId, setlId, org_name, doctor)
	c.ServeSuccessJSON(map[string]interface{}{
		"pre": result,
	})
}

//func (c *GdybController) PostTen() {
//	fixmedins_type := c.GetString("fixmedins_type")
//	//fixmedins_type := c.GetString("org_name")
//
//	result := service.Gdyb1201(fixmedins_type)
//	c.ServeSuccessJSON(map[string]interface{}{
//		"pre": result,
//	})
//}
