123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- package service
-
- import (
- "IC/models"
- "IC/utils"
- _ "IC/utils"
- "bytes"
- "encoding/json"
- _ "encoding/json"
- _ "encoding/xml"
- "fmt"
- _ "github.com/jinzhu/gorm"
- "io/ioutil"
- "net/http"
-
- "strconv"
- "strings"
- "time"
- )
-
- // 福建天福医院透析中心Lis同步
- func SyncFjtfLis() (err error) {
- org_id := int64(10330)
-
- // 第一步:获取上一次同步的时间点
- syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
- var sync_time int64
- if syncLastInfo.ID > 0 {
- sync_time = syncLastInfo.SyncTime
- } else {
- sync_time = 1651334400 // 2022-05-01 00:00:00
- }
-
- sync_time_temp := time.Unix(sync_time, 0)
- syncTimeStr := sync_time_temp.Format("2006-01-02 15:04:05")
- // 第二步:获取所有患者的病历号
-
- 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
- utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
- // utils.InfoLog("VipStr:%v", VipStr)
-
- result, _ := FjtjGetLab(patient.IdCardNo, syncTimeStr)
- var fjtfLab LabResult
- if err := json.Unmarshal([]byte(result), &fjtfLab); err != nil {
- utils.ErrorLog("解析失败:%v", err)
- }
- fmt.Println(fjtfLab)
-
- if len(fjtfLab.Result) > 0 {
- // 根据获取的头部lab01获取检查结果
-
- for _, info := range fjtfLab.Result {
- lacResult, _ := FjtjGetLac(info.Lab01)
- var fjtfLac LacResult
- if err := json.Unmarshal([]byte(lacResult), &fjtfLac); err != nil {
- utils.ErrorLog("解析失败:%v", err)
- }
- fmt.Println(fjtfLac)
- for _, cinfo := range fjtfLac.Result {
- project_id := int64(0)
- item_id := int64(0)
- if len(cinfo.Bbx05a) > 0 {
- project_id, _ = GetfjtfProjectID(org_id, cinfo.Bbx05a)
- item_id, _ = GetFjtfItemID(org_id, cinfo.Bbx05a, cinfo.Bbx05, project_id)
- } else {
- project_id, _ = GetfjtfProjectID(org_id, cinfo.Bbx05)
- item_id, _ = GetFjtfItemID(org_id, cinfo.Bbx05, cinfo.Bbx05, project_id)
- }
-
- tx := writeMiddleDb.Begin()
- var inspection models.MiddleInspection
- var inspection_reference models.MiddleInspectionReference
-
- loc, _ := time.LoadLocation("Local")
- theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", cinfo.Lac25, loc)
- record_date := theTime.Unix()
-
- if record_date == 0 {
- record_date = time.Now().Unix()
- }
-
- inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
- var total int
- var RangeOptions string
- var RangeMin string
- var RangeMax string
-
- // // 判断检查类型
- ItemType := 1
- Range := strings.Split(cinfo.Lac15, "-")
- if len(Range) > 1 {
- RangeMin = cinfo.Lac13
- RangeMax = cinfo.Lac14
- ItemType = 1
- } else {
- ItemType = 2
- RangeOptions = cinfo.Lac15
- }
-
- 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
- if len(cinfo.Bbx05a) > 0 {
- inspection_reference.ProjectName = cinfo.Bbx05a
- inspection_reference.Project = cinfo.Bbx05a
- } else {
- inspection_reference.ProjectName = cinfo.Bbx05
- inspection_reference.Project = cinfo.Bbx05
- }
- inspection_reference.ProjectId = project_id
- inspection_reference.ItemName = cinfo.Bbx05
- 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 = cinfo.Bdg02
- 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 {
- 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 = cinfo.Lac10
- inspection.InspectDate = inspect_date
- inspection.RecordDate = record_date
- inspection.InspectTips = cinfo.Lac11
- 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
- }
- SyncToFjtftx()
- return
- }
-
- type LabResult struct {
- Msg string `json:"msg"`
- Result []struct {
- Lab01 int64 `json:"lab01"`
- Lab58 string `json:"lab58"`
- } `json:"result"`
- Code string `json:"code"`
- }
-
- func FjtjGetLab(id_card_no string, synctime string) (string, string) {
- inputData := make(map[string]interface{})
-
- inputData["vaa15"] = id_card_no
- inputData["begintime"] = synctime
-
- var inputLog string
- bytesData, err := json.Marshal(inputData)
- inputLog = string(bytesData)
- fmt.Println(string(bytesData))
- if err != nil {
- fmt.Println(err.Error())
- return err.Error(), ""
- }
- reader := bytes.NewReader(bytesData)
- url := "http://hip.zptfyy.com/esb/listener/getLab1"
-
- request, err := http.NewRequest("POST", url, reader)
- if err != nil {
- fmt.Println(err.Error())
- return err.Error(), ""
- }
-
- request.Header.Set("Content-Type", "application/json;charset=UTF-8")
- request.Header.Set("code", "xt")
-
- 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)
- return str, inputLog
- }
-
- type LacResult struct {
- Msg string `json:"msg"`
- Result []struct {
- Lab01 int64 `json:"labo1"`
- Bbx05 string `json:"BBX05"`
- Bbx05a string `json:"BBX05A"`
- Bfc03 string `json:"BFC03"`
- Bdg02 string `json:"BDG02"`
- Lac10 string `json:"LAC10"`
- Lac11 string `json:"LAC11"`
- Lac13 string `json:"LAC13"`
- Lac14 string `json:"LAC14"`
- Lac15 string `json:"LAC15"`
- Lac25 string `json:"LAC25"`
- } `json:"result"`
- Code string `json:"code"`
- }
-
- func FjtjGetLac(lab01 int64) (string, string) {
- inputData := make(map[string]interface{})
-
- inputData["lab01"] = lab01
-
- var inputLog string
- bytesData, err := json.Marshal(inputData)
- inputLog = string(bytesData)
- fmt.Println(string(bytesData))
- if err != nil {
- fmt.Println(err.Error())
- return err.Error(), ""
- }
- reader := bytes.NewReader(bytesData)
- url := "http://hip.zptfyy.com/esb/listener/getLac1"
-
- request, err := http.NewRequest("POST", url, reader)
- if err != nil {
- fmt.Println(err.Error())
- return err.Error(), ""
- }
-
- request.Header.Set("Content-Type", "application/json;charset=UTF-8")
- request.Header.Set("code", "xt")
-
- 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)
- return str, inputLog
- }
-
- // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
- func GetfjtfProjectID(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 330001, err
- }
- }
- }
-
- // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
- func GetFjtfItemID(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*100 + 1, err
- }
- }
- }
-
- func SyncToFjtftx() {
- utils.TraceLog("检验检查同步任务开始执行")
- org_id := int64(10330)
-
- // 第一步:跟进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("检验检查同步任务完成")
- }
|