classify_menu.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. // ClassifyMenu 报告分类-子目录表
  8. type ClassifyMenu struct {
  9. MenuId int `orm:"column(menu_id);pk"`
  10. MenuName string `description:"子目录名称"`
  11. ClassifyId int `description:"一级分类ID"`
  12. Sort int `description:"排序"`
  13. CreateTime time.Time `description:"创建时间"`
  14. ModifyTime time.Time `description:"更新时间"`
  15. }
  16. func (item *ClassifyMenu) TableName() string {
  17. return "classify_menu"
  18. }
  19. func (item *ClassifyMenu) Create() (err error) {
  20. o := orm.NewOrmUsingDB("rddp")
  21. id, err := o.Insert(item)
  22. if err != nil {
  23. return
  24. }
  25. item.MenuId = int(id)
  26. return
  27. }
  28. func (item *ClassifyMenu) Update(cols []string) (err error) {
  29. o := orm.NewOrmUsingDB("rddp")
  30. _, err = o.Update(item, cols...)
  31. return
  32. }
  33. func (item *ClassifyMenu) InsertMulti(items []*ClassifyMenu) (err error) {
  34. o := orm.NewOrmUsingDB("rddp")
  35. _, err = o.InsertMulti(len(items), items)
  36. return
  37. }
  38. // GetClassifyMenuById 主键获取子目录
  39. func GetClassifyMenuById(id int) (item *ClassifyMenu, err error) {
  40. o := orm.NewOrmUsingDB("rddp")
  41. sql := `SELECT * FROM classify_menu WHERE menu_id = ? LIMIT 1`
  42. err = o.Raw(sql, id).QueryRow(&item)
  43. return
  44. }
  45. // GetClassifyMenuList 获取子目录列表
  46. func GetClassifyMenuList(condition string, pars []interface{}) (list []*ClassifyMenu, err error) {
  47. o := orm.NewOrmUsingDB("rddp")
  48. sql := `SELECT * FROM classify_menu WHERE 1 = 1 `
  49. sql += condition
  50. sql += ` ORDER BY sort ASC, create_time ASC`
  51. _, err = o.Raw(sql, pars).QueryRows(&list)
  52. return
  53. }
  54. // ClassifyMenuSaveReq 保存分类子目录请求体
  55. type ClassifyMenuSaveReq struct {
  56. MenuId int `description:"子目录ID, 0为新增, 大于0为编辑"`
  57. MenuName string `description:"子目录名称"`
  58. //ClassifyId int `description:"一级分类ID"`
  59. }
  60. // InsertAndUpdateClassifyMenu 新增/编辑/删除分类子目录
  61. func InsertAndUpdateClassifyMenu(insertMenus []*ClassifyMenu, editMenus []*ClassifyMenu, deleteMenuIds []int) (err error) {
  62. o := orm.NewOrmUsingDB("rddp")
  63. tx, err := o.Begin()
  64. if err != nil {
  65. return
  66. }
  67. defer func() {
  68. if err != nil {
  69. _ = tx.Rollback()
  70. } else {
  71. _ = tx.Commit()
  72. }
  73. }()
  74. // 编辑
  75. sql := ``
  76. if len(editMenus) > 0 {
  77. for i := range editMenus {
  78. sql = fmt.Sprintf(`UPDATE classify_menu SET menu_name = '%s', sort = %d, modify_time = NOW() WHERE menu_id = %d`,
  79. editMenus[i].MenuName, editMenus[i].Sort, editMenus[i].MenuId)
  80. if _, e := tx.Raw(sql).Exec(); e != nil {
  81. err = e
  82. return
  83. }
  84. }
  85. }
  86. // 删除
  87. if len(deleteMenuIds) > 0 {
  88. for i := range deleteMenuIds {
  89. sql = fmt.Sprintf(`DELETE FROM classify_menu WHERE menu_id = %d LIMIT 1`, deleteMenuIds[i])
  90. if _, e := tx.Raw(sql).Exec(); e != nil {
  91. err = e
  92. return
  93. }
  94. // 删除关联关系
  95. sql = fmt.Sprintf(`DELETE FROM classify_menu_relation WHERE menu_id = %d`, deleteMenuIds[i])
  96. if _, e := tx.Raw(sql).Exec(); e != nil {
  97. err = e
  98. return
  99. }
  100. }
  101. }
  102. // 新增
  103. if len(insertMenus) > 0 {
  104. _, e := tx.InsertMulti(len(insertMenus), insertMenus)
  105. if e != nil {
  106. err = e
  107. return
  108. }
  109. }
  110. return
  111. }