VerifyTemplateClass.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package VerifyTemplate
  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 VerifyTemplateClass 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 []VerifyTemplateClass `orm:"-"`
  19. }
  20. func (t *VerifyTemplateClass) TableName() string {
  21. return "verify_template_class" // 数据库名称 // ************** 替换 FormulaList **************
  22. }
  23. func init() {
  24. //注册模型
  25. orm.RegisterModel(new(VerifyTemplateClass))
  26. }
  27. type VerifyTemplateClass_R struct {
  28. Id int
  29. T_fid int // 项目 id, VerifyTemplateClass.ID
  30. T_name string // 名称
  31. T_State int // 0 删除(伪删除) 1 正常(已交付) 2 开发中
  32. Children []VerifyTemplateClass `orm:"-"`
  33. }
  34. // 获取 ById
  35. func Read_VerifyTemplateClass_ById(id int) (r VerifyTemplateClass, err error) {
  36. o := orm.NewOrm()
  37. r = VerifyTemplateClass{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_VerifyTemplateClass(var_ VerifyTemplateClass) (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_VerifyTemplateClass_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_VerifyTemplateClass(r VerifyTemplateClass, 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_VerifyTemplateClass_ByIds(ids []int) bool {
  84. o := orm.NewOrm()
  85. o.Begin()
  86. for _, id := range ids {
  87. v := VerifyTemplateClass{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_VerifyTemplateClass_List() (r []VerifyTemplateClass) {
  98. o := orm.NewOrm()
  99. // 也可以直接使用 Model 结构体作为表名
  100. var map_r []VerifyTemplateClass
  101. qs := o.QueryTable(new(VerifyTemplateClass))
  102. cond := orm.NewCondition()
  103. cond1 := cond.And("T_State", 1)
  104. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
  105. r = VerifyTemplateClass_Call(map_r, 0)
  106. return r
  107. }
  108. func VerifyTemplateClass_Call(list []VerifyTemplateClass, parentId int) []VerifyTemplateClass {
  109. res := make([]VerifyTemplateClass, 0)
  110. for _, v := range list {
  111. if v.T_fid == parentId {
  112. v.Children = VerifyTemplateClass_Call(list, v.Id)
  113. res = append(res, v)
  114. }
  115. }
  116. return res
  117. }
  118. // 通过T_pid查询所有子id
  119. func ReadVerifyTemplateClassIds_T_path(T_path string) (VerifyTemplateClassIds []int) {
  120. o := orm.NewOrm()
  121. qs := o.QueryTable(new(VerifyTemplateClass))
  122. var VerifyTemplateClassList []VerifyTemplateClass
  123. qs.Filter("T_path__startswith", T_path).Filter("T_State", 1).All(&VerifyTemplateClassList)
  124. for _, v := range VerifyTemplateClassList {
  125. VerifyTemplateClassIds = append(VerifyTemplateClassIds, v.Id)
  126. }
  127. return VerifyTemplateClassIds
  128. }