Răsfoiți Sursa

fix:edit whatsapp.list

jimmyyem 2 luni în urmă
părinte
comite
e610cbd90f

+ 2 - 0
desc/wechat/whatsapp.api

@@ -30,9 +30,11 @@ type (
 
         // 机构 ID 
         OrganizationId  *uint64 `json:"organizationId,optional"`
+		OrganizationName *string `json:"organizationName,optional"`
 
         // 模式ID 
         AgentId  *uint64 `json:"agentId,optional"`
+		AgentInfo *AgentInfo `json:"agentInfo,optional"`
 
         // 大模型服务地址 
         ApiBase  *string `json:"apiBase,optional"`

+ 17 - 1
ent/agent.go

@@ -51,9 +51,11 @@ type AgentEdges struct {
 	WxAgent []*Wx `json:"wx_agent,omitempty"`
 	// TokenAgent holds the value of the token_agent edge.
 	TokenAgent []*Token `json:"token_agent,omitempty"`
+	// WaAgent holds the value of the wa_agent edge.
+	WaAgent []*Whatsapp `json:"wa_agent,omitempty"`
 	// loadedTypes holds the information for reporting if a
 	// type was loaded (or requested) in eager-loading or not.
-	loadedTypes [2]bool
+	loadedTypes [3]bool
 }
 
 // WxAgentOrErr returns the WxAgent value or an error if the edge
@@ -74,6 +76,15 @@ func (e AgentEdges) TokenAgentOrErr() ([]*Token, error) {
 	return nil, &NotLoadedError{edge: "token_agent"}
 }
 
+// WaAgentOrErr returns the WaAgent value or an error if the edge
+// was not loaded in eager-loading.
+func (e AgentEdges) WaAgentOrErr() ([]*Whatsapp, error) {
+	if e.loadedTypes[2] {
+		return e.WaAgent, nil
+	}
+	return nil, &NotLoadedError{edge: "wa_agent"}
+}
+
 // scanValues returns the types for scanning values from sql.Rows.
 func (*Agent) scanValues(columns []string) ([]any, error) {
 	values := make([]any, len(columns))
@@ -195,6 +206,11 @@ func (a *Agent) QueryTokenAgent() *TokenQuery {
 	return NewAgentClient(a.config).QueryTokenAgent(a)
 }
 
+// QueryWaAgent queries the "wa_agent" edge of the Agent entity.
+func (a *Agent) QueryWaAgent() *WhatsappQuery {
+	return NewAgentClient(a.config).QueryWaAgent(a)
+}
+
 // Update returns a builder for updating this Agent.
 // Note that you need to call Agent.Unwrap() before calling this method if this Agent
 // was returned from a transaction, and the transaction was committed or rolled back.

+ 30 - 0
ent/agent/agent.go

@@ -41,6 +41,8 @@ const (
 	EdgeWxAgent = "wx_agent"
 	// EdgeTokenAgent holds the string denoting the token_agent edge name in mutations.
 	EdgeTokenAgent = "token_agent"
+	// EdgeWaAgent holds the string denoting the wa_agent edge name in mutations.
+	EdgeWaAgent = "wa_agent"
 	// Table holds the table name of the agent in the database.
 	Table = "agent"
 	// WxAgentTable is the table that holds the wx_agent relation/edge.
@@ -57,6 +59,13 @@ const (
 	TokenAgentInverseTable = "token"
 	// TokenAgentColumn is the table column denoting the token_agent relation/edge.
 	TokenAgentColumn = "agent_id"
+	// WaAgentTable is the table that holds the wa_agent relation/edge.
+	WaAgentTable = "whatsapp"
+	// WaAgentInverseTable is the table name for the Whatsapp entity.
+	// It exists in this package in order to avoid circular dependency with the "whatsapp" package.
+	WaAgentInverseTable = "whatsapp"
+	// WaAgentColumn is the table column denoting the wa_agent relation/edge.
+	WaAgentColumn = "agent_id"
 )
 
 // Columns holds all SQL columns for agent fields.
@@ -211,6 +220,20 @@ func ByTokenAgent(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption {
 		sqlgraph.OrderByNeighborTerms(s, newTokenAgentStep(), append([]sql.OrderTerm{term}, terms...)...)
 	}
 }
+
+// ByWaAgentCount orders the results by wa_agent count.
+func ByWaAgentCount(opts ...sql.OrderTermOption) OrderOption {
+	return func(s *sql.Selector) {
+		sqlgraph.OrderByNeighborsCount(s, newWaAgentStep(), opts...)
+	}
+}
+
+// ByWaAgent orders the results by wa_agent terms.
+func ByWaAgent(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption {
+	return func(s *sql.Selector) {
+		sqlgraph.OrderByNeighborTerms(s, newWaAgentStep(), append([]sql.OrderTerm{term}, terms...)...)
+	}
+}
 func newWxAgentStep() *sqlgraph.Step {
 	return sqlgraph.NewStep(
 		sqlgraph.From(Table, FieldID),
@@ -225,3 +248,10 @@ func newTokenAgentStep() *sqlgraph.Step {
 		sqlgraph.Edge(sqlgraph.O2M, false, TokenAgentTable, TokenAgentColumn),
 	)
 }
+func newWaAgentStep() *sqlgraph.Step {
+	return sqlgraph.NewStep(
+		sqlgraph.From(Table, FieldID),
+		sqlgraph.To(WaAgentInverseTable, FieldID),
+		sqlgraph.Edge(sqlgraph.O2M, false, WaAgentTable, WaAgentColumn),
+	)
+}

+ 23 - 0
ent/agent/where.go

@@ -786,6 +786,29 @@ func HasTokenAgentWith(preds ...predicate.Token) predicate.Agent {
 	})
 }
 
+// HasWaAgent applies the HasEdge predicate on the "wa_agent" edge.
+func HasWaAgent() predicate.Agent {
+	return predicate.Agent(func(s *sql.Selector) {
+		step := sqlgraph.NewStep(
+			sqlgraph.From(Table, FieldID),
+			sqlgraph.Edge(sqlgraph.O2M, false, WaAgentTable, WaAgentColumn),
+		)
+		sqlgraph.HasNeighbors(s, step)
+	})
+}
+
+// HasWaAgentWith applies the HasEdge predicate on the "wa_agent" edge with a given conditions (other predicates).
+func HasWaAgentWith(preds ...predicate.Whatsapp) predicate.Agent {
+	return predicate.Agent(func(s *sql.Selector) {
+		step := newWaAgentStep()
+		sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
+			for _, p := range preds {
+				p(s)
+			}
+		})
+	})
+}
+
 // And groups predicates with the AND operator between them.
 func And(predicates ...predicate.Agent) predicate.Agent {
 	return predicate.Agent(sql.AndPredicates(predicates...))

+ 32 - 0
ent/agent_create.go

@@ -9,6 +9,7 @@ import (
 	"time"
 	"wechat-api/ent/agent"
 	"wechat-api/ent/token"
+	"wechat-api/ent/whatsapp"
 	"wechat-api/ent/wx"
 
 	"entgo.io/ent/dialect/sql"
@@ -190,6 +191,21 @@ func (ac *AgentCreate) AddTokenAgent(t ...*Token) *AgentCreate {
 	return ac.AddTokenAgentIDs(ids...)
 }
 
+// AddWaAgentIDs adds the "wa_agent" edge to the Whatsapp entity by IDs.
+func (ac *AgentCreate) AddWaAgentIDs(ids ...uint64) *AgentCreate {
+	ac.mutation.AddWaAgentIDs(ids...)
+	return ac
+}
+
+// AddWaAgent adds the "wa_agent" edges to the Whatsapp entity.
+func (ac *AgentCreate) AddWaAgent(w ...*Whatsapp) *AgentCreate {
+	ids := make([]uint64, len(w))
+	for i := range w {
+		ids[i] = w[i].ID
+	}
+	return ac.AddWaAgentIDs(ids...)
+}
+
 // Mutation returns the AgentMutation object of the builder.
 func (ac *AgentCreate) Mutation() *AgentMutation {
 	return ac.mutation
@@ -421,6 +437,22 @@ func (ac *AgentCreate) createSpec() (*Agent, *sqlgraph.CreateSpec) {
 		}
 		_spec.Edges = append(_spec.Edges, edge)
 	}
+	if nodes := ac.mutation.WaAgentIDs(); len(nodes) > 0 {
+		edge := &sqlgraph.EdgeSpec{
+			Rel:     sqlgraph.O2M,
+			Inverse: false,
+			Table:   agent.WaAgentTable,
+			Columns: []string{agent.WaAgentColumn},
+			Bidi:    false,
+			Target: &sqlgraph.EdgeTarget{
+				IDSpec: sqlgraph.NewFieldSpec(whatsapp.FieldID, field.TypeUint64),
+			},
+		}
+		for _, k := range nodes {
+			edge.Target.Nodes = append(edge.Target.Nodes, k)
+		}
+		_spec.Edges = append(_spec.Edges, edge)
+	}
 	return _node, _spec
 }
 

+ 75 - 1
ent/agent_query.go

@@ -10,6 +10,7 @@ import (
 	"wechat-api/ent/agent"
 	"wechat-api/ent/predicate"
 	"wechat-api/ent/token"
+	"wechat-api/ent/whatsapp"
 	"wechat-api/ent/wx"
 
 	"entgo.io/ent/dialect/sql"
@@ -26,6 +27,7 @@ type AgentQuery struct {
 	predicates     []predicate.Agent
 	withWxAgent    *WxQuery
 	withTokenAgent *TokenQuery
+	withWaAgent    *WhatsappQuery
 	// intermediate query (i.e. traversal path).
 	sql  *sql.Selector
 	path func(context.Context) (*sql.Selector, error)
@@ -106,6 +108,28 @@ func (aq *AgentQuery) QueryTokenAgent() *TokenQuery {
 	return query
 }
 
+// QueryWaAgent chains the current query on the "wa_agent" edge.
+func (aq *AgentQuery) QueryWaAgent() *WhatsappQuery {
+	query := (&WhatsappClient{config: aq.config}).Query()
+	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
+		if err := aq.prepareQuery(ctx); err != nil {
+			return nil, err
+		}
+		selector := aq.sqlQuery(ctx)
+		if err := selector.Err(); err != nil {
+			return nil, err
+		}
+		step := sqlgraph.NewStep(
+			sqlgraph.From(agent.Table, agent.FieldID, selector),
+			sqlgraph.To(whatsapp.Table, whatsapp.FieldID),
+			sqlgraph.Edge(sqlgraph.O2M, false, agent.WaAgentTable, agent.WaAgentColumn),
+		)
+		fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step)
+		return fromU, nil
+	}
+	return query
+}
+
 // First returns the first Agent entity from the query.
 // Returns a *NotFoundError when no Agent was found.
 func (aq *AgentQuery) First(ctx context.Context) (*Agent, error) {
@@ -300,6 +324,7 @@ func (aq *AgentQuery) Clone() *AgentQuery {
 		predicates:     append([]predicate.Agent{}, aq.predicates...),
 		withWxAgent:    aq.withWxAgent.Clone(),
 		withTokenAgent: aq.withTokenAgent.Clone(),
+		withWaAgent:    aq.withWaAgent.Clone(),
 		// clone intermediate query.
 		sql:  aq.sql.Clone(),
 		path: aq.path,
@@ -328,6 +353,17 @@ func (aq *AgentQuery) WithTokenAgent(opts ...func(*TokenQuery)) *AgentQuery {
 	return aq
 }
 
+// WithWaAgent tells the query-builder to eager-load the nodes that are connected to
+// the "wa_agent" edge. The optional arguments are used to configure the query builder of the edge.
+func (aq *AgentQuery) WithWaAgent(opts ...func(*WhatsappQuery)) *AgentQuery {
+	query := (&WhatsappClient{config: aq.config}).Query()
+	for _, opt := range opts {
+		opt(query)
+	}
+	aq.withWaAgent = query
+	return aq
+}
+
 // GroupBy is used to group vertices by one or more fields/columns.
 // It is often used with aggregate functions, like: count, max, mean, min, sum.
 //
@@ -406,9 +442,10 @@ func (aq *AgentQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Agent,
 	var (
 		nodes       = []*Agent{}
 		_spec       = aq.querySpec()
-		loadedTypes = [2]bool{
+		loadedTypes = [3]bool{
 			aq.withWxAgent != nil,
 			aq.withTokenAgent != nil,
+			aq.withWaAgent != nil,
 		}
 	)
 	_spec.ScanValues = func(columns []string) ([]any, error) {
@@ -443,6 +480,13 @@ func (aq *AgentQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Agent,
 			return nil, err
 		}
 	}
+	if query := aq.withWaAgent; query != nil {
+		if err := aq.loadWaAgent(ctx, query, nodes,
+			func(n *Agent) { n.Edges.WaAgent = []*Whatsapp{} },
+			func(n *Agent, e *Whatsapp) { n.Edges.WaAgent = append(n.Edges.WaAgent, e) }); err != nil {
+			return nil, err
+		}
+	}
 	return nodes, nil
 }
 
@@ -507,6 +551,36 @@ func (aq *AgentQuery) loadTokenAgent(ctx context.Context, query *TokenQuery, nod
 	}
 	return nil
 }
+func (aq *AgentQuery) loadWaAgent(ctx context.Context, query *WhatsappQuery, nodes []*Agent, init func(*Agent), assign func(*Agent, *Whatsapp)) error {
+	fks := make([]driver.Value, 0, len(nodes))
+	nodeids := make(map[uint64]*Agent)
+	for i := range nodes {
+		fks = append(fks, nodes[i].ID)
+		nodeids[nodes[i].ID] = nodes[i]
+		if init != nil {
+			init(nodes[i])
+		}
+	}
+	if len(query.ctx.Fields) > 0 {
+		query.ctx.AppendFieldOnce(whatsapp.FieldAgentID)
+	}
+	query.Where(predicate.Whatsapp(func(s *sql.Selector) {
+		s.Where(sql.InValues(s.C(agent.WaAgentColumn), fks...))
+	}))
+	neighbors, err := query.All(ctx)
+	if err != nil {
+		return err
+	}
+	for _, n := range neighbors {
+		fk := n.AgentID
+		node, ok := nodeids[fk]
+		if !ok {
+			return fmt.Errorf(`unexpected referenced foreign-key "agent_id" returned %v for node %v`, fk, n.ID)
+		}
+		assign(node, n)
+	}
+	return nil
+}
 
 func (aq *AgentQuery) sqlCount(ctx context.Context) (int, error) {
 	_spec := aq.querySpec()

+ 163 - 0
ent/agent_update.go

@@ -10,6 +10,7 @@ import (
 	"wechat-api/ent/agent"
 	"wechat-api/ent/predicate"
 	"wechat-api/ent/token"
+	"wechat-api/ent/whatsapp"
 	"wechat-api/ent/wx"
 
 	"entgo.io/ent/dialect/sql"
@@ -230,6 +231,21 @@ func (au *AgentUpdate) AddTokenAgent(t ...*Token) *AgentUpdate {
 	return au.AddTokenAgentIDs(ids...)
 }
 
+// AddWaAgentIDs adds the "wa_agent" edge to the Whatsapp entity by IDs.
+func (au *AgentUpdate) AddWaAgentIDs(ids ...uint64) *AgentUpdate {
+	au.mutation.AddWaAgentIDs(ids...)
+	return au
+}
+
+// AddWaAgent adds the "wa_agent" edges to the Whatsapp entity.
+func (au *AgentUpdate) AddWaAgent(w ...*Whatsapp) *AgentUpdate {
+	ids := make([]uint64, len(w))
+	for i := range w {
+		ids[i] = w[i].ID
+	}
+	return au.AddWaAgentIDs(ids...)
+}
+
 // Mutation returns the AgentMutation object of the builder.
 func (au *AgentUpdate) Mutation() *AgentMutation {
 	return au.mutation
@@ -277,6 +293,27 @@ func (au *AgentUpdate) RemoveTokenAgent(t ...*Token) *AgentUpdate {
 	return au.RemoveTokenAgentIDs(ids...)
 }
 
+// ClearWaAgent clears all "wa_agent" edges to the Whatsapp entity.
+func (au *AgentUpdate) ClearWaAgent() *AgentUpdate {
+	au.mutation.ClearWaAgent()
+	return au
+}
+
+// RemoveWaAgentIDs removes the "wa_agent" edge to Whatsapp entities by IDs.
+func (au *AgentUpdate) RemoveWaAgentIDs(ids ...uint64) *AgentUpdate {
+	au.mutation.RemoveWaAgentIDs(ids...)
+	return au
+}
+
+// RemoveWaAgent removes "wa_agent" edges to Whatsapp entities.
+func (au *AgentUpdate) RemoveWaAgent(w ...*Whatsapp) *AgentUpdate {
+	ids := make([]uint64, len(w))
+	for i := range w {
+		ids[i] = w[i].ID
+	}
+	return au.RemoveWaAgentIDs(ids...)
+}
+
 // Save executes the query and returns the number of nodes affected by the update operation.
 func (au *AgentUpdate) Save(ctx context.Context) (int, error) {
 	if err := au.defaults(); err != nil {
@@ -499,6 +536,51 @@ func (au *AgentUpdate) sqlSave(ctx context.Context) (n int, err error) {
 		}
 		_spec.Edges.Add = append(_spec.Edges.Add, edge)
 	}
+	if au.mutation.WaAgentCleared() {
+		edge := &sqlgraph.EdgeSpec{
+			Rel:     sqlgraph.O2M,
+			Inverse: false,
+			Table:   agent.WaAgentTable,
+			Columns: []string{agent.WaAgentColumn},
+			Bidi:    false,
+			Target: &sqlgraph.EdgeTarget{
+				IDSpec: sqlgraph.NewFieldSpec(whatsapp.FieldID, field.TypeUint64),
+			},
+		}
+		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
+	}
+	if nodes := au.mutation.RemovedWaAgentIDs(); len(nodes) > 0 && !au.mutation.WaAgentCleared() {
+		edge := &sqlgraph.EdgeSpec{
+			Rel:     sqlgraph.O2M,
+			Inverse: false,
+			Table:   agent.WaAgentTable,
+			Columns: []string{agent.WaAgentColumn},
+			Bidi:    false,
+			Target: &sqlgraph.EdgeTarget{
+				IDSpec: sqlgraph.NewFieldSpec(whatsapp.FieldID, field.TypeUint64),
+			},
+		}
+		for _, k := range nodes {
+			edge.Target.Nodes = append(edge.Target.Nodes, k)
+		}
+		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
+	}
+	if nodes := au.mutation.WaAgentIDs(); len(nodes) > 0 {
+		edge := &sqlgraph.EdgeSpec{
+			Rel:     sqlgraph.O2M,
+			Inverse: false,
+			Table:   agent.WaAgentTable,
+			Columns: []string{agent.WaAgentColumn},
+			Bidi:    false,
+			Target: &sqlgraph.EdgeTarget{
+				IDSpec: sqlgraph.NewFieldSpec(whatsapp.FieldID, field.TypeUint64),
+			},
+		}
+		for _, k := range nodes {
+			edge.Target.Nodes = append(edge.Target.Nodes, k)
+		}
+		_spec.Edges.Add = append(_spec.Edges.Add, edge)
+	}
 	if n, err = sqlgraph.UpdateNodes(ctx, au.driver, _spec); err != nil {
 		if _, ok := err.(*sqlgraph.NotFoundError); ok {
 			err = &NotFoundError{agent.Label}
@@ -719,6 +801,21 @@ func (auo *AgentUpdateOne) AddTokenAgent(t ...*Token) *AgentUpdateOne {
 	return auo.AddTokenAgentIDs(ids...)
 }
 
+// AddWaAgentIDs adds the "wa_agent" edge to the Whatsapp entity by IDs.
+func (auo *AgentUpdateOne) AddWaAgentIDs(ids ...uint64) *AgentUpdateOne {
+	auo.mutation.AddWaAgentIDs(ids...)
+	return auo
+}
+
+// AddWaAgent adds the "wa_agent" edges to the Whatsapp entity.
+func (auo *AgentUpdateOne) AddWaAgent(w ...*Whatsapp) *AgentUpdateOne {
+	ids := make([]uint64, len(w))
+	for i := range w {
+		ids[i] = w[i].ID
+	}
+	return auo.AddWaAgentIDs(ids...)
+}
+
 // Mutation returns the AgentMutation object of the builder.
 func (auo *AgentUpdateOne) Mutation() *AgentMutation {
 	return auo.mutation
@@ -766,6 +863,27 @@ func (auo *AgentUpdateOne) RemoveTokenAgent(t ...*Token) *AgentUpdateOne {
 	return auo.RemoveTokenAgentIDs(ids...)
 }
 
+// ClearWaAgent clears all "wa_agent" edges to the Whatsapp entity.
+func (auo *AgentUpdateOne) ClearWaAgent() *AgentUpdateOne {
+	auo.mutation.ClearWaAgent()
+	return auo
+}
+
+// RemoveWaAgentIDs removes the "wa_agent" edge to Whatsapp entities by IDs.
+func (auo *AgentUpdateOne) RemoveWaAgentIDs(ids ...uint64) *AgentUpdateOne {
+	auo.mutation.RemoveWaAgentIDs(ids...)
+	return auo
+}
+
+// RemoveWaAgent removes "wa_agent" edges to Whatsapp entities.
+func (auo *AgentUpdateOne) RemoveWaAgent(w ...*Whatsapp) *AgentUpdateOne {
+	ids := make([]uint64, len(w))
+	for i := range w {
+		ids[i] = w[i].ID
+	}
+	return auo.RemoveWaAgentIDs(ids...)
+}
+
 // Where appends a list predicates to the AgentUpdate builder.
 func (auo *AgentUpdateOne) Where(ps ...predicate.Agent) *AgentUpdateOne {
 	auo.mutation.Where(ps...)
@@ -1018,6 +1136,51 @@ func (auo *AgentUpdateOne) sqlSave(ctx context.Context) (_node *Agent, err error
 		}
 		_spec.Edges.Add = append(_spec.Edges.Add, edge)
 	}
+	if auo.mutation.WaAgentCleared() {
+		edge := &sqlgraph.EdgeSpec{
+			Rel:     sqlgraph.O2M,
+			Inverse: false,
+			Table:   agent.WaAgentTable,
+			Columns: []string{agent.WaAgentColumn},
+			Bidi:    false,
+			Target: &sqlgraph.EdgeTarget{
+				IDSpec: sqlgraph.NewFieldSpec(whatsapp.FieldID, field.TypeUint64),
+			},
+		}
+		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
+	}
+	if nodes := auo.mutation.RemovedWaAgentIDs(); len(nodes) > 0 && !auo.mutation.WaAgentCleared() {
+		edge := &sqlgraph.EdgeSpec{
+			Rel:     sqlgraph.O2M,
+			Inverse: false,
+			Table:   agent.WaAgentTable,
+			Columns: []string{agent.WaAgentColumn},
+			Bidi:    false,
+			Target: &sqlgraph.EdgeTarget{
+				IDSpec: sqlgraph.NewFieldSpec(whatsapp.FieldID, field.TypeUint64),
+			},
+		}
+		for _, k := range nodes {
+			edge.Target.Nodes = append(edge.Target.Nodes, k)
+		}
+		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
+	}
+	if nodes := auo.mutation.WaAgentIDs(); len(nodes) > 0 {
+		edge := &sqlgraph.EdgeSpec{
+			Rel:     sqlgraph.O2M,
+			Inverse: false,
+			Table:   agent.WaAgentTable,
+			Columns: []string{agent.WaAgentColumn},
+			Bidi:    false,
+			Target: &sqlgraph.EdgeTarget{
+				IDSpec: sqlgraph.NewFieldSpec(whatsapp.FieldID, field.TypeUint64),
+			},
+		}
+		for _, k := range nodes {
+			edge.Target.Nodes = append(edge.Target.Nodes, k)
+		}
+		_spec.Edges.Add = append(_spec.Edges.Add, edge)
+	}
 	_node = &Agent{config: auo.config}
 	_spec.Assign = _node.assignValues
 	_spec.ScanValues = _node.scanValues

+ 32 - 0
ent/client.go

@@ -666,6 +666,22 @@ func (c *AgentClient) QueryTokenAgent(a *Agent) *TokenQuery {
 	return query
 }
 
+// QueryWaAgent queries the wa_agent edge of a Agent.
+func (c *AgentClient) QueryWaAgent(a *Agent) *WhatsappQuery {
+	query := (&WhatsappClient{config: c.config}).Query()
+	query.path = func(context.Context) (fromV *sql.Selector, _ error) {
+		id := a.ID
+		step := sqlgraph.NewStep(
+			sqlgraph.From(agent.Table, agent.FieldID, id),
+			sqlgraph.To(whatsapp.Table, whatsapp.FieldID),
+			sqlgraph.Edge(sqlgraph.O2M, false, agent.WaAgentTable, agent.WaAgentColumn),
+		)
+		fromV = sqlgraph.Neighbors(a.driver.Dialect(), step)
+		return fromV, nil
+	}
+	return query
+}
+
 // Hooks returns the client hooks.
 func (c *AgentClient) Hooks() []Hook {
 	hooks := c.hooks.Agent
@@ -5157,6 +5173,22 @@ func (c *WhatsappClient) GetX(ctx context.Context, id uint64) *Whatsapp {
 	return obj
 }
 
+// QueryAgent queries the agent edge of a Whatsapp.
+func (c *WhatsappClient) QueryAgent(w *Whatsapp) *AgentQuery {
+	query := (&AgentClient{config: c.config}).Query()
+	query.path = func(context.Context) (fromV *sql.Selector, _ error) {
+		id := w.ID
+		step := sqlgraph.NewStep(
+			sqlgraph.From(whatsapp.Table, whatsapp.FieldID, id),
+			sqlgraph.To(agent.Table, agent.FieldID),
+			sqlgraph.Edge(sqlgraph.M2O, true, whatsapp.AgentTable, whatsapp.AgentColumn),
+		)
+		fromV = sqlgraph.Neighbors(w.driver.Dialect(), step)
+		return fromV, nil
+	}
+	return query
+}
+
 // Hooks returns the client hooks.
 func (c *WhatsappClient) Hooks() []Hook {
 	hooks := c.hooks.Whatsapp

+ 10 - 1
ent/migrate/schema.go

@@ -1047,19 +1047,27 @@ var (
 		{Name: "nickname", Type: field.TypeString, Default: ""},
 		{Name: "phone", Type: field.TypeString, Default: ""},
 		{Name: "organization_id", Type: field.TypeUint64, Nullable: true, Default: 0},
-		{Name: "agent_id", Type: field.TypeUint64, Default: 0},
 		{Name: "api_base", Type: field.TypeString, Nullable: true, Default: ""},
 		{Name: "api_key", Type: field.TypeString, Nullable: true, Default: ""},
 		{Name: "allow_list", Type: field.TypeJSON, Nullable: true},
 		{Name: "group_allow_list", Type: field.TypeJSON, Nullable: true},
 		{Name: "block_list", Type: field.TypeJSON, Nullable: true},
 		{Name: "group_block_list", Type: field.TypeJSON, Nullable: true},
+		{Name: "agent_id", Type: field.TypeUint64, Default: 0},
 	}
 	// WhatsappTable holds the schema information for the "whatsapp" table.
 	WhatsappTable = &schema.Table{
 		Name:       "whatsapp",
 		Columns:    WhatsappColumns,
 		PrimaryKey: []*schema.Column{WhatsappColumns[0]},
+		ForeignKeys: []*schema.ForeignKey{
+			{
+				Symbol:     "whatsapp_agent_wa_agent",
+				Columns:    []*schema.Column{WhatsappColumns[18]},
+				RefColumns: []*schema.Column{AgentColumns[0]},
+				OnDelete:   schema.NoAction,
+			},
+		},
 		Indexes: []*schema.Index{
 			{
 				Name:    "whatsapp_ak_sk",
@@ -1476,6 +1484,7 @@ func init() {
 	UsageTotalTable.Annotation = &entsql.Annotation{
 		Table: "usage_total",
 	}
+	WhatsappTable.ForeignKeys[0].RefTable = AgentTable
 	WhatsappTable.Annotation = &entsql.Annotation{
 		Table: "whatsapp",
 	}

+ 148 - 44
ent/mutation.go

@@ -130,6 +130,9 @@ type AgentMutation struct {
 	token_agent        map[uint64]struct{}
 	removedtoken_agent map[uint64]struct{}
 	clearedtoken_agent bool
+	wa_agent           map[uint64]struct{}
+	removedwa_agent    map[uint64]struct{}
+	clearedwa_agent    bool
 	done               bool
 	oldValue           func(context.Context) (*Agent, error)
 	predicates         []predicate.Agent
@@ -836,6 +839,60 @@ func (m *AgentMutation) ResetTokenAgent() {
 	m.removedtoken_agent = nil
 }
 
+// AddWaAgentIDs adds the "wa_agent" edge to the Whatsapp entity by ids.
+func (m *AgentMutation) AddWaAgentIDs(ids ...uint64) {
+	if m.wa_agent == nil {
+		m.wa_agent = make(map[uint64]struct{})
+	}
+	for i := range ids {
+		m.wa_agent[ids[i]] = struct{}{}
+	}
+}
+
+// ClearWaAgent clears the "wa_agent" edge to the Whatsapp entity.
+func (m *AgentMutation) ClearWaAgent() {
+	m.clearedwa_agent = true
+}
+
+// WaAgentCleared reports if the "wa_agent" edge to the Whatsapp entity was cleared.
+func (m *AgentMutation) WaAgentCleared() bool {
+	return m.clearedwa_agent
+}
+
+// RemoveWaAgentIDs removes the "wa_agent" edge to the Whatsapp entity by IDs.
+func (m *AgentMutation) RemoveWaAgentIDs(ids ...uint64) {
+	if m.removedwa_agent == nil {
+		m.removedwa_agent = make(map[uint64]struct{})
+	}
+	for i := range ids {
+		delete(m.wa_agent, ids[i])
+		m.removedwa_agent[ids[i]] = struct{}{}
+	}
+}
+
+// RemovedWaAgent returns the removed IDs of the "wa_agent" edge to the Whatsapp entity.
+func (m *AgentMutation) RemovedWaAgentIDs() (ids []uint64) {
+	for id := range m.removedwa_agent {
+		ids = append(ids, id)
+	}
+	return
+}
+
+// WaAgentIDs returns the "wa_agent" edge IDs in the mutation.
+func (m *AgentMutation) WaAgentIDs() (ids []uint64) {
+	for id := range m.wa_agent {
+		ids = append(ids, id)
+	}
+	return
+}
+
+// ResetWaAgent resets all changes to the "wa_agent" edge.
+func (m *AgentMutation) ResetWaAgent() {
+	m.wa_agent = nil
+	m.clearedwa_agent = false
+	m.removedwa_agent = nil
+}
+
 // Where appends a list predicates to the AgentMutation builder.
 func (m *AgentMutation) Where(ps ...predicate.Agent) {
 	m.predicates = append(m.predicates, ps...)
@@ -1193,13 +1250,16 @@ func (m *AgentMutation) ResetField(name string) error {
 
 // AddedEdges returns all edge names that were set/added in this mutation.
 func (m *AgentMutation) AddedEdges() []string {
-	edges := make([]string, 0, 2)
+	edges := make([]string, 0, 3)
 	if m.wx_agent != nil {
 		edges = append(edges, agent.EdgeWxAgent)
 	}
 	if m.token_agent != nil {
 		edges = append(edges, agent.EdgeTokenAgent)
 	}
+	if m.wa_agent != nil {
+		edges = append(edges, agent.EdgeWaAgent)
+	}
 	return edges
 }
 
@@ -1219,19 +1279,28 @@ func (m *AgentMutation) AddedIDs(name string) []ent.Value {
 			ids = append(ids, id)
 		}
 		return ids
+	case agent.EdgeWaAgent:
+		ids := make([]ent.Value, 0, len(m.wa_agent))
+		for id := range m.wa_agent {
+			ids = append(ids, id)
+		}
+		return ids
 	}
 	return nil
 }
 
 // RemovedEdges returns all edge names that were removed in this mutation.
 func (m *AgentMutation) RemovedEdges() []string {
-	edges := make([]string, 0, 2)
+	edges := make([]string, 0, 3)
 	if m.removedwx_agent != nil {
 		edges = append(edges, agent.EdgeWxAgent)
 	}
 	if m.removedtoken_agent != nil {
 		edges = append(edges, agent.EdgeTokenAgent)
 	}
+	if m.removedwa_agent != nil {
+		edges = append(edges, agent.EdgeWaAgent)
+	}
 	return edges
 }
 
@@ -1251,19 +1320,28 @@ func (m *AgentMutation) RemovedIDs(name string) []ent.Value {
 			ids = append(ids, id)
 		}
 		return ids
+	case agent.EdgeWaAgent:
+		ids := make([]ent.Value, 0, len(m.removedwa_agent))
+		for id := range m.removedwa_agent {
+			ids = append(ids, id)
+		}
+		return ids
 	}
 	return nil
 }
 
 // ClearedEdges returns all edge names that were cleared in this mutation.
 func (m *AgentMutation) ClearedEdges() []string {
-	edges := make([]string, 0, 2)
+	edges := make([]string, 0, 3)
 	if m.clearedwx_agent {
 		edges = append(edges, agent.EdgeWxAgent)
 	}
 	if m.clearedtoken_agent {
 		edges = append(edges, agent.EdgeTokenAgent)
 	}
+	if m.clearedwa_agent {
+		edges = append(edges, agent.EdgeWaAgent)
+	}
 	return edges
 }
 
@@ -1275,6 +1353,8 @@ func (m *AgentMutation) EdgeCleared(name string) bool {
 		return m.clearedwx_agent
 	case agent.EdgeTokenAgent:
 		return m.clearedtoken_agent
+	case agent.EdgeWaAgent:
+		return m.clearedwa_agent
 	}
 	return false
 }
@@ -1297,6 +1377,9 @@ func (m *AgentMutation) ResetEdge(name string) error {
 	case agent.EdgeTokenAgent:
 		m.ResetTokenAgent()
 		return nil
+	case agent.EdgeWaAgent:
+		m.ResetWaAgent()
+		return nil
 	}
 	return fmt.Errorf("unknown Agent edge %s", name)
 }
@@ -36863,8 +36946,6 @@ type WhatsappMutation struct {
 	phone                  *string
 	organization_id        *uint64
 	addorganization_id     *int64
-	agent_id               *uint64
-	addagent_id            *int64
 	api_base               *string
 	api_key                *string
 	allow_list             *[]string
@@ -36876,6 +36957,8 @@ type WhatsappMutation struct {
 	group_block_list       *[]string
 	appendgroup_block_list []string
 	clearedFields          map[string]struct{}
+	agent                  *uint64
+	clearedagent           bool
 	done                   bool
 	oldValue               func(context.Context) (*Whatsapp, error)
 	predicates             []predicate.Whatsapp
@@ -37477,13 +37560,12 @@ func (m *WhatsappMutation) ResetOrganizationID() {
 
 // SetAgentID sets the "agent_id" field.
 func (m *WhatsappMutation) SetAgentID(u uint64) {
-	m.agent_id = &u
-	m.addagent_id = nil
+	m.agent = &u
 }
 
 // AgentID returns the value of the "agent_id" field in the mutation.
 func (m *WhatsappMutation) AgentID() (r uint64, exists bool) {
-	v := m.agent_id
+	v := m.agent
 	if v == nil {
 		return
 	}
@@ -37507,28 +37589,9 @@ func (m *WhatsappMutation) OldAgentID(ctx context.Context) (v uint64, err error)
 	return oldValue.AgentID, nil
 }
 
-// AddAgentID adds u to the "agent_id" field.
-func (m *WhatsappMutation) AddAgentID(u int64) {
-	if m.addagent_id != nil {
-		*m.addagent_id += u
-	} else {
-		m.addagent_id = &u
-	}
-}
-
-// AddedAgentID returns the value that was added to the "agent_id" field in this mutation.
-func (m *WhatsappMutation) AddedAgentID() (r int64, exists bool) {
-	v := m.addagent_id
-	if v == nil {
-		return
-	}
-	return *v, true
-}
-
 // ResetAgentID resets all changes to the "agent_id" field.
 func (m *WhatsappMutation) ResetAgentID() {
-	m.agent_id = nil
-	m.addagent_id = nil
+	m.agent = nil
 }
 
 // SetAPIBase sets the "api_base" field.
@@ -37889,6 +37952,33 @@ func (m *WhatsappMutation) ResetGroupBlockList() {
 	delete(m.clearedFields, whatsapp.FieldGroupBlockList)
 }
 
+// ClearAgent clears the "agent" edge to the Agent entity.
+func (m *WhatsappMutation) ClearAgent() {
+	m.clearedagent = true
+	m.clearedFields[whatsapp.FieldAgentID] = struct{}{}
+}
+
+// AgentCleared reports if the "agent" edge to the Agent entity was cleared.
+func (m *WhatsappMutation) AgentCleared() bool {
+	return m.clearedagent
+}
+
+// AgentIDs returns the "agent" edge IDs in the mutation.
+// Note that IDs always returns len(IDs) <= 1 for unique edges, and you should use
+// AgentID instead. It exists only for internal usage by the builders.
+func (m *WhatsappMutation) AgentIDs() (ids []uint64) {
+	if id := m.agent; id != nil {
+		ids = append(ids, *id)
+	}
+	return
+}
+
+// ResetAgent resets all changes to the "agent" edge.
+func (m *WhatsappMutation) ResetAgent() {
+	m.agent = nil
+	m.clearedagent = false
+}
+
 // Where appends a list predicates to the WhatsappMutation builder.
 func (m *WhatsappMutation) Where(ps ...predicate.Whatsapp) {
 	m.predicates = append(m.predicates, ps...)
@@ -37957,7 +38047,7 @@ func (m *WhatsappMutation) Fields() []string {
 	if m.organization_id != nil {
 		fields = append(fields, whatsapp.FieldOrganizationID)
 	}
-	if m.agent_id != nil {
+	if m.agent != nil {
 		fields = append(fields, whatsapp.FieldAgentID)
 	}
 	if m.api_base != nil {
@@ -38216,9 +38306,6 @@ func (m *WhatsappMutation) AddedFields() []string {
 	if m.addorganization_id != nil {
 		fields = append(fields, whatsapp.FieldOrganizationID)
 	}
-	if m.addagent_id != nil {
-		fields = append(fields, whatsapp.FieldAgentID)
-	}
 	return fields
 }
 
@@ -38231,8 +38318,6 @@ func (m *WhatsappMutation) AddedField(name string) (ent.Value, bool) {
 		return m.AddedStatus()
 	case whatsapp.FieldOrganizationID:
 		return m.AddedOrganizationID()
-	case whatsapp.FieldAgentID:
-		return m.AddedAgentID()
 	}
 	return nil, false
 }
@@ -38256,13 +38341,6 @@ func (m *WhatsappMutation) AddField(name string, value ent.Value) error {
 		}
 		m.AddOrganizationID(v)
 		return nil
-	case whatsapp.FieldAgentID:
-		v, ok := value.(int64)
-		if !ok {
-			return fmt.Errorf("unexpected type %T for field %s", value, name)
-		}
-		m.AddAgentID(v)
-		return nil
 	}
 	return fmt.Errorf("unknown Whatsapp numeric field %s", name)
 }
@@ -38413,19 +38491,28 @@ func (m *WhatsappMutation) ResetField(name string) error {
 
 // AddedEdges returns all edge names that were set/added in this mutation.
 func (m *WhatsappMutation) AddedEdges() []string {
-	edges := make([]string, 0, 0)
+	edges := make([]string, 0, 1)
+	if m.agent != nil {
+		edges = append(edges, whatsapp.EdgeAgent)
+	}
 	return edges
 }
 
 // AddedIDs returns all IDs (to other nodes) that were added for the given edge
 // name in this mutation.
 func (m *WhatsappMutation) AddedIDs(name string) []ent.Value {
+	switch name {
+	case whatsapp.EdgeAgent:
+		if id := m.agent; id != nil {
+			return []ent.Value{*id}
+		}
+	}
 	return nil
 }
 
 // RemovedEdges returns all edge names that were removed in this mutation.
 func (m *WhatsappMutation) RemovedEdges() []string {
-	edges := make([]string, 0, 0)
+	edges := make([]string, 0, 1)
 	return edges
 }
 
@@ -38437,25 +38524,42 @@ func (m *WhatsappMutation) RemovedIDs(name string) []ent.Value {
 
 // ClearedEdges returns all edge names that were cleared in this mutation.
 func (m *WhatsappMutation) ClearedEdges() []string {
-	edges := make([]string, 0, 0)
+	edges := make([]string, 0, 1)
+	if m.clearedagent {
+		edges = append(edges, whatsapp.EdgeAgent)
+	}
 	return edges
 }
 
 // EdgeCleared returns a boolean which indicates if the edge with the given name
 // was cleared in this mutation.
 func (m *WhatsappMutation) EdgeCleared(name string) bool {
+	switch name {
+	case whatsapp.EdgeAgent:
+		return m.clearedagent
+	}
 	return false
 }
 
 // ClearEdge clears the value of the edge with the given name. It returns an error
 // if that edge is not defined in the schema.
 func (m *WhatsappMutation) ClearEdge(name string) error {
+	switch name {
+	case whatsapp.EdgeAgent:
+		m.ClearAgent()
+		return nil
+	}
 	return fmt.Errorf("unknown Whatsapp unique edge %s", name)
 }
 
 // ResetEdge resets all changes to the edge with the given name in this mutation.
 // It returns an error if the edge is not defined in the schema.
 func (m *WhatsappMutation) ResetEdge(name string) error {
+	switch name {
+	case whatsapp.EdgeAgent:
+		m.ResetAgent()
+		return nil
+	}
 	return fmt.Errorf("unknown Whatsapp edge %s", name)
 }
 

+ 1 - 0
ent/schema/agent.go

@@ -40,6 +40,7 @@ func (Agent) Edges() []ent.Edge {
 	return []ent.Edge{
 		edge.To("wx_agent", Wx.Type),
 		edge.To("token_agent", Token.Type),
+		edge.To("wa_agent", Whatsapp.Type),
 	}
 }
 

+ 8 - 1
ent/schema/whatsapp.go

@@ -1,6 +1,7 @@
 package schema
 
 import (
+	"entgo.io/ent/schema/edge"
 	"wechat-api/ent/schema/localmixin"
 
 	"entgo.io/ent"
@@ -49,7 +50,13 @@ func (Whatsapp) Indexes() []ent.Index {
 }
 
 func (Whatsapp) Edges() []ent.Edge {
-	return nil
+	return []ent.Edge{
+		edge.From("agent", Agent.Type).
+			Ref("wa_agent").
+			Unique().
+			Field("agent_id").
+			Required(),
+	}
 }
 
 func (Whatsapp) Annotations() []schema.Annotation {

+ 30 - 1
ent/whatsapp.go

@@ -7,6 +7,7 @@ import (
 	"fmt"
 	"strings"
 	"time"
+	"wechat-api/ent/agent"
 	"wechat-api/ent/whatsapp"
 
 	"entgo.io/ent"
@@ -54,7 +55,30 @@ type Whatsapp struct {
 	BlockList []string `json:"block_list,omitempty"`
 	// 群黑名单
 	GroupBlockList []string `json:"group_block_list,omitempty"`
-	selectValues   sql.SelectValues
+	// Edges holds the relations/edges for other nodes in the graph.
+	// The values are being populated by the WhatsappQuery when eager-loading is set.
+	Edges        WhatsappEdges `json:"edges"`
+	selectValues sql.SelectValues
+}
+
+// WhatsappEdges holds the relations/edges for other nodes in the graph.
+type WhatsappEdges struct {
+	// Agent holds the value of the agent edge.
+	Agent *Agent `json:"agent,omitempty"`
+	// loadedTypes holds the information for reporting if a
+	// type was loaded (or requested) in eager-loading or not.
+	loadedTypes [1]bool
+}
+
+// AgentOrErr returns the Agent value or an error if the edge
+// was not loaded in eager-loading, or loaded but was not found.
+func (e WhatsappEdges) AgentOrErr() (*Agent, error) {
+	if e.Agent != nil {
+		return e.Agent, nil
+	} else if e.loadedTypes[0] {
+		return nil, &NotFoundError{label: agent.Label}
+	}
+	return nil, &NotLoadedError{edge: "agent"}
 }
 
 // scanValues returns the types for scanning values from sql.Rows.
@@ -220,6 +244,11 @@ func (w *Whatsapp) Value(name string) (ent.Value, error) {
 	return w.selectValues.Get(name)
 }
 
+// QueryAgent queries the "agent" edge of the Whatsapp entity.
+func (w *Whatsapp) QueryAgent() *AgentQuery {
+	return NewWhatsappClient(w.config).QueryAgent(w)
+}
+
 // Update returns a builder for updating this Whatsapp.
 // Note that you need to call Whatsapp.Unwrap() before calling this method if this Whatsapp
 // was returned from a transaction, and the transaction was committed or rolled back.

+ 24 - 0
ent/whatsapp/whatsapp.go

@@ -7,6 +7,7 @@ import (
 
 	"entgo.io/ent"
 	"entgo.io/ent/dialect/sql"
+	"entgo.io/ent/dialect/sql/sqlgraph"
 )
 
 const (
@@ -50,8 +51,17 @@ const (
 	FieldBlockList = "block_list"
 	// FieldGroupBlockList holds the string denoting the group_block_list field in the database.
 	FieldGroupBlockList = "group_block_list"
+	// EdgeAgent holds the string denoting the agent edge name in mutations.
+	EdgeAgent = "agent"
 	// Table holds the table name of the whatsapp in the database.
 	Table = "whatsapp"
+	// AgentTable is the table that holds the agent relation/edge.
+	AgentTable = "whatsapp"
+	// AgentInverseTable is the table name for the Agent entity.
+	// It exists in this package in order to avoid circular dependency with the "agent" package.
+	AgentInverseTable = "agent"
+	// AgentColumn is the table column denoting the agent relation/edge.
+	AgentColumn = "agent_id"
 )
 
 // Columns holds all SQL columns for whatsapp fields.
@@ -202,3 +212,17 @@ func ByAPIBase(opts ...sql.OrderTermOption) OrderOption {
 func ByAPIKey(opts ...sql.OrderTermOption) OrderOption {
 	return sql.OrderByField(FieldAPIKey, opts...).ToFunc()
 }
+
+// ByAgentField orders the results by agent field.
+func ByAgentField(field string, opts ...sql.OrderTermOption) OrderOption {
+	return func(s *sql.Selector) {
+		sqlgraph.OrderByNeighborTerms(s, newAgentStep(), sql.OrderByField(field, opts...))
+	}
+}
+func newAgentStep() *sqlgraph.Step {
+	return sqlgraph.NewStep(
+		sqlgraph.From(Table, FieldID),
+		sqlgraph.To(AgentInverseTable, FieldID),
+		sqlgraph.Edge(sqlgraph.M2O, true, AgentTable, AgentColumn),
+	)
+}

+ 24 - 20
ent/whatsapp/where.go

@@ -7,6 +7,7 @@ import (
 	"wechat-api/ent/predicate"
 
 	"entgo.io/ent/dialect/sql"
+	"entgo.io/ent/dialect/sql/sqlgraph"
 )
 
 // ID filters vertices based on their ID field.
@@ -774,26 +775,6 @@ func AgentIDNotIn(vs ...uint64) predicate.Whatsapp {
 	return predicate.Whatsapp(sql.FieldNotIn(FieldAgentID, vs...))
 }
 
-// AgentIDGT applies the GT predicate on the "agent_id" field.
-func AgentIDGT(v uint64) predicate.Whatsapp {
-	return predicate.Whatsapp(sql.FieldGT(FieldAgentID, v))
-}
-
-// AgentIDGTE applies the GTE predicate on the "agent_id" field.
-func AgentIDGTE(v uint64) predicate.Whatsapp {
-	return predicate.Whatsapp(sql.FieldGTE(FieldAgentID, v))
-}
-
-// AgentIDLT applies the LT predicate on the "agent_id" field.
-func AgentIDLT(v uint64) predicate.Whatsapp {
-	return predicate.Whatsapp(sql.FieldLT(FieldAgentID, v))
-}
-
-// AgentIDLTE applies the LTE predicate on the "agent_id" field.
-func AgentIDLTE(v uint64) predicate.Whatsapp {
-	return predicate.Whatsapp(sql.FieldLTE(FieldAgentID, v))
-}
-
 // APIBaseEQ applies the EQ predicate on the "api_base" field.
 func APIBaseEQ(v string) predicate.Whatsapp {
 	return predicate.Whatsapp(sql.FieldEQ(FieldAPIBase, v))
@@ -984,6 +965,29 @@ func GroupBlockListNotNil() predicate.Whatsapp {
 	return predicate.Whatsapp(sql.FieldNotNull(FieldGroupBlockList))
 }
 
+// HasAgent applies the HasEdge predicate on the "agent" edge.
+func HasAgent() predicate.Whatsapp {
+	return predicate.Whatsapp(func(s *sql.Selector) {
+		step := sqlgraph.NewStep(
+			sqlgraph.From(Table, FieldID),
+			sqlgraph.Edge(sqlgraph.M2O, true, AgentTable, AgentColumn),
+		)
+		sqlgraph.HasNeighbors(s, step)
+	})
+}
+
+// HasAgentWith applies the HasEdge predicate on the "agent" edge with a given conditions (other predicates).
+func HasAgentWith(preds ...predicate.Agent) predicate.Whatsapp {
+	return predicate.Whatsapp(func(s *sql.Selector) {
+		step := newAgentStep()
+		sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
+			for _, p := range preds {
+				p(s)
+			}
+		})
+	})
+}
+
 // And groups predicates with the AND operator between them.
 func And(predicates ...predicate.Whatsapp) predicate.Whatsapp {
 	return predicate.Whatsapp(sql.AndPredicates(predicates...))

+ 26 - 24
ent/whatsapp_create.go

@@ -7,6 +7,7 @@ import (
 	"errors"
 	"fmt"
 	"time"
+	"wechat-api/ent/agent"
 	"wechat-api/ent/whatsapp"
 
 	"entgo.io/ent/dialect/sql"
@@ -248,6 +249,11 @@ func (wc *WhatsappCreate) SetID(u uint64) *WhatsappCreate {
 	return wc
 }
 
+// SetAgent sets the "agent" edge to the Agent entity.
+func (wc *WhatsappCreate) SetAgent(a *Agent) *WhatsappCreate {
+	return wc.SetAgentID(a.ID)
+}
+
 // Mutation returns the WhatsappMutation object of the builder.
 func (wc *WhatsappCreate) Mutation() *WhatsappMutation {
 	return wc.mutation
@@ -372,6 +378,9 @@ func (wc *WhatsappCreate) check() error {
 	if _, ok := wc.mutation.AgentID(); !ok {
 		return &ValidationError{Name: "agent_id", err: errors.New(`ent: missing required field "Whatsapp.agent_id"`)}
 	}
+	if _, ok := wc.mutation.AgentID(); !ok {
+		return &ValidationError{Name: "agent", err: errors.New(`ent: missing required edge "Whatsapp.agent"`)}
+	}
 	return nil
 }
 
@@ -449,10 +458,6 @@ func (wc *WhatsappCreate) createSpec() (*Whatsapp, *sqlgraph.CreateSpec) {
 		_spec.SetField(whatsapp.FieldOrganizationID, field.TypeUint64, value)
 		_node.OrganizationID = value
 	}
-	if value, ok := wc.mutation.AgentID(); ok {
-		_spec.SetField(whatsapp.FieldAgentID, field.TypeUint64, value)
-		_node.AgentID = value
-	}
 	if value, ok := wc.mutation.APIBase(); ok {
 		_spec.SetField(whatsapp.FieldAPIBase, field.TypeString, value)
 		_node.APIBase = value
@@ -477,6 +482,23 @@ func (wc *WhatsappCreate) createSpec() (*Whatsapp, *sqlgraph.CreateSpec) {
 		_spec.SetField(whatsapp.FieldGroupBlockList, field.TypeJSON, value)
 		_node.GroupBlockList = value
 	}
+	if nodes := wc.mutation.AgentIDs(); len(nodes) > 0 {
+		edge := &sqlgraph.EdgeSpec{
+			Rel:     sqlgraph.M2O,
+			Inverse: true,
+			Table:   whatsapp.AgentTable,
+			Columns: []string{whatsapp.AgentColumn},
+			Bidi:    false,
+			Target: &sqlgraph.EdgeTarget{
+				IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
+			},
+		}
+		for _, k := range nodes {
+			edge.Target.Nodes = append(edge.Target.Nodes, k)
+		}
+		_node.AgentID = nodes[0]
+		_spec.Edges = append(_spec.Edges, edge)
+	}
 	return _node, _spec
 }
 
@@ -697,12 +719,6 @@ func (u *WhatsappUpsert) UpdateAgentID() *WhatsappUpsert {
 	return u
 }
 
-// AddAgentID adds v to the "agent_id" field.
-func (u *WhatsappUpsert) AddAgentID(v uint64) *WhatsappUpsert {
-	u.Add(whatsapp.FieldAgentID, v)
-	return u
-}
-
 // SetAPIBase sets the "api_base" field.
 func (u *WhatsappUpsert) SetAPIBase(v string) *WhatsappUpsert {
 	u.Set(whatsapp.FieldAPIBase, v)
@@ -1051,13 +1067,6 @@ func (u *WhatsappUpsertOne) SetAgentID(v uint64) *WhatsappUpsertOne {
 	})
 }
 
-// AddAgentID adds v to the "agent_id" field.
-func (u *WhatsappUpsertOne) AddAgentID(v uint64) *WhatsappUpsertOne {
-	return u.Update(func(s *WhatsappUpsert) {
-		s.AddAgentID(v)
-	})
-}
-
 // UpdateAgentID sets the "agent_id" field to the value that was provided on create.
 func (u *WhatsappUpsertOne) UpdateAgentID() *WhatsappUpsertOne {
 	return u.Update(func(s *WhatsappUpsert) {
@@ -1597,13 +1606,6 @@ func (u *WhatsappUpsertBulk) SetAgentID(v uint64) *WhatsappUpsertBulk {
 	})
 }
 
-// AddAgentID adds v to the "agent_id" field.
-func (u *WhatsappUpsertBulk) AddAgentID(v uint64) *WhatsappUpsertBulk {
-	return u.Update(func(s *WhatsappUpsert) {
-		s.AddAgentID(v)
-	})
-}
-
 // UpdateAgentID sets the "agent_id" field to the value that was provided on create.
 func (u *WhatsappUpsertBulk) UpdateAgentID() *WhatsappUpsertBulk {
 	return u.Update(func(s *WhatsappUpsert) {

+ 81 - 2
ent/whatsapp_query.go

@@ -6,6 +6,7 @@ import (
 	"context"
 	"fmt"
 	"math"
+	"wechat-api/ent/agent"
 	"wechat-api/ent/predicate"
 	"wechat-api/ent/whatsapp"
 
@@ -21,6 +22,7 @@ type WhatsappQuery struct {
 	order      []whatsapp.OrderOption
 	inters     []Interceptor
 	predicates []predicate.Whatsapp
+	withAgent  *AgentQuery
 	// intermediate query (i.e. traversal path).
 	sql  *sql.Selector
 	path func(context.Context) (*sql.Selector, error)
@@ -57,6 +59,28 @@ func (wq *WhatsappQuery) Order(o ...whatsapp.OrderOption) *WhatsappQuery {
 	return wq
 }
 
+// QueryAgent chains the current query on the "agent" edge.
+func (wq *WhatsappQuery) QueryAgent() *AgentQuery {
+	query := (&AgentClient{config: wq.config}).Query()
+	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
+		if err := wq.prepareQuery(ctx); err != nil {
+			return nil, err
+		}
+		selector := wq.sqlQuery(ctx)
+		if err := selector.Err(); err != nil {
+			return nil, err
+		}
+		step := sqlgraph.NewStep(
+			sqlgraph.From(whatsapp.Table, whatsapp.FieldID, selector),
+			sqlgraph.To(agent.Table, agent.FieldID),
+			sqlgraph.Edge(sqlgraph.M2O, true, whatsapp.AgentTable, whatsapp.AgentColumn),
+		)
+		fromU = sqlgraph.SetNeighbors(wq.driver.Dialect(), step)
+		return fromU, nil
+	}
+	return query
+}
+
 // First returns the first Whatsapp entity from the query.
 // Returns a *NotFoundError when no Whatsapp was found.
 func (wq *WhatsappQuery) First(ctx context.Context) (*Whatsapp, error) {
@@ -249,12 +273,24 @@ func (wq *WhatsappQuery) Clone() *WhatsappQuery {
 		order:      append([]whatsapp.OrderOption{}, wq.order...),
 		inters:     append([]Interceptor{}, wq.inters...),
 		predicates: append([]predicate.Whatsapp{}, wq.predicates...),
+		withAgent:  wq.withAgent.Clone(),
 		// clone intermediate query.
 		sql:  wq.sql.Clone(),
 		path: wq.path,
 	}
 }
 
+// WithAgent tells the query-builder to eager-load the nodes that are connected to
+// the "agent" edge. The optional arguments are used to configure the query builder of the edge.
+func (wq *WhatsappQuery) WithAgent(opts ...func(*AgentQuery)) *WhatsappQuery {
+	query := (&AgentClient{config: wq.config}).Query()
+	for _, opt := range opts {
+		opt(query)
+	}
+	wq.withAgent = query
+	return wq
+}
+
 // GroupBy is used to group vertices by one or more fields/columns.
 // It is often used with aggregate functions, like: count, max, mean, min, sum.
 //
@@ -331,8 +367,11 @@ func (wq *WhatsappQuery) prepareQuery(ctx context.Context) error {
 
 func (wq *WhatsappQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Whatsapp, error) {
 	var (
-		nodes = []*Whatsapp{}
-		_spec = wq.querySpec()
+		nodes       = []*Whatsapp{}
+		_spec       = wq.querySpec()
+		loadedTypes = [1]bool{
+			wq.withAgent != nil,
+		}
 	)
 	_spec.ScanValues = func(columns []string) ([]any, error) {
 		return (*Whatsapp).scanValues(nil, columns)
@@ -340,6 +379,7 @@ func (wq *WhatsappQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Wha
 	_spec.Assign = func(columns []string, values []any) error {
 		node := &Whatsapp{config: wq.config}
 		nodes = append(nodes, node)
+		node.Edges.loadedTypes = loadedTypes
 		return node.assignValues(columns, values)
 	}
 	for i := range hooks {
@@ -351,9 +391,45 @@ func (wq *WhatsappQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Wha
 	if len(nodes) == 0 {
 		return nodes, nil
 	}
+	if query := wq.withAgent; query != nil {
+		if err := wq.loadAgent(ctx, query, nodes, nil,
+			func(n *Whatsapp, e *Agent) { n.Edges.Agent = e }); err != nil {
+			return nil, err
+		}
+	}
 	return nodes, nil
 }
 
+func (wq *WhatsappQuery) loadAgent(ctx context.Context, query *AgentQuery, nodes []*Whatsapp, init func(*Whatsapp), assign func(*Whatsapp, *Agent)) error {
+	ids := make([]uint64, 0, len(nodes))
+	nodeids := make(map[uint64][]*Whatsapp)
+	for i := range nodes {
+		fk := nodes[i].AgentID
+		if _, ok := nodeids[fk]; !ok {
+			ids = append(ids, fk)
+		}
+		nodeids[fk] = append(nodeids[fk], nodes[i])
+	}
+	if len(ids) == 0 {
+		return nil
+	}
+	query.Where(agent.IDIn(ids...))
+	neighbors, err := query.All(ctx)
+	if err != nil {
+		return err
+	}
+	for _, n := range neighbors {
+		nodes, ok := nodeids[n.ID]
+		if !ok {
+			return fmt.Errorf(`unexpected foreign-key "agent_id" returned %v`, n.ID)
+		}
+		for i := range nodes {
+			assign(nodes[i], n)
+		}
+	}
+	return nil
+}
+
 func (wq *WhatsappQuery) sqlCount(ctx context.Context) (int, error) {
 	_spec := wq.querySpec()
 	_spec.Node.Columns = wq.ctx.Fields
@@ -379,6 +455,9 @@ func (wq *WhatsappQuery) querySpec() *sqlgraph.QuerySpec {
 				_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
 			}
 		}
+		if wq.withAgent != nil {
+			_spec.Node.AddColumnOnce(whatsapp.FieldAgentID)
+		}
 	}
 	if ps := wq.predicates; len(ps) > 0 {
 		_spec.Predicate = func(selector *sql.Selector) {

+ 103 - 26
ent/whatsapp_update.go

@@ -7,6 +7,7 @@ import (
 	"errors"
 	"fmt"
 	"time"
+	"wechat-api/ent/agent"
 	"wechat-api/ent/predicate"
 	"wechat-api/ent/whatsapp"
 
@@ -201,7 +202,6 @@ func (wu *WhatsappUpdate) ClearOrganizationID() *WhatsappUpdate {
 
 // SetAgentID sets the "agent_id" field.
 func (wu *WhatsappUpdate) SetAgentID(u uint64) *WhatsappUpdate {
-	wu.mutation.ResetAgentID()
 	wu.mutation.SetAgentID(u)
 	return wu
 }
@@ -214,12 +214,6 @@ func (wu *WhatsappUpdate) SetNillableAgentID(u *uint64) *WhatsappUpdate {
 	return wu
 }
 
-// AddAgentID adds u to the "agent_id" field.
-func (wu *WhatsappUpdate) AddAgentID(u int64) *WhatsappUpdate {
-	wu.mutation.AddAgentID(u)
-	return wu
-}
-
 // SetAPIBase sets the "api_base" field.
 func (wu *WhatsappUpdate) SetAPIBase(s string) *WhatsappUpdate {
 	wu.mutation.SetAPIBase(s)
@@ -332,11 +326,22 @@ func (wu *WhatsappUpdate) ClearGroupBlockList() *WhatsappUpdate {
 	return wu
 }
 
+// SetAgent sets the "agent" edge to the Agent entity.
+func (wu *WhatsappUpdate) SetAgent(a *Agent) *WhatsappUpdate {
+	return wu.SetAgentID(a.ID)
+}
+
 // Mutation returns the WhatsappMutation object of the builder.
 func (wu *WhatsappUpdate) Mutation() *WhatsappMutation {
 	return wu.mutation
 }
 
+// ClearAgent clears the "agent" edge to the Agent entity.
+func (wu *WhatsappUpdate) ClearAgent() *WhatsappUpdate {
+	wu.mutation.ClearAgent()
+	return wu
+}
+
 // Save executes the query and returns the number of nodes affected by the update operation.
 func (wu *WhatsappUpdate) Save(ctx context.Context) (int, error) {
 	if err := wu.defaults(); err != nil {
@@ -379,7 +384,18 @@ func (wu *WhatsappUpdate) defaults() error {
 	return nil
 }
 
+// check runs all checks and user-defined validators on the builder.
+func (wu *WhatsappUpdate) check() error {
+	if _, ok := wu.mutation.AgentID(); wu.mutation.AgentCleared() && !ok {
+		return errors.New(`ent: clearing a required unique edge "Whatsapp.agent"`)
+	}
+	return nil
+}
+
 func (wu *WhatsappUpdate) sqlSave(ctx context.Context) (n int, err error) {
+	if err := wu.check(); err != nil {
+		return n, err
+	}
 	_spec := sqlgraph.NewUpdateSpec(whatsapp.Table, whatsapp.Columns, sqlgraph.NewFieldSpec(whatsapp.FieldID, field.TypeUint64))
 	if ps := wu.mutation.predicates; len(ps) > 0 {
 		_spec.Predicate = func(selector *sql.Selector) {
@@ -436,12 +452,6 @@ func (wu *WhatsappUpdate) sqlSave(ctx context.Context) (n int, err error) {
 	if wu.mutation.OrganizationIDCleared() {
 		_spec.ClearField(whatsapp.FieldOrganizationID, field.TypeUint64)
 	}
-	if value, ok := wu.mutation.AgentID(); ok {
-		_spec.SetField(whatsapp.FieldAgentID, field.TypeUint64, value)
-	}
-	if value, ok := wu.mutation.AddedAgentID(); ok {
-		_spec.AddField(whatsapp.FieldAgentID, field.TypeUint64, value)
-	}
 	if value, ok := wu.mutation.APIBase(); ok {
 		_spec.SetField(whatsapp.FieldAPIBase, field.TypeString, value)
 	}
@@ -498,6 +508,35 @@ func (wu *WhatsappUpdate) sqlSave(ctx context.Context) (n int, err error) {
 	if wu.mutation.GroupBlockListCleared() {
 		_spec.ClearField(whatsapp.FieldGroupBlockList, field.TypeJSON)
 	}
+	if wu.mutation.AgentCleared() {
+		edge := &sqlgraph.EdgeSpec{
+			Rel:     sqlgraph.M2O,
+			Inverse: true,
+			Table:   whatsapp.AgentTable,
+			Columns: []string{whatsapp.AgentColumn},
+			Bidi:    false,
+			Target: &sqlgraph.EdgeTarget{
+				IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
+			},
+		}
+		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
+	}
+	if nodes := wu.mutation.AgentIDs(); len(nodes) > 0 {
+		edge := &sqlgraph.EdgeSpec{
+			Rel:     sqlgraph.M2O,
+			Inverse: true,
+			Table:   whatsapp.AgentTable,
+			Columns: []string{whatsapp.AgentColumn},
+			Bidi:    false,
+			Target: &sqlgraph.EdgeTarget{
+				IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
+			},
+		}
+		for _, k := range nodes {
+			edge.Target.Nodes = append(edge.Target.Nodes, k)
+		}
+		_spec.Edges.Add = append(_spec.Edges.Add, edge)
+	}
 	if n, err = sqlgraph.UpdateNodes(ctx, wu.driver, _spec); err != nil {
 		if _, ok := err.(*sqlgraph.NotFoundError); ok {
 			err = &NotFoundError{whatsapp.Label}
@@ -690,7 +729,6 @@ func (wuo *WhatsappUpdateOne) ClearOrganizationID() *WhatsappUpdateOne {
 
 // SetAgentID sets the "agent_id" field.
 func (wuo *WhatsappUpdateOne) SetAgentID(u uint64) *WhatsappUpdateOne {
-	wuo.mutation.ResetAgentID()
 	wuo.mutation.SetAgentID(u)
 	return wuo
 }
@@ -703,12 +741,6 @@ func (wuo *WhatsappUpdateOne) SetNillableAgentID(u *uint64) *WhatsappUpdateOne {
 	return wuo
 }
 
-// AddAgentID adds u to the "agent_id" field.
-func (wuo *WhatsappUpdateOne) AddAgentID(u int64) *WhatsappUpdateOne {
-	wuo.mutation.AddAgentID(u)
-	return wuo
-}
-
 // SetAPIBase sets the "api_base" field.
 func (wuo *WhatsappUpdateOne) SetAPIBase(s string) *WhatsappUpdateOne {
 	wuo.mutation.SetAPIBase(s)
@@ -821,11 +853,22 @@ func (wuo *WhatsappUpdateOne) ClearGroupBlockList() *WhatsappUpdateOne {
 	return wuo
 }
 
+// SetAgent sets the "agent" edge to the Agent entity.
+func (wuo *WhatsappUpdateOne) SetAgent(a *Agent) *WhatsappUpdateOne {
+	return wuo.SetAgentID(a.ID)
+}
+
 // Mutation returns the WhatsappMutation object of the builder.
 func (wuo *WhatsappUpdateOne) Mutation() *WhatsappMutation {
 	return wuo.mutation
 }
 
+// ClearAgent clears the "agent" edge to the Agent entity.
+func (wuo *WhatsappUpdateOne) ClearAgent() *WhatsappUpdateOne {
+	wuo.mutation.ClearAgent()
+	return wuo
+}
+
 // Where appends a list predicates to the WhatsappUpdate builder.
 func (wuo *WhatsappUpdateOne) Where(ps ...predicate.Whatsapp) *WhatsappUpdateOne {
 	wuo.mutation.Where(ps...)
@@ -881,7 +924,18 @@ func (wuo *WhatsappUpdateOne) defaults() error {
 	return nil
 }
 
+// check runs all checks and user-defined validators on the builder.
+func (wuo *WhatsappUpdateOne) check() error {
+	if _, ok := wuo.mutation.AgentID(); wuo.mutation.AgentCleared() && !ok {
+		return errors.New(`ent: clearing a required unique edge "Whatsapp.agent"`)
+	}
+	return nil
+}
+
 func (wuo *WhatsappUpdateOne) sqlSave(ctx context.Context) (_node *Whatsapp, err error) {
+	if err := wuo.check(); err != nil {
+		return _node, err
+	}
 	_spec := sqlgraph.NewUpdateSpec(whatsapp.Table, whatsapp.Columns, sqlgraph.NewFieldSpec(whatsapp.FieldID, field.TypeUint64))
 	id, ok := wuo.mutation.ID()
 	if !ok {
@@ -955,12 +1009,6 @@ func (wuo *WhatsappUpdateOne) sqlSave(ctx context.Context) (_node *Whatsapp, err
 	if wuo.mutation.OrganizationIDCleared() {
 		_spec.ClearField(whatsapp.FieldOrganizationID, field.TypeUint64)
 	}
-	if value, ok := wuo.mutation.AgentID(); ok {
-		_spec.SetField(whatsapp.FieldAgentID, field.TypeUint64, value)
-	}
-	if value, ok := wuo.mutation.AddedAgentID(); ok {
-		_spec.AddField(whatsapp.FieldAgentID, field.TypeUint64, value)
-	}
 	if value, ok := wuo.mutation.APIBase(); ok {
 		_spec.SetField(whatsapp.FieldAPIBase, field.TypeString, value)
 	}
@@ -1017,6 +1065,35 @@ func (wuo *WhatsappUpdateOne) sqlSave(ctx context.Context) (_node *Whatsapp, err
 	if wuo.mutation.GroupBlockListCleared() {
 		_spec.ClearField(whatsapp.FieldGroupBlockList, field.TypeJSON)
 	}
+	if wuo.mutation.AgentCleared() {
+		edge := &sqlgraph.EdgeSpec{
+			Rel:     sqlgraph.M2O,
+			Inverse: true,
+			Table:   whatsapp.AgentTable,
+			Columns: []string{whatsapp.AgentColumn},
+			Bidi:    false,
+			Target: &sqlgraph.EdgeTarget{
+				IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
+			},
+		}
+		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
+	}
+	if nodes := wuo.mutation.AgentIDs(); len(nodes) > 0 {
+		edge := &sqlgraph.EdgeSpec{
+			Rel:     sqlgraph.M2O,
+			Inverse: true,
+			Table:   whatsapp.AgentTable,
+			Columns: []string{whatsapp.AgentColumn},
+			Bidi:    false,
+			Target: &sqlgraph.EdgeTarget{
+				IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
+			},
+		}
+		for _, k := range nodes {
+			edge.Target.Nodes = append(edge.Target.Nodes, k)
+		}
+		_spec.Edges.Add = append(_spec.Edges.Add, edge)
+	}
 	_node = &Whatsapp{config: wuo.config}
 	_spec.Assign = _node.assignValues
 	_spec.ScanValues = _node.scanValues

+ 36 - 12
internal/logic/whatsapp/get_whatsapp_by_id_logic.go

@@ -2,6 +2,8 @@ package whatsapp
 
 import (
 	"context"
+	"github.com/suyuan32/simple-admin-core/rpc/types/core"
+	"wechat-api/ent/whatsapp"
 
 	"wechat-api/internal/svc"
 	"wechat-api/internal/types"
@@ -28,11 +30,31 @@ func NewGetWhatsappByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *G
 }
 
 func (l *GetWhatsappByIdLogic) GetWhatsappById(req *types.IDReq) (*types.WhatsappInfoResp, error) {
-	data, err := l.svcCtx.DB.Whatsapp.Get(l.ctx, req.Id)
+	data, err := l.svcCtx.DB.Whatsapp.Query().Where(whatsapp.ID(req.Id)).WithAgent().Only(l.ctx)
 	if err != nil {
 		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
 	}
 
+	departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: data.OrganizationID})
+	if err != nil {
+		l.Error("获取部门信息失败", err)
+	}
+
+	var agent types.AgentInfo
+	l.Logger.Infof("agent=%v data=%v\n", data.Edges.Agent, data)
+	if data.Edges.Agent != nil {
+		agent = types.AgentInfo{
+			BaseIDInfo: types.BaseIDInfo{
+				Id: &data.AgentID,
+			},
+			Name:       &data.Edges.Agent.Name,
+			Role:       &data.Edges.Agent.Role,
+			Status:     &data.Edges.Agent.Status,
+			Background: &data.Edges.Agent.Background,
+			Examples:   &data.Edges.Agent.Examples,
+		}
+	}
+
 	return &types.WhatsappInfoResp{
 		BaseDataInfo: types.BaseDataInfo{
 			Code: 0,
@@ -44,17 +66,19 @@ func (l *GetWhatsappByIdLogic) GetWhatsappById(req *types.IDReq) (*types.Whatsap
 				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
 				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
 			},
-			Status:         &data.Status,
-			Ak:             &data.Ak,
-			Sk:             &data.Sk,
-			Callback:       &data.Callback,
-			Account:        &data.Account,
-			Nickname:       &data.Nickname,
-			Phone:          &data.Phone,
-			OrganizationId: &data.OrganizationID,
-			AgentId:        &data.AgentID,
-			ApiBase:        &data.APIBase,
-			ApiKey:         &data.APIKey,
+			Status:           &data.Status,
+			Ak:               &data.Ak,
+			Sk:               &data.Sk,
+			Callback:         &data.Callback,
+			Account:          &data.Account,
+			Nickname:         &data.Nickname,
+			Phone:            &data.Phone,
+			OrganizationId:   &data.OrganizationID,
+			OrganizationName: departmentInfo.Name,
+			AgentId:          &data.AgentID,
+			AgentInfo:        &agent,
+			ApiBase:          &data.APIBase,
+			ApiKey:           &data.APIKey,
 		},
 	}, nil
 }

+ 35 - 12
internal/logic/whatsapp/get_whatsapp_list_logic.go

@@ -2,6 +2,7 @@ package whatsapp
 
 import (
 	"context"
+	"github.com/suyuan32/simple-admin-core/rpc/types/core"
 
 	"wechat-api/ent/predicate"
 	"wechat-api/ent/whatsapp"
@@ -40,7 +41,7 @@ func (l *GetWhatsappListLogic) GetWhatsappList(req *types.WhatsappListReq) (*typ
 	if req.Callback != nil {
 		predicates = append(predicates, whatsapp.CallbackContains(*req.Callback))
 	}
-	data, err := l.svcCtx.DB.Whatsapp.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
+	data, err := l.svcCtx.DB.Whatsapp.Query().Where(predicates...).WithAgent().Page(l.ctx, req.Page, req.PageSize)
 
 	if err != nil {
 		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
@@ -51,6 +52,26 @@ func (l *GetWhatsappListLogic) GetWhatsappList(req *types.WhatsappListReq) (*typ
 	resp.Data.Total = data.PageDetails.Total
 
 	for _, v := range data.List {
+		departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: v.OrganizationID})
+		if err != nil {
+			l.Error("获取部门信息失败", err)
+		}
+
+		agent := types.AgentInfo{}
+		l.Logger.Infof("v.agent=%v v=%v\n", v.Edges.Agent, v)
+		if v.Edges.Agent != nil {
+			agent = types.AgentInfo{
+				BaseIDInfo: types.BaseIDInfo{
+					Id: &v.AgentID,
+				},
+				Name:       &v.Edges.Agent.Name,
+				Role:       &v.Edges.Agent.Role,
+				Status:     &v.Edges.Agent.Status,
+				Background: &v.Edges.Agent.Background,
+				Examples:   &v.Edges.Agent.Examples,
+			}
+		}
+
 		resp.Data.Data = append(resp.Data.Data,
 			types.WhatsappInfo{
 				BaseIDInfo: types.BaseIDInfo{
@@ -58,17 +79,19 @@ func (l *GetWhatsappListLogic) GetWhatsappList(req *types.WhatsappListReq) (*typ
 					CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
 					UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
 				},
-				Status:         &v.Status,
-				Ak:             &v.Ak,
-				Sk:             &v.Sk,
-				Callback:       &v.Callback,
-				Account:        &v.Account,
-				Nickname:       &v.Nickname,
-				Phone:          &v.Phone,
-				OrganizationId: &v.OrganizationID,
-				AgentId:        &v.AgentID,
-				ApiBase:        &v.APIBase,
-				ApiKey:         &v.APIKey,
+				Status:           &v.Status,
+				Ak:               &v.Ak,
+				Sk:               &v.Sk,
+				Callback:         &v.Callback,
+				Account:          &v.Account,
+				Nickname:         &v.Nickname,
+				Phone:            &v.Phone,
+				OrganizationId:   &v.OrganizationID,
+				OrganizationName: departmentInfo.Name,
+				AgentId:          &v.AgentID,
+				AgentInfo:        &agent,
+				ApiBase:          &v.APIBase,
+				ApiKey:           &v.APIKey,
 			})
 	}
 

+ 4 - 2
internal/types/types.go

@@ -3440,9 +3440,11 @@ type WhatsappInfo struct {
 	// 手机号
 	Phone *string `json:"phone,optional"`
 	// 机构 ID
-	OrganizationId *uint64 `json:"organizationId,optional"`
+	OrganizationId   *uint64 `json:"organizationId,optional"`
+	OrganizationName *string `json:"organizationName,optional"`
 	// 模式ID
-	AgentId *uint64 `json:"agentId,optional"`
+	AgentId   *uint64    `json:"agentId,optional"`
+	AgentInfo *AgentInfo `json:"agentInfo,optional"`
 	// 大模型服务地址
 	ApiBase *string `json:"apiBase,optional"`
 	// 大模型服务密钥