sccdkfyy_service.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. "github.com/jinzhu/gorm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. // 四川城东康复医院Lis同步
  11. func SyncSccdkfyyLis() (err error) {
  12. org_id := int64(10551)
  13. org := &models.DataUploadConfig{
  14. OrgId: org_id,
  15. DbHost: "192.168.1.5",
  16. DbPort: "1433",
  17. DbPass: "XTXT_User",
  18. DbUser: "XTXT_User",
  19. DbName: "ndlmis15",
  20. }
  21. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  22. if err != nil {
  23. utils.ErrorLog("创建数据库连接失败:%v", err)
  24. return
  25. }
  26. // var sync_time int64
  27. // sync_time = 1627747200
  28. // scpaLis, _ := GetScpaLis(orgDb, sync_time,"512926196302182682")
  29. // utils.InfoLog("IdCardNo:%v",scpaLis)
  30. // 第一步:获取上一次同步的plug_plugcase的ID
  31. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  32. var sync_time int64
  33. if syncLastInfo.ID > 0 {
  34. sync_time = syncLastInfo.SyncTime
  35. } else {
  36. sync_time = 0
  37. }
  38. sync_time_temp := time.Unix(sync_time, 0)
  39. syncTimeStr := sync_time_temp.Format("2006-01-02 15:04:05")
  40. // 第二步:获取时间内所有检验结果
  41. sccdLis, _ := GetScdckfyyLis(orgDb, syncTimeStr)
  42. if len(sccdLis) > 0 {
  43. for _, item := range sccdLis {
  44. printInfo, _ := GetPatientsInfoBySfzh(item.Sfzh, 10551)
  45. if printInfo.ID == 0 {
  46. continue
  47. }
  48. // result 里查询
  49. antiRresult,_ := GetScCdkfyyAnti(orgDb,item.TestOrderNo)
  50. if len(antiRresult) > 0 {
  51. for _,anti := range antiRresult {
  52. LgthScdckfyySync(anti,item, org_id, printInfo.ID)
  53. }
  54. }
  55. }
  56. }
  57. // 第二步:获取所有患者的病历号
  58. // patientList, _ := GetPatientGzjhByOrgId(org_id)
  59. // if len(patientList) > 0 {
  60. // for _, patient := range patientList {
  61. // if len(patient.IdCardNo) > 0 {
  62. // patient_id := patient.ID
  63. // utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
  64. // scdcCaseList, _ := GetScCdkfyyCase(orgDb, sync_time,lastId, patient.IdCardNo)
  65. // // 如果存在纪录,再在plug_plugCheckitem里根据信息查询检验信息
  66. // if len(scdcCaseList) > 0 {
  67. // for _, info := range scdcCaseList {
  68. // utils.InfoLog("Result:%v", info)
  69. // scdcItemList,_ := GetScCdkfyyCheckItem(orgDb,info.Serialno)
  70. // //根据item里的数据,查询检验结果
  71. // if len(scdcItemList) > 0 {
  72. // for _,item := range scdcItemList {
  73. // resultList,_ := GetScCdkfyyResult(orgDb,item.Serialno,item.Itemcode)
  74. // if len(resultList) > 0 {
  75. // for _ ,resultinfo := range resultList{
  76. // LgthScdckfyySync(resultinfo,item, org_id, patient_id)
  77. // }
  78. // }
  79. // }
  80. // }
  81. // }
  82. // }
  83. // }
  84. // }
  85. // }
  86. var syncInfo models.MiddleSyncInfo
  87. syncInfo.OrgId = org_id
  88. syncInfo.SyncTime = time.Now().Unix()
  89. syncInfo.SyncResultType = 1
  90. syncInfo.SyncRsultRemark = "同步成功"
  91. syncInfo.SyncTotalNum = 0
  92. syncInfo.SyncSuccessNum = 0
  93. syncInfo.SyncInfo = ""
  94. syncInfo.CreateTime = time.Now().Unix()
  95. syncInfo.UpdateTime = time.Now().Unix()
  96. cwderr := CreateSyncInfo(&syncInfo)
  97. if cwderr != nil {
  98. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  99. return
  100. }
  101. SyncToSccdkfyy()
  102. return
  103. }
  104. func SyncToSccdkfyy() {
  105. utils.TraceLog("检验检查同步任务开始执行")
  106. org_id := int64(10551)
  107. // 第一步:跟进org_id 去中间库查出需要同步的数据
  108. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  109. inspections, _ := GetSyncInspectionByOrgId(org_id)
  110. // 第二步:将数据同步到业务库
  111. if len(inspection_references) > 0 {
  112. for _, inspection_reference := range inspection_references {
  113. SyncInspectionReference(&inspection_reference)
  114. }
  115. }
  116. if len(inspections) > 0 {
  117. for _, inspection := range inspections {
  118. SyncInspection(&inspection)
  119. }
  120. }
  121. utils.SuccessLog("检验检查同步任务完成")
  122. }
  123. func GetScCdkfyyAnti(rdb *gorm.DB, test_order_no string) (record []*models.VYunjingPatientLabAnti, err error) {
  124. err = rdb.Model(&models.VYunjingPatientLabAnti{}).Where("TEST_ORDER_NO = ?", test_order_no).Find(&record).Error
  125. return
  126. }
  127. func GetPatientsInfoBySfzh(sfzh string, org_id int64) (info models.Patients, err error) {
  128. err = readDb.Model(&models.Patients{}).Where("id_card_no = ? AND user_org_id = ?", sfzh, org_id).First(&info).Error
  129. return
  130. }
  131. func GetScdckfyyLis(rdb *gorm.DB, synctime string) (record []*models.VYunjingPatientLabItems, err error) {
  132. err = rdb.Model(&models.VYunjingPatientLabItems{}).Where("CREATE_AT >= ? ", synctime).Find(&record).Error
  133. return
  134. }
  135. func SyncToScdckfyytx() {
  136. utils.TraceLog("检验检查同步任务开始执行")
  137. org_id := int64(10551)
  138. // 第一步:跟进org_id 去中间库查出需要同步的数据
  139. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  140. inspections, _ := GetSyncInspectionByOrgId(org_id)
  141. // 第二步:将数据同步到业务库
  142. if len(inspection_references) > 0 {
  143. for _, inspection_reference := range inspection_references {
  144. SyncInspectionReference(&inspection_reference)
  145. }
  146. }
  147. if len(inspections) > 0 {
  148. for _, inspection := range inspections {
  149. SyncInspection(&inspection)
  150. }
  151. }
  152. utils.SuccessLog("检验检查同步任务完成")
  153. }
  154. // 从机构将数据同步到中间库
  155. func LgthScdckfyySync(result *models.VYunjingPatientLabAnti, checkItem *models.VYunjingPatientLabItems, org_id int64, patient_id int64) (*models.MiddleInspectionReference, error) {
  156. tx := writeMiddleDb.Begin()
  157. var inspection models.MiddleInspection
  158. var inspection_reference models.MiddleInspectionReference
  159. timstr := strings.Split(checkItem.CreateAt, "T")
  160. timstem := strings.Split(timstr[1], "Z")
  161. recordDateStr := timstr[0] + " " + timstem[0]
  162. utils.InfoLog("recordDateStr: %v", recordDateStr)
  163. if len(recordDateStr) == 0 {
  164. recordDateStr = time.Now().Format("2006-01-02 15:04")
  165. }
  166. record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  167. utils.InfoLog("record_date_str: %v", record_date_str)
  168. record_date := record_date_str.Unix()
  169. utils.InfoLog("record_date: %v", record_date)
  170. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  171. utils.InfoLog("inspect_date: %v", inspect_date)
  172. var total int
  173. var RangeOptions string
  174. var RangeMin string
  175. var RangeMax string
  176. // // 判断检查类型
  177. ItemType := 1
  178. Range := strings.Split(result.ReferRange, "-")
  179. if len(Range) > 1 {
  180. RangeMin = Range[0]
  181. RangeMax = Range[1]
  182. ItemType = 1
  183. } else {
  184. ItemType = 2
  185. RangeOptions = result.ReferRange
  186. }
  187. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project_name = ? and item_name = ? and status = 1", org_id,checkItem.ItemTypeName,result.AntiName).Find(&inspection_reference).Count(&total).Error
  188. if inspection_reference.ID > 0 {
  189. ItemType = inspection_reference.RangeType
  190. }
  191. if total <= 0 {
  192. // 如果配置表里没有相关配置,则创建project_id,item_id和相关配置信息
  193. project_id, _ := GetSccdkfyyProjectID(org_id,checkItem.ItemTypeName)
  194. item_id,_ := GetSccdkfyyItemID(org_id, checkItem.ItemTypeName, result.AntiName, project_id)
  195. inspection_reference.OrgId = org_id
  196. inspection_reference.ProjectName = checkItem.ItemTypeName
  197. inspection_reference.Project = checkItem.ItemType
  198. inspection_reference.ProjectId = project_id
  199. inspection_reference.ItemName = result.AntiName
  200. inspection_reference.ItemNameAddition = result.AntiName
  201. inspection_reference.ItemId = item_id
  202. inspection_reference.ItemCode = result.AntiName
  203. inspection_reference.RangeType = ItemType
  204. inspection_reference.RangeMin = RangeMin
  205. inspection_reference.RangeMax = RangeMax
  206. // inspection_reference.RangeValue = RangeValue
  207. inspection_reference.RangeOptions = RangeOptions
  208. inspection_reference.Unit = result.Unit
  209. inspection_reference.Status = 1
  210. inspection_reference.CreatedTime = time.Now().Unix()
  211. inspection_reference.UpdatedTime = time.Now().Unix()
  212. inspection_reference.InspectDate = inspect_date
  213. inspection_reference.UTime = inspect_date
  214. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  215. if err != nil {
  216. tx.Rollback()
  217. }
  218. }
  219. var itotal int
  220. 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, patient_id).Find(&inspection).Count(&itotal).Error
  221. if itotal <= 0 {
  222. result.TestResult = strings.Replace(result.TestResult, "&gt;", ">", -1)
  223. result.TestResult = strings.Replace(result.TestResult, "&lt;", "<", -1)
  224. inspection.PatientId = patient_id
  225. inspection.OrgId = org_id
  226. inspection.ProjectId = inspection_reference.ProjectId
  227. inspection.ItemName = inspection_reference.ItemName
  228. inspection.ProjectName = inspection_reference.ProjectName
  229. inspection.InspectType = ItemType
  230. inspection.ItemId = inspection_reference.ItemId
  231. inspection.InspectValue = result.TestResult
  232. inspection.InspectDate = inspect_date
  233. inspection.RecordDate = record_date
  234. inspection.InspectTips = result.AntiCode
  235. inspection.Status = 1
  236. inspection.CreatedTime = time.Now().Unix()
  237. inspection.UpdatedTime = time.Now().Unix()
  238. inspection.UTime = inspect_date
  239. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  240. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  241. if err != nil {
  242. tx.Rollback()
  243. }
  244. }
  245. tx.Commit()
  246. return nil, err
  247. }
  248. func GetProjectInfoSccdkfyy(item_id int64, org_id int64) (project_info models.MiddleInspectionReference, err error) {
  249. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and item_id = ?", org_id, item_id).First(&project_info).Error
  250. return
  251. }
  252. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  253. func GetSccdkfyyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  254. var inspection_reference models.MiddleInspectionReference
  255. 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
  256. if inspection_reference.ID > 0 {
  257. return inspection_reference.ItemId, err
  258. } else {
  259. 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
  260. utils.InfoLog("inspection_reference: %v", inspection_reference)
  261. if inspection_reference.ItemId > 0 {
  262. return inspection_reference.ItemId + 1, err
  263. } else {
  264. return project_id*100 + 1, err
  265. }
  266. }
  267. }
  268. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  269. func GetSccdkfyyProjectID(org_id int64, project_name string) (project_id int64, err error) {
  270. var inspection_reference models.MiddleInspectionReference
  271. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  272. if inspection_reference.ID > 0 {
  273. return inspection_reference.ProjectId, err
  274. } else {
  275. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  276. if inspection_reference.ProjectId > 0 {
  277. return inspection_reference.ProjectId + 1, err
  278. } else {
  279. return 1055101, err
  280. }
  281. }
  282. }
  283. func GetScCdkfyyResult(rdb *gorm.DB, serialNo string,parItemNo string) (record []*models.PlugPlugresult, err error) {
  284. err = rdb.Model(&models.PlugPlugresult{}).Where("serialNo = ? and parItemNo = ?", serialNo,parItemNo).Find(&record).Error
  285. return
  286. }
  287. func GetScCdkfyyCheckItem(rdb *gorm.DB, serialNo string) (record []*models.PlugPlugcheckitem, err error) {
  288. err = rdb.Model(&models.PlugPlugcheckitem{}).Where("serialNo = ? ", serialNo).Find(&record).Error
  289. return
  290. }
  291. func GetScCdkfyyCase(rdb *gorm.DB, startId int64,endId int64, hisuserid string) (record []*models.PlugPlugcase, err error) {
  292. err = rdb.Model(&models.PlugPlugcase{}).Where("ID >= ? and ID <= ? and mainIndex = ?", startId,endId, hisuserid).Find(&record).Error
  293. return
  294. }
  295. func GetScCdkfyyCaseMaxID(rdb *gorm.DB) (record models.PlugPlugcase, err error) {
  296. err = rdb.Model(&models.PlugPlugcase{}).Select("TOP 1 *").Order("ID desc").Find(&record).Error
  297. return
  298. }