Browse Source

切换分支前暂存

boweniac 7 months ago
parent
commit
57a1da538a

+ 15 - 0
desc/wechat/label_relationship.api

@@ -132,6 +132,17 @@ type (
         ContactId  *uint64 `json:"contactId,optional"`
     }
 
+    BatchLabelRelationshipsInfo {
+        // 更新类型:为1 时表示增加标签,为 -1 时表示移除标签
+        UpdateType int `json:"updateType,optional"`
+
+        // 标签 ID
+        LabelIds  []uint64 `json:"labelIds,optional"`
+
+        // 联系人 ID
+        ContactIds  []uint64 `json:"contactIds,optional"`
+    }
+
     // The response data of label relationship list | LabelRelationship列表数据
     LabelRelationshipListResp {
         BaseDataInfo
@@ -181,6 +192,10 @@ service Wechat {
     @handler updateLabelRelationships
     post /label_relationship/update_contact_labels (LabelRelationshipsInfo) returns (BaseMsgResp)
 
+    // Batch Update label relationships information | 批量更新联系人所有 LabelRelationship
+    @handler batchUpdateLabelRelationships
+    post /label_relationship/Batch_update_contact_labels (BatchLabelRelationshipsInfo) returns (BaseMsgResp)
+
     // Delete label relationship information | 删除LabelRelationship信息
     @handler deleteLabelRelationship
     post /label_relationship/delete (IDsReq) returns (BaseMsgResp)

+ 68 - 0
etc/wechat-docker.yaml

@@ -0,0 +1,68 @@
+Name: Wechat.api
+Host: 0.0.0.0
+Port: 19101
+Timeout: 30000
+
+Mode: "dev"
+
+Auth:
+  AccessSecret: jS6VKDtsJf3z1n2VKDtsJf3z1n2
+  AccessExpire: 259200
+
+CROSConf:
+  Address: '*'
+
+Log:
+  ServiceName: WechatApiLogger
+  Mode: console
+  Encoding: plain
+  Stat: false
+  Path: /tmp
+  Level: info
+  Compress: false
+  KeepDays: 7
+  StackCoolDownMillis: 100
+
+DatabaseConf:
+  Type: mysql
+  Host: mysql-server
+  Port: 3306
+  DBName: wechat
+  Username: root
+  Password: simple-admin.
+  MaxOpenConn: 100
+  SSLMode: disable
+  CacheTime: 5
+
+CoreRpc:
+  # Target: k8s://default/core-rpc-svc:9101
+  Endpoints:
+    - core-rpc:9101
+  Enabled: true
+
+RedisConf:
+  Host: redis-server:6379
+
+CasbinDatabaseConf:
+  Type: mysql
+  Host: mysql-server
+  Port: 3306
+  DBName: simple_admin
+  Username: root
+  Password: simple-admin.
+  MaxOpenConn: 100
+  SSLMode: disable
+  CacheTime: 5
+
+CasbinConf:
+  ModelText: |
+    [request_definition]
+    r = sub, obj, act
+    [policy_definition]
+    p = sub, obj, act
+    [role_definition]
+    g = _, _
+    [policy_effect]
+    e = some(where (p.eft == allow))
+    [matchers]
+    m = r.sub == p.sub && keyMatch2(r.obj,p.obj) && r.act == p.act

+ 6 - 6
etc/wechat.yaml

@@ -17,7 +17,7 @@ Log:
   Mode: console
   Encoding: plain
   Stat: false
-  Path: /tmp
+  Path: /Users/songbowen/Downloads
   Level: info
   Compress: false
   KeepDays: 7
@@ -25,7 +25,7 @@ Log:
 
 DatabaseConf:
   Type: mysql
-  Host: mysql-server
+  Host: localhost
   Port: 3306
   DBName: wechat
   Username: root
@@ -37,17 +37,17 @@ DatabaseConf:
 CoreRpc:
   # Target: k8s://default/core-rpc-svc:9101
   Endpoints:
-    - core-rpc:9101
+    - localhost:9101
   Enabled: true
 
 RedisConf:
-  Host: redis-server:6379
+  Host: localhost:6379
 
 CasbinDatabaseConf:
   Type: mysql
-  Host: mysql-server
+  Host: localhost
   Port: 3306
-  DBName: wechat_admin
+  DBName: simple_admin
   Username: root
   Password: simple-admin.
   MaxOpenConn: 100

+ 44 - 0
internal/handler/label_relationship/batch_update_label_relationships_handler.go

@@ -0,0 +1,44 @@
+package label_relationship
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/label_relationship"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /label_relationship/Batch_update_contact_labels label_relationship BatchUpdateLabelRelationships
+//
+// Batch Update label relationships information | 批量更新联系人所有 LabelRelationship
+//
+// Batch Update label relationships information | 批量更新联系人所有 LabelRelationship
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: BatchLabelRelationshipsInfo
+//
+// Responses:
+//  200: BaseMsgResp
+
+func BatchUpdateLabelRelationshipsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.BatchLabelRelationshipsInfo
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := label_relationship.NewBatchUpdateLabelRelationshipsLogic(r.Context(), svcCtx)
+		resp, err := l.BatchUpdateLabelRelationships(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 5 - 0
internal/handler/routes.go

@@ -243,6 +243,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 				},
 				{
 					Method:  http.MethodPost,
+					Path:    "/label_relationship/Batch_update_contact_labels",
+					Handler: label_relationship.BatchUpdateLabelRelationshipsHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
 					Path:    "/label_relationship/delete",
 					Handler: label_relationship.DeleteLabelRelationshipHandler(serverCtx),
 				},

+ 124 - 0
internal/logic/label_relationship/batch_update_label_relationships_logic.go

@@ -0,0 +1,124 @@
+package label_relationship
+
+import (
+	"context"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type BatchUpdateLabelRelationshipsLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewBatchUpdateLabelRelationshipsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BatchUpdateLabelRelationshipsLogic {
+	return &BatchUpdateLabelRelationshipsLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *BatchUpdateLabelRelationshipsLogic) BatchUpdateLabelRelationships(req *types.BatchLabelRelationshipsInfo) (resp *types.BaseMsgResp, err error) {
+	//organizationId := l.ctx.Value("organizationId").(uint64)
+	//// 开始事务
+	//tx, err := l.svcCtx.DB.Tx(context.Background())
+	//if err != nil {
+	//	return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	//}
+	//
+	//// 遍历所有联系人
+	//for _, contactId := range req.ContactIds {
+	//	// 获取联系人信息
+	//	c, err := tx.Contact.Query().Where(contact.ID(contactId), contact.OrganizationIDEQ(organizationId)).Only(l.ctx)
+	//	// 获取联系人当前已关联的标签
+	//	currentLabelRelationships, err := tx.LabelRelationship.Query().Where(labelrelationship.ContactID(contactId)).All(l.ctx)
+	//	if err != nil {
+	//		_ = tx.Rollback()
+	//		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	//	}
+	//
+	//	// 提取当前标签ID
+	//	var currentLabelIds []uint64
+	//	for _, relationship := range currentLabelRelationships {
+	//		currentLabelIds = append(currentLabelIds, relationship.LabelID)
+	//	}
+	//
+	//	// 对比新旧标签ID,找出需要新增和移除的标签
+	//	addLabelIds, removeLabelIds := compareLabelIds(req.LabelIds, currentLabelIds)
+	//
+	//	// 如果 req.UpdateType 为空,或 req.UpdateType 的值为 “all” 时
+	//	if req.UpdateType == nil || *req.UpdateType == "all" {
+	//		// 删除需要移除的标签关系
+	//		for _, id := range removeLabelIds {
+	//			_, err := tx.LabelRelationship.
+	//				Delete().
+	//				Where(
+	//					labelrelationship.ContactID(*req.ContactId),
+	//					labelrelationship.LabelID(id),
+	//				).
+	//				Exec(l.ctx)
+	//			if err != nil {
+	//				_ = tx.Rollback()
+	//				return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	//			}
+	//		}
+	//	}
+	//
+	//	// 创建需要新增的标签关系
+	//	for _, id := range addLabelIds {
+	//		_, _ = tx.LabelRelationship.Create().
+	//			SetLabelID(id).
+	//			SetContactID(*req.ContactId).
+	//			SetOrganizationID(organizationId).
+	//			Save(l.ctx)
+	//		//if err != nil {
+	//		//	return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	//		//}
+	//	}
+	//
+	//	// 获取所有 status 为 3 且 bot_wxid_list 包含 c.wx_wxid 的 sop_task
+	//	sopTasks, err := tx.SopTask.Query().Where(soptask.Status(3), soptask.OrganizationIDEQ(organizationId)).All(l.ctx)
+	//	if err != nil {
+	//		_ = tx.Rollback()
+	//		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	//	}
+	//
+	//	var filteredSopTasks []*ent.SopTask
+	//	for _, task := range sopTasks {
+	//		for _, botWxid := range task.BotWxidList {
+	//			if botWxid == c.WxWxid {
+	//				filteredSopTasks = append(filteredSopTasks, task)
+	//				break
+	//			}
+	//		}
+	//	}
+	//
+	//	// 获取所有 filteredSopTasks 的 sop_stages
+	//	var sopStages []*ent.SopStage
+	//	for _, task := range filteredSopTasks {
+	//		stages, err := task.QueryTaskStages().All(l.ctx)
+	//		if err != nil {
+	//			_ = tx.Rollback()
+	//			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	//		}
+	//		sopStages = append(sopStages, stages...)
+	//	}
+	//	err = l.AddLabelRelationships(sopStages, *c, req.LabelIds, organizationId)
+	//
+	//	if err != nil {
+	//		_ = tx.Rollback()
+	//		return nil, err
+	//	}
+	//	// 所有操作成功,提交事务
+	//	err = tx.Commit()
+	//	if err != nil {
+	//		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	//	}
+	//}
+	//
+	//return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
+	return nil, nil
+}

+ 10 - 0
internal/types/types.go

@@ -623,6 +623,16 @@ type LabelRelationshipsInfo struct {
 	ContactId *uint64 `json:"contactId,optional"`
 }
 
+// swagger:model BatchLabelRelationshipsInfo
+type BatchLabelRelationshipsInfo struct {
+	// 更新类型:为1 时表示增加标签,为 -1 时表示移除标签
+	UpdateType int `json:"updateType,optional"`
+	// 标签 ID
+	LabelIds []uint64 `json:"labelIds,optional"`
+	// 联系人 ID
+	ContactIds []uint64 `json:"contactIds,optional"`
+}
+
 // The response data of label relationship list | LabelRelationship列表数据
 // swagger:model LabelRelationshipListResp
 type LabelRelationshipListResp struct {