package Announcement import ( "Cold_Api/conf" "Cold_Api/controllers/lib" "encoding/json" "fmt" "github.com/astaxie/beego/cache" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" "strconv" "sync" "time" ) type Bulletin struct { Id int `orm:"column(ID);size(11);auto;pk" json:"id"` T_uuid string `orm:"size(256);null"` BulletinId string `orm:"size(256);null" json:"bulletinId"` // 公告id Title string `orm:"size(256);null" json:"title"` // 标题 Content string `orm:"size(256);null" json:"content"` // 内容 State int `orm:"size(2);default(0)" json:"state"` // 状态 0 可查看 1 不可查看 IsShow int `orm:"size(2);default(0)" json:"IsShow"` // 是否查看 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 第一次保存时才设置时间 } func (ab *Bulletin) MarshalJSON() ([]byte, error) { loc, _ := time.LoadLocation("Asia/Shanghai") type Alias Bulletin return json.Marshal(&struct { CreateTime string `json:"CreateTime,omitempty"` UpdateTime string `json:"UpdateTime,omitempty"` *Alias }{ CreateTime: ab.CreateTime.In(loc).Format("2006-01-02 15:04:05"), UpdateTime: ab.UpdateTime.In(loc).Format("2006-01-02 15:04:05"), Alias: (*Alias)(ab), }) } func (n *Bulletin) TableName() string { return "bulletin" } var redisCache_bulletin cache.Cache var Notice_map *sync.Map func init() { //注册模型 Notice_map = new(sync.Map) //注册模型 orm.RegisterModel(new(Bulletin)) config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`, "redis_Notice", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password) fmt.Println(config) var err error redisCache_bulletin, err = cache.NewCache("redis", config) if err != nil || redisCache_bulletin == nil { errMsg := "failed to init redis" logs.Error(errMsg, err) panic(errMsg) } } // ---------------- Redis ------------------- func Redis_Notice_Set(r Bulletin) (err error) { //json序列化 str, err := json.Marshal(r) if err != nil { logs.Error(lib.FuncName(), err) return } uptime := r.UpdateTime.Format("2006-01-02 15:04:05") key := uptime + r.Title err = redisCache_bulletin.Put(key, str, 24*time.Hour) if err != nil { logs.Error("set key:", key, ",value:", str, err) } return } func Redis_Notice_Get(b Bulletin) (r Bulletin, is bool) { uptime := b.UpdateTime.Format("2006-01-02 15:04:05") key := uptime + b.Title if redisCache_bulletin.IsExist(key) { //println("找到key:",key) v := redisCache_bulletin.Get(key) err := json.Unmarshal(v.([]byte), &r) if err != nil { logs.Error(lib.FuncName(), err) return Bulletin{}, false } return r, true } //println("没有 找到key:",key) return Bulletin{}, false } func Redis_Company_DelK(b Bulletin) (err error) { uptime := b.UpdateTime.Format("2006-01-02 15:04:05") key := uptime + b.Title err = redisCache_bulletin.Delete(key) if err != nil { logs.Error(lib.FuncName(), err) } return } // ---------------- 特殊方法 ------------------- // 添加公告 func AddBulletin(n Bulletin) (id int64, err error) { o := orm.NewOrm() id, err = o.Insert(&n) if err != nil { logs.Error(lib.FuncName(), err) return } n.Id = int(id) Redis_Notice_Set(n) return id, err } // GetBulletinById 根据id查询公告 func GetBulletinById(id, t_uuid string) (bulletin Bulletin, err error) { o := orm.NewOrm() query := o.QueryTable(&bulletin) err = query.Filter("ID", id).Filter("State", 0).Filter("t_uuid", t_uuid).One(&bulletin) if err != nil { logs.Error(lib.FuncName(), err) return } return bulletin, err } // DeleteBulletinById 删除公告 func DeleteBulletinById(bulletin_id string) (err error) { o := orm.NewOrm() query := o.QueryTable(new(Bulletin)) _, err = query.Filter("bulletin_id", bulletin_id).Delete() if err != nil { logs.Error(lib.FuncName(), err) return } //Redis_Company_DelK(bulletin) return err } // UpdateBulletin 修改公告 func UpdateBulletin(b Bulletin) (err error) { o := orm.NewOrm() query := o.QueryTable(new(Bulletin)) params := orm.Params{ "UpdateTime": b.UpdateTime, "state": b.State, } if b.Title != "" { params["Title"] = b.Title } if b.Content != "" { params["Content"] = b.Content } if b.IsShow != 0 { params["IsShow"] = b.IsShow } _, err = query.Filter("bulletin_id", b.BulletinId).Update(orm2.Params(params)) if err != nil { logs.Error(lib.FuncName(), err) return } return err } // Read_Bulletin_List 获取公告列表 func Read_Bulletin_List(t_uuid string, page int, page_z int, is_show string) (BulletinList []Bulletin, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(Bulletin)) var maps []Bulletin var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } cond := orm.NewCondition() cond1 := cond.And("State", 0).And("T_uuid", t_uuid) if len(is_show) > 0 { is_show_int, _ := strconv.Atoi(is_show) cond1 = cond1.And("is_show", is_show_int) } //if len(T_name) > 0 { // cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name)) //} var err error if page_z == 9999 { _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-update_time").All(&maps) } else { _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-update_time").All(&maps) } if err != nil { logs.Error(lib.FuncName(), err) } cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error(lib.FuncName(), err) } for _, v := range maps { BulletinList = append(BulletinList, v) } return BulletinList, cnt } // IsReadBulletin 已读状态 func IsReadBulletin(id, t_uuid, bulletin_id string) (err error) { o := orm.NewOrm() query := o.QueryTable(new(Bulletin)) params := orm.Params{ "is_show": bulletin_id, } _, err = query.Filter("id", id).Filter("t_uuid", t_uuid).Update(orm2.Params(params)) if err != nil { logs.Error(lib.FuncName(), err) return } return err } func Read_Bulletin_List_Admin(t_uuid string, page int, page_z int) (BulletinList []Bulletin, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(Bulletin)) var maps []Bulletin var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } cond := orm.NewCondition() cond1 := cond.And("T_uuid", t_uuid) //if len(T_name) > 0 { // cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name)) //} var err error if page_z == 9999 { _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-update_time").All(&maps) } else { _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-update_time").All(&maps) } if err != nil { logs.Error(lib.FuncName(), err) } cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error(lib.FuncName(), err) } for i, _ := range maps { BulletinList = append(BulletinList, maps[i]) } return BulletinList, cnt }