pajm_service.go 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 int64 `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. // 四川蓬安济民透析中心Lis同步
  71. func SyncScpaLis() (err error) {
  72. org_id := int64(9829)
  73. org := &models.DataUploadConfig{
  74. OrgId: org_id,
  75. DbHost: "192.168.0.254",
  76. DbPort: "1433",
  77. DbPass: "999999",
  78. DbUser: "sa",
  79. DbName: "nfjiminhis",
  80. }
  81. orgDb, err := CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  82. if err != nil {
  83. utils.ErrorLog("创建数据库连接失败:%v", err)
  84. return
  85. }
  86. // var sync_time int64
  87. // sync_time = 1627747200
  88. // scpaLis, _ := GetScpaLis(orgDb, sync_time,"512926196302182682")
  89. // utils.InfoLog("IdCardNo:%v",scpaLis)
  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 = 1627747200
  97. }
  98. // 第二步:获取所有患者的病历号
  99. patientList, _ := GetPatientGzjhByOrgId(org_id)
  100. if len(patientList) > 0 {
  101. for _, patient := range patientList {
  102. if len(patient.IdCardNo) > 0 {
  103. // 上次同步时间转换为接口需要的格式
  104. patient_id := patient.ID
  105. utils.InfoLog("IdCardNo:%v", patient.IdCardNo)
  106. scpaLis, _ := GetScpaLis(orgDb, sync_time, patient.IdCardNo)
  107. if len(scpaLis) > 0 {
  108. for _, info := range scpaLis {
  109. utils.InfoLog("Result:%v", info)
  110. // 根据project_id和item_id 判断该项是否存在
  111. project_id := int64(0)
  112. if info.ProjectId > 0 {
  113. project_id, _ = GetScpaProjectID(org_id, info.ProjectName)
  114. } else {
  115. continue
  116. }
  117. item_id, _ := GetScpaItemID(org_id, info.ProjectName, info.ItemName, project_id)
  118. tx := writeMiddleDb.Begin()
  119. var inspection models.MiddleInspection
  120. var inspection_reference models.MiddleInspectionReference
  121. record_date := info.InspectDate
  122. if info.InspectDate == 0 {
  123. record_date = time.Now().Unix()
  124. }
  125. inspect_date := time.Unix(record_date, 0).Format("2006-01-02 15:04")
  126. var total int
  127. var RangeOptions string
  128. var RangeMin string
  129. var RangeMax string
  130. // // 判断检查类型
  131. ItemType := info.ItemType
  132. RangeMin = info.RangeMin
  133. RangeMax = info.RangeMax
  134. RangeOptions = info.RangeValue
  135. // Range := strings.Split(info.RangeValue, "-")
  136. // if len(Range) > 1 {
  137. // RangeMin = Range[0]
  138. // RangeMax = Range[1]
  139. // ItemType = 1
  140. // } else {
  141. // ItemType = 2
  142. // RangeOptions = info.RangeOptions
  143. // }
  144. 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
  145. if total <= 0 {
  146. inspection_reference.OrgId = org_id
  147. inspection_reference.ProjectName = info.ProjectName
  148. inspection_reference.Project = info.ProjectName
  149. inspection_reference.ProjectId = project_id
  150. inspection_reference.ItemName = info.ItemName
  151. inspection_reference.ItemNameAddition = strconv.FormatInt(info.ItemId, 10)
  152. inspection_reference.ItemId = item_id
  153. inspection_reference.RangeType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  154. inspection_reference.RangeMin = RangeMin
  155. inspection_reference.RangeMax = RangeMax
  156. // inspection_reference.RangeValue = RangeValue
  157. inspection_reference.RangeOptions = RangeOptions
  158. inspection_reference.Unit = info.Unit
  159. inspection_reference.Status = 1
  160. inspection_reference.CreatedTime = time.Now().Unix()
  161. inspection_reference.UpdatedTime = time.Now().Unix()
  162. inspection_reference.InspectDate = inspect_date
  163. inspection_reference.UTime = inspect_date
  164. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  165. if err != nil {
  166. tx.Rollback()
  167. }
  168. }
  169. var itotal int
  170. 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
  171. if itotal <= 0 {
  172. info.ItemValue = strings.Replace(info.ItemValue, "&gt;", ">", -1)
  173. info.ItemValue = strings.Replace(info.ItemValue, "&lt;", "<", -1)
  174. inspection.PatientId = patient_id
  175. inspection.OrgId = org_id
  176. inspection.ProjectId = project_id
  177. inspection.ItemName = inspection_reference.ItemName
  178. inspection.ProjectName = inspection_reference.ProjectName
  179. inspection.InspectType, _ = strconv.Atoi(strconv.FormatInt(ItemType, 10))
  180. inspection.ItemId = item_id
  181. inspection.InspectValue = info.ItemValue
  182. inspection.InspectDate = inspect_date
  183. inspection.RecordDate = record_date
  184. // inspection.InspectTips = report.Resultstate
  185. inspection.Status = 1
  186. inspection.CreatedTime = time.Now().Unix()
  187. inspection.UpdatedTime = time.Now().Unix()
  188. inspection.UTime = inspect_date
  189. inspection.HisUserId = strconv.FormatInt(patient_id, 10)
  190. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  191. if err != nil {
  192. tx.Rollback()
  193. }
  194. }
  195. tx.Commit()
  196. }
  197. }
  198. } else {
  199. continue
  200. }
  201. }
  202. }
  203. var syncInfo models.MiddleSyncInfo
  204. syncInfo.OrgId = org_id
  205. syncInfo.SyncTime = time.Now().Unix()
  206. syncInfo.SyncResultType = 1
  207. syncInfo.SyncRsultRemark = "同步成功"
  208. syncInfo.SyncTotalNum = 0
  209. syncInfo.SyncSuccessNum = 0
  210. syncInfo.SyncInfo = ""
  211. syncInfo.CreateTime = time.Now().Unix()
  212. syncInfo.UpdateTime = time.Now().Unix()
  213. cwderr := CreateSyncInfo(&syncInfo)
  214. if cwderr != nil {
  215. utils.ErrorLog("创建同步信息失败:%v", cwderr)
  216. return
  217. }
  218. SyncToScpatx()
  219. return
  220. }
  221. func SyncToScpatx() {
  222. utils.TraceLog("检验检查同步任务开始执行")
  223. org_id := int64(9829)
  224. // 第一步:跟进org_id 去中间库查出需要同步的数据
  225. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  226. inspections, _ := GetSyncInspectionByOrgId(org_id)
  227. // 第二步:将数据同步到业务库
  228. if len(inspection_references) > 0 {
  229. for _, inspection_reference := range inspection_references {
  230. SyncInspectionReference(&inspection_reference)
  231. }
  232. }
  233. if len(inspections) > 0 {
  234. for _, inspection := range inspections {
  235. SyncInspection(&inspection)
  236. }
  237. }
  238. utils.SuccessLog("检验检查同步任务完成")
  239. }