jhhjyy_service.go 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. _ "encoding/json"
  6. _ "fmt"
  7. "github.com/jinzhu/gorm"
  8. "strings"
  9. "time"
  10. )
  11. type jhhjyyLis struct {
  12. HisUserId string `json:"his_user_id"`
  13. PatientName string `json:"patient_name"`
  14. ProjectId string `json:"project_id"`
  15. ProjectName string `json:"project_name"`
  16. InspectDate string `json:"inspect_date"`
  17. ItemId string `json:"item_id"`
  18. ItemName string `json:"item_name"`
  19. ItemType string `json:"item_type"`
  20. ItemValue string `json:"item_value"`
  21. RangeMin string `json:"range_min"`
  22. RangeMax string `json:"range_max"`
  23. RangeValue string `json:"range_value"`
  24. RangeOptions string `json:"range_options"`
  25. Unit string `json:"unit"`
  26. State string `json:"state"`
  27. CTime string `json:"c_time"`
  28. UTime string `json:"u_time"`
  29. }
  30. func (jhhjyyLis) TableName() string {
  31. return "GetJianYanXX"
  32. }
  33. func GetJhhjyyLis(rdb *gorm.DB, synctime string, his_user_id string, patient_name string) (record []*jhhjyyLis, err error) {
  34. err = rdb.Model(&jhhjyyLis{}).Where("c_time >= ? and (his_user_id = ? or patient_name = ?)", synctime, his_user_id, patient_name).Find(&record).Error
  35. return
  36. }
  37. //// 根据机构ID和患者身份证号码获取患者信息
  38. //func GetUserInIdcardNoByOrg(org_id int64) (patients []*models.Patients, err error) {
  39. // err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1 ", org_id).Find(&patients).Error
  40. // return
  41. //}
  42. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  43. func GetJhhjyyProjectID(org_id int64, project_name string, item_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 = ? and item_name = ? ", org_id, project_name, item_name).First(&inspection_reference).Error
  46. if inspection_reference.ID > 0 {
  47. return inspection_reference.ProjectId, err
  48. } else {
  49. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project = ? ", org_id, project_name).First(&inspection_reference).Error
  50. if inspection_reference.ID > 0 {
  51. return inspection_reference.ProjectId, err
  52. } else {
  53. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  54. if inspection_reference.ProjectId > 0 {
  55. return inspection_reference.ProjectId + 1, err
  56. } else {
  57. return 1062901, err
  58. }
  59. }
  60. }
  61. }
  62. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  63. func GetJhhjyyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  64. var inspection_reference models.MiddleInspectionReference
  65. 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
  66. if inspection_reference.ID > 0 {
  67. return inspection_reference.ItemId, err
  68. } else {
  69. 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
  70. if inspection_reference.ItemId > 0 {
  71. return inspection_reference.ItemId + 1, err
  72. } else {
  73. return project_id*100 + 1, err
  74. }
  75. }
  76. }
  77. // 湖南郴州嘉禾恒佳医院Lis同步
  78. func SyncJhhjyyLis() (err error) {
  79. org_id := int64(10629)
  80. org := &models.DataUploadConfig{
  81. OrgId: org_id,
  82. DbHost: "192.168.5.253",
  83. DbPort: "1433",
  84. DbPass: "XtUser",
  85. DbUser: "XtUser",
  86. DbName: "HealthOne",
  87. }
  88. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  89. if err != nil {
  90. utils.ErrorLog("创建数据库连接失败:%v", err)
  91. return
  92. }
  93. // 第一步:获取上一次同步的时间点
  94. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  95. var sync_time int64
  96. if syncLastInfo.ID > 0 {
  97. sync_time = syncLastInfo.SyncTime
  98. } else {
  99. sync_time = 1704038400 // 2023-01-01
  100. }
  101. // 第二步:获取时间内所有检验结果
  102. sync_time_temp := time.Unix(sync_time, 0)
  103. startTimeStr := sync_time_temp.Format("2006-01-02 15:04:05")
  104. patients, _ := GetUserInIdcardNoByOrg(org_id)
  105. if len(patients) > 0 {
  106. for _, patient := range patients {
  107. userCardId := patient.IdCardNo
  108. // 根据姓名获取患者ID
  109. jhhyyyLis, _ := GetJhhjyyLis(orgDb, startTimeStr, userCardId, patient.Name)
  110. if len(jhhyyyLis) > 0 {
  111. for _, LisInfo := range jhhyyyLis {
  112. project_id, _ := GetJhhjyyProjectID(org_id, LisInfo.ProjectName, LisInfo.ItemName)
  113. item_id, _ := GetJhhjyyItemID(org_id, LisInfo.ProjectName, LisInfo.ItemName, project_id)
  114. tx := writeMiddleDb.Begin()
  115. var inspection models.MiddleInspection
  116. var inspection_reference models.MiddleInspectionReference
  117. timeLayout := "2006-01-02 15:04"
  118. loc, _ := time.LoadLocation("Local")
  119. utils.InfoLog("1111111---%v", LisInfo.InspectDate)
  120. timstr := strings.Split(LisInfo.InspectDate, "T")
  121. timstem := strings.Split(timstr[1], "Z")
  122. tempRecord := timstr[0] + " " + timstem[0]
  123. utils.InfoLog("2222222---%v", tempRecord)
  124. recordTime, _ := time.ParseInLocation("2006-01-02 15:04:05", tempRecord, loc)
  125. recordTimeInt := recordTime.Unix()
  126. recordTimeStr := time.Unix(recordTimeInt, 0).Format(timeLayout)
  127. var total int
  128. var RangeOptions string
  129. var RangeMin string
  130. var RangeMax string
  131. var ItemType int
  132. Range := strings.Split(LisInfo.RangeValue, "-")
  133. if len(Range) > 1 {
  134. RangeMin = Range[0]
  135. RangeMax = Range[1]
  136. ItemType = 1
  137. } else {
  138. ItemType = 2
  139. RangeOptions = LisInfo.RangeValue
  140. }
  141. 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
  142. if total <= 0 {
  143. inspection_reference.OrgId = org_id
  144. inspection_reference.ProjectName = LisInfo.ProjectName
  145. inspection_reference.Project = LisInfo.ProjectName
  146. inspection_reference.ProjectId = project_id
  147. inspection_reference.ItemName = LisInfo.ItemName
  148. // inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  149. inspection_reference.ItemId = item_id
  150. inspection_reference.RangeType = ItemType
  151. inspection_reference.RangeMin = RangeMin
  152. inspection_reference.RangeMax = RangeMax
  153. // inspection_reference.RangeValue = RangeValue
  154. inspection_reference.RangeOptions = RangeOptions
  155. inspection_reference.Unit = LisInfo.Unit
  156. inspection_reference.Status = 1
  157. inspection_reference.CreatedTime = time.Now().Unix()
  158. inspection_reference.UpdatedTime = time.Now().Unix()
  159. inspection_reference.InspectDate = recordTimeStr
  160. inspection_reference.UTime = recordTimeStr
  161. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  162. if err != nil {
  163. tx.Rollback()
  164. }
  165. }
  166. var itotal int
  167. 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
  168. if itotal <= 0 {
  169. // inspection.InspectValue = strings.Replace(LisInfo.Testresult, "&gt;", ">", -1)
  170. // inspection.InspectValue = strings.Replace(LiLisInfo.Testresult, "&lt;", "<", -1)
  171. inspection.PatientId = patient.ID
  172. inspection.OrgId = org_id
  173. inspection.ProjectId = project_id
  174. inspection.ItemName = inspection_reference.ItemName
  175. inspection.ProjectName = inspection_reference.ProjectName
  176. inspection.InspectType = ItemType
  177. inspection.ItemId = item_id
  178. inspection.InspectValue = LisInfo.ItemValue
  179. inspection.InspectDate = recordTimeStr
  180. inspection.RecordDate = recordTimeInt
  181. // inspection.InspectTips = report.Resultstate
  182. inspection.Status = 1
  183. inspection.CreatedTime = time.Now().Unix()
  184. inspection.UpdatedTime = time.Now().Unix()
  185. // inspection.UTime = record_date.Format(timeLayout)
  186. // inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  187. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  188. if err != nil {
  189. tx.Rollback()
  190. }
  191. }
  192. tx.Commit()
  193. }
  194. } else {
  195. continue
  196. }
  197. }
  198. var syncInfo models.MiddleSyncInfo
  199. syncInfo.OrgId = org_id
  200. syncInfo.SyncTime = time.Now().Unix()
  201. syncInfo.SyncResultType = 1
  202. syncInfo.SyncRsultRemark = "同步成功"
  203. syncInfo.SyncTotalNum = 0
  204. syncInfo.SyncSuccessNum = 0
  205. syncInfo.SyncInfo = ""
  206. syncInfo.CreateTime = time.Now().Unix()
  207. syncInfo.UpdateTime = time.Now().Unix()
  208. cwderr := CreateSyncInfo(&syncInfo)
  209. if cwderr != nil {
  210. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  211. return
  212. }
  213. }
  214. SyncToJhhjyy()
  215. return
  216. }
  217. func SyncToJhhjyy() {
  218. utils.TraceLog("检验检查同步任务开始执行")
  219. org_id := int64(10629)
  220. // 第一步:跟进org_id 去中间库查出需要同步的数据
  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. utils.SuccessLog("检验检查同步任务完成")
  235. }