package Patient import ( "FollowUp_Notice/lib" "FollowUp_Notice/models/Illness" "FollowUp_Notice/models/Surgical" "FollowUp_Notice/models/Tag" "fmt" "github.com/astaxie/beego/logs" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "strconv" "strings" "sync" "time" ) // 患者 type Patient struct { Id int `orm:"column(ID);size(11);auto;pk"` T_uuid string `orm:"size(32);null"` // T_uid int `orm:"size(200);null"` // 关联的用户id T_number string `orm:"size(256);null"` // 病历号 T_name string `orm:"size(256);null"` // 姓名 T_age string `orm:"size(200);null"` // 年轻 T_tag string `orm:"size(200);null"` // 标签 T_illness int `orm:"size(200);null"` // 诊断 T_surgical int `orm:"size(200);null"` // 术式 T_phone string `orm:"size(256);null"` // 电话号码 T_next_time string `orm:"size(200);null"` // 下次复诊时间 T_notice_phone int `orm:"size(200);null"` // 手机 1通知 0不通知 T_notice_message int `orm:"size(200);null"` // 短信 1通知 0不通知 T_notice_interval string `orm:"size(200);null"` // 0,0,0,0 提前1天,提前2天,提前3天,提前7天 T_notice int `orm:"size(200);null"` // 通知状态 1待通知 2已通知 //T_follow_up int `orm:"size(200);null"` // 复诊状态 1正常 2超时 3结束 //T_record string `orm:"type(text);null"` // 2022-07-27,15|2022-07-27,15| 时间,间隔天数| T_State int `orm:"size(200);default(1)"` // 0删除 1正常 CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间 } type Patient_R struct { Id int T_uuid string // T_uid int // 关联的用户id T_number string // 病历号 T_name string // 姓名 T_age string // 年轻 T_tag_str string // 标签 T_tag []int // 标签 T_tag_List []string // 标签 T_illness int // 疾病 T_illness_name string // 疾病名称 T_surgical int // 术式 T_surgical_name string // 术式名称 T_phone string // 电话号码 T_next_time string // 下次复诊时间 T_notice_phone int // 手机 1通知 0不通知 T_notice_message int // 短信 1通知 0不通知 T_notice_interval string // 0,0,0,0 提前1天,提前2天,提前3天,提前7天 T_notice int // 待通知 已通知 //T_follow_up int // 复诊 超时 结束 //T_record string // 2022-07-27,15|2022-07-27,15| 时间,间隔天数| T_State int // 0删除 1正常 } type Patient_R2 struct { Id int `json:"id"` T_uuid string `json:"t_uuid"` // T_uid int `json:"t_uid"` // 关联的用户id T_name string `json:"t_name"` T_phone string `json:"t_phone"` // 电话号码 T_notice_phone int `json:"t_notice_phone"` // 手机 1通知 0不通知 T_notice_message int `json:"t_notice_message"` // 短信 1通知 0不通知 T_notice_interval string `json:"t_notice_interval"` // 0,0,0,0 提前1天,提前2天,提前3天,提前7天 T_patient_revisit_record_id int `json:"t_patient_revisit_record_id"` // 复诊记录id T_date string `json:"t_date"` // 复诊日期 T_pid int `json:"t_pid"` // 患者id T_illness_notice int `json:"t_illness_notice"` // 复诊通知内容id T_remark string `json:"t_remark"` // } func (t *Patient) TableName() string { return "patient" // 数据库名称 // ************** 替换 FormulaList ************** } var PatientMap *sync.Map // 泛型 func init() { //注册模型 orm.RegisterModel(new(Patient)) PatientMap = new(sync.Map) } func PatientToPatient_R(r Patient) (m Patient_R) { m.Id = r.Id m.T_uuid = r.T_uuid m.T_uid = r.T_uid m.T_number = r.T_number m.T_name = r.T_name m.T_age = r.T_age m.T_tag_str = r.T_tag if len(r.T_tag) > 0 { T_tag := strings.Split(strings.Trim(r.T_tag, "|"), "|") for _, v := range T_tag { t, _ := strconv.Atoi(v) m.T_tag = append(m.T_tag, t) m.T_tag_List = append(m.T_tag_List, Tag.Read_Tag_Get(t)) } } m.T_illness = r.T_illness m.T_illness_name = Illness.Read_Illness_Get(r.T_illness) m.T_surgical = r.T_surgical m.T_surgical_name = Surgical.Read_Surgical_Get(r.T_surgical) m.T_phone = r.T_phone m.T_next_time = r.T_next_time m.T_notice_phone = r.T_notice_phone m.T_notice_message = r.T_notice_message m.T_notice_interval = r.T_notice_interval m.T_notice = r.T_notice //m.T_follow_up = r.T_follow_up //m.T_record = r.T_record m.T_State = r.T_State return } // 添加 func Add_Patient(r Patient) (id int64, err error) { o := orm.NewOrm() //生成编号 rand_x := 0 for true { r.T_uuid = lib.GetRandstring(32, "", int64(rand_x)) err = o.Read(&r, "T_uuid") if err != nil { break } rand_x += 1 } r.T_State = 1 id, err = o.Insert(&r) if err != nil { logs.Error(lib.FuncName(), err) } return id, err } // 获取 ById func Read_Patient_ByT_uuid(T_uuid string) (r Patient, err error) { o := orm.NewOrm() qs := o.QueryTable(new(Patient)) err = qs.Filter("T_uuid", T_uuid).Filter("T_State", 1).One(&r) if err != nil { logs.Error(lib.FuncName(), err) } return } // 获取 ById func Read_Patient_ById(Id int) (r Patient, err error) { o := orm.NewOrm() qs := o.QueryTable(new(Patient)) err = qs.Filter("Id", Id).Filter("T_State", 1).One(&r) if err != nil { logs.Error(lib.FuncName(), err) } return } // 获取 ByT_number func Read_Patient_ByT_number(T_number string, T_uid int) (r Patient, err error) { o := orm.NewOrm() qs := o.QueryTable(new(Patient)) err = qs.Filter("T_number", T_number).Filter("T_uid", T_uid).Filter("T_State", 1).One(&r) if err != nil { logs.Error(lib.FuncName(), err) } return } // 修改 func Update_Patient(m Patient, cols ...string) error { o := orm.NewOrm() num, err := o.Update(&m, cols...) if err != nil { logs.Error(lib.FuncName(), err) return err } fmt.Println("Number of records updated in database:", num) return nil } // 删除 func Delete_Patient(v Patient) error { o := orm.NewOrm() v.T_State = 0 _, err := o.Update(&v, "T_State") if err != nil { logs.Error(lib.FuncName(), err) } return err } // 获取列表 func Read_Patient_List(T_uid int, T_number, T_name string, T_tag, T_illness, T_surgical, T_notice, T_age_sort, T_next_time_sort, page, page_z int) (r_ []Patient_R, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(Patient)) var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } // 过滤 cond := orm.NewCondition() cond = cond.And("T_State__gt", 0) if T_uid > 0 { cond = cond.And("T_uid", T_uid) } if len(T_number) > 0 { cond = cond.And("T_number__icontains", T_number) } if len(T_name) > 0 { cond = cond.And("T_name__icontains", T_name) } if T_tag > 0 { cond = cond.And("T_tag__icontains", fmt.Sprintf("|%d|", T_tag)) } if T_illness > 0 { cond = cond.And("T_illness", T_illness) } if T_surgical > 0 { cond = cond.And("T_surgical", T_surgical) } if T_notice > 0 { cond = cond.And("T_notice", T_notice) } var order []string if T_age_sort == 1 { order = append(order, "T_age") } else if T_age_sort == 2 { order = append(order, "-T_age") } if T_next_time_sort == 1 { order = append(order, "T_next_time") } else if T_next_time_sort == 2 { order = append(order, "-T_next_time") } order = append(order, "Id") // 查询 var r []Patient var err error if page_z == 9999 { _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy(order...).All(&r) } else { _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy(order...).All(&r) } if err != nil { logs.Error(lib.FuncName(), err) return } cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range r { r_ = append(r_, PatientToPatient_R(v)) } return r_, cnt } func Read_Patient_List_Lt_NextTime(T_uid int, T_next_time string) (r_ []Patient_R, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(Patient)) // 过滤 cond := orm.NewCondition() cond = cond.And("T_State__gt", 0) if T_uid > 0 { cond = cond.And("T_uid", T_uid) } if len(T_next_time) > 0 { cond = cond.And("T_next_time__lte", T_next_time) } // 查询 var r []Patient var err error _, err = qs.SetCond((*orm2.Condition)(cond)).All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range r { r_ = append(r_, PatientToPatient_R(v)) } return r_, cnt } // 患者通知列表 func Read_Patient_List_For_Notice(T_uid int, nowTime string) (r_ []Patient_R2) { o := orm.NewOrm() sql := "SELECT p.ID as id,t_uid,t_name,t_phone,t_notice_phone,t_notice_message,t_notice_interval," + "prr.ID as t_patient_revisit_record_id,t_date,t_illness_notice,t_remark" + " from patient p INNER JOIN patient_revisit_record prr ON p.id = prr.t_pid WHERE p.t__state > 0 AND p.t_uid = ? AND prr.t_date > '" + nowTime + "'" _, err := o.Raw(sql, T_uid).QueryRows(&r_) if err != nil { logs.Error(lib.FuncName(), err) } return r_ } func Read_Patient_All_Map(T_uid int) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(Patient)) var maps []Patient if T_uid > 0 { qs.Filter("T_uid", T_uid).All(&maps) } else { qs.All(&maps) } for _, v := range maps { PatientMap.Store(v.Id, v.T_name) } } func Read_Patient_T_name_Get(T_pid int) string { v, ok := PatientMap.Load(T_pid) if ok { return v.(string) } else { return "" } }