package hb

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

type HBybController struct {
	controllers.BaseAuthAPIController
}

func HbybRegistRouters() {
	beego.Router("/hbyb/1101", &HBybController{}, "get:PostOne")
	beego.Router("/hbyb/2201", &HBybController{}, "get:PostTwo")
	beego.Router("/hbyb/2202", &HBybController{}, "get:PostThree")
	beego.Router("/hbyb/2203", &HBybController{}, "post:PostFour")
	beego.Router("/hbyb/2204", &HBybController{}, "post:PostFive")
	beego.Router("/hbyb/2205", &HBybController{}, "get:PostSix")
	beego.Router("/hbyb/2206", &HBybController{}, "get:PostSeven")
	beego.Router("/hbyb/2207", &HBybController{}, "get:PostEight")
	beego.Router("/hbyb/2208", &HBybController{}, "get:PostNine")
	beego.Router("/hbyb/5203", &HBybController{}, "get:PostTen")
	beego.Router("/hbyb/2001", &HBybController{}, "get:Post2001")

}
func (c *HBybController) Post2001() {
	org_name := c.GetString("org_name")
	doctor := c.GetString("doctor")
	fixmedins_code := c.GetString("fixmedins_code")
	insuplc_admdvs := c.GetString("insuplc_admdvs")
	mdtrtarea_admvs := c.GetString("mdtrtarea_admvs")
	secret_key := c.GetString("secret_key")
	psn_no := c.GetString("psn_no")
	insutype := c.GetString("insutype")
	med_type := c.GetString("med_type")
	result, inputLog := service.Hbyb2001(secret_key, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, psn_no, insutype, med_type)
	fmt.Println(result)
	fmt.Println(inputLog)

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

	} else {

	}

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

	} else {

	}

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

func (c *HBybController) PostOne() {
	certNo := c.GetString("cert_no")
	org_name := c.GetString("org_name")
	doctor := c.GetString("doctor")
	fixmedins_code := c.GetString("fixmedins_code")
	insuplc_admdvs := c.GetString("insuplc_admdvs")
	mdtrtarea_admvs := c.GetString("mdtrtarea_admvs")
	secret_key := c.GetString("secret_key")
	id_card_type, _ := c.GetInt64("id_card_type")
	card_sn := c.GetString("card_sn")
	certificates := c.GetString("certificates")

	result, inputLog := service.Hbyb1101(certNo, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, secret_key, id_card_type, card_sn, certificates)
	fmt.Println(result)
	fmt.Println(inputLog)

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

	} else {

	}

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

	} else {

	}

	c.ServeSuccessJSON(map[string]interface{}{
		"pre": dat,
		"log": dat2,
	})
}
func (c *HBybController) 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")

	dept_code := c.GetString("dept_code")
	doctor_id := c.GetString("doctor_id")

	insuplc_admdvs := c.GetString("insuplc_admdvs")
	mdtrtarea_admvs := c.GetString("mdtrtarea_admvs")

	secret_key := c.GetString("secret_key")

	id_card_type, _ := c.GetInt64("id_card_type")

	result, _ := service.Hbyb2201(psnNo, insutype, certNo, org_name, doctor, ipt_otp_no, dept, fixmedins_code, dept_code, doctor_id, insuplc_admdvs, mdtrtarea_admvs, secret_key, id_card_type)

	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 *HBybController) 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")
	insuplc_admdvs := c.GetString("insuplc_admdvs")
	mdtrtarea_admvs := c.GetString("mdtrtarea_admvs")
	secret_key := c.GetString("secret_key")
	fixmedins_code := c.GetString("fixmedins_code")
	result, requestLog := service.Hbyb2202(psnNo, mdtrtId, iptOtpNo, org_name, doctor, insuplc_admdvs, mdtrtarea_admvs, secret_key, fixmedins_code)
	var dat map[string]interface{}
	if err := json.Unmarshal([]byte(result), &dat); err == nil {
	} else {
	}

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

