sy_service.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package service
  2. // 三优 检验检查对接
  3. import (
  4. "IC/models"
  5. "IC/utils"
  6. _ "encoding/json"
  7. _ "encoding/xml"
  8. "github.com/jinzhu/gorm"
  9. _ "github.com/jinzhu/gorm"
  10. _"strconv"
  11. "strings"
  12. "time"
  13. "fmt"
  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("resulttime >= ? ", 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: "127.0.0.1",
  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. for _,LisInfo := range stsyLis{
  115. brxm := LisInfo.Brxm
  116. // 根据姓名获取患者ID
  117. patient,_ := GetUserInfoByName(org_id,brxm)
  118. if patient.ID > 0 {
  119. project_id, _ := GetStsyProjectID(org_id, LisInfo.Instrument, LisInfo.Itemname)
  120. item_id, _ := GetStsyItemID(org_id, LisInfo.Instrument, LisInfo.Itemname, project_id)
  121. tx := writeMiddleDb.Begin()
  122. var inspection models.MiddleInspection
  123. var inspection_reference models.MiddleInspectionReference
  124. timeLayout := "2006-01-02 15:04"
  125. loc, _ := time.LoadLocation("Local")
  126. tempTime := strings.Split(LisInfo.Lastmodify, "T")
  127. tempTime1 := strings.Split(tempTime[1], ".")
  128. tempRecord := tempTime[0] + " " + tempTime1[0]
  129. recordTime, _ := time.ParseInLocation("2006-01-02 15:04:05", tempRecord, loc)
  130. recordTimeInt := recordTime.Unix()
  131. recordTimeStr := time.Unix(recordTimeInt,0).Format(timeLayout)
  132. var total int
  133. var RangeOptions string
  134. var RangeMin string
  135. var RangeMax string
  136. var ItemType int
  137. Range := strings.Split(LisInfo.Ranges, "--")
  138. if len(Range) > 1 {
  139. RangeMin = Range[0]
  140. RangeMax = Range[1]
  141. ItemType = 1
  142. } else {
  143. ItemType = 2
  144. RangeOptions = LisInfo.Ranges
  145. }
  146. 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
  147. if total <= 0 {
  148. inspection_reference.OrgId = org_id
  149. inspection_reference.ProjectName = LisInfo.Instrument
  150. inspection_reference.Project = LisInfo.Instrument
  151. inspection_reference.ProjectId = project_id
  152. inspection_reference.ItemName = LisInfo.Itemname
  153. // inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  154. inspection_reference.ItemId = item_id
  155. inspection_reference.RangeType = ItemType
  156. inspection_reference.RangeMin = RangeMin
  157. inspection_reference.RangeMax = RangeMax
  158. // inspection_reference.RangeValue = RangeValue
  159. inspection_reference.RangeOptions = RangeOptions
  160. inspection_reference.Unit = LisInfo.Units
  161. inspection_reference.Status = 1
  162. inspection_reference.CreatedTime = time.Now().Unix()
  163. inspection_reference.UpdatedTime = time.Now().Unix()
  164. inspection_reference.InspectDate = recordTimeStr
  165. inspection_reference.UTime = recordTimeStr
  166. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  167. if err != nil {
  168. tx.Rollback()
  169. }
  170. }
  171. var itotal int
  172. 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, recordTimeInt, patient.ID).Find(&inspection).Count(&itotal).Error
  173. if itotal <= 0 {
  174. // inspection.InspectValue = strings.Replace(LisInfo.Testresult, "&gt;", ">", -1)
  175. // inspection.InspectValue = strings.Replace(LiLisInfo.Testresult, "&lt;", "<", -1)
  176. inspection.PatientId = patient.ID
  177. inspection.OrgId = org_id
  178. inspection.ProjectId = project_id
  179. inspection.ItemName = inspection_reference.ItemName
  180. inspection.ProjectName = inspection_reference.ProjectName
  181. inspection.InspectType = ItemType
  182. inspection.ItemId = item_id
  183. inspection.InspectValue = LisInfo.Testresult
  184. inspection.InspectDate = recordTimeStr
  185. inspection.RecordDate = recordTimeInt
  186. // inspection.InspectTips = report.Resultstate
  187. inspection.Status = 1
  188. inspection.CreatedTime = time.Now().Unix()
  189. inspection.UpdatedTime = time.Now().Unix()
  190. // inspection.UTime = record_date.Format(timeLayout)
  191. // inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  192. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  193. if err != nil {
  194. tx.Rollback()
  195. }
  196. }
  197. tx.Commit()
  198. } else {
  199. continue
  200. }
  201. }
  202. }
  203. var syncInfo models.MiddleSyncInfo
  204. syncInfo.OrgId = org_id
  205. syncInfo.SyncTime = time.Now().Unix()
  206. syncInfo.SyncResultType = 1
  207. syncInfo.SyncRsultRemark = "同步成功"
  208. syncInfo.SyncTotalNum = 0
  209. syncInfo.SyncSuccessNum = 0
  210. syncInfo.SyncInfo = ""
  211. syncInfo.CreateTime = time.Now().Unix()
  212. syncInfo.UpdateTime = time.Now().Unix()
  213. cwderr := CreateSyncInfo(&syncInfo)
  214. if cwderr != nil {
  215. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  216. return
  217. }
  218. SyncToStsytx()
  219. return
  220. }
  221. // func deleteMid(tx *gorm.DB) (err error) {
  222. // err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock as t," +
  223. // "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  224. // "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  225. // "WHERE t1.user_org_id = t2.orgid)tmp " +
  226. // "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  227. // "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  228. // return
  229. // }
  230. func SyncToStsytx() {
  231. utils.TraceLog("检验检查同步任务开始执行")
  232. org_id := int64(10223)
  233. // 第一步:跟进org_id 去中间库查出需要同步的数据
  234. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  235. inspections, _ := GetSyncInspectionByOrgId(org_id)
  236. // 第二步:将数据同步到业务库
  237. if len(inspection_references) > 0 {
  238. for _, inspection_reference := range inspection_references {
  239. SyncInspectionReference(&inspection_reference)
  240. }
  241. }
  242. if len(inspections) > 0 {
  243. for _, inspection := range inspections {
  244. SyncInspection(&inspection)
  245. }
  246. }
  247. utils.SuccessLog("检验检查同步任务完成")
  248. }