123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package InfoCollection
- import (
- "ColdVerify_server/lib"
- "ColdVerify_server/logs"
- _ "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 InfoTemplateClass 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 []InfoTemplateClass `orm:"-"`
- }
- func (t *InfoTemplateClass) TableName() string {
- return "info_template_class" // 数据库名称 // ************** 替换 FormulaList **************
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(InfoTemplateClass))
- }
- type InfoTemplateClass_R struct {
- Id int
- T_fid int // 项目 id, InfoTemplateClass.ID
- T_name string // 名称
- T_State int // 0 删除(伪删除) 1 正常(已交付) 2 开发中
- Children []InfoTemplateClass `orm:"-"`
- }
- // 获取 ById
- func Read_InfoTemplateClass_ById(id int) (r InfoTemplateClass, err error) {
- o := orm.NewOrm()
- r = InfoTemplateClass{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_InfoTemplateClass(var_ InfoTemplateClass) (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 {
- class_r, err := Read_InfoTemplateClass_ById(var_.T_fid)
- if err != nil {
- o.Rollback()
- return id, err
- }
- T_Path = class_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_InfoTemplateClass(r InfoTemplateClass, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&r, cols...); err == nil {
- logs.Println("Number of records updated in database:", num)
- return true
- }
- return false
- }
- // 批量删除
- func Delete_InfoTemplateClass_ByIds(ids []int) bool {
- o := orm.NewOrm()
- o.Begin()
- for _, id := range ids {
- v := InfoTemplateClass{Id: id, T_State: 0}
- if _, err := o.Update(&v, "T_State"); err != nil {
- o.Rollback()
- return false
- }
- }
- o.Commit()
- return true
- }
- // 获取列表
- func Read_InfoTemplateClass_List() (r []InfoTemplateClass) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- var map_r []InfoTemplateClass
- qs := o.QueryTable(new(InfoTemplateClass))
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1)
- qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
- r = InfoTemplateClass_Call(map_r, 0)
- return r
- }
- func InfoTemplateClass_Call(list []InfoTemplateClass, parentId int) []InfoTemplateClass {
- res := make([]InfoTemplateClass, 0)
- for _, v := range list {
- if v.T_fid == parentId {
- v.Children = InfoTemplateClass_Call(list, v.Id)
- res = append(res, v)
- }
- }
- return res
- }
- // 通过T_pid查询所有子id
- func ReadInfoTemplateClassIds_T_path(T_path string) (InfoTemplateClassIds []int) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(InfoTemplateClass))
- var InfoTemplateClassList []InfoTemplateClass
- qs.Filter("T_path__startswith", T_path).Filter("T_State", 1).All(&InfoTemplateClassList)
- for _, v := range InfoTemplateClassList {
- InfoTemplateClassIds = append(InfoTemplateClassIds, v.Id)
- }
- return InfoTemplateClassIds
- }
|