张保健 1 month ago
parent
commit
0962a89120
4 changed files with 722 additions and 226 deletions
  1. 2 1
      main.go
  2. 1 0
      models/lis_model.go
  3. 9 9
      service/cron.go
  4. 710 216
      service/gzky_service.go

+ 2 - 1
main.go View File

@@ -34,6 +34,7 @@ func main() {
34 34
 	//service.SyncSHLisFunc() // 舒和LIS对接
35 35
 	//service.SyncLisLaxk() //乐安祥康LIS对接
36 36
 	//service.SyncLisPljx(10597) // 揭阳LIS对接
37
-	service.SyncLisPljx(10599) // 揭阳LIS对接
37
+	//service.SyncLisPljx(10599) // 揭阳LIS对接
38
+	service.SyncLisGzky(0) // 广州康允血液透析中心LIS对接
38 39
 	beego.Run()
39 40
 }

+ 1 - 0
models/lis_model.go View File

@@ -333,6 +333,7 @@ type Inspection struct {
333 333
 	Status       int64  `gorm:"column:status" json:"status" form:"status"`
334 334
 	CreatedTime  int64  `gorm:"column:created_time" json:"created_time" form:"created_time"`
335 335
 	UpdatedTime  int64  `gorm:"column:updated_time" json:"updated_time" form:"updated_time"`
336
+	SgnTreat     int64  `gorm:"column:sgn_treat" json:"sgn_treat" form:"sgn_treat"`
336 337
 }
337 338
 
338 339
 func (Inspection) TableName() string {

+ 9 - 9
service/cron.go View File

@@ -101,15 +101,15 @@ func BeginAutoSyncLis() {
101 101
 	//	SyncLisSgyhs()
102 102
 	//})
103 103
 
104
-	spec11 := "0 20 */12 * * ?" // 每个小时同步一次 揭阳景熹LIS对接,本地部署的
105
-	createLisSyncCronJob.AddFunc(spec11, func() {
106
-		SyncLisPljx(10597)
107
-	})
108
-
109
-	spec12 := "0 40 */12 * * ?" // 每个小时同步一次 普宁景熹LIS对接,本地部署的
110
-	createLisSyncCronJob.AddFunc(spec12, func() {
111
-		SyncLisPljx(10599)
112
-	})
104
+	//spec11 := "0 20 */12 * * ?" // 每个小时同步一次 揭阳景熹LIS对接,本地部署的
105
+	//createLisSyncCronJob.AddFunc(spec11, func() {
106
+	//	SyncLisPljx(10597)
107
+	//})
108
+	//
109
+	//spec12 := "0 40 */12 * * ?" // 每个小时同步一次 普宁景熹LIS对接,本地部署的
110
+	//createLisSyncCronJob.AddFunc(spec12, func() {
111
+	//	SyncLisPljx(10599)
112
+	//})
113 113
 
114 114
 	//spec13 := "0 10 * * * ?" // 每个小时同步一次 唐山遵化仁爱医院LIS对接,本地部署的
115 115
 	//createLisSyncCronJob.AddFunc(spec13, func() {

+ 710 - 216
service/gzky_service.go View File

@@ -1,260 +1,217 @@
1 1
 package service
2 2
 
3
-// 广州康允血液透析中心 检验检查对接
4 3
 import (
5 4
 	"IC/models"
6 5
 	"IC/utils"
6
+	_ "IC/utils"
7
+	"encoding/json"
7 8
 	_ "encoding/json"
8
-	_ "encoding/xml"
9
-	"fmt"
10
-	"github.com/jinzhu/gorm"
9
+	"encoding/xml"
10
+	_ "fmt"
11
+	"github.com/astaxie/beego/httplib"
11 12
 	_ "github.com/jinzhu/gorm"
12
-	_ "strconv"
13
+	"net/url"
14
+	"strconv"
13 15
 	"strings"
14 16
 	"time"
15 17
 )
16 18
 
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"`
19
+// 广州康允血液透析中心 检验检查对接
20
+
21
+type XmlKeyGzky struct {
22
+	XMLName    xml.Name `xml:"string"`
23
+	StringName string   `xml:"name,attr"`
24
+	InnerText  string   `xml:",innerxml"`
31 25
 }
32 26
 
33
-func (gzkylis) TableName() string {
34
-	return "V_KM_LIS_Result"
27
+// 获取艾迪康的key
28
+func GetAdKeyGzky(logid string, password string) (key string) {
29
+	Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/Login?"
30
+	apiurl := "logid=" + logid + "&password=" + password
31
+	apiurl = url.PathEscape(apiurl)
32
+	apiurl = Domain + apiurl
33
+	XmlKeyGzky := XmlKeyGzky{}
34
+	httplib.Get(apiurl).ToXML(&XmlKeyGzky)
35
+	return XmlKeyGzky.InnerText
35 36
 }
36 37
 
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
38
+type ADStringGzky struct {
39
+	XMLName        xml.Name         `xml:"string" json:"string"`
40
+	NewDataSetGzky rpnewDataSetGzky `xml:"NewDataSet" json:"NewDataSet"`
40 41
 }
41 42
 
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
-	}
43
+type rpnewDataSetGzky struct {
44
+	ListTableGzky []rplisttableGzky `xml:"listtable" json:"listtable"`
56 45
 }
57 46
 
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
-	}
47
+type rplisttableGzky struct {
48
+	Id              string `xml:"Id" json:"Id"`
49
+	ReportType      string `xml:"ReportType" json:"ReportType"`
50
+	AdiconBarcode   string `xml:"AdiconBarcode" json:"AdiconBarcode"`
51
+	PatientName     string `xml:"PatientName" json:"PatientName"`
52
+	Bgrq            string `xml:"Bgrq" json:"Bgrq"`
53
+	Instrument      string `xml:"Instrument" json:"Instrument"`
54
+	LisDate         string `xml:"LisDate" json:"LisDate"`
55
+	Sampleid        string `xml:"Sampleid" json:"Sampleid"`
56
+	CustomerBarcode string `xml:"CustomerBarcode" json:"CustomerBarcode"`
57
+	Repno           string `xml:"Repno" json:"Repno"`
58
+	// PdfFileName		string `xml:"PdfFileName" json:"PdfFileName"`
59
+	// DownFlag		string `xml:"DownFlag" json:"DownFlag"`
60
+	// DownDate		string `xml:"DownDate" json:"DownDate"`
61
+	Sjrq string `xml:"Sjrq" json:"Sjrq"`
62
+	// Sjys		string `xml:"Sjys" json:"Sjys"`
63
+	// Brnl		string `xml:"Brnl" json:"Brnl"`
64
+	// Brxb		string `xml:"Brxb" json:"Brxb"`
65
+	Bbzl string `xml:"bbzl" json:"bbzl"`
66
+	// Bzlbid		string `xml:"Bzlbid" json:"Bzlbid"`
67
+	// PDFFileType		string `xml:"PDFFileType" json:"PDFFileType"`
68
+	// PageType		string `xml:"PageType" json:"PageType"`
69
+	PatientNo string `xml:"PatientNo" json:"PatientNo"`
70
+	// SfzId		string `xml:"SfzId" json:"SfzId"`
71
+	// CertificatesType		string `xml:"CertificatesType" json:"CertificatesType"`
72
+	// Nationality		string `xml:"Nationality" json:"Nationality"`
73
+	// SerialNumber		string `xml:"SerialNumber" json:"SerialNumber"`
74
+	// Str1		string `xml:"Str1" json:"Str1"`
75
+	// Str2		string `xml:"Str2" json:"Str2"`
76
+	// Str3		string `xml:"Str3" json:"Str3"`
77
+	// Str4		string `xml:"Str4" json:"Str4"`
78
+	// Str5		string `xml:"Str5" json:"Str5"`
79
+	// TWBG		string `xml:"TWBG" json:"TWBG"`
72 80
 }
73 81
 
74
-// 汕头三优血液透析中心
75
-func SyncGzkyLis() (err error) {
76
-	org_id := int64(10567)
82
+// 获取机构一段时间内检验检查列表
83
+func GetAdGetReportListGzky(Key string, BeginDateTime string, EndDateTime string, TypeDateTime string, AgainFlag string) (key ADStringGzky) {
84
+	Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetReportList"
85
+	//apiurl := "Key=" + Key + "&BeginDateTime=" + BeginDateTime + "&EndDateTime=" + EndDateTime + "&TypeDateTime=" + TypeDateTime + "&AgainFlag=" + AgainFlag
86
+	//apiurl = url.PathEscape(apiurl)
87
+	//apiurl = Domain + apiurl
88
+	//utils.InfoLog("url0: %v", apiurl)
89
+	var xmlKey ADStringGzky
90
+	req := httplib.Post(Domain)
91
+	req.Param("Key", Key)
92
+	req.Param("BeginDateTime", BeginDateTime)
93
+	req.Param("EndDateTime", EndDateTime)
94
+	req.Param("TypeDateTime", TypeDateTime)
95
+	req.Param("AgainFlag", AgainFlag)
96
+	xmlString, _ := req.String()
97
+	xmlString = strings.Replace(xmlString, "&lt;", "<", -1)
98
+	xmlString = strings.Replace(xmlString, "&gt;", ">", -1)
99
+	xml.Unmarshal([]byte(xmlString), &xmlKey)
100
+	return xmlKey
101
+}
77 102
 
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
-	}
103
+type AdkCheckGzky struct {
104
+	Cgbg AdkSResultGzky `json:"常规报告"`
105
+}
86 106
 
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
-	}
107
+type AdkSResultGzky struct {
108
+	Item []AdkItemGzky `json:"item"`
109
+}
92 110
 
93
-	// var sync_time int64
94
-	// sync_time = 1627747200
111
+type AdkItemGzky struct {
112
+	Adiconbarcode   string `json:"AdiconBarcode"`
113
+	Patientname     string `json:"PatientName"`
114
+	Sex             string `json:"Sex"`
115
+	Age             string `json:"Age"`
116
+	Reportdate      string `json:"ReportDate"`
117
+	Itemcode        string `json:"ItemCode"`
118
+	ItemnameCn      string `json:"ItemName_CN"`
119
+	ItemnameEn      string `json:"ItemName_EN"`
120
+	Result          string `json:"Result"`
121
+	Resulthint      string `json:"ResultHint"`
122
+	Resultreference string `json:"ResultReference"`
123
+	Resultunit      string `json:"ResultUnit"`
124
+	Zhmc            string `json:"zhmc"`
125
+}
126
+
127
+func GetAdkInfoGzky(key string, id string) (str AdkCheckGzky, checkStr string) {
128
+	Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetJSONReportItemListByAdiconBarocde?"
129
+	//apiurl := "key=" + key + "&AdiconBarocde=" + id
130
+	//apiurl = url.PathEscape(apiurl)
131
+	//apiurl = Domain + apiurl
132
+	//utils.InfoLog("url1: %v", apiurl)
133
+	xmlKey := XmlKeyGzky{}
134
+	rep := httplib.Post(Domain)
135
+	rep.Param("Key", key)
136
+	rep.Param("AdiconBarcode", id)
137
+	rep.ToXML(&xmlKey)
138
+	utils.InfoLog("InnerText: %v", xmlKey.InnerText)
139
+	var checkJSON AdkCheckGzky
140
+	json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
95 141
 
96
-	// scpaLis, _ := GetScpaLis(orgDb, sync_time,"512926196302182682")
97
-	// utils.InfoLog("IdCardNo:%v",scpaLis)
142
+	return checkJSON, xmlKey.InnerText
143
+}
98 144
 
99
-	// 第一步:获取上一次同步的时间点
145
+// 广州康允透析中心Lis同步
146
+func SyncLisGzky(flag int64) (err error) {
147
+	// 第一步:获取艾迪康下发的Key
148
+	org_id := int64(10567)
149
+	login := "RH0395"
150
+	password := "abc123456"
151
+	key := GetAdKeyGzky(login, password)
152
+	utils.InfoLog("key:%v", key)
153
+	// 第二步:获取上一次同步的时间点
100 154
 	syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
101 155
 	var sync_time int64
102 156
 	if syncLastInfo.ID > 0 {
103
-		sync_time = syncLastInfo.SyncTime
157
+		sync_time = syncLastInfo.SyncTime - 60*60*24*3
104 158
 	} else {
105
-		sync_time = 1685548800
159
+		sync_time = 1730390400
106 160
 	}
107 161
 
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
-				}
162
+	if flag > 0 {
163
+		sync_time = 1730390400
164
+	}
167 165
 
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()
166
+	nowTimes := time.Now().Unix()
167
+	// syncTimes := time.Unix(sync_time,0).Format("2006-01-02")
168
+	if sync_time < nowTimes {
169
+		for i := sync_time; i < nowTimes; i = i + 86400 {
170
+			tempsyncTimes := time.Unix(i, 0).Format("2006-01-02")
171
+			adString := GetAdGetReportListGzky(key, tempsyncTimes, tempsyncTimes, "2", "1")
172
+			listtable := adString.NewDataSetGzky.ListTableGzky
173
+			if len(listtable) >= 1 {
174
+				// 第三步:根据检查列表获取的ID来获取检验检查详情
175
+				for _, list := range listtable {
176
+					listId := list.AdiconBarcode
177
+					checkInfo, _ := GetAdkInfoGzky(key, listId)
178
+					//utils.InfoLog("checkStr: %v", checkStr)
179
+					for _, info := range checkInfo.Cgbg.Item {
180
+						// 根据姓名 去查询对应的patient_id
181
+						patient, _ := GetPatientID(org_id, info.Patientname)
182
+						utils.InfoLog("patient: %v", patient)
183
+						if patient.ID > 0 {
184
+							GzkyReportInfoHandle(info, org_id, patient.ID)
185
+						}
191 186
 					}
192 187
 				}
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
-					}
188
+				var syncInfo models.MiddleSyncInfo
189
+				syncInfo.OrgId = org_id
190
+
191
+				syncInfo.SyncTime = i
192
+				syncInfo.SyncResultType = 1
193
+				syncInfo.SyncRsultRemark = "同步成功"
194
+				syncInfo.SyncTotalNum = 0
195
+				syncInfo.SyncSuccessNum = 0
196
+				syncInfo.SyncInfo = ""
197
+				syncInfo.CreateTime = time.Now().Unix()
198
+				syncInfo.UpdateTime = time.Now().Unix()
199
+
200
+				cwderr := CreateSyncInfo(&syncInfo)
201
+				if cwderr != nil {
202
+					utils.ErrorLog("创建同步信息失败:%v", cwderr)
203
+					return
220 204
 				}
221
-				tx.Commit()
222
-			} else {
223
-				continue
224 205
 			}
225 206
 		}
207
+
226 208
 	}
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()
209
+	SyncToGzky()
210
+	TreatSgnForGzky(10567)
244 211
 	return
245 212
 }
246 213
 
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() {
214
+func SyncToGzky() {
258 215
 
259 216
 	utils.TraceLog("检验检查同步任务开始执行")
260 217
 	org_id := int64(10567)
@@ -287,7 +244,6 @@ func SyncGzkyInspection(data *models.MiddleInspection) error {
287 244
 	var total int
288 245
 	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 246
 	if total <= 0 {
290
-
291 247
 		inspection.OrgId = data.OrgId
292 248
 		inspection.PatientId = data.PatientId
293 249
 		inspection.ProjectName = data.ProjectName
@@ -319,3 +275,541 @@ func SyncGzkyInspection(data *models.MiddleInspection) error {
319 275
 	tx.Commit()
320 276
 	return err
321 277
 }
278
+
279
+// 同步到正式库后,确认透前肾功能和透后肾功能
280
+// 计算KT/V 和 URR
281
+func TreatSgnForGzky(org_id int64) error {
282
+	tx := writeDb.Begin()
283
+	var inspection []models.Inspection
284
+	err = readDb.Model(&models.Inspection{}).Where("org_id = ? and project_id = 15 and sgn_treat = 0  and status = 1", org_id).Find(&inspection).Error
285
+	if len(inspection) > 0 {
286
+		for _, insp := range inspection {
287
+			var inspectionInfo models.Inspection
288
+			err = readDb.Model(&models.Inspection{}).Where("org_id = ? and patient_id = ? and project_id = 15 and item_id = ? and inspect_date = ? and sgn_treat = 0  and status = 1", org_id, insp.PatientId, insp.ItemId, insp.InspectDate).First(&inspectionInfo).Error
289
+			if inspectionInfo.ID > 0 {
290
+				// 查询前面两天是否有数据,有的话,当前数据有透后数据
291
+				startData := inspectionInfo.InspectDate - 60*60*48
292
+				var inspectionBefore models.Inspection
293
+				err = readDb.Model(&models.Inspection{}).Where("org_id = ? and patient_id = ? and project_id = 15 and item_id = ? and inspect_date >= ? and inspect_date < ? and sgn_treat = 0  and status = 1", org_id, insp.PatientId, insp.ItemId, startData, insp.InspectDate).First(&inspectionBefore).Error
294
+				if inspectionBefore.ID > 0 {
295
+					inspectionBefore.SgnTreat = 1
296
+					inspectionBefore.UpdatedTime = time.Now().Unix()
297
+					err = writeDb.Save(&inspectionBefore).Error
298
+					if err != nil {
299
+						tx.Rollback()
300
+					}
301
+					insp.ProjectId = 8060
302
+					switch insp.ItemId {
303
+					case 37: //尿素
304
+						insp.ItemId = 8061
305
+						break
306
+					case 38: //尿酸
307
+						insp.ItemId = 10834
308
+						break
309
+					case 42: //β2-微球蛋白
310
+						insp.ItemId = 10835
311
+						break
312
+					case 115: //肌酐
313
+						insp.ItemId = 8060
314
+						break
315
+					}
316
+					insp.UpdatedTime = time.Now().Unix()
317
+					insp.SgnTreat = 1
318
+					err = writeDb.Save(&insp).Error
319
+					if err != nil {
320
+						tx.Rollback()
321
+					}
322
+				} else {
323
+					// 前面两天没有查到数据,则可能当前数据是透前数据,后面两天查询到的数据是透后数据
324
+					endData := inspectionInfo.InspectDate + 60*60*48
325
+					var inspectionEnd models.Inspection
326
+					err = readDb.Model(&models.Inspection{}).Where("org_id = ? and patient_id = ? and project_id = 15 and item_id = ? and inspect_date > ? and inspect_date <= ? and sgn_treat = 0  and status = 1", org_id, insp.PatientId, insp.ItemId, insp.InspectDate, endData).First(&inspectionEnd).Error
327
+					if inspectionEnd.ID > 0 {
328
+						insp.SgnTreat = 1
329
+						insp.UpdatedTime = time.Now().Unix()
330
+						err = writeDb.Save(&insp).Error
331
+						if err != nil {
332
+							tx.Rollback()
333
+						}
334
+						inspectionEnd.ProjectId = 8060
335
+						switch inspectionEnd.ItemId {
336
+						case 37: //尿素
337
+							inspectionEnd.ItemId = 8061
338
+							break
339
+						case 38: //尿酸
340
+							inspectionEnd.ItemId = 10834
341
+							break
342
+						case 42: //β2-微球蛋白
343
+							inspectionEnd.ItemId = 10835
344
+							break
345
+						case 115: //肌酐
346
+							inspectionEnd.ItemId = 8060
347
+							break
348
+						}
349
+						inspectionEnd.UpdatedTime = time.Now().Unix()
350
+						inspectionEnd.SgnTreat = 1
351
+						err = writeDb.Save(&inspectionEnd).Error
352
+						if err != nil {
353
+							tx.Rollback()
354
+						}
355
+					} else {
356
+						// 如果前面两天和后面两天都没有数据,则直接改变状体
357
+						insp.SgnTreat = 1
358
+						insp.UpdatedTime = time.Now().Unix()
359
+						err = writeDb.Save(&insp).Error
360
+						if err != nil {
361
+							tx.Rollback()
362
+						}
363
+					}
364
+				}
365
+			} else {
366
+				continue
367
+			}
368
+		}
369
+	}
370
+	tx.Commit()
371
+	return err
372
+}
373
+
374
+// 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
375
+func GetGzkyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
376
+	var inspection_reference models.MiddleInspectionReference
377
+	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
378
+	if inspection_reference.ID > 0 {
379
+		return inspection_reference.ItemId, err
380
+	} else {
381
+		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
382
+		utils.InfoLog("inspection_reference: %v", inspection_reference)
383
+		if inspection_reference.ItemId > 0 {
384
+			return inspection_reference.ItemId + 1, err
385
+		} else {
386
+			return project_id*100 + 1, err
387
+		}
388
+	}
389
+}
390
+
391
+// 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
392
+func GetGzkyProjectID(org_id int64, project_name string) (project_id int64, err error) {
393
+	var inspection_reference models.MiddleInspectionReference
394
+	err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
395
+	if inspection_reference.ID > 0 {
396
+		return inspection_reference.ProjectId, err
397
+	} else {
398
+		err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
399
+		if inspection_reference.ProjectId > 0 {
400
+			return inspection_reference.ProjectId + 1, err
401
+		} else {
402
+			return 1056701, err
403
+		}
404
+	}
405
+}
406
+
407
+func GzkyReportInfoHandle(CheckInfo AdkItemGzky, org_id int64, patientId int64) {
408
+	tx := writeMiddleDb.Begin()
409
+	var total int
410
+	var RangeMin string
411
+	var RangeMax string
412
+
413
+	timstr := strings.Split(CheckInfo.Reportdate, ".")
414
+	recordDateStr := timstr[0]
415
+
416
+	if len(recordDateStr) == 0 {
417
+		recordDateStr = time.Now().Format("2006-01-02 15:04")
418
+	}
419
+
420
+	record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
421
+	record_date := record_date_str.Unix()
422
+	inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
423
+
424
+	// // 判断检查类型
425
+	ItemType := 1
426
+	Range := strings.Split(CheckInfo.Resultreference, "-")
427
+	if len(Range) > 1 {
428
+		RangeMin = Range[0]
429
+		RangeMax = Range[1]
430
+		ItemType = 1
431
+	} else {
432
+		ItemType = 2
433
+	}
434
+
435
+	var inspection models.MiddleInspection
436
+	var inspection_reference models.MiddleInspectionReference
437
+	err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project = ? and item_code = ? and status = 1", org_id, CheckInfo.Zhmc, CheckInfo.Itemcode).Find(&inspection_reference).Count(&total).Error
438
+	if inspection_reference.ID > 0 {
439
+		ItemType = inspection_reference.RangeType
440
+	}
441
+	if total <= 0 {
442
+		//  如果配置表里没有相关配置,则创建project_id,item_id和相关配置信息
443
+		project_id, _ := GetChydProjectID(org_id, CheckInfo.Zhmc)
444
+		item_id, _ := GetChydItemID(org_id, CheckInfo.Zhmc, CheckInfo.ItemnameCn, project_id)
445
+
446
+		inspection_reference.OrgId = org_id
447
+		inspection_reference.ProjectName = CheckInfo.Zhmc
448
+		inspection_reference.Project = CheckInfo.Zhmc
449
+		inspection_reference.ProjectId = project_id
450
+		inspection_reference.ItemName = CheckInfo.ItemnameCn
451
+		inspection_reference.ItemNameAddition = CheckInfo.ItemnameEn
452
+		inspection_reference.ItemId = item_id
453
+		inspection_reference.ItemCode = CheckInfo.Itemcode
454
+		inspection_reference.RangeType = ItemType
455
+		inspection_reference.RangeMin = RangeMin
456
+		inspection_reference.RangeMax = RangeMax
457
+		// inspection_reference.RangeValue = RangeValue
458
+		//inspection_reference.RangeOptions = RangeOptions
459
+		inspection_reference.Unit = CheckInfo.Resultunit
460
+		inspection_reference.Status = 1
461
+		inspection_reference.CreatedTime = time.Now().Unix()
462
+		inspection_reference.UpdatedTime = time.Now().Unix()
463
+		inspection_reference.InspectDate = inspect_date
464
+		inspection_reference.UTime = inspect_date
465
+		inspection_reference.Adiconbarcode = CheckInfo.Adiconbarcode
466
+		err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
467
+		if err != nil {
468
+			tx.Rollback()
469
+		}
470
+	}
471
+
472
+	var itotal int
473
+	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, inspection_reference.ProjectId, inspection_reference.ItemId, record_date, patientId).Find(&inspection).Count(&itotal).Error
474
+	if itotal <= 0 {
475
+		inspection.PatientId = patientId
476
+		inspection.OrgId = org_id
477
+		inspection.ProjectId = inspection_reference.ProjectId
478
+		inspection.ItemName = inspection_reference.ItemName
479
+		inspection.ProjectName = inspection_reference.ProjectName
480
+		inspection.InspectType = ItemType
481
+		inspection.ItemId = inspection_reference.ItemId
482
+		inspection.InspectValue = CheckInfo.Result
483
+		inspection.InspectDate = inspect_date
484
+		inspection.RecordDate = record_date
485
+		//inspection.InspectTips = result.AntiCode
486
+		inspection.Status = 1
487
+		inspection.CreatedTime = time.Now().Unix()
488
+		inspection.UpdatedTime = time.Now().Unix()
489
+		inspection.SysProjectId = inspection_reference.XtProjectId
490
+		inspection.SysItemId = inspection_reference.XtItemId
491
+		inspection.UTime = inspect_date
492
+		inspection.HisUserId = strconv.FormatInt(patientId, 10)
493
+		inspection.Adiconbarcode = CheckInfo.Adiconbarcode
494
+		if inspection_reference.ItemId == 103180107 || inspection_reference.ItemId == 103180301 {
495
+			if strings.Index(CheckInfo.Result, "阳性") != -1 {
496
+				inspection.InspectValue = "阳性"
497
+			} else {
498
+				inspection.InspectValue = "阴性"
499
+			}
500
+		}
501
+
502
+		err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
503
+		if err != nil {
504
+			tx.Rollback()
505
+		}
506
+	}
507
+	tx.Commit()
508
+}
509
+
510
+//
511
+//type gzkylis struct {
512
+//	Finputdate           string `gorm:"column:F_InputDate" json:"F_InputDate"`
513
+//	Fname                string `gorm:"column:F_Name" json:"F_Name"`
514
+//	Fsex                 string `gorm:"column:F_Sex" json:"F_Sex"`
515
+//	Fage                 string `gorm:"column:F_Age" json:"F_Age"`
516
+//	Fhospnaturalitem     string `gorm:"column:F_HospNaturalItem" json:"F_HospNaturalItem"`
517
+//	Fhospnaturalitemname string `gorm:"column:F_HospNaturalItemName" json:"F_HospNaturalItemName"`
518
+//	Fhospitem            string `gorm:"column:F_HospItem" json:"F_HospItem"`
519
+//	Fhospitemname        string `gorm:"column:F_HospItemName" json:"F_HospItemName"`
520
+//	Fresult              string `gorm:"column:F_Result" json:"F_Result"`
521
+//	Funit                string `gorm:"column:F_Unit" json:"F_Unit"`
522
+//	Fhint                string `gorm:"column:F_Hint" json:"F_Hint"`
523
+//	Freference           string `gorm:"column:F_Reference" json:"F_Reference"`
524
+//	Frecordtime          string `gorm:"column:F_RecordTime" json:"F_RecordTime"`
525
+//}
526
+//
527
+//func (gzkylis) TableName() string {
528
+//	return "V_KM_LIS_Result"
529
+//}
530
+//
531
+//func GetGzkyLis(rdb *gorm.DB, synctime string) (record []*gzkylis, err error) {
532
+//	err = rdb.Model(&gzkylis{}).Where("F_RecordTime >= ? ", synctime).Find(&record).Error
533
+//	return
534
+//}
535
+//
536
+//// 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
537
+//func GetGzkyProjectID(org_id int64, project_name string) (project_id int64, err error) {
538
+//	var inspection_reference models.MiddleInspectionReference
539
+//	err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?  ", org_id, project_name).First(&inspection_reference).Error
540
+//	if inspection_reference.ID > 0 {
541
+//		return inspection_reference.ProjectId, err
542
+//	} else {
543
+//		err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
544
+//		if inspection_reference.ProjectId > 0 {
545
+//			return inspection_reference.ProjectId + 1, err
546
+//		} else {
547
+//			return 1056701, err
548
+//		}
549
+//	}
550
+//}
551
+//
552
+//// 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
553
+//func GetGzkyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
554
+//	var inspection_reference models.MiddleInspectionReference
555
+//	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
556
+//	if inspection_reference.ID > 0 {
557
+//		return inspection_reference.ItemId, err
558
+//	} else {
559
+//		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
560
+//		if inspection_reference.ItemId > 0 {
561
+//			return inspection_reference.ItemId + 1, err
562
+//		} else {
563
+//			return project_id*100 + 1, err
564
+//		}
565
+//	}
566
+//}
567
+//
568
+//// 汕头三优血液透析中心
569
+//func SyncGzkyLis() (err error) {
570
+//	org_id := int64(10567)
571
+//
572
+//	org := &models.DataUploadConfig{
573
+//		OrgId:  org_id,
574
+//		DbHost: "127.0.0.1",
575
+//		DbPort: "1433",
576
+//		DbPass: "1Q2W3e4r!@#$",
577
+//		DbUser: "kyy",
578
+//		DbName: "kmdb",
579
+//	}
580
+//
581
+//	orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
582
+//	if err != nil {
583
+//		utils.ErrorLog("创建数据库连接失败:%v", err)
584
+//		return
585
+//	}
586
+//
587
+//	// var sync_time int64
588
+//	// sync_time = 1627747200
589
+//
590
+//	// scpaLis, _ := GetScpaLis(orgDb, sync_time,"512926196302182682")
591
+//	// utils.InfoLog("IdCardNo:%v",scpaLis)
592
+//
593
+//	// 第一步:获取上一次同步的时间点
594
+//	syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
595
+//	var sync_time int64
596
+//	if syncLastInfo.ID > 0 {
597
+//		sync_time = syncLastInfo.SyncTime
598
+//	} else {
599
+//		sync_time = 1685548800
600
+//	}
601
+//
602
+//	SyncTime := time.Unix(sync_time, 0)
603
+//	syncTimeStr := SyncTime.Format("2006-01-02 15:04:05")
604
+//
605
+//	fmt.Println(SyncTime)
606
+//	// 第二步:获取时间内所有检验结果
607
+//	gzkyLis, _ := GetGzkyLis(orgDb, syncTimeStr)
608
+//
609
+//	if len(gzkyLis) > 0 {
610
+//		for _, LisInfo := range gzkyLis {
611
+//			utils.InfoLog("LisInfo:%v", LisInfo)
612
+//			utils.InfoLog("患者姓名:%v", LisInfo.Fname)
613
+//			brxm := LisInfo.Fname
614
+//			// 根据姓名获取患者ID
615
+//			patient, _ := GetUserInfoByName(org_id, brxm)
616
+//			if patient.ID > 0 {
617
+//				project_id, _ := GetGzkyProjectID(org_id, LisInfo.Fhospnaturalitemname)
618
+//				item_id, _ := GetGzkyItemID(org_id, LisInfo.Fhospnaturalitemname, LisInfo.Fhospitemname, project_id)
619
+//				tx := writeMiddleDb.Begin()
620
+//				var inspection models.MiddleInspection
621
+//				var inspection_reference models.MiddleInspectionReference
622
+//
623
+//				tempTime := strings.Split(LisInfo.Frecordtime, "T")
624
+//				utils.InfoLog("tempTime1:%v", tempTime[0])
625
+//				utils.InfoLog("tempTime2:%v", tempTime[1])
626
+//				tempTime1 := strings.Split(tempTime[1], "Z")
627
+//				tempRecord := tempTime[0] + " " + tempTime1[0]
628
+//
629
+//				utils.InfoLog("tempTime3:%v", tempRecord)
630
+//
631
+//				recordTime, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", tempRecord)
632
+//				utils.InfoLog("tempTime4:%v", recordTime)
633
+//				inspect_date := recordTime.Format("2006-01-02 15:04")
634
+//				recordTimeStr, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", inspect_date)
635
+//				recordTimeInt := recordTimeStr.Unix()
636
+//				utils.InfoLog("tempTime5:%v", recordTimeInt)
637
+//				utils.InfoLog("tempTime6:%v", recordTimeStr)
638
+//
639
+//				var total int
640
+//				var RangeOptions string
641
+//				var RangeMin string
642
+//				var RangeMax string
643
+//				var ItemType int
644
+//				typeFlag := strings.Contains(LisInfo.Freference, "阴性")
645
+//				if typeFlag {
646
+//					ItemType = 2
647
+//					RangeMin = LisInfo.Freference
648
+//					RangeOptions = LisInfo.Fhint
649
+//				} else {
650
+//					Range := strings.Split(LisInfo.Freference, "-")
651
+//					if len(Range) > 1 {
652
+//						RangeMin = Range[0]
653
+//						RangeMax = Range[1]
654
+//						ItemType = 1
655
+//					} else {
656
+//						ItemType = 2
657
+//						RangeMin = LisInfo.Freference
658
+//					}
659
+//					RangeOptions = LisInfo.Fhint
660
+//				}
661
+//
662
+//				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
663
+//				if total <= 0 {
664
+//					inspection_reference.OrgId = org_id
665
+//					inspection_reference.ProjectName = LisInfo.Fhospnaturalitemname
666
+//					inspection_reference.Project = LisInfo.Fhospnaturalitem
667
+//					inspection_reference.ProjectId = project_id
668
+//					inspection_reference.ItemName = LisInfo.Fhospitemname
669
+//					inspection_reference.ItemNameAddition = LisInfo.Fhospitem
670
+//					inspection_reference.ItemId = item_id
671
+//					inspection_reference.RangeType = ItemType
672
+//					inspection_reference.RangeMin = RangeMin
673
+//					inspection_reference.RangeMax = RangeMax
674
+//					inspection_reference.RangeValue = LisInfo.Freference
675
+//					inspection_reference.RangeOptions = RangeOptions
676
+//					inspection_reference.Unit = LisInfo.Funit
677
+//					inspection_reference.Status = 1
678
+//					inspection_reference.CreatedTime = time.Now().Unix()
679
+//					inspection_reference.UpdatedTime = time.Now().Unix()
680
+//					inspection_reference.InspectDate = tempRecord
681
+//					inspection_reference.UTime = tempRecord
682
+//					err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
683
+//					if err != nil {
684
+//						tx.Rollback()
685
+//					}
686
+//				}
687
+//				var itotal int
688
+//				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
689
+//				if itotal <= 0 {
690
+//					// inspection.InspectValue = strings.Replace(LisInfo.Testresult, "&gt;", ">", -1)
691
+//					// inspection.InspectValue = strings.Replace(LiLisInfo.Testresult, "&lt;", "<", -1)
692
+//					inspection.PatientId = patient.ID
693
+//					inspection.OrgId = org_id
694
+//					inspection.ProjectId = project_id
695
+//					inspection.ItemName = inspection_reference.ItemName
696
+//					inspection.ProjectName = inspection_reference.ProjectName
697
+//					inspection.InspectType = ItemType
698
+//					inspection.ItemId = item_id
699
+//					inspection.InspectValue = LisInfo.Fresult
700
+//					inspection.InspectDate = inspect_date
701
+//					inspection.RecordDate = recordTimeInt
702
+//					inspection.InspectTips = LisInfo.Fhint
703
+//					inspection.Status = 1
704
+//					inspection.CreatedTime = time.Now().Unix()
705
+//					inspection.UpdatedTime = time.Now().Unix()
706
+//					// inspection.UTime = record_date.Format(timeLayout)
707
+//					// inspection.HisUserId = strconv.FormatInt(patient_id, 10)
708
+//					inspection.SysProjectId = inspection_reference.XtProjectId
709
+//					inspection.SysItemId = inspection_reference.XtItemId
710
+//					err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
711
+//					if err != nil {
712
+//						tx.Rollback()
713
+//					}
714
+//				}
715
+//				tx.Commit()
716
+//			} else {
717
+//				continue
718
+//			}
719
+//		}
720
+//	}
721
+//	var syncInfo models.MiddleSyncInfo
722
+//	syncInfo.OrgId = org_id
723
+//	syncInfo.SyncTime = time.Now().Unix()
724
+//	syncInfo.SyncResultType = 1
725
+//	syncInfo.SyncRsultRemark = "同步成功"
726
+//	syncInfo.SyncTotalNum = 0
727
+//	syncInfo.SyncSuccessNum = 0
728
+//	syncInfo.SyncInfo = ""
729
+//	syncInfo.CreateTime = time.Now().Unix()
730
+//	syncInfo.UpdateTime = time.Now().Unix()
731
+//
732
+//	cwderr := CreateSyncInfo(&syncInfo)
733
+//	if cwderr != nil {
734
+//		utils.ErrorLog("创建同步信息失败:%v", cwderr)
735
+//		return
736
+//	}
737
+//	SyncToGzkytx()
738
+//	return
739
+//}
740
+//
741
+//// func deleteMid(tx *gorm.DB) (err error) {
742
+//// 	err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock as t," +
743
+//// 		"(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
744
+//// 		"(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock  WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
745
+//// 		"WHERE t1.user_org_id = t2.orgid)tmp " +
746
+//// 		"SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
747
+//// 		"WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
748
+//// 	return
749
+//// }
750
+//
751
+//func SyncToGzkytx() {
752
+//
753
+//	utils.TraceLog("检验检查同步任务开始执行")
754
+//	org_id := int64(10567)
755
+//
756
+//	// 第一步:跟进org_id 去中间库查出需要同步的数据
757
+//	//inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
758
+//	inspections, _ := GetSyncInspectionByOrgId(org_id)
759
+//
760
+//	// 第二步:将数据同步到业务库
761
+//	//if len(inspection_references) > 0 {
762
+//	//	for _, inspection_reference := range inspection_references {
763
+//	//		SyncInspectionReference(&inspection_reference)
764
+//	//	}
765
+//	//}
766
+//
767
+//	if len(inspections) > 0 {
768
+//		for _, inspection := range inspections {
769
+//			if inspection.SysProjectId > 0 && inspection.SysItemId > 0 {
770
+//				SyncGzkyInspection(&inspection)
771
+//			}
772
+//		}
773
+//	}
774
+//	utils.SuccessLog("检验检查同步任务完成")
775
+//}
776
+//
777
+//// 从机构将数据同步到中间库
778
+//func SyncGzkyInspection(data *models.MiddleInspection) error {
779
+//	tx := writeDb.Begin()
780
+//	var inspection models.Inspection
781
+//	var total int
782
+//	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
783
+//	if total <= 0 {
784
+//
785
+//		inspection.OrgId = data.OrgId
786
+//		inspection.PatientId = data.PatientId
787
+//		inspection.ProjectName = data.ProjectName
788
+//		// inspection.Project = data.ProjectName
789
+//		inspection.ProjectId = data.SysProjectId
790
+//		inspection.ItemName = data.ItemName
791
+//		inspection.ItemId = data.SysItemId
792
+//		inspection.InspectType = int64(data.InspectType)
793
+//		inspection.InspectValue = data.InspectValue
794
+//		inspection.InspectTips = data.InspectTips
795
+//		inspection.InspectDate = data.RecordDate
796
+//		inspection.Status = 1
797
+//		inspection.CreatedTime = time.Now().Unix()
798
+//		inspection.UpdatedTime = time.Now().Unix()
799
+//
800
+//		err := tx.Model(&models.Inspection{}).Create(&inspection).Error
801
+//		if err != nil {
802
+//			tx.Rollback()
803
+//		}
804
+//
805
+//		data.IsSync = 1
806
+//		data.SyncId = inspection.ID
807
+//		data.UpdatedTime = time.Now().Unix()
808
+//		ierr := writeMiddleDb.Save(&data).Error
809
+//		if ierr != nil {
810
+//			tx.Rollback()
811
+//		}
812
+//	}
813
+//	tx.Commit()
814
+//	return err
815
+//}