sy_service.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package service
  2. // 三优 检验检查对接
  3. import (
  4. _ "encoding/json"
  5. _ "encoding/xml"
  6. "fmt"
  7. _ "strconv"
  8. "strings"
  9. "time"
  10. "IC/models"
  11. "IC/utils"
  12. "github.com/jinzhu/gorm"
  13. _ "github.com/jinzhu/gorm"
  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. func SyncShuHeLis() (err error) {
  82. org_id := int64(10644)
  83. org := &models.DataUploadConfig{
  84. OrgId: org_id,
  85. DbHost: "192.168.2.4",
  86. DbPort: "1433",
  87. DbPass: "a123456..",
  88. DbUser: "sa",
  89. DbName: "BI",
  90. }
  91. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  92. if err != nil {
  93. utils.ErrorLog("创建数据库连接失败:%v", err)
  94. return
  95. }
  96. fmt.Println(orgDb)
  97. infoList, _ := GetHisLabelPrintStatusInfo(org_id)
  98. if len(infoList) > 0 {
  99. for _, item := range infoList {
  100. p_ids_arr := strings.Split(item.Ids, ",")
  101. list, _ := GetHisLabelPrintInfo(org_id, p_ids_arr)
  102. fmt.Println("list==============", list)
  103. patients, _ := GetPatientByIDOne(org_id, item.PatientId)
  104. fmt.Println("patinets", patients.Name)
  105. fmt.Println("patinets", patients.IdCardNo)
  106. if len(list) > 0 {
  107. for _, it := range list {
  108. printCode := models.DataPrintCode{
  109. HisUserId: patients.IdCardNo,
  110. Name: patients.Name,
  111. RecordDate: it.RecordDate,
  112. ProjectName: it.ProjectName,
  113. Ctime: time.Now().Unix(),
  114. Mtime: time.Now().Unix(),
  115. Status: 1,
  116. PrintId: item.ID,
  117. PatientId: it.PatientId,
  118. }
  119. err = CratePrintCode(orgDb, printCode)
  120. fmt.Println("errr-000000000000000", err)
  121. }
  122. }
  123. }
  124. }
  125. return
  126. }
  127. // 汕头三优血液透析中心
  128. func SyncStsyLis() (err error) {
  129. org_id := int64(10223)
  130. org := &models.DataUploadConfig{
  131. OrgId: org_id,
  132. DbHost: "192.168.1.209",
  133. DbPort: "1433",
  134. DbPass: "Aa123456",
  135. DbUser: "kyys",
  136. DbName: "lis2022",
  137. }
  138. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  139. if err != nil {
  140. utils.ErrorLog("创建数据库连接失败:%v", err)
  141. return
  142. }
  143. // var sync_time int64
  144. // sync_time = 1627747200
  145. // scpaLis, _ := GetScpaLis(orgDb, sync_time,"512926196302182682")
  146. // utils.InfoLog("IdCardNo:%v",scpaLis)
  147. // 第一步:获取上一次同步的时间点
  148. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  149. var sync_time int64
  150. if syncLastInfo.ID > 0 {
  151. sync_time = syncLastInfo.SyncTime
  152. } else {
  153. sync_time = 1685548800
  154. }
  155. SyncTime := time.Unix(sync_time, 0)
  156. fmt.Println(SyncTime)
  157. // 第二步:获取时间内所有检验结果
  158. stsyLis, _ := GetStsyLis(orgDb, SyncTime)
  159. if len(stsyLis) > 0 {
  160. var syncTimeEnd int64
  161. syncTimeEnd = time.Now().Unix()
  162. for _, LisInfo := range stsyLis {
  163. brxm := LisInfo.Brxm
  164. // 根据姓名获取患者ID
  165. patient, _ := GetUserInfoByName(org_id, brxm)
  166. if patient.ID > 0 {
  167. project_id, _ := GetStsyProjectID(org_id, LisInfo.Instrument, LisInfo.Itemname)
  168. item_id, _ := GetStsyItemID(org_id, LisInfo.Instrument, LisInfo.Itemname, project_id)
  169. tx := writeMiddleDb.Begin()
  170. var inspection models.MiddleInspection
  171. var inspection_reference models.MiddleInspectionReference
  172. timeLayout := "2006-01-02 15:04"
  173. loc, _ := time.LoadLocation("Local")
  174. tempTime := strings.Split(LisInfo.Lastmodify, "T")
  175. tempTime1 := strings.Split(tempTime[1], ".")
  176. tempRecord := tempTime[0] + " " + tempTime1[0]
  177. recordTime, _ := time.ParseInLocation("2006-01-02 15:04:05", tempRecord, loc)
  178. recordTimeInt := recordTime.Unix()
  179. recordTimeStr := time.Unix(recordTimeInt, 0).Format(timeLayout)
  180. var total int
  181. var RangeOptions string
  182. var RangeMin string
  183. var RangeMax string
  184. var ItemType int
  185. Range := strings.Split(LisInfo.Ranges, "--")
  186. if len(Range) > 1 {
  187. RangeMin = Range[0]
  188. RangeMax = Range[1]
  189. ItemType = 1
  190. } else {
  191. ItemType = 2
  192. RangeOptions = LisInfo.Ranges
  193. }
  194. 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
  195. if total <= 0 {
  196. inspection_reference.OrgId = org_id
  197. inspection_reference.ProjectName = LisInfo.Instrument
  198. inspection_reference.Project = LisInfo.Instrument
  199. inspection_reference.ProjectId = project_id
  200. inspection_reference.ItemName = LisInfo.Itemname
  201. // inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  202. inspection_reference.ItemId = item_id
  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 = LisInfo.Units
  209. inspection_reference.Status = 1
  210. inspection_reference.CreatedTime = time.Now().Unix()
  211. inspection_reference.UpdatedTime = time.Now().Unix()
  212. inspection_reference.InspectDate = recordTimeStr
  213. inspection_reference.UTime = recordTimeStr
  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, project_id, item_id, recordTimeInt, patient.ID).Find(&inspection).Count(&itotal).Error
  221. if itotal <= 0 {
  222. // inspection.InspectValue = strings.Replace(LisInfo.Testresult, "&gt;", ">", -1)
  223. // inspection.InspectValue = strings.Replace(LiLisInfo.Testresult, "&lt;", "<", -1)
  224. inspection.PatientId = patient.ID
  225. inspection.OrgId = org_id
  226. inspection.ProjectId = project_id
  227. inspection.ItemName = inspection_reference.ItemName
  228. inspection.ProjectName = inspection_reference.ProjectName
  229. inspection.InspectType = ItemType
  230. inspection.ItemId = item_id
  231. inspection.InspectValue = LisInfo.Testresult
  232. inspection.InspectDate = recordTimeStr
  233. inspection.RecordDate = recordTimeInt
  234. // inspection.InspectTips = report.Resultstate
  235. inspection.Status = 1
  236. inspection.CreatedTime = time.Now().Unix()
  237. inspection.UpdatedTime = time.Now().Unix()
  238. // inspection.UTime = record_date.Format(timeLayout)
  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. } else {
  247. continue
  248. }
  249. }
  250. var syncInfo models.MiddleSyncInfo
  251. syncInfo.OrgId = org_id
  252. syncInfo.SyncTime = syncTimeEnd
  253. syncInfo.SyncResultType = 1
  254. syncInfo.SyncRsultRemark = "同步成功"
  255. syncInfo.SyncTotalNum = 0
  256. syncInfo.SyncSuccessNum = 0
  257. syncInfo.SyncInfo = ""
  258. syncInfo.CreateTime = time.Now().Unix()
  259. syncInfo.UpdateTime = time.Now().Unix()
  260. cwderr := CreateSyncInfo(&syncInfo)
  261. if cwderr != nil {
  262. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  263. return
  264. }
  265. }
  266. // 第四步:关闭数据库连接
  267. CloseDB(orgDb)
  268. SyncToStsytx()
  269. return
  270. }
  271. // func deleteMid(tx *gorm.DB) (err error) {
  272. // err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock as t," +
  273. // "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," +
  274. // "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " +
  275. // "WHERE t1.user_org_id = t2.orgid)tmp " +
  276. // "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" +
  277. // "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error
  278. // return
  279. // }
  280. func SyncToStsytx() {
  281. org_id := int64(10223)
  282. utils.TraceLog("检验检查同步任务开始执行")
  283. // 第一步:跟进org_id 去中间库查出需要同步的数据
  284. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  285. inspections, _ := GetSyncInspectionByOrgId(org_id)
  286. // 第二步:将数据同步到业务库
  287. if len(inspection_references) > 0 {
  288. for _, inspection_reference := range inspection_references {
  289. SyncInspectionReference(&inspection_reference)
  290. }
  291. }
  292. if len(inspections) > 0 {
  293. for _, inspection := range inspections {
  294. SyncInspection(&inspection)
  295. }
  296. }
  297. // 第四步:关闭数据库连接
  298. utils.SuccessLog("检验检查同步任务完成")
  299. }
  300. //舒和
  301. func CratePrintCode(rdb *gorm.DB, code models.DataPrintCode) error {
  302. err := rdb.Create(&code).Error
  303. return err
  304. }
  305. func GetHisLabelPrintStatusInfo(user_org_id int64) (info []*models.XtHisLabelPrintStatusInfo, err error) {
  306. err = XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&info).Error
  307. return info, err
  308. }
  309. func GetHisLabelPrintInfo(user_org_id int64, p_ids_arr []string) (list []*models.HisLabelPrintInfo, err error) {
  310. err = XTReadDB().Where("user_org_id = ? and status=1 and id in(?)", user_org_id, p_ids_arr).Find(&list).Error
  311. return list, err
  312. }
  313. func GetPatientByIDOne(orgID int64, patientID int64) (models.Patients, error) {
  314. var patient models.Patients
  315. err := readDb.Model(&models.Patients{}).Where("id = ? and user_org_id=? and status=1", patientID, orgID).First(&patient).Error
  316. return patient, err
  317. }