lis_service.go 11KB

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