VerifyTemplateClass.go 3.7 KB

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