func (c *HBybController) PostFour() {

	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["config"])
	var config []*models.HisXtDiagnoseConfig
	if err := json.Unmarshal(userJSONBytes, &config); err != nil {
		utils.ErrorLog("解析失败：%v", err)
		return
	}
	fmt.Println(respJSON["psn_no"].(string))
	fmt.Println(respJSON["mdtrt_id"].(string))

	psnNo := respJSON["psn_no"].(string)
	mdtrtId := respJSON["mdtrt_id"].(string)
	doctor := respJSON["doctor"].(string)
	department := respJSON["department"].(string)

	org_name := respJSON["org_name"].(string)
	med_type := respJSON["med_type"].(string)
	doctor_id := respJSON["doctor_id"].(string)
	fixmedins_code := respJSON["fixmedins_code"].(string)
	sick_code := respJSON["sick_code"].(string)
	sick_name := respJSON["sick_name"].(string)
	dept := respJSON["dept"].(string)

	insuplc_admdvs := respJSON["insuplc_admdvs"].(string)
	mdtrtarea_admvs := respJSON["mdtrtarea_admvs"].(string)
	secret_key := respJSON["secret_key"].(string)
	begin_time := respJSON["begin_time"].(string)

	result, request_log := service.Hbyb2203(psnNo, mdtrtId, doctor, department, org_name, med_type, doctor_id, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, secret_key, sick_code, sick_name, dept, config, begin_time)

	fmt.Println(request_log)

	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 *HBybController) 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
	}
	fmt.Println(respJSON["psn_no"].(string))
	fmt.Println(respJSON["mdtrt_id"].(string))

	fmt.Println(respJSON["chrg_bchno"].(string))
	fmt.Println(respJSON["org_name"].(string))
	fmt.Println(respJSON["doctor"].(string))
	fmt.Println(respJSON["dept"].(string))

	fmt.Println(respJSON["fixmedins_code"].(string))
	fmt.Println(respJSON["dept_code"].(string))
	fmt.Println(respJSON["doctor_id"].(string))

	fmt.Println(respJSON["insuplc_admdvs"].(string))
	fmt.Println(respJSON["mdtrtarea_admvs"].(string))
	fmt.Println(respJSON["secret_key"].(string))

	result, requestLog := service.Hbyb2204(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), respJSON["dept_code"].(string), respJSON["doctor_id"].(string), respJSON["insuplc_admdvs"].(string), respJSON["mdtrtarea_admvs"].(string), respJSON["secret_key"].(string), respJSON["balance_accounts_type"].(string))

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

	var dat2 map[string]interface{}
	if err := json.Unmarshal([]byte(requestLog), &dat2); err == nil {
		fmt.Println(dat2)
	} else {
		fmt.Println(err)
	}

	c.ServeSuccessJSON(map[string]interface{}{
		"pre":         dat,
		"request_log": requestLog,
	})
}
func (c *HBybController) 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")

	insuplc_admdvs := c.GetString("insuplc_admdvs")
	mdtrtarea_admvs := c.GetString("mdtrtarea_admvs")

	secret_key := c.GetString("secret_key")
	fixmedins_code := c.GetString("fixmedins_code")

	result, requestlog := service.Hbyb2205(psnNo, mdtrtId, chrgBchno, org_name, doctor, insuplc_admdvs, mdtrtarea_admvs, secret_key, fixmedins_code)
	fmt.Println(requestlog)
	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 *HBybController) 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")
	fixmedins_code := c.GetString("fixmedins_code")
	insuplc_admdvs := c.GetString("insuplc_admdvs")
	mdtrtarea_admvs := c.GetString("mdtrtarea_admvs")
	secret_key := c.GetString("secret_key")
	med_type := c.GetString("med_type")
	id_card_type, _ := c.GetInt64("id_card_type")
	certificates, _ := c.GetInt64("certificates")

	result, requestlog := service.Hbyb2206(psnNo, mdtrtId, chrgBchno, certNo, insutype, total, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, secret_key, med_type, id_card_type, 0, 0, 0, 0, certificates)
	fmt.Println(requestlog)

	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 *HBybController) 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")
	insuplc_admdvs := c.GetString("insuplc_admdvs")
	mdtrtarea_admvs := c.GetString("mdtrtarea_admvs")
	secret_key := c.GetString("secret_key")
	med_type := c.GetString("med_type")
	id_card_type, _ := c.GetInt64("id_card_type")
	certificates, _ := c.GetInt64("certificates")

	fulamt_ownpay_amt, _ := c.GetFloat("fulamt_ownpay_amt")
	overlmt_selfpay, _ := c.GetFloat("overlmt_selfpay")
	preselfpay_amt, _ := c.GetFloat("preselfpay_amt")
	inscp_scp_amt, _ := c.GetFloat("inscp_scp_amt")

	acct_used_flag, _ := c.GetInt64("acct_used_flag")

	result, srcRequest := service.Hbyb2207(psnNo, mdtrtId, chrgBchno, certNo, insutype, total, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, secret_key, med_type, id_card_type, fulamt_ownpay_amt, overlmt_selfpay, preselfpay_amt, inscp_scp_amt, certificates, acct_used_flag)
	var dat map[string]interface{}
	if err := json.Unmarshal([]byte(result), &dat); err == nil {
		fmt.Println(dat)
	} else {
		fmt.Println(err)
	}

	var dat2 map[string]interface{}
	if err := json.Unmarshal([]byte(srcRequest), &dat2); err == nil {
		fmt.Println(dat2)
	} else {
		fmt.Println(err)
	}

	c.ServeSuccessJSON(map[string]interface{}{
		"pre":  dat,
		"pre2": dat2,
	})
}
func (c *HBybController) 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")
	secret_key := c.GetString("secret_key")
	fixmedins_code := c.GetString("fixmedins_code")
	insuplc_admdvs := c.GetString("insuplc_admdvs")
	mdtrtarea_admvs := c.GetString("mdtrtarea_admvs")
	result, src_request := service.Hbyb2208(psnNo, mdtrtId, setlId, org_name, doctor, secret_key, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs)
	fmt.Println(src_request)
	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 *HBybController) PostTen() {
	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")
	med_type := c.GetString("med_type")

	secret_key := c.GetString("secret_key")
	fixmedins_code := c.GetString("fixmedins_code")
	insuplc_admdvs := c.GetString("insuplc_admdvs")
	mdtrtarea_admvs := c.GetString("mdtrtarea_admvs")

	baseParams := models.BaseParams{
		SecretKey:      secret_key,
		FixmedinsCode:  fixmedins_code,
		InsuplcAdmdvs:  insuplc_admdvs,
		MdtrtareaAdmvs: mdtrtarea_admvs,
		OrgName:        org_name,
		Doctor:         doctor,
	}

	businessParams := models.BusinessParams{
		PsnNo:   psnNo,
		MdtrtId: mdtrtId,
		SetlId:  setlId,
	}

	result := service.Hbyb5203(baseParams, businessParams, med_type)
	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,
	})
}
