News.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package System
  2. import (
  3. "ColdVerify_server/conf"
  4. "fmt"
  5. "github.com/beego/beego/v2/adapter/orm"
  6. orm2 "github.com/beego/beego/v2/client/orm"
  7. "github.com/go-resty/resty/v2"
  8. "time"
  9. )
  10. type News struct {
  11. Id int `orm:"column(ID);size(11);auto;pk"`
  12. T_uuid string `orm:"size(256);null"` //
  13. T_Title string `orm:"size(256);null"` // 标题
  14. T_Url string `orm:"size(256);null"` // 地址
  15. T_Tag int `orm:"size(2);null"` // 标记 0未阅读 1 已阅读
  16. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  17. }
  18. type News_R struct {
  19. Id int
  20. T_uuid string
  21. T_Title string
  22. T_Url string
  23. T_Tag int
  24. CreateTime string
  25. }
  26. func (t *News) TableName() string {
  27. return "news" // 数据库名称
  28. }
  29. func init() {
  30. //注册模型
  31. orm.RegisterModel(new(News))
  32. }
  33. func NewsToNews_R(r News) (m News_R) {
  34. m.Id = r.Id
  35. m.T_uuid = r.T_uuid
  36. m.T_Title = r.T_Title
  37. m.T_Url = r.T_Url
  38. m.T_Tag = r.T_Tag
  39. m.CreateTime = r.CreateTime.Format("2006-01-02 15:04:05")
  40. return
  41. }
  42. // 添加
  43. func Add_News(T_uuid, T_Title, T_Url string) {
  44. r := News{T_uuid: T_uuid, T_Title: T_Title, T_Url: T_Url}
  45. o := orm.NewOrm()
  46. r.T_Tag = 0
  47. o.Insert(&r)
  48. //id, err = o.Insert(&r)
  49. //if err != nil {
  50. // fmt.Println(err)
  51. //}
  52. //return id, err
  53. }
  54. // 发送微信通知
  55. func Send_Weichat_News(T_admin_name, T_Title, T_Url string) {
  56. client := resty.New()
  57. client.R().
  58. SetFormData(map[string]string{
  59. "name": conf.WechatNews_GroupName,
  60. "data": fmt.Sprintf("@%s %s", T_admin_name, T_Title),
  61. }).
  62. Post(conf.WechatNews_Url)
  63. }
  64. // 获取列表
  65. func Read_News_List(T_uuid string, T_Title string, T_Tag int, page int, page_z int) (r_ []News_R, cnt int64) {
  66. o := orm.NewOrm()
  67. // 也可以直接使用 Model 结构体作为表名
  68. qs := o.QueryTable(new(News))
  69. var offset int64
  70. if page <= 1 {
  71. offset = 0
  72. } else {
  73. offset = int64((page - 1) * page_z)
  74. }
  75. // 过滤
  76. cond := orm.NewCondition()
  77. cond1 := cond.And("T_uuid", T_uuid) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  78. if len(T_Title) > 0 {
  79. //cond1.AndCond(cond.And("T_class", class))
  80. cond1 = cond1.And("T_Title__icontains", T_Title)
  81. }
  82. if T_Tag == 0 {
  83. cond1 = cond1.And("T_Tag", 0)
  84. }
  85. var r []News
  86. // 查询
  87. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  88. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  89. for _, v := range r {
  90. r_ = append(r_, NewsToNews_R(v))
  91. }
  92. return r_, cnt
  93. }
  94. // 修改 排序
  95. func Update_News_Tag(Id int, T_uuid string) (err error) {
  96. o := orm.NewOrm()
  97. v := News{Id: Id}
  98. if err = o.Read(&v, "Id"); err == nil {
  99. var num int64
  100. if v.T_uuid != T_uuid {
  101. return
  102. }
  103. v.T_Tag = 1
  104. if num, err = o.Update(&v, "T_Tag"); err == nil {
  105. fmt.Println("Number of records updated in database:", num)
  106. }
  107. }
  108. return
  109. }