gzky_service.go 12KB

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