tscyyy_service.go 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. _ "fmt"
  12. "github.com/astaxie/beego/httplib"
  13. _ "github.com/jinzhu/gorm"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. type tscylis struct {
  19. HisUserId string `json:"his_user_id"`
  20. PatientName string `json:"patient_name"`
  21. ProjectId string `json:"project_id"`
  22. ProjectName string `json:"project_name"`
  23. InspectDate string `json:"inspect_date"`
  24. ItemId string `json:"item_id"`
  25. ItemName string `json:"item_name"`
  26. ItemType string `json:"item_type"`
  27. ItemValue string `json:"item_value"`
  28. RangeMin string `json:"range_min"`
  29. RangeMax string `json:"range_max"`
  30. RangeValue string `json:"range_value"`
  31. RangeOptions string `json:"range_options"`
  32. Unit string `json:"unit"`
  33. State string `json:"state"`
  34. CTime string `json:"c_time"`
  35. UTime string `json:"u_time"`
  36. }
  37. type tscyLisResult struct {
  38. ErrorCode int64 `json:"error_code"`
  39. Result []tscylis `json:"data"`
  40. }
  41. func GetTscyLis(sync_time string, his_user_id string) (str tscyLisResult, checkStr string) {
  42. apiurl := "http://60.2.56.98:8797/tscy/lis_report.do"
  43. rep := httplib.Get(apiurl)
  44. rep.Param("sync_time", sync_time)
  45. rep.Param("his_user_id", his_user_id)
  46. strs, err := rep.String()
  47. if err != nil {
  48. utils.InfoLog("err:%v", err)
  49. }
  50. utils.InfoLog("str:%v", strs)
  51. var checkJSON tscyLisResult
  52. json.Unmarshal([]byte(strs), &checkJSON)
  53. // utils.InfoLog("checkJSON:%v", checkJSON)
  54. return checkJSON, strs
  55. }
  56. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  57. func GetTscyProjectID(org_id int64, project_name string) (project_id int64, err error) {
  58. var inspection_reference models.MiddleInspectionReference
  59. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  60. if inspection_reference.ID > 0 {
  61. return inspection_reference.ProjectId, err
  62. } else {
  63. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  64. if inspection_reference.ProjectId > 0 {
  65. return inspection_reference.ProjectId + 1, err
  66. } else {
  67. return 92001, err
  68. }
  69. }
  70. }
  71. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  72. func GetTscyItemID(org_id int64,project_name string,item_name string,project_id int64) (item_id int64 ,err error) {
  73. var inspection_reference models.MiddleInspectionReference
  74. 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
  75. if inspection_reference.ID > 0 {
  76. return inspection_reference.ItemId,err
  77. } else {
  78. 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
  79. utils.InfoLog("inspection_reference: %v",inspection_reference)
  80. if inspection_reference.ItemId > 0 {
  81. return inspection_reference.ItemId + 1,err
  82. } else {
  83. return project_id * 100 + 1 ,err
  84. }
  85. }
  86. }
  87. // 唐山朝阳医院透析中心Lis同步
  88. func SyncTscyLis() (err error) {
  89. org_id := int64(9882)
  90. // 第一步:获取上一次同步的时间点
  91. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  92. var sync_time int64
  93. if syncLastInfo.ID > 0 {
  94. sync_time = syncLastInfo.SyncTime
  95. } else {
  96. sync_time = 1651334400
  97. }
  98. // 第二步:获取所有患者的病历号
  99. patientList, _ := GetPatientGzjhByOrgId(org_id)
  100. if len(patientList) > 0 {
  101. for _, patient := range patientList {
  102. if len(patient.IdCardNo) > 0 || len(patient.DialysisNo) > 0 {
  103. // 上次同步时间转换为接口需要的格式
  104. patient_id := patient.ID
  105. // sync_time_temp := time.Unix(sync_time,0)
  106. // syncTimeStr := sync_time_temp.Format("2006010215:04:05")
  107. utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
  108. // 获取会员号
  109. // utils.InfoLog("VipStr:%v", VipStr)
  110. gzjhLis, _ := GetTscyLis(strconv.FormatInt(sync_time, 10), patient.IdCardNo)
  111. // if len(gzjhLis.Result) == 0 {
  112. // gzjhLis,_ = GethbdyLis(strconv.FormatInt(sync_time,10),VipStr)
  113. // }
  114. // utils.InfoLog("yzstr:%v", gzjhLis)
  115. if len(gzjhLis.Result) > 0 {
  116. // timeLayout := "2006010215:04:05" // 转化所需模板
  117. // loc, _ := time.LoadLocation("Local") // 重要:获取时区
  118. for _, info := range gzjhLis.Result {
  119. utils.InfoLog("Result:%v", info)
  120. // 根据project_id和item_id 判断该项是否存在
  121. project_id := int64(0)
  122. // if info.ProjectId > 0 {
  123. project_id, _ = GetTscyProjectID(org_id, info.ProjectName)
  124. // } else {
  125. // continue
  126. // }
  127. item_id, _ := GetTscyItemID(org_id, info.ProjectName, info.ItemName, project_id)
  128. tx := writeMiddleDb.Begin()
  129. var inspection models.MiddleInspection
  130. var inspection_reference models.MiddleInspectionReference
  131. record_date,_ := strconv.ParseInt(info.InspectDate, 10, 64)
  132. if record_date == 0 {
  133. record_date = time.Now().Unix()
  134. }
  135. // record_date_str, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  136. // record_date := record_date_str.Unix()
  137. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  138. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  139. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  140. var total int
  141. var RangeOptions string
  142. var RangeMin string
  143. var RangeMax string
  144. // // 判断检查类型
  145. ItemType := info.ItemType
  146. // Range := strings.Split(info.RangeValue, "-")
  147. // if len(Range) > 1 {
  148. RangeMin = info.RangeMin
  149. RangeMax = info.RangeMax
  150. // ItemType = 1
  151. // } else {
  152. // ItemType = 2
  153. RangeOptions = info.RangeOptions
  154. // }
  155. 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
  156. // if inspection_reference.ID > 0 {
  157. // ItemType = int64(inspection_reference.RangeType)
  158. // }
  159. if total <= 0 {
  160. inspection_reference.OrgId = org_id
  161. inspection_reference.ProjectName = info.ProjectName
  162. inspection_reference.Project = info.ProjectName
  163. inspection_reference.ProjectId = project_id
  164. inspection_reference.ItemName = info.ItemName
  165. inspection_reference.ItemNameAddition = info.ItemId
  166. inspection_reference.ItemId = item_id
  167. inspection_reference.RangeType, _ = strconv.Atoi(ItemType)
  168. inspection_reference.RangeMin = RangeMin
  169. inspection_reference.RangeMax = RangeMax
  170. // inspection_reference.RangeValue = RangeValue
  171. inspection_reference.RangeOptions = RangeOptions
  172. inspection_reference.Unit = info.Unit
  173. inspection_reference.Status = 1
  174. inspection_reference.CreatedTime = time.Now().Unix()
  175. inspection_reference.UpdatedTime = time.Now().Unix()
  176. inspection_reference.InspectDate = inspect_date
  177. inspection_reference.UTime = inspect_date
  178. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  179. if err != nil {
  180. tx.Rollback()
  181. }
  182. }
  183. var itotal int
  184. 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
  185. if itotal <= 0 {
  186. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  187. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  188. inspection.PatientId = patient_id
  189. inspection.OrgId = org_id
  190. inspection.ProjectId = project_id
  191. inspection.ItemName = inspection_reference.ItemName
  192. inspection.ProjectName = inspection_reference.ProjectName
  193. inspection.InspectType, _ = strconv.Atoi(ItemType)
  194. inspection.ItemId = item_id
  195. inspection.InspectValue = info.ItemValue
  196. inspection.InspectDate = inspect_date
  197. inspection.RecordDate = record_date
  198. // inspection.InspectTips = report.Resultstate
  199. inspection.Status = 1
  200. inspection.CreatedTime = time.Now().Unix()
  201. inspection.UpdatedTime = time.Now().Unix()
  202. inspection.UTime = inspect_date
  203. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  204. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  205. if err != nil {
  206. tx.Rollback()
  207. }
  208. }
  209. tx.Commit()
  210. }
  211. }
  212. } else {
  213. continue
  214. }
  215. }
  216. }
  217. var syncInfo models.MiddleSyncInfo
  218. syncInfo.OrgId = org_id
  219. syncInfo.SyncTime = time.Now().Unix()
  220. syncInfo.SyncResultType = 1
  221. syncInfo.SyncRsultRemark = "同步成功"
  222. syncInfo.SyncTotalNum = 0
  223. syncInfo.SyncSuccessNum = 0
  224. syncInfo.SyncInfo = ""
  225. syncInfo.CreateTime = time.Now().Unix()
  226. syncInfo.UpdateTime = time.Now().Unix()
  227. cwderr := CreateSyncInfo(&syncInfo)
  228. if cwderr != nil {
  229. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  230. return
  231. }
  232. SyncToTscytx()
  233. return
  234. }
  235. func SyncToTscytx() {
  236. utils.TraceLog("检验检查同步任务开始执行")
  237. org_id := int64(9882)
  238. // 第一步:跟进org_id 去中间库查出需要同步的数据
  239. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  240. inspections, _ := GetSyncInspectionByOrgId(org_id)
  241. // 第二步:将数据同步到业务库
  242. if len(inspection_references) > 0 {
  243. for _, inspection_reference := range inspection_references {
  244. SyncInspectionReference(&inspection_reference)
  245. }
  246. }
  247. if len(inspections) > 0 {
  248. for _, inspection := range inspections {
  249. SyncInspection(&inspection)
  250. }
  251. }
  252. utils.SuccessLog("检验检查同步任务完成")
  253. }