package VerifyTemplate import ( "ColdVerify_server/lib" "ColdVerify_server/logs" "fmt" _ "github.com/astaxie/beego/cache/redis" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" _ "github.com/go-sql-driver/mysql" "strconv" ) // 模板 type VerifyTemplateClass struct { Id int `orm:"column(ID);size(11);auto;pk"` T_fid int `orm:"size(200);null"` // 父级分类ID,主类 为 0 T_name string `orm:"size(256);null"` // 标题 T_path string `orm:"size(256);null"` // 路径 T_State int `orm:"size(2);default(1)"` // 0 删除(伪删除) 1 正常 Children []VerifyTemplateClass `orm:"-"` } func (t *VerifyTemplateClass) TableName() string { return "verify_template_class" // 数据库名称 // ************** 替换 FormulaList ************** } func init() { //注册模型 orm.RegisterModel(new(VerifyTemplateClass)) } type VerifyTemplateClass_R struct { Id int T_fid int // 项目 id, VerifyTemplateClass.ID T_name string // 名称 T_State int // 0 删除(伪删除) 1 正常(已交付) 2 开发中 Children []VerifyTemplateClass `orm:"-"` } // 获取 ById func Read_VerifyTemplateClass_ById(id int) (r VerifyTemplateClass, err error) { o := orm.NewOrm() r = VerifyTemplateClass{Id: id, T_State: 1} err = o.Read(&r, "Id", "T_State") if err != nil { logs.Error(lib.FuncName(), err) return r, err } return r, err } // 添加 func Add_VerifyTemplateClass(var_ VerifyTemplateClass) (id int64, err error) { o := orm.NewOrm() o.Begin() id, err = o.Insert(&var_) if err != nil { o.Rollback() return id, err } T_Path := "/0/" + strconv.Itoa(int(id)) + "/" if var_.T_fid != 0 { Company_r, err := Read_VerifyTemplateClass_ById(var_.T_fid) if err != nil { o.Rollback() return id, err } T_Path = Company_r.T_path + strconv.Itoa(int(id)) + "/" } var_.Id = int(id) var_.T_path = T_Path _, err = o.Update(&var_, "T_path") if err != nil { o.Rollback() return id, err } o.Commit() return id, err } // 修改 func Update_VerifyTemplateClass(r VerifyTemplateClass, cols ...string) bool { o := orm.NewOrm() if num, err := o.Update(&r, cols...); err == nil { fmt.Println("Number of records updated in database:", num) return true } return false } // 批量删除 func Delete_VerifyTemplateClass_ByIds(ids []int) bool { o := orm.NewOrm() o.Begin() for _, id := range ids { v := VerifyTemplateClass{Id: id, T_State: 0} if _, err := o.Update(&v, "T_State"); err != nil { o.Rollback() return false } } o.Commit() return true } // 获取列表 func Read_VerifyTemplateClass_List() (r []VerifyTemplateClass) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 var map_r []VerifyTemplateClass qs := o.QueryTable(new(VerifyTemplateClass)) cond := orm.NewCondition() cond1 := cond.And("T_State", 1) qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r) r = VerifyTemplateClass_Call(map_r, 0) return r } func VerifyTemplateClass_Call(list []VerifyTemplateClass, parentId int) []VerifyTemplateClass { res := make([]VerifyTemplateClass, 0) for _, v := range list { if v.T_fid == parentId { v.Children = VerifyTemplateClass_Call(list, v.Id) res = append(res, v) } } return res } // 通过T_pid查询所有子id func ReadVerifyTemplateClassIds_T_path(T_path string) (VerifyTemplateClassIds []int) { o := orm.NewOrm() qs := o.QueryTable(new(VerifyTemplateClass)) var VerifyTemplateClassList []VerifyTemplateClass qs.Filter("T_path__startswith", T_path).Filter("T_State", 1).All(&VerifyTemplateClassList) for _, v := range VerifyTemplateClassList { VerifyTemplateClassIds = append(VerifyTemplateClassIds, v.Id) } return VerifyTemplateClassIds }