张保健 11 ay önce
ebeveyn
işleme
84ed4f453f
4 değiştirilmiş dosya ile 432 ekleme ve 0 silme
  1. 8 0
      controllers/lis.go
  2. 1 0
      routers/router.go
  3. 321 0
      service/gzky_service.go
  4. 102 0
      service/szwz_service.go

+ 8 - 0
controllers/lis.go Dosyayı Görüntüle

@@ -44,6 +44,14 @@ func (c *LisController) SyncGzky() {
44 44
 	return
45 45
 }
46 46
 
47
+func (c *LisController) SyncSzwz() {
48
+	service.SyncLisSzwz()
49
+	c.ServeSuccessJSON(map[string]interface{}{
50
+		"resultList": "12345",
51
+	})
52
+	return
53
+}
54
+
47 55
 // 石狮盛誉LIS同步
48 56
 func (c *LisController) SyncSssy() {
49 57
 	service.SyncSYData2()

+ 1 - 0
routers/router.go Dosyayı Görüntüle

@@ -35,4 +35,5 @@ func init() {
35 35
 	beego.Router("/api/lis/sssy", &controllers.LisController{}, "get:SyncSssy")
36 36
 	beego.Router("/api/lis/tscyvip", &controllers.LisController{}, "get:SyncTscyVipLis")
37 37
 	beego.Router("/api/lis/syncgzky", &controllers.LisController{}, "get:SyncGzky")
38
+	beego.Router("/api/lis/syncszwz", &controllers.LisController{}, "get:SyncSzwz")
38 39
 }

+ 321 - 0
service/gzky_service.go Dosyayı Görüntüle

@@ -0,0 +1,321 @@
1
+package service
2
+
3
+// 广州康允血液透析中心 检验检查对接
4
+import (
5
+	"IC/models"
6
+	"IC/utils"
7
+	_ "encoding/json"
8
+	_ "encoding/xml"
9
+	"fmt"
10
+	"github.com/jinzhu/gorm"
11
+	_ "github.com/jinzhu/gorm"
12
+	_ "strconv"
13
+	"strings"
14
+	"time"
15
+)
16
+
17
+type gzkylis struct {
18
+	Finputdate           string `gorm:"column:F_InputDate" json:"F_InputDate"`
19
+	Fname                string `gorm:"column:F_Name" json:"F_Name"`
20
+	Fsex                 string `gorm:"column:F_Sex" json:"F_Sex"`
21
+	Fage                 string `gorm:"column:F_Age" json:"F_Age"`
22
+	Fhospnaturalitem     string `gorm:"column:F_HospNaturalItem" json:"F_HospNaturalItem"`
23
+	Fhospnaturalitemname string `gorm:"column:F_HospNaturalItemName" json:"F_HospNaturalItemName"`
24
+	Fhospitem            string `gorm:"column:F_HospItem" json:"F_HospItem"`
25
+	Fhospitemname        string `gorm:"column:F_HospItemName" json:"F_HospItemName"`
26
+	Fresult              string `gorm:"column:F_Result" json:"F_Result"`
27
+	Funit                string `gorm:"column:F_Unit" json:"F_Unit"`
28
+	Fhint                string `gorm:"column:F_Hint" json:"F_Hint"`
29
+	Freference           string `gorm:"column:F_Reference" json:"F_Reference"`
30
+	Frecordtime          string `gorm:"column:F_RecordTime" json:"F_RecordTime"`
31
+}
32
+
33
+func (gzkylis) TableName() string {
34
+	return "V_KM_LIS_Result"
35
+}
36
+
37
+func GetGzkyLis(rdb *gorm.DB, synctime string) (record []*gzkylis, err error) {
38
+	err = rdb.Model(&gzkylis{}).Where("F_RecordTime >= ? ", synctime).Find(&record).Error
39
+	return
40
+}
41
+
42
+// 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
43
+func GetGzkyProjectID(org_id int64, project_name string) (project_id int64, err error) {
44
+	var inspection_reference models.MiddleInspectionReference
45
+	err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?  ", org_id, project_name).First(&inspection_reference).Error
46
+	if inspection_reference.ID > 0 {
47
+		return inspection_reference.ProjectId, err
48
+	} else {
49
+		err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
50
+		if inspection_reference.ProjectId > 0 {
51
+			return inspection_reference.ProjectId + 1, err
52
+		} else {
53
+			return 1056701, err
54
+		}
55
+	}
56
+}
57
+
58
+// 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
59
+func GetGzkyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
60
+	var inspection_reference models.MiddleInspectionReference
61
+	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
62
+	if inspection_reference.ID > 0 {
63
+		return inspection_reference.ItemId, err
64
+	} else {
65
+		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
66
+		if inspection_reference.ItemId > 0 {
67
+			return inspection_reference.ItemId + 1, err
68
+		} else {
69
+			return project_id*100 + 1, err
70
+		}
71
+	}
72
+}
73
+
74
+// 汕头三优血液透析中心
75
+func SyncGzkyLis() (err error) {
76
+	org_id := int64(10567)
77
+
78
+	org := &models.DataUploadConfig{
79
+		OrgId:  org_id,
80
+		DbHost: "127.0.0.1",
81
+		DbPort: "1433",
82
+		DbPass: "1Q2W3e4r!@#$",
83
+		DbUser: "kyy",
84
+		DbName: "kmdb",
85
+	}
86
+
87
+	orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
88
+	if err != nil {
89
+		utils.ErrorLog("创建数据库连接失败:%v", err)
90
+		return
91
+	}
92
+
93
+	// var sync_time int64
94
+	// sync_time = 1627747200
95
+
96
+	// scpaLis, _ := GetScpaLis(orgDb, sync_time,"512926196302182682")
97
+	// utils.InfoLog("IdCardNo:%v",scpaLis)
98
+
99
+	// 第一步:获取上一次同步的时间点
100
+	syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
101
+	var sync_time int64
102
+	if syncLastInfo.ID > 0 {
103
+		sync_time = syncLastInfo.SyncTime
104
+	} else {
105
+		sync_time = 1685548800
106
+	}
107
+
108
+	SyncTime := time.Unix(sync_time, 0)
109
+	syncTimeStr := SyncTime.Format("2006-01-02 15:04:05")
110
+
111
+	fmt.Println(SyncTime)
112
+	// 第二步:获取时间内所有检验结果
113
+	gzkyLis, _ := GetGzkyLis(orgDb, syncTimeStr)
114
+
115
+	if len(gzkyLis) > 0 {
116
+		for _, LisInfo := range gzkyLis {
117
+			utils.InfoLog("LisInfo:%v", LisInfo)
118
+			utils.InfoLog("患者姓名:%v", LisInfo.Fname)
119
+			brxm := LisInfo.Fname
120
+			// 根据姓名获取患者ID
121
+			patient, _ := GetUserInfoByName(org_id, brxm)
122
+			if patient.ID > 0 {
123
+				project_id, _ := GetGzkyProjectID(org_id, LisInfo.Fhospnaturalitemname)
124
+				item_id, _ := GetGzkyItemID(org_id, LisInfo.Fhospnaturalitemname, LisInfo.Fhospitemname, project_id)
125
+				tx := writeMiddleDb.Begin()
126
+				var inspection models.MiddleInspection
127
+				var inspection_reference models.MiddleInspectionReference
128
+
129
+				tempTime := strings.Split(LisInfo.Frecordtime, "T")
130
+				utils.InfoLog("tempTime1:%v", tempTime[0])
131
+				utils.InfoLog("tempTime2:%v", tempTime[1])
132
+				tempTime1 := strings.Split(tempTime[1], "Z")
133
+				tempRecord := tempTime[0] + " " + tempTime1[0]
134
+
135
+				utils.InfoLog("tempTime3:%v", tempRecord)
136
+
137
+				recordTime, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", tempRecord)
138
+				utils.InfoLog("tempTime4:%v", recordTime)
139
+				inspect_date := recordTime.Format("2006-01-02 15:04")
140
+				recordTimeStr, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", inspect_date)
141
+				recordTimeInt := recordTimeStr.Unix()
142
+				utils.InfoLog("tempTime5:%v", recordTimeInt)
143
+				utils.InfoLog("tempTime6:%v", recordTimeStr)
144
+
145
+				var total int
146
+				var RangeOptions string
147
+				var RangeMin string
148
+				var RangeMax string
149
+				var ItemType int
150
+				typeFlag := strings.Contains(LisInfo.Freference, "阴性")
151
+				if typeFlag {
152
+					ItemType = 2
153
+					RangeMin = LisInfo.Freference
154
+					RangeOptions = LisInfo.Fhint
155
+				} else {
156
+					Range := strings.Split(LisInfo.Freference, "-")
157
+					if len(Range) > 1 {
158
+						RangeMin = Range[0]
159
+						RangeMax = Range[1]
160
+						ItemType = 1
161
+					} else {
162
+						ItemType = 2
163
+						RangeMin = LisInfo.Freference
164
+					}
165
+					RangeOptions = LisInfo.Fhint
166
+				}
167
+
168
+				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
169
+				if total <= 0 {
170
+					inspection_reference.OrgId = org_id
171
+					inspection_reference.ProjectName = LisInfo.Fhospnaturalitemname
172
+					inspection_reference.Project = LisInfo.Fhospnaturalitem
173
+					inspection_reference.ProjectId = project_id
174
+					inspection_reference.ItemName = LisInfo.Fhospitemname
175
+					inspection_reference.ItemNameAddition = LisInfo.Fhospitem
176
+					inspection_reference.ItemId = item_id
177
+					inspection_reference.RangeType = ItemType
178
+					inspection_reference.RangeMin = RangeMin
179
+					inspection_reference.RangeMax = RangeMax
180
+					inspection_reference.RangeValue = LisInfo.Freference
181
+					inspection_reference.RangeOptions = RangeOptions
182
+					inspection_reference.Unit = LisInfo.Funit
183
+					inspection_reference.Status = 1
184
+					inspection_reference.CreatedTime = time.Now().Unix()
185
+					inspection_reference.UpdatedTime = time.Now().Unix()
186
+					inspection_reference.InspectDate = tempRecord
187
+					inspection_reference.UTime = tempRecord
188
+					err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
189
+					if err != nil {
190
+						tx.Rollback()
191
+					}
192
+				}
193
+				var itotal int
194
+				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, recordTimeStr, patient.ID).Find(&inspection).Count(&itotal).Error
195
+				if itotal <= 0 {
196
+					// inspection.InspectValue = strings.Replace(LisInfo.Testresult, "&gt;", ">", -1)
197
+					// inspection.InspectValue = strings.Replace(LiLisInfo.Testresult, "&lt;", "<", -1)
198
+					inspection.PatientId = patient.ID
199
+					inspection.OrgId = org_id
200
+					inspection.ProjectId = project_id
201
+					inspection.ItemName = inspection_reference.ItemName
202
+					inspection.ProjectName = inspection_reference.ProjectName
203
+					inspection.InspectType = ItemType
204
+					inspection.ItemId = item_id
205
+					inspection.InspectValue = LisInfo.Fresult
206
+					inspection.InspectDate = inspect_date
207
+					inspection.RecordDate = recordTimeInt
208
+					inspection.InspectTips = LisInfo.Fhint
209
+					inspection.Status = 1
210
+					inspection.CreatedTime = time.Now().Unix()
211
+					inspection.UpdatedTime = time.Now().Unix()
212
+					// inspection.UTime = record_date.Format(timeLayout)
213
+					// inspection.HisUserId = strconv.FormatInt(patient_id, 10)
214
+					inspection.SysProjectId = inspection_reference.XtProjectId
215
+					inspection.SysItemId = inspection_reference.XtItemId
216
+					err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
217
+					if err != nil {
218
+						tx.Rollback()
219
+					}
220
+				}
221
+				tx.Commit()
222
+			} else {
223
+				continue
224
+			}
225
+		}
226
+	}
227
+	var syncInfo models.MiddleSyncInfo
228
+	syncInfo.OrgId = org_id
229
+	syncInfo.SyncTime = time.Now().Unix()
230
+	syncInfo.SyncResultType = 1
231
+	syncInfo.SyncRsultRemark = "同步成功"
232
+	syncInfo.SyncTotalNum = 0
233
+	syncInfo.SyncSuccessNum = 0
234
+	syncInfo.SyncInfo = ""
235
+	syncInfo.CreateTime = time.Now().Unix()
236
+	syncInfo.UpdateTime = time.Now().Unix()
237
+
238
+	cwderr := CreateSyncInfo(&syncInfo)
239
+	if cwderr != nil {
240
+		utils.ErrorLog("创建同步信息失败:%v", cwderr)
241
+		return
242
+	}
243
+	SyncToGzkytx()
244
+	return
245
+}
246
+
247
+// func deleteMid(tx *gorm.DB) (err error) {
248
+// 	err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock as t," +
249
+// 		"(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
250
+// 		"(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock  WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
251
+// 		"WHERE t1.user_org_id = t2.orgid)tmp " +
252
+// 		"SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
253
+// 		"WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
254
+// 	return
255
+// }
256
+
257
+func SyncToGzkytx() {
258
+
259
+	utils.TraceLog("检验检查同步任务开始执行")
260
+	org_id := int64(10567)
261
+
262
+	// 第一步:跟进org_id 去中间库查出需要同步的数据
263
+	//inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
264
+	inspections, _ := GetSyncInspectionByOrgId(org_id)
265
+
266
+	// 第二步:将数据同步到业务库
267
+	//if len(inspection_references) > 0 {
268
+	//	for _, inspection_reference := range inspection_references {
269
+	//		SyncInspectionReference(&inspection_reference)
270
+	//	}
271
+	//}
272
+
273
+	if len(inspections) > 0 {
274
+		for _, inspection := range inspections {
275
+			if inspection.SysProjectId > 0 && inspection.SysItemId > 0 {
276
+				SyncGzkyInspection(&inspection)
277
+			}
278
+		}
279
+	}
280
+	utils.SuccessLog("检验检查同步任务完成")
281
+}
282
+
283
+// 从机构将数据同步到中间库
284
+func SyncGzkyInspection(data *models.MiddleInspection) error {
285
+	tx := writeDb.Begin()
286
+	var inspection models.Inspection
287
+	var total int
288
+	err = readDb.Model(&models.Inspection{}).Where("org_id = ? and project_id = ? and item_id = ? and patient_id =? and inspect_date=?  and status = 1", data.OrgId, data.ProjectId, data.ItemId, data.PatientId, data.RecordDate).Find(&inspection).Count(&total).Error
289
+	if total <= 0 {
290
+
291
+		inspection.OrgId = data.OrgId
292
+		inspection.PatientId = data.PatientId
293
+		inspection.ProjectName = data.ProjectName
294
+		// inspection.Project = data.ProjectName
295
+		inspection.ProjectId = data.SysProjectId
296
+		inspection.ItemName = data.ItemName
297
+		inspection.ItemId = data.SysItemId
298
+		inspection.InspectType = int64(data.InspectType)
299
+		inspection.InspectValue = data.InspectValue
300
+		inspection.InspectTips = data.InspectTips
301
+		inspection.InspectDate = data.RecordDate
302
+		inspection.Status = 1
303
+		inspection.CreatedTime = time.Now().Unix()
304
+		inspection.UpdatedTime = time.Now().Unix()
305
+
306
+		err := tx.Model(&models.Inspection{}).Create(&inspection).Error
307
+		if err != nil {
308
+			tx.Rollback()
309
+		}
310
+
311
+		data.IsSync = 1
312
+		data.SyncId = inspection.ID
313
+		data.UpdatedTime = time.Now().Unix()
314
+		ierr := writeMiddleDb.Save(&data).Error
315
+		if ierr != nil {
316
+			tx.Rollback()
317
+		}
318
+	}
319
+	tx.Commit()
320
+	return err
321
+}

+ 102 - 0
service/szwz_service.go Dosyayı Görüntüle

@@ -0,0 +1,102 @@
1
+package service
2
+
3
+import (
4
+	"IC/utils"
5
+	_ "IC/utils"
6
+	_ "encoding/json"
7
+	"fmt"
8
+	oracle "github.com/godoes/gorm-oracle"
9
+	_ "github.com/godror/godror"
10
+	"gorm.io/gorm"
11
+	_ "strconv"
12
+	"time"
13
+)
14
+
15
+type wzLisReport struct {
16
+	Repno          string `gorm:"column:REPNO" json:"REPNO"`
17
+	Repname        string `gorm:"column:REPNAME" json:"REPNAME"`
18
+	NAME           string `gorm:"column:NAME" json:"NAME"`
19
+	Appdeptname    string `gorm:"column:APPDEPTNAME" json:"APPDEPTNAME"`
20
+	Fisrtaudittime string `gorm:"column:FISRTAUDITTIME" json:"FISRTAUDITTIME"`
21
+}
22
+
23
+func (wzLisReport) TableName() string {
24
+	return "V_LIS_REPORT"
25
+}
26
+
27
+type wzLisReportResult struct {
28
+	Repno     string `gorm:"column:REPNO" json:"REPNO"`
29
+	Itemid    string `gorm:"column:ITEMID" json:"ITEMID"`
30
+	Itemcname string `gorm:"column:ITEMCNAME" json:"ITEMCNAME"`
31
+	Itemename string `gorm:"column:ITEMENAME" json:"ITEMENAME"`
32
+	Result    string `gorm:"column:RESULT" json:"RESULT"`
33
+	Unit      string `gorm:"column:UNIT" json:"UNIT"`
34
+	Prompt    string `gorm:"column:PROMPT" json:"PROMPT"`
35
+	Refmax    string `gorm:"column:REFMAX" json:"REFMAX"`
36
+	Refmin    string `gorm:"column:REFMIN" json:"REFMIN"`
37
+	Time      string `gorm:"column:TIME" json:"TIME"`
38
+}
39
+
40
+func (wzLisReportResult) TableName() string {
41
+	return "V_LIS_REPORT_RESULT"
42
+}
43
+
44
+func GetSzwzyLisReport(rdb *gorm.DB, synctime string) (record []*wzLisReport, err error) {
45
+	err = rdb.Model(&wzLisReport{}).Where("FISRTAUDITTIME >= ? and APPDEPTNAME = '肾病科'", synctime).Find(&record).Error
46
+	return
47
+}
48
+
49
+func GetSzwzyLisReportResule(rdb *gorm.DB, repno string) (record []*wzLisReportResult, err error) {
50
+	err = rdb.Model(&wzLisReportResult{}).Where("REPNO = ? ", repno).Find(&record).Error
51
+	return
52
+}
53
+
54
+// 深圳五洲中医院Lis同步
55
+func SyncLisSzwz() (err error) {
56
+
57
+	org_id := int64(10580)
58
+	// oracle://user:password@127.0.0.1:1521/service
59
+	url := oracle.BuildUrl("172.9.200.3", 1521, "JKY", "BSLIS52", "zhsoft", nil)
60
+	orgDb, err := gorm.Open(oracle.Open(url), &gorm.Config{})
61
+	if err != nil {
62
+		utils.ErrorLog("创建数据库连接失败:%v", err)
63
+		return
64
+	}
65
+
66
+	// 第一步:获取上一次同步的时间点
67
+	syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
68
+	var sync_time int64
69
+	if syncLastInfo.ID > 0 {
70
+		sync_time = syncLastInfo.SyncTime
71
+	} else {
72
+		sync_time = 1672502400 // 2023-01-01
73
+	}
74
+
75
+	SyncTime := time.Unix(sync_time, 0)
76
+	syncTimeStr := SyncTime.Format("2006-01-02 15:04:05")
77
+
78
+	fmt.Println(SyncTime)
79
+
80
+	// 第二步:获取时间内所有检验结果
81
+	szwzLisReport, _ := GetSzwzyLisReport(orgDb, syncTimeStr)
82
+	if len(szwzLisReport) > 0 {
83
+		for _, Report := range szwzLisReport {
84
+			utils.InfoLog("Report:%v", Report)
85
+			utils.InfoLog("患者姓名:%v", Report.NAME)
86
+			if len(Report.Repname) <= 0 {
87
+				Report.Repname = "未知"
88
+			}
89
+			patient, _ := GetUserInfoByName(org_id, Report.NAME)
90
+			if patient.ID > 0 {
91
+				// 第三步,根据REPNO查询对应的检查小项
92
+				szwzLisReportResult, _ := GetSzwzyLisReportResule(orgDb, Report.Repno)
93
+				if len(szwzLisReportResult) > 0 {
94
+					for _, Result := range szwzLisReportResult {
95
+						utils.InfoLog("Result:%v", Result)
96
+					}
97
+				}
98
+			}
99
+		}
100
+	}
101
+	return err
102
+}