123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- package service
-
- import (
- "IC/models"
- "IC/utils"
- _ "IC/utils"
- _ "fmt"
- _ "github.com/jinzhu/gorm"
- _ "strconv"
- "strings"
- "time"
- )
-
- // 获取备份库所有的医嘱信息
- func GetAllAdvice() (Advice []models.DoctorAdvice, err error) {
- err = backupDb.Model(&models.DoctorAdvice{}).Offset(100000).Limit(10000).Find(&Advice).Error
- return
- }
-
- // 获取备份库所有的医嘱信息
- func GetScheduless(id int64) (schedule models.Schedules, err error) {
- err = tempwriteDb.Model(&models.Schedules{}).Where("id = ?", id).First(&schedule).Error
- return
- }
-
- func UpdateSchedule(id int64, status int64) error {
- err := writeDb.Model(&models.Schedules{}).Where("id = ? ", id).Updates(map[string]interface{}{"status": status}).Error
- return err
- }
-
- // 修改医嘱信息
- func SyncAdvice(Advice *models.DoctorAdvice) error {
- err := writeDb.Model(&models.DoctorAdvice{}).Updates(&Advice).Error
- return err
- }
-
- // 查找有字段的机构
- func GetfiledConfigOrg() (FiledConfig []models.FiledConfig, err error) {
- db := readDb.Begin()
- err = db.Table("xt_filed_config").Group("org_id").Select("org_id,module,filed_name").Where("org_id > 0").Scan(&FiledConfig).Error
- return
- }
-
- // 查询系统字段
- func GetfiledConfigSys() (FiledConfig []models.FiledConfig, err error) {
- err = readDb.Model(&models.FiledConfig{}).Where("org_id = 0").Find(&FiledConfig).Error
- return
- }
-
- // 跟进查询条件查询结果
- func GetfiledConfigByOrgId(org_id int64, module int64, filed_name string) (FiledConfig models.FiledConfig, err error) {
- err = readDb.Model(&models.FiledConfig{}).Where("org_id = ? and module = ? and filed_name = ?", org_id, module, filed_name).First(&FiledConfig).Error
- return
- }
-
- // 创建一条字段数据详情
- func CreatefiledConfig(FiledConfig *models.FiledConfig) error {
- err := writeDb.Create(&FiledConfig).Error
- return err
- }
-
- // 从机构将数据同步到中间库
- func SyncInspectionReference(data *models.MiddleInspectionReference) error {
- tx := writeDb.Begin()
- var inspection_reference models.InspectionReference
- var total int
- 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
- if total <= 0 {
- inspection_reference.OrgId = data.OrgId
- inspection_reference.ProjectName = data.ProjectName
- inspection_reference.Project = data.ProjectName
- inspection_reference.ProjectId = data.ProjectId
- inspection_reference.ItemName = data.ItemName
- inspection_reference.ItemId = data.ItemId
- inspection_reference.RangeType = data.RangeType
- inspection_reference.RangeMin = data.RangeMin
- inspection_reference.RangeMax = data.RangeMax
- inspection_reference.RangeValue = data.RangeValue
- inspection_reference.RangeOptions = data.RangeOptions
- inspection_reference.Unit = data.Unit
- inspection_reference.Status = 1
- inspection_reference.CreatedTime = time.Now().Unix()
- inspection_reference.UpdatedTime = time.Now().Unix()
-
- err := tx.Model(&models.InspectionReference{}).Create(&inspection_reference).Error
- if err != nil {
- tx.Rollback()
- }
-
- data.IsSync = 1
- data.SyncId = inspection_reference.ID
- data.UpdatedTime = time.Now().Unix()
- ierr := writeMiddleDb.Save(&data).Error
- if ierr != nil {
- tx.Rollback()
- }
- }
- tx.Commit()
- return err
- }
-
- func SyncInspectionReferenceTwo(data *models.MiddleInspectionReference) error {
- tx := writeDb.Begin()
- var inspection_reference models.InspectionReference
- var total int
- 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
- if total <= 0 {
- inspection_reference.OrgId = data.OrgId
- inspection_reference.ProjectName = data.ProjectName
- inspection_reference.Project = data.ProjectName
- inspection_reference.ProjectId = data.ProjectId
- inspection_reference.ItemName = data.ItemName
- inspection_reference.ItemId = data.ItemId
- inspection_reference.RangeType = data.RangeType
- inspection_reference.RangeMin = data.RangeMin
- inspection_reference.RangeMax = data.RangeMax
- inspection_reference.RangeValue = data.RangeValue
- inspection_reference.RangeOptions = data.RangeOptions
- inspection_reference.Unit = data.Unit
- inspection_reference.Status = 1
- inspection_reference.CreatedTime = time.Now().Unix()
- inspection_reference.UpdatedTime = time.Now().Unix()
-
- err := tx.Model(&models.InspectionReference{}).Create(&inspection_reference).Error
- if err != nil {
- tx.Rollback()
- }
-
- data.IsSync = 1
- data.SyncId = inspection_reference.ID
- data.UpdatedTime = time.Now().Unix()
- ierr := writeMiddleDb.Save(&data).Error
- if ierr != nil {
- tx.Rollback()
- }
- }
- tx.Commit()
- return err
- }
-
- // 从机构将数据同步到中间库
- func SyncInspection(data *models.MiddleInspection) error {
- tx := writeDb.Begin()
- var inspection models.Inspection
- var total int
- 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
- if total <= 0 {
- inspection.OrgId = data.OrgId
- inspection.PatientId = data.PatientId
- inspection.ProjectName = data.ProjectName
- // inspection.Project = data.ProjectName
- inspection.ProjectId = data.ProjectId
- inspection.ItemName = data.ItemName
- inspection.ItemId = data.ItemId
- inspection.InspectType = int64(data.InspectType)
- inspection.InspectValue = data.InspectValue
- inspection.InspectTips = data.InspectTips
- inspection.InspectDate = data.RecordDate
- inspection.Status = 1
- inspection.CreatedTime = time.Now().Unix()
- inspection.UpdatedTime = time.Now().Unix()
-
- err := tx.Model(&models.Inspection{}).Create(&inspection).Error
- if err != nil {
- tx.Rollback()
- }
-
- data.IsSync = 1
- data.SyncId = inspection.ID
- data.UpdatedTime = time.Now().Unix()
- ierr := writeMiddleDb.Save(&data).Error
- if ierr != nil {
- tx.Rollback()
- }
- }
- tx.Commit()
- return err
- }
-
- // 跟进org_id获取未同步的检验检查选项信息
- func GetSyncInspectionReferenceByOrgId(org_id int64) (InspectionReferences []models.MiddleInspectionReference, err error) {
- err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and is_sync = 0", org_id).Find(&InspectionReferences).Error
- return
- }
-
- // 跟进org_id获取未同步的检验检查信息
- func GetSyncInspectionByOrgId(org_id int64) (Inspection []models.MiddleInspection, err error) {
- err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and status = 1 and is_sync = 0", org_id).Find(&Inspection).Error
- return
- }
-
- // 修改一条同步数据详情
- func UpdateSyncInfo(syncInfo *models.MiddleSyncInfo) error {
- err := writeMiddleDb.Save(&syncInfo).Error
- return err
- }
-
- // 创建一条同步数据详情
- func CreateSyncInfo(syncInfo *models.MiddleSyncInfo) error {
- err := writeMiddleDb.Create(&syncInfo).Error
- return err
- }
-
- // 创建一条检验检查操作表数据
- func CreateReportHandle(report *models.MiddleReportHandle) error {
- err := writeMiddleDb.Create(&report).Error
- return err
- }
-
- // 根据机构ID获取该机构检验检查操作表里状态为0的数据
- func GetReportHandleListyOrgID(org_id int64) (report_handle_list []models.MiddleReportHandle, err error) {
- err = readMiddleDb.Model(&models.MiddleReportHandle{}).Where("org_id = ? and handle_type = 0 ", org_id).Find(&report_handle_list).Error
- return
- }
-
- // 根据机构ID修改检验检查操作表里的数据
- func UpdateReportHandleListyOrgID(report_handle *models.MiddleReportHandle) error {
- err := writeMiddleDb.Model(&models.MiddleReportHandle{}).Updates(&report_handle).Error
- return err
- }
-
- // 跟进机构ID获取该机构所有病人的his_user_id
- func GetHisUserIdByOrgID(org_id int64) (patients []models.MiddlePatients, err error) {
- err = readMiddleDb.Model(&models.MiddlePatients{}).Where("user_org_id = ? and status = 1", org_id).Find(&patients).Error
- return
- }
-
- // 根据机构ID获取该机构上次同步信息
- func GetSyncTimeByOrgID(org_id int64, his_user_id string) (sync_info models.MiddleSyncInfo, err error) {
- err = readMiddleDb.Model(&models.MiddleSyncInfo{}).Where("org_id = ? and his_user_id =? ", org_id, his_user_id).Last(&sync_info).Error
- return
- }
-
- // 根据机构ID获取该机构上次同步信息
- func GetSyncTimeByOrgIDForYs(org_id int64) (sync_info models.MiddleSyncInfo, err error) {
- err = readMiddleDb.Model(&models.MiddleSyncInfo{}).Where("org_id = ? ", org_id).Last(&sync_info).Error
- return
- }
-
- // 根据机构ID和His_useer_id获取该患者最后一条同步检查数据
- func GetInspectionLastInfoByOrgID(org_id int64, his_user_id string) (inspectionLastInfo models.MiddleInspection, err error) {
- err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and his_user_id =? ", org_id, his_user_id).Order("created_time desc").First(&inspectionLastInfo).Error
- return
- }
-
- // 从机构将数据同步到中间库
- func Sync(data *models.TBLISItem, org_id int64, patient_id int64) (*models.MiddleInspectionReference, *models.MiddleInspection, error) {
- tx := writeMiddleDb.Begin()
- var inspection models.MiddleInspection
- var inspection_reference models.MiddleInspectionReference
- recordDateStr := data.UTime
- if len(data.UTime) == 0 {
- recordDateStr = time.Now().Format("2006-01-02 15:04")
- }
- record_date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
- // date, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", recordDateStr)
- // record_date,_ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
- var total int
- var itotal int
- if len(data.RangeOptions) > 0 {
- data.ItemType = 2
- } else {
- data.ItemType = 1
- if len(data.RangeMax) > 0 {
- Range := strings.Split(data.RangeMax, "-")
- if len(Range) > 1 {
- data.RangeMin = Range[0]
- data.RangeMax = Range[1]
- } else {
- data.RangeMin = Range[0]
- }
- }
- }
-
- 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
- if total <= 0 && len(data.ProjectName) > 0 {
- inspection_reference.OrgId = org_id
- inspection_reference.ProjectName = data.ProjectName
- inspection_reference.Project = data.ProjectName
- inspection_reference.ProjectId = data.ProjectId
- inspection_reference.ItemName = data.ItemName
- inspection_reference.ItemId = data.ItemId
- inspection_reference.RangeType = data.ItemType
- inspection_reference.RangeMin = data.RangeMin
- inspection_reference.RangeMax = data.RangeMax
- inspection_reference.RangeValue = data.RangeValue
- inspection_reference.RangeOptions = data.RangeMax
- inspection_reference.Unit = data.Unit
- inspection_reference.Status = 1
- inspection_reference.CreatedTime = time.Now().Unix()
- inspection_reference.UpdatedTime = time.Now().Unix()
- inspection_reference.InspectDate = data.InspectDate
- inspection_reference.UTime = data.UTime
- err := tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
- if err != nil {
- tx.Rollback()
- }
- }
-
- 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
- if itotal <= 0 {
- inspection.PatientId = patient_id
- inspection.OrgId = org_id
- inspection.ProjectId = data.ProjectId
- inspection.ItemName = data.ItemName
- inspection.ProjectName = data.ProjectName
- inspection.InspectType = data.ItemType
- inspection.ItemId = data.ItemId
- if data.ItemType == 1 {
- inspection.InspectValue = data.ItemValue
- } else {
- inspection.InspectValue = data.RangeValue
- }
- inspection.InspectDate = data.InspectDate
- inspection.RecordDate = record_date.Unix()
- inspection.Status = 1
- inspection.CreatedTime = time.Now().Unix()
- inspection.UpdatedTime = time.Now().Unix()
- inspection.UTime = data.UTime
- inspection.HisUserId = data.HisUserId
- err := tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
- if err != nil {
- tx.Rollback()
- }
- }
-
- tx.Commit()
- return &inspection_reference, &inspection, err
- }
-
- // GetPatientList 返回患者的列表
- func GetList(his_user_id []interface{}, syncTimes string) (lis []*models.TBLISItem, err error) {
- err = readRemoteDb.Model(&models.TBLISItem{}).Where("u_time > ? AND his_user_id in (?) ", syncTimes, his_user_id).Find(&lis).Error
- return
- }
-
- // GetPatientList 返回患者的列表
- func GetListByHisUserID(his_user_id string, syncTimes string, selectType int64) (lis []*models.TBLISItem, err error) {
- if selectType == 1 {
- err = readRemoteDb.Model(&models.TBLISItem{}).Where("u_time > ? AND patient_name = ? ", syncTimes, his_user_id).Find(&lis).Error
- } else {
- err = readRemoteDb.Model(&models.TBLISItem{}).Where("u_time > ? AND his_user_id = ? ", syncTimes, his_user_id).Find(&lis).Error
- }
- return
- }
|