addressBook.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package my
  2. import (
  3. "Gwen/http/request/admin"
  4. "Gwen/http/response"
  5. "Gwen/service"
  6. "encoding/json"
  7. "github.com/gin-gonic/gin"
  8. "gorm.io/gorm"
  9. )
  10. type AddressBook struct{}
  11. func (ct *AddressBook) BatchCreateFromPeers(c *gin.Context) {
  12. f := &admin.BatchCreateFromPeersForm{}
  13. if err := c.ShouldBindJSON(f); err != nil {
  14. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  15. return
  16. }
  17. u := service.AllService.UserService.CurUser(c)
  18. if f.CollectionId != 0 {
  19. collection := service.AllService.AddressBookService.CollectionInfoById(f.CollectionId)
  20. if collection.Id == 0 {
  21. response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
  22. return
  23. }
  24. if collection.UserId != u.Id {
  25. response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
  26. return
  27. }
  28. }
  29. if len(f.PeerIds) == 0 {
  30. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
  31. return
  32. }
  33. pl := int64(len(f.PeerIds))
  34. peers := service.AllService.PeerService.List(1, uint(pl), func(tx *gorm.DB) {
  35. tx.Where("row_id in ?", f.PeerIds)
  36. tx.Where("user_id = ?", u.Id)
  37. })
  38. if peers.Total == 0 || pl != peers.Total {
  39. response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
  40. return
  41. }
  42. tags, _ := json.Marshal(f.Tags)
  43. for _, peer := range peers.Peers {
  44. ab := service.AllService.AddressBookService.FromPeer(peer)
  45. ab.Tags = tags
  46. ab.CollectionId = f.CollectionId
  47. ex := service.AllService.AddressBookService.InfoByUserIdAndIdAndCid(u.Id, ab.Id, ab.CollectionId)
  48. if ex.RowId != 0 {
  49. continue
  50. }
  51. service.AllService.AddressBookService.Create(ab)
  52. }
  53. response.Success(c, nil)
  54. }