classify_menu_relation.go 2.3 KB

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