123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- package service
-
- import (
- "IC/models"
- "IC/utils"
- _ "IC/utils"
- "encoding/json"
- _ "encoding/json"
- _ "fmt"
- "github.com/astaxie/beego/httplib"
- _ "github.com/jinzhu/gorm"
- "net/url"
- _ "strconv"
- "strings"
- "time"
- )
-
- // 龙岗二院Lis同步
- func SyncLisLgty() (err error) {
- // 第一步:获取龙岗二院所有病人
- org_id := int64(9598)
- patientlist, patientError := GetUserInfoByOrgID(org_id)
- if patientError != nil {
- utils.ErrorLog("获取龙岗二院患者信息失败:%v", patientError)
- return
- }
-
- if len(patientlist) > 0 {
- // 第二步:根据病人的住院号来查询上次同步时间
- for _, patient := range patientlist {
- //if len(patient.AdmissionNumber) <= 0 {
- // continue
- //}
- syncLastInfo, _ := GetSyncTimeByOrgID(org_id, patient.IdCardNo)
- var sync_time int64
- if syncLastInfo.ID > 0 {
- sync_time = syncLastInfo.SyncTime
- } else {
- inspectionLastInfo, _ := GetInspectionLastInfoByOrgID(org_id, patient.IdCardNo)
- if inspectionLastInfo.ID > 0 {
- sync_time = inspectionLastInfo.CreatedTime
- } else {
- sync_time = 1640966400
- }
- }
-
- syncTimes := time.Unix(sync_time, 0).Format("2006-01-02 15:04")
- utils.InfoLog("syncTimes:%v", syncTimes)
- nowTimes := time.Now().Format("2006-01-02 15:04")
- // 第三步:跟进上次同步时间和指定的门诊号,获取上次同步到现在指定人的检验检查记录
- lisHead, HeadString := GetHead(patient.IdCardNo, syncTimes, nowTimes)
- var resultCount int64
- // 第四步,将同步过来的头部信息存到中间库的检验检查操作表中
- if len(lisHead) > 0 {
- resultCount = 0
- for _, head := range lisHead {
- // if len(head.Item) <= 0 {
- // continue
- // }
- if len(head.ReportId) > 1 {
- if len(head.Item) == 0 {
- head.Item = "肾功三项"
- }
- middleReportHandle := models.MiddleReportHandle{
- OrgId: org_id,
- HisUserId: patient.AdmissionNumber,
- ApplyNo: head.ApplyNo,
- ReportId: head.ReportId,
- Name: head.Name,
- Sex: head.Sex,
- Age: head.Age,
- Dept: head.Dept,
- PatientId: patient.IdCardNo,
- RequestTime: head.RequestTime,
- ExecuteTime: head.ExecuteTime,
- ReceiveTime: head.ReceiveTime,
- Item: head.Item,
- CheckTime: head.CheckTime,
- PatientType: head.PatientType,
- CreatedTime: time.Now().Unix(),
- UpdatedTime: time.Now().Unix(),
- }
- handleerr := CreateReportHandle(&middleReportHandle)
- if handleerr != nil {
- utils.ErrorLog("创建检验检查操作数据失败:%v", handleerr)
- return
- }
- // // reportInfos,ReportString := GetReport(head.ReportId)
- // HeadString = HeadString + " " + ReportString
- // // 获取该项的检验检查project_id,如果没有,则创建一个
- // project_id,_ := GetProjectID(org_id,head.Item)
- // if len(reportInfos) > 2 {
- // // 将同步过来的检验检查数据存到中间库
- // for _,reportInfo := range reportInfos{
- // if len(reportInfo.ItemName) <= 0 {
- // continue
- // }
- // // 获取该小项的检验检查item_id,如果没有,则创建一个
- // item_id,_ := GetItemID(org_id,head.Item,reportInfo.ItemName,project_id)
- // _,_,syerr := LgthSync(head,reportInfo,project_id,item_id,org_id,patient.ID,patient.AdmissionNumber)
- // if syerr == nil {
- // resultCount = resultCount + 1
- // }
- // }
- // }
- }
- }
- var syncInfo models.MiddleSyncInfo
- syncInfo.OrgId = org_id
- syncInfo.HisUserId = patient.AdmissionNumber
- syncInfo.SyncTime = time.Now().Unix()
- syncInfo.SyncResultType = 1
- syncInfo.SyncRsultRemark = "同步成功"
- syncInfo.SyncTotalNum = resultCount
- syncInfo.SyncSuccessNum = 0
- syncInfo.SyncInfo = HeadString
- syncInfo.CreateTime = time.Now().Unix()
- syncInfo.UpdateTime = time.Now().Unix()
-
- cwderr := CreateSyncInfo(&syncInfo)
- if cwderr != nil {
- utils.ErrorLog("创建同步信息失败:%v", cwderr)
- return
- }
- } else {
- // 未同步到检验检查头部信息
- var syncInfo models.MiddleSyncInfo
- syncInfo.OrgId = org_id
- syncInfo.HisUserId = patient.AdmissionNumber
- syncInfo.SyncTime = time.Now().Unix()
- syncInfo.SyncResultType = 1
- syncInfo.SyncRsultRemark = "该患者这个时间段没有任何检验检查数据"
- syncInfo.CreateTime = time.Now().Unix()
- syncInfo.UpdateTime = time.Now().Unix()
-
- serr := CreateSyncInfo(&syncInfo)
- if serr != nil {
- utils.ErrorLog("创建同步信息失败:%v", serr)
- return
- }
- continue
- }
-
- }
- }
- // 第五步,查询检验检查操作表里状态为0的数据,并将该对应报告的检验检查详情数据请求回来并存入该条数据里的report_info字段
- ReportInfoHandle(org_id)
- return
- }
-
- func ReportInfoHandle(org_id int64) {
- // 1.从检验检查表中查询操作状态为0的数据
- report_handle_list, _ := GetReportHandleListyOrgID(org_id)
- if len(report_handle_list) > 0 {
- for _, report_handle := range report_handle_list {
- // 2. 根据report_id获取报告详情并进行相关处理
- reportInfos, ReportString := GetReport(report_handle.ReportId)
- if len(reportInfos) > 0 {
- report_info_item_num := len(reportInfos)
- // 将检验检查详情,检验检查小项条数更新到操作表数据中
- middleReportHandle := models.MiddleReportHandle{
- ID: report_handle.ID,
- ReportInfo: ReportString,
- ReportInfoItemNum: int64(report_info_item_num),
- UpdatedTime: time.Now().Unix(),
- }
- err := UpdateReportHandleListyOrgID(&middleReportHandle)
- if err != nil {
- utils.ErrorLog("创建同步信息失败:%v", err)
- //return
- }
- report_handle.ReportInfoItemNum = int64(report_info_item_num)
- project_id, _ := GetProjectID(org_id, report_handle.Item)
- patient, perr := GetUserInfoByAdmission(report_handle.PatientId, org_id)
- if perr != nil {
- utils.ErrorLog("查询病人信息失败:%v", perr)
- //return
- }
- if len(reportInfos) > 0 {
- // 将同步过来的检验检查数据存到中间库
- for _, reportInfo := range reportInfos {
- if len(reportInfo.ItemName) <= 0 {
- continue
- }
- // 获取该小项的检验检查item_id,如果没有,则创建一个
- item_id, _ := GetItemID(org_id, report_handle.Item, reportInfo.ItemName, project_id)
- _, _, syerr := LgthSync(&report_handle, reportInfo, project_id, item_id, org_id, patient.ID, patient.AdmissionNumber)
- if syerr != nil {
- utils.ErrorLog("创建同步信息失败:%v", syerr)
- return
- }
- }
- }
- }
- }
- }
- SyncToLgey()
- }
-
- func SyncToLgey() {
- utils.TraceLog("检验检查同步任务开始执行")
- org_id := int64(9598)
-
- // 第一步:跟进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("检验检查同步任务完成")
- }
-
- // 从机构将数据同步到中间库
- func LgthSync(Head *models.MiddleReportHandle, report ReportInfo, project_id int64, item_id int64, org_id int64, patient_id int64, his_user_id string) (*models.MiddleInspectionReference, *models.MiddleInspection, error) {
- tx := writeMiddleDb.Begin()
- var inspection models.MiddleInspection
- var inspection_reference models.MiddleInspectionReference
- recordDateStr := Head.CheckTime
- utils.InfoLog("打印测试: %v", recordDateStr)
-
- if len(Head.CheckTime) == 0 {
- recordDateStr = time.Now().Format("2006-01-02 15:04")
- } else {
- //dateTempArrOne := strings.Split(recordDateStr, "T")
- dateTempArrTwo := strings.Split(recordDateStr, ".")
- recordDateStr = dateTempArrTwo[0]
- }
-
- 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 itotal int
- var RangeOptions string
- var RangeMin string
- var RangeMax string
-
- // // 判断检查类型
- ItemType := 1
- Range := strings.Split(report.Reference, "~")
- if len(Range) > 1 {
- RangeMin = Range[0]
- RangeMax = Range[1]
- ItemType = 1
- } else {
- ItemType = 2
- RangeOptions = report.Reference
- }
-
- 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 && len(Head.Item) > 0 {
- inspection_reference.OrgId = org_id
- inspection_reference.ProjectName = Head.Item
- inspection_reference.Project = Head.Item
- inspection_reference.ProjectId = project_id
- inspection_reference.ItemName = report.ItemName
- 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.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()
- }
- }
-
- 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 {
- inspection.PatientId = patient_id
- inspection.OrgId = org_id
- inspection.ProjectId = project_id
- inspection.ItemName = report.ItemName
- inspection.ProjectName = Head.Item
- inspection.InspectType = ItemType
- inspection.ItemId = item_id
- inspection.InspectValue = report.Result
- inspection.InspectDate = inspect_date
- inspection.RecordDate = record_date
- inspection.Status = 1
- inspection.CreatedTime = time.Now().Unix()
- inspection.UpdatedTime = time.Now().Unix()
- inspection.UTime = inspect_date
- inspection.HisUserId = his_user_id
- 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, nil, err
- }
-
- // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
- func GetItemID(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*1000 + 1, err
- }
- }
- }
-
- // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
- func GetProjectID(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 18001, err
- }
- }
- }
-
- // 根据机构ID获取该机构所有病人的admission_number (住院号)
- func GetUserInfoByOrgID(org_id int64) (patients []models.Patients, err error) {
- err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1", org_id).Find(&patients).Error
- return
- }
-
- // 根据机构ID获取该机构所有病人的admission_number (住院号)
- func GetUserInfoByAdmission(admission_number string, org_id int64) (patients models.Patients, err error) {
- err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and id_card_no = ? and status = 1", org_id, admission_number).First(&patients).Error
- return
- }
-
- type HeadInfo struct {
- ApplyNo string `json:"applyNo"`
- ReportId string `json:"reportId"`
- Name string `json:"name"`
- Sex string `json:"sex"`
- Age string `json:"age"`
- Dept string `json:"dept"`
- BedNo string `json:"bedNo"`
- PatientId string `json:"patientId"`
- PatientPid string `json:"patientPid"`
- SampleNo float64 `json:"sampleNo"`
- RequestTime string `json:"requestTime"`
- ExecuteTime string `json:"executeTime"`
- ReceiveTime string `json:"receiveTime"`
- Item string `json:"item"`
- CheckTime string `json:"checkTime"`
- PatientType string `json:"patientType"`
- RequestTimes string `json:"request_time"`
- }
-
- type ReportInfo struct {
- ReportId string `json:"reportId"`
- ItemCode string `json:"itemCode"`
- ItemName string `json:"itemName"`
- StdCode string `json:"stdCode"`
- Result string `json:"result"`
- Unit string `json:"unit"`
- SampleNo float64 `json:"sampleNo"`
- Reference string `json:"reference"`
- Prompt string `json:"prompt"`
- }
-
- func GetHead(patient_id string, start_time string, end_time string) (head []HeadInfo, str string) {
- Domain := "http://www.szlg2h.cn:9991/xuetoushi/head/getHead?"
- apiurl := "identity_Card=" + patient_id + "&check_time_start=" + start_time + "&check_time_end=" + end_time
- apiurl = url.PathEscape(apiurl)
- apiurl = Domain + apiurl
- utils.InfoLog("url: %v", apiurl)
- var respJSON []HeadInfo
- httplib.Get(apiurl).ToJSON(&respJSON)
- jsonStr, _ := json.Marshal(respJSON)
- utils.InfoLog("请求患者检查头部数据接口返回数据: %v", string(jsonStr))
- return respJSON, string(jsonStr)
- }
-
- func GetReport(report_id string) (head []ReportInfo, str string) {
- Domain := "http://www.szlg2h.cn:9991/xuetoushi/detail/getDetail?"
- apiurl := "report_id=" + report_id
- apiurl = url.PathEscape(apiurl)
- apiurl = Domain + apiurl
-
- var respJSON []ReportInfo
- httplib.Get(apiurl).ToJSON(&respJSON)
- jsonStr, _ := json.Marshal(respJSON)
- utils.InfoLog("请求患者检查详情数据接口返回数据: %v", string(jsonStr))
- return respJSON, string(jsonStr)
- }
|