package System import ( "ColdVerify_server/conf" "fmt" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "github.com/go-resty/resty/v2" "time" ) type News struct { Id int `orm:"column(ID);size(11);auto;pk"` T_uuid string `orm:"size(256);null"` // T_Title string `orm:"size(256);null"` // 标题 T_Url string `orm:"size(256);null"` // 地址 T_Tag int `orm:"size(2);null"` // 标记 0未阅读 1 已阅读 CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新 } type News_R struct { Id int T_uuid string T_Title string T_Url string T_Tag int CreateTime string } func (t *News) TableName() string { return "news" // 数据库名称 } func init() { //注册模型 orm.RegisterModel(new(News)) } func NewsToNews_R(r News) (m News_R) { m.Id = r.Id m.T_uuid = r.T_uuid m.T_Title = r.T_Title m.T_Url = r.T_Url m.T_Tag = r.T_Tag m.CreateTime = r.CreateTime.Format("2006-01-02 15:04:05") return } // 添加 func Add_News(T_uuid, T_Title, T_Url string) { r := News{T_uuid: T_uuid, T_Title: T_Title, T_Url: T_Url} o := orm.NewOrm() r.T_Tag = 0 o.Insert(&r) //id, err = o.Insert(&r) //if err != nil { // fmt.Println(err) //} //return id, err } // 发送微信通知 func Send_Weichat_News(T_admin_name, T_Title, T_Url string) { client := resty.New() client.R(). SetFormData(map[string]string{ "name": conf.WechatNews_GroupName, "data": fmt.Sprintf("@%s %s", T_admin_name, T_Title), }). Post(conf.WechatNews_Url) } // 获取列表 func Read_News_List(T_uuid string, T_Title string, T_Tag int, page int, page_z int) (r_ []News_R, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(News)) 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) // .AndNot("status__in", 1).Or("profile__age__gt", 2000) if len(T_Title) > 0 { //cond1.AndCond(cond.And("T_class", class)) cond1 = cond1.And("T_Title__icontains", T_Title) } if T_Tag == 0 { cond1 = cond1.And("T_Tag", 0) } var r []News // 查询 qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r) cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count() for _, v := range r { r_ = append(r_, NewsToNews_R(v)) } return r_, cnt } // 修改 排序 func Update_News_Tag(Id int, T_uuid string) (err error) { o := orm.NewOrm() v := News{Id: Id} if err = o.Read(&v, "Id"); err == nil { var num int64 if v.T_uuid != T_uuid { return } v.T_Tag = 1 if num, err = o.Update(&v, "T_Tag"); err == nil { fmt.Println("Number of records updated in database:", num) } } return }