fjtf_service.go 11KB

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