Browse Source

fix:sumit Agent API

jimmyyem 7 months ago
parent
commit
9f1031d485

+ 14 - 3
ent/agent.go

@@ -32,8 +32,10 @@ type Agent struct {
 	// background | 背景介绍
 	Background string `json:"background,omitempty"`
 	// examples | 对话案例
-	Examples     string `json:"examples,omitempty"`
-	selectValues sql.SelectValues
+	Examples string `json:"examples,omitempty"`
+	// organization_id | 租户ID
+	OrganizationID uint64 `json:"organization_id,omitempty"`
+	selectValues   sql.SelectValues
 }
 
 // scanValues returns the types for scanning values from sql.Rows.
@@ -41,7 +43,7 @@ func (*Agent) scanValues(columns []string) ([]any, error) {
 	values := make([]any, len(columns))
 	for i := range columns {
 		switch columns[i] {
-		case agent.FieldID, agent.FieldStatus:
+		case agent.FieldID, agent.FieldStatus, agent.FieldOrganizationID:
 			values[i] = new(sql.NullInt64)
 		case agent.FieldName, agent.FieldRole, agent.FieldBackground, agent.FieldExamples:
 			values[i] = new(sql.NullString)
@@ -116,6 +118,12 @@ func (a *Agent) assignValues(columns []string, values []any) error {
 			} else if value.Valid {
 				a.Examples = value.String
 			}
+		case agent.FieldOrganizationID:
+			if value, ok := values[i].(*sql.NullInt64); !ok {
+				return fmt.Errorf("unexpected type %T for field organization_id", values[i])
+			} else if value.Valid {
+				a.OrganizationID = uint64(value.Int64)
+			}
 		default:
 			a.selectValues.Set(columns[i], values[i])
 		}
@@ -175,6 +183,9 @@ func (a *Agent) String() string {
 	builder.WriteString(", ")
 	builder.WriteString("examples=")
 	builder.WriteString(a.Examples)
+	builder.WriteString(", ")
+	builder.WriteString("organization_id=")
+	builder.WriteString(fmt.Sprintf("%v", a.OrganizationID))
 	builder.WriteByte(')')
 	return builder.String()
 }

+ 8 - 0
ent/agent/agent.go

@@ -30,6 +30,8 @@ const (
 	FieldBackground = "background"
 	// FieldExamples holds the string denoting the examples field in the database.
 	FieldExamples = "examples"
+	// FieldOrganizationID holds the string denoting the organization_id field in the database.
+	FieldOrganizationID = "organization_id"
 	// Table holds the table name of the agent in the database.
 	Table = "agent"
 )
@@ -45,6 +47,7 @@ var Columns = []string{
 	FieldStatus,
 	FieldBackground,
 	FieldExamples,
+	FieldOrganizationID,
 }
 
 // ValidColumn reports if the column name is valid (part of the table columns).
@@ -130,3 +133,8 @@ func ByBackground(opts ...sql.OrderTermOption) OrderOption {
 func ByExamples(opts ...sql.OrderTermOption) OrderOption {
 	return sql.OrderByField(FieldExamples, opts...).ToFunc()
 }
+
+// ByOrganizationID orders the results by the organization_id field.
+func ByOrganizationID(opts ...sql.OrderTermOption) OrderOption {
+	return sql.OrderByField(FieldOrganizationID, opts...).ToFunc()
+}

+ 45 - 0
ent/agent/where.go

@@ -94,6 +94,11 @@ func Examples(v string) predicate.Agent {
 	return predicate.Agent(sql.FieldEQ(FieldExamples, v))
 }
 
+// OrganizationID applies equality check predicate on the "organization_id" field. It's identical to OrganizationIDEQ.
+func OrganizationID(v uint64) predicate.Agent {
+	return predicate.Agent(sql.FieldEQ(FieldOrganizationID, v))
+}
+
 // CreatedAtEQ applies the EQ predicate on the "created_at" field.
 func CreatedAtEQ(v time.Time) predicate.Agent {
 	return predicate.Agent(sql.FieldEQ(FieldCreatedAt, v))
@@ -534,6 +539,46 @@ func ExamplesContainsFold(v string) predicate.Agent {
 	return predicate.Agent(sql.FieldContainsFold(FieldExamples, v))
 }
 
+// OrganizationIDEQ applies the EQ predicate on the "organization_id" field.
+func OrganizationIDEQ(v uint64) predicate.Agent {
+	return predicate.Agent(sql.FieldEQ(FieldOrganizationID, v))
+}
+
+// OrganizationIDNEQ applies the NEQ predicate on the "organization_id" field.
+func OrganizationIDNEQ(v uint64) predicate.Agent {
+	return predicate.Agent(sql.FieldNEQ(FieldOrganizationID, v))
+}
+
+// OrganizationIDIn applies the In predicate on the "organization_id" field.
+func OrganizationIDIn(vs ...uint64) predicate.Agent {
+	return predicate.Agent(sql.FieldIn(FieldOrganizationID, vs...))
+}
+
+// OrganizationIDNotIn applies the NotIn predicate on the "organization_id" field.
+func OrganizationIDNotIn(vs ...uint64) predicate.Agent {
+	return predicate.Agent(sql.FieldNotIn(FieldOrganizationID, vs...))
+}
+
+// OrganizationIDGT applies the GT predicate on the "organization_id" field.
+func OrganizationIDGT(v uint64) predicate.Agent {
+	return predicate.Agent(sql.FieldGT(FieldOrganizationID, v))
+}
+
+// OrganizationIDGTE applies the GTE predicate on the "organization_id" field.
+func OrganizationIDGTE(v uint64) predicate.Agent {
+	return predicate.Agent(sql.FieldGTE(FieldOrganizationID, v))
+}
+
+// OrganizationIDLT applies the LT predicate on the "organization_id" field.
+func OrganizationIDLT(v uint64) predicate.Agent {
+	return predicate.Agent(sql.FieldLT(FieldOrganizationID, v))
+}
+
+// OrganizationIDLTE applies the LTE predicate on the "organization_id" field.
+func OrganizationIDLTE(v uint64) predicate.Agent {
+	return predicate.Agent(sql.FieldLTE(FieldOrganizationID, v))
+}
+
 // And groups predicates with the AND operator between them.
 func And(predicates ...predicate.Agent) predicate.Agent {
 	return predicate.Agent(sql.AndPredicates(predicates...))

+ 73 - 0
ent/agent_create.go

@@ -102,6 +102,12 @@ func (ac *AgentCreate) SetExamples(s string) *AgentCreate {
 	return ac
 }
 
+// SetOrganizationID sets the "organization_id" field.
+func (ac *AgentCreate) SetOrganizationID(u uint64) *AgentCreate {
+	ac.mutation.SetOrganizationID(u)
+	return ac
+}
+
 // SetID sets the "id" field.
 func (ac *AgentCreate) SetID(u uint64) *AgentCreate {
 	ac.mutation.SetID(u)
@@ -206,6 +212,9 @@ func (ac *AgentCreate) check() error {
 			return &ValidationError{Name: "examples", err: fmt.Errorf(`ent: validator failed for field "Agent.examples": %w`, err)}
 		}
 	}
+	if _, ok := ac.mutation.OrganizationID(); !ok {
+		return &ValidationError{Name: "organization_id", err: errors.New(`ent: missing required field "Agent.organization_id"`)}
+	}
 	return nil
 }
 
@@ -271,6 +280,10 @@ func (ac *AgentCreate) createSpec() (*Agent, *sqlgraph.CreateSpec) {
 		_spec.SetField(agent.FieldExamples, field.TypeString, value)
 		_node.Examples = value
 	}
+	if value, ok := ac.mutation.OrganizationID(); ok {
+		_spec.SetField(agent.FieldOrganizationID, field.TypeUint64, value)
+		_node.OrganizationID = value
+	}
 	return _node, _spec
 }
 
@@ -425,6 +438,24 @@ func (u *AgentUpsert) UpdateExamples() *AgentUpsert {
 	return u
 }
 
+// SetOrganizationID sets the "organization_id" field.
+func (u *AgentUpsert) SetOrganizationID(v uint64) *AgentUpsert {
+	u.Set(agent.FieldOrganizationID, v)
+	return u
+}
+
+// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
+func (u *AgentUpsert) UpdateOrganizationID() *AgentUpsert {
+	u.SetExcluded(agent.FieldOrganizationID)
+	return u
+}
+
+// AddOrganizationID adds v to the "organization_id" field.
+func (u *AgentUpsert) AddOrganizationID(v uint64) *AgentUpsert {
+	u.Add(agent.FieldOrganizationID, v)
+	return u
+}
+
 // UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
 // Using this option is equivalent to using:
 //
@@ -595,6 +626,27 @@ func (u *AgentUpsertOne) UpdateExamples() *AgentUpsertOne {
 	})
 }
 
+// SetOrganizationID sets the "organization_id" field.
+func (u *AgentUpsertOne) SetOrganizationID(v uint64) *AgentUpsertOne {
+	return u.Update(func(s *AgentUpsert) {
+		s.SetOrganizationID(v)
+	})
+}
+
+// AddOrganizationID adds v to the "organization_id" field.
+func (u *AgentUpsertOne) AddOrganizationID(v uint64) *AgentUpsertOne {
+	return u.Update(func(s *AgentUpsert) {
+		s.AddOrganizationID(v)
+	})
+}
+
+// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
+func (u *AgentUpsertOne) UpdateOrganizationID() *AgentUpsertOne {
+	return u.Update(func(s *AgentUpsert) {
+		s.UpdateOrganizationID()
+	})
+}
+
 // Exec executes the query.
 func (u *AgentUpsertOne) Exec(ctx context.Context) error {
 	if len(u.create.conflict) == 0 {
@@ -931,6 +983,27 @@ func (u *AgentUpsertBulk) UpdateExamples() *AgentUpsertBulk {
 	})
 }
 
+// SetOrganizationID sets the "organization_id" field.
+func (u *AgentUpsertBulk) SetOrganizationID(v uint64) *AgentUpsertBulk {
+	return u.Update(func(s *AgentUpsert) {
+		s.SetOrganizationID(v)
+	})
+}
+
+// AddOrganizationID adds v to the "organization_id" field.
+func (u *AgentUpsertBulk) AddOrganizationID(v uint64) *AgentUpsertBulk {
+	return u.Update(func(s *AgentUpsert) {
+		s.AddOrganizationID(v)
+	})
+}
+
+// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
+func (u *AgentUpsertBulk) UpdateOrganizationID() *AgentUpsertBulk {
+	return u.Update(func(s *AgentUpsert) {
+		s.UpdateOrganizationID()
+	})
+}
+
 // Exec executes the query.
 func (u *AgentUpsertBulk) Exec(ctx context.Context) error {
 	if u.create.err != nil {

+ 54 - 0
ent/agent_update.go

@@ -137,6 +137,27 @@ func (au *AgentUpdate) SetNillableExamples(s *string) *AgentUpdate {
 	return au
 }
 
+// SetOrganizationID sets the "organization_id" field.
+func (au *AgentUpdate) SetOrganizationID(u uint64) *AgentUpdate {
+	au.mutation.ResetOrganizationID()
+	au.mutation.SetOrganizationID(u)
+	return au
+}
+
+// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
+func (au *AgentUpdate) SetNillableOrganizationID(u *uint64) *AgentUpdate {
+	if u != nil {
+		au.SetOrganizationID(*u)
+	}
+	return au
+}
+
+// AddOrganizationID adds u to the "organization_id" field.
+func (au *AgentUpdate) AddOrganizationID(u int64) *AgentUpdate {
+	au.mutation.AddOrganizationID(u)
+	return au
+}
+
 // Mutation returns the AgentMutation object of the builder.
 func (au *AgentUpdate) Mutation() *AgentMutation {
 	return au.mutation
@@ -251,6 +272,12 @@ func (au *AgentUpdate) sqlSave(ctx context.Context) (n int, err error) {
 	if value, ok := au.mutation.Examples(); ok {
 		_spec.SetField(agent.FieldExamples, field.TypeString, value)
 	}
+	if value, ok := au.mutation.OrganizationID(); ok {
+		_spec.SetField(agent.FieldOrganizationID, field.TypeUint64, value)
+	}
+	if value, ok := au.mutation.AddedOrganizationID(); ok {
+		_spec.AddField(agent.FieldOrganizationID, field.TypeUint64, value)
+	}
 	if n, err = sqlgraph.UpdateNodes(ctx, au.driver, _spec); err != nil {
 		if _, ok := err.(*sqlgraph.NotFoundError); ok {
 			err = &NotFoundError{agent.Label}
@@ -380,6 +407,27 @@ func (auo *AgentUpdateOne) SetNillableExamples(s *string) *AgentUpdateOne {
 	return auo
 }
 
+// SetOrganizationID sets the "organization_id" field.
+func (auo *AgentUpdateOne) SetOrganizationID(u uint64) *AgentUpdateOne {
+	auo.mutation.ResetOrganizationID()
+	auo.mutation.SetOrganizationID(u)
+	return auo
+}
+
+// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
+func (auo *AgentUpdateOne) SetNillableOrganizationID(u *uint64) *AgentUpdateOne {
+	if u != nil {
+		auo.SetOrganizationID(*u)
+	}
+	return auo
+}
+
+// AddOrganizationID adds u to the "organization_id" field.
+func (auo *AgentUpdateOne) AddOrganizationID(u int64) *AgentUpdateOne {
+	auo.mutation.AddOrganizationID(u)
+	return auo
+}
+
 // Mutation returns the AgentMutation object of the builder.
 func (auo *AgentUpdateOne) Mutation() *AgentMutation {
 	return auo.mutation
@@ -524,6 +572,12 @@ func (auo *AgentUpdateOne) sqlSave(ctx context.Context) (_node *Agent, err error
 	if value, ok := auo.mutation.Examples(); ok {
 		_spec.SetField(agent.FieldExamples, field.TypeString, value)
 	}
+	if value, ok := auo.mutation.OrganizationID(); ok {
+		_spec.SetField(agent.FieldOrganizationID, field.TypeUint64, value)
+	}
+	if value, ok := auo.mutation.AddedOrganizationID(); ok {
+		_spec.AddField(agent.FieldOrganizationID, field.TypeUint64, value)
+	}
 	_node = &Agent{config: auo.config}
 	_spec.Assign = _node.assignValues
 	_spec.ScanValues = _node.scanValues

+ 1 - 0
ent/migrate/schema.go

@@ -20,6 +20,7 @@ var (
 		{Name: "status", Type: field.TypeInt, Nullable: true, Comment: "status | 状态 1-正常 2-禁用", Default: 1},
 		{Name: "background", Type: field.TypeString, Size: 1000, Comment: "background | 背景介绍"},
 		{Name: "examples", Type: field.TypeString, Size: 5000, Comment: "examples | 对话案例"},
+		{Name: "organization_id", Type: field.TypeUint64, Comment: "organization_id | 租户ID"},
 	}
 	// AgentTable holds the schema information for the "agent" table.
 	AgentTable = &schema.Table{

+ 104 - 17
ent/mutation.go

@@ -55,22 +55,24 @@ const (
 // AgentMutation represents an operation that mutates the Agent nodes in the graph.
 type AgentMutation struct {
 	config
-	op            Op
-	typ           string
-	id            *uint64
-	created_at    *time.Time
-	updated_at    *time.Time
-	deleted_at    *time.Time
-	name          *string
-	role          *string
-	status        *int
-	addstatus     *int
-	background    *string
-	examples      *string
-	clearedFields map[string]struct{}
-	done          bool
-	oldValue      func(context.Context) (*Agent, error)
-	predicates    []predicate.Agent
+	op                 Op
+	typ                string
+	id                 *uint64
+	created_at         *time.Time
+	updated_at         *time.Time
+	deleted_at         *time.Time
+	name               *string
+	role               *string
+	status             *int
+	addstatus          *int
+	background         *string
+	examples           *string
+	organization_id    *uint64
+	addorganization_id *int64
+	clearedFields      map[string]struct{}
+	done               bool
+	oldValue           func(context.Context) (*Agent, error)
+	predicates         []predicate.Agent
 }
 
 var _ ent.Mutation = (*AgentMutation)(nil)
@@ -512,6 +514,62 @@ func (m *AgentMutation) ResetExamples() {
 	m.examples = nil
 }
 
+// SetOrganizationID sets the "organization_id" field.
+func (m *AgentMutation) SetOrganizationID(u uint64) {
+	m.organization_id = &u
+	m.addorganization_id = nil
+}
+
+// OrganizationID returns the value of the "organization_id" field in the mutation.
+func (m *AgentMutation) OrganizationID() (r uint64, exists bool) {
+	v := m.organization_id
+	if v == nil {
+		return
+	}
+	return *v, true
+}
+
+// OldOrganizationID returns the old "organization_id" field's value of the Agent entity.
+// If the Agent object wasn't provided to the builder, the object is fetched from the database.
+// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
+func (m *AgentMutation) OldOrganizationID(ctx context.Context) (v uint64, err error) {
+	if !m.op.Is(OpUpdateOne) {
+		return v, errors.New("OldOrganizationID is only allowed on UpdateOne operations")
+	}
+	if m.id == nil || m.oldValue == nil {
+		return v, errors.New("OldOrganizationID requires an ID field in the mutation")
+	}
+	oldValue, err := m.oldValue(ctx)
+	if err != nil {
+		return v, fmt.Errorf("querying old value for OldOrganizationID: %w", err)
+	}
+	return oldValue.OrganizationID, nil
+}
+
+// AddOrganizationID adds u to the "organization_id" field.
+func (m *AgentMutation) AddOrganizationID(u int64) {
+	if m.addorganization_id != nil {
+		*m.addorganization_id += u
+	} else {
+		m.addorganization_id = &u
+	}
+}
+
+// AddedOrganizationID returns the value that was added to the "organization_id" field in this mutation.
+func (m *AgentMutation) AddedOrganizationID() (r int64, exists bool) {
+	v := m.addorganization_id
+	if v == nil {
+		return
+	}
+	return *v, true
+}
+
+// ResetOrganizationID resets all changes to the "organization_id" field.
+func (m *AgentMutation) ResetOrganizationID() {
+	m.organization_id = nil
+	m.addorganization_id = nil
+}
+
 // Where appends a list predicates to the AgentMutation builder.
 func (m *AgentMutation) Where(ps ...predicate.Agent) {
 	m.predicates = append(m.predicates, ps...)
@@ -546,7 +604,7 @@ func (m *AgentMutation) Type() string {
 // order to get all numeric fields that were incremented/decremented, call
 // AddedFields().
 func (m *AgentMutation) Fields() []string {
-	fields := make([]string, 0, 8)
+	fields := make([]string, 0, 9)
 	if m.created_at != nil {
 		fields = append(fields, agent.FieldCreatedAt)
 	}
@@ -571,6 +629,9 @@ func (m *AgentMutation) Fields() []string {
 	if m.examples != nil {
 		fields = append(fields, agent.FieldExamples)
 	}
+	if m.organization_id != nil {
+		fields = append(fields, agent.FieldOrganizationID)
+	}
 	return fields
 }
 
@@ -595,6 +656,8 @@ func (m *AgentMutation) Field(name string) (ent.Value, bool) {
 		return m.Background()
 	case agent.FieldExamples:
 		return m.Examples()
+	case agent.FieldOrganizationID:
+		return m.OrganizationID()
 	}
 	return nil, false
 }
@@ -620,6 +683,8 @@ func (m *AgentMutation) OldField(ctx context.Context, name string) (ent.Value, e
 		return m.OldBackground(ctx)
 	case agent.FieldExamples:
 		return m.OldExamples(ctx)
+	case agent.FieldOrganizationID:
+		return m.OldOrganizationID(ctx)
 	}
 	return nil, fmt.Errorf("unknown Agent field %s", name)
 }
@@ -685,6 +750,13 @@ func (m *AgentMutation) SetField(name string, value ent.Value) error {
 		}
 		m.SetExamples(v)
 		return nil
+	case agent.FieldOrganizationID:
+		v, ok := value.(uint64)
+		if !ok {
+			return fmt.Errorf("unexpected type %T for field %s", value, name)
+		}
+		m.SetOrganizationID(v)
+		return nil
 	}
 	return fmt.Errorf("unknown Agent field %s", name)
 }
@@ -696,6 +768,9 @@ func (m *AgentMutation) AddedFields() []string {
 	if m.addstatus != nil {
 		fields = append(fields, agent.FieldStatus)
 	}
+	if m.addorganization_id != nil {
+		fields = append(fields, agent.FieldOrganizationID)
+	}
 	return fields
 }
 
@@ -706,6 +781,8 @@ func (m *AgentMutation) AddedField(name string) (ent.Value, bool) {
 	switch name {
 	case agent.FieldStatus:
 		return m.AddedStatus()
+	case agent.FieldOrganizationID:
+		return m.AddedOrganizationID()
 	}
 	return nil, false
 }
@@ -722,6 +799,13 @@ func (m *AgentMutation) AddField(name string, value ent.Value) error {
 		}
 		m.AddStatus(v)
 		return nil
+	case agent.FieldOrganizationID:
+		v, ok := value.(int64)
+		if !ok {
+			return fmt.Errorf("unexpected type %T for field %s", value, name)
+		}
+		m.AddOrganizationID(v)
+		return nil
 	}
 	return fmt.Errorf("unknown Agent numeric field %s", name)
 }
@@ -788,6 +872,9 @@ func (m *AgentMutation) ResetField(name string) error {
 	case agent.FieldExamples:
 		m.ResetExamples()
 		return nil
+	case agent.FieldOrganizationID:
+		m.ResetOrganizationID()
+		return nil
 	}
 	return fmt.Errorf("unknown Agent field %s", name)
 }

+ 1 - 0
ent/schema/agent.go

@@ -21,6 +21,7 @@ func (Agent) Fields() []ent.Field {
 		field.Int("status").Optional().Default(1).Comment("status | 状态 1-正常 2-禁用"),
 		field.String("background").MaxLen(1000).Comment("background | 背景介绍"),
 		field.String("examples").MaxLen(5000).Comment("examples | 对话案例"),
+		field.Uint64("organization_id").Comment("organization_id | 租户ID"),
 	}
 }
 

+ 24 - 0
ent/set_not_nil.go

@@ -176,6 +176,30 @@ func (a *AgentCreate) SetNotNilExamples(value *string) *AgentCreate {
 }
 
 // set field if value's pointer is not nil.
+func (a *AgentUpdate) SetNotNilOrganizationID(value *uint64) *AgentUpdate {
+	if value != nil {
+		return a.SetOrganizationID(*value)
+	}
+	return a
+}
+
+// set field if value's pointer is not nil.
+func (a *AgentUpdateOne) SetNotNilOrganizationID(value *uint64) *AgentUpdateOne {
+	if value != nil {
+		return a.SetOrganizationID(*value)
+	}
+	return a
+}
+
+// set field if value's pointer is not nil.
+func (a *AgentCreate) SetNotNilOrganizationID(value *uint64) *AgentCreate {
+	if value != nil {
+		return a.SetOrganizationID(*value)
+	}
+	return a
+}
+
+// set field if value's pointer is not nil.
 func (bm *BatchMsgUpdate) SetNotNilUpdatedAt(value *time.Time) *BatchMsgUpdate {
 	if value != nil {
 		return bm.SetUpdatedAt(*value)

+ 2 - 1
etc/wechat.yaml

@@ -6,7 +6,7 @@ Timeout: 30000
 Mode: "dev"
 
 Auth:
-  AccessSecret: "LnQD46hBde0AgFXBer8ZZZe3FgC"
+  AccessSecret: jS6VKDtsJf3z1n2VKDtsJf3z1n2
   AccessExpire: 259200
 
 CROSConf:
@@ -17,6 +17,7 @@ Log:
   Mode: console
   Encoding: plain
   Stat: false
+  Path: /tmp
   Level: info
   Compress: false
   KeepDays: 7

+ 3 - 15
internal/logic/agent/create_agent_logic.go

@@ -2,9 +2,6 @@ package agent
 
 import (
 	"context"
-	"errors"
-	"wechat-api/ent/agent"
-
 	"wechat-api/internal/svc"
 	"wechat-api/internal/types"
 	"wechat-api/internal/utils/dberrorhandler"
@@ -29,22 +26,13 @@ func NewCreateAgentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Creat
 }
 
 func (l *CreateAgentLogic) CreateAgent(req *types.AgentInfo) (*types.BaseMsgResp, error) {
-	item, err := l.svcCtx.DB.Agent.Query().Where(agent.Name(*req.Name)).First(l.ctx)
-
-	if err != nil {
-		l.Error("DB查询失败")
-		return nil, err
-	}
-
-	if item != nil && item.ID > 0 {
-		l.Error("角色名字已经存在")
-		return nil, errors.New("角色名字已经存在")
-	}
+	organizationId := l.ctx.Value("organizationId").(uint64)
 
-	_, err = l.svcCtx.DB.Agent.Create().
+	_, err := l.svcCtx.DB.Agent.Create().
 		SetNotNilName(req.Name).
 		SetNotNilRole(req.Role).
 		//SetNotNilStatus(req.Status).
+		SetOrganizationID(organizationId).
 		SetNotNilBackground(req.Background).
 		SetNotNilExamples(req.Examples).
 		Save(l.ctx)

+ 2 - 1
internal/logic/agent/delete_agent_logic.go

@@ -28,11 +28,12 @@ func NewDeleteAgentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Delet
 }
 
 func (l *DeleteAgentLogic) DeleteAgent(req *types.IDsReq) (*types.BaseMsgResp, error) {
+	organizationId := l.ctx.Value("organizationId").(uint64)
 	if len(req.Ids) == 0 {
 		return nil, errors.New("ids参数不能为空")
 	}
 
-	_, err := l.svcCtx.DB.Agent.Delete().Where(agent.IDIn(req.Ids...)).Exec(l.ctx)
+	_, err := l.svcCtx.DB.Agent.Delete().Where(agent.IDIn(req.Ids...), agent.OrganizationID(organizationId)).Exec(l.ctx)
 
 	if err != nil {
 		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)

+ 20 - 19
internal/logic/agent/get_agent_by_id_logic.go

@@ -2,12 +2,13 @@ package agent
 
 import (
 	"context"
+	"wechat-api/ent/agent"
 
 	"wechat-api/internal/svc"
 	"wechat-api/internal/types"
 	"wechat-api/internal/utils/dberrorhandler"
 
-    "github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"github.com/suyuan32/simple-admin-common/msg/errormsg"
 
 	"github.com/suyuan32/simple-admin-common/utils/pointy"
 	"github.com/zeromicro/go-zero/core/logx"
@@ -28,28 +29,28 @@ func NewGetAgentByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetA
 }
 
 func (l *GetAgentByIdLogic) GetAgentById(req *types.IDReq) (*types.AgentInfoResp, error) {
-	data, err := l.svcCtx.DB.Agent.Get(l.ctx, req.Id)
+	organizationId := l.ctx.Value("organizationId").(uint64)
+	data, err := l.svcCtx.DB.Agent.Query().Where(agent.ID(req.Id), agent.OrganizationID(organizationId)).First(l.ctx)
 	if err != nil {
 		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
 	}
 
 	return &types.AgentInfoResp{
-	    BaseDataInfo: types.BaseDataInfo{
-            Code: 0,
-            Msg:  errormsg.Success,
-        },
-        Data: types.AgentInfo{
-            BaseIDInfo:    types.BaseIDInfo{
-				Id:          &data.ID,
-				CreatedAt:    pointy.GetPointer(data.CreatedAt.UnixMilli()),
-				UpdatedAt:    pointy.GetPointer(data.UpdatedAt.UnixMilli()),
-            },
-			Name:	&data.Name,
-			Role:	&data.Role,
-			Status:	&data.Status,
-			Background:	&data.Background,
-			Examples:	&data.Examples,
-        },
+		BaseDataInfo: types.BaseDataInfo{
+			Code: 0,
+			Msg:  errormsg.Success,
+		},
+		Data: types.AgentInfo{
+			BaseIDInfo: types.BaseIDInfo{
+				Id:        &data.ID,
+				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
+				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
+			},
+			Name:       &data.Name,
+			Role:       &data.Role,
+			Status:     &data.Status,
+			Background: &data.Background,
+			Examples:   &data.Examples,
+		},
 	}, nil
 }
-

+ 15 - 13
internal/logic/agent/get_agent_list_logic.go

@@ -9,7 +9,7 @@ import (
 	"wechat-api/internal/types"
 	"wechat-api/internal/utils/dberrorhandler"
 
-    "github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"github.com/suyuan32/simple-admin-common/msg/errormsg"
 
 	"github.com/suyuan32/simple-admin-common/utils/pointy"
 	"github.com/zeromicro/go-zero/core/logx"
@@ -30,7 +30,9 @@ func NewGetAgentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetA
 }
 
 func (l *GetAgentListLogic) GetAgentList(req *types.AgentListReq) (*types.AgentListResp, error) {
+	organizationId := l.ctx.Value("organizationId").(uint64)
 	var predicates []predicate.Agent
+	predicates = append(predicates, agent.OrganizationID(organizationId))
 	if req.Name != nil {
 		predicates = append(predicates, agent.NameContains(*req.Name))
 	}
@@ -52,18 +54,18 @@ func (l *GetAgentListLogic) GetAgentList(req *types.AgentListReq) (*types.AgentL
 
 	for _, v := range data.List {
 		resp.Data.Data = append(resp.Data.Data,
-		types.AgentInfo{
-			BaseIDInfo:    types.BaseIDInfo{
-				Id:          &v.ID,
-				CreatedAt:    pointy.GetPointer(v.CreatedAt.UnixMilli()),
-				UpdatedAt:    pointy.GetPointer(v.UpdatedAt.UnixMilli()),
-            },
-			Name:	&v.Name,
-			Role:	&v.Role,
-			Status:	&v.Status,
-			Background:	&v.Background,
-			Examples:	&v.Examples,
-		})
+			types.AgentInfo{
+				BaseIDInfo: types.BaseIDInfo{
+					Id:        &v.ID,
+					CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
+					UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
+				},
+				Name:       &v.Name,
+				Role:       &v.Role,
+				Status:     &v.Status,
+				Background: &v.Background,
+				Examples:   &v.Examples,
+			})
 	}
 
 	return resp, nil

+ 9 - 6
internal/logic/agent/update_agent_logic.go

@@ -28,13 +28,16 @@ func NewUpdateAgentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Updat
 }
 
 func (l *UpdateAgentLogic) UpdateAgent(req *types.AgentInfo) (*types.BaseMsgResp, error) {
-	item, err := l.svcCtx.DB.Agent.Query().Where(agent.Name(*req.Name)).Where(agent.IDNEQ(*req.Id)).First(l.ctx)
-	if err != nil {
-		l.Error("DB查询失败")
-		return nil, err
+	organizationId := l.ctx.Value("organizationId").(uint64)
+
+	if *req.Id <= 0 {
+		l.Error("记录ID不存在")
+		return nil, errors.New("记录ID不存在")
 	}
-	if item != nil && item.ID > 0 {
-		return nil, errors.New("已有相同名字的角色")
+
+	item, err := l.svcCtx.DB.Agent.Query().Where(agent.ID(*req.Id), agent.OrganizationID(organizationId)).First(l.ctx)
+	if item.ID == 0 {
+		return nil, errors.New("记录不存在")
 	}
 
 	err = l.svcCtx.DB.Agent.UpdateOneID(*req.Id).