agent_query.go 28 KB

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