diff --git a/http/controller/admin/addressBook.go b/http/controller/admin/addressBook.go index 23d801d..4983ecb 100644 --- a/http/controller/admin/addressBook.go +++ b/http/controller/admin/addressBook.go @@ -6,6 +6,7 @@ import ( "Gwen/http/response" "Gwen/model" "Gwen/service" + "encoding/json" _ "encoding/json" "github.com/gin-gonic/gin" "gorm.io/gorm" @@ -327,3 +328,70 @@ func (ct *AddressBook) ShareByWebClient(c *gin.Context) { "share_token": m.ShareToken, }) } + +func (ct *AddressBook) BatchCreateFromPeers(c *gin.Context) { + f := &admin.BatchCreateFromPeersForm{} + if err := c.ShouldBindJSON(f); err != nil { + response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error()) + return + } + u := service.AllService.UserService.CurUser(c) + + if f.CollectionId != 0 { + collection := service.AllService.AddressBookService.CollectionInfoById(f.CollectionId) + if collection.Id == 0 { + response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound")) + return + } + if collection.UserId != u.Id { + response.Fail(c, 101, response.TranslateMsg(c, "NoAccess")) + return + } + } + + peers := service.AllService.PeerService.List(1, 999, func(tx *gorm.DB) { + tx.Where("row_id in ?", f.PeerIds) + tx.Where("user_id = ?", u.Id) + }) + if peers.Total == 0 { + response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound")) + return + } + + tags, _ := json.Marshal(f.Tags) + for _, peer := range peers.Peers { + ab := service.AllService.AddressBookService.FromPeer(peer) + ab.Tags = tags + ab.CollectionId = f.CollectionId + ex := service.AllService.AddressBookService.InfoByUserIdAndIdAndCid(u.Id, ab.Id, ab.CollectionId) + if ex.RowId != 0 { + continue + } + service.AllService.AddressBookService.Create(ab) + } + response.Success(c, nil) +} + +func (ct *AddressBook) BatchUpdateTags(c *gin.Context) { + f := &admin.BatchUpdateTagsForm{} + if err := c.ShouldBindJSON(f); err != nil { + response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error()) + return + } + u := service.AllService.UserService.CurUser(c) + + abs := service.AllService.AddressBookService.List(1, 999, func(tx *gorm.DB) { + tx.Where("row_id in ?", f.RowIds) + tx.Where("user_id = ?", u.Id) + }) + if abs.Total == 0 { + response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound")) + return + } + err := service.AllService.AddressBookService.BatchUpdateTags(abs.AddressBooks, f.Tags) + if err != nil { + response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error()) + return + } + response.Success(c, nil) +} diff --git a/http/request/admin/addressBook.go b/http/request/admin/addressBook.go index 615a666..94452e5 100644 --- a/http/request/admin/addressBook.go +++ b/http/request/admin/addressBook.go @@ -122,3 +122,13 @@ type AddressBookCollectionRuleQuery struct { IsMy int `form:"is_my"` PageQuery } + +type BatchCreateFromPeersForm struct { + CollectionId uint `json:"collection_id"` + PeerIds []uint `json:"peer_ids"` + Tags []string `json:"tags"` +} +type BatchUpdateTagsForm struct { + RowIds []uint `json:"row_ids"` + Tags []string `json:"tags"` +} diff --git a/http/router/admin.go b/http/router/admin.go index 463a4e3..4977aa2 100644 --- a/http/router/admin.go +++ b/http/router/admin.go @@ -107,9 +107,12 @@ func AddressBookBind(rg *gin.RouterGroup) { aR.POST("/update", cont.Update) aR.POST("/delete", cont.Delete) aR.POST("/shareByWebClient", cont.ShareByWebClient) + aR.POST("/batchCreateFromPeers", cont.BatchCreateFromPeers) + aR.POST("/batchUpdateTags", cont.BatchUpdateTags) arp := aR.Use(middleware.AdminPrivilege()) arp.POST("/batchCreate", cont.BatchCreate) + } } func PeerBind(rg *gin.RouterGroup) { diff --git a/service/addressBook.go b/service/addressBook.go index 341e07e..ac06359 100644 --- a/service/addressBook.go +++ b/service/addressBook.go @@ -3,6 +3,7 @@ package service import ( "Gwen/global" "Gwen/model" + "encoding/json" "github.com/google/uuid" "gorm.io/gorm" "strings" @@ -116,6 +117,16 @@ func (s *AddressBookService) List(page, pageSize uint, where func(tx *gorm.DB)) return } +func (s *AddressBookService) FromPeer(peer *model.Peer) (a *model.AddressBook) { + a = &model.AddressBook{} + a.Id = peer.Id + a.Username = peer.Username + a.Hostname = peer.Hostname + a.UserId = peer.UserId + a.Platform = s.PlatformFromOs(peer.Os) + return a +} + // Create 创建 func (s *AddressBookService) Create(u *model.AddressBook) error { res := global.DB.Create(u).Error @@ -318,3 +329,12 @@ func (s *AddressBookService) CheckCollectionOwner(uid uint, cid uint) bool { p := s.CollectionInfoById(cid) return p.UserId == uid } + +func (s *AddressBookService) BatchUpdateTags(abs []*model.AddressBook, tags []string) error { + ids := make([]uint, 0) + for _, ab := range abs { + ids = append(ids, ab.RowId) + } + tagsv, _ := json.Marshal(tags) + return global.DB.Model(&model.AddressBook{}).Where("row_id in ?", ids).Update("tags", tagsv).Error +}