agent_query.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "database/sql/driver"
  6. "fmt"
  7. "math"
  8. "wechat-api/ent/agent"
  9. "wechat-api/ent/apikey"
  10. "wechat-api/ent/predicate"
  11. "wechat-api/ent/token"
  12. "wechat-api/ent/whatsapp"
  13. "wechat-api/ent/wx"
  14. "wechat-api/ent/xunjiservice"
  15. "entgo.io/ent/dialect/sql"
  16. "entgo.io/ent/dialect/sql/sqlgraph"
  17. "entgo.io/ent/schema/field"
  18. )
  19. // AgentQuery is the builder for querying Agent entities.
  20. type AgentQuery struct {
  21. config
  22. ctx *QueryContext
  23. order []agent.OrderOption
  24. inters []Interceptor
  25. predicates []predicate.Agent
  26. withWxAgent *WxQuery
  27. withTokenAgent *TokenQuery
  28. withWaAgent *WhatsappQuery
  29. withXjsAgent *XunjiServiceQuery
  30. withKeyAgent *ApiKeyQuery
  31. // intermediate query (i.e. traversal path).
  32. sql *sql.Selector
  33. path func(context.Context) (*sql.Selector, error)
  34. }
  35. // Where adds a new predicate for the AgentQuery builder.
  36. func (aq *AgentQuery) Where(ps ...predicate.Agent) *AgentQuery {
  37. aq.predicates = append(aq.predicates, ps...)
  38. return aq
  39. }
  40. // Limit the number of records to be returned by this query.
  41. func (aq *AgentQuery) Limit(limit int) *AgentQuery {
  42. aq.ctx.Limit = &limit
  43. return aq
  44. }
  45. // Offset to start from.
  46. func (aq *AgentQuery) Offset(offset int) *AgentQuery {
  47. aq.ctx.Offset = &offset
  48. return aq
  49. }
  50. // Unique configures the query builder to filter duplicate records on query.
  51. // By default, unique is set to true, and can be disabled using this method.
  52. func (aq *AgentQuery) Unique(unique bool) *AgentQuery {
  53. aq.ctx.Unique = &unique
  54. return aq
  55. }
  56. // Order specifies how the records should be ordered.
  57. func (aq *AgentQuery) Order(o ...agent.OrderOption) *AgentQuery {
  58. aq.order = append(aq.order, o...)
  59. return aq
  60. }
  61. // QueryWxAgent chains the current query on the "wx_agent" edge.
  62. func (aq *AgentQuery) QueryWxAgent() *WxQuery {
  63. query := (&WxClient{config: aq.config}).Query()
  64. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  65. if err := aq.prepareQuery(ctx); err != nil {
  66. return nil, err
  67. }
  68. selector := aq.sqlQuery(ctx)
  69. if err := selector.Err(); err != nil {
  70. return nil, err
  71. }
  72. step := sqlgraph.NewStep(
  73. sqlgraph.From(agent.Table, agent.FieldID, selector),
  74. sqlgraph.To(wx.Table, wx.FieldID),
  75. sqlgraph.Edge(sqlgraph.O2M, false, agent.WxAgentTable, agent.WxAgentColumn),
  76. )
  77. fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step)
  78. return fromU, nil
  79. }
  80. return query
  81. }
  82. // QueryTokenAgent chains the current query on the "token_agent" edge.
  83. func (aq *AgentQuery) QueryTokenAgent() *TokenQuery {
  84. query := (&TokenClient{config: aq.config}).Query()
  85. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  86. if err := aq.prepareQuery(ctx); err != nil {
  87. return nil, err
  88. }
  89. selector := aq.sqlQuery(ctx)
  90. if err := selector.Err(); err != nil {
  91. return nil, err
  92. }
  93. step := sqlgraph.NewStep(
  94. sqlgraph.From(agent.Table, agent.FieldID, selector),
  95. sqlgraph.To(token.Table, token.FieldID),
  96. sqlgraph.Edge(sqlgraph.O2M, false, agent.TokenAgentTable, agent.TokenAgentColumn),
  97. )
  98. fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step)
  99. return fromU, nil
  100. }
  101. return query
  102. }
  103. // QueryWaAgent chains the current query on the "wa_agent" edge.
  104. func (aq *AgentQuery) QueryWaAgent() *WhatsappQuery {
  105. query := (&WhatsappClient{config: aq.config}).Query()
  106. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  107. if err := aq.prepareQuery(ctx); err != nil {
  108. return nil, err
  109. }
  110. selector := aq.sqlQuery(ctx)
  111. if err := selector.Err(); err != nil {
  112. return nil, err
  113. }
  114. step := sqlgraph.NewStep(
  115. sqlgraph.From(agent.Table, agent.FieldID, selector),
  116. sqlgraph.To(whatsapp.Table, whatsapp.FieldID),
  117. sqlgraph.Edge(sqlgraph.O2M, false, agent.WaAgentTable, agent.WaAgentColumn),
  118. )
  119. fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step)
  120. return fromU, nil
  121. }
  122. return query
  123. }
  124. // QueryXjsAgent chains the current query on the "xjs_agent" edge.
  125. func (aq *AgentQuery) QueryXjsAgent() *XunjiServiceQuery {
  126. query := (&XunjiServiceClient{config: aq.config}).Query()
  127. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  128. if err := aq.prepareQuery(ctx); err != nil {
  129. return nil, err
  130. }
  131. selector := aq.sqlQuery(ctx)
  132. if err := selector.Err(); err != nil {
  133. return nil, err
  134. }
  135. step := sqlgraph.NewStep(
  136. sqlgraph.From(agent.Table, agent.FieldID, selector),
  137. sqlgraph.To(xunjiservice.Table, xunjiservice.FieldID),
  138. sqlgraph.Edge(sqlgraph.O2M, false, agent.XjsAgentTable, agent.XjsAgentColumn),
  139. )
  140. fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step)
  141. return fromU, nil
  142. }
  143. return query
  144. }
  145. // QueryKeyAgent chains the current query on the "key_agent" edge.
  146. func (aq *AgentQuery) QueryKeyAgent() *ApiKeyQuery {
  147. query := (&ApiKeyClient{config: aq.config}).Query()
  148. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  149. if err := aq.prepareQuery(ctx); err != nil {
  150. return nil, err
  151. }
  152. selector := aq.sqlQuery(ctx)
  153. if err := selector.Err(); err != nil {
  154. return nil, err
  155. }
  156. step := sqlgraph.NewStep(
  157. sqlgraph.From(agent.Table, agent.FieldID, selector),
  158. sqlgraph.To(apikey.Table, apikey.FieldID),
  159. sqlgraph.Edge(sqlgraph.O2M, false, agent.KeyAgentTable, agent.KeyAgentColumn),
  160. )
  161. fromU = sqlgraph.SetNeighbors(aq.driver.Dialect(), step)
  162. return fromU, nil
  163. }
  164. return query
  165. }
  166. // First returns the first Agent entity from the query.
  167. // Returns a *NotFoundError when no Agent was found.
  168. func (aq *AgentQuery) First(ctx context.Context) (*Agent, error) {
  169. nodes, err := aq.Limit(1).All(setContextOp(ctx, aq.ctx, "First"))
  170. if err != nil {
  171. return nil, err
  172. }
  173. if len(nodes) == 0 {
  174. return nil, &NotFoundError{agent.Label}
  175. }
  176. return nodes[0], nil
  177. }
  178. // FirstX is like First, but panics if an error occurs.
  179. func (aq *AgentQuery) FirstX(ctx context.Context) *Agent {
  180. node, err := aq.First(ctx)
  181. if err != nil && !IsNotFound(err) {
  182. panic(err)
  183. }
  184. return node
  185. }
  186. // FirstID returns the first Agent ID from the query.
  187. // Returns a *NotFoundError when no Agent ID was found.
  188. func (aq *AgentQuery) FirstID(ctx context.Context) (id uint64, err error) {
  189. var ids []uint64
  190. if ids, err = aq.Limit(1).IDs(setContextOp(ctx, aq.ctx, "FirstID")); err != nil {
  191. return
  192. }
  193. if len(ids) == 0 {
  194. err = &NotFoundError{agent.Label}
  195. return
  196. }
  197. return ids[0], nil
  198. }
  199. // FirstIDX is like FirstID, but panics if an error occurs.
  200. func (aq *AgentQuery) FirstIDX(ctx context.Context) uint64 {
  201. id, err := aq.FirstID(ctx)
  202. if err != nil && !IsNotFound(err) {
  203. panic(err)
  204. }
  205. return id
  206. }
  207. // Only returns a single Agent entity found by the query, ensuring it only returns one.
  208. // Returns a *NotSingularError when more than one Agent entity is found.
  209. // Returns a *NotFoundError when no Agent entities are found.
  210. func (aq *AgentQuery) Only(ctx context.Context) (*Agent, error) {
  211. nodes, err := aq.Limit(2).All(setContextOp(ctx, aq.ctx, "Only"))
  212. if err != nil {
  213. return nil, err
  214. }
  215. switch len(nodes) {
  216. case 1:
  217. return nodes[0], nil
  218. case 0:
  219. return nil, &NotFoundError{agent.Label}
  220. default:
  221. return nil, &NotSingularError{agent.Label}
  222. }
  223. }
  224. // OnlyX is like Only, but panics if an error occurs.
  225. func (aq *AgentQuery) OnlyX(ctx context.Context) *Agent {
  226. node, err := aq.Only(ctx)
  227. if err != nil {
  228. panic(err)
  229. }
  230. return node
  231. }
  232. // OnlyID is like Only, but returns the only Agent ID in the query.
  233. // Returns a *NotSingularError when more than one Agent ID is found.
  234. // Returns a *NotFoundError when no entities are found.
  235. func (aq *AgentQuery) OnlyID(ctx context.Context) (id uint64, err error) {
  236. var ids []uint64
  237. if ids, err = aq.Limit(2).IDs(setContextOp(ctx, aq.ctx, "OnlyID")); err != nil {
  238. return
  239. }
  240. switch len(ids) {
  241. case 1:
  242. id = ids[0]
  243. case 0:
  244. err = &NotFoundError{agent.Label}
  245. default:
  246. err = &NotSingularError{agent.Label}
  247. }
  248. return
  249. }
  250. // OnlyIDX is like OnlyID, but panics if an error occurs.
  251. func (aq *AgentQuery) OnlyIDX(ctx context.Context) uint64 {
  252. id, err := aq.OnlyID(ctx)
  253. if err != nil {
  254. panic(err)
  255. }
  256. return id
  257. }
  258. // All executes the query and returns a list of Agents.
  259. func (aq *AgentQuery) All(ctx context.Context) ([]*Agent, error) {
  260. ctx = setContextOp(ctx, aq.ctx, "All")
  261. if err := aq.prepareQuery(ctx); err != nil {
  262. return nil, err
  263. }
  264. qr := querierAll[[]*Agent, *AgentQuery]()
  265. return withInterceptors[[]*Agent](ctx, aq, qr, aq.inters)
  266. }
  267. // AllX is like All, but panics if an error occurs.
  268. func (aq *AgentQuery) AllX(ctx context.Context) []*Agent {
  269. nodes, err := aq.All(ctx)
  270. if err != nil {
  271. panic(err)
  272. }
  273. return nodes
  274. }
  275. // IDs executes the query and returns a list of Agent IDs.
  276. func (aq *AgentQuery) IDs(ctx context.Context) (ids []uint64, err error) {
  277. if aq.ctx.Unique == nil && aq.path != nil {
  278. aq.Unique(true)
  279. }
  280. ctx = setContextOp(ctx, aq.ctx, "IDs")
  281. if err = aq.Select(agent.FieldID).Scan(ctx, &ids); err != nil {
  282. return nil, err
  283. }
  284. return ids, nil
  285. }
  286. // IDsX is like IDs, but panics if an error occurs.
  287. func (aq *AgentQuery) IDsX(ctx context.Context) []uint64 {
  288. ids, err := aq.IDs(ctx)
  289. if err != nil {
  290. panic(err)
  291. }
  292. return ids
  293. }
  294. // Count returns the count of the given query.
  295. func (aq *AgentQuery) Count(ctx context.Context) (int, error) {
  296. ctx = setContextOp(ctx, aq.ctx, "Count")
  297. if err := aq.prepareQuery(ctx); err != nil {
  298. return 0, err
  299. }
  300. return withInterceptors[int](ctx, aq, querierCount[*AgentQuery](), aq.inters)
  301. }
  302. // CountX is like Count, but panics if an error occurs.
  303. func (aq *AgentQuery) CountX(ctx context.Context) int {
  304. count, err := aq.Count(ctx)
  305. if err != nil {
  306. panic(err)
  307. }
  308. return count
  309. }
  310. // Exist returns true if the query has elements in the graph.
  311. func (aq *AgentQuery) Exist(ctx context.Context) (bool, error) {
  312. ctx = setContextOp(ctx, aq.ctx, "Exist")
  313. switch _, err := aq.FirstID(ctx); {
  314. case IsNotFound(err):
  315. return false, nil
  316. case err != nil:
  317. return false, fmt.Errorf("ent: check existence: %w", err)
  318. default:
  319. return true, nil
  320. }
  321. }
  322. // ExistX is like Exist, but panics if an error occurs.
  323. func (aq *AgentQuery) ExistX(ctx context.Context) bool {
  324. exist, err := aq.Exist(ctx)
  325. if err != nil {
  326. panic(err)
  327. }
  328. return exist
  329. }
  330. // Clone returns a duplicate of the AgentQuery builder, including all associated steps. It can be
  331. // used to prepare common query builders and use them differently after the clone is made.
  332. func (aq *AgentQuery) Clone() *AgentQuery {
  333. if aq == nil {
  334. return nil
  335. }
  336. return &AgentQuery{
  337. config: aq.config,
  338. ctx: aq.ctx.Clone(),
  339. order: append([]agent.OrderOption{}, aq.order...),
  340. inters: append([]Interceptor{}, aq.inters...),
  341. predicates: append([]predicate.Agent{}, aq.predicates...),
  342. withWxAgent: aq.withWxAgent.Clone(),
  343. withTokenAgent: aq.withTokenAgent.Clone(),
  344. withWaAgent: aq.withWaAgent.Clone(),
  345. withXjsAgent: aq.withXjsAgent.Clone(),
  346. withKeyAgent: aq.withKeyAgent.Clone(),
  347. // clone intermediate query.
  348. sql: aq.sql.Clone(),
  349. path: aq.path,
  350. }
  351. }
  352. // WithWxAgent tells the query-builder to eager-load the nodes that are connected to
  353. // the "wx_agent" edge. The optional arguments are used to configure the query builder of the edge.
  354. func (aq *AgentQuery) WithWxAgent(opts ...func(*WxQuery)) *AgentQuery {
  355. query := (&WxClient{config: aq.config}).Query()
  356. for _, opt := range opts {
  357. opt(query)
  358. }
  359. aq.withWxAgent = query
  360. return aq
  361. }
  362. // WithTokenAgent tells the query-builder to eager-load the nodes that are connected to
  363. // the "token_agent" edge. The optional arguments are used to configure the query builder of the edge.
  364. func (aq *AgentQuery) WithTokenAgent(opts ...func(*TokenQuery)) *AgentQuery {
  365. query := (&TokenClient{config: aq.config}).Query()
  366. for _, opt := range opts {
  367. opt(query)
  368. }
  369. aq.withTokenAgent = query
  370. return aq
  371. }
  372. // WithWaAgent tells the query-builder to eager-load the nodes that are connected to
  373. // the "wa_agent" edge. The optional arguments are used to configure the query builder of the edge.
  374. func (aq *AgentQuery) WithWaAgent(opts ...func(*WhatsappQuery)) *AgentQuery {
  375. query := (&WhatsappClient{config: aq.config}).Query()
  376. for _, opt := range opts {
  377. opt(query)
  378. }
  379. aq.withWaAgent = query
  380. return aq
  381. }
  382. // WithXjsAgent tells the query-builder to eager-load the nodes that are connected to
  383. // the "xjs_agent" edge. The optional arguments are used to configure the query builder of the edge.
  384. func (aq *AgentQuery) WithXjsAgent(opts ...func(*XunjiServiceQuery)) *AgentQuery {
  385. query := (&XunjiServiceClient{config: aq.config}).Query()
  386. for _, opt := range opts {
  387. opt(query)
  388. }
  389. aq.withXjsAgent = query
  390. return aq
  391. }
  392. // WithKeyAgent tells the query-builder to eager-load the nodes that are connected to
  393. // the "key_agent" edge. The optional arguments are used to configure the query builder of the edge.
  394. func (aq *AgentQuery) WithKeyAgent(opts ...func(*ApiKeyQuery)) *AgentQuery {
  395. query := (&ApiKeyClient{config: aq.config}).Query()
  396. for _, opt := range opts {
  397. opt(query)
  398. }
  399. aq.withKeyAgent = query
  400. return aq
  401. }
  402. // GroupBy is used to group vertices by one or more fields/columns.
  403. // It is often used with aggregate functions, like: count, max, mean, min, sum.
  404. //
  405. // Example:
  406. //
  407. // var v []struct {
  408. // CreatedAt time.Time `json:"created_at,omitempty"`
  409. // Count int `json:"count,omitempty"`
  410. // }
  411. //
  412. // client.Agent.Query().
  413. // GroupBy(agent.FieldCreatedAt).
  414. // Aggregate(ent.Count()).
  415. // Scan(ctx, &v)
  416. func (aq *AgentQuery) GroupBy(field string, fields ...string) *AgentGroupBy {
  417. aq.ctx.Fields = append([]string{field}, fields...)
  418. grbuild := &AgentGroupBy{build: aq}
  419. grbuild.flds = &aq.ctx.Fields
  420. grbuild.label = agent.Label
  421. grbuild.scan = grbuild.Scan
  422. return grbuild
  423. }
  424. // Select allows the selection one or more fields/columns for the given query,
  425. // instead of selecting all fields in the entity.
  426. //
  427. // Example:
  428. //
  429. // var v []struct {
  430. // CreatedAt time.Time `json:"created_at,omitempty"`
  431. // }
  432. //
  433. // client.Agent.Query().
  434. // Select(agent.FieldCreatedAt).
  435. // Scan(ctx, &v)
  436. func (aq *AgentQuery) Select(fields ...string) *AgentSelect {
  437. aq.ctx.Fields = append(aq.ctx.Fields, fields...)
  438. sbuild := &AgentSelect{AgentQuery: aq}
  439. sbuild.label = agent.Label
  440. sbuild.flds, sbuild.scan = &aq.ctx.Fields, sbuild.Scan
  441. return sbuild
  442. }
  443. // Aggregate returns a AgentSelect configured with the given aggregations.
  444. func (aq *AgentQuery) Aggregate(fns ...AggregateFunc) *AgentSelect {
  445. return aq.Select().Aggregate(fns...)
  446. }
  447. func (aq *AgentQuery) prepareQuery(ctx context.Context) error {
  448. for _, inter := range aq.inters {
  449. if inter == nil {
  450. return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
  451. }
  452. if trv, ok := inter.(Traverser); ok {
  453. if err := trv.Traverse(ctx, aq); err != nil {
  454. return err
  455. }
  456. }
  457. }
  458. for _, f := range aq.ctx.Fields {
  459. if !agent.ValidColumn(f) {
  460. return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  461. }
  462. }
  463. if aq.path != nil {
  464. prev, err := aq.path(ctx)
  465. if err != nil {
  466. return err
  467. }
  468. aq.sql = prev
  469. }
  470. return nil
  471. }
  472. func (aq *AgentQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Agent, error) {
  473. var (
  474. nodes = []*Agent{}
  475. _spec = aq.querySpec()
  476. loadedTypes = [4]bool{
  477. aq.withWxAgent != nil,
  478. aq.withTokenAgent != nil,
  479. aq.withWaAgent != nil,
  480. aq.withXjsAgent != nil,
  481. aq.withKeyAgent != nil,
  482. }
  483. )
  484. _spec.ScanValues = func(columns []string) ([]any, error) {
  485. return (*Agent).scanValues(nil, columns)
  486. }
  487. _spec.Assign = func(columns []string, values []any) error {
  488. node := &Agent{config: aq.config}
  489. nodes = append(nodes, node)
  490. node.Edges.loadedTypes = loadedTypes
  491. return node.assignValues(columns, values)
  492. }
  493. for i := range hooks {
  494. hooks[i](ctx, _spec)
  495. }
  496. if err := sqlgraph.QueryNodes(ctx, aq.driver, _spec); err != nil {
  497. return nil, err
  498. }
  499. if len(nodes) == 0 {
  500. return nodes, nil
  501. }
  502. if query := aq.withWxAgent; query != nil {
  503. if err := aq.loadWxAgent(ctx, query, nodes,
  504. func(n *Agent) { n.Edges.WxAgent = []*Wx{} },
  505. func(n *Agent, e *Wx) { n.Edges.WxAgent = append(n.Edges.WxAgent, e) }); err != nil {
  506. return nil, err
  507. }
  508. }
  509. if query := aq.withTokenAgent; query != nil {
  510. if err := aq.loadTokenAgent(ctx, query, nodes,
  511. func(n *Agent) { n.Edges.TokenAgent = []*Token{} },
  512. func(n *Agent, e *Token) { n.Edges.TokenAgent = append(n.Edges.TokenAgent, e) }); err != nil {
  513. return nil, err
  514. }
  515. }
  516. if query := aq.withWaAgent; query != nil {
  517. if err := aq.loadWaAgent(ctx, query, nodes,
  518. func(n *Agent) { n.Edges.WaAgent = []*Whatsapp{} },
  519. func(n *Agent, e *Whatsapp) { n.Edges.WaAgent = append(n.Edges.WaAgent, e) }); err != nil {
  520. return nil, err
  521. }
  522. }
  523. if query := aq.withXjsAgent; query != nil {
  524. if err := aq.loadXjsAgent(ctx, query, nodes,
  525. func(n *Agent) { n.Edges.XjsAgent = []*XunjiService{} },
  526. func(n *Agent, e *XunjiService) { n.Edges.XjsAgent = append(n.Edges.XjsAgent, e) }); err != nil {
  527. return nil, err
  528. }
  529. }
  530. if query := aq.withKeyAgent; query != nil {
  531. if err := aq.loadKeyAgent(ctx, query, nodes,
  532. func(n *Agent) { n.Edges.KeyAgent = []*ApiKey{} },
  533. func(n *Agent, e *ApiKey) { n.Edges.KeyAgent = append(n.Edges.KeyAgent, e) }); err != nil {
  534. return nil, err
  535. }
  536. }
  537. return nodes, nil
  538. }
  539. func (aq *AgentQuery) loadWxAgent(ctx context.Context, query *WxQuery, nodes []*Agent, init func(*Agent), assign func(*Agent, *Wx)) error {
  540. fks := make([]driver.Value, 0, len(nodes))
  541. nodeids := make(map[uint64]*Agent)
  542. for i := range nodes {
  543. fks = append(fks, nodes[i].ID)
  544. nodeids[nodes[i].ID] = nodes[i]
  545. if init != nil {
  546. init(nodes[i])
  547. }
  548. }
  549. query.withFKs = true
  550. if len(query.ctx.Fields) > 0 {
  551. query.ctx.AppendFieldOnce(wx.FieldAgentID)
  552. }
  553. query.Where(predicate.Wx(func(s *sql.Selector) {
  554. s.Where(sql.InValues(s.C(agent.WxAgentColumn), fks...))
  555. }))
  556. neighbors, err := query.All(ctx)
  557. if err != nil {
  558. return err
  559. }
  560. for _, n := range neighbors {
  561. fk := n.AgentID
  562. node, ok := nodeids[fk]
  563. if !ok {
  564. return fmt.Errorf(`unexpected referenced foreign-key "agent_id" returned %v for node %v`, fk, n.ID)
  565. }
  566. assign(node, n)
  567. }
  568. return nil
  569. }
  570. func (aq *AgentQuery) loadTokenAgent(ctx context.Context, query *TokenQuery, nodes []*Agent, init func(*Agent), assign func(*Agent, *Token)) error {
  571. fks := make([]driver.Value, 0, len(nodes))
  572. nodeids := make(map[uint64]*Agent)
  573. for i := range nodes {
  574. fks = append(fks, nodes[i].ID)
  575. nodeids[nodes[i].ID] = nodes[i]
  576. if init != nil {
  577. init(nodes[i])
  578. }
  579. }
  580. if len(query.ctx.Fields) > 0 {
  581. query.ctx.AppendFieldOnce(token.FieldAgentID)
  582. }
  583. query.Where(predicate.Token(func(s *sql.Selector) {
  584. s.Where(sql.InValues(s.C(agent.TokenAgentColumn), fks...))
  585. }))
  586. neighbors, err := query.All(ctx)
  587. if err != nil {
  588. return err
  589. }
  590. for _, n := range neighbors {
  591. fk := n.AgentID
  592. node, ok := nodeids[fk]
  593. if !ok {
  594. return fmt.Errorf(`unexpected referenced foreign-key "agent_id" returned %v for node %v`, fk, n.ID)
  595. }
  596. assign(node, n)
  597. }
  598. return nil
  599. }
  600. func (aq *AgentQuery) loadWaAgent(ctx context.Context, query *WhatsappQuery, nodes []*Agent, init func(*Agent), assign func(*Agent, *Whatsapp)) error {
  601. fks := make([]driver.Value, 0, len(nodes))
  602. nodeids := make(map[uint64]*Agent)
  603. for i := range nodes {
  604. fks = append(fks, nodes[i].ID)
  605. nodeids[nodes[i].ID] = nodes[i]
  606. if init != nil {
  607. init(nodes[i])
  608. }
  609. }
  610. if len(query.ctx.Fields) > 0 {
  611. query.ctx.AppendFieldOnce(whatsapp.FieldAgentID)
  612. }
  613. query.Where(predicate.Whatsapp(func(s *sql.Selector) {
  614. s.Where(sql.InValues(s.C(agent.WaAgentColumn), fks...))
  615. }))
  616. neighbors, err := query.All(ctx)
  617. if err != nil {
  618. return err
  619. }
  620. for _, n := range neighbors {
  621. fk := n.AgentID
  622. node, ok := nodeids[fk]
  623. if !ok {
  624. return fmt.Errorf(`unexpected referenced foreign-key "agent_id" returned %v for node %v`, fk, n.ID)
  625. }
  626. assign(node, n)
  627. }
  628. return nil
  629. }
  630. func (aq *AgentQuery) loadXjsAgent(ctx context.Context, query *XunjiServiceQuery, nodes []*Agent, init func(*Agent), assign func(*Agent, *XunjiService)) error {
  631. fks := make([]driver.Value, 0, len(nodes))
  632. nodeids := make(map[uint64]*Agent)
  633. for i := range nodes {
  634. fks = append(fks, nodes[i].ID)
  635. nodeids[nodes[i].ID] = nodes[i]
  636. if init != nil {
  637. init(nodes[i])
  638. }
  639. }
  640. if len(query.ctx.Fields) > 0 {
  641. query.ctx.AppendFieldOnce(xunjiservice.FieldAgentID)
  642. }
  643. query.Where(predicate.XunjiService(func(s *sql.Selector) {
  644. s.Where(sql.InValues(s.C(agent.XjsAgentColumn), fks...))
  645. query.ctx.AppendFieldOnce(apikey.FieldAgentID)
  646. }
  647. neighbors, err := query.All(ctx)
  648. if err != nil {
  649. return err
  650. }
  651. for _, n := range neighbors {
  652. fk := n.AgentID
  653. node, ok := nodeids[fk]
  654. if !ok {
  655. return fmt.Errorf(`unexpected referenced foreign-key "agent_id" returned %v for node %v`, fk, n.ID)
  656. }
  657. assign(node, n)
  658. }
  659. return nil
  660. }
  661. func (aq *AgentQuery) loadKeyAgent(ctx context.Context, query *ApiKeyQuery, nodes []*Agent, init func(*Agent), assign func(*Agent, *ApiKey)) error {
  662. fks := make([]driver.Value, 0, len(nodes))
  663. nodeids := make(map[uint64]*Agent)
  664. for i := range nodes {
  665. fks = append(fks, nodes[i].ID)
  666. nodeids[nodes[i].ID] = nodes[i]
  667. if init != nil {
  668. init(nodes[i])
  669. }
  670. }
  671. if len(query.ctx.Fields) > 0 {
  672. query.ctx.AppendFieldOnce(apikey.FieldAgentID)
  673. }
  674. query.Where(predicate.ApiKey(func(s *sql.Selector) {
  675. s.Where(sql.InValues(s.C(agent.KeyAgentColumn), fks...))
  676. }))
  677. neighbors, err := query.All(ctx)
  678. if err != nil {
  679. return err
  680. }
  681. for _, n := range neighbors {
  682. fk := n.AgentID
  683. node, ok := nodeids[fk]
  684. if !ok {
  685. return fmt.Errorf(`unexpected referenced foreign-key "agent_id" returned %v for node %v`, fk, n.ID)
  686. }
  687. assign(node, n)
  688. }
  689. return nil
  690. }
  691. func (aq *AgentQuery) sqlCount(ctx context.Context) (int, error) {
  692. _spec := aq.querySpec()
  693. _spec.Node.Columns = aq.ctx.Fields
  694. if len(aq.ctx.Fields) > 0 {
  695. _spec.Unique = aq.ctx.Unique != nil && *aq.ctx.Unique
  696. }
  697. return sqlgraph.CountNodes(ctx, aq.driver, _spec)
  698. }
  699. func (aq *AgentQuery) querySpec() *sqlgraph.QuerySpec {
  700. _spec := sqlgraph.NewQuerySpec(agent.Table, agent.Columns, sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64))
  701. _spec.From = aq.sql
  702. if unique := aq.ctx.Unique; unique != nil {
  703. _spec.Unique = *unique
  704. } else if aq.path != nil {
  705. _spec.Unique = true
  706. }
  707. if fields := aq.ctx.Fields; len(fields) > 0 {
  708. _spec.Node.Columns = make([]string, 0, len(fields))
  709. _spec.Node.Columns = append(_spec.Node.Columns, agent.FieldID)
  710. for i := range fields {
  711. if fields[i] != agent.FieldID {
  712. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  713. }
  714. }
  715. }
  716. if ps := aq.predicates; len(ps) > 0 {
  717. _spec.Predicate = func(selector *sql.Selector) {
  718. for i := range ps {
  719. ps[i](selector)
  720. }
  721. }
  722. }
  723. if limit := aq.ctx.Limit; limit != nil {
  724. _spec.Limit = *limit
  725. }
  726. if offset := aq.ctx.Offset; offset != nil {
  727. _spec.Offset = *offset
  728. }
  729. if ps := aq.order; len(ps) > 0 {
  730. _spec.Order = func(selector *sql.Selector) {
  731. for i := range ps {
  732. ps[i](selector)
  733. }
  734. }
  735. }
  736. return _spec
  737. }
  738. func (aq *AgentQuery) sqlQuery(ctx context.Context) *sql.Selector {
  739. builder := sql.Dialect(aq.driver.Dialect())
  740. t1 := builder.Table(agent.Table)
  741. columns := aq.ctx.Fields
  742. if len(columns) == 0 {
  743. columns = agent.Columns
  744. }
  745. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  746. if aq.sql != nil {
  747. selector = aq.sql
  748. selector.Select(selector.Columns(columns...)...)
  749. }
  750. if aq.ctx.Unique != nil && *aq.ctx.Unique {
  751. selector.Distinct()
  752. }
  753. for _, p := range aq.predicates {
  754. p(selector)
  755. }
  756. for _, p := range aq.order {
  757. p(selector)
  758. }
  759. if offset := aq.ctx.Offset; offset != nil {
  760. // limit is mandatory for offset clause. We start
  761. // with default value, and override it below if needed.
  762. selector.Offset(*offset).Limit(math.MaxInt32)
  763. }
  764. if limit := aq.ctx.Limit; limit != nil {
  765. selector.Limit(*limit)
  766. }
  767. return selector
  768. }
  769. // AgentGroupBy is the group-by builder for Agent entities.
  770. type AgentGroupBy struct {
  771. selector
  772. build *AgentQuery
  773. }
  774. // Aggregate adds the given aggregation functions to the group-by query.
  775. func (agb *AgentGroupBy) Aggregate(fns ...AggregateFunc) *AgentGroupBy {
  776. agb.fns = append(agb.fns, fns...)
  777. return agb
  778. }
  779. // Scan applies the selector query and scans the result into the given value.
  780. func (agb *AgentGroupBy) Scan(ctx context.Context, v any) error {
  781. ctx = setContextOp(ctx, agb.build.ctx, "GroupBy")
  782. if err := agb.build.prepareQuery(ctx); err != nil {
  783. return err
  784. }
  785. return scanWithInterceptors[*AgentQuery, *AgentGroupBy](ctx, agb.build, agb, agb.build.inters, v)
  786. }
  787. func (agb *AgentGroupBy) sqlScan(ctx context.Context, root *AgentQuery, v any) error {
  788. selector := root.sqlQuery(ctx).Select()
  789. aggregation := make([]string, 0, len(agb.fns))
  790. for _, fn := range agb.fns {
  791. aggregation = append(aggregation, fn(selector))
  792. }
  793. if len(selector.SelectedColumns()) == 0 {
  794. columns := make([]string, 0, len(*agb.flds)+len(agb.fns))
  795. for _, f := range *agb.flds {
  796. columns = append(columns, selector.C(f))
  797. }
  798. columns = append(columns, aggregation...)
  799. selector.Select(columns...)
  800. }
  801. selector.GroupBy(selector.Columns(*agb.flds...)...)
  802. if err := selector.Err(); err != nil {
  803. return err
  804. }
  805. rows := &sql.Rows{}
  806. query, args := selector.Query()
  807. if err := agb.build.driver.Query(ctx, query, args, rows); err != nil {
  808. return err
  809. }
  810. defer rows.Close()
  811. return sql.ScanSlice(rows, v)
  812. }
  813. // AgentSelect is the builder for selecting fields of Agent entities.
  814. type AgentSelect struct {
  815. *AgentQuery
  816. selector
  817. }
  818. // Aggregate adds the given aggregation functions to the selector query.
  819. func (as *AgentSelect) Aggregate(fns ...AggregateFunc) *AgentSelect {
  820. as.fns = append(as.fns, fns...)
  821. return as
  822. }
  823. // Scan applies the selector query and scans the result into the given value.
  824. func (as *AgentSelect) Scan(ctx context.Context, v any) error {
  825. ctx = setContextOp(ctx, as.ctx, "Select")
  826. if err := as.prepareQuery(ctx); err != nil {
  827. return err
  828. }
  829. return scanWithInterceptors[*AgentQuery, *AgentSelect](ctx, as.AgentQuery, as, as.inters, v)
  830. }
  831. func (as *AgentSelect) sqlScan(ctx context.Context, root *AgentQuery, v any) error {
  832. selector := root.sqlQuery(ctx)
  833. aggregation := make([]string, 0, len(as.fns))
  834. for _, fn := range as.fns {
  835. aggregation = append(aggregation, fn(selector))
  836. }
  837. switch n := len(*as.selector.flds); {
  838. case n == 0 && len(aggregation) > 0:
  839. selector.Select(aggregation...)
  840. case n != 0 && len(aggregation) > 0:
  841. selector.AppendSelect(aggregation...)
  842. }
  843. rows := &sql.Rows{}
  844. query, args := selector.Query()
  845. if err := as.driver.Query(ctx, query, args, rows); err != nil {
  846. return err
  847. }
  848. defer rows.Close()
  849. return sql.ScanSlice(rows, v)
  850. }