classify_visible.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 GetClassifyVisibleAll() (items []*ClassifyVisible, err error) {
  17. sql := "SELECT * FROM classify_visible"
  18. err = global.DmSQL["rddp"].Raw(sql).Find(&items).Error
  19. return
  20. }
  21. func GetNoVisibleClassifyIdByAdminId(adminId int) (classifyIds []int, err error) {
  22. sql := `SELECT classify_id FROM classify_visible GROUP BY classify_id HAVING SUM(CASE WHEN admin_id =? THEN 1 ELSE 0 END) = 0`
  23. err = global.DmSQL["rddp"].Raw(sql, adminId).Find(&classifyIds).Error
  24. return
  25. }
  26. func ExtendClassifyVisible(parentId, classifyId int, adminIds []int) (err error) {
  27. tx := global.DmSQL["rddp"].Begin()
  28. defer func() {
  29. if err != nil {
  30. tx.Rollback()
  31. return
  32. }
  33. tx.Commit()
  34. }()
  35. err = tx.Table(ClassifyVisible{}.TableName()).Where("classify_id =?", parentId).Delete(&ClassifyVisible{}).Error
  36. insertList := make([]ClassifyVisible, 0, len(adminIds))
  37. for _, adminId := range adminIds {
  38. classifyVisible := ClassifyVisible{
  39. ClassifyId: classifyId,
  40. AdminId: adminId,
  41. CreateTime: time.Now(),
  42. }
  43. insertList = append(insertList, classifyVisible)
  44. }
  45. err = tx.CreateInBatches(insertList, utils.MultiAddNum).Error
  46. return
  47. }
  48. func UpdateClassifyVisible(classifyId int, adminId []int) (err error) {
  49. tx := global.DmSQL["rddp"].Begin()
  50. defer func() {
  51. if err != nil {
  52. tx.Rollback()
  53. return
  54. }
  55. tx.Commit()
  56. }()
  57. err = tx.Table(ClassifyVisible{}.TableName()).Where("classify_id =?", classifyId).Delete(&ClassifyVisible{}).Error
  58. if err != nil {
  59. return
  60. }
  61. if len(adminId) > 0 {
  62. insertList := make([]ClassifyVisible, 0, len(adminId))
  63. for _, admin := range adminId {
  64. classifyVisible := ClassifyVisible{
  65. ClassifyId: classifyId,
  66. AdminId: admin,
  67. CreateTime: time.Now(),
  68. }
  69. insertList = append(insertList, classifyVisible)
  70. }
  71. err = tx.CreateInBatches(insertList, utils.MultiAddNum).Error
  72. }
  73. return
  74. }
  75. func GetClassifyVisibleUserIdByClassifyId(classifyId int) (classifyVisibles []int, err error) {
  76. db := global.DmSQL["rddp"]
  77. err = db.Table(ClassifyVisible{}.TableName()).Where("classify_id =?", classifyId).Pluck("admin_id", &classifyVisibles).Error
  78. return
  79. }