InfoTemplateClass.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. func Read_InfoTemplateClass_List_ALL() (maps []InfoTemplateClass) {
  35. o := orm.NewOrm()
  36. qs := o.QueryTable(new(InfoTemplateClass))
  37. qs.OrderBy("Id").All(&maps)
  38. return maps
  39. }
  40. func InfoTemplateClassListToMap(A []InfoTemplateClass) map[string]string {
  41. maps := make(map[string]string, len(A))
  42. for _, v := range A {
  43. maps[strconv.Itoa(v.Id)] = v.T_name
  44. }
  45. return maps
  46. }
  47. // 获取 ById
  48. func Read_InfoTemplateClass_ById(id int) (r InfoTemplateClass, err error) {
  49. o := orm.NewOrm()
  50. r = InfoTemplateClass{Id: id, T_State: 1}
  51. err = o.Read(&r, "Id", "T_State")
  52. if err != nil {
  53. logs.Error(lib.FuncName(), err)
  54. return r, err
  55. }
  56. return r, err
  57. }
  58. // 添加
  59. func Add_InfoTemplateClass(var_ InfoTemplateClass) (id int64, err error) {
  60. o := orm.NewOrm()
  61. o.Begin()
  62. id, err = o.Insert(&var_)
  63. if err != nil {
  64. o.Rollback()
  65. return id, err
  66. }
  67. T_Path := "/0/" + strconv.Itoa(int(id)) + "/"
  68. if var_.T_fid != 0 {
  69. class_r, err := Read_InfoTemplateClass_ById(var_.T_fid)
  70. if err != nil {
  71. o.Rollback()
  72. return id, err
  73. }
  74. T_Path = class_r.T_path + strconv.Itoa(int(id)) + "/"
  75. }
  76. var_.Id = int(id)
  77. var_.T_path = T_Path
  78. _, err = o.Update(&var_, "T_path")
  79. if err != nil {
  80. o.Rollback()
  81. return id, err
  82. }
  83. o.Commit()
  84. return id, err
  85. }
  86. // 修改
  87. func Update_InfoTemplateClass(r InfoTemplateClass, cols ...string) bool {
  88. o := orm.NewOrm()
  89. if num, err := o.Update(&r, cols...); err == nil {
  90. logs.Println("Number of records updated in database:", num)
  91. return true
  92. }
  93. return false
  94. }
  95. // 批量删除
  96. func Delete_InfoTemplateClass_ByIds(ids []int) bool {
  97. o := orm.NewOrm()
  98. o.Begin()
  99. for _, id := range ids {
  100. v := InfoTemplateClass{Id: id, T_State: 0}
  101. if _, err := o.Update(&v, "T_State"); err != nil {
  102. o.Rollback()
  103. return false
  104. }
  105. }
  106. o.Commit()
  107. return true
  108. }
  109. // 获取列表
  110. func Read_InfoTemplateClass_List() (r []InfoTemplateClass) {
  111. o := orm.NewOrm()
  112. // 也可以直接使用 Model 结构体作为表名
  113. var map_r []InfoTemplateClass
  114. qs := o.QueryTable(new(InfoTemplateClass))
  115. cond := orm.NewCondition()
  116. cond1 := cond.And("T_State", 1)
  117. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
  118. r = InfoTemplateClass_Call(map_r, 0)
  119. return r
  120. }
  121. func InfoTemplateClass_Call(list []InfoTemplateClass, parentId int) []InfoTemplateClass {
  122. res := make([]InfoTemplateClass, 0)
  123. for _, v := range list {
  124. if v.T_fid == parentId {
  125. v.Children = InfoTemplateClass_Call(list, v.Id)
  126. res = append(res, v)
  127. }
  128. }
  129. return res
  130. }
  131. // 通过T_pid查询所有子id
  132. func ReadInfoTemplateClassIds_T_path(T_path string) (InfoTemplateClassIds []int) {
  133. o := orm.NewOrm()
  134. qs := o.QueryTable(new(InfoTemplateClass))
  135. var InfoTemplateClassList []InfoTemplateClass
  136. qs.Filter("T_path__startswith", T_path).Filter("T_State", 1).All(&InfoTemplateClassList)
  137. for _, v := range InfoTemplateClassList {
  138. InfoTemplateClassIds = append(InfoTemplateClassIds, v.Id)
  139. }
  140. return InfoTemplateClassIds
  141. }