pajm_service.go 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. _ "encoding/json"
  6. _ "encoding/xml"
  7. "github.com/jinzhu/gorm"
  8. _ "github.com/jinzhu/gorm"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type scpalis struct {
  14. HisUserId string `gorm:"column:his_user_id" json:"his_user_id"`
  15. PatientName string `gorm:"column:patient_name" json:"patient_name"`
  16. ProjectId int64 `gorm:"column:project_id" json:"project_id"`
  17. ProjectName string `gorm:"column:project_name" json:"project_name"`
  18. InspectDate int64 `gorm:"column:inspect_date" json:"inspect_date"`
  19. ItemId int64 `gorm:"column:item_id" json:"item_id"`
  20. ItemName string `gorm:"column:item_name" json:"item_name"`
  21. ItemType int64 `gorm:"column:item_type" json:"item_type"`
  22. ItemValue string `gorm:"column:item_value" json:"item_value"`
  23. RangeMin string `gorm:"column:range_min" json:"range_min"`
  24. RangeMax string `gorm:"column:range_max" json:"range_max"`
  25. RangeValue string `gorm:"column:range_value" json:"range_value"`
  26. RangeOptions string `gorm:"column:range_options" json:"range_options"`
  27. Unit string `gorm:"column:unit" json:"unit"`
  28. State string `gorm:"column:state" json:"state"`
  29. CTime int64 `gorm:"column:c_time" json:"c_time"`
  30. UTime int64 `gorm:"column:u_time" json:"u_time"`
  31. }
  32. func (scpalis) TableName() string {
  33. return "检验检查信息"
  34. }
  35. func GetScpaLis(rdb *gorm.DB, synctime int64, hisuserid string) (record []*scpalis, err error) {
  36. err = rdb.Model(&scpalis{}).Where("inspect_date >= ? and his_user_id = ?", synctime, hisuserid).Find(&record).Error
  37. return
  38. }
  39. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  40. func GetScpaProjectID(org_id int64, project_name string) (project_id int64, err error) {
  41. var inspection_reference models.MiddleInspectionReference
  42. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  43. if inspection_reference.ID > 0 {
  44. return inspection_reference.ProjectId, err
  45. } else {
  46. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  47. if inspection_reference.ProjectId > 0 {
  48. return inspection_reference.ProjectId + 1, err
  49. } else {
  50. return 9829001, err
  51. }
  52. }
  53. }
  54. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  55. func GetScpaItemID(org_id int64, project_name string, item_name string, project_id int64) (item_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 = ? and item_name = ?", org_id, project_name, item_name).First(&inspection_reference).Error
  58. if inspection_reference.ID > 0 {
  59. return inspection_reference.ItemId, err
  60. } else {
  61. 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
  62. utils.InfoLog("inspection_reference: %v", inspection_reference)
  63. if inspection_reference.ItemId > 0 {
  64. return inspection_reference.ItemId + 1, err
  65. } else {
  66. return project_id*100 + 1, err
  67. }
  68. }
  69. }
  70. func GetPatientPajmByOrgId(org_id int64) (patientList []*models.Patients, err error) {
  71. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? and status = 1", org_id).Find(&patientList).Error
  72. return
  73. }
  74. // 四川蓬安济民透析中心Lis同步
  75. func SyncScpaLis() (err error) {
  76. org_id := int64(9829)
  77. org := &models.DataUploadConfig{
  78. OrgId: org_id,
  79. DbHost: "192.168.0.254",
  80. DbPort: "1433",
  81. DbPass: "999999",
  82. DbUser: "sa",
  83. DbName: "nfjiminhis",
  84. }
  85. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  86. if err != nil {
  87. utils.ErrorLog("创建数据库连接失败:%v", err)
  88. return
  89. }
  90. // var sync_time int64
  91. // sync_time = 1627747200
  92. // scpaLis, _ := GetScpaLis(orgDb, sync_time,"512926196302182682")
  93. // utils.InfoLog("IdCardNo:%v",scpaLis)
  94. // 第一步:获取上一次同步的时间点
  95. syncLastInfo, _ := GetSyncTimeByOrgIDForYs(org_id)
  96. var sync_time int64
  97. if syncLastInfo.ID > 0 {
  98. sync_time = syncLastInfo.SyncTime
  99. } else {
  100. sync_time = 1627747200
  101. }
  102. // 第二步:获取所有患者的病历号
  103. patientList, _ := GetPatientPajmByOrgId(org_id)
  104. if len(patientList) > 0 {
  105. for _, patient := range patientList {
  106. if len(patient.IdCardNo) > 0 {
  107. // 上次同步时间转换为接口需要的格式
  108. patient_id := patient.ID
  109. utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
  110. scpaLis, _ := GetScpaLis(orgDb, sync_time, patient.IdCardNo)
  111. if len(scpaLis) > 0 {
  112. for _, info := range scpaLis {
  113. utils.InfoLog("Result:%v", info)
  114. // 根据project_id和item_id 判断该项是否存在
  115. project_id := int64(0)
  116. if info.ProjectId > 0 {
  117. project_id, _ = GetScpaProjectID(org_id, info.ProjectName)
  118. } else {
  119. continue
  120. }
  121. item_id, _ := GetScpaItemID(org_id, info.ProjectName, info.ItemName, project_id)
  122. tx := writeMiddleDb.Begin()
  123. var inspection models.MiddleInspection
  124. var inspection_reference models.MiddleInspectionReference
  125. record_date := info.InspectDate
  126. if info.InspectDate == 0 {
  127. record_date = time.Now().Unix()
  128. }
  129. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  130. var total int
  131. var RangeOptions string
  132. var RangeMin string
  133. var RangeMax string
  134. // // 判断检查类型
  135. ItemType := info.ItemType
  136. RangeMin = info.RangeMin
  137. RangeMax = info.RangeMax
  138. RangeOptions = info.RangeValue
  139. // Range := strings.Split(info.RangeValue, "-")
  140. // if len(Range) > 1 {
  141. // RangeMin = Range[0]
  142. // RangeMax = Range[1]
  143. // ItemType = 1
  144. // } else {
  145. // ItemType = 2
  146. // RangeOptions = info.RangeOptions
  147. // }
  148. 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
  149. if total <= 0 {
  150. inspection_reference.OrgId = org_id
  151. inspection_reference.ProjectName = info.ProjectName
  152. inspection_reference.Project = info.ProjectName
  153. inspection_reference.ProjectId = project_id
  154. inspection_reference.ItemName = info.ItemName
  155. inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  156. inspection_reference.ItemId = item_id
  157. inspection_reference.RangeType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  158. inspection_reference.RangeMin = RangeMin
  159. inspection_reference.RangeMax = RangeMax
  160. // inspection_reference.RangeValue = RangeValue
  161. inspection_reference.RangeOptions = RangeOptions
  162. inspection_reference.Unit = info.Unit
  163. inspection_reference.Status = 1
  164. inspection_reference.CreatedTime = time.Now().Unix()
  165. inspection_reference.UpdatedTime = time.Now().Unix()
  166. inspection_reference.InspectDate = inspect_date
  167. inspection_reference.UTime = inspect_date
  168. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  169. if err != nil {
  170. tx.Rollback()
  171. }
  172. }
  173. var itotal int
  174. 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
  175. if itotal <= 0 {
  176. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  177. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  178. inspection.PatientId = patient_id
  179. inspection.OrgId = org_id
  180. inspection.ProjectId = project_id
  181. inspection.ItemName = inspection_reference.ItemName
  182. inspection.ProjectName = inspection_reference.ProjectName
  183. inspection.InspectType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  184. inspection.ItemId = item_id
  185. inspection.InspectValue = info.ItemValue
  186. inspection.InspectDate = inspect_date
  187. inspection.RecordDate = record_date
  188. // inspection.InspectTips = report.Resultstate
  189. inspection.Status = 1
  190. inspection.CreatedTime = time.Now().Unix()
  191. inspection.UpdatedTime = time.Now().Unix()
  192. inspection.UTime = inspect_date
  193. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  194. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  195. if err != nil {
  196. tx.Rollback()
  197. }
  198. }
  199. tx.Commit()
  200. }
  201. }
  202. } else {
  203. continue
  204. }
  205. }
  206. }
  207. var syncInfo models.MiddleSyncInfo
  208. syncInfo.OrgId = org_id
  209. syncInfo.SyncTime = time.Now().Unix()
  210. syncInfo.SyncResultType = 1
  211. syncInfo.SyncRsultRemark = "同步成功"
  212. syncInfo.SyncTotalNum = 0
  213. syncInfo.SyncSuccessNum = 0
  214. syncInfo.SyncInfo = ""
  215. syncInfo.CreateTime = time.Now().Unix()
  216. syncInfo.UpdateTime = time.Now().Unix()
  217. cwderr := CreateSyncInfo(&syncInfo)
  218. if cwderr != nil {
  219. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  220. return
  221. }
  222. SyncToScpatx()
  223. return
  224. }
  225. func SyncToScpatx() {
  226. utils.TraceLog("检验检查同步任务开始执行")
  227. org_id := int64(9829)
  228. // 第一步:跟进org_id 去中间库查出需要同步的数据
  229. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  230. inspections, _ := GetSyncInspectionByOrgId(org_id)
  231. // 第二步:将数据同步到业务库
  232. if len(inspection_references) > 0 {
  233. for _, inspection_reference := range inspection_references {
  234. SyncInspectionReference(&inspection_reference)
  235. }
  236. }
  237. if len(inspections) > 0 {
  238. for _, inspection := range inspections {
  239. SyncInspection(&inspection)
  240. }
  241. }
  242. utils.SuccessLog("检验检查同步任务完成")
  243. }