package Patient import ( "FollowUp_Notice/conf" "FollowUp_Notice/lib" "fmt" "github.com/astaxie/beego/cache" _ "github.com/astaxie/beego/cache/redis" "github.com/astaxie/beego/logs" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "time" ) // 患者信息发送 type PatientSend struct { Id int `orm:"column(ID);size(11);auto;pk"` T_uid int `orm:"index;size(100);null"` // 用户id T_pid int `orm:"index;size(100);null"` // 患者id T_phone string `orm:"size(256);null"` // 患者电话 18888888888 T_type int `orm:"index;size(4);null"` // 1 短信 2 电话 3 电话满意度调查 T_id string `orm:"size(256);null"` // 发送id T_code string `orm:"size(256);null"` // 错误码 T_fee float64 `orm:"digits(12);decimals(2)"` // 错误码 T_remark string `orm:"size(256);null"` // 备注 T_State int `orm:"size(200);default(1)"` // 0失败 1正常 T_digitInfo string `orm:"size(200)"` // 满意度调查 放音收号结果 CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间 } type PatientSend_R struct { Id int T_pid int // 患者电话 T_phone string // 18888888888 T_type int // 1 短信 2 电话 T_id string T_code string T_remark string // 备注 T_State int // 0失败 1正常 T_fee_num int // 计费条数 赛邮云 短信 T_fee 电话 T_fee/0.09 CreateTime string //auto_now_add 第一次保存时才设置时间 } func (t *PatientSend) TableName() string { return "patient_send" // 数据库名称 // ************** 替换 FormulaList ************** } var redisCache_PatientSend cache.Cache func init() { //注册模型 orm.RegisterModel(new(PatientSend)) config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`, "redis_PatientSend", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password) fmt.Println(config) var err error redisCache_PatientSend, err = cache.NewCache("redis", config) if err != nil || redisCache_PatientSend == nil { errMsg := "failed to init redis" logs.Error(errMsg, err) } } // ---------------- Redis ------------------- func Redis_PatientSend_Set(key string, r string) (err error) { err = redisCache_PatientSend.Put(key, r, 10*time.Minute) if err != nil { logs.Error("set key:", key, ",value:", r, err) } return } func Redis_PatientSend_Get(key string) (r string, is bool) { if redisCache_PatientSend.IsExist(key) { //println("找到key:",key) v := redisCache_PatientSend.Get(key) value := string(v.([]byte)) Redis_PatientSend_Set(key, value) return value, true } //println("没有 找到key:",key) return "", false } func Redis_PatientSend_DelK(key string) (err error) { err = redisCache_PatientSend.Delete(key) if err != nil { logs.Error(lib.FuncName(), err) } return } func PatientSendToPatientSend_R(r PatientSend) (m PatientSend_R) { m.Id = r.Id m.T_pid = r.T_pid m.T_phone = r.T_phone m.T_type = r.T_type m.T_id = r.T_id m.T_code = r.T_code m.T_remark = r.T_remark m.T_State = r.T_State if r.T_type == 1 { m.T_fee_num = int(r.T_fee) } if r.T_type == 2 { m.T_fee_num = int(r.T_fee / 0.09) } m.CreateTime = r.CreateTime.Format("2006-01-02 15:04:05") return } // 添加 func Add_PatientSend(r PatientSend) (id int64, err error) { o := orm.NewOrm() id, err = o.Insert(&r) if err != nil { logs.Error(lib.FuncName(), err) } return id, err } // 获取 ById func Read_PatientSend_ByT_id(T_id string) (r PatientSend, err error) { o := orm.NewOrm() qs := o.QueryTable(new(PatientSend)) err = qs.Filter("T_id", T_id).One(&r) if err != nil { logs.Error(lib.FuncName(), err) } return } // 修改 func Update_PatientSend(m PatientSend, 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 Read_PatientSend_List(T_uid, T_pid, T_type int, T_date string, page, page_z int) (r_ []PatientSend_R, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(PatientSend)) var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } // 过滤 cond := orm.NewCondition() if T_uid > 0 { cond = cond.And("T_uid", T_uid) } if T_pid > 0 { cond = cond.And("T_pid", T_pid) } if T_type > 0 { cond = cond.And("T_type", T_type) } if len(T_date) > 0 { cond = cond.And("CreateTime__startswith", T_date) } // 查询 var r []PatientSend var err error if page_z == 9999 { _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r) } else { _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").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_, PatientSendToPatientSend_R(v)) } return r_, cnt } // 获取列表 func Read_PatientSend_Count_Yesterday(T_uid, T_pid, T_type int, date string) (cnt float64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(PatientSend)) // 过滤 cond := orm.NewCondition() cond = cond.And("T_State", 1) if T_uid > 0 { cond = cond.And("T_uid", T_uid) } if T_pid > 0 { cond = cond.And("T_pid", T_pid) } if T_type > 0 { cond = cond.And("T_type", T_type) } if len(date) > 0 { cond = cond.And("CreateTime__gte", date+" 00:00:00").And("CreateTime__lte", date+" 23:59:59") } type result struct { Total float64 `json:"total"` } var res []result _, err := qs.SetCond((*orm2.Condition)(cond)).Aggregate("sum(t_fee) as total").All(&res) if err != nil { logs.Error(lib.FuncName(), err) return } //cnt, err := qs.SetCond((*orm2.Condition)(cond)).Count() //var total float64 //_, err := qs.SetCond((*orm2.Condition)(cond)).Aggregate("sum(t_fee) as total").All(&total) //if err != nil { // logs.Error(lib.FuncName(), err) // return //} return res[0].Total } func Read_PatientSend_Satisfaction_Count_Yesterday(T_uid, T_pid int, date string) (cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(PatientSend)) // 过滤 cond := orm.NewCondition() cond = cond.And("T_type", 3) cond = cond.AndCond(cond.Or("T_State", 1).Or("T_code", "8008")) if T_uid > 0 { cond = cond.And("T_uid", T_uid) } if T_pid > 0 { cond = cond.And("T_pid", T_pid) } if len(date) > 0 { cond = cond.And("CreateTime__gte", date+" 00:00:00").And("CreateTime__lte", date+" 23:59:59") } cnt, err := qs.SetCond((*orm2.Condition)(cond)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } return cnt } // 获取列表 func Read_PatientSend_Count_Month(T_uid, T_pid, T_type int, month string, T_State int) (cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(PatientSend)) // 过滤 cond := orm.NewCondition() cond = cond.And("T_State", T_State) if T_uid > 0 { cond = cond.And("T_uid", T_uid) } if T_pid > 0 { cond = cond.And("T_pid", T_pid) } if T_type > 0 { cond = cond.And("T_type", T_type) } if len(month) > 0 { cond = cond.And("CreateTime__startswith", month) } cnt, err := qs.SetCond((*orm2.Condition)(cond)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } return cnt }