pljx_service.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. "github.com/jinzhu/gorm"
  6. "strings"
  7. "time"
  8. )
  9. type pljxLisReportResult struct {
  10. Itemcode string `gorm:"column:itemcode" json:"itemcode"`
  11. Itemname string `gorm:"column:itemname" json:"itemname"`
  12. Result string `gorm:"column:result" json:"result"`
  13. Unit string `gorm:"column:unit" json:"unit"`
  14. Refrange string `gorm:"column:refrange" json:"refrange"`
  15. Ts string `gorm:"column:ts" json:"ts"`
  16. Repdate string `gorm:"column:repdate" json:"repdate"`
  17. Name string `gorm:"column:name" json:"name"`
  18. DockingapplyitemName string `gorm:"column:DockingApplyItemName" json:"DockingApplyItemName"`
  19. Sex string `gorm:"column:sex" json:"sex"`
  20. Age string `gorm:"column:age" json:"age"`
  21. }
  22. func (pljxLisReportResult) TableName() string {
  23. return "v_cm_result"
  24. }
  25. func GetPljxLisReport(rdb *gorm.DB, synctime string) (record []*pljxLisReportResult, err error) {
  26. err = rdb.Model(&pljxLisReportResult{}).Where("repdate >= ?", synctime).Find(&record).Error
  27. return
  28. }
  29. // 普宁锦熹血液透析中心Lis同步
  30. func SyncLisPljx(org_id int64) (err error) {
  31. org := &models.DataUploadConfig{
  32. OrgId: org_id,
  33. DbHost: "localhost",
  34. DbPort: "1433",
  35. DbPass: "1Q2W3e4r!@#$",
  36. DbUser: "kyy",
  37. DbName: "hyDB",
  38. }
  39. if org_id == 10599 {
  40. org.DbName = "hy_pndb"
  41. }
  42. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  43. if err != nil {
  44. utils.ErrorLog("创建数据库连接失败:%v", err)
  45. return
  46. }
  47. // 第一步:获取上一次同步的时间点
  48. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  49. var sync_time int64
  50. if syncLastInfo.ID > 0 {
  51. sync_time = syncLastInfo.SyncTime
  52. } else {
  53. sync_time = 1698768000 // 2023-01-01
  54. }
  55. SyncTime := time.Unix(sync_time, 0)
  56. syncTimeStr := SyncTime.Format("2006-01-02 15:04:05")
  57. // 第二步:获取时间内所有检验结果
  58. pljxLisResult, _ := GetPljxLisReport(orgDb, syncTimeStr)
  59. if len(pljxLisResult) > 0 {
  60. for _, Result := range pljxLisResult {
  61. utils.InfoLog("Report:%v", Result)
  62. patient, _ := GetUserInfoByName(org_id, Result.Name)
  63. if patient.ID > 0 {
  64. project_id, _ := GetPljxProjectID(org_id, Result.DockingapplyitemName, Result.Itemname)
  65. item_id, _ := GetPljxItemID(org_id, Result.DockingapplyitemName, Result.Itemname, project_id)
  66. tx := writeMiddleDb.Begin()
  67. var inspection models.MiddleInspection
  68. var inspection_reference models.MiddleInspectionReference
  69. tempTime := strings.Split(Result.Repdate, "T")
  70. tempTime1 := strings.Split(tempTime[1], "Z")
  71. tempRecord := tempTime[0] + " " + tempTime1[0]
  72. recordTime, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", tempRecord)
  73. inspect_date := recordTime.Format("2006-01-02 15:04")
  74. recordTimeStr, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", inspect_date)
  75. recordTimeInt := recordTimeStr.Unix()
  76. //tempTime := strings.Split(Result.Time, "T")
  77. //tempTime1 := strings.Split(tempTime[1], "+")
  78. //tempRecord := tempTime[0] + " " + tempTime1[0]
  79. //recordTime, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", tempRecord)
  80. //inspect_date := recordTime.Format("2006-01-02 15:04")
  81. //recordTimeStr, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", inspect_date)
  82. //recordTimeInt := recordTimeStr.Unix()
  83. var total int
  84. var RangeOptions string
  85. var RangeMin string
  86. var RangeMax string
  87. var ItemType int
  88. Range := strings.Split(Result.Refrange, "-")
  89. if len(Range) > 1 {
  90. Range1 := strings.Split(Range[0], "[")
  91. if len(Range1) > 1 {
  92. Range2 := strings.Split(Range[1], "]")
  93. RangeMin = Range1[1]
  94. RangeMax = Range2[0]
  95. ItemType = 1
  96. } else {
  97. ItemType = 2
  98. RangeMin = Result.Refrange
  99. }
  100. } else {
  101. ItemType = 2
  102. RangeMin = Result.Refrange
  103. }
  104. RangeOptions = Result.Ts
  105. 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
  106. if total <= 0 {
  107. inspection_reference.OrgId = org_id
  108. inspection_reference.ProjectName = Result.DockingapplyitemName
  109. inspection_reference.Project = Result.DockingapplyitemName
  110. inspection_reference.ProjectId = project_id
  111. inspection_reference.ItemName = Result.Itemname
  112. inspection_reference.ItemNameAddition = Result.Itemcode
  113. inspection_reference.ItemId = item_id
  114. inspection_reference.RangeType = ItemType
  115. inspection_reference.RangeMin = RangeMin
  116. inspection_reference.RangeMax = RangeMax
  117. inspection_reference.Unit = Result.Unit
  118. inspection_reference.Status = 1
  119. inspection_reference.CreatedTime = time.Now().Unix()
  120. inspection_reference.UpdatedTime = time.Now().Unix()
  121. inspection_reference.InspectDate = tempRecord
  122. inspection_reference.UTime = tempRecord
  123. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  124. if err != nil {
  125. tx.Rollback()
  126. }
  127. }
  128. var itotal int
  129. 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
  130. if itotal <= 0 {
  131. // inspection.InspectValue = strings.Replace(LisInfo.Testresult, "&gt;", ">", -1)
  132. // inspection.InspectValue = strings.Replace(LiLisInfo.Testresult, "&lt;", "<", -1)
  133. inspection.PatientId = patient.ID
  134. inspection.OrgId = org_id
  135. inspection.ProjectId = project_id
  136. inspection.ItemName = inspection_reference.ItemName
  137. inspection.ProjectName = inspection_reference.ProjectName
  138. inspection.InspectType = ItemType
  139. inspection.ItemId = item_id
  140. inspection.InspectValue = Result.Result
  141. inspection.InspectDate = inspect_date
  142. inspection.RecordDate = recordTimeInt
  143. inspection.InspectTips = RangeOptions
  144. inspection.Status = 1
  145. inspection.CreatedTime = time.Now().Unix()
  146. inspection.UpdatedTime = time.Now().Unix()
  147. // inspection.UTime = record_date.Format(timeLayout)
  148. // inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  149. inspection.SysProjectId = inspection_reference.XtProjectId
  150. inspection.SysItemId = inspection_reference.XtItemId
  151. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  152. if err != nil {
  153. tx.Rollback()
  154. }
  155. }
  156. tx.Commit()
  157. } else {
  158. continue
  159. }
  160. }
  161. var syncInfo models.MiddleSyncInfo
  162. syncInfo.OrgId = org_id
  163. syncInfo.SyncTime = time.Now().Unix()
  164. syncInfo.SyncResultType = 1
  165. syncInfo.SyncRsultRemark = "同步成功"
  166. syncInfo.SyncTotalNum = 0
  167. syncInfo.SyncSuccessNum = 0
  168. syncInfo.SyncInfo = ""
  169. syncInfo.CreateTime = time.Now().Unix()
  170. syncInfo.UpdateTime = time.Now().Unix()
  171. cwderr := CreateSyncInfo(&syncInfo)
  172. if cwderr != nil {
  173. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  174. return
  175. }
  176. }
  177. SyncToPljx(org_id)
  178. return
  179. }
  180. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  181. func GetPljxProjectID(org_id int64, project_name string, item_name string) (project_id int64, err error) {
  182. var inspection_reference models.MiddleInspectionReference
  183. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project = ? and item_name = ? ", org_id, project_name, item_name).First(&inspection_reference).Error
  184. if inspection_reference.ID > 0 {
  185. return inspection_reference.ProjectId, err
  186. } else {
  187. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project = ? ", org_id, project_name).First(&inspection_reference).Error
  188. if inspection_reference.ID > 0 {
  189. return inspection_reference.ProjectId, err
  190. } else {
  191. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  192. if inspection_reference.ProjectId > 0 {
  193. return inspection_reference.ProjectId + 1, err
  194. } else {
  195. if org_id == 10597 {
  196. return 1059901, err
  197. } else {
  198. return 1059701, err
  199. }
  200. }
  201. }
  202. }
  203. }
  204. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  205. func GetPljxItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  206. var inspection_reference models.MiddleInspectionReference
  207. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project = ? and item_name = ?", org_id, project_name, item_name).First(&inspection_reference).Error
  208. if inspection_reference.ID > 0 {
  209. return inspection_reference.ItemId, err
  210. } else {
  211. 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
  212. if inspection_reference.ItemId > 0 {
  213. return inspection_reference.ItemId + 1, err
  214. } else {
  215. return project_id*100 + 1, err
  216. }
  217. }
  218. }
  219. func SyncToPljx(org_id int64) {
  220. utils.TraceLog("检验检查同步任务开始执行")
  221. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  222. inspections, _ := GetSyncInspectionByOrgId(org_id)
  223. // 第二步:将数据同步到业务库
  224. if len(inspection_references) > 0 {
  225. for _, inspection_reference := range inspection_references {
  226. SyncInspectionReference(&inspection_reference)
  227. }
  228. }
  229. if len(inspections) > 0 {
  230. for _, inspection := range inspections {
  231. SyncInspection(&inspection)
  232. }
  233. }
  234. //if len(inspections) > 0 {
  235. // for _, inspection := range inspections {
  236. // if inspection.SysProjectId > 0 && inspection.SysItemId > 0 {
  237. // SyncPljxInspection(&inspection)
  238. // }
  239. // }
  240. //}
  241. utils.SuccessLog("检验检查同步任务完成")
  242. }
  243. // 从机构将数据同步到中间库
  244. func SyncPljxInspection(data *models.MiddleInspection) error {
  245. tx := writeDb.Begin()
  246. var inspection models.Inspection
  247. var total int
  248. 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
  249. if total <= 0 {
  250. inspection.OrgId = data.OrgId
  251. inspection.PatientId = data.PatientId
  252. inspection.ProjectName = data.ProjectName
  253. // inspection.Project = data.ProjectName
  254. inspection.ProjectId = data.SysProjectId
  255. inspection.ItemName = data.ItemName
  256. inspection.ItemId = data.SysItemId
  257. inspection.InspectType = int64(data.InspectType)
  258. inspection.InspectValue = data.InspectValue
  259. inspection.InspectTips = data.InspectTips
  260. inspection.InspectDate = data.RecordDate
  261. inspection.Status = 1
  262. inspection.CreatedTime = time.Now().Unix()
  263. inspection.UpdatedTime = time.Now().Unix()
  264. err := tx.Model(&models.Inspection{}).Create(&inspection).Error
  265. if err != nil {
  266. tx.Rollback()
  267. }
  268. data.IsSync = 1
  269. data.SyncId = inspection.ID
  270. data.UpdatedTime = time.Now().Unix()
  271. ierr := writeMiddleDb.Save(&data).Error
  272. if ierr != nil {
  273. tx.Rollback()
  274. }
  275. }
  276. tx.Commit()
  277. return err
  278. }