tscyyy_service.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. _ "IC/utils"
  6. _ "bytes"
  7. "encoding/json"
  8. _ "encoding/json"
  9. _ "encoding/xml"
  10. _ "fmt"
  11. "github.com/astaxie/beego/httplib"
  12. _ "github.com/jinzhu/gorm"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. type tscylis struct {
  18. HisUserId string `json:"his_user_id"`
  19. PatientName string `json:"patient_name"`
  20. ProjectId string `json:"project_id"`
  21. ProjectName string `json:"project_name"`
  22. InspectDate string `json:"inspect_date"`
  23. ItemId string `json:"item_id"`
  24. ItemName string `json:"item_name"`
  25. ItemType string `json:"item_type"`
  26. ItemValue string `json:"item_value"`
  27. RangeMin string `json:"range_min"`
  28. RangeMax string `json:"range_max"`
  29. RangeValue string `json:"range_value"`
  30. RangeOptions string `json:"range_options"`
  31. Unit string `json:"unit"`
  32. State string `json:"state"`
  33. CTime string `json:"c_time"`
  34. UTime string `json:"u_time"`
  35. }
  36. type tscyLisResult struct {
  37. ErrorCode int64 `json:"error_code"`
  38. Result []tscylis `json:"data"`
  39. }
  40. func GetTscyLis(sync_time string, his_user_id string) (str tscyLisResult, checkStr string) {
  41. apiurl := "http://60.2.56.98:8797/tscy/lis_report.do"
  42. rep := httplib.Get(apiurl)
  43. rep.Param("sync_time", sync_time)
  44. rep.Param("his_user_id", his_user_id)
  45. strs, err := rep.String()
  46. if err != nil {
  47. utils.InfoLog("err:%v", err)
  48. }
  49. var checkJSON tscyLisResult
  50. json.Unmarshal([]byte(strs), &checkJSON)
  51. // utils.InfoLog("checkJSON:%v", checkJSON)
  52. return checkJSON, strs
  53. }
  54. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  55. func GetTscyProjectID(org_id int64, project_name string) (project_id int64, err error) {
  56. var inspection_reference models.MiddleInspectionReference
  57. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  58. if inspection_reference.ID > 0 {
  59. return inspection_reference.ProjectId, err
  60. } else {
  61. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  62. if inspection_reference.ProjectId > 0 {
  63. return inspection_reference.ProjectId + 1, err
  64. } else {
  65. return 92001, err
  66. }
  67. }
  68. }
  69. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  70. func GetTscyVipProjectID(org_id int64, project_name string) (project_id int64, err error) {
  71. var inspection_reference models.MiddleInspectionReference
  72. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  73. if inspection_reference.ID > 0 {
  74. return inspection_reference.ProjectId, err
  75. } else {
  76. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  77. if inspection_reference.ProjectId > 0 {
  78. return inspection_reference.ProjectId + 1, err
  79. } else {
  80. return 1056201, err
  81. }
  82. }
  83. }
  84. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  85. func GetTscyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  86. var inspection_reference models.MiddleInspectionReference
  87. 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
  88. if inspection_reference.ID > 0 {
  89. return inspection_reference.ItemId, err
  90. } else {
  91. 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
  92. utils.InfoLog("inspection_reference: %v", inspection_reference)
  93. if inspection_reference.ItemId > 0 {
  94. return inspection_reference.ItemId + 1, err
  95. } else {
  96. return project_id*100 + 1, err
  97. }
  98. }
  99. }
  100. // 唐山朝阳医院透析中心Lis同步
  101. func SyncTscyLis() (err error) {
  102. org_id := int64(9882)
  103. // 第一步:获取上一次同步的时间点
  104. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  105. var sync_time int64
  106. if syncLastInfo.ID > 0 {
  107. sync_time = syncLastInfo.SyncTime
  108. } else {
  109. sync_time = 1651334400
  110. }
  111. // 第二步:获取所有患者的病历号
  112. patientList, _ := GetPatientGzjhByOrgId(org_id)
  113. if len(patientList) > 0 {
  114. for _, patient := range patientList {
  115. if len(patient.IdCardNo) > 0 || len(patient.DialysisNo) > 0 {
  116. // 上次同步时间转换为接口需要的格式
  117. patient_id := patient.ID
  118. // sync_time_temp := time.Unix(sync_time,0)
  119. // syncTimeStr := sync_time_temp.Format("2006010215:04:05")
  120. utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
  121. // 获取会员号
  122. // utils.InfoLog("VipStr:%v", VipStr)
  123. gzjhLis, _ := GetTscyLis(strconv.FormatInt(sync_time, 10), patient.IdCardNo)
  124. // if len(gzjhLis.Result) == 0 {
  125. // gzjhLis,_ = GethbdyLis(strconv.FormatInt(sync_time,10),VipStr)
  126. // }
  127. // utils.InfoLog("yzstr:%v", gzjhLis)
  128. if len(gzjhLis.Result) > 0 {
  129. // timeLayout := "2006010215:04:05" // 转化所需模板
  130. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  131. for _, info := range gzjhLis.Result {
  132. utils.InfoLog("Result:%v", info)
  133. // 根据project_id和item_id 判断该项是否存在
  134. project_id := int64(0)
  135. // if info.ProjectId > 0 {
  136. project_id, _ = GetTscyProjectID(org_id, info.ProjectName)
  137. // } else {
  138. // continue
  139. // }
  140. item_id, _ := GetTscyItemID(org_id, info.ProjectName, info.ItemName, project_id)
  141. tx := writeMiddleDb.Begin()
  142. var inspection models.MiddleInspection
  143. var inspection_reference models.MiddleInspectionReference
  144. record_date, _ := strconv.ParseInt(info.InspectDate, 10, 64)
  145. if record_date == 0 {
  146. record_date = time.Now().Unix()
  147. }
  148. // record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  149. // record_date := record_date_str.Unix()
  150. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  151. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  152. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  153. var total int
  154. var RangeOptions string
  155. var RangeMin string
  156. var RangeMax string
  157. // // 判断检查类型
  158. ItemType := info.ItemType
  159. // Range := strings.Split(info.RangeValue, "-")
  160. // if len(Range) > 1 {
  161. RangeMin = info.RangeMin
  162. RangeMax = info.RangeMax
  163. // ItemType = 1
  164. // } else {
  165. // ItemType = 2
  166. RangeOptions = info.RangeOptions
  167. // }
  168. 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
  169. // if inspection_reference.ID > 0 {
  170. // ItemType = int64(inspection_reference.RangeType)
  171. // }
  172. if total <= 0 {
  173. inspection_reference.OrgId = org_id
  174. inspection_reference.ProjectName = info.ProjectName
  175. inspection_reference.Project = info.ProjectName
  176. inspection_reference.ProjectId = project_id
  177. inspection_reference.ItemName = info.ItemName
  178. inspection_reference.ItemNameAddition = info.ItemId
  179. inspection_reference.ItemId = item_id
  180. inspection_reference.RangeType, _ = strconv.Atoi(ItemType)
  181. inspection_reference.RangeMin = RangeMin
  182. inspection_reference.RangeMax = RangeMax
  183. // inspection_reference.RangeValue = RangeValue
  184. inspection_reference.RangeOptions = RangeOptions
  185. inspection_reference.Unit = info.Unit
  186. inspection_reference.Status = 1
  187. inspection_reference.CreatedTime = time.Now().Unix()
  188. inspection_reference.UpdatedTime = time.Now().Unix()
  189. inspection_reference.InspectDate = inspect_date
  190. inspection_reference.UTime = inspect_date
  191. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  192. if err != nil {
  193. tx.Rollback()
  194. }
  195. }
  196. var itotal int
  197. 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, record_date, patient_id).Find(&inspection).Count(&itotal).Error
  198. if itotal <= 0 {
  199. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  200. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  201. inspection.PatientId = patient_id
  202. inspection.OrgId = org_id
  203. inspection.ProjectId = project_id
  204. inspection.ItemName = inspection_reference.ItemName
  205. inspection.ProjectName = inspection_reference.ProjectName
  206. inspection.InspectType, _ = strconv.Atoi(ItemType)
  207. inspection.ItemId = item_id
  208. inspection.InspectValue = info.ItemValue
  209. inspection.InspectDate = inspect_date
  210. inspection.RecordDate = record_date
  211. // inspection.InspectTips = report.Resultstate
  212. inspection.Status = 1
  213. inspection.CreatedTime = time.Now().Unix()
  214. inspection.UpdatedTime = time.Now().Unix()
  215. inspection.UTime = inspect_date
  216. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  217. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  218. if err != nil {
  219. tx.Rollback()
  220. }
  221. }
  222. tx.Commit()
  223. }
  224. }
  225. } else {
  226. continue
  227. }
  228. }
  229. }
  230. var syncInfo models.MiddleSyncInfo
  231. syncInfo.OrgId = org_id
  232. syncInfo.SyncTime = time.Now().Unix()
  233. syncInfo.SyncResultType = 1
  234. syncInfo.SyncRsultRemark = "同步成功"
  235. syncInfo.SyncTotalNum = 0
  236. syncInfo.SyncSuccessNum = 0
  237. syncInfo.SyncInfo = ""
  238. syncInfo.CreateTime = time.Now().Unix()
  239. syncInfo.UpdateTime = time.Now().Unix()
  240. cwderr := CreateSyncInfo(&syncInfo)
  241. if cwderr != nil {
  242. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  243. return
  244. }
  245. SyncToTscytx()
  246. return
  247. }
  248. func SyncToTscytx() {
  249. utils.TraceLog("检验检查同步任务开始执行")
  250. org_id := int64(9882)
  251. // 第一步:跟进org_id 去中间库查出需要同步的数据
  252. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  253. inspections, _ := GetSyncInspectionByOrgId(org_id)
  254. // 第二步:将数据同步到业务库
  255. if len(inspection_references) > 0 {
  256. for _, inspection_reference := range inspection_references {
  257. SyncInspectionReference(&inspection_reference)
  258. }
  259. }
  260. if len(inspections) > 0 {
  261. for _, inspection := range inspections {
  262. SyncInspection(&inspection)
  263. }
  264. }
  265. utils.SuccessLog("检验检查同步任务完成")
  266. }
  267. // 唐山朝阳医院Vip区透析中心Lis同步
  268. func SyncTscyVipLis() (err error) {
  269. org_id := int64(10562)
  270. // 第一步:获取上一次同步的时间点
  271. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  272. var sync_time int64
  273. if syncLastInfo.ID > 0 {
  274. sync_time = syncLastInfo.SyncTime
  275. } else {
  276. sync_time = 1651334400
  277. }
  278. // 第二步:获取所有患者的病历号
  279. patientList, _ := GetPatientGzjhByOrgId(org_id)
  280. if len(patientList) > 0 {
  281. for _, patient := range patientList {
  282. if len(patient.IdCardNo) > 0 || len(patient.DialysisNo) > 0 {
  283. // 上次同步时间转换为接口需要的格式
  284. patient_id := patient.ID
  285. utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
  286. gzjhLis, _ := GetTscyLis(strconv.FormatInt(sync_time, 10), patient.IdCardNo)
  287. if len(gzjhLis.Result) > 0 {
  288. for _, info := range gzjhLis.Result {
  289. // 根据project_id和item_id 判断该项是否存在
  290. project_id := int64(0)
  291. project_id, _ = GetTscyVipProjectID(org_id, info.ProjectName)
  292. item_id, _ := GetTscyItemID(org_id, info.ProjectName, info.ItemName, project_id)
  293. tx := writeMiddleDb.Begin()
  294. var inspection models.MiddleInspection
  295. var inspection_reference models.MiddleInspectionReference
  296. record_date, _ := strconv.ParseInt(info.InspectDate, 10, 64)
  297. if record_date == 0 {
  298. record_date = time.Now().Unix()
  299. }
  300. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  301. var total int
  302. var RangeOptions string
  303. var RangeMin string
  304. var RangeMax string
  305. // // 判断检查类型
  306. ItemType := info.ItemType
  307. RangeMin = info.RangeMin
  308. RangeMax = info.RangeMax
  309. RangeOptions = info.RangeOptions
  310. 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
  311. if total <= 0 {
  312. inspection_reference.OrgId = org_id
  313. inspection_reference.ProjectName = info.ProjectName
  314. inspection_reference.Project = info.ProjectName
  315. inspection_reference.ProjectId = project_id
  316. inspection_reference.ItemName = info.ItemName
  317. inspection_reference.ItemNameAddition = info.ItemId
  318. inspection_reference.ItemId = item_id
  319. inspection_reference.RangeType, _ = strconv.Atoi(ItemType)
  320. inspection_reference.RangeMin = RangeMin
  321. inspection_reference.RangeMax = RangeMax
  322. // inspection_reference.RangeValue = RangeValue
  323. inspection_reference.RangeOptions = RangeOptions
  324. inspection_reference.Unit = info.Unit
  325. inspection_reference.Status = 1
  326. inspection_reference.CreatedTime = time.Now().Unix()
  327. inspection_reference.UpdatedTime = time.Now().Unix()
  328. inspection_reference.InspectDate = inspect_date
  329. inspection_reference.UTime = inspect_date
  330. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  331. if err != nil {
  332. tx.Rollback()
  333. }
  334. }
  335. var itotal int
  336. 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, record_date, patient_id).Find(&inspection).Count(&itotal).Error
  337. if itotal <= 0 {
  338. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  339. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  340. inspection.PatientId = patient_id
  341. inspection.OrgId = org_id
  342. inspection.ProjectId = project_id
  343. inspection.ItemName = inspection_reference.ItemName
  344. inspection.ProjectName = inspection_reference.ProjectName
  345. inspection.InspectType, _ = strconv.Atoi(ItemType)
  346. inspection.ItemId = item_id
  347. inspection.InspectValue = info.ItemValue
  348. inspection.InspectDate = inspect_date
  349. inspection.RecordDate = record_date
  350. // inspection.InspectTips = report.Resultstate
  351. inspection.Status = 1
  352. inspection.CreatedTime = time.Now().Unix()
  353. inspection.UpdatedTime = time.Now().Unix()
  354. inspection.UTime = inspect_date
  355. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  356. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  357. if err != nil {
  358. tx.Rollback()
  359. }
  360. }
  361. tx.Commit()
  362. }
  363. }
  364. } else {
  365. continue
  366. }
  367. }
  368. }
  369. var syncInfo models.MiddleSyncInfo
  370. syncInfo.OrgId = org_id
  371. syncInfo.SyncTime = time.Now().Unix()
  372. syncInfo.SyncResultType = 1
  373. syncInfo.SyncRsultRemark = "同步成功"
  374. syncInfo.SyncTotalNum = 0
  375. syncInfo.SyncSuccessNum = 0
  376. syncInfo.SyncInfo = ""
  377. syncInfo.CreateTime = time.Now().Unix()
  378. syncInfo.UpdateTime = time.Now().Unix()
  379. cwderr := CreateSyncInfo(&syncInfo)
  380. if cwderr != nil {
  381. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  382. return
  383. }
  384. SyncToTscyVip()
  385. return
  386. }
  387. func SyncToTscyVip() {
  388. utils.TraceLog("检验检查同步任务开始执行")
  389. org_id := int64(10562)
  390. // 第一步:跟进org_id 去中间库查出需要同步的数据
  391. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  392. inspections, _ := GetSyncInspectionByOrgId(org_id)
  393. // 第二步:将数据同步到业务库
  394. if len(inspection_references) > 0 {
  395. for _, inspection_reference := range inspection_references {
  396. SyncInspectionReference(&inspection_reference)
  397. }
  398. }
  399. if len(inspections) > 0 {
  400. for _, inspection := range inspections {
  401. SyncInspection(&inspection)
  402. }
  403. }
  404. utils.SuccessLog("检验检查同步任务完成")
  405. }