123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- package InfoCollection
- import (
- "ColdVerify_server/lib"
- "ColdVerify_server/logs"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- _ "github.com/go-sql-driver/mysql"
- "time"
- )
- // 模板
- type InfoTemplate struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_class int `orm:"size(8);default(0)"` // 分类
- T_InfoTemplate_id string `orm:"size(256);null"` // id
- T_name string `orm:"size(256);null"` // 标题
- T_sort int `orm:"size(2);default(1)"` // 排序
- CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
- }
- type InfoTemplate_R struct {
- T_InfoTemplate_id string `orm:"size(256);null"` // id
- T_name string `orm:"size(256);null"` // 标题
- T_sort int `orm:"size(200);null"` // 分类
- }
- func (t *InfoTemplate) TableName() string {
- return "info_template" // 数据库名称 // ************** 替换 FormulaList **************
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(InfoTemplate))
- }
- // -------------------------------------------------------------
- func InfoTemplateToInfoTemplate_R(T InfoTemplate) (T_r InfoTemplate_R) {
- T_r.T_InfoTemplate_id = T.T_InfoTemplate_id
- T_r.T_name = T.T_name
- T_r.T_sort = T.T_sort
- return T_r
- }
- func Read_InfoTemplate_List_ALL() (maps []InfoTemplate) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(InfoTemplate))
- qs.OrderBy("Id").All(&maps)
- return maps
- }
- func InfoTemplateListToMap(A []InfoTemplate) map[string]string {
- maps := make(map[string]string, len(A))
- for _, v := range A {
- maps[v.T_InfoTemplate_id] = v.T_name
- }
- return maps
- }
- // ---------------- 特殊方法 -------------------
- // 获取 ById
- func Read_InfoTemplate_ById(id int) (r InfoTemplate, is bool) {
- o := orm.NewOrm()
- r = InfoTemplate{Id: id}
- err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, false
- }
- return r, true
- }
- // 获取 By
- func Read_InfoTemplate(T_InfoTemplate_id string) (r InfoTemplate, is bool) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(InfoTemplate))
- err := qs.Filter("T_InfoTemplate_id", T_InfoTemplate_id).One(&r)
- if err != nil {
- return r, false
- }
- return r, true
- }
- func Read_InfoTemplate_T_name(T_name string) (r InfoTemplate, is bool) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(InfoTemplate))
- err := qs.Filter("T_name", T_name).One(&r)
- if err != nil {
- return r, false
- }
- return r, true
- }
- // 添加
- func Add_InfoTemplate(r InfoTemplate) (T_InfoTemplate_id string, is bool) {
- o := orm.NewOrm()
- // 生成编号
- rand_x := 0
- for true {
- r.T_InfoTemplate_id = lib.GetRandstring(4, "", int64(rand_x)) // 1,336,336
- err := o.Read(&r, "T_InfoTemplate_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- break
- }
- rand_x += 1
- }
- _, err := o.Insert(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return "", false
- }
- return r.T_InfoTemplate_id, true
- }
- // 删除
- func Delete_InfoTemplate(v InfoTemplate) bool {
- o := orm.NewOrm()
- if num, err := o.Delete(&v); err == nil {
- logs.Println("Number of records deleted in database:", num)
- } else {
- return false
- }
- return true
- }
- // 修改
- func Update_InfoTemplate(m InfoTemplate, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, cols...); err == nil {
- logs.Println("Number of records updated in database:", num)
- return true
- }
- return false
- }
- // 获取列表
- func Read_InfoTemplate_List(T_class int, T_name string, page int, page_z int) ([]InfoTemplate_R, int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- var r []InfoTemplate
- qs := o.QueryTable(new(InfoTemplate))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- cond := orm.NewCondition()
- cond1 := cond.And("T_class", T_class).And("T_name__icontains", T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_name").All(&r)
- cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
- // 转换
- var InfoTemplate_r []InfoTemplate_R
- for _, v := range r {
- InfoTemplate_r = append(InfoTemplate_r, InfoTemplateToInfoTemplate_R(v))
- }
- return InfoTemplate_r, cnt
- }
|