package models import ( "eta_gn/eta_api/global" "eta_gn/eta_api/utils" "time" ) type ClassifyVisible struct { ClassifyVisibleId int `gorm:"column:classify_visible_id;primary_key"` ClassifyId int `gorm:"column:classify_id"` AdminId int `gorm:"column:admin_id"` CreateTime time.Time `gorm:"column:create_time"` } func (ClassifyVisible) TableName() string { return "classify_visible" } func GetClassifyVisibleAll() (items []*ClassifyVisible, err error) { sql := "SELECT * FROM classify_visible" err = global.DmSQL["rddp"].Raw(sql).Find(&items).Error return } func GetNoVisibleClassifyIdByAdminId(adminId int) (classifyIds []int, err error) { sql := `SELECT classify_id FROM classify_visible GROUP BY classify_id HAVING SUM(CASE WHEN admin_id =? THEN 1 ELSE 0 END) = 0` err = global.DmSQL["rddp"].Raw(sql, adminId).Find(&classifyIds).Error return } func ExtendClassifyVisible(parentId, classifyId int, adminIds []int) (err error) { tx := global.DmSQL["rddp"].Begin() defer func() { if err != nil { tx.Rollback() return } tx.Commit() }() err = tx.Table(ClassifyVisible{}.TableName()).Where("classify_id =?", parentId).Delete(&ClassifyVisible{}).Error insertList := make([]ClassifyVisible, 0, len(adminIds)) for _, adminId := range adminIds { classifyVisible := ClassifyVisible{ ClassifyId: classifyId, AdminId: adminId, CreateTime: time.Now(), } insertList = append(insertList, classifyVisible) } err = tx.CreateInBatches(insertList, utils.MultiAddNum).Error return } func UpdateClassifyVisible(classifyId int, adminId []int) (err error) { tx := global.DmSQL["rddp"].Begin() defer func() { if err != nil { tx.Rollback() return } tx.Commit() }() err = tx.Table(ClassifyVisible{}.TableName()).Where("classify_id =?", classifyId).Delete(&ClassifyVisible{}).Error if err != nil { return } if len(adminId) > 0 { insertList := make([]ClassifyVisible, 0, len(adminId)) for _, admin := range adminId { classifyVisible := ClassifyVisible{ ClassifyId: classifyId, AdminId: admin, CreateTime: time.Now(), } insertList = append(insertList, classifyVisible) } err = tx.CreateInBatches(insertList, utils.MultiAddNum).Error } return } func GetClassifyVisibleUserIdByClassifyId(classifyId int) (classifyVisibles []int, err error) { db := global.DmSQL["rddp"] err = db.Table(ClassifyVisible{}.TableName()).Where("classify_id =?", classifyId).Pluck("admin_id", &classifyVisibles).Error return }