lis_service.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. _ "IC/utils"
  6. _ "fmt"
  7. _ "github.com/jinzhu/gorm"
  8. _ "strconv"
  9. "strings"
  10. "time"
  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.InspectDate).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.ReferenceProjectId = data.
  146. //inspection.UpdatedTime = time.Now().Unix()
  147. //inspection.ReferenceItemId = data.
  148. err := tx.Model(&models.Inspection{}).Create(&inspection).Error
  149. if err != nil {
  150. tx.Rollback()
  151. }
  152. data.IsSync = 1
  153. data.SyncId = inspection.ID
  154. data.UpdatedTime = time.Now().Unix()
  155. ierr := writeMiddleDb.Save(&data).Error
  156. if ierr != nil {
  157. tx.Rollback()
  158. }
  159. }
  160. tx.Commit()
  161. return err
  162. }
  163. // 跟进org_id获取未同步的检验检查选项信息
  164. func GetSyncInspectionReferenceByOrgId(org_id int64) (InspectionReferences []models.MiddleInspectionReference, err error) {
  165. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and is_sync = 0", org_id).Find(&InspectionReferences).Error
  166. return
  167. }
  168. // 跟进org_id获取未同步的检验检查信息
  169. func GetSyncInspectionByOrgId(org_id int64) (Inspection []models.MiddleInspection, err error) {
  170. err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and status = 1 and is_sync = 0", org_id).Find(&Inspection).Error
  171. return
  172. }
  173. // 修改一条同步数据详情
  174. func UpdateSyncInfo(syncInfo *models.MiddleSyncInfo) error {
  175. err := writeMiddleDb.Save(&syncInfo).Error
  176. return err
  177. }
  178. // 创建一条同步数据详情
  179. func CreateSyncInfo(syncInfo *models.MiddleSyncInfo) error {
  180. err := writeMiddleDb.Create(&syncInfo).Error
  181. return err
  182. }
  183. // 创建一条检验检查操作表数据
  184. func CreateReportHandle(report *models.MiddleReportHandle) error {
  185. err := writeMiddleDb.Create(&report).Error
  186. return err
  187. }
  188. // 根据机构ID获取该机构检验检查操作表里状态为0的数据
  189. func GetReportHandleListyOrgID(org_id int64) (report_handle_list []models.MiddleReportHandle, err error) {
  190. err = readMiddleDb.Model(&models.MiddleReportHandle{}).Where("org_id = ? and handle_type = 0 ", org_id).Find(&report_handle_list).Error
  191. return
  192. }
  193. // 根据机构ID修改检验检查操作表里的数据
  194. func UpdateReportHandleListyOrgID(report_handle *models.MiddleReportHandle) error {
  195. err := writeMiddleDb.Model(&models.MiddleReportHandle{}).Updates(&report_handle).Error
  196. return err
  197. }
  198. // 跟进机构ID获取该机构所有病人的his_user_id
  199. func GetHisUserIdByOrgID(org_id int64) (patients []models.MiddlePatients, err error) {
  200. err = readMiddleDb.Model(&models.MiddlePatients{}).Where("user_org_id = ? and status = 1", org_id).Find(&patients).Error
  201. return
  202. }
  203. // 根据机构ID获取该机构上次同步信息
  204. func GetSyncTimeByOrgID(org_id int64, his_user_id string) (sync_info models.MiddleSyncInfo, err error) {
  205. err = readMiddleDb.Model(&models.MiddleSyncInfo{}).Where("org_id = ? and his_user_id =? ", org_id, his_user_id).Last(&sync_info).Error
  206. return
  207. }
  208. // 根据机构ID获取该机构上次同步信息
  209. func GetSyncTimeByOrgIDForYs(org_id int64) (sync_info models.MiddleSyncInfo, err error) {
  210. err = readMiddleDb.Model(&models.MiddleSyncInfo{}).Where("org_id = ? ", org_id).Last(&sync_info).Error
  211. return
  212. }
  213. // 根据机构ID和His_useer_id获取该患者最后一条同步检查数据
  214. func GetInspectionLastInfoByOrgID(org_id int64, his_user_id string) (inspectionLastInfo models.MiddleInspection, err error) {
  215. err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and his_user_id =? ", org_id, his_user_id).Order("created_time desc").First(&inspectionLastInfo).Error
  216. return
  217. }
  218. // 从机构将数据同步到中间库
  219. func Sync(data *models.TBLISItem, org_id int64, patient_id int64) (*models.MiddleInspectionReference, *models.MiddleInspection, error) {
  220. tx := writeMiddleDb.Begin()
  221. var inspection models.MiddleInspection
  222. var inspection_reference models.MiddleInspectionReference
  223. recordDateStr := data.UTime
  224. if len(data.UTime) == 0 {
  225. recordDateStr = time.Now().Format("2006-01-02 15:04")
  226. }
  227. record_date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  228. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  229. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  230. var total int
  231. var itotal int
  232. if len(data.RangeOptions) > 0 {
  233. data.ItemType = 2
  234. } else {
  235. data.ItemType = 1
  236. if len(data.RangeMax) > 0 {
  237. Range := strings.Split(data.RangeMax, "-")
  238. if len(Range) > 1 {
  239. data.RangeMin = Range[0]
  240. data.RangeMax = Range[1]
  241. } else {
  242. data.RangeMin = Range[0]
  243. }
  244. }
  245. }
  246. 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
  247. if total <= 0 && len(data.ProjectName) > 0 {
  248. inspection_reference.OrgId = org_id
  249. inspection_reference.ProjectName = data.ProjectName
  250. inspection_reference.Project = data.ProjectName
  251. inspection_reference.ProjectId = data.ProjectId
  252. inspection_reference.ItemName = data.ItemName
  253. inspection_reference.ItemId = data.ItemId
  254. inspection_reference.RangeType = data.ItemType
  255. inspection_reference.RangeMin = data.RangeMin
  256. inspection_reference.RangeMax = data.RangeMax
  257. inspection_reference.RangeValue = data.RangeValue
  258. inspection_reference.RangeOptions = data.RangeMax
  259. inspection_reference.Unit = data.Unit
  260. inspection_reference.Status = 1
  261. inspection_reference.CreatedTime = time.Now().Unix()
  262. inspection_reference.UpdatedTime = time.Now().Unix()
  263. inspection_reference.InspectDate = data.InspectDate
  264. inspection_reference.UTime = data.UTime
  265. err := tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  266. if err != nil {
  267. tx.Rollback()
  268. }
  269. }
  270. 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
  271. if itotal <= 0 {
  272. inspection.PatientId = patient_id
  273. inspection.OrgId = org_id
  274. inspection.ProjectId = data.ProjectId
  275. inspection.ItemName = data.ItemName
  276. inspection.ProjectName = data.ProjectName
  277. inspection.InspectType = data.ItemType
  278. inspection.ItemId = data.ItemId
  279. if data.ItemType == 1 {
  280. inspection.InspectValue = data.ItemValue
  281. } else {
  282. inspection.InspectValue = data.RangeValue
  283. }
  284. inspection.InspectDate = data.InspectDate
  285. inspection.RecordDate = record_date.Unix()
  286. inspection.Status = 1
  287. inspection.CreatedTime = time.Now().Unix()
  288. inspection.UpdatedTime = time.Now().Unix()
  289. inspection.UTime = data.UTime
  290. inspection.HisUserId = data.HisUserId
  291. err := tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  292. if err != nil {
  293. tx.Rollback()
  294. }
  295. }
  296. tx.Commit()
  297. return &inspection_reference, &inspection, err
  298. }
  299. // GetPatientList 返回患者的列表
  300. func GetList(his_user_id []interface{}, syncTimes string) (lis []*models.TBLISItem, err error) {
  301. err = readRemoteDb.Model(&models.TBLISItem{}).Where("u_time > ? AND his_user_id in (?) ", syncTimes, his_user_id).Find(&lis).Error
  302. return
  303. }
  304. // GetPatientList 返回患者的列表
  305. func GetListByHisUserID(his_user_id string, syncTimes string, selectType int64) (lis []*models.TBLISItem, err error) {
  306. if selectType == 1 {
  307. err = readRemoteDb.Model(&models.TBLISItem{}).Where("u_time > ? AND patient_name = ? ", syncTimes, his_user_id).Find(&lis).Error
  308. } else {
  309. err = readRemoteDb.Model(&models.TBLISItem{}).Where("u_time > ? AND his_user_id = ? ", syncTimes, his_user_id).Find(&lis).Error
  310. }
  311. return
  312. }