lis_service.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. _"strconv"
  6. "strings"
  7. "time"
  8. _"IC/utils"
  9. _"fmt"
  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. // 从机构将数据同步到中间库
  90. func SyncInspection(data *models.MiddleInspection) error {
  91. tx := writeDb.Begin()
  92. var inspection models.Inspection
  93. var total int
  94. 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
  95. if total <= 0 {
  96. inspection.OrgId = data.OrgId
  97. inspection.PatientId = data.PatientId
  98. inspection.ProjectName = data.ProjectName
  99. // inspection.Project = data.ProjectName
  100. inspection.ProjectId = data.ProjectId
  101. inspection.ItemName = data.ItemName
  102. inspection.ItemId = data.ItemId
  103. inspection.InspectType = int64(data.InspectType)
  104. inspection.InspectValue = data.InspectValue
  105. inspection.InspectTips = data.InspectTips
  106. inspection.InspectDate = data.RecordDate
  107. inspection.Status = 1
  108. inspection.CreatedTime = time.Now().Unix()
  109. inspection.UpdatedTime = time.Now().Unix()
  110. err := tx.Model(&models.Inspection{}).Create(&inspection).Error
  111. if err != nil {
  112. tx.Rollback()
  113. }
  114. data.IsSync = 1
  115. data.SyncId = inspection.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. // 跟进org_id获取未同步的检验检查选项信息
  126. func GetSyncInspectionReferenceByOrgId(org_id int64) (InspectionReferences []models.MiddleInspectionReference,err error) {
  127. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and is_sync = 0", org_id).Find(&InspectionReferences).Error
  128. return
  129. }
  130. // 跟进org_id获取未同步的检验检查信息
  131. func GetSyncInspectionByOrgId(org_id int64) (Inspection []models.MiddleInspection,err error) {
  132. err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and status = 1 and is_sync = 0", org_id).Find(&Inspection).Error
  133. return
  134. }
  135. // 修改一条同步数据详情
  136. func UpdateSyncInfo(syncInfo *models.MiddleSyncInfo) error {
  137. err := writeMiddleDb.Save(&syncInfo).Error
  138. return err
  139. }
  140. // 创建一条同步数据详情
  141. func CreateSyncInfo(syncInfo *models.MiddleSyncInfo) error {
  142. err := writeMiddleDb.Create(&syncInfo).Error
  143. return err
  144. }
  145. // 创建一条检验检查操作表数据
  146. func CreateReportHandle(report *models.MiddleReportHandle) error {
  147. err := writeMiddleDb.Create(&report).Error
  148. return err
  149. }
  150. // 根据机构ID获取该机构检验检查操作表里状态为0的数据
  151. func GetReportHandleListyOrgID(org_id int64) (report_handle_list []models.MiddleReportHandle ,err error){
  152. err = readMiddleDb.Model(&models.MiddleReportHandle{}).Where("org_id = ? and handle_type = 0 ", org_id).Find(&report_handle_list).Error
  153. return
  154. }
  155. // 根据机构ID修改检验检查操作表里的数据
  156. func UpdateReportHandleListyOrgID(report_handle *models.MiddleReportHandle) error{
  157. err := writeMiddleDb.Model(&models.MiddleReportHandle{}).Updates(&report_handle).Error
  158. return err
  159. }
  160. // 跟进机构ID获取该机构所有病人的his_user_id
  161. func GetHisUserIdByOrgID(org_id int64) (patients []models.MiddlePatients ,err error) {
  162. err = readMiddleDb.Model(&models.MiddlePatients{}).Where("user_org_id = ? and status = 1", org_id).Find(&patients).Error
  163. return
  164. }
  165. // 根据机构ID获取该机构上次同步信息
  166. func GetSyncTimeByOrgID(org_id int64,his_user_id string) (sync_info models.MiddleSyncInfo ,err error){
  167. err = readMiddleDb.Model(&models.MiddleSyncInfo{}).Where("org_id = ? and his_user_id =? ", org_id,his_user_id).Last(&sync_info).Error
  168. return
  169. }
  170. // 根据机构ID获取该机构上次同步信息
  171. func GetSyncTimeByOrgIDForYs(org_id int64) (sync_info models.MiddleSyncInfo ,err error){
  172. err = readMiddleDb.Model(&models.MiddleSyncInfo{}).Where("org_id = ? ", org_id).Last(&sync_info).Error
  173. return
  174. }
  175. // 根据机构ID和His_useer_id获取该患者最后一条同步检查数据
  176. func GetInspectionLastInfoByOrgID(org_id int64,his_user_id string) (inspectionLastInfo models.MiddleInspection ,err error){
  177. err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and his_user_id =? ", org_id,his_user_id).Order("created_time desc").First(&inspectionLastInfo).Error
  178. return
  179. }
  180. // 从机构将数据同步到中间库
  181. func Sync(data *models.TBLISItem,org_id int64,patient_id int64)(*models.MiddleInspectionReference,*models.MiddleInspection,error) {
  182. tx := writeMiddleDb.Begin()
  183. var inspection models.MiddleInspection
  184. var inspection_reference models.MiddleInspectionReference
  185. recordDateStr := data.UTime
  186. if len(data.UTime) == 0 {
  187. recordDateStr = time.Now().Format("2006-01-02 15:04")
  188. }
  189. record_date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  190. // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
  191. // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  192. var total int
  193. var itotal int
  194. if len(data.RangeOptions) > 0 {
  195. data.ItemType = 2
  196. } else {
  197. data.ItemType = 1
  198. if len(data.RangeMax) > 0 {
  199. Range := strings.Split(data.RangeMax,"-")
  200. if len(Range) > 1 {
  201. data.RangeMin = Range[0]
  202. data.RangeMax = Range[1]
  203. } else {
  204. data.RangeMin = Range[0]
  205. }
  206. }
  207. }
  208. 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
  209. if total <= 0 && len(data.ProjectName) > 0 {
  210. inspection_reference.OrgId = org_id
  211. inspection_reference.ProjectName = data.ProjectName
  212. inspection_reference.Project = data.ProjectName
  213. inspection_reference.ProjectId = data.ProjectId
  214. inspection_reference.ItemName = data.ItemName
  215. inspection_reference.ItemId = data.ItemId
  216. inspection_reference.RangeType = data.ItemType
  217. inspection_reference.RangeMin = data.RangeMin
  218. inspection_reference.RangeMax = data.RangeMax
  219. inspection_reference.RangeValue = data.RangeValue
  220. inspection_reference.RangeOptions = data.RangeMax
  221. inspection_reference.Unit = data.Unit
  222. inspection_reference.Status = 1
  223. inspection_reference.CreatedTime = time.Now().Unix()
  224. inspection_reference.UpdatedTime = time.Now().Unix()
  225. inspection_reference.InspectDate = data.InspectDate
  226. inspection_reference.UTime = data.UTime
  227. err := tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  228. if err != nil {
  229. tx.Rollback()
  230. }
  231. }
  232. 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
  233. if itotal <= 0 {
  234. inspection.PatientId = patient_id
  235. inspection.OrgId = org_id
  236. inspection.ProjectId = data.ProjectId
  237. inspection.ItemName = data.ItemName
  238. inspection.ProjectName = data.ProjectName
  239. inspection.InspectType = data.ItemType
  240. inspection.ItemId = data.ItemId
  241. if data.ItemType == 1 {
  242. inspection.InspectValue = data.ItemValue
  243. } else {
  244. inspection.InspectValue = data.RangeValue
  245. }
  246. inspection.InspectDate = data.InspectDate
  247. inspection.RecordDate = record_date.Unix()
  248. inspection.Status = 1
  249. inspection.CreatedTime = time.Now().Unix()
  250. inspection.UpdatedTime = time.Now().Unix()
  251. inspection.UTime = data.UTime
  252. inspection.HisUserId = data.HisUserId
  253. err := tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  254. if err != nil {
  255. tx.Rollback()
  256. }
  257. }
  258. tx.Commit()
  259. return &inspection_reference,&inspection,err
  260. }
  261. //GetPatientList 返回患者的列表
  262. func GetList(his_user_id []interface{},syncTimes string) (lis []*models.TBLISItem, err error) {
  263. err = readRemoteDb.Model(&models.TBLISItem{}).Where("u_time > ? AND his_user_id in (?) ",syncTimes,his_user_id).Find(&lis).Error
  264. return
  265. }
  266. //GetPatientList 返回患者的列表
  267. func GetListByHisUserID(his_user_id string,syncTimes string,selectType int64) (lis []*models.TBLISItem,err error) {
  268. if selectType == 1 {
  269. err = readRemoteDb.Model(&models.TBLISItem{}).Where("u_time > ? AND patient_name = ? ",syncTimes,his_user_id).Find(&lis).Error
  270. } else {
  271. err = readRemoteDb.Model(&models.TBLISItem{}).Where("u_time > ? AND his_user_id = ? ",syncTimes,his_user_id).Find(&lis).Error
  272. }
  273. return
  274. }