News.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package System
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/adapter/orm"
  5. "time"
  6. )
  7. type News struct {
  8. Id int `orm:"column(ID);size(11);auto;pk"`
  9. Admin_uuid string `orm:"size(256);null"` //
  10. News_Title string `orm:"size(256);null"` // 标题
  11. News_Url string `orm:"size(256);null"` // 地址
  12. News_Tag int `orm:"size(2);null"` // 标记 1 未阅读
  13. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  14. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  15. }
  16. func (t *News) TableName() string {
  17. return "news" // 数据库名称 // ************** 替换 FormulaList **************
  18. }
  19. func init() {
  20. //注册模型
  21. orm.RegisterModel(new(News))
  22. }
  23. // 获取 ById
  24. func Read_AdminNews_all_1(Admin_uuid string) (r []News) {
  25. o := orm.NewOrm()
  26. qs := o.QueryTable(new(News))
  27. qs.Filter("Admin_uuid", Admin_uuid).Filter("News_Tag", 1).All(&r)
  28. return r
  29. }
  30. // 修改 排序
  31. func Update_AdminNews_News_Tag_2(Id int) (err error) {
  32. o := orm.NewOrm()
  33. v := News{Id: Id}
  34. if err = o.Read(&v, "Id", "Admin_uuid"); err == nil {
  35. var num int64
  36. v.News_Tag = 2
  37. if num, err = o.Update(&v, "News_Tag"); err == nil {
  38. fmt.Println("Number of records updated in database:", num)
  39. }
  40. }
  41. return
  42. }