chyd_service.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. _ "IC/utils"
  6. "encoding/json"
  7. _ "encoding/json"
  8. "encoding/xml"
  9. _ "fmt"
  10. "github.com/astaxie/beego/httplib"
  11. _ "github.com/jinzhu/gorm"
  12. "net/url"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. type XmlKeyChyd struct {
  18. XMLName xml.Name `xml:"string"`
  19. StringName string `xml:"name,attr"`
  20. InnerText string `xml:",innerxml"`
  21. }
  22. // 获取艾迪康的key
  23. func GetAdKeyChyd(logid string, password string) (key string) {
  24. Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/Login?"
  25. apiurl := "logid=" + logid + "&password=" + password
  26. apiurl = url.PathEscape(apiurl)
  27. apiurl = Domain + apiurl
  28. XmlKeyChyd := XmlKeyChyd{}
  29. httplib.Get(apiurl).ToXML(&XmlKeyChyd)
  30. return XmlKeyChyd.InnerText
  31. }
  32. type ADStringChyd struct {
  33. XMLName xml.Name `xml:"string" json:"string"`
  34. NewDataSetChyd rpnewDataSetChyd `xml:"NewDataSet" json:"NewDataSet"`
  35. }
  36. type rpnewDataSetChyd struct {
  37. ListTableChyd []rplisttableChyd `xml:"listtable" json:"listtable"`
  38. }
  39. type rplisttableChyd struct {
  40. Id string `xml:"Id" json:"Id"`
  41. ReportType string `xml:"ReportType" json:"ReportType"`
  42. AdiconBarcode string `xml:"AdiconBarcode" json:"AdiconBarcode"`
  43. PatientName string `xml:"PatientName" json:"PatientName"`
  44. Bgrq string `xml:"Bgrq" json:"Bgrq"`
  45. Instrument string `xml:"Instrument" json:"Instrument"`
  46. LisDate string `xml:"LisDate" json:"LisDate"`
  47. Sampleid string `xml:"Sampleid" json:"Sampleid"`
  48. CustomerBarcode string `xml:"CustomerBarcode" json:"CustomerBarcode"`
  49. Repno string `xml:"Repno" json:"Repno"`
  50. // PdfFileName string `xml:"PdfFileName" json:"PdfFileName"`
  51. // DownFlag string `xml:"DownFlag" json:"DownFlag"`
  52. // DownDate string `xml:"DownDate" json:"DownDate"`
  53. Sjrq string `xml:"Sjrq" json:"Sjrq"`
  54. // Sjys string `xml:"Sjys" json:"Sjys"`
  55. // Brnl string `xml:"Brnl" json:"Brnl"`
  56. // Brxb string `xml:"Brxb" json:"Brxb"`
  57. Bbzl string `xml:"bbzl" json:"bbzl"`
  58. // Bzlbid string `xml:"Bzlbid" json:"Bzlbid"`
  59. // PDFFileType string `xml:"PDFFileType" json:"PDFFileType"`
  60. // PageType string `xml:"PageType" json:"PageType"`
  61. PatientNo string `xml:"PatientNo" json:"PatientNo"`
  62. // SfzId string `xml:"SfzId" json:"SfzId"`
  63. // CertificatesType string `xml:"CertificatesType" json:"CertificatesType"`
  64. // Nationality string `xml:"Nationality" json:"Nationality"`
  65. // SerialNumber string `xml:"SerialNumber" json:"SerialNumber"`
  66. // Str1 string `xml:"Str1" json:"Str1"`
  67. // Str2 string `xml:"Str2" json:"Str2"`
  68. // Str3 string `xml:"Str3" json:"Str3"`
  69. // Str4 string `xml:"Str4" json:"Str4"`
  70. // Str5 string `xml:"Str5" json:"Str5"`
  71. // TWBG string `xml:"TWBG" json:"TWBG"`
  72. }
  73. // 获取机构一段时间内检验检查列表
  74. func GetAdGetReportListChyd(Key string, BeginDateTime string, EndDateTime string, TypeDateTime string, AgainFlag string) (key ADStringChyd) {
  75. Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetReportList"
  76. //apiurl := "Key=" + Key + "&BeginDateTime=" + BeginDateTime + "&EndDateTime=" + EndDateTime + "&TypeDateTime=" + TypeDateTime + "&AgainFlag=" + AgainFlag
  77. //apiurl = url.PathEscape(apiurl)
  78. //apiurl = Domain + apiurl
  79. //utils.InfoLog("url0: %v", apiurl)
  80. var xmlKey ADStringChyd
  81. req := httplib.Post(Domain)
  82. req.Param("Key", Key)
  83. req.Param("BeginDateTime", BeginDateTime)
  84. req.Param("EndDateTime", EndDateTime)
  85. req.Param("TypeDateTime", TypeDateTime)
  86. req.Param("AgainFlag", AgainFlag)
  87. xmlString, _ := req.String()
  88. xmlString = strings.Replace(xmlString, "&lt;", "<", -1)
  89. xmlString = strings.Replace(xmlString, "&gt;", ">", -1)
  90. xml.Unmarshal([]byte(xmlString), &xmlKey)
  91. return xmlKey
  92. }
  93. type AdkCheckChyd struct {
  94. Cgbg AdkSResult `json:"常规报告"`
  95. }
  96. type AdkSResult struct {
  97. Item []AdkItem `json:"item"`
  98. }
  99. type AdkItem struct {
  100. Adiconbarcode string `json:"AdiconBarcode"`
  101. Patientname string `json:"PatientName"`
  102. Sex string `json:"Sex"`
  103. Age string `json:"Age"`
  104. Reportdate string `json:"ReportDate"`
  105. Itemcode string `json:"ItemCode"`
  106. ItemnameCn string `json:"ItemName_CN"`
  107. ItemnameEn string `json:"ItemName_EN"`
  108. Result string `json:"Result"`
  109. Resulthint string `json:"ResultHint"`
  110. Resultreference string `json:"ResultReference"`
  111. Resultunit string `json:"ResultUnit"`
  112. Zhmc string `json:"zhmc"`
  113. }
  114. func GetAdkInfoChyd(key string, id string) (str AdkCheckChyd, checkStr string) {
  115. Domain := "http://183.63.151.26:8081/AdWebService_new/ADReportWebService.asmx/GetJSONReportItemListByAdiconBarocde?"
  116. //apiurl := "key=" + key + "&AdiconBarocde=" + id
  117. //apiurl = url.PathEscape(apiurl)
  118. //apiurl = Domain + apiurl
  119. //utils.InfoLog("url1: %v", apiurl)
  120. xmlKey := XmlKeyChyd{}
  121. rep := httplib.Post(Domain)
  122. rep.Param("Key", key)
  123. rep.Param("AdiconBarcode", id)
  124. rep.ToXML(&xmlKey)
  125. utils.InfoLog("InnerText: %v", xmlKey.InnerText)
  126. var checkJSON AdkCheckChyd
  127. json.Unmarshal([]byte(xmlKey.InnerText), &checkJSON)
  128. return checkJSON, xmlKey.InnerText
  129. }
  130. // 从化意达透析中心Lis同步
  131. func SyncLisChyd(flag int64) (err error) {
  132. // 第一步:获取艾迪康下发的Key
  133. org_id := int64(10318)
  134. login := "RH0222"
  135. password := "Abc123123"
  136. key := GetAdKeyChyd(login, password)
  137. utils.InfoLog("key:%v", key)
  138. // 第二步:获取上一次同步的时间点
  139. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  140. var sync_time int64
  141. if syncLastInfo.ID > 0 {
  142. sync_time = syncLastInfo.SyncTime
  143. } else {
  144. sync_time = 1704038400
  145. }
  146. if flag > 0 {
  147. sync_time = 1704038400
  148. }
  149. nowTimes := time.Now().Unix()
  150. // syncTimes := time.Unix(sync_time,0).Format("2006-01-02")
  151. if sync_time < nowTimes {
  152. for i := sync_time; i < nowTimes; i = i + 86400 {
  153. tempsyncTimes := time.Unix(i, 0).Format("2006-01-02")
  154. adString := GetAdGetReportListChyd(key, tempsyncTimes, tempsyncTimes, "2", "1")
  155. listtable := adString.NewDataSetChyd.ListTableChyd
  156. if len(listtable) >= 1 {
  157. // 第三步:根据检查列表获取的ID来获取检验检查详情
  158. for _, list := range listtable {
  159. listId := list.AdiconBarcode
  160. checkInfo, _ := GetAdkInfoChyd(key, listId)
  161. //utils.InfoLog("checkStr: %v", checkStr)
  162. for _, info := range checkInfo.Cgbg.Item {
  163. // 根据姓名 去查询对应的patient_id
  164. patient, _ := GetPatientID(org_id, info.Patientname)
  165. utils.InfoLog("patient: %v", patient)
  166. if patient.ID > 0 {
  167. ChydReportInfoHandle(info, org_id, patient.ID)
  168. }
  169. }
  170. }
  171. var syncInfo models.MiddleSyncInfo
  172. syncInfo.OrgId = org_id
  173. syncInfo.SyncTime = i
  174. syncInfo.SyncResultType = 1
  175. syncInfo.SyncRsultRemark = "同步成功"
  176. syncInfo.SyncTotalNum = 0
  177. syncInfo.SyncSuccessNum = 0
  178. syncInfo.SyncInfo = ""
  179. syncInfo.CreateTime = time.Now().Unix()
  180. syncInfo.UpdateTime = time.Now().Unix()
  181. cwderr := CreateSyncInfo(&syncInfo)
  182. if cwderr != nil {
  183. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  184. return
  185. }
  186. }
  187. }
  188. }
  189. SyncToChyd()
  190. return
  191. }
  192. func SyncToChyd() {
  193. utils.TraceLog("检验检查同步任务开始执行")
  194. org_id := int64(10318)
  195. // 第一步:跟进org_id 去中间库查出需要同步的数据
  196. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  197. inspections, _ := GetSyncInspectionByOrgId(org_id)
  198. // 第二步:将数据同步到业务库
  199. if len(inspection_references) > 0 {
  200. for _, inspection_reference := range inspection_references {
  201. SyncInspectionReference(&inspection_reference)
  202. }
  203. }
  204. if len(inspections) > 0 {
  205. for _, inspection := range inspections {
  206. SyncInspection(&inspection)
  207. }
  208. }
  209. utils.SuccessLog("检验检查同步任务完成")
  210. }
  211. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  212. func GetChydItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  213. var inspection_reference models.MiddleInspectionReference
  214. 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
  215. if inspection_reference.ID > 0 {
  216. return inspection_reference.ItemId, err
  217. } else {
  218. 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
  219. utils.InfoLog("inspection_reference: %v", inspection_reference)
  220. if inspection_reference.ItemId > 0 {
  221. return inspection_reference.ItemId + 1, err
  222. } else {
  223. return project_id*100 + 1, err
  224. }
  225. }
  226. }
  227. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  228. func GetChydProjectID(org_id int64, project_name string) (project_id int64, err error) {
  229. var inspection_reference models.MiddleInspectionReference
  230. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  231. if inspection_reference.ID > 0 {
  232. return inspection_reference.ProjectId, err
  233. } else {
  234. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  235. if inspection_reference.ProjectId > 0 {
  236. return inspection_reference.ProjectId + 1, err
  237. } else {
  238. return 1031801, err
  239. }
  240. }
  241. }
  242. func ChydReportInfoHandle(CheckInfo AdkItem, org_id int64, patientId int64) {
  243. tx := writeMiddleDb.Begin()
  244. var total int
  245. var RangeMin string
  246. var RangeMax string
  247. timstr := strings.Split(CheckInfo.Reportdate, ".")
  248. recordDateStr := timstr[0]
  249. if len(recordDateStr) == 0 {
  250. recordDateStr = time.Now().Format("2006-01-02 15:04")
  251. }
  252. record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  253. record_date := record_date_str.Unix()
  254. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  255. // // 判断检查类型
  256. ItemType := 1
  257. Range := strings.Split(CheckInfo.Resultreference, "-")
  258. if len(Range) > 1 {
  259. RangeMin = Range[0]
  260. RangeMax = Range[1]
  261. ItemType = 1
  262. } else {
  263. ItemType = 2
  264. }
  265. var inspection models.MiddleInspection
  266. var inspection_reference models.MiddleInspectionReference
  267. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project = ? and item_code = ? and status = 1", org_id, CheckInfo.Zhmc, CheckInfo.Itemcode).Find(&inspection_reference).Count(&total).Error
  268. if inspection_reference.ID > 0 {
  269. ItemType = inspection_reference.RangeType
  270. }
  271. if total <= 0 {
  272. // 如果配置表里没有相关配置,则创建project_id,item_id和相关配置信息
  273. project_id, _ := GetChydProjectID(org_id, CheckInfo.Zhmc)
  274. item_id, _ := GetChydItemID(org_id, CheckInfo.Zhmc, CheckInfo.ItemnameCn, project_id)
  275. inspection_reference.OrgId = org_id
  276. inspection_reference.ProjectName = CheckInfo.Zhmc
  277. inspection_reference.Project = CheckInfo.Zhmc
  278. inspection_reference.ProjectId = project_id
  279. inspection_reference.ItemName = CheckInfo.ItemnameCn
  280. inspection_reference.ItemNameAddition = CheckInfo.ItemnameEn
  281. inspection_reference.ItemId = item_id
  282. inspection_reference.ItemCode = CheckInfo.Itemcode
  283. inspection_reference.RangeType = ItemType
  284. inspection_reference.RangeMin = RangeMin
  285. inspection_reference.RangeMax = RangeMax
  286. // inspection_reference.RangeValue = RangeValue
  287. //inspection_reference.RangeOptions = RangeOptions
  288. inspection_reference.Unit = CheckInfo.Resultunit
  289. inspection_reference.Status = 1
  290. inspection_reference.CreatedTime = time.Now().Unix()
  291. inspection_reference.UpdatedTime = time.Now().Unix()
  292. inspection_reference.InspectDate = inspect_date
  293. inspection_reference.UTime = inspect_date
  294. inspection_reference.Adiconbarcode = CheckInfo.Adiconbarcode
  295. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  296. if err != nil {
  297. tx.Rollback()
  298. }
  299. }
  300. var itotal int
  301. 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, inspection_reference.ProjectId, inspection_reference.ItemId, record_date, patientId).Find(&inspection).Count(&itotal).Error
  302. if itotal <= 0 {
  303. inspection.PatientId = patientId
  304. inspection.OrgId = org_id
  305. inspection.ProjectId = inspection_reference.ProjectId
  306. inspection.ItemName = inspection_reference.ItemName
  307. inspection.ProjectName = inspection_reference.ProjectName
  308. inspection.InspectType = ItemType
  309. inspection.ItemId = inspection_reference.ItemId
  310. inspection.InspectValue = CheckInfo.Result
  311. inspection.InspectDate = inspect_date
  312. inspection.RecordDate = record_date
  313. //inspection.InspectTips = result.AntiCode
  314. inspection.Status = 1
  315. inspection.CreatedTime = time.Now().Unix()
  316. inspection.UpdatedTime = time.Now().Unix()
  317. inspection.UTime = inspect_date
  318. inspection.HisUserId = strconv.FormatInt(patientId, 10)
  319. inspection.Adiconbarcode = CheckInfo.Adiconbarcode
  320. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  321. if err != nil {
  322. tx.Rollback()
  323. }
  324. }
  325. tx.Commit()
  326. }