classify_menu_relation.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. // ClassifyMenuRelation 报告分类-子目录关联表
  7. type ClassifyMenuRelation struct {
  8. Id int `orm:"column(id);pk"`
  9. MenuId int `description:"子目录ID"`
  10. ClassifyId int `description:"二级分类ID"`
  11. CreateTime time.Time `description:"创建时间"`
  12. }
  13. func (item *ClassifyMenuRelation) TableName() string {
  14. return "classify_menu_relation"
  15. }
  16. func (item *ClassifyMenuRelation) Create() (err error) {
  17. o := orm.NewOrmUsingDB("rddp")
  18. id, err := o.Insert(item)
  19. if err != nil {
  20. return
  21. }
  22. item.Id = int(id)
  23. return
  24. }
  25. func (item *ClassifyMenuRelation) Update(cols []string) (err error) {
  26. o := orm.NewOrmUsingDB("rddp")
  27. _, err = o.Update(item, cols...)
  28. return
  29. }
  30. func (item *ClassifyMenuRelation) InsertMulti(items []*ClassifyMenuRelation) (err error) {
  31. o := orm.NewOrmUsingDB("rddp")
  32. _, err = o.InsertMulti(len(items), items)
  33. return
  34. }
  35. // GetClassifyMenuRelationList 获取子目录关联列表
  36. func GetClassifyMenuRelationList(condition string, pars []interface{}) (list []*ClassifyMenuRelation, err error) {
  37. o := orm.NewOrmUsingDB("rddp")
  38. sql := `SELECT * FROM classify_menu_relation WHERE 1 = 1 `
  39. sql += condition
  40. sql += ` ORDER BY create_time DESC`
  41. _, err = o.Raw(sql, pars).QueryRows(&list)
  42. return
  43. }
  44. // DeleteAndInsertClassifyMenuRelation 新增子目录关联
  45. func DeleteAndInsertClassifyMenuRelation(classifyId, menuId int) (err error) {
  46. o := orm.NewOrmUsingDB("rddp")
  47. tx, err := o.Begin()
  48. if err != nil {
  49. return
  50. }
  51. defer func() {
  52. if err != nil {
  53. _ = tx.Rollback()
  54. } else {
  55. _ = tx.Commit()
  56. }
  57. }()
  58. // 删除
  59. sql := `DELETE FROM classify_menu_relation WHERE classify_id = ?`
  60. if _, e := tx.Raw(sql, classifyId).Exec(); e != nil {
  61. err = e
  62. return
  63. }
  64. // 新增
  65. if menuId > 0 {
  66. relate := &ClassifyMenuRelation{
  67. ClassifyId: classifyId,
  68. MenuId: menuId,
  69. CreateTime: time.Now().Local(),
  70. }
  71. _, e := tx.Insert(relate)
  72. if e != nil {
  73. err = e
  74. }
  75. }
  76. return
  77. }