12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436 |
- package service
-
- import (
- "IC/models"
- "IC/utils"
- _ "IC/utils"
- "encoding/json"
- _ "encoding/json"
- "encoding/xml"
- _ "fmt"
- "github.com/astaxie/beego/httplib"
- _ "github.com/jinzhu/gorm"
- "net/url"
- "strconv"
- "strings"
- "time"
- )
-
- type XmlKey struct {
- XMLName xml.Name `xml:"string"`
- StringName string `xml:"name,attr"`
- InnerText string `xml:",innerxml"`
- }
-
- // 获取艾迪康的key
- func GetAdKey(logid string, password string) (key string) {
- Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/Login?"
- apiurl := "logid=" + logid + "&password=" + password
- apiurl = url.PathEscape(apiurl)
- apiurl = Domain + apiurl
- xmlKey := XmlKey{}
- httplib.Get(apiurl).ToXML(&xmlKey)
- return xmlKey.InnerText
- }
-
- type ADString struct {
- XMLName xml.Name `xml:"string" json:"string"`
- NewDataSet rpnewDataSet `xml:"NewDataSet" json:"NewDataSet"`
- }
-
- type rpnewDataSet struct {
- ListTable []rplisttable `xml:"listtable" json:"listtable"`
- }
-
- type rplisttable struct {
- Id string `xml:"Id" json:"Id"`
- PatientName string `xml:"PatientName" json:"PatientName"`
- Bgrq string `xml:"Bgrq" json:"Bgrq"`
- Instrument string `xml:"Instrument" json:"Instrument"`
- LisDate string `xml:"LisDate" json:"LisDate"`
- Sampleid string `xml:"Sampleid" json:"Sampleid"`
- // ReportType string `xml:"ReportType" json:"ReportType"`
- // AdiconBarcode string `xml:"AdiconBarcode" json:"AdiconBarcode"`
- // CustomerBarcode string `xml:"CustomerBarcode" json:"CustomerBarcode"`
- // Repno string `xml:"Repno" json:"Repno"`
- // PdfFileName string `xml:"PdfFileName" json:"PdfFileName"`
- // DownFlag string `xml:"DownFlag" json:"DownFlag"`
- // DownDate string `xml:"DownDate" json:"DownDate"`
- // Sjrq string `xml:"Sjrq" json:"Sjrq"`
- // Sjys string `xml:"Sjys" json:"Sjys"`
- // Brnl string `xml:"Brnl" json:"Brnl"`
- // Brxb string `xml:"Brxb" json:"Brxb"`
- // Bbzl string `xml:"bbzl" json:"bbzl"`
- // Bzlbid string `xml:"Bzlbid" json:"Bzlbid"`
- // PDFFileType string `xml:"PDFFileType" json:"PDFFileType"`
- // PageType string `xml:"PageType" json:"PageType"`
- // PatientNo string `xml:"PatientNo" json:"PatientNo"`
- // SfzId string `xml:"SfzId" json:"SfzId"`
- // CertificatesType string `xml:"CertificatesType" json:"CertificatesType"`
- // Nationality string `xml:"Nationality" json:"Nationality"`
- // SerialNumber string `xml:"SerialNumber" json:"SerialNumber"`
- // Str1 string `xml:"Str1" json:"Str1"`
- // Str2 string `xml:"Str2" json:"Str2"`
- // Str3 string `xml:"Str3" json:"Str3"`
- // Str4 string `xml:"Str4" json:"Str4"`
- // Str5 string `xml:"Str5" json:"Str5"`
- // TWBG string `xml:"TWBG" json:"TWBG"`
- }
-
- // 获取机构一段时间内检验检查列表
- func GetAdGetReportList(Key string, BeginDateTime string, EndDateTime string, TypeDateTime string, AgainFlag string) (key ADString) {
- Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetReportList?"
- apiurl := "Key=" + Key + "&BeginDateTime=" + BeginDateTime + "&EndDateTime=" + EndDateTime + "&TypeDateTime=" + TypeDateTime + "&AgainFlag=" + AgainFlag
- apiurl = url.PathEscape(apiurl)
- apiurl = Domain + apiurl
- utils.InfoLog("url0: %v", apiurl)
- var xmlKey ADString
- xmlString, _ := httplib.Get(apiurl).String()
- xmlString = strings.Replace(xmlString, "<", "<", -1)
- xmlString = strings.Replace(xmlString, ">", ">", -1)
- xml.Unmarshal([]byte(xmlString), &xmlKey)
- return xmlKey
- }
-
- type adkCheck struct {
- ApplyId string `json:"applyid"`
- ReportNo string `json:"reportno"`
- PatName string `json:"patname"`
- Sex string `json:"sex"`
- Age string `json:"age"`
- DptName string `json:"dptname"`
- Testtype string `json:"testtype"`
- Testcode string `json:"testcode"`
- ExecuteTime string `json:"receivetime"`
- Checktime string `json:"checktime"`
- Checkcode string `json:"checkcode"`
- Result []adkResult `json:"result"`
- }
-
- func GetAdkInfo(key string, id string) (str []adkCheck, checkStr string) {
- Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetJSONReportItemListById?"
- apiurl := "key=" + key + "&Id=" + id
- apiurl = url.PathEscape(apiurl)
- apiurl = Domain + apiurl
- utils.InfoLog("url1: %v", apiurl)
- xmlKey := XmlKey{}
- httplib.Get(apiurl).ToXML(&xmlKey)
- utils.InfoLog("url2: %v", xmlKey.InnerText)
- var checkJSON []adkCheck
- json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
-
- return checkJSON, xmlKey.InnerText
- }
-
- // 沅胜透析中心Lis同步
- func SyncLisYstx() (err error) {
- // 第一步:获取艾迪康下发的Key
- org_id := int64(9919)
- login := "R26028"
- password := "abc123"
- key := GetAdKey(login, password)
- // 第二步:获取上一次同步的时间点
- syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
- var sync_time int64
- if syncLastInfo.ID > 0 {
- sync_time = syncLastInfo.SyncTime + 86400
- } else {
- sync_time = 1577808000
- }
- nowTimes := time.Now().Unix()
- // syncTimes := time.Unix(sync_time,0).Format("2006-01-02")
- if sync_time < nowTimes {
- for i := sync_time; i < nowTimes; i = i + 86400 {
- tempsyncTimes := time.Unix(i, 0).Format("2006-01-02")
- utils.InfoLog("tempsyncTimes:%v", tempsyncTimes)
- adString := GetAdGetReportList(key, tempsyncTimes, tempsyncTimes, "2", "2")
- listtable := adString.NewDataSet.ListTable
- utils.InfoLog("listtable: %v", listtable)
- if len(listtable) > 3 {
- // 第三步:根据检查列表获取的ID来获取检验检查详情
- for _, list := range listtable {
- listId := list.Id
- utils.InfoLog("listId: %v", listId)
- checkInfo, checkStr := GetAdkInfo(key, listId)
- utils.InfoLog("checkInfo: %v", checkInfo)
- utils.InfoLog("checkStr: %v", checkStr)
- for _, info := range checkInfo {
- // 根据姓名 去查询对应的patient_id
- patient, _ := GetPatientID(org_id, info.PatName)
- utils.InfoLog("patient: %v", patient)
- if patient.ID > 0 {
- middleReportHandle := models.MiddleReportHandle{
- OrgId: org_id,
- HisUserId: strconv.FormatInt(patient.ID, 10),
- ApplyNo: info.ApplyId,
- ReportId: info.ReportNo,
- Name: info.PatName,
- Sex: info.Sex,
- Age: info.Age,
- Dept: info.DptName,
- PatientId: strconv.FormatInt(patient.ID, 10),
- RequestTime: info.ExecuteTime,
- ExecuteTime: info.ExecuteTime,
- ReceiveTime: info.ExecuteTime,
- CheckTime: info.ExecuteTime,
- ReportInfo: checkStr,
- CreatedTime: time.Now().Unix(),
- UpdatedTime: time.Now().Unix(),
- }
- handleerr := CreateReportHandle(&middleReportHandle)
- if handleerr != nil {
- utils.ErrorLog("创建检验检查操作数据失败:%v", handleerr)
- return
- }
-
- YsReportInfoHandle(info, org_id, patient.ID)
- }
- }
- }
- var syncInfo models.MiddleSyncInfo
- syncInfo.OrgId = org_id
-
- syncInfo.SyncTime = i
- syncInfo.SyncResultType = 1
- syncInfo.SyncRsultRemark = "同步成功"
- syncInfo.SyncTotalNum = 0
- syncInfo.SyncSuccessNum = 0
- syncInfo.SyncInfo = ""
- syncInfo.CreateTime = time.Now().Unix()
- syncInfo.UpdateTime = time.Now().Unix()
-
- cwderr := CreateSyncInfo(&syncInfo)
- if cwderr != nil {
- utils.ErrorLog("创建同步信息失败:%v", cwderr)
- return
- }
- }
-
- }
-
- }
-
- return
- }
-
- // 根据姓名获取patient_id
- func GetPatientID(org_id int64, patient_name string) (patient models.Patients, err error) {
- err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1 and name = ? ", org_id, patient_name).First(&patient).Error
- return
- }
-
- func GetProjectInfo(item_id int64, org_id int64) (project_info models.MiddleInspectionReference, err error) {
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and item_id = ?", org_id, item_id).First(&project_info).Error
- return
- }
-
- // 修改配置信息
- func UpdateInspectionReference(InspectionReference *models.MiddleInspectionReference) error {
- err := readMiddleDb.Model(&models.MiddleInspectionReference{}).Updates(&InspectionReference).Error
- return err
- }
-
- // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
- func GetYsProjectID(org_id int64, project_name string) (project_id int64, err error) {
- var inspection_reference models.MiddleInspectionReference
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
- if inspection_reference.ID > 0 {
- return inspection_reference.ProjectId, err
- } else {
- err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
- utils.InfoLog("inspection_reference: %v", inspection_reference)
- if inspection_reference.ProjectId > 0 {
- return inspection_reference.ProjectId + 1, err
- } else {
- return 38001, err
- }
- }
- }
-
- // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
- func GetGzjhProjectID(org_id int64, project_name string) (project_id int64, err error) {
- var inspection_reference models.MiddleInspectionReference
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
- if inspection_reference.ID > 0 {
- return inspection_reference.ProjectId, err
- } else {
- err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
- if inspection_reference.ProjectId > 0 {
- return inspection_reference.ProjectId + 1, err
- } else {
- return 116001, err
- }
- }
- }
-
- func YsReportInfoHandle(CheckInfo adkCheck, org_id int64, patientId int64) {
- // 1.先把所有的检查项弄出来
- for _, info := range CheckInfo.Result {
- // 根据project_id和item_id 判断该项是否存在
- curitemcode, _ := strconv.ParseInt(info.Itemcode, 10, 64)
- reference, _ := GetProjectInfo(curitemcode, org_id)
- utils.InfoLog("reference: %v", info)
- if reference.ID > 0 {
- if reference.ProjectId > 0 {
- LgthYsSync(info, reference.ProjectId, curitemcode, org_id, patientId, CheckInfo.ExecuteTime)
- } else {
- project_id := int64(0)
- if len(info.Groupitemcode) > 0 {
- // 对没有project信息的项目进行修改
- project_id, _ = GetYsProjectID(org_id, info.Groupitemcode)
- reference.ProjectId = project_id
- reference.ProjectName = info.Groupitemcode
- reference.Project = info.Groupitemcode
- // 修改配置信息
- UpdateInspectionReference(&reference)
- }
- LgthYsSync(info, project_id, curitemcode, org_id, patientId, CheckInfo.ExecuteTime)
- }
- } else {
- project_id := int64(0)
- if len(info.Groupitemcode) > 0 {
- project_id, _ = GetYsProjectID(org_id, info.Groupitemcode)
- }
- LgthYsSync(info, project_id, curitemcode, org_id, patientId, CheckInfo.ExecuteTime)
- }
- }
-
- SyncToYstx()
- }
-
- type adkResult struct {
- Testno string `json:"testno"`
- Reportno string `json:"reportno"`
- Itemord string `json:"itemord"`
- Itemcode string `json:"itemcode"`
- Itemname string `json:"itemname"`
- ItemEname string `json:"itemename"`
- Result string `json:"result"`
- Units string `json:"units"`
- Testdate string `json:"testdate"`
- Eqpname string `json:"eqpname"`
- Groupitemcode string `json:"groupitemcode"`
- Groupitemname string `json:"groupitemname"`
- Limit string `json:"limit"`
- Resultstate string `json:"resultstate"`
- Criticalflag string `json:"criticalflag"`
- Itemename string `json:"itemename"`
- }
-
- // 从机构将数据同步到中间库
- func LgthYsSync(report adkResult, project_id int64, item_id int64, org_id int64, patient_id int64, receivetime string) (*models.MiddleInspectionReference, error) {
- tx := writeMiddleDb.Begin()
- var inspection models.MiddleInspection
- var inspection_reference models.MiddleInspectionReference
- recordDateStr := receivetime
-
- if len(receivetime) == 0 {
- recordDateStr = time.Now().Format("2006-01-02 15:04")
- }
-
- record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
- record_date := record_date_str.Unix()
- inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
- // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
- // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
- var total int
- var RangeOptions string
- var RangeMin string
- var RangeMax string
-
- // // 判断检查类型
- ItemType := 1
- Range := strings.Split(report.Limit, "-")
- if len(Range) > 1 {
- RangeMin = Range[0]
- RangeMax = Range[1]
- ItemType = 1
- } else {
- ItemType = 2
- RangeOptions = report.Limit
- }
-
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project_id = ? and item_id = ? and status = 1", org_id, project_id, item_id).Find(&inspection_reference).Count(&total).Error
- if inspection_reference.ID > 0 {
- ItemType = inspection_reference.RangeType
- }
- if total <= 0 {
- inspection_reference.OrgId = org_id
- inspection_reference.ProjectName = report.Groupitemcode
- inspection_reference.Project = report.Groupitemcode
- inspection_reference.ProjectId = project_id
- inspection_reference.ItemName = report.Itemname
- inspection_reference.ItemNameAddition = report.ItemEname
- inspection_reference.ItemId = item_id
- inspection_reference.RangeType = ItemType
- inspection_reference.RangeMin = RangeMin
- inspection_reference.RangeMax = RangeMax
- // inspection_reference.RangeValue = RangeValue
- inspection_reference.RangeOptions = RangeOptions
- inspection_reference.Unit = report.Units
- inspection_reference.Status = 1
- inspection_reference.CreatedTime = time.Now().Unix()
- inspection_reference.UpdatedTime = time.Now().Unix()
- inspection_reference.InspectDate = inspect_date
- inspection_reference.UTime = inspect_date
- err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
- if err != nil {
- tx.Rollback()
- }
- }
-
- var itotal int
- err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and project_id = ? and item_id = ? and record_date = ? and patient_id = ? and status = 1", org_id, project_id, item_id, record_date, patient_id).Find(&inspection).Count(&itotal).Error
- if itotal <= 0 {
- report.Result = strings.Replace(report.Result, ">", ">", -1)
- report.Result = strings.Replace(report.Result, "<", "<", -1)
- inspection.PatientId = patient_id
- inspection.OrgId = org_id
- inspection.ProjectId = project_id
- inspection.ItemName = inspection_reference.ItemName
- inspection.ProjectName = inspection_reference.ProjectName
- inspection.InspectType = ItemType
- inspection.ItemId = item_id
- inspection.InspectValue = report.Result
- inspection.InspectDate = inspect_date
- inspection.RecordDate = record_date
- inspection.InspectTips = report.Resultstate
- inspection.Status = 1
- inspection.CreatedTime = time.Now().Unix()
- inspection.UpdatedTime = time.Now().Unix()
- inspection.UTime = inspect_date
- inspection.HisUserId = strconv.FormatInt(patient_id, 10)
- err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
- if err != nil {
- tx.Rollback()
- }
- }
-
- // //处理检验检查操作表里同步条数和状态
- // cur_num := Head.ReportInfoItemHandleNum + 1
- // Head.ReportInfoItemHandleNum = cur_num
- // handleType := 2
- // if cur_num == Head.ReportInfoItemNum {
- // handleType = 1
- // }
- // middleReportHandle := models.MiddleReportHandle{
- // ID: Head.ID,
- // ReportInfoItemHandleNum: cur_num,
- // HandleType: int64(handleType),
- // UpdatedTime: time.Now().Unix(),
- // }
- // err = UpdateReportHandleListyOrgID(&middleReportHandle)
- // if err != nil {
- // tx.Rollback()
- // }
- // }
-
- tx.Commit()
- return nil, err
- }
-
- func SyncToYstx() {
- utils.TraceLog("检验检查同步任务开始执行")
- org_id := int64(9919)
-
- // 第一步:跟进org_id 去中间库查出需要同步的数据
- inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
- inspections, _ := GetSyncInspectionByOrgId(org_id)
-
- // 第二步:将数据同步到业务库
- if len(inspection_references) > 0 {
- for _, inspection_reference := range inspection_references {
- SyncInspectionReference(&inspection_reference)
- }
- }
-
- if len(inspections) > 0 {
- for _, inspection := range inspections {
- SyncInspection(&inspection)
- }
- }
- utils.SuccessLog("检验检查同步任务完成")
- }
-
- // 根据姓名获取patient_id
- func GetPatientGzjhList(org_id int64, schedulesTime int64) (schedules []*models.DialysisSchedule, err error) {
- err = readDb.Model(&models.DialysisSchedule{}).Where("user_org_id = ? and status = 1 and schedule_date = ?", org_id, schedulesTime).Preload("Patients", "status = 1").Find(&schedules).Error
- return
- }
-
- // 根据姓名获取admin_user_id
- func GetAdminUserId(org_id int64, doctor_name string) (doctor models.UserAdminRole, err error) {
- err = writeUserDb.Model(&models.UserAdminRole{}).Where("org_id = ? and status = 1 and user_name = ?", org_id, doctor_name).First(&doctor).Error
- return
- }
-
- func GetPatientGzjhByOrgId(org_id int64) (patientList []*models.Patients, err error) {
- err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1", org_id).Find(&patientList).Error
- return
- }
-
- type gzjhResult struct {
- ErrorCode int64 `json:"error_code"`
- Result []gzjhyz `json:"data"`
- }
-
- type gzjhyz struct {
- HisUserId string `json:"his_user_id"`
- AdviceId int64 `json:"advice_id"`
- UserName string `json:"user_name"`
- DoctorId string `json:"doctor_id"`
- AdviceType string `json:"advice_type"`
- StartTime string `json:"start_time"`
- AdviceName string `json:"advice_name"`
- AdviceDesc string `json:"advice_desc"`
- SingleDose string `json:"single_dose"`
- SingleDoseUnit string `json:"single_dose_unit"`
- PrescribingNumber string `json:"prescribing_number"`
- PrescribingNumberUnit string `json:"prescribing_number_unit"`
- DeliveryWay string `json:"delivery_way"`
- ExecutionFrequency string `json:"execution_frequency"`
- State string `json:"state"`
- CTime string `json:"c_time"`
- UTime string `json:"u_time"`
- }
-
- func GetGzjhYz(sync_time int64, his_user_id string) (str gzjhResult, checkStr string) {
- apiurl := "http://219.135.166.106:8067/WebService.asmx/queryyzxx"
- rep := httplib.Get(apiurl)
- rep.Param("sync_time", strconv.FormatInt(sync_time,10))
- rep.Param("access_token","0")
- rep.Param("his_user_id", his_user_id)
- utils.InfoLog(strconv.FormatInt(sync_time,10))
- utils.InfoLog(his_user_id)
- // str, _ := rep.String()
- xmlKey := XmlKey{}
- rep.ToXML(&xmlKey)
-
- var checkJSON gzjhResult
- json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
- return checkJSON, xmlKey.InnerText
- }
-
- func CreateDoctorAdviceHandle(advice *models.XtDoctorAdvice) error {
- err := writeDb.Create(&advice).Error
- return err
- }
-
- func GetAdviceBySyncAdviceId(orgId int64, sync_advice_id int64) (advice models.XtDoctorAdvice, err error) {
- err = readDb.Model(&models.XtDoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND sync_advice_id = ?", orgId, sync_advice_id).First(&advice).Error
- return
- }
-
- // 广州暨华透析中心医嘱同步
- func SyncGzjhyz() (err error) {
- // 第一步:获取上次同步时间
- org_id := int64(10015)
-
- redisClient := RedisClient()
- defer redisClient.Close()
- sync_time_tt, _ := redisClient.Get(redisClient.Context(),"gdjh_sync_time").Result()
-
- sync_time,_ := strconv.ParseInt(sync_time_tt,10,64)
- if sync_time == 0 {
- sync_time = time.Now().Unix()
- }
- new_sync_time := time.Now().Unix()
- utils.InfoLog("new_sync_time:%v", new_sync_time)
- // 切换为当前时间
- redisClient.Set(redisClient.Context(),"gdjh_sync_time", new_sync_time, time.Minute*60*24)
- // 第二步: 获取当天排班患者的住院号
- schedulesDateStr := time.Now().Format("2006-01-02")
- schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
- schedulesTime := schedulesDate.Unix()
- var patient_zy_ids string
- // patient_zy_ids, _ := redisClient.Get(redisClient.Context(),"gdjh_patient_zy_ids").Result()
- // if len(patient_zy_ids) == 0 {
- schedules, _ := GetPatientGzjhList(org_id, schedulesTime)
- if len(schedules) > 0 {
- for _, item := range schedules {
- // if len(item.Patients.AdmissionNumber) > 0 {
- if len(patient_zy_ids) == 0 {
- patient_zy_ids = item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10) + "-" + item.Patients.AdmissionNumber
- } else {
- patient_zy_ids = patient_zy_ids + "," + item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10) + "-" + item.Patients.AdmissionNumber
- }
- // }
- }
- // redisClient.Set(redisClient.Context(),"gdjh_patient_zy_ids", patient_zy_ids, time.Minute*5)
- }
- // }
- utils.InfoLog("2gdjh_patient_zy_ids:%v", patient_zy_ids)
-
- // 第三步: 根据获取的同步时间,同步人员,去获取医嘱信息
- idArray := strings.Split(patient_zy_ids, ",")
- for _, idStr := range idArray {
- temp_id := strings.Split(idStr, "-")
- id := temp_id[0]
- patient_id := temp_id[1]
- admission_number := temp_id[2]
- gzjhyz, yzstr := GetGzjhYz(sync_time, id)
- if len(gzjhyz.Result) == 0 && len(admission_number) > 0{
- utils.InfoLog("admission_number:%v", admission_number)
- gzjhyz, yzstr = GetGzjhYz(sync_time, admission_number)
- }
- utils.InfoLog("sync_time:%v", sync_time)
- utils.InfoLog("yzstr:%v", yzstr)
- if len(gzjhyz.Result) > 0 {
- timeLayout := "2006010215:04:05" //转化所需模板
- loc, _ := time.LoadLocation("Local") //重要:获取时区
- for _, yz := range gzjhyz.Result {
- // 根据姓名获取医生ID
- var doctor_id int64
- temp_doctor_id, _ := redisClient.Get(redisClient.Context(),"gdjh_doctor_id_" + yz.DoctorId).Result()
- if len(temp_doctor_id) == 0 {
- doctor, _ := GetAdminUserId(org_id, yz.DoctorId)
- doctor_id = doctor.AdminUserId
- if doctor_id > 0 {
- redisClient.Set(redisClient.Context(),"gdjh_doctor_id_"+yz.DoctorId, doctor_id, time.Minute*60*2)
- } else {
- continue
- }
- } else {
- doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
- }
- theTime, _ := time.ParseInLocation(timeLayout, yz.StartTime, loc) //使用模板在对应时区转化为time.time类型
- start_time := theTime.Unix()
- temp_patient_id, _ := strconv.ParseInt(patient_id, 10, 64)
- SingleDose, _ := strconv.ParseFloat(yz.SingleDose, 64)
- PrescribingNumber, _ := strconv.ParseFloat(yz.PrescribingNumber, 64)
- advice := models.XtDoctorAdvice{
- UserOrgId: org_id,
- PatientId: temp_patient_id,
- AdviceType: 2,
- AdviceDate: schedulesTime,
- StartTime: start_time,
- AdviceName: yz.AdviceName,
- AdviceDesc: yz.AdviceDesc,
- RecordDate: schedulesTime,
- SingleDose: SingleDose,
- SingleDoseUnit: yz.SingleDoseUnit,
- PrescribingNumber: PrescribingNumber,
- PrescribingNumberUnit: yz.PrescribingNumberUnit,
- DeliveryWay: yz.DeliveryWay,
- ExecutionFrequency: yz.ExecutionFrequency,
- AdviceDoctor: doctor_id,
- CreatedTime: time.Now().Unix(),
- UpdatedTime: time.Now().Unix(),
- SyncAdviceId: yz.AdviceId,
- ExecutionState: 2,
- Status: 1,
- IsSync: 1,
- }
- utils.InfoLog("advice:%v", advice)
- // 根据同步来的 advice_id 来查询库里是否已经存在
- advice_info, _ := GetAdviceBySyncAdviceId(org_id, yz.AdviceId)
- utils.InfoLog("advice_info:%v", advice_info)
- if advice_info.ID > 0 {
- key := strconv.FormatInt(org_id, 10) + ":" + strconv.FormatInt(temp_patient_id, 10) + ":" + strconv.FormatInt(schedulesTime, 10) + ":doctor_advices"
- //清空key 值
- redisClient.Set(redisClient.Context(),key, "", time.Second)
- keyOne := strconv.FormatInt(org_id, 10) + ":" + strconv.FormatInt(schedulesTime, 10) + ":advice_list_all"
- redisClient.Set(redisClient.Context(),keyOne, "", time.Second)
- continue
- } else {
- if len(yz.AdviceName) > 0 {
- handleerr := CreateDoctorAdviceHandle(&advice)
- if handleerr != nil {
- utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
- continue
- }
- } else {
- continue
- }
- }
- }
- }
- }
- return
- }
-
- type gzjhLisResult struct {
- ErrorCode int64 `json:"error_code"`
- Result []gzjhlis `json:"data"`
- }
-
- type gzjhlis struct {
- HisUserId string `json:"his_user_id"`
- PatientName string `json:"patient_name"`
- ProjectId string `json:"project_id"`
- ProjectName string `json:"project_name"`
- InspectDate string `json:"inspect_date"`
- ItemId string `json:"item_id"`
- ItemName string `json:"item_name"`
- ItemType string `json:"item_type"`
- ItemValue string `json:"item_value"`
- RangeMin string `json:"range_min"`
- RangeMax string `json:"range_max"`
- RangeValue string `json:"range_value"`
- RangeOptions string `json:"range_options"`
- Unit string `json:"unit"`
- State string `json:"state"`
- CTime string `json:"c_time"`
- UTime string `json:"u_time"`
- }
-
- func GetGzjhLis(sync_time string, his_user_id string) (str gzjhLisResult, checkStr string) {
- apiurl := "http://219.135.166.106:8067/WebService.asmx/queryjyjcxx"
- rep := httplib.Post(apiurl)
- rep.Param("sync_time", sync_time)
- rep.Param("access_token", "")
- rep.Param("his_user_id", his_user_id)
- // str, _ := rep.String()
- xmlKey := XmlKey{}
- rep.ToXML(&xmlKey)
-
- var checkJSON gzjhLisResult
- json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
- return checkJSON, xmlKey.InnerText
- }
-
- // 广州暨华透析中心Lis同步
- func SyncGzjhLis() (err error) {
- org_id := int64(10016)
-
- // 第一步:获取上一次同步的时间点
- syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
- var sync_time int64
- if syncLastInfo.ID > 0 {
- sync_time = syncLastInfo.SyncTime + 86400
- } else {
- sync_time = 1577808000
- }
- // 第二步:获取所有患者的病历号
-
- patientList, _ := GetPatientGzjhByOrgId(org_id)
- if len(patientList) > 0 {
- for _, patient := range patientList {
- if len(patient.AdmissionNumber) > 0 {
- // 上次同步时间转换为接口需要的格式
- patient_id := patient.ID
- sync_time_temp := time.Unix(sync_time, 0)
- syncTimeStr := sync_time_temp.Format("2006010215:04:05")
- gzjhLis, lisStr := GetGzjhLis(syncTimeStr, patient.IdCardNo)
- utils.InfoLog("yzstr:%v", lisStr)
-
- if len(gzjhLis.Result) > 0 {
- // timeLayout := "2006010215:04:05" // 转化所需模板
- // loc, _ := time.LoadLocation("Local") // 重要:获取时区
- for _, info := range gzjhLis.Result {
- // 根据project_id和item_id 判断该项是否存在
- project_id := int64(0)
- if len(info.ProjectId) > 0 {
- project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
- } else {
- continue
- }
-
- item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
-
- tx := writeMiddleDb.Begin()
- var inspection models.MiddleInspection
- var inspection_reference models.MiddleInspectionReference
- recordDateStr := info.InspectDate
-
- if len(info.InspectDate) == 0 {
- recordDateStr = time.Now().Format("2006-01-02 15:04")
- }
-
- record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
- record_date := record_date_str.Unix()
- inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
- // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
- // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
- var total int
- var RangeOptions string
- var RangeMin string
- var RangeMax string
-
- // // 判断检查类型
- ItemType, _ := strconv.Atoi(info.ItemType)
- Range := strings.Split(info.RangeValue, "~")
- if len(Range) > 1 {
- RangeMin = Range[0]
- RangeMax = Range[1]
- ItemType = 1
- } else {
- ItemType = 2
- RangeOptions = info.RangeOptions
- }
-
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project_id = ? and item_id = ? and status = 1", org_id, project_id, item_id).Find(&inspection_reference).Count(&total).Error
- if inspection_reference.ID > 0 {
- ItemType = inspection_reference.RangeType
- }
- if total <= 0 {
- inspection_reference.OrgId = org_id
- inspection_reference.ProjectName = info.ProjectName
- inspection_reference.Project = info.ProjectName
- inspection_reference.ProjectId = project_id
- inspection_reference.ItemName = info.ItemName
- inspection_reference.ItemNameAddition = info.ItemId
- inspection_reference.ItemId = item_id
- inspection_reference.RangeType = ItemType
- inspection_reference.RangeMin = RangeMin
- inspection_reference.RangeMax = RangeMax
- // inspection_reference.RangeValue = RangeValue
- inspection_reference.RangeOptions = RangeOptions
- inspection_reference.Unit = info.Unit
- inspection_reference.Status = 1
- inspection_reference.CreatedTime = time.Now().Unix()
- inspection_reference.UpdatedTime = time.Now().Unix()
- inspection_reference.InspectDate = inspect_date
- inspection_reference.UTime = inspect_date
- err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
- if err != nil {
- tx.Rollback()
- }
- }
-
- var itotal int
- err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and project_id = ? and item_id = ? and record_date = ? and patient_id = ? and status = 1", org_id, project_id, item_id, record_date, patient_id).Find(&inspection).Count(&itotal).Error
- if itotal <= 0 {
- info.ItemValue = strings.Replace(info.ItemValue, ">", ">", -1)
- info.ItemValue = strings.Replace(info.ItemValue, "<", "<", -1)
- inspection.PatientId = patient_id
- inspection.OrgId = org_id
- inspection.ProjectId = project_id
- inspection.ItemName = inspection_reference.ItemName
- inspection.ProjectName = inspection_reference.ProjectName
- inspection.InspectType = ItemType
- inspection.ItemId = item_id
- inspection.InspectValue = info.ItemValue
- inspection.InspectDate = inspect_date
- inspection.RecordDate = record_date
- // inspection.InspectTips = report.Resultstate
- inspection.Status = 1
- inspection.CreatedTime = time.Now().Unix()
- inspection.UpdatedTime = time.Now().Unix()
- inspection.UTime = inspect_date
- inspection.HisUserId = strconv.FormatInt(patient_id, 10)
- err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
- if err != nil {
- tx.Rollback()
- }
- }
-
- tx.Commit()
- }
- } else {
- continue
- }
- } else {
- continue
- }
- }
- }
- var syncInfo models.MiddleSyncInfo
- syncInfo.OrgId = org_id
- syncInfo.SyncTime = time.Now().Unix()
- syncInfo.SyncResultType = 1
- syncInfo.SyncRsultRemark = "同步成功"
- syncInfo.SyncTotalNum = 0
- syncInfo.SyncSuccessNum = 0
- syncInfo.SyncInfo = ""
- syncInfo.CreateTime = time.Now().Unix()
- syncInfo.UpdateTime = time.Now().Unix()
-
- cwderr := CreateSyncInfo(&syncInfo)
- if cwderr != nil {
- utils.ErrorLog("创建同步信息失败:%v", cwderr)
- return
- }
- SyncToGzjhtx()
- return
- }
-
- func SyncToGzjhtx() {
- utils.TraceLog("检验检查同步任务开始执行")
- org_id := int64(10016)
-
- // 第一步:跟进org_id 去中间库查出需要同步的数据
- inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
- inspections, _ := GetSyncInspectionByOrgId(org_id)
-
- // 第二步:将数据同步到业务库
- if len(inspection_references) > 0 {
- for _, inspection_reference := range inspection_references {
- SyncInspectionReference(&inspection_reference)
- }
- }
-
- if len(inspections) > 0 {
- for _, inspection := range inspections {
- SyncInspection(&inspection)
- }
- }
- utils.SuccessLog("检验检查同步任务完成")
- }
-
- type hbdyyz struct {
- HisUserId string `json:"his_user_id"`
- UserName string `json:"user_name"`
- DoctorId int64 `json:"doctor_id"`
- DoctorName string `json:"Doctor_name"`
- AdviceId int64 `json:"advice_id"`
- MedListCodg string `json:"med_list_codg"`
- StartTime int64 `json:"start_time"`
- AdviceName string `json:"advice_name"`
- AdviceDesc string `json:"advice_desc"`
- SingleDose string `json:"single_dose"`
- SingleDoseUnit string `json:"single_dose_unit"`
- PrescribingNumber string `json:"prescribing_number"`
- PrescribingNumberUnit string `json:"prescribing_number_unit"`
- DeliveryWay string `json:"delivery_way"`
- ExecutionFrequency string `json:"execution_frequency"`
- State int64 `json:"state"`
- CTime int64 `json:"create_time"`
- UTime int64 `json:"update_time"`
- }
-
- type hbdyHisResult struct {
- ErrorCode int64 `json:"error_code"`
- Result []hbdyyz `json:"data"`
- }
-
- func GetHbdyYz(sync_time string, his_user_id string) (str hbdyHisResult, checkStr string) {
- apiurl := "http://218.200.78.144:30800/esb-scheduler/api/XT_GET_ADVICE"
- rep := httplib.Post(apiurl)
- rep.Param("sync_time", sync_time)
- rep.Param("his_user_id", his_user_id)
- strs, err := rep.String()
- if err != nil {
- utils.InfoLog("err:%v", err)
- }
- utils.InfoLog("str:%v", strs)
-
- var checkJSON hbdyHisResult
- json.Unmarshal([]byte(strs), &checkJSON)
- utils.InfoLog("checkJSON:%v", checkJSON)
- return checkJSON, strs
- }
-
- // 湖北监利大垸透析中心医嘱同步
- func SyncHbJldyyz() (err error) {
- // 第一步:获取上次同步时间
- org_id := int64(10101)
-
- redisClient := RedisClient()
- defer redisClient.Close()
- sync_time, _ := redisClient.Get(redisClient.Context(),"gdjh_sync_time1").Result()
- if len(sync_time) == 0 {
- sync_time = strconv.FormatInt(time.Now().Unix(), 10)
- utils.InfoLog("sync_time:%v", sync_time)
- // formatTimeStr := "2021-08-31 00:00"
- // checkDateUnix, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", sync_time)
- // sync_time = strconv.FormatInt(checkDateUnix.Unix(),10)
- }
- new_sync_time := time.Now().Unix()
- utils.InfoLog("new_sync_time:%v", new_sync_time)
- // 切换为当前时间
- redisClient.Set(redisClient.Context(),"gdjh_sync_time", new_sync_time, time.Minute*60*24)
- // 第二步: 获取当天排班患者的住院号
- schedulesDateStr := time.Now().Format("2006-01-02")
- schedulesDate, _ := utils.ParseTimeStringToTime("2006-01-02", schedulesDateStr)
- schedulesTime := schedulesDate.Unix()
- patient_zy_ids, _ := redisClient.Get(redisClient.Context(),"gdjh_patient_zy_ids").Result()
- if len(patient_zy_ids) == 0 {
- schedules, _ := GetPatientGzjhList(org_id, schedulesTime)
- if len(schedules) > 0 {
- for _, item := range schedules {
- if len(patient_zy_ids) == 0 {
- patient_zy_ids = item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10)
- } else {
- patient_zy_ids = patient_zy_ids + "," + item.Patients.IdCardNo + "-" + strconv.FormatInt(item.Patients.ID, 10)
- }
- }
- redisClient.Set(redisClient.Context(),"gdjh_patient_zy_ids", patient_zy_ids, time.Minute*60*2)
- }
- }
- utils.InfoLog("2gdjh_patient_zy_ids:%v", patient_zy_ids)
-
- // 第三步: 根据获取的同步时间,同步人员,去获取医嘱信息
- idArray := strings.Split(patient_zy_ids, ",")
- for _, idStr := range idArray {
- temp_id := strings.Split(idStr, "-")
- id := temp_id[0]
- patient_id := temp_id[1]
- hbdyyz, _ := GetHbdyYz(sync_time, id)
- if len(hbdyyz.Result) > 0 {
- for _, yz := range hbdyyz.Result {
- utils.InfoLog("yz:%v", yz)
- // 根据姓名获取医生ID
- var doctor_id int64
- temp_doctor_id, _ := redisClient.Get(redisClient.Context(),"gdjh_doctor_id_" + strconv.FormatInt(yz.DoctorId, 10)).Result()
- if len(temp_doctor_id) == 0 {
- doctor, _ := GetAdminUserId(org_id, yz.DoctorName)
- doctor_id = doctor.AdminUserId
- if doctor_id > 0 {
- redisClient.Set(redisClient.Context(),"gdjh_doctor_id_"+strconv.FormatInt(yz.DoctorId, 10), doctor_id, time.Minute*60*2)
- } else {
- continue
- }
- } else {
- doctor_id, _ = strconv.ParseInt(temp_doctor_id, 10, 64)
- }
- temp_patient_id, _ := strconv.ParseInt(patient_id, 10, 64)
- SingleDose, _ := strconv.ParseFloat(yz.SingleDose, 64)
- PrescribingNumber, _ := strconv.ParseFloat(yz.PrescribingNumber, 64)
- utils.InfoLog("PrescribingNumber:%v", PrescribingNumber)
- advice := models.XtDoctorAdvice{
- UserOrgId: org_id,
- PatientId: temp_patient_id,
- AdviceType: 2,
- AdviceDate: schedulesTime,
- StartTime: yz.StartTime,
- AdviceName: yz.AdviceName,
- AdviceDesc: yz.AdviceDesc,
- RecordDate: schedulesTime,
- SingleDose: SingleDose,
- SingleDoseUnit: yz.SingleDoseUnit,
- PrescribingNumber: PrescribingNumber,
- PrescribingNumberUnit: yz.PrescribingNumberUnit,
- DeliveryWay: yz.DeliveryWay,
- ExecutionFrequency: yz.ExecutionFrequency,
- AdviceDoctor: doctor_id,
- CreatedTime: time.Now().Unix(),
- UpdatedTime: time.Now().Unix(),
- SyncAdviceId: yz.AdviceId,
- ExecutionState: 2,
- Status: 1,
- IsSync: 1,
- }
- utils.InfoLog("advice:%v", advice)
- // 根据同步来的 advice_id 来查询库里是否已经存在
- advice_info, _ := GetAdviceBySyncAdviceId(org_id, yz.AdviceId)
- utils.InfoLog("advice_info:%v", advice_info)
- if advice_info.ID > 0 {
- continue
- } else {
- if yz.State == 1 {
- handleerr := CreateDoctorAdviceHandle(&advice)
- if handleerr != nil {
- utils.ErrorLog("添加医嘱信息失败:%v", handleerr)
- continue
- }
- } else {
- continue
- }
- }
- }
- }
- }
- return
- }
-
- type hbdylis struct {
- HisUserId string `json:"his_user_id"`
- PatientName string `json:"patient_name"`
- ProjectId int64 `json:"project_id"`
- ProjectName string `json:"project_name"`
- InspectDate int64 `json:"inspect_date"`
- ItemId int64 `json:"item_id"`
- ItemName string `json:"item_name"`
- ItemType int64 `json:"item_type"`
- ItemValue string `json:"item_value"`
- RangeMin string `json:"range_min"`
- RangeMax string `json:"range_max"`
- RangeValue string `json:"range_value"`
- RangeOptions string `json:"range_options"`
- Unit string `json:"unit"`
- State int64 `json:"state"`
- CTime int64 `json:"c_time"`
- UTime int64 `json:"u_time"`
- }
-
- type hbdyLisResult struct {
- ErrorCode int64 `json:"error_code"`
- Result []hbdylis `json:"data"`
- }
-
- func GethbdyLis(sync_time string, his_user_id string) (str hbdyLisResult, checkStr string) {
- apiurl := "http://218.200.78.144:30800/esb-scheduler/api/XT_GET_INSPECTION"
- rep := httplib.Post(apiurl)
- rep.Param("sync_time", sync_time)
- rep.Param("his_user_id", his_user_id)
- strs, err := rep.String()
- if err != nil {
- utils.InfoLog("err:%v", err)
- }
- utils.InfoLog("str:%v", strs)
-
- var checkJSON hbdyLisResult
- json.Unmarshal([]byte(strs), &checkJSON)
- // utils.InfoLog("checkJSON:%v", checkJSON)
- return checkJSON, strs
- }
-
- // 湖北监利大院透析中心Lis同步
- func SyncHbdyLis() (err error) {
- org_id := int64(10101)
-
- // 第一步:获取上一次同步的时间点
- syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
- var sync_time int64
- if syncLastInfo.ID > 0 {
- sync_time = syncLastInfo.SyncTime
- } else {
- sync_time = 1627747200
- }
- // 第二步:获取所有患者的病历号
-
- patientList, _ := GetPatientGzjhByOrgId(org_id)
- if len(patientList) > 0 {
- for _, patient := range patientList {
- if len(patient.IdCardNo) > 0 || len(patient.DialysisNo) > 0 {
- // 上次同步时间转换为接口需要的格式
- patient_id := patient.ID
- // sync_time_temp := time.Unix(sync_time,0)
- // syncTimeStr := sync_time_temp.Format("2006010215:04:05")
- utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
- // 获取会员号
-
- // utils.InfoLog("VipStr:%v", VipStr)
-
- gzjhLis, _ := GethbdyLis(strconv.FormatInt(sync_time, 10), patient.IdCardNo)
- // if len(gzjhLis.Result) == 0 {
- // gzjhLis,_ = GethbdyLis(strconv.FormatInt(sync_time,10),VipStr)
- // }
- // utils.InfoLog("yzstr:%v", gzjhLis)
-
- if len(gzjhLis.Result) > 0 {
- // timeLayout := "2006010215:04:05" // 转化所需模板
- // loc, _ := time.LoadLocation("Local") // 重要:获取时区
- for _, info := range gzjhLis.Result {
- utils.InfoLog("Result:%v", info)
- // 根据project_id和item_id 判断该项是否存在
- project_id := int64(0)
- if info.ProjectId > 0 {
- project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
- } else {
- continue
- }
-
- item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
-
- tx := writeMiddleDb.Begin()
- var inspection models.MiddleInspection
- var inspection_reference models.MiddleInspectionReference
-
- record_date := info.InspectDate
-
- if info.InspectDate == 0 {
- record_date = time.Now().Unix()
- }
-
- // record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
- // record_date := record_date_str.Unix()
- inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
- // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
- // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
- var total int
- var RangeOptions string
- var RangeMin string
- var RangeMax string
-
- // // 判断检查类型
- ItemType := info.ItemType
- Range := strings.Split(info.RangeValue, "-")
- if len(Range) > 1 {
- RangeMin = Range[0]
- RangeMax = Range[1]
- ItemType = 1
- } else {
- ItemType = 2
- RangeOptions = info.RangeOptions
- }
-
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project_id = ? and item_id = ? and status = 1", org_id, project_id, item_id).Find(&inspection_reference).Count(&total).Error
- // if inspection_reference.ID > 0 {
- // ItemType = int64(inspection_reference.RangeType)
- // }
- if total <= 0 {
- inspection_reference.OrgId = org_id
- inspection_reference.ProjectName = info.ProjectName
- inspection_reference.Project = info.ProjectName
- inspection_reference.ProjectId = project_id
- inspection_reference.ItemName = info.ItemName
- inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
- inspection_reference.ItemId = item_id
- inspection_reference.RangeType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
- inspection_reference.RangeMin = RangeMin
- inspection_reference.RangeMax = RangeMax
- // inspection_reference.RangeValue = RangeValue
- inspection_reference.RangeOptions = RangeOptions
- inspection_reference.Unit = info.Unit
- inspection_reference.Status = 1
- inspection_reference.CreatedTime = time.Now().Unix()
- inspection_reference.UpdatedTime = time.Now().Unix()
- inspection_reference.InspectDate = inspect_date
- inspection_reference.UTime = inspect_date
- err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
- if err != nil {
- tx.Rollback()
- }
- }
-
- var itotal int
- err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and project_id = ? and item_id = ? and record_date = ? and patient_id = ? and status = 1", org_id, project_id, item_id, record_date, patient_id).Find(&inspection).Count(&itotal).Error
- if itotal <= 0 {
- info.ItemValue = strings.Replace(info.ItemValue, ">", ">", -1)
- info.ItemValue = strings.Replace(info.ItemValue, "<", "<", -1)
- inspection.PatientId = patient_id
- inspection.OrgId = org_id
- inspection.ProjectId = project_id
- inspection.ItemName = inspection_reference.ItemName
- inspection.ProjectName = inspection_reference.ProjectName
- inspection.InspectType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
- inspection.ItemId = item_id
- inspection.InspectValue = info.ItemValue
- inspection.InspectDate = inspect_date
- inspection.RecordDate = record_date
- // inspection.InspectTips = report.Resultstate
- inspection.Status = 1
- inspection.CreatedTime = time.Now().Unix()
- inspection.UpdatedTime = time.Now().Unix()
- inspection.UTime = inspect_date
- inspection.HisUserId = strconv.FormatInt(patient_id, 10)
- err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
- if err != nil {
- tx.Rollback()
- }
- }
-
- tx.Commit()
- }
- }
-
- VipStr := ""
- VipArr := strings.Split(patient.DialysisNo, "(")
- if len(VipArr) > 1 {
- VipArrTwo := strings.Split(VipArr[1], ")")
- VipStr = VipArrTwo[0]
-
- gzjhLisbyVip, _ := GethbdyLis(strconv.FormatInt(sync_time, 10), VipStr)
- if len(gzjhLisbyVip.Result) > 0 {
- // timeLayout := "2006010215:04:05" // 转化所需模板
- // loc, _ := time.LoadLocation("Local") // 重要:获取时区
- for _, info := range gzjhLisbyVip.Result {
- // utils.InfoLog("Result:%v", info)
- // 根据project_id和item_id 判断该项是否存在
- project_id := int64(0)
- if info.ProjectId > 0 {
- project_id, _ = GetGzjhProjectID(org_id, info.ProjectName)
- } else {
- continue
- }
-
- item_id, _ := GetItemID(org_id, info.ProjectName, info.ItemName, project_id)
-
- tx := writeMiddleDb.Begin()
- var inspection models.MiddleInspection
- var inspection_reference models.MiddleInspectionReference
-
- record_date := info.InspectDate
-
- if info.InspectDate == 0 {
- record_date = time.Now().Unix()
- }
-
- // record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
- // record_date := record_date_str.Unix()
- inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
- // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
- // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
- var total int
- var RangeOptions string
- var RangeMin string
- var RangeMax string
-
- // // 判断检查类型
- ItemType := info.ItemType
- Range := strings.Split(info.RangeValue, "-")
- if len(Range) > 1 {
- RangeMin = Range[0]
- RangeMax = Range[1]
- ItemType = 1
- } else {
- ItemType = 2
- RangeOptions = info.RangeOptions
- }
-
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project_id = ? and item_id = ? and status = 1", org_id, project_id, item_id).Find(&inspection_reference).Count(&total).Error
- if inspection_reference.ID > 0 {
- ItemType = int64(inspection_reference.RangeType)
- }
- if total <= 0 {
- inspection_reference.OrgId = org_id
- inspection_reference.ProjectName = info.ProjectName
- inspection_reference.Project = info.ProjectName
- inspection_reference.ProjectId = project_id
- inspection_reference.ItemName = info.ItemName
- inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
- inspection_reference.ItemId = item_id
- inspection_reference.RangeType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
- inspection_reference.RangeMin = RangeMin
- inspection_reference.RangeMax = RangeMax
- // inspection_reference.RangeValue = RangeValue
- inspection_reference.RangeOptions = RangeOptions
- inspection_reference.Unit = info.Unit
- inspection_reference.Status = 1
- inspection_reference.CreatedTime = time.Now().Unix()
- inspection_reference.UpdatedTime = time.Now().Unix()
- inspection_reference.InspectDate = inspect_date
- inspection_reference.UTime = inspect_date
- err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
- if err != nil {
- tx.Rollback()
- }
- }
-
- var itotal int
- err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and project_id = ? and item_id = ? and record_date = ? and patient_id = ? and status = 1", org_id, project_id, item_id, record_date, patient_id).Find(&inspection).Count(&itotal).Error
- if itotal <= 0 {
- info.ItemValue = strings.Replace(info.ItemValue, ">", ">", -1)
- info.ItemValue = strings.Replace(info.ItemValue, "<", "<", -1)
- inspection.PatientId = patient_id
- inspection.OrgId = org_id
- inspection.ProjectId = project_id
- inspection.ItemName = inspection_reference.ItemName
- inspection.ProjectName = inspection_reference.ProjectName
- inspection.InspectType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
- inspection.ItemId = item_id
- inspection.InspectValue = info.ItemValue
- inspection.InspectDate = inspect_date
- inspection.RecordDate = record_date
- // inspection.InspectTips = report.Resultstate
- inspection.Status = 1
- inspection.CreatedTime = time.Now().Unix()
- inspection.UpdatedTime = time.Now().Unix()
- inspection.UTime = inspect_date
- inspection.HisUserId = strconv.FormatInt(patient_id, 10)
- err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
- if err != nil {
- tx.Rollback()
- }
- }
-
- tx.Commit()
- }
- }
- }
- } else {
- continue
- }
- }
- }
- var syncInfo models.MiddleSyncInfo
- syncInfo.OrgId = org_id
- syncInfo.SyncTime = time.Now().Unix()
- syncInfo.SyncResultType = 1
- syncInfo.SyncRsultRemark = "同步成功"
- syncInfo.SyncTotalNum = 0
- syncInfo.SyncSuccessNum = 0
- syncInfo.SyncInfo = ""
- syncInfo.CreateTime = time.Now().Unix()
- syncInfo.UpdateTime = time.Now().Unix()
-
- cwderr := CreateSyncInfo(&syncInfo)
- if cwderr != nil {
- utils.ErrorLog("创建同步信息失败:%v", cwderr)
- return
- }
- SyncToHbdytx()
- return
- }
-
- func SyncToHbdytx() {
- utils.TraceLog("检验检查同步任务开始执行")
- org_id := int64(10101)
-
- // 第一步:跟进org_id 去中间库查出需要同步的数据
- inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
- inspections, _ := GetSyncInspectionByOrgId(org_id)
-
- // 第二步:将数据同步到业务库
- if len(inspection_references) > 0 {
- for _, inspection_reference := range inspection_references {
- SyncInspectionReference(&inspection_reference)
- }
- }
-
- if len(inspections) > 0 {
- for _, inspection := range inspections {
- SyncInspection(&inspection)
- }
- }
- utils.SuccessLog("检验检查同步任务完成")
- }
-
- // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
- func GetBljhProjectID(org_id int64, project_name string) (project_id int64, err error) {
- var inspection_reference models.MiddleInspectionReference
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
- if inspection_reference.ID > 0 {
- return inspection_reference.ProjectId, err
- } else {
- err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
- if inspection_reference.ProjectId > 0 {
- return inspection_reference.ProjectId + 1, err
- } else {
- return 5210138, err
- }
- }
- }
-
- // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
- func GetBlItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
- var inspection_reference models.MiddleInspectionReference
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ? and item_name = ?", org_id, project_name, item_name).First(&inspection_reference).Error
- if inspection_reference.ID > 0 {
- return inspection_reference.ItemId, err
- } else {
- err := readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id = ? and project_id = ? ", org_id, project_id).Select("max(item_id) as item_id").First(&inspection_reference).Error
- utils.InfoLog("inspection_reference: %v", inspection_reference)
- if inspection_reference.ItemId > 0 {
- return inspection_reference.ItemId + 1, err
- } else {
- return project_id*10 + 1, err
- }
- }
- }
-
- // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
- func GetShjhProjectID(org_id int64, project_name string) (project_id int64, err error) {
- var inspection_reference models.MiddleInspectionReference
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
- if inspection_reference.ID > 0 {
- return inspection_reference.ProjectId, err
- } else {
- err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
- if inspection_reference.ProjectId > 0 {
- return inspection_reference.ProjectId + 1, err
- } else {
- return 5210215, err
- }
- }
- }
-
- // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
- func GetShItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
- var inspection_reference models.MiddleInspectionReference
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ? and item_name = ?", org_id, project_name, item_name).First(&inspection_reference).Error
- if inspection_reference.ID > 0 {
- return inspection_reference.ItemId, err
- } else {
- err := readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id = ? and project_id = ? ", org_id, project_id).Select("max(item_id) as item_id").First(&inspection_reference).Error
- utils.InfoLog("inspection_reference: %v", inspection_reference)
- if inspection_reference.ItemId > 0 {
- return inspection_reference.ItemId + 1, err
- } else {
- return project_id*10 + 1, err
- }
- }
- }
|