package service

import (
	//"bytes"
	"encoding/json"
	"fmt"
	"gdyb/models"
	"gdyb/utils/csbhttp"
	//"io/ioutil"
	"net"
	//"net/http"
	"strconv"
	"time"
)

func Jsyb2201F() (string, string) {

	// 生成签名
	timestamp := time.Now().Unix()
	tempTime := time.Unix(timestamp, 0)
	timeFormatOne := tempTime.Format("2006-01-02 15:04:05")

	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, "盐城响水悦达血液透析中心有限公司", "王曙光", "H32092103007", "320921", "320921", "04aaa201baecc02c5f5e67d123810f2442dc04444e60a95ce529708bef8143f2fd3c5af38e21cccf9377c0ae3eef0a05528321f6265101a324802581709e2cdc297d94a3b176ed4112c8bc435d4f4362948720ea989bc58399fd251dd49b41d03356a1b99b6fd2c0bef2c6a2c970b03de9e3ce33499cc52b0c98a508d1eca0e104")

	//inputMessage := Se/**/tJSInputMessage(timestamp, org_name, opera, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "2201"                     // 交易编码
	inputData["psn_no"] = "32090000000000001004606391" // 人员编号 （来自1101接口返回）
	inputData["insutype"] = "390"                      // 险种类型（来自1101接口返回）
	inputData["begntime"] = timeFormatOne              // 开始时间
	//
	//if id_card_type == 1 {
	//	inputData["mdtrt_cert_type"] = "03"                       // 就诊凭证类型
	//	inputData["mdtrt_cert_no"] = certNo + "|" + verify_number // 就诊凭证编号
	//} else if id_card_type == 3 {
	//	inputData["mdtrt_cert_type"] = "04"                                     // 就诊凭证类型
	//	inputData["mdtrt_cert_no"] = "320822197705194836" + "｜" + verify_number // 就诊凭证编号
	//} else {
	//	inputData["mdtrt_cert_type"] = "02"                       // 就诊凭证类型
	//	inputData["mdtrt_cert_no"] = certNo + "|" + verify_number // 就诊凭证编号
	//
	//}
	inputData["mdtrt_cert_type"] = "04"                                                                                                                                                                        // 就诊凭证类型
	inputData["mdtrt_cert_no"] = "320822197705194836|Y9gJ7nijh+DzGkn2W8El9YxSR0x1/zb2+tcA4a5fsVC4RLNACwi93U4Q9xEJVKKnN6LCbomuiCuxDOI+Skz/wsjlScPKIzyXxBcKl9ZrZjCrr+cRTRRKRdz6JqhoexGR/Cxnipr62JOW2fmrI1X2XA==" // 就诊凭证编号

	inputData["ipt_otp_no"] = timeFormatOne // 住院/门诊号
	inputData["atddr_no"] = "2484"          // 医师编码
	inputData["dr_name"] = "王曙光"            // 医师姓名
	inputData["dept_code"] = "01"           // 科室编码
	inputData["dept_name"] = "血透室"          // 科室名称
	inputData["caty"] = "A03.06"            // 科别

	input["data"] = inputData
	inputMessage["input"] = input //交易输入

	var requestLog string
	bytesData, err := json.Marshal(inputMessage)
	requestLog = string(bytesData)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	request_url := "http://10.72.3.87:8086/CSB/" + "2201"
	//request_url = request_url + "2201"
	return HttpRequest(request_url, "1781ce9fb73a478289326c16ba8bf2c4", "b5DPOGjc08DAAMJ8FP31pwwNHKo=", timestamp, string(bytesData)), requestLog

}

func Jsyb1101F() (string, string) {
	timestamp := time.Now().Unix()
	//生成输入报文
	inputMessage := SetJSInputMessage(timestamp, "盐城响水悦达血液透析中心有限公司", "王曙光", "H32092103007", "320921", "320921", "04aaa201baecc02c5f5e67d123810f2442dc04444e60a95ce529708bef8143f2fd3c5af38e21cccf9377c0ae3eef0a05528321f6265101a324802581709e2cdc297d94a3b176ed4112c8bc435d4f4362948720ea989bc58399fd251dd49b41d03356a1b99b6fd2c0bef2c6a2c970b03de9e3ce33499cc52b0c98a508d1eca0e104")

	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "1101" // 交易编码

	inputData["mdtrt_cert_type"] = "04"                                                                                                                                                                        // 就诊凭证类型
	inputData["card_sn"] = ""                                                                                                                                                                                  // 卡识别码
	inputData["certno"] = ""                                                                                                                                                                                   // 证件号码
	inputData["psn_cert_type"] = "01"                                                                                                                                                                          // 人员证件类型
	inputData["mdtrt_cert_no"] = "320822197705194836|Y9gJ7nijh+DzGkn2W8El9YxSR0x1/zb2+tcA4a5fsVC4RLNACwi93U4Q9xEJVKKnN6LCbomuiCuxDOI+Skz/wsjlScPKIzyXxBcKl9ZrZjCrr+cRTRRKRdz6JqhoexGR/Cxnipr62JOW2fmrI1X2XA==" // 就诊凭证编号

	inputData["begntime"] = "" // 开始时间
	inputData["psn_name"] = "" // 人员姓名
	input["data"] = inputData
	inputMessage["input"] = input //交易输入

	var inputLog string
	bytesData, _ := json.Marshal(inputMessage)
	inputLog = string(bytesData)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	request_url := "http://10.72.3.87:8086/CSB/" + "1101"
	return HttpRequest(request_url, "1781ce9fb73a478289326c16ba8bf2c4", "b5DPOGjc08DAAMJ8FP31pwwNHKo=", timestamp, string(bytesData)), inputLog
}

// 人员基本信息
func Jsyb1101(certNo string, org_name string, doctor string, fixmedins_code string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, id_card_type int64, card_sn string, certificates string, request_url string, access_key string, cainfo string) (string, string) {
	timestamp := time.Now().Unix()
	//生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)

	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "1101" // 交易编码

	if certificates == "3" {
		inputData["mdtrt_cert_type"] = "99" // 就诊凭证类型
		inputData["certno"] = certNo        // 证件号码
		inputData["psn_cert_type"] = "99"   // 人员证件类型

	} else {
		if id_card_type == 1 {
			inputData["mdtrt_cert_type"] = "03" // 就诊凭证类型
			inputData["card_sn"] = card_sn      // 卡识别码
			inputData["certno"] = certNo        // 证件号码
			inputData["psn_cert_type"] = "01"   // 人员证件类型

		} else {
			inputData["mdtrt_cert_type"] = "02" // 就诊凭证类型
			inputData["card_sn"] = ""           // 卡识别码
			inputData["certno"] = certNo        // 证件号码
			inputData["psn_cert_type"] = "01"   // 人员证件类型
		}
	}

	inputData["mdtrt_cert_no"] = certNo // 就诊凭证编号
	inputData["begntime"] = ""          // 开始时间
	inputData["psn_name"] = ""          // 人员姓名
	input["data"] = inputData
	inputMessage["input"] = input //交易输入

	var inputLog string
	bytesData, _ := json.Marshal(inputMessage)
	inputLog = string(bytesData)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	request_url = request_url + "1101"
	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), inputLog
}

//人员基本信息版本2
func Jsyb1101A(certNo string, org_name string, doctor string, fixmedins_code string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, id_card_type int64, card_sn string, certificates string, request_url string, access_key string, pBusiCardInfo string, id_card_no string, cainfo string) (string, string) {
	timestamp := time.Now().Unix()
	//生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)

	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "1101" // 交易编码

	if certificates == "3" {
		inputData["mdtrt_cert_type"] = "99" // 就诊凭证类型
		inputData["certno"] = certNo        // 证件号码
		inputData["psn_cert_type"] = "99"   // 人员证件类型

	} else {
		if id_card_type == 1 {
			inputData["mdtrt_cert_type"] = "03" // 就诊凭证类型
			inputData["card_sn"] = card_sn      // 卡识别码
			inputData["certno"] = id_card_no    // 证件号码
			inputData["psn_cert_type"] = "01"   // 人员证件类型

		} else {
			inputData["mdtrt_cert_type"] = "02" // 就诊凭证类型
			inputData["card_sn"] = ""           // 卡识别码
			inputData["certno"] = certNo        // 证件号码
			inputData["psn_cert_type"] = "01"   // 人员证件类型
		}
	}

	inputData["mdtrt_cert_no"] = certNo + "|" + pBusiCardInfo // 就诊凭证编号
	inputData["begntime"] = ""                                // 开始时间
	inputData["psn_name"] = ""                                // 人员姓名
	input["data"] = inputData
	inputMessage["input"] = input //交易输入

	var inputLog string
	bytesData, _ := json.Marshal(inputMessage)
	inputLog = string(bytesData)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	request_url = request_url + "1101"
	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), inputLog
}

func Jsyb1101B(org_name string, doctor string, fixmedins_code string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, request_url string, access_key string, Token string, cainfo string, id_card_no string) (string, string) {
	timestamp := time.Now().Unix()
	//生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)

	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "1101" // 交易编码

	inputData["mdtrt_cert_type"] = "04" // 就诊凭证类型
	inputData["card_sn"] = ""           // 卡识别码
	inputData["certno"] = ""            // 证件号码
	inputData["psn_cert_type"] = "01"   // 人员证件类型

	inputData["mdtrt_cert_no"] = id_card_no + "|" + Token // 就诊凭证编号
	inputData["begntime"] = ""                            // 开始时间
	inputData["psn_name"] = ""                            // 人员姓名
	input["data"] = inputData
	inputMessage["input"] = input //交易输入

	var inputLog string
	bytesData, _ := json.Marshal(inputMessage)
	inputLog = string(bytesData)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	request_url = request_url + "1101"
	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), inputLog
}

// 门诊挂号
func Jsyb2201(psnNo string, insutype string, certNo string, org_name string, opera string, ipt_otp_no string, dept string, fixmedins_code string, dept_code string, doctor_id string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, id_card_type int64, doctor_name string, request_url string, access_key string, verify_number string, cainfo string) (string, string) {
	fmt.Println(opera)

	// 生成签名
	timestamp := time.Now().Unix()
	tempTime := time.Unix(timestamp, 0)
	timeFormatOne := tempTime.Format("2006-01-02 15:04:05")

	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, opera, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "2201"        // 交易编码
	inputData["psn_no"] = psnNo           // 人员编号 （来自1101接口返回）
	inputData["insutype"] = insutype      // 险种类型（来自1101接口返回）
	inputData["begntime"] = timeFormatOne // 开始时间

	if id_card_type == 1 {
		inputData["mdtrt_cert_type"] = "03"                       // 就诊凭证类型
		inputData["mdtrt_cert_no"] = certNo + "|" + verify_number // 就诊凭证编号
	} else if id_card_type == 3 {
		inputData["mdtrt_cert_type"] = "04"        // 就诊凭证类型
		inputData["mdtrt_cert_no"] = verify_number // 就诊凭证编号
	} else {
		inputData["mdtrt_cert_type"] = "02"                       // 就诊凭证类型
		inputData["mdtrt_cert_no"] = certNo + "|" + verify_number // 就诊凭证编号

	}

	inputData["ipt_otp_no"] = ipt_otp_no // 住院/门诊号
	inputData["atddr_no"] = doctor_id    // 医师编码
	inputData["dr_name"] = doctor_name   // 医师姓名
	inputData["dept_code"] = dept_code   // 科室编码
	inputData["dept_name"] = dept        // 科室名称
	inputData["caty"] = "A03.06"         // 科别

	input["data"] = inputData
	inputMessage["input"] = input //交易输入

	var requestLog string
	bytesData, err := json.Marshal(inputMessage)
	requestLog = string(bytesData)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}

	request_url = request_url + "2201"
	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), requestLog

}

// 门诊挂号撤销
func Jsyb2202(psnNo string, mdtrtId string, ipt_otp_no string, org_name string, doctor string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, fixmedins_code string, request_url string, access_key string, cainfo string) (string, string) {
	// 生成签名
	timestamp := time.Now().Unix()
	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "2202"       // 交易编码
	inputData["psn_no"] = psnNo          // 人员编号 （来自1101接口返回）
	inputData["mdtrt_id"] = mdtrtId      // 就诊 ID（来自2201接口返回）
	inputData["ipt_otp_no"] = ipt_otp_no // 住院/门诊号

	input["data"] = inputData
	inputMessage["input"] = input //交易输入

	var requestLog string

	bytesData, err := json.Marshal(inputMessage)
	fmt.Println(string(bytesData))
	requestLog = string(bytesData)
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}

	request_url = request_url + "2202"
	//fmt.Println(HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)))
	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), requestLog
}

// 门诊就诊信息上传
func Jsyb2203(psnNo string, mdtrtId string, doctor string, department string, org_name string, med_type string, doctor_id string, fixmedins_code string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, sick_code string, sick_name string, dept string, config []*models.HisXtDiagnoseConfig, request_url string, access_key string, begin_time string, cainfo string) (string, string, int64) {
	// 生成签名
	timestamp := time.Now().Unix()
	tempTime := time.Unix(timestamp, 0)
	timeFormatOne := tempTime.Format("2006-01-02 15:04:05")
	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, dept, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	diseinfo := make([]map[string]interface{}, 0)

	if med_type == "1111" || med_type == "1112" {
		med_type = "11"
	}

	inputMessage["infno"] = "2203A"     // 交易编码
	inputData["mdtrt_id"] = mdtrtId     // 就诊 ID（来自2201接口返回）
	inputData["psn_no"] = psnNo         // 人员编号 （来自1101接口返回）
	inputData["med_type"] = med_type    // 医疗类别 16门诊特殊病
	inputData["begntime"] = begin_time  // 开始时间
	inputData["main_cond_dscr"] = ""    // 主要病情描述
	inputData["dise_codg"] = sick_code  // 病种编码
	inputData["dise_name"] = sick_name  // 病种名称
	inputData["birctrl_type"] = ""      // 计划生育手术类别
	inputData["birctrl_matn_date"] = "" // 计划生育手术或生育日期

	for index, item := range config {
		diseinfo_sun := make(map[string]interface{})
		diseinfo_sun["diag_type"] = "1"              // 诊断类别
		diseinfo_sun["diag_srt_no"] = index          // 诊断排序号
		diseinfo_sun["diag_code"] = item.CountryCode // 诊断代码
		diseinfo_sun["diag_name"] = item.ClassName   // 诊断名称
		diseinfo_sun["diag_dept"] = department       // 诊断科室
		diseinfo_sun["dise_dor_no"] = doctor_id      // 诊断医生编码
		diseinfo_sun["dise_dor_name"] = doctor       // 诊断医生姓名
		diseinfo_sun["diag_time"] = timeFormatOne    // 诊断时间
		diseinfo_sun["vali_flag"] = "1"              // 有效标志
		diseinfo = append(diseinfo, diseinfo_sun)
	}

	input["diseinfo"] = diseinfo
	input["mdtrtinfo"] = inputData
	inputMessage["input"] = input //交易输入
	var requestLog string
	bytesData, err := json.Marshal(inputMessage)
	fmt.Println(string(bytesData))
	requestLog = string(bytesData)
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), "", 0
	}
	request_url = request_url + "2203A"
	//fmt.Println(HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)))

	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), requestLog, timestamp
}

// 门诊就诊信息上传
func Jsyb2203A(psnNo string, mdtrtId string, doctor string, department string, org_name string, med_type string, doctor_id string, fixmedins_code string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, sick_code string, sick_name string, dept string, config []*models.HisXtDiagnoseConfig, request_url string, access_key string, cainfo string) (string, string) {
	// 生成签名
	timestamp := time.Now().Unix()
	tempTime := time.Unix(timestamp, 0)
	timeFormatOne := tempTime.Format("2006-01-02 15:04:05")

	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, dept, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	diseinfo := make([]map[string]interface{}, 0)

	if med_type == "1111" || med_type == "1112" {
		med_type = "11"
	}

	inputMessage["infno"] = "2203A"       // 交易编码
	inputData["mdtrt_id"] = mdtrtId       // 就诊 ID（来自2201接口返回）
	inputData["psn_no"] = psnNo           // 人员编号 （来自1101接口返回）
	inputData["med_type"] = med_type      // 医疗类别 16门诊特殊病
	inputData["begntime"] = timeFormatOne // 开始时间

	inputData["main_cond_dscr"] = ""    // 主要病情描述
	inputData["dise_codg"] = sick_code  // 病种编码
	inputData["dise_name"] = sick_name  // 病种名称
	inputData["birctrl_type"] = ""      // 计划生育手术类别
	inputData["birctrl_matn_date"] = "" // 计划生育手术或生育日期

	inputData["matn_type"] = "" // 生育类别
	inputData["geso_val"] = ""  // 孕周数

	for index, item := range config {
		diseinfo_sun := make(map[string]interface{})
		diseinfo_sun["diag_type"] = "1"              // 诊断类别
		diseinfo_sun["diag_srt_no"] = index          // 诊断排序号
		diseinfo_sun["diag_code"] = item.CountryCode // 诊断代码
		diseinfo_sun["diag_name"] = item.ClassName   // 诊断名称
		diseinfo_sun["diag_dept"] = department       // 诊断科室
		diseinfo_sun["dise_dor_no"] = doctor_id      // 诊断医生编码
		diseinfo_sun["dise_dor_name"] = doctor       // 诊断医生姓名
		diseinfo_sun["diag_time"] = timeFormatOne    // 诊断时间
		diseinfo_sun["vali_flag"] = "1"              // 有效标志
		diseinfo = append(diseinfo, diseinfo_sun)
	}

	input["diseinfo"] = diseinfo
	input["mdtrtinfo"] = inputData
	inputMessage["input"] = input //交易输入
	var requestLog string
	bytesData, err := json.Marshal(inputMessage)
	fmt.Println(string(bytesData))
	requestLog = string(bytesData)
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	request_url = request_url + "2203A"
	//fmt.Println(HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)))

	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), requestLog
}

//  门诊费用明细信息上传
func Jsyb2204(psnNo string, mdtrtId string, hisPrescription []*models.HisPrescription, chrg_bchno string, org_name string, doctor string, dept string, fixmedins_code string, dept_code string, doctor_id string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, balance_accounts_type string, diag_code string, request_url string, access_key string, new_doctor_name string, time_stamp_2203 string, cainfo string) (string, string) {
	// 生成签名
	//timestamp := time.Now().Unix()
	timestamp := time.Now().Unix()

	timestamp2 := time.Now().Unix()

	//tempTime := time.Unix(timestamp2, 0)
	//timeFormatOne := tempTime.Format("2006-01-02 15:04:05")
	var customs []*Custom
	for _, item := range hisPrescription {
		tm := time.Unix(item.PreTime, 0)

		if item.Type == 1 { //药品
			for _, subItem := range item.HisDoctorAdviceInfo {
				if len(subItem.BaseDrugLib.MedicalInsuranceNumber) > 0 {
					//var randNum int
					//randNum = rand.Intn(10000) + 1000
					cus := &Custom{
						DetItemFeeSumamt:        fmt.Sprintf("%.4f", subItem.Price*subItem.PrescribingNumber),
						Cut:                     fmt.Sprintf("%.4f", subItem.PrescribingNumber),
						FeedetlSn:               subItem.FeedetlSn,
						Price:                   fmt.Sprintf("%.4f", subItem.Price),
						MedListCodg:             subItem.BaseDrugLib.MedicalInsuranceNumber,
						HospApprFlag:            subItem.HospApprFlag,
						DoctorNumber:            doctor_id,
						DoctorName:              new_doctor_name,
						ProvinceDrugMedListCodg: "",
						ProvinceGoodMedListCodg: "",
						FeeOcurTime:             tm.Format("2006-01-02 15:04:05"),
					}

					if subItem.BaseDrugLib.DrugClassify == "2" {
						cus.ProvinceDrugMedListCodg = subItem.BaseDrugLib.ProvincesCode
					}

					customs = append(customs, cus)
				}
			}
		}

		if item.Type == 2 { //项目
			for _, subItem := range item.HisPrescriptionProject {
				//var randNum int
				//randNum = rand.Intn(10000) + 1000
				if subItem.Type == 2 {
					if len(subItem.HisProject.MedicalCode) > 0 {
						cnt, _ := strconv.ParseFloat(subItem.Count, 64)
						cus := &Custom{
							DetItemFeeSumamt:        fmt.Sprintf("%.4f", subItem.Price*cnt),
							Cut:                     fmt.Sprintf("%.4f", cnt),
							FeedetlSn:               subItem.FeedetlSn,
							Price:                   fmt.Sprintf("%.4f", float64(subItem.Price)),
							MedListCodg:             subItem.HisProject.MedicalCode,
							DoctorNumber:            doctor_id,
							DoctorName:              new_doctor_name,
							HospApprFlag:            -1,
							ProvinceDrugMedListCodg: "",
							ProvinceGoodMedListCodg: "",
							FeeOcurTime:             tm.Format("2006-01-02 15:04:05"),
						}
						customs = append(customs, cus)
					}

				} else if subItem.Type == 3 {
					if len(subItem.GoodInfo.MedicalInsuranceNumber) > 0 {
						cnt, _ := strconv.ParseFloat(subItem.Count, 64)

						cus := &Custom{
							DetItemFeeSumamt:        fmt.Sprintf("%.4f", subItem.Price*cnt),
							Cut:                     fmt.Sprintf("%.4f", cnt),
							FeedetlSn:               subItem.FeedetlSn,
							Price:                   fmt.Sprintf("%.4f", float64(subItem.Price)),
							MedListCodg:             subItem.GoodInfo.MedicalInsuranceNumber,
							HospApprFlag:            -1,
							DoctorNumber:            doctor_id,
							DoctorName:              new_doctor_name,
							ProvinceDrugMedListCodg: "",
							ProvinceGoodMedListCodg: subItem.GoodInfo.ProvincesCode,
							FeeOcurTime:             tm.Format("2006-01-02 15:04:05"),
						}
						customs = append(customs, cus)
					}

				}

			}
		}

		for _, subItem := range item.HisAdditionalCharge {
			if len(subItem.XtHisAddtionConfig.Code) > 0 {
				cus := &Custom{
					DetItemFeeSumamt: fmt.Sprintf("%.4f", subItem.Price*float64(subItem.Count)),
					Cut:              fmt.Sprintf("%.4f", float64(subItem.Count)),
					FeedetlSn:        subItem.FeedetlSn,
					Price:            fmt.Sprintf("%.4f", float64(subItem.Price)),
					MedListCodg:      subItem.XtHisAddtionConfig.Code,
					HospApprFlag:     -1,
					DoctorNumber:     doctor_id,
					DoctorName:       new_doctor_name,
				}
				customs = append(customs, cus)
			}
		}
	}

	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp2, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	feedetail := make([]map[string]interface{}, 0)
	inputMessage["infno"] = "2204" // 交易编码

	//chrg_bchno := timeFormat + strconv.FormatInt(int64(chrgBchno), 10)
	for _, item := range customs {
		feedetailInfo := make(map[string]interface{})
		subfeedetailInfo := make(map[string]interface{})

		feedetailInfo["feedetl_sn"] = item.FeedetlSn
		feedetailInfo["mdtrt_id"] = mdtrtId                          // 就诊 ID（来自2201接口返回）
		feedetailInfo["psn_no"] = psnNo                              // 人员编号 （来自1101接口返回）
		feedetailInfo["chrg_bchno"] = chrg_bchno                     // 收费批次号
		feedetailInfo["dise_codg"] = diag_code                       // 病种编码
		feedetailInfo["rxno"] = ""                                   // 处方号
		feedetailInfo["rx_circ_flag"] = "0"                          // 外购处方标志
		feedetailInfo["fee_ocur_time"] = item.FeeOcurTime            // 费用发生时间
		feedetailInfo["med_list_codg"] = item.MedListCodg            // 医疗目录编码
		feedetailInfo["medins_list_codg"] = fixmedins_code           // 医药机构目录编码
		feedetailInfo["det_item_fee_sumamt"] = item.DetItemFeeSumamt // 明细项目费用总额
		feedetailInfo["cnt"] = item.Cut                              // 数量
		feedetailInfo["pric"] = item.Price                           // 单价
		feedetailInfo["sin_dos_dscr"] = ""                           // 单次剂量描述
		feedetailInfo["used_frqu_dscr"] = ""                         // 使用频次描述
		feedetailInfo["prd_days"] = "0"                              // 周期天数
		feedetailInfo["medc_way_dscr"] = ""                          // 用药途径描述
		feedetailInfo["bilg_dept_codg"] = dept_code                  // 开单科室编码
		feedetailInfo["bilg_dept_name"] = dept                       // 开单科室名称
		feedetailInfo["bilg_dr_codg"] = item.DoctorNumber            // 开单医生编码
		feedetailInfo["bilg_dr_name"] = item.DoctorName              // 开单医师姓名
		feedetailInfo["acord_dept_codg"] = ""                        // 受单科室编码
		feedetailInfo["acord_dept_name"] = ""                        // 受单科室名称
		feedetailInfo["orders_dr_code"] = ""                         // 受单医生编码
		feedetailInfo["orders_dr_name"] = ""                         // 受单医生姓名
		if balance_accounts_type == "8" {
			feedetailInfo["hosp_appr_flag"] = "2" // 医院审批标志
		} else {
			if item.HospApprFlag != -1 {
				if item.HospApprFlag == 1 {
					feedetailInfo["hosp_appr_flag"] = "1" // 医院审批标志
				} else if item.HospApprFlag == 2 {
					feedetailInfo["hosp_appr_flag"] = "2" // 医院审批标志
				} else if item.HospApprFlag == 3 {
					feedetailInfo["hosp_appr_flag"] = "0" // 医院审批标志
				} else {
					feedetailInfo["hosp_appr_flag"] = "1" // 医院审批标志
				}
			} else {
				feedetailInfo["hosp_appr_flag"] = "1" // 医院审批标志
			}

		}

		feedetailInfo["tcmdrug_used_way"] = "" // 中药使用方式
		feedetailInfo["etip_flag"] = ""        // 外检标志
		feedetailInfo["etip_hosp_code"] = ""   // 外检医院编码
		feedetailInfo["dscg_tkdrug_flag"] = "" // 出院带药标志
		feedetailInfo["matn_fee_flag"] = ""    // 生育费用标志
		subfeedetailInfo["tcmherb_prov_code"] = item.ProvinceDrugMedListCodg
		subfeedetailInfo["mcs_prov_code"] = item.ProvinceGoodMedListCodg
		feedetailInfo["expContent"] = subfeedetailInfo
		feedetail = append(feedetail, feedetailInfo)
	}

	input["feedetail"] = feedetail
	inputMessage["input"] = input //交易输入
	var requestLog string
	bytesData, err := json.Marshal(inputMessage)
	requestLog = string(bytesData)
	fmt.Println(string(bytesData))

	if err != nil {
		fmt.Println(err.Error())
		return "", ""
	}

	request_url = request_url + "2204"
	//fmt.Println(HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)))

	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), requestLog
}

//  门诊费用明细信息撤销
func Jsyb2205(psnNo string, mdtrtId string, chrgBchno string, org_name string, doctor string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, fixmedins_code string, request_url string, access_key string, cainfo string) (string, string) {

	timestamp := time.Now().Unix()

	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "2205" // 交易编码

	inputData["mdtrt_id"] = mdtrtId     // 就诊 ID（来自2201接口返回）
	inputData["chrg_bchno"] = chrgBchno // 收费批次号(来自2204生成的)
	inputData["psn_no"] = psnNo         // 人员编号 （来自1101接口返回）

	input["data"] = inputData
	inputMessage["input"] = input //交易输入

	bytesData, err := json.Marshal(inputMessage)
	fmt.Println(string(bytesData))

	var requestLog string
	requestLog = string(bytesData)
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	request_url = request_url + "2205"
	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), requestLog
}

//  门诊预结算
func Jsyb2206(psnNo string, mdtrtId string, chrgBchno string, certNo string, insutype string, total string, org_name string, doctor string, fixmedins_code string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, med_type string, id_card_type int64, fulamt_ownpay_amt float64, overlmt_selfpay float64, preselfpay_amt float64, inscp_scp_amt float64, certificates int64, request_url string, access_key string, verify_number string, cainfo string) (string, string) {

	// 生成签名
	timestamp := time.Now().Unix() //		inputData["mdtrt_cert_type"] = "03" // 就诊凭证类型
	//		inputData["mdtrt_cert_no"] = certNo
	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "2206" // 交易编码
	inputData["psn_no"] = psnNo    // 人员编号 （来自1101接口返回）
	if certificates == 3 {
		inputData["mdtrt_cert_type"] = "99" // 就诊凭证类型
		inputData["mdtrt_cert_no"] = certNo // 就诊凭证编号
	} else {
		if id_card_type == 1 {
			inputData["mdtrt_cert_type"] = "03" // 就诊凭证类型
			inputData["mdtrt_cert_no"] = certNo // 就诊凭证编号
		} else if id_card_type == 3 {
			inputData["mdtrt_cert_type"] = "04"        // 就诊凭证类型
			inputData["mdtrt_cert_no"] = verify_number // 就诊凭证编号
		} else {
			inputData["mdtrt_cert_type"] = "02"                       // 就诊凭证类型
			inputData["mdtrt_cert_no"] = certNo + "|" + verify_number // 就诊凭证编号
		}
	}
	if insuplc_admdvs == "421300" {
		if med_type == "11" || med_type == "1112" || med_type == "1111" {
			inputData["med_type"] = "110104" // 医疗类别 11 普通门诊  12 门诊挂号
		} else if med_type == "14" {
			inputData["med_type"] = "140110" // 医疗类别 11 普通门诊  12 门诊挂号
		}
	} else {
		inputData["med_type"] = med_type // 医疗类别 11 普通门诊  12 门诊挂号
	}

	inputData["medfee_sumamt"] = total  // 医疗费总额
	inputData["psn_setlway"] = "01"     // 个人结算方式 01 按项目结 02 按定额结算
	inputData["mdtrt_id"] = mdtrtId     // 就诊 ID（来自2201接口返回）
	inputData["chrg_bchno"] = chrgBchno // 收费批次号(来自2204生成的)
	inputData["acct_used_flag"] = "0"   // 个人账户使用标志 0否 1是
	inputData["insutype"] = insutype    // 险种类型
	inputData["invono"] = ""            // 发票号

	input["data"] = inputData
	inputMessage["input"] = input //交易输入

	var requestLog string

	bytesData, err := json.Marshal(inputMessage)

	requestLog = string(bytesData)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	request_url = request_url + "2206"
	//fmt.Println(HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)))

	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), requestLog

}

//  门诊结算
func Jsyb2207(psnNo string, mdtrtId string, chrgBchno string, certNo string, insutype string, total string, org_name string, doctor string, fixmedins_code string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, med_type string, id_card_type int64, fulamt_ownpay_amt float64, overlmt_selfpay float64, preselfpay_amt float64, inscp_scp_amt float64, certificates int64, acct_used_flag string, request_url string, access_key string, verify_number string, cainfo string) (string, string) {
	// 生成签名
	timestamp := time.Now().Unix()
	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "2207" // 交易编码
	inputData["psn_no"] = psnNo    // 人员编号 （来自1101接口返回）

	if certificates == 3 {
		inputData["mdtrt_cert_type"] = "99"                       // 就诊凭证类型
		inputData["mdtrt_cert_no"] = certNo + "|" + verify_number // 就诊凭证编号
	} else {
		if id_card_type == 1 {
			inputData["mdtrt_cert_type"] = "03"                       // 就诊凭证类型
			inputData["mdtrt_cert_no"] = certNo + "|" + verify_number // 就诊凭证编号
		} else if id_card_type == 3 {
			inputData["mdtrt_cert_type"] = "04"        // 就诊凭证类型
			inputData["mdtrt_cert_no"] = verify_number // 就诊凭证编号
		} else {
			inputData["mdtrt_cert_type"] = "02"                       // 就诊凭证类型
			inputData["mdtrt_cert_no"] = certNo + "|" + verify_number // 就诊凭证编号
		}
	}
	if med_type == "1111" || med_type == "1112" {
		med_type = "11"
	}
	if insuplc_admdvs == "421300" {
		if med_type == "11" {
			inputData["med_type"] = "110104" // 医疗类别 11 普通门诊  12 门诊挂号
		} else if med_type == "14" {
			inputData["med_type"] = "140101" // 医疗类别 11 普通门诊  12 门诊挂号
		}
	} else {
		inputData["med_type"] = med_type // 医疗类别 11 普通门诊  12 门诊挂号
	}
	inputData["medfee_sumamt"] = total           // 医疗费总额
	inputData["psn_setlway"] = "01"              // 个人结算方式 01 按项目结 02 按定额结算
	inputData["mdtrt_id"] = mdtrtId              // 就诊 ID（来自2201接口返回）
	inputData["chrg_bchno"] = chrgBchno          // 收费批次号(来自2204生成的)
	inputData["acct_used_flag"] = acct_used_flag // 个人账户使用标志 0否 1是
	inputData["insutype"] = insutype             // 险种类型
	inputData["invono"] = ""                     // 发票号

	inputData["fulamt_ownpay_amt"] = fulamt_ownpay_amt //
	inputData["overlmt_selfpay"] = overlmt_selfpay     //
	inputData["preselfpay_amt"] = preselfpay_amt       //
	inputData["inscp_scp_amt"] = inscp_scp_amt         //

	input["data"] = inputData
	inputMessage["input"] = input //交易输入

	bytesData, err := json.Marshal(inputMessage)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	request_url = request_url + "2207"
	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), string(bytesData)

}

func Jsyb2207A(psnNo string, mdtrtId string, chrgBchno string, certNo string, insutype string, total string, org_name string, doctor string, fixmedins_code string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, med_type string, id_card_type int64, fulamt_ownpay_amt float64, overlmt_selfpay float64, preselfpay_amt float64, inscp_scp_amt float64, certificates int64, acct_used_flag string, request_url string, access_key string, cainfo string) (string, string) {
	// 生成签名
	timestamp := time.Now().Unix()

	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "2207A" // 交易编码
	inputData["psn_no"] = psnNo     // 人员编号 （来自1101接口返回）
	if certificates == 3 {
		inputData["mdtrt_cert_type"] = "99" // 就诊凭证类型
		inputData["mdtrt_cert_no"] = certNo // 就诊凭证编号
	} else {
		if id_card_type == 1 {
			inputData["mdtrt_cert_type"] = "03" // 就诊凭证类型
			inputData["mdtrt_cert_no"] = certNo // 就诊凭证编号
		} else {
			inputData["mdtrt_cert_type"] = "02" // 就诊凭证类型
			inputData["mdtrt_cert_no"] = certNo // 就诊凭证编号
		}
	}
	if med_type == "1111" || med_type == "1112" {
		med_type = "11"
	}
	if insuplc_admdvs == "421300" {
		if med_type == "11" {
			inputData["med_type"] = "110104" // 医疗类别 11 普通门诊  12 门诊挂号
		} else if med_type == "14" {
			inputData["med_type"] = "140101" // 医疗类别 11 普通门诊  12 门诊挂号
		}
	} else {
		inputData["med_type"] = med_type // 医疗类别 11 普通门诊  12 门诊挂号
	}
	inputData["medfee_sumamt"] = total           // 医疗费总额
	inputData["psn_setlway"] = "01"              // 个人结算方式 01 按项目结 02 按定额结算
	inputData["mdtrt_id"] = mdtrtId              // 就诊 ID（来自2201接口返回）
	inputData["chrg_bchno"] = chrgBchno          // 收费批次号(来自2204生成的)
	inputData["acct_used_flag"] = acct_used_flag // 个人账户使用标志 0否 1是
	inputData["insutype"] = insutype             // 险种类型
	inputData["invono"] = ""                     // 发票号

	inputData["fulamt_ownpay_amt"] = fulamt_ownpay_amt //
	inputData["overlmt_selfpay"] = overlmt_selfpay     //
	inputData["preselfpay_amt"] = preselfpay_amt       //
	inputData["inscp_scp_amt"] = inscp_scp_amt         //

	input["data"] = inputData
	inputMessage["input"] = input //交易输入

	bytesData, err := json.Marshal(inputMessage)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	request_url = request_url + "2207A"
	//fmt.Println(HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)))

	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), string(bytesData)

}

//  门诊结算撤销
func Jsyb2208(psnNo string, mdtrtId string, setlId string, org_name string, doctor string, secret_key string, fixmedins_code string, insuplc_admdvs string, mdtrtarea_admvs string, request_url string, access_key string, cainfo string) (string, string) {
	timestamp := time.Now().Unix()
	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "2208" // 交易编码

	inputData["psn_no"] = psnNo     // 人员编号 （来自1101接口返回）
	inputData["mdtrt_id"] = mdtrtId // 就诊 ID（来自2201接口返回）
	inputData["setl_id"] = setlId   // 结算 ID

	input["data"] = inputData
	inputMessage["input"] = input //交易输入

	bytesData, err := json.Marshal(inputMessage)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	request_url = request_url + "2208"
	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), string(bytesData)

}

func Jsyb5203(baseParams models.BaseParams, businessParams models.BusinessParams) (string, string) {
	timestamp := time.Now().Unix()
	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, baseParams.OrgName, baseParams.Doctor, baseParams.FixmedinsCode, baseParams.InsuplcAdmdvs, baseParams.MdtrtareaAdmvs, baseParams.Cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "5203" // 交易编码
	inputData["psn_no"] = businessParams.PsnNo
	inputData["setl_id"] = businessParams.SetlId
	inputData["mdtrt_id"] = businessParams.MdtrtId
	input["data"] = inputData
	inputMessage["input"] = input //交易输入
	bytesData, err := json.Marshal(inputMessage)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	baseParams.RequestUrl = baseParams.RequestUrl + "5203"
	//fmt.Println(HttpRequest(baseParams.RequestUrl, baseParams.AccessKey, baseParams.SecretKey, timestamp, string(bytesData)))

	return HttpRequest(baseParams.RequestUrl, baseParams.AccessKey, baseParams.SecretKey, timestamp, string(bytesData)), string(bytesData)
}

//CBS总线
func HttpRequest(url string, ak string, sk string, timestamp int64, json string) string {
	fmt.Println(url)
	//step 1. 设置请求参数
	csbHP := csbhttp.NewHttpParams(url) //设置请求地址及url参数
	//设置调用方式 get 或者 post
	csbHP.SetMethod("post")
	//csbHP.AddParam("input", "")
	//设置 调用的服务名和版本
	csbHP.SetApi("hssServives")
	csbHP.SetVersion("1.0.0")

	//添加附加的http请求头 (可以定义多条)
	//key := "_api_access_key=" + ak + "&_api_name=" + "hssServives" + "&_api_timestamp=" + strconv.FormatInt(timestamp, 10) + "&_api_version=" + "1.0.0"
	csbHP.AddHeader("_api_timestamp", strconv.FormatInt(timestamp, 10))
	csbHP.AddHeader("_api_name", "hssServives")
	csbHP.AddHeader("_api_version", "1.0.0")
	csbHP.AddHeader("_api_access_key", ak)
	csbHP.AddHeader("Content-Type", "application/json;charset=UTF-8")

	//request.Header.Set("Content-Type", "application/json;charset=UTF-8")

	//还可以,设置附加的byte[]或者json请求体

	csbHP.SetContentBody(json, nil)
	//设置请求的ak和sk
	csbHP.SetAK(ak)
	csbHP.SetSK(sk)
	//打印请求参数信息
	csbHP.Print()
	//进行调用, 返回的第二项为resonse http headers (map[string][]string类型)
	//res, _, err := csbhttp.Invoke(*csbHP, requestByte)
	res, _, err := csbhttp.Invoke(*csbHP, strconv.FormatInt(timestamp, 10))
	//是否发成错误
	if err != nil {
		fmt.Println(err)
		return ""
	}
	fmt.Println(res)
	return res
}

// 生成基础报文
func SetJSInputMessage(timestamp int64, org_name string, doctor string, fixmedins_code string, insuplc_admdvs string, mdtrtarea_admvs string, cainfo string) map[string]interface{} {
	// 生成签名
	tempTime := time.Unix(timestamp, 0)
	//timeFormat := tempTime.Format("20060102150405")
	timeFormatOne := tempTime.Format("2006-01-02 15:04:05")
	//randNum := rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(10000)

	month := time.Unix(1557042972, 0).Format("1")
	year := time.Now().Format("2006")
	month = time.Now().Format("01")
	day := time.Now().Format("02")

	hour := time.Now().Format("15")
	min := time.Now().Format("04")
	sec := time.Now().Format("05")

	msec := time.Now().Format("000")

	// 生成输入报文
	inputMessage := make(map[string]interface{})
	inputMessage["msgid"] = fixmedins_code + year + month + day + hour + min + sec + msec + "0" // 发送方报文 ID
	inputMessage["mdtrtarea_admvs"] = mdtrtarea_admvs                                           // 就医地医保区划
	inputMessage["insuplc_admdvs"] = insuplc_admdvs                                             // 参保地医保区划
	inputMessage["recer_sys_code"] = "1"                                                        // 接收方系统代码
	inputMessage["dev_no"] = "1"                                                                // 设备编号
	inputMessage["dev_safe_info"] = "1"                                                         // 设备安全信息
	inputMessage["cainfo"] = cainfo                                                             // 数字签名信息
	inputMessage["signtype"] = "SM3"                                                            // 签名类型
	inputMessage["infver"] = "V1.0"                                                             // 接收方系统代码
	inputMessage["opter_type"] = "1"                                                            // 经办人类别
	inputMessage["opter"] = doctor                                                              // 经办人
	inputMessage["opter_name"] = doctor                                                         // 经办人姓名
	inputMessage["inf_time"] = timeFormatOne                                                    // 交易时间
	inputMessage["fixmedins_code"] = fixmedins_code                                             // 定点医药机构编号
	inputMessage["fixmedins_name"] = org_name
	inputMessage["fixmedins_soft_fcty"] = "深圳市领透科技有限公司" //定点医药机构名称
	//定点医药机构名称
	inputMessage["sign_no"] = "" //交易签到流水号
	return inputMessage
}

func Jsyb5301(psnNo string, org_name string, doctor string, fixmedins_code string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, request_url string, access_key string, cainfo string) (string, string) {
	// 生成签名
	//nonce := GetRandomString(32)
	timestamp := time.Now().Unix()
	//signature := setSignature(timestamp, nonce, secret_key)
	//tempTime := time.Unix(timestamp, 0)
	//timeFormatOne := tempTime.Format("2006-01-02 15:04:05")

	//timeFormatTwo := tempTime.Format("2006-01-02")

	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "5301" // 交易编码
	inputData["psn_no"] = psnNo    // 人员编号 （来自1101接口返回）
	input["data"] = inputData
	inputMessage["input"] = input //交易输入
	bytesData, err := json.Marshal(inputMessage)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}

	request_url = request_url + "5301"
	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), string(bytesData)

}

func Jsyb2503(psnNo string, insutype string, org_name string, doctor string, fixmedins_code string, doctor_id string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, sick_name string, sick_code string, start_time string, end_time string, check_time string, request_url string, access_key string, cainfo string) (string, string) {
	timestamp := time.Now().Unix()
	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "2503"                 // 交易编码
	inputData["psn_no"] = psnNo                    // 人员编号 （来自1101接口返回）
	inputData["insutype"] = insutype               // 险种类型（来自1101接口返回）
	inputData["opsp_dise_code"] = sick_code        // 险种类型（来自1101接口返回）
	inputData["opsp_dise_name"] = sick_name        // 险种类型（来自1101接口返回）
	inputData["insu_optins"] = insuplc_admdvs      // 险种类型（来自1101接口返回）
	inputData["ide_fixmedins_no"] = fixmedins_code // 险种类型（来自1101接口返回）
	inputData["ide_fixmedins_name"] = org_name     // 险种类型（来自1101接口返回）
	inputData["hosp_ide_date"] = check_time        // 险种类型（来自1101接口返回）
	inputData["diag_dr_codg"] = doctor_id          // 开始时间
	inputData["diag_dr_name"] = doctor             // 开始时间
	inputData["begndate"] = start_time             // 医师编码
	inputData["tel"] = ""                          // 医师编码
	inputData["addr"] = ""                         // 医师编码
	inputData["enddate"] = end_time                // 医师编码

	input["data"] = inputData
	inputMessage["input"] = input //交易输入
	bytesData, err := json.Marshal(inputMessage)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	request_url = request_url + "2503"
	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), string(bytesData)
}

func Jsyb3201(fixmedins_setl_cnt int64, acct_pay float64, fund_pay_sumamt float64, medfee_sumamt float64, stmt_begndate string, stmt_enddate string, setl_optins string, clr_type string, insutype string, org_name string, doctor string, fixmedins_code string, insuplc_admdvs string, mdtrtarea_admvs string, request_url string, access_key string, secret_key string, cainfo string) (string, string) {
	// 生成签名
	timestamp := time.Now().Unix()
	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "3201"                       // 交易编码
	inputData["insutype"] = insutype                     // 险种
	inputData["clr_type"] = clr_type                     // 就诊 ID（来自2201接口返回）
	inputData["setl_optins"] = setl_optins               // 结算经办机构
	inputData["stmt_begndate"] = stmt_begndate           // 对帐开始时间
	inputData["stmt_enddate"] = stmt_enddate             //对帐结束时间
	inputData["medfee_sumamt"] = medfee_sumamt           //医疗费总额
	inputData["fund_pay_sumamt"] = fund_pay_sumamt       //基金支付总额
	inputData["acct_pay"] = acct_pay                     //个人账户支付金额
	inputData["fixmedins_setl_cnt"] = fixmedins_setl_cnt //定点医药机构结算笔数
	inputData["refd_setl_flag"] = 0                      //

	input["data"] = inputData
	inputMessage["input"] = input //交易输入
	bytesData, err := json.Marshal(inputMessage)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}
	request_url = request_url + "3201"
	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), string(bytesData)
}

func Jsyb9001(org_name string, doctor string, fixmedins_code string, insuplc_admdvs string, mdtrtarea_admvs string, secret_key string, admin_user_id int64, request_url string, cainfo string, access_key string) (string, string) {

	// 生成签名
	timestamp := time.Now().Unix()
	// 生成输入报文
	inputMessage := SetJSInputMessage(timestamp, org_name, doctor, fixmedins_code, insuplc_admdvs, mdtrtarea_admvs, cainfo)
	input := make(map[string]interface{})
	inputData := make(map[string]interface{})
	inputMessage["infno"] = "9001" // 交易编码
	inputData["opter_no"] = admin_user_id
	inputData["mac"] = GetMacTwo() // 开始时间
	inputData["ip"] = GetLocalIpTwo()
	input["signIn"] = inputData

	inputMessage["input"] = input //交易输入
	bytesData, err := json.Marshal(inputMessage)
	fmt.Println(string(bytesData))
	if err != nil {
		fmt.Println(err.Error())
		return err.Error(), ""
	}

	//reader := bytes.NewReader(bytesData)
	//url := "http://igb.hsa.gdgov.cn/ebus/gdyb_api/prd/hsa/hgs/5301"
	//gdyb_url := beego.AppConfig.String("gdyb_url")
	//gdyb_paasid := beego.AppConfig.String("gdyb_paasid")
	//url := "http://igb.hsa.gdgov.cn/ebus/gdyb_api/prd/hsa/hgs/1101"
	//url := gdyb_url

	//request, err := http.NewRequest("POST", request_url+"9001", reader)
	//if err != nil {
	//	fmt.Println(err.Error())
	//	return err.Error(), ""
	//}
	//
	//request.Header.Set("Content-Type", "application/json;charset=UTF-8")
	//request.Header.Set("_api_timestamp", strconv.FormatInt(timestamp, 10))
	//request.Header.Set("_api_name", "hssServives")
	//request.Header.Set("_api_version", "1.0.0")
	//request.Header.Set("_api_access_key", access_key)
	//request.Header.Set("_api_signature", "")
	//
	//client := http.Client{}
	//resp, err := client.Do(request)
	//if err != nil {
	//	fmt.Println(err.Error())
	//	return err.Error(), ""
	//}
	//respBytes, err := ioutil.ReadAll(resp.Body)
	//if err != nil {
	//	fmt.Println(err.Error())
	//	return err.Error(), ""
	//}
	//str := string(respBytes)
	//fmt.Println(str)
	//return str, ""

	request_url = request_url + "9001"
	return HttpRequest(request_url, access_key, secret_key, timestamp, string(bytesData)), string(bytesData)

}

//获取本机ip
func GetLocalIpTwo() string {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		fmt.Println("get local ip failed")
	}
	for _, address := range addrs {
		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil {
				return ipnet.IP.String()
			}
		}
	}
	return ""
}

//获取本机Mac
func GetMacTwo() string {
	interfaces, err := net.Interfaces()
	if err != nil {
		fmt.Println("Get loacl Mac failed")
	}
	for _, inter := range interfaces {
		mac := inter.HardwareAddr
		fmt.Println("+======" + mac.String())
		if mac.String() != "" {
			return mac.String()
		}

	}
	return ""
}
