sy_service.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 stsylis struct {
  16. Testno string `gorm:"column:testno " json:"testno "`
  17. Seqno string `gorm:"column:seqno " json:"seqno "`
  18. Itemno string `gorm:"column:itemno " json:"itemno "`
  19. Itemname string `gorm:"column:itemname" json:"itemname"`
  20. Testresult string `gorm:"column:testresult" json:"testresult"`
  21. Result1 string `gorm:"column:result1" json:"result1"`
  22. Result2 string `gorm:"column:result2" json:"result2"`
  23. Resultflag string `gorm:"column:resultflag" json:"resultflag"`
  24. Units string `gorm:"column:units" json:"units"`
  25. Ranges string `gorm:"column:ranges" json:"ranges"`
  26. Resulttime string `gorm:"column:resulttime" json:"resulttime"`
  27. Checkman string `gorm:"column:checkman" json:"checkman"`
  28. Testman string `gorm:"column:testman" json:"testman"`
  29. Lastmodify string `gorm:"column:lastmodify" json:"lastmodify"`
  30. Instrument string `gorm:"column:instrument" json:"instrument"`
  31. Sampleno string `gorm:"column:sampleno" json:"sampleno"`
  32. Brdh string `gorm:"column:brdh" json:"brdh"`
  33. Brxm string `gorm:"column:brxm" json:"brxm"`
  34. Sfxmdh string `gorm:"column:sfxmdh" json:"sfxmdh"`
  35. Sendflag string `gorm:"column:sendflag" json:"sendflag"`
  36. Myid string `gorm:"column:myid" json:"myid"`
  37. Uploadflag string `gorm:"column:uploadflag" json:"uploadflag"`
  38. }
  39. func (stsylis) TableName() string {
  40. return "lis_reqresult"
  41. }
  42. func GetStsyLis(rdb *gorm.DB, synctime time.Time) (record []*stsylis, err error) {
  43. err = rdb.Model(&stsylis{}).Where("lastmodify >= ? ", synctime).Find(&record).Error
  44. return
  45. }
  46. // 根据机构ID和患者姓名获取患者信息
  47. func GetUserInfoByName(org_id int64, name string) (patients models.Patients, err error) {
  48. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1 and name = ?", org_id, name).First(&patients).Error
  49. return
  50. }
  51. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  52. func GetStsyProjectID(org_id int64, project_name string, item_name string) (project_id int64, err error) {
  53. var inspection_reference models.MiddleInspectionReference
  54. 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
  55. if inspection_reference.ID > 0 {
  56. return inspection_reference.ProjectId, err
  57. } else {
  58. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  59. if inspection_reference.ProjectId > 0 {
  60. return inspection_reference.ProjectId + 1, err
  61. } else {
  62. return 1022301, err
  63. }
  64. }
  65. }
  66. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  67. func GetStsyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  68. var inspection_reference models.MiddleInspectionReference
  69. 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
  70. if inspection_reference.ID > 0 {
  71. return inspection_reference.ItemId, err
  72. } else {
  73. 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
  74. if inspection_reference.ItemId > 0 {
  75. return inspection_reference.ItemId + 1, err
  76. } else {
  77. return project_id*100 + 1, err
  78. }
  79. }
  80. }
  81. // 汕头三优血液透析中心
  82. func SyncStsyLis() (err error) {
  83. org_id := int64(10223)
  84. org := &models.DataUploadConfig{
  85. OrgId: org_id,
  86. DbHost: "192.168.1.209",
  87. DbPort: "1433",
  88. DbPass: "Aa123456",
  89. DbUser: "kyys",
  90. DbName: "lis2022",
  91. }
  92. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  93. if err != nil {
  94. utils.ErrorLog("创建数据库连接失败:%v", err)
  95. return
  96. }
  97. // var sync_time int64
  98. // sync_time = 1627747200
  99. // scpaLis, _ := GetScpaLis(orgDb, sync_time,"512926196302182682")
  100. // utils.InfoLog("IdCardNo:%v",scpaLis)
  101. // 第一步:获取上一次同步的时间点
  102. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  103. var sync_time int64
  104. if syncLastInfo.ID > 0 {
  105. sync_time = syncLastInfo.SyncTime
  106. } else {
  107. sync_time = 1685548800
  108. }
  109. SyncTime := time.Unix(sync_time, 0)
  110. fmt.Println(SyncTime)
  111. // 第二步:获取时间内所有检验结果
  112. stsyLis, _ := GetStsyLis(orgDb, SyncTime)
  113. if len(stsyLis) > 0 {
  114. var syncTimeEnd int64
  115. syncTimeEnd = time.Now().Unix()
  116. for _, LisInfo := range stsyLis {
  117. brxm := LisInfo.Brxm
  118. // 根据姓名获取患者ID
  119. patient, _ := GetUserInfoByName(org_id, brxm)
  120. if patient.ID > 0 {
  121. project_id, _ := GetStsyProjectID(org_id, LisInfo.Instrument, LisInfo.Itemname)
  122. item_id, _ := GetStsyItemID(org_id, LisInfo.Instrument, LisInfo.Itemname, project_id)
  123. tx := writeMiddleDb.Begin()
  124. var inspection models.MiddleInspection
  125. var inspection_reference models.MiddleInspectionReference
  126. timeLayout := "2006-01-02 15:04"
  127. loc, _ := time.LoadLocation("Local")
  128. tempTime := strings.Split(LisInfo.Lastmodify, "T")
  129. tempTime1 := strings.Split(tempTime[1], ".")
  130. tempRecord := tempTime[0] + " " + tempTime1[0]
  131. recordTime, _ := time.ParseInLocation("2006-01-02 15:04:05", tempRecord, loc)
  132. recordTimeInt := recordTime.Unix()
  133. recordTimeStr := time.Unix(recordTimeInt, 0).Format(timeLayout)
  134. var total int
  135. var RangeOptions string
  136. var RangeMin string
  137. var RangeMax string
  138. var ItemType int
  139. Range := strings.Split(LisInfo.Ranges, "--")
  140. if len(Range) > 1 {
  141. RangeMin = Range[0]
  142. RangeMax = Range[1]
  143. ItemType = 1
  144. } else {
  145. ItemType = 2
  146. RangeOptions = LisInfo.Ranges
  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.Instrument
  152. inspection_reference.Project = LisInfo.Instrument
  153. inspection_reference.ProjectId = project_id
  154. inspection_reference.ItemName = LisInfo.Itemname
  155. // inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  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 = RangeValue
  161. inspection_reference.RangeOptions = RangeOptions
  162. inspection_reference.Unit = LisInfo.Units
  163. inspection_reference.Status = 1
  164. inspection_reference.CreatedTime = time.Now().Unix()
  165. inspection_reference.UpdatedTime = time.Now().Unix()
  166. inspection_reference.InspectDate = recordTimeStr
  167. inspection_reference.UTime = recordTimeStr
  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.Testresult
  186. inspection.InspectDate = recordTimeStr
  187. inspection.RecordDate = recordTimeInt
  188. // inspection.InspectTips = report.Resultstate
  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. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  195. if err != nil {
  196. tx.Rollback()
  197. }
  198. }
  199. tx.Commit()
  200. } else {
  201. continue
  202. }
  203. }
  204. var syncInfo models.MiddleSyncInfo
  205. syncInfo.OrgId = org_id
  206. syncInfo.SyncTime = syncTimeEnd
  207. syncInfo.SyncResultType = 1
  208. syncInfo.SyncRsultRemark = "同步成功"
  209. syncInfo.SyncTotalNum = 0
  210. syncInfo.SyncSuccessNum = 0
  211. syncInfo.SyncInfo = ""
  212. syncInfo.CreateTime = time.Now().Unix()
  213. syncInfo.UpdateTime = time.Now().Unix()
  214. cwderr := CreateSyncInfo(&syncInfo)
  215. if cwderr != nil {
  216. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  217. return
  218. }
  219. }
  220. // 第四步:关闭数据库连接
  221. CloseDB(orgDb)
  222. SyncToStsytx()
  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 SyncToStsytx() {
  235. org_id := int64(10223)
  236. utils.TraceLog("检验检查同步任务开始执行")
  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. SyncInspection(&inspection)
  249. }
  250. }
  251. // 第四步:关闭数据库连接
  252. utils.SuccessLog("检验检查同步任务完成")
  253. }