InfoTemplateClass.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package InfoCollection
  2. import (
  3. "ColdVerify_server/lib"
  4. "ColdVerify_server/logs"
  5. _ "github.com/astaxie/beego/cache/redis"
  6. "github.com/beego/beego/v2/adapter/orm"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  8. _ "github.com/go-sql-driver/mysql"
  9. "strconv"
  10. )
  11. // 模板
  12. type InfoTemplateClass struct {
  13. Id int `orm:"column(ID);size(11);auto;pk"`
  14. T_fid int `orm:"size(200);null"` // 父级分类ID,主类 为 0
  15. T_name string `orm:"size(256);null"` // 标题
  16. T_path string `orm:"size(256);null"` // 路径
  17. T_State int `orm:"size(2);default(1)"` // 0 删除(伪删除) 1 正常
  18. Children []InfoTemplateClass `orm:"-"`
  19. }
  20. func (t *InfoTemplateClass) TableName() string {
  21. return "info_template_class" // 数据库名称 // ************** 替换 FormulaList **************
  22. }
  23. func init() {
  24. //注册模型
  25. orm.RegisterModel(new(InfoTemplateClass))
  26. }
  27. type InfoTemplateClass_R struct {
  28. Id int
  29. T_fid int // 项目 id, InfoTemplateClass.ID
  30. T_name string // 名称
  31. T_State int // 0 删除(伪删除) 1 正常(已交付) 2 开发中
  32. Children []InfoTemplateClass `orm:"-"`
  33. }
  34. // 获取 ById
  35. func Read_InfoTemplateClass_ById(id int) (r InfoTemplateClass, err error) {
  36. o := orm.NewOrm()
  37. r = InfoTemplateClass{Id: id, T_State: 1}
  38. err = o.Read(&r, "Id", "T_State")
  39. if err != nil {
  40. logs.Error(lib.FuncName(), err)
  41. return r, err
  42. }
  43. return r, err
  44. }
  45. // 添加
  46. func Add_InfoTemplateClass(var_ InfoTemplateClass) (id int64, err error) {
  47. o := orm.NewOrm()
  48. o.Begin()
  49. id, err = o.Insert(&var_)
  50. if err != nil {
  51. o.Rollback()
  52. return id, err
  53. }
  54. T_Path := "/0/" + strconv.Itoa(int(id)) + "/"
  55. if var_.T_fid != 0 {
  56. class_r, err := Read_InfoTemplateClass_ById(var_.T_fid)
  57. if err != nil {
  58. o.Rollback()
  59. return id, err
  60. }
  61. T_Path = class_r.T_path + strconv.Itoa(int(id)) + "/"
  62. }
  63. var_.Id = int(id)
  64. var_.T_path = T_Path
  65. _, err = o.Update(&var_, "T_path")
  66. if err != nil {
  67. o.Rollback()
  68. return id, err
  69. }
  70. o.Commit()
  71. return id, err
  72. }
  73. // 修改
  74. func Update_InfoTemplateClass(r InfoTemplateClass, cols ...string) bool {
  75. o := orm.NewOrm()
  76. if num, err := o.Update(&r, cols...); err == nil {
  77. logs.Println("Number of records updated in database:", num)
  78. return true
  79. }
  80. return false
  81. }
  82. // 批量删除
  83. func Delete_InfoTemplateClass_ByIds(ids []int) bool {
  84. o := orm.NewOrm()
  85. o.Begin()
  86. for _, id := range ids {
  87. v := InfoTemplateClass{Id: id, T_State: 0}
  88. if _, err := o.Update(&v, "T_State"); err != nil {
  89. o.Rollback()
  90. return false
  91. }
  92. }
  93. o.Commit()
  94. return true
  95. }
  96. // 获取列表
  97. func Read_InfoTemplateClass_List() (r []InfoTemplateClass) {
  98. o := orm.NewOrm()
  99. // 也可以直接使用 Model 结构体作为表名
  100. var map_r []InfoTemplateClass
  101. qs := o.QueryTable(new(InfoTemplateClass))
  102. cond := orm.NewCondition()
  103. cond1 := cond.And("T_State", 1)
  104. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
  105. r = InfoTemplateClass_Call(map_r, 0)
  106. return r
  107. }
  108. func InfoTemplateClass_Call(list []InfoTemplateClass, parentId int) []InfoTemplateClass {
  109. res := make([]InfoTemplateClass, 0)
  110. for _, v := range list {
  111. if v.T_fid == parentId {
  112. v.Children = InfoTemplateClass_Call(list, v.Id)
  113. res = append(res, v)
  114. }
  115. }
  116. return res
  117. }
  118. // 通过T_pid查询所有子id
  119. func ReadInfoTemplateClassIds_T_path(T_path string) (InfoTemplateClassIds []int) {
  120. o := orm.NewOrm()
  121. qs := o.QueryTable(new(InfoTemplateClass))
  122. var InfoTemplateClassList []InfoTemplateClass
  123. qs.Filter("T_path__startswith", T_path).Filter("T_State", 1).All(&InfoTemplateClassList)
  124. for _, v := range InfoTemplateClassList {
  125. InfoTemplateClassIds = append(InfoTemplateClassIds, v.Id)
  126. }
  127. return InfoTemplateClassIds
  128. }