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