csny_service.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. "net/http"
  12. "io/ioutil"
  13. _ "github.com/jinzhu/gorm"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // 福建天福医院透析中心Lis同步
  19. func SyncCsnyLis() (err error) {
  20. org_id := int64(10340)
  21. // 第一步:获取上一次同步的时间点
  22. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  23. var sync_time int64
  24. if syncLastInfo.ID > 0 {
  25. sync_time = syncLastInfo.SyncTime
  26. } else {
  27. sync_time = 1651334400 // 2022-05-01 00:00:00
  28. }
  29. sync_time_temp := time.Unix(sync_time,0)
  30. syncTimeStr := sync_time_temp.Format("2006-01-02 15:04:05")
  31. // 第二步:获取所有患者的病历号
  32. patientList, _ := GetPatientGzjhByOrgId(org_id)
  33. if len(patientList) > 0 {
  34. for _, patient := range patientList {
  35. if len(patient.IdCardNo) > 0 || len(patient.DialysisNo) > 0 {
  36. patient_id := patient.ID
  37. utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
  38. // utils.InfoLog("VipStr:%v", VipStr)
  39. result, _ := CsnyGetLab(patient.IdCardNo,syncTimeStr)
  40. utils.InfoLog("result:%v", result)
  41. var fjtfLab LabCsnyResult
  42. if err := json.Unmarshal([]byte(result), &fjtfLab); err != nil {
  43. utils.ErrorLog("解析失败:%v", err)
  44. }
  45. fmt.Println(fjtfLab)
  46. if len(fjtfLab.Result) > 0 {
  47. // 根据获取的头部lab01获取检查结果
  48. for _, info := range fjtfLab.Result {
  49. lacResult, _ := CsnyGetLac(info.Lab01)
  50. utils.InfoLog("lacResult:%v", lacResult)
  51. var fjtfLac LacCsnyResult
  52. if err := json.Unmarshal([]byte(lacResult), &fjtfLac); err != nil {
  53. utils.ErrorLog("解析失败:%v", err)
  54. }
  55. fmt.Println(fjtfLac)
  56. for _,cinfo := range fjtfLac.Result{
  57. project_id := int64(0)
  58. item_id := int64(0)
  59. if len(cinfo.Bbx05a) > 0 {
  60. project_id, _ = GetCsnyProjectID(org_id, cinfo.Bbx05a)
  61. item_id, _ = GetCsnyItemID(org_id, cinfo.Bbx05a, cinfo.Bbx05, project_id)
  62. } else {
  63. project_id, _ = GetCsnyProjectID(org_id, cinfo.Bbx05)
  64. item_id, _ = GetCsnyItemID(org_id, cinfo.Bbx05, cinfo.Bbx05, project_id)
  65. }
  66. tx := writeMiddleDb.Begin()
  67. var inspection models.MiddleInspection
  68. var inspection_reference models.MiddleInspectionReference
  69. loc, _ := time.LoadLocation("Local")
  70. theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", cinfo.Lac25, loc)
  71. record_date := theTime.Unix()
  72. if record_date == 0 {
  73. record_date = time.Now().Unix()
  74. }
  75. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  76. var total int
  77. var RangeOptions string
  78. var RangeMin string
  79. var RangeMax string
  80. // // 判断检查类型
  81. ItemType := 1
  82. Range := strings.Split(cinfo.Lac15, "-")
  83. if len(Range) > 1 {
  84. RangeMin = cinfo.Lac13
  85. RangeMax = cinfo.Lac14
  86. ItemType = 1
  87. } else {
  88. ItemType = 2
  89. RangeOptions = cinfo.Lac15
  90. }
  91. 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
  92. // if inspection_reference.ID > 0 {
  93. // ItemType = int64(inspection_reference.RangeType)
  94. // }
  95. if total <= 0 {
  96. inspection_reference.OrgId = org_id
  97. if len(cinfo.Bbx05a) > 0 {
  98. inspection_reference.ProjectName = cinfo.Bbx05a
  99. inspection_reference.Project = cinfo.Bbx05a
  100. } else {
  101. inspection_reference.ProjectName = cinfo.Bbx05
  102. inspection_reference.Project = cinfo.Bbx05
  103. }
  104. inspection_reference.ProjectId = project_id
  105. inspection_reference.ItemName = cinfo.Bbx05
  106. inspection_reference.ItemId = item_id
  107. inspection_reference.RangeType = ItemType
  108. inspection_reference.RangeMin = RangeMin
  109. inspection_reference.RangeMax = RangeMax
  110. // inspection_reference.RangeValue = RangeValue
  111. inspection_reference.RangeOptions = RangeOptions
  112. inspection_reference.Unit = cinfo.Bdg02
  113. inspection_reference.Status = 1
  114. inspection_reference.CreatedTime = time.Now().Unix()
  115. inspection_reference.UpdatedTime = time.Now().Unix()
  116. inspection_reference.InspectDate = inspect_date
  117. inspection_reference.UTime = inspect_date
  118. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  119. if err != nil {
  120. tx.Rollback()
  121. }
  122. }
  123. var itotal int
  124. 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
  125. if itotal <= 0 {
  126. inspection.PatientId = patient_id
  127. inspection.OrgId = org_id
  128. inspection.ProjectId = project_id
  129. inspection.ItemName = inspection_reference.ItemName
  130. inspection.ProjectName = inspection_reference.ProjectName
  131. inspection.InspectType = ItemType
  132. inspection.ItemId = item_id
  133. inspection.InspectValue = cinfo.Lac10
  134. inspection.InspectDate = inspect_date
  135. inspection.RecordDate = record_date
  136. inspection.InspectTips = cinfo.Lac11
  137. inspection.Status = 1
  138. inspection.CreatedTime = time.Now().Unix()
  139. inspection.UpdatedTime = time.Now().Unix()
  140. inspection.UTime = inspect_date
  141. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  142. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  143. if err != nil {
  144. tx.Rollback()
  145. }
  146. }
  147. tx.Commit()
  148. }
  149. }
  150. }
  151. } else {
  152. continue
  153. }
  154. }
  155. }
  156. var syncInfo models.MiddleSyncInfo
  157. syncInfo.OrgId = org_id
  158. syncInfo.SyncTime = time.Now().Unix()
  159. syncInfo.SyncResultType = 1
  160. syncInfo.SyncRsultRemark = "同步成功"
  161. syncInfo.SyncTotalNum = 0
  162. syncInfo.SyncSuccessNum = 0
  163. syncInfo.SyncInfo = ""
  164. syncInfo.CreateTime = time.Now().Unix()
  165. syncInfo.UpdateTime = time.Now().Unix()
  166. cwderr := CreateSyncInfo(&syncInfo)
  167. if cwderr != nil {
  168. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  169. return
  170. }
  171. SyncToCsnytx()
  172. return
  173. }
  174. type LabCsnyResult struct {
  175. Msg string `json:"msg"`
  176. Result []struct {
  177. Lab01 int64 `json:"lab01"`
  178. Lab58 string `json:"lab58"`
  179. } `json:"result"`
  180. Code string `json:"code"`
  181. }
  182. func CsnyGetLab(id_card_no string, synctime string) (string, string) {
  183. inputData := make(map[string]interface{})
  184. inputData["vaa15"] = id_card_no
  185. inputData["begintime"] = synctime
  186. var inputLog string
  187. bytesData, err := json.Marshal(inputData)
  188. inputLog = string(bytesData)
  189. fmt.Println(string(bytesData))
  190. if err != nil {
  191. fmt.Println(err.Error())
  192. return err.Error(), ""
  193. }
  194. reader := bytes.NewReader(bytesData)
  195. url := "http://218.104.146.179:9091/esb/listener/getLab1"
  196. request, err := http.NewRequest("POST", url, reader)
  197. if err != nil {
  198. fmt.Println(err.Error())
  199. return err.Error(), ""
  200. }
  201. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  202. request.Header.Set("code", "Xmrjyy")
  203. client := http.Client{}
  204. resp, err := client.Do(request)
  205. if err != nil {
  206. fmt.Println(err.Error())
  207. return err.Error(), ""
  208. }
  209. respBytes, err := ioutil.ReadAll(resp.Body)
  210. if err != nil {
  211. fmt.Println(err.Error())
  212. return err.Error(), ""
  213. }
  214. str := string(respBytes)
  215. return str, inputLog
  216. }
  217. type LacCsnyResult struct {
  218. Msg string `json:"msg"`
  219. Result []struct {
  220. Lab01 int64 `json:"labo1"`
  221. Bbx05 string `json:"BBX05"`
  222. Bbx05a string `json:"BBX05A"`
  223. Bfc03 string `json:"BFC03"`
  224. Bdg02 string `json:"BDG02"`
  225. Lac10 string `json:"LAC10"`
  226. Lac11 string `json:"LAC11"`
  227. Lac13 string `json:"LAC13"`
  228. Lac14 string `json:"LAC14"`
  229. Lac15 string `json:"LAC15"`
  230. Lac25 string `json:"LAC25"`
  231. } `json:"result"`
  232. Code string `json:"code"`
  233. }
  234. func CsnyGetLac(lab01 int64) (string, string) {
  235. inputData := make(map[string]interface{})
  236. inputData["lab01"] = lab01
  237. var inputLog string
  238. bytesData, err := json.Marshal(inputData)
  239. inputLog = string(bytesData)
  240. fmt.Println(string(bytesData))
  241. if err != nil {
  242. fmt.Println(err.Error())
  243. return err.Error(), ""
  244. }
  245. reader := bytes.NewReader(bytesData)
  246. url := "http://218.104.146.179:9091/esb/listener/getLac1"
  247. request, err := http.NewRequest("POST", url, reader)
  248. if err != nil {
  249. fmt.Println(err.Error())
  250. return err.Error(), ""
  251. }
  252. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  253. request.Header.Set("code", "Xmrjyy")
  254. client := http.Client{}
  255. resp, err := client.Do(request)
  256. if err != nil {
  257. fmt.Println(err.Error())
  258. return err.Error(), ""
  259. }
  260. respBytes, err := ioutil.ReadAll(resp.Body)
  261. if err != nil {
  262. fmt.Println(err.Error())
  263. return err.Error(), ""
  264. }
  265. str := string(respBytes)
  266. return str, inputLog
  267. }
  268. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  269. func GetCsnyProjectID(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 340001, err
  280. }
  281. }
  282. }
  283. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  284. func GetCsnyItemID(org_id int64,project_name string,item_name string,project_id int64) (item_id int64 ,err error) {
  285. var inspection_reference models.MiddleInspectionReference
  286. 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
  287. if inspection_reference.ID > 0 {
  288. return inspection_reference.ItemId,err
  289. } else {
  290. 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
  291. utils.InfoLog("inspection_reference: %v",inspection_reference)
  292. if inspection_reference.ItemId > 0 {
  293. return inspection_reference.ItemId + 1,err
  294. } else {
  295. return project_id * 100 + 1 ,err
  296. }
  297. }
  298. }
  299. func SyncToCsnytx() {
  300. utils.TraceLog("检验检查同步任务开始执行")
  301. org_id := int64(10340)
  302. // 第一步:跟进org_id 去中间库查出需要同步的数据
  303. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  304. inspections, _ := GetSyncInspectionByOrgId(org_id)
  305. // 第二步:将数据同步到业务库
  306. if len(inspection_references) > 0 {
  307. for _, inspection_reference := range inspection_references {
  308. SyncInspectionReference(&inspection_reference)
  309. }
  310. }
  311. if len(inspections) > 0 {
  312. for _, inspection := range inspections {
  313. SyncInspection(&inspection)
  314. }
  315. }
  316. utils.SuccessLog("检验检查同步任务完成")
  317. }