lis_service.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package service
  2. import (
  3. _ "fmt"
  4. _ "strconv"
  5. "strings"
  6. "time"
  7. "IC/models"
  8. "IC/utils"
  9. _ "IC/utils"
  10. _ "github.com/jinzhu/gorm"
  11. )
  12. // 获取备份库所有的医嘱信息
  13. func GetAllAdvice() (Advice []models.DoctorAdvice, err error) {
  14. err = backupDb.Model(&models.DoctorAdvice{}).Offset(100000).Limit(10000).Find(&Advice).Error
  15. return
  16. }
  17. // 获取备份库所有的医嘱信息
  18. func GetScheduless(id int64) (schedule models.Schedules, err error) {
  19. err = tempwriteDb.Model(&models.Schedules{}).Where("id = ?", id).First(&schedule).Error
  20. return
  21. }
  22. func UpdateSchedule(id int64, status int64) error {
  23. err := writeDb.Model(&models.Schedules{}).Where("id = ? ", id).Updates(map[string]interface{}{"status": status}).Error
  24. return err
  25. }
  26. // 修改医嘱信息
  27. func SyncAdvice(Advice *models.DoctorAdvice) error {
  28. err := writeDb.Model(&models.DoctorAdvice{}).Updates(&Advice).Error
  29. return err
  30. }
  31. // 查找有字段的机构
  32. func GetfiledConfigOrg() (FiledConfig []models.FiledConfig, err error) {
  33. db := readDb.Begin()
  34. err = db.Table("xt_filed_config").Group("org_id").Select("org_id,module,filed_name").Where("org_id > 0").Scan(&FiledConfig).Error
  35. return
  36. }
  37. // 查询系统字段
  38. func GetfiledConfigSys() (FiledConfig []models.FiledConfig, err error) {
  39. err = readDb.Model(&models.FiledConfig{}).Where("org_id = 0").Find(&FiledConfig).Error
  40. return
  41. }
  42. // 跟进查询条件查询结果
  43. func GetfiledConfigByOrgId(org_id int64, module int64, filed_name string) (FiledConfig models.FiledConfig, err error) {
  44. err = readDb.Model(&models.FiledConfig{}).Where("org_id = ? and module = ? and filed_name = ?", org_id, module, filed_name).First(&FiledConfig).Error
  45. return
  46. }
  47. // 创建一条字段数据详情
  48. func CreatefiledConfig(FiledConfig *models.FiledConfig) error {
  49. err := writeDb.Create(&FiledConfig).Error
  50. return err
  51. }
  52. // 从机构将数据同步到中间库
  53. func SyncInspectionReference(data *models.MiddleInspectionReference) error {
  54. tx := writeDb.Begin()
  55. var inspection_reference models.InspectionReference
  56. var total int
  57. err = readDb.Model(&models.InspectionReference{}).Where("org_id = ? and project_id = ? and item_id = ? and status = 1", data.OrgId, data.ProjectId, data.ItemId).Find(&inspection_reference).Count(&total).Error
  58. if total <= 0 {
  59. inspection_reference.OrgId = data.OrgId
  60. inspection_reference.ProjectName = data.ProjectName
  61. inspection_reference.Project = data.ProjectName
  62. inspection_reference.ProjectId = data.ProjectId
  63. inspection_reference.ItemName = data.ItemName
  64. inspection_reference.ItemId = data.ItemId
  65. inspection_reference.RangeType = data.RangeType
  66. inspection_reference.RangeMin = data.RangeMin
  67. inspection_reference.RangeMax = data.RangeMax
  68. inspection_reference.RangeValue = data.RangeValue
  69. inspection_reference.RangeOptions = data.RangeOptions
  70. inspection_reference.Unit = data.Unit
  71. inspection_reference.Status = 1
  72. inspection_reference.CreatedTime = time.Now().Unix()
  73. inspection_reference.UpdatedTime = time.Now().Unix()
  74. err := tx.Model(&models.InspectionReference{}).Create(&inspection_reference).Error
  75. if err != nil {
  76. tx.Rollback()
  77. }
  78. data.IsSync = 1
  79. data.SyncId = inspection_reference.ID
  80. data.UpdatedTime = time.Now().Unix()
  81. ierr := writeMiddleDb.Save(&data).Error
  82. if ierr != nil {
  83. tx.Rollback()
  84. }
  85. }
  86. tx.Commit()
  87. return err
  88. }
  89. func SyncInspectionReferenceTwo(data *models.MiddleInspectionReference) error {
  90. tx := writeDb.Begin()
  91. var inspection_reference models.InspectionReference
  92. var total int
  93. err = readDb.Model(&models.InspectionReference{}).Where("org_id = ? and project_id = ? and item_id = ? and status = 1", data.OrgId, data.XtProjectId, data.XtItemId).Find(&inspection_reference).Count(&total).Error
  94. if total <= 0 {
  95. inspection_reference.OrgId = data.OrgId
  96. inspection_reference.ProjectName = data.ProjectName
  97. inspection_reference.Project = data.ProjectName
  98. inspection_reference.ProjectId = data.ProjectId
  99. inspection_reference.ItemName = data.ItemName
  100. inspection_reference.ItemId = data.ItemId
  101. inspection_reference.RangeType = data.RangeType
  102. inspection_reference.RangeMin = data.RangeMin
  103. inspection_reference.RangeMax = data.RangeMax
  104. inspection_reference.RangeValue = data.RangeValue
  105. inspection_reference.RangeOptions = data.RangeOptions
  106. inspection_reference.Unit = data.Unit
  107. inspection_reference.Status = 1
  108. inspection_reference.CreatedTime = time.Now().Unix()
  109. inspection_reference.UpdatedTime = time.Now().Unix()
  110. err := tx.Model(&models.InspectionReference{}).Create(&inspection_reference).Error
  111. if err != nil {
  112. tx.Rollback()
  113. }
  114. data.IsSync = 1
  115. data.SyncId = inspection_reference.ID
  116. data.UpdatedTime = time.Now().Unix()
  117. ierr := writeMiddleDb.Save(&data).Error
  118. if ierr != nil {
  119. tx.Rollback()
  120. }
  121. }
  122. tx.Commit()
  123. return err
  124. }
  125. // 从机构将数据同步到中间库
  126. func SyncInspection(data *models.MiddleInspection) error {
  127. tx := writeDb.Begin()
  128. var inspection models.Inspection
  129. var total int
  130. err = readDb.Model(&models.Inspection{}).Where("org_id = ? and project_id = ? and item_id = ? and patient_id =? and inspect_date=? and status = 1", data.OrgId, data.ProjectId, data.ItemId, data.PatientId, data.RecordDate).Find(&inspection).Count(&total).Error
  131. if total <= 0 {
  132. inspection.OrgId = data.OrgId
  133. inspection.PatientId = data.PatientId
  134. inspection.ProjectName = data.ProjectName
  135. // inspection.Project = data.ProjectName
  136. inspection.ProjectId = data.ProjectId
  137. inspection.ItemName = data.ItemName
  138. inspection.ItemId = data.ItemId
  139. inspection.InspectType = int64(data.InspectType)
  140. inspection.InspectValue = data.InspectValue
  141. inspection.InspectTips = data.InspectTips
  142. inspection.InspectDate = data.RecordDate
  143. inspection.Status = 1
  144. inspection.CreatedTime = time.Now().Unix()
  145. inspection.UpdatedTime = time.Now().Unix()
  146. err := tx.Model(&models.Inspection{}).Create(&inspection).Error
  147. if err != nil {
  148. tx.Rollback()
  149. }
  150. data.IsSync = 1
  151. data.SyncId = inspection.ID
  152. data.UpdatedTime = time.Now().Unix()
  153. ierr := writeMiddleDb.Save(&data).Error
  154. if ierr != nil {
  155. tx.Rollback()
  156. }
  157. }
  158. tx.Commit()
  159. return err
  160. }
  161. // 跟进org_id获取未同步的检验检查选项信息
  162. func GetSyncInspectionReferenceByOrgId(org_id int64) (InspectionReferences []models.MiddleInspectionReference, err error) {
  163. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and is_sync = 0", org_id).Find(&InspectionReferences).Error
  164. return
  165. }
  166. // 跟进org_id获取未同步的检验检查信息
  167. func GetSyncInspectionByOrgId(org_id int64) (Inspection []models.MiddleInspection, err error) {
  168. err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and status = 1 and is_sync = 0", org_id).Find(&Inspection).Error
  169. return
  170. }
  171. // 修改一条同步数据详情
  172. func UpdateSyncInfo(syncInfo *models.MiddleSyncInfo) error {
  173. err := writeMiddleDb.Save(&syncInfo).Error
  174. return err
  175. }
  176. // 创建一条同步数据详情
  177. func CreateSyncInfo(syncInfo *models.MiddleSyncInfo) error {
  178. err := writeMiddleDb.Create(&syncInfo).Error
  179. return err
  180. }
  181. // 创建一条检验检查操作表数据
  182. func CreateReportHandle(report *models.MiddleReportHandle) error {
  183. err := writeMiddleDb.Create(&report).Error
  184. return err
  185. }
  186. // 根据机构ID获取该机构检验检查操作表里状态为0的数据
  187. func GetReportHandleListyOrgID(org_id int64) (report_handle_list []models.MiddleReportHandle, err error) {
  188. err = readMiddleDb.Model(&models.MiddleReportHandle{}).Where("org_id = ? and handle_type = 0 ", org_id).Find(&report_handle_list).Error
  189. return
  190. }
  191. // 根据机构ID修改检验检查操作表里的数据
  192. func UpdateReportHandleListyOrgID(report_handle *models.MiddleReportHandle) error {
  193. err := writeMiddleDb.Model(&models.MiddleReportHandle{}).Updates(&report_handle).Error
  194. return err
  195. }
  196. // 跟进机构ID获取该机构所有病人的his_user_id
  197. func GetHisUserIdByOrgID(org_id int64) (patients []models.MiddlePatients, err error) {
  198. err = readMiddleDb.Model(&models.MiddlePatients{}).Where("user_org_id = ? and status = 1", org_id).Find(&patients).Error
  199. return
  200. }
  201. // 根据机构ID获取该机构上次同步信息
  202. func GetSyncTimeByOrgID(org_id int64, his_user_id string) (sync_info models.MiddleSyncInfo, err error) {
  203. err = readMiddleDb.Model(&models.MiddleSyncInfo{}).Where("org_id = ? and his_user_id =? ", org_id, his_user_id).Last(&sync_info).Error
  204. return
  205. }
  206. // 根据机构ID获取该机构上次同步信息
  207. func GetSyncTimeByOrgIDForYs(org_id int64) (sync_info models.MiddleSyncInfo, err error) {
  208. err = readMiddleDb.Model(&models.MiddleSyncInfo{}).Where("org_id = ? ", org_id).Last(&sync_info).Error
  209. return
  210. }
  211. // 根据机构ID和His_useer_id获取该患者最后一条同步检查数据
  212. func GetInspectionLastInfoByOrgID(org_id int64, his_user_id string) (inspectionLastInfo models.MiddleInspection, err error) {
  213. err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and his_user_id =? ", org_id, his_user_id).Order("created_time desc").First(&inspectionLastInfo).Error
  214. return
  215. }
  216. // 从机构将数据同步到中间库
  217. func Sync(data *models.TBLISItem, org_id int64, patient_id int64) (*models.MiddleInspectionReference, *models.MiddleInspection, error) {
  218. tx := writeMiddleDb.Begin()
  219. var inspection models.MiddleInspection
  220. var inspection_reference models.MiddleInspectionReference
  221. recordDateStr := data.UTime
  222. if len(data.UTime) == 0 {
  223. recordDateStr = time.Now().Format("2006-01-02 15:04")
  224. }
  225. record_date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  226. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  227. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  228. var total int
  229. var itotal int
  230. if len(data.RangeOptions) > 0 {
  231. data.ItemType = 2
  232. } else {
  233. data.ItemType = 1
  234. if len(data.RangeMax) > 0 {
  235. Range := strings.Split(data.RangeMax, "-")
  236. if len(Range) > 1 {
  237. data.RangeMin = Range[0]
  238. data.RangeMax = Range[1]
  239. } else {
  240. data.RangeMin = Range[0]
  241. }
  242. }
  243. }
  244. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project_id = ? and item_id = ? and status = 1", org_id, data.ProjectId, data.ItemId).Find(&inspection_reference).Count(&total).Error
  245. if total <= 0 && len(data.ProjectName) > 0 {
  246. inspection_reference.OrgId = org_id
  247. inspection_reference.ProjectName = data.ProjectName
  248. inspection_reference.Project = data.ProjectName
  249. inspection_reference.ProjectId = data.ProjectId
  250. inspection_reference.ItemName = data.ItemName
  251. inspection_reference.ItemId = data.ItemId
  252. inspection_reference.RangeType = data.ItemType
  253. inspection_reference.RangeMin = data.RangeMin
  254. inspection_reference.RangeMax = data.RangeMax
  255. inspection_reference.RangeValue = data.RangeValue
  256. inspection_reference.RangeOptions = data.RangeMax
  257. inspection_reference.Unit = data.Unit
  258. inspection_reference.Status = 1
  259. inspection_reference.CreatedTime = time.Now().Unix()
  260. inspection_reference.UpdatedTime = time.Now().Unix()
  261. inspection_reference.InspectDate = data.InspectDate
  262. inspection_reference.UTime = data.UTime
  263. err := tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  264. if err != nil {
  265. tx.Rollback()
  266. }
  267. }
  268. 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, data.ProjectId, data.ItemId, record_date.Unix(), patient_id).Find(&inspection).Count(&itotal).Error
  269. if itotal <= 0 {
  270. inspection.PatientId = patient_id
  271. inspection.OrgId = org_id
  272. inspection.ProjectId = data.ProjectId
  273. inspection.ItemName = data.ItemName
  274. inspection.ProjectName = data.ProjectName
  275. inspection.InspectType = data.ItemType
  276. inspection.ItemId = data.ItemId
  277. if data.ItemType == 1 {
  278. inspection.InspectValue = data.ItemValue
  279. } else {
  280. inspection.InspectValue = data.RangeValue
  281. }
  282. inspection.InspectDate = data.InspectDate
  283. inspection.RecordDate = record_date.Unix()
  284. inspection.Status = 1
  285. inspection.CreatedTime = time.Now().Unix()
  286. inspection.UpdatedTime = time.Now().Unix()
  287. inspection.UTime = data.UTime
  288. inspection.HisUserId = data.HisUserId
  289. err := tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  290. if err != nil {
  291. tx.Rollback()
  292. }
  293. }
  294. tx.Commit()
  295. return &inspection_reference, &inspection, err
  296. }
  297. // GetPatientList 返回患者的列表
  298. func GetList(his_user_id []interface{}, syncTimes string) (lis []*models.TBLISItem, err error) {
  299. err = readRemoteDb.Model(&models.TBLISItem{}).Where("u_time > ? AND his_user_id in (?) ", syncTimes, his_user_id).Find(&lis).Error
  300. return
  301. }
  302. // GetPatientList 返回患者的列表
  303. func GetListByHisUserID(his_user_id string, syncTimes string, selectType int64) (lis []*models.TBLISItem, err error) {
  304. if selectType == 1 {
  305. err = readRemoteDb.Model(&models.TBLISItem{}).Where("u_time > ? AND patient_name = ? ", syncTimes, his_user_id).Find(&lis).Error
  306. } else {
  307. err = readRemoteDb.Model(&models.TBLISItem{}).Where("u_time > ? AND his_user_id = ? ", syncTimes, his_user_id).Find(&lis).Error
  308. }
  309. return
  310. }