classify_visible.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package models
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "time"
  6. )
  7. type ClassifyVisible struct {
  8. ClassifyVisibleId int `gorm:"column:classify_visible_id;primary_key"`
  9. ClassifyId int `gorm:"column:classify_id"`
  10. AdminId int `gorm:"column:admin_id"`
  11. CreateTime time.Time `gorm:"column:create_time"`
  12. }
  13. func (ClassifyVisible) TableName() string {
  14. return "classify_visible"
  15. }
  16. func ExtendClassifyVisible(parentId, classifyId int, adminIds []int) (err error) {
  17. tx := global.DmSQL["rddp"].Begin()
  18. defer func() {
  19. if err != nil {
  20. tx.Rollback()
  21. return
  22. }
  23. tx.Commit()
  24. }()
  25. err = tx.Table(ClassifyVisible{}.TableName()).Where("classify_id =?", parentId).Delete(&ClassifyVisible{}).Error
  26. insertList := make([]ClassifyVisible, 0, len(adminIds))
  27. for _, adminId := range adminIds {
  28. classifyVisible := ClassifyVisible{
  29. ClassifyId: classifyId,
  30. AdminId: adminId,
  31. CreateTime: time.Now(),
  32. }
  33. insertList = append(insertList, classifyVisible)
  34. }
  35. err = tx.CreateInBatches(insertList, utils.MultiAddNum).Error
  36. return
  37. }
  38. func UpdateClassifyVisible(classifyId int, adminId []int) (err error) {
  39. tx := global.DmSQL["rddp"].Begin()
  40. defer func() {
  41. if err != nil {
  42. tx.Rollback()
  43. return
  44. }
  45. tx.Commit()
  46. }()
  47. err = tx.Table(ClassifyVisible{}.TableName()).Where("classify_id =?", classifyId).Delete(&ClassifyVisible{}).Error
  48. insertList := make([]ClassifyVisible, 0, len(adminId))
  49. for _, admin := range adminId {
  50. classifyVisible := ClassifyVisible{
  51. ClassifyId: classifyId,
  52. AdminId: admin,
  53. CreateTime: time.Now(),
  54. }
  55. insertList = append(insertList, classifyVisible)
  56. }
  57. err = tx.CreateInBatches(insertList, utils.MultiAddNum).Error
  58. return
  59. }
  60. func GetClassifyVisibleUserIdByClassifyId(classifyId int) (classifyVisibles []int, err error) {
  61. db := global.DmSQL["rddp"]
  62. err = db.Table(ClassifyVisible{}.TableName()).Where("classify_id =?", classifyId).Pluck("admin_id", &classifyVisibles).Error
  63. return
  64. }