pajm_service.go 9.9KB

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