szwz_service.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. oracle "github.com/dzwvip/gorm-oracle"
  6. "gorm.io/gorm"
  7. "strings"
  8. "time"
  9. )
  10. type wzLisReport struct {
  11. Repno string `gorm:"column:REPNO" json:"REPNO"`
  12. Repname string `gorm:"column:REPNAME" json:"REPNAME"`
  13. NAME string `gorm:"column:NAME" json:"NAME"`
  14. Appdeptname string `gorm:"column:APPDEPTNAME" json:"APPDEPTNAME"`
  15. Firstaudittime string `gorm:"column:FIRSTAUDITTIME" json:"FIRSTAUDITTIME"`
  16. }
  17. func (wzLisReport) TableName() string {
  18. return "V_LIS_REPORT"
  19. }
  20. type wzLisReportResult struct {
  21. Repno string `gorm:"column:REPNO" json:"REPNO"`
  22. Itemid string `gorm:"column:ITEMID" json:"ITEMID"`
  23. Itemcname string `gorm:"column:ITEMCNAME" json:"ITEMCNAME"`
  24. Itemename string `gorm:"column:ITEMENAME" json:"ITEMENAME"`
  25. Result string `gorm:"column:RESULT" json:"RESULT"`
  26. Unit string `gorm:"column:UNIT" json:"UNIT"`
  27. Prompt string `gorm:"column:PROMPT" json:"PROMPT"`
  28. Refmax string `gorm:"column:REFMAX" json:"REFMAX"`
  29. Refmin string `gorm:"column:REFMIN" json:"REFMIN"`
  30. Time string `gorm:"column:TIME" json:"TIME"`
  31. }
  32. func (wzLisReportResult) TableName() string {
  33. return "V_LIS_REPORT_RESULT"
  34. }
  35. func GetSzwzyLisReport(rdb *gorm.DB, synctime string) (record []*wzLisReport, err error) {
  36. err = rdb.Model(&wzLisReport{}).Where("FIRSTAUDITTIME >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') and APPDEPTNAME = '肾病科'", synctime).Find(&record).Error
  37. return
  38. }
  39. func GetSzwzyLisReportResule(rdb *gorm.DB, repno string) (record []*wzLisReportResult, err error) {
  40. err = rdb.Model(&wzLisReportResult{}).Where("REPNO = ? ", repno).Find(&record).Error
  41. return
  42. }
  43. // 深圳五洲中医院Lis同步
  44. func SyncLisSzwz() (err error) {
  45. org_id := int64(10580)
  46. orgDb, err := gorm.Open(oracle.Open("oracle://BSLIS52:zhsoft@172.9.200.3:1521/JYK"), &gorm.Config{})
  47. if err != nil {
  48. utils.InfoLog("syncTimeStr:%v", err)
  49. }
  50. // 第一步:获取上一次同步的时间点
  51. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  52. var sync_time int64
  53. if syncLastInfo.ID > 0 {
  54. sync_time = syncLastInfo.SyncTime
  55. } else {
  56. sync_time = 1672502400 // 2023-01-01
  57. }
  58. SyncTime := time.Unix(sync_time, 0)
  59. syncTimeStr := SyncTime.Format("2006-01-02 15:04:05")
  60. utils.InfoLog("syncTimeStr:%v", syncTimeStr)
  61. // 第二步:获取时间内所有检验结果
  62. szwzLisReport, _ := GetSzwzyLisReport(orgDb, syncTimeStr)
  63. if len(szwzLisReport) > 0 {
  64. for _, Report := range szwzLisReport {
  65. utils.InfoLog("Report:%v", Report)
  66. if len(Report.Repname) <= 0 {
  67. Report.Repname = "未知"
  68. }
  69. patient, _ := GetUserInfoByName(org_id, Report.NAME)
  70. if patient.ID > 0 {
  71. // 第三步,根据REPNO查询对应的检查小项
  72. szwzLisReportResult, _ := GetSzwzyLisReportResule(orgDb, Report.Repno)
  73. if len(szwzLisReportResult) > 0 {
  74. for _, Result := range szwzLisReportResult {
  75. project_id, _ := GetSzwzProjectID(org_id, Report.Repname)
  76. item_id, _ := GetSzwzItemID(org_id, Report.Repname, Result.Itemcname, project_id)
  77. tx := writeMiddleDb.Begin()
  78. var inspection models.MiddleInspection
  79. var inspection_reference models.MiddleInspectionReference
  80. tempTime := strings.Split(Result.Time, "T")
  81. tempTime1 := strings.Split(tempTime[1], "+")
  82. tempRecord := tempTime[0] + " " + tempTime1[0]
  83. recordTime, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", tempRecord)
  84. inspect_date := recordTime.Format("2006-01-02 15:04")
  85. recordTimeStr, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", inspect_date)
  86. recordTimeInt := recordTimeStr.Unix()
  87. var total int
  88. var RangeOptions string
  89. var RangeMin string
  90. var RangeMax string
  91. var ItemType int
  92. RangeMin = Result.Refmin
  93. RangeMax = Result.Refmax
  94. ItemType = 1
  95. RangeOptions = Result.Prompt
  96. 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
  97. if total <= 0 {
  98. inspection_reference.OrgId = org_id
  99. inspection_reference.ProjectName = Report.Repname
  100. inspection_reference.Project = Report.Repname
  101. inspection_reference.ProjectId = project_id
  102. inspection_reference.ItemName = Result.Itemcname
  103. inspection_reference.ItemNameAddition = Result.Itemename
  104. inspection_reference.ItemId = item_id
  105. inspection_reference.RangeType = ItemType
  106. inspection_reference.RangeMin = RangeMin
  107. inspection_reference.RangeMax = RangeMax
  108. inspection_reference.Unit = Result.Unit
  109. inspection_reference.Status = 1
  110. inspection_reference.CreatedTime = time.Now().Unix()
  111. inspection_reference.UpdatedTime = time.Now().Unix()
  112. inspection_reference.InspectDate = tempRecord
  113. inspection_reference.UTime = tempRecord
  114. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  115. if err != nil {
  116. tx.Rollback()
  117. }
  118. }
  119. var itotal int
  120. 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
  121. if itotal <= 0 {
  122. // inspection.InspectValue = strings.Replace(LisInfo.Testresult, "&gt;", ">", -1)
  123. // inspection.InspectValue = strings.Replace(LiLisInfo.Testresult, "&lt;", "<", -1)
  124. inspection.PatientId = patient.ID
  125. inspection.OrgId = org_id
  126. inspection.ProjectId = project_id
  127. inspection.ItemName = inspection_reference.ItemName
  128. inspection.ProjectName = inspection_reference.ProjectName
  129. inspection.InspectType = ItemType
  130. inspection.ItemId = item_id
  131. inspection.InspectValue = Result.Result
  132. inspection.InspectDate = inspect_date
  133. inspection.RecordDate = recordTimeInt
  134. inspection.InspectTips = RangeOptions
  135. inspection.Status = 1
  136. inspection.CreatedTime = time.Now().Unix()
  137. inspection.UpdatedTime = time.Now().Unix()
  138. // inspection.UTime = record_date.Format(timeLayout)
  139. // inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  140. inspection.SysProjectId = inspection_reference.XtProjectId
  141. inspection.SysItemId = inspection_reference.XtItemId
  142. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  143. if err != nil {
  144. tx.Rollback()
  145. }
  146. }
  147. tx.Commit()
  148. }
  149. }
  150. } else {
  151. continue
  152. }
  153. }
  154. }
  155. var syncInfo models.MiddleSyncInfo
  156. syncInfo.OrgId = org_id
  157. syncInfo.SyncTime = time.Now().Unix()
  158. syncInfo.SyncResultType = 1
  159. syncInfo.SyncRsultRemark = "同步成功"
  160. syncInfo.SyncTotalNum = 0
  161. syncInfo.SyncSuccessNum = 0
  162. syncInfo.SyncInfo = ""
  163. syncInfo.CreateTime = time.Now().Unix()
  164. syncInfo.UpdateTime = time.Now().Unix()
  165. cwderr := CreateSyncInfo(&syncInfo)
  166. if cwderr != nil {
  167. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  168. return
  169. }
  170. SyncToSzwz()
  171. return
  172. }
  173. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  174. func GetSzwzProjectID(org_id int64, project_name string) (project_id int64, err error) {
  175. var inspection_reference models.MiddleInspectionReference
  176. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ? ", org_id, project_name).First(&inspection_reference).Error
  177. if inspection_reference.ID > 0 {
  178. return inspection_reference.ProjectId, err
  179. } else {
  180. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  181. if inspection_reference.ProjectId > 0 {
  182. return inspection_reference.ProjectId + 1, err
  183. } else {
  184. return 1058001, err
  185. }
  186. }
  187. }
  188. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  189. func GetSzwzItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  190. var inspection_reference models.MiddleInspectionReference
  191. 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
  192. if inspection_reference.ID > 0 {
  193. return inspection_reference.ItemId, err
  194. } else {
  195. 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
  196. if inspection_reference.ItemId > 0 {
  197. return inspection_reference.ItemId + 1, err
  198. } else {
  199. return project_id*100 + 1, err
  200. }
  201. }
  202. }
  203. func SyncToSzwz() {
  204. utils.TraceLog("检验检查同步任务开始执行")
  205. org_id := int64(10580)
  206. inspections, _ := GetSyncInspectionByOrgId(org_id)
  207. if len(inspections) > 0 {
  208. for _, inspection := range inspections {
  209. if inspection.SysProjectId > 0 && inspection.SysItemId > 0 {
  210. SyncSzwzInspection(&inspection)
  211. }
  212. }
  213. }
  214. utils.SuccessLog("检验检查同步任务完成")
  215. }
  216. // 从机构将数据同步到中间库
  217. func SyncSzwzInspection(data *models.MiddleInspection) error {
  218. tx := writeDb.Begin()
  219. var inspection models.Inspection
  220. var total int
  221. 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
  222. if total <= 0 {
  223. inspection.OrgId = data.OrgId
  224. inspection.PatientId = data.PatientId
  225. inspection.ProjectName = data.ProjectName
  226. // inspection.Project = data.ProjectName
  227. inspection.ProjectId = data.SysProjectId
  228. inspection.ItemName = data.ItemName
  229. inspection.ItemId = data.SysItemId
  230. inspection.InspectType = int64(data.InspectType)
  231. inspection.InspectValue = data.InspectValue
  232. inspection.InspectTips = data.InspectTips
  233. inspection.InspectDate = data.RecordDate
  234. inspection.Status = 1
  235. inspection.CreatedTime = time.Now().Unix()
  236. inspection.UpdatedTime = time.Now().Unix()
  237. err := tx.Model(&models.Inspection{}).Create(&inspection).Error
  238. if err != nil {
  239. tx.Rollback()
  240. }
  241. data.IsSync = 1
  242. data.SyncId = inspection.ID
  243. data.UpdatedTime = time.Now().Unix()
  244. ierr := writeMiddleDb.Save(&data).Error
  245. if ierr != nil {
  246. tx.Rollback()
  247. }
  248. }
  249. tx.Commit()
  250. return err
  251. }