pagination.go 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "wechat-api/ent/agent"
  7. "wechat-api/ent/batchmsg"
  8. "wechat-api/ent/contact"
  9. "wechat-api/ent/label"
  10. "wechat-api/ent/labelrelationship"
  11. "wechat-api/ent/message"
  12. "wechat-api/ent/messagerecords"
  13. "wechat-api/ent/msg"
  14. "wechat-api/ent/server"
  15. "wechat-api/ent/sopnode"
  16. "wechat-api/ent/sopstage"
  17. "wechat-api/ent/soptask"
  18. "wechat-api/ent/wx"
  19. )
  20. const errInvalidPage = "INVALID_PAGE"
  21. const (
  22. listField = "list"
  23. pageNumField = "pageNum"
  24. pageSizeField = "pageSize"
  25. )
  26. type PageDetails struct {
  27. Page uint64 `json:"page"`
  28. Size uint64 `json:"size"`
  29. Total uint64 `json:"total"`
  30. }
  31. // OrderDirection defines the directions in which to order a list of items.
  32. type OrderDirection string
  33. const (
  34. // OrderDirectionAsc specifies an ascending order.
  35. OrderDirectionAsc OrderDirection = "ASC"
  36. // OrderDirectionDesc specifies a descending order.
  37. OrderDirectionDesc OrderDirection = "DESC"
  38. )
  39. // Validate the order direction value.
  40. func (o OrderDirection) Validate() error {
  41. if o != OrderDirectionAsc && o != OrderDirectionDesc {
  42. return fmt.Errorf("%s is not a valid OrderDirection", o)
  43. }
  44. return nil
  45. }
  46. // String implements fmt.Stringer interface.
  47. func (o OrderDirection) String() string {
  48. return string(o)
  49. }
  50. func (o OrderDirection) reverse() OrderDirection {
  51. if o == OrderDirectionDesc {
  52. return OrderDirectionAsc
  53. }
  54. return OrderDirectionDesc
  55. }
  56. const errInvalidPagination = "INVALID_PAGINATION"
  57. type AgentPager struct {
  58. Order agent.OrderOption
  59. Filter func(*AgentQuery) (*AgentQuery, error)
  60. }
  61. // AgentPaginateOption enables pagination customization.
  62. type AgentPaginateOption func(*AgentPager)
  63. // DefaultAgentOrder is the default ordering of Agent.
  64. var DefaultAgentOrder = Desc(agent.FieldID)
  65. func newAgentPager(opts []AgentPaginateOption) (*AgentPager, error) {
  66. pager := &AgentPager{}
  67. for _, opt := range opts {
  68. opt(pager)
  69. }
  70. if pager.Order == nil {
  71. pager.Order = DefaultAgentOrder
  72. }
  73. return pager, nil
  74. }
  75. func (p *AgentPager) ApplyFilter(query *AgentQuery) (*AgentQuery, error) {
  76. if p.Filter != nil {
  77. return p.Filter(query)
  78. }
  79. return query, nil
  80. }
  81. // AgentPageList is Agent PageList result.
  82. type AgentPageList struct {
  83. List []*Agent `json:"list"`
  84. PageDetails *PageDetails `json:"pageDetails"`
  85. }
  86. func (a *AgentQuery) Page(
  87. ctx context.Context, pageNum uint64, pageSize uint64, opts ...AgentPaginateOption,
  88. ) (*AgentPageList, error) {
  89. pager, err := newAgentPager(opts)
  90. if err != nil {
  91. return nil, err
  92. }
  93. if a, err = pager.ApplyFilter(a); err != nil {
  94. return nil, err
  95. }
  96. ret := &AgentPageList{}
  97. ret.PageDetails = &PageDetails{
  98. Page: pageNum,
  99. Size: pageSize,
  100. }
  101. query := a.Clone()
  102. query.ctx.Fields = nil
  103. count, err := query.Count(ctx)
  104. if err != nil {
  105. return nil, err
  106. }
  107. ret.PageDetails.Total = uint64(count)
  108. if pager.Order != nil {
  109. a = a.Order(pager.Order)
  110. } else {
  111. a = a.Order(DefaultAgentOrder)
  112. }
  113. a = a.Offset(int((pageNum - 1) * pageSize)).Limit(int(pageSize))
  114. list, err := a.All(ctx)
  115. if err != nil {
  116. return nil, err
  117. }
  118. ret.List = list
  119. return ret, nil
  120. }
  121. type BatchMsgPager struct {
  122. Order batchmsg.OrderOption
  123. Filter func(*BatchMsgQuery) (*BatchMsgQuery, error)
  124. }
  125. // BatchMsgPaginateOption enables pagination customization.
  126. type BatchMsgPaginateOption func(*BatchMsgPager)
  127. // DefaultBatchMsgOrder is the default ordering of BatchMsg.
  128. var DefaultBatchMsgOrder = Desc(batchmsg.FieldID)
  129. func newBatchMsgPager(opts []BatchMsgPaginateOption) (*BatchMsgPager, error) {
  130. pager := &BatchMsgPager{}
  131. for _, opt := range opts {
  132. opt(pager)
  133. }
  134. if pager.Order == nil {
  135. pager.Order = DefaultBatchMsgOrder
  136. }
  137. return pager, nil
  138. }
  139. func (p *BatchMsgPager) ApplyFilter(query *BatchMsgQuery) (*BatchMsgQuery, error) {
  140. if p.Filter != nil {
  141. return p.Filter(query)
  142. }
  143. return query, nil
  144. }
  145. // BatchMsgPageList is BatchMsg PageList result.
  146. type BatchMsgPageList struct {
  147. List []*BatchMsg `json:"list"`
  148. PageDetails *PageDetails `json:"pageDetails"`
  149. }
  150. func (bm *BatchMsgQuery) Page(
  151. ctx context.Context, pageNum uint64, pageSize uint64, opts ...BatchMsgPaginateOption,
  152. ) (*BatchMsgPageList, error) {
  153. pager, err := newBatchMsgPager(opts)
  154. if err != nil {
  155. return nil, err
  156. }
  157. if bm, err = pager.ApplyFilter(bm); err != nil {
  158. return nil, err
  159. }
  160. ret := &BatchMsgPageList{}
  161. ret.PageDetails = &PageDetails{
  162. Page: pageNum,
  163. Size: pageSize,
  164. }
  165. query := bm.Clone()
  166. query.ctx.Fields = nil
  167. count, err := query.Count(ctx)
  168. if err != nil {
  169. return nil, err
  170. }
  171. ret.PageDetails.Total = uint64(count)
  172. if pager.Order != nil {
  173. bm = bm.Order(pager.Order)
  174. } else {
  175. bm = bm.Order(DefaultBatchMsgOrder)
  176. }
  177. bm = bm.Offset(int((pageNum - 1) * pageSize)).Limit(int(pageSize))
  178. list, err := bm.All(ctx)
  179. if err != nil {
  180. return nil, err
  181. }
  182. ret.List = list
  183. return ret, nil
  184. }
  185. type ContactPager struct {
  186. Order contact.OrderOption
  187. Filter func(*ContactQuery) (*ContactQuery, error)
  188. }
  189. // ContactPaginateOption enables pagination customization.
  190. type ContactPaginateOption func(*ContactPager)
  191. // DefaultContactOrder is the default ordering of Contact.
  192. var DefaultContactOrder = Desc(contact.FieldID)
  193. func newContactPager(opts []ContactPaginateOption) (*ContactPager, error) {
  194. pager := &ContactPager{}
  195. for _, opt := range opts {
  196. opt(pager)
  197. }
  198. if pager.Order == nil {
  199. pager.Order = DefaultContactOrder
  200. }
  201. return pager, nil
  202. }
  203. func (p *ContactPager) ApplyFilter(query *ContactQuery) (*ContactQuery, error) {
  204. if p.Filter != nil {
  205. return p.Filter(query)
  206. }
  207. return query, nil
  208. }
  209. // ContactPageList is Contact PageList result.
  210. type ContactPageList struct {
  211. List []*Contact `json:"list"`
  212. PageDetails *PageDetails `json:"pageDetails"`
  213. }
  214. func (c *ContactQuery) Page(
  215. ctx context.Context, pageNum uint64, pageSize uint64, opts ...ContactPaginateOption,
  216. ) (*ContactPageList, error) {
  217. pager, err := newContactPager(opts)
  218. if err != nil {
  219. return nil, err
  220. }
  221. if c, err = pager.ApplyFilter(c); err != nil {
  222. return nil, err
  223. }
  224. ret := &ContactPageList{}
  225. ret.PageDetails = &PageDetails{
  226. Page: pageNum,
  227. Size: pageSize,
  228. }
  229. query := c.Clone()
  230. query.ctx.Fields = nil
  231. count, err := query.Count(ctx)
  232. if err != nil {
  233. return nil, err
  234. }
  235. ret.PageDetails.Total = uint64(count)
  236. if pager.Order != nil {
  237. c = c.Order(pager.Order)
  238. } else {
  239. c = c.Order(DefaultContactOrder)
  240. }
  241. c = c.Offset(int((pageNum - 1) * pageSize)).Limit(int(pageSize))
  242. list, err := c.All(ctx)
  243. if err != nil {
  244. return nil, err
  245. }
  246. ret.List = list
  247. return ret, nil
  248. }
  249. type LabelPager struct {
  250. Order label.OrderOption
  251. Filter func(*LabelQuery) (*LabelQuery, error)
  252. }
  253. // LabelPaginateOption enables pagination customization.
  254. type LabelPaginateOption func(*LabelPager)
  255. // DefaultLabelOrder is the default ordering of Label.
  256. var DefaultLabelOrder = Desc(label.FieldID)
  257. func newLabelPager(opts []LabelPaginateOption) (*LabelPager, error) {
  258. pager := &LabelPager{}
  259. for _, opt := range opts {
  260. opt(pager)
  261. }
  262. if pager.Order == nil {
  263. pager.Order = DefaultLabelOrder
  264. }
  265. return pager, nil
  266. }
  267. func (p *LabelPager) ApplyFilter(query *LabelQuery) (*LabelQuery, error) {
  268. if p.Filter != nil {
  269. return p.Filter(query)
  270. }
  271. return query, nil
  272. }
  273. // LabelPageList is Label PageList result.
  274. type LabelPageList struct {
  275. List []*Label `json:"list"`
  276. PageDetails *PageDetails `json:"pageDetails"`
  277. }
  278. func (l *LabelQuery) Page(
  279. ctx context.Context, pageNum uint64, pageSize uint64, opts ...LabelPaginateOption,
  280. ) (*LabelPageList, error) {
  281. pager, err := newLabelPager(opts)
  282. if err != nil {
  283. return nil, err
  284. }
  285. if l, err = pager.ApplyFilter(l); err != nil {
  286. return nil, err
  287. }
  288. ret := &LabelPageList{}
  289. ret.PageDetails = &PageDetails{
  290. Page: pageNum,
  291. Size: pageSize,
  292. }
  293. query := l.Clone()
  294. query.ctx.Fields = nil
  295. count, err := query.Count(ctx)
  296. if err != nil {
  297. return nil, err
  298. }
  299. ret.PageDetails.Total = uint64(count)
  300. if pager.Order != nil {
  301. l = l.Order(pager.Order)
  302. } else {
  303. l = l.Order(DefaultLabelOrder)
  304. }
  305. l = l.Offset(int((pageNum - 1) * pageSize)).Limit(int(pageSize))
  306. list, err := l.All(ctx)
  307. if err != nil {
  308. return nil, err
  309. }
  310. ret.List = list
  311. return ret, nil
  312. }
  313. type LabelRelationshipPager struct {
  314. Order labelrelationship.OrderOption
  315. Filter func(*LabelRelationshipQuery) (*LabelRelationshipQuery, error)
  316. }
  317. // LabelRelationshipPaginateOption enables pagination customization.
  318. type LabelRelationshipPaginateOption func(*LabelRelationshipPager)
  319. // DefaultLabelRelationshipOrder is the default ordering of LabelRelationship.
  320. var DefaultLabelRelationshipOrder = Desc(labelrelationship.FieldID)
  321. func newLabelRelationshipPager(opts []LabelRelationshipPaginateOption) (*LabelRelationshipPager, error) {
  322. pager := &LabelRelationshipPager{}
  323. for _, opt := range opts {
  324. opt(pager)
  325. }
  326. if pager.Order == nil {
  327. pager.Order = DefaultLabelRelationshipOrder
  328. }
  329. return pager, nil
  330. }
  331. func (p *LabelRelationshipPager) ApplyFilter(query *LabelRelationshipQuery) (*LabelRelationshipQuery, error) {
  332. if p.Filter != nil {
  333. return p.Filter(query)
  334. }
  335. return query, nil
  336. }
  337. // LabelRelationshipPageList is LabelRelationship PageList result.
  338. type LabelRelationshipPageList struct {
  339. List []*LabelRelationship `json:"list"`
  340. PageDetails *PageDetails `json:"pageDetails"`
  341. }
  342. func (lr *LabelRelationshipQuery) Page(
  343. ctx context.Context, pageNum uint64, pageSize uint64, opts ...LabelRelationshipPaginateOption,
  344. ) (*LabelRelationshipPageList, error) {
  345. pager, err := newLabelRelationshipPager(opts)
  346. if err != nil {
  347. return nil, err
  348. }
  349. if lr, err = pager.ApplyFilter(lr); err != nil {
  350. return nil, err
  351. }
  352. ret := &LabelRelationshipPageList{}
  353. ret.PageDetails = &PageDetails{
  354. Page: pageNum,
  355. Size: pageSize,
  356. }
  357. query := lr.Clone()
  358. query.ctx.Fields = nil
  359. count, err := query.Count(ctx)
  360. if err != nil {
  361. return nil, err
  362. }
  363. ret.PageDetails.Total = uint64(count)
  364. if pager.Order != nil {
  365. lr = lr.Order(pager.Order)
  366. } else {
  367. lr = lr.Order(DefaultLabelRelationshipOrder)
  368. }
  369. lr = lr.Offset(int((pageNum - 1) * pageSize)).Limit(int(pageSize))
  370. list, err := lr.All(ctx)
  371. if err != nil {
  372. return nil, err
  373. }
  374. ret.List = list
  375. return ret, nil
  376. }
  377. type MessagePager struct {
  378. Order message.OrderOption
  379. Filter func(*MessageQuery) (*MessageQuery, error)
  380. }
  381. // MessagePaginateOption enables pagination customization.
  382. type MessagePaginateOption func(*MessagePager)
  383. // DefaultMessageOrder is the default ordering of Message.
  384. var DefaultMessageOrder = Desc(message.FieldID)
  385. func newMessagePager(opts []MessagePaginateOption) (*MessagePager, error) {
  386. pager := &MessagePager{}
  387. for _, opt := range opts {
  388. opt(pager)
  389. }
  390. if pager.Order == nil {
  391. pager.Order = DefaultMessageOrder
  392. }
  393. return pager, nil
  394. }
  395. func (p *MessagePager) ApplyFilter(query *MessageQuery) (*MessageQuery, error) {
  396. if p.Filter != nil {
  397. return p.Filter(query)
  398. }
  399. return query, nil
  400. }
  401. // MessagePageList is Message PageList result.
  402. type MessagePageList struct {
  403. List []*Message `json:"list"`
  404. PageDetails *PageDetails `json:"pageDetails"`
  405. }
  406. func (m *MessageQuery) Page(
  407. ctx context.Context, pageNum uint64, pageSize uint64, opts ...MessagePaginateOption,
  408. ) (*MessagePageList, error) {
  409. pager, err := newMessagePager(opts)
  410. if err != nil {
  411. return nil, err
  412. }
  413. if m, err = pager.ApplyFilter(m); err != nil {
  414. return nil, err
  415. }
  416. ret := &MessagePageList{}
  417. ret.PageDetails = &PageDetails{
  418. Page: pageNum,
  419. Size: pageSize,
  420. }
  421. query := m.Clone()
  422. query.ctx.Fields = nil
  423. count, err := query.Count(ctx)
  424. if err != nil {
  425. return nil, err
  426. }
  427. ret.PageDetails.Total = uint64(count)
  428. if pager.Order != nil {
  429. m = m.Order(pager.Order)
  430. } else {
  431. m = m.Order(DefaultMessageOrder)
  432. }
  433. m = m.Offset(int((pageNum - 1) * pageSize)).Limit(int(pageSize))
  434. list, err := m.All(ctx)
  435. if err != nil {
  436. return nil, err
  437. }
  438. ret.List = list
  439. return ret, nil
  440. }
  441. type MessageRecordsPager struct {
  442. Order messagerecords.OrderOption
  443. Filter func(*MessageRecordsQuery) (*MessageRecordsQuery, error)
  444. }
  445. // MessageRecordsPaginateOption enables pagination customization.
  446. type MessageRecordsPaginateOption func(*MessageRecordsPager)
  447. // DefaultMessageRecordsOrder is the default ordering of MessageRecords.
  448. var DefaultMessageRecordsOrder = Desc(messagerecords.FieldID)
  449. func newMessageRecordsPager(opts []MessageRecordsPaginateOption) (*MessageRecordsPager, error) {
  450. pager := &MessageRecordsPager{}
  451. for _, opt := range opts {
  452. opt(pager)
  453. }
  454. if pager.Order == nil {
  455. pager.Order = DefaultMessageRecordsOrder
  456. }
  457. return pager, nil
  458. }
  459. func (p *MessageRecordsPager) ApplyFilter(query *MessageRecordsQuery) (*MessageRecordsQuery, error) {
  460. if p.Filter != nil {
  461. return p.Filter(query)
  462. }
  463. return query, nil
  464. }
  465. // MessageRecordsPageList is MessageRecords PageList result.
  466. type MessageRecordsPageList struct {
  467. List []*MessageRecords `json:"list"`
  468. PageDetails *PageDetails `json:"pageDetails"`
  469. }
  470. func (mr *MessageRecordsQuery) Page(
  471. ctx context.Context, pageNum uint64, pageSize uint64, opts ...MessageRecordsPaginateOption,
  472. ) (*MessageRecordsPageList, error) {
  473. pager, err := newMessageRecordsPager(opts)
  474. if err != nil {
  475. return nil, err
  476. }
  477. if mr, err = pager.ApplyFilter(mr); err != nil {
  478. return nil, err
  479. }
  480. ret := &MessageRecordsPageList{}
  481. ret.PageDetails = &PageDetails{
  482. Page: pageNum,
  483. Size: pageSize,
  484. }
  485. query := mr.Clone()
  486. query.ctx.Fields = nil
  487. count, err := query.Count(ctx)
  488. if err != nil {
  489. return nil, err
  490. }
  491. ret.PageDetails.Total = uint64(count)
  492. if pager.Order != nil {
  493. mr = mr.Order(pager.Order)
  494. } else {
  495. mr = mr.Order(DefaultMessageRecordsOrder)
  496. }
  497. mr = mr.Offset(int((pageNum - 1) * pageSize)).Limit(int(pageSize))
  498. list, err := mr.All(ctx)
  499. if err != nil {
  500. return nil, err
  501. }
  502. ret.List = list
  503. return ret, nil
  504. }
  505. type MsgPager struct {
  506. Order msg.OrderOption
  507. Filter func(*MsgQuery) (*MsgQuery, error)
  508. }
  509. // MsgPaginateOption enables pagination customization.
  510. type MsgPaginateOption func(*MsgPager)
  511. // DefaultMsgOrder is the default ordering of Msg.
  512. var DefaultMsgOrder = Desc(msg.FieldID)
  513. func newMsgPager(opts []MsgPaginateOption) (*MsgPager, error) {
  514. pager := &MsgPager{}
  515. for _, opt := range opts {
  516. opt(pager)
  517. }
  518. if pager.Order == nil {
  519. pager.Order = DefaultMsgOrder
  520. }
  521. return pager, nil
  522. }
  523. func (p *MsgPager) ApplyFilter(query *MsgQuery) (*MsgQuery, error) {
  524. if p.Filter != nil {
  525. return p.Filter(query)
  526. }
  527. return query, nil
  528. }
  529. // MsgPageList is Msg PageList result.
  530. type MsgPageList struct {
  531. List []*Msg `json:"list"`
  532. PageDetails *PageDetails `json:"pageDetails"`
  533. }
  534. func (m *MsgQuery) Page(
  535. ctx context.Context, pageNum uint64, pageSize uint64, opts ...MsgPaginateOption,
  536. ) (*MsgPageList, error) {
  537. pager, err := newMsgPager(opts)
  538. if err != nil {
  539. return nil, err
  540. }
  541. if m, err = pager.ApplyFilter(m); err != nil {
  542. return nil, err
  543. }
  544. ret := &MsgPageList{}
  545. ret.PageDetails = &PageDetails{
  546. Page: pageNum,
  547. Size: pageSize,
  548. }
  549. query := m.Clone()
  550. query.ctx.Fields = nil
  551. count, err := query.Count(ctx)
  552. if err != nil {
  553. return nil, err
  554. }
  555. ret.PageDetails.Total = uint64(count)
  556. if pager.Order != nil {
  557. m = m.Order(pager.Order)
  558. } else {
  559. m = m.Order(DefaultMsgOrder)
  560. }
  561. m = m.Offset(int((pageNum - 1) * pageSize)).Limit(int(pageSize))
  562. list, err := m.All(ctx)
  563. if err != nil {
  564. return nil, err
  565. }
  566. ret.List = list
  567. return ret, nil
  568. }
  569. type ServerPager struct {
  570. Order server.OrderOption
  571. Filter func(*ServerQuery) (*ServerQuery, error)
  572. }
  573. // ServerPaginateOption enables pagination customization.
  574. type ServerPaginateOption func(*ServerPager)
  575. // DefaultServerOrder is the default ordering of Server.
  576. var DefaultServerOrder = Desc(server.FieldID)
  577. func newServerPager(opts []ServerPaginateOption) (*ServerPager, error) {
  578. pager := &ServerPager{}
  579. for _, opt := range opts {
  580. opt(pager)
  581. }
  582. if pager.Order == nil {
  583. pager.Order = DefaultServerOrder
  584. }
  585. return pager, nil
  586. }
  587. func (p *ServerPager) ApplyFilter(query *ServerQuery) (*ServerQuery, error) {
  588. if p.Filter != nil {
  589. return p.Filter(query)
  590. }
  591. return query, nil
  592. }
  593. // ServerPageList is Server PageList result.
  594. type ServerPageList struct {
  595. List []*Server `json:"list"`
  596. PageDetails *PageDetails `json:"pageDetails"`
  597. }
  598. func (s *ServerQuery) Page(
  599. ctx context.Context, pageNum uint64, pageSize uint64, opts ...ServerPaginateOption,
  600. ) (*ServerPageList, error) {
  601. pager, err := newServerPager(opts)
  602. if err != nil {
  603. return nil, err
  604. }
  605. if s, err = pager.ApplyFilter(s); err != nil {
  606. return nil, err
  607. }
  608. ret := &ServerPageList{}
  609. ret.PageDetails = &PageDetails{
  610. Page: pageNum,
  611. Size: pageSize,
  612. }
  613. query := s.Clone()
  614. query.ctx.Fields = nil
  615. count, err := query.Count(ctx)
  616. if err != nil {
  617. return nil, err
  618. }
  619. ret.PageDetails.Total = uint64(count)
  620. if pager.Order != nil {
  621. s = s.Order(pager.Order)
  622. } else {
  623. s = s.Order(DefaultServerOrder)
  624. }
  625. s = s.Offset(int((pageNum - 1) * pageSize)).Limit(int(pageSize))
  626. list, err := s.All(ctx)
  627. if err != nil {
  628. return nil, err
  629. }
  630. ret.List = list
  631. return ret, nil
  632. }
  633. type SopNodePager struct {
  634. Order sopnode.OrderOption
  635. Filter func(*SopNodeQuery) (*SopNodeQuery, error)
  636. }
  637. // SopNodePaginateOption enables pagination customization.
  638. type SopNodePaginateOption func(*SopNodePager)
  639. // DefaultSopNodeOrder is the default ordering of SopNode.
  640. var DefaultSopNodeOrder = Desc(sopnode.FieldID)
  641. func newSopNodePager(opts []SopNodePaginateOption) (*SopNodePager, error) {
  642. pager := &SopNodePager{}
  643. for _, opt := range opts {
  644. opt(pager)
  645. }
  646. if pager.Order == nil {
  647. pager.Order = DefaultSopNodeOrder
  648. }
  649. return pager, nil
  650. }
  651. func (p *SopNodePager) ApplyFilter(query *SopNodeQuery) (*SopNodeQuery, error) {
  652. if p.Filter != nil {
  653. return p.Filter(query)
  654. }
  655. return query, nil
  656. }
  657. // SopNodePageList is SopNode PageList result.
  658. type SopNodePageList struct {
  659. List []*SopNode `json:"list"`
  660. PageDetails *PageDetails `json:"pageDetails"`
  661. }
  662. func (sn *SopNodeQuery) Page(
  663. ctx context.Context, pageNum uint64, pageSize uint64, opts ...SopNodePaginateOption,
  664. ) (*SopNodePageList, error) {
  665. pager, err := newSopNodePager(opts)
  666. if err != nil {
  667. return nil, err
  668. }
  669. if sn, err = pager.ApplyFilter(sn); err != nil {
  670. return nil, err
  671. }
  672. ret := &SopNodePageList{}
  673. ret.PageDetails = &PageDetails{
  674. Page: pageNum,
  675. Size: pageSize,
  676. }
  677. query := sn.Clone()
  678. query.ctx.Fields = nil
  679. count, err := query.Count(ctx)
  680. if err != nil {
  681. return nil, err
  682. }
  683. ret.PageDetails.Total = uint64(count)
  684. if pager.Order != nil {
  685. sn = sn.Order(pager.Order)
  686. } else {
  687. sn = sn.Order(DefaultSopNodeOrder)
  688. }
  689. sn = sn.Offset(int((pageNum - 1) * pageSize)).Limit(int(pageSize))
  690. list, err := sn.All(ctx)
  691. if err != nil {
  692. return nil, err
  693. }
  694. ret.List = list
  695. return ret, nil
  696. }
  697. type SopStagePager struct {
  698. Order sopstage.OrderOption
  699. Filter func(*SopStageQuery) (*SopStageQuery, error)
  700. }
  701. // SopStagePaginateOption enables pagination customization.
  702. type SopStagePaginateOption func(*SopStagePager)
  703. // DefaultSopStageOrder is the default ordering of SopStage.
  704. var DefaultSopStageOrder = Desc(sopstage.FieldID)
  705. func newSopStagePager(opts []SopStagePaginateOption) (*SopStagePager, error) {
  706. pager := &SopStagePager{}
  707. for _, opt := range opts {
  708. opt(pager)
  709. }
  710. if pager.Order == nil {
  711. pager.Order = DefaultSopStageOrder
  712. }
  713. return pager, nil
  714. }
  715. func (p *SopStagePager) ApplyFilter(query *SopStageQuery) (*SopStageQuery, error) {
  716. if p.Filter != nil {
  717. return p.Filter(query)
  718. }
  719. return query, nil
  720. }
  721. // SopStagePageList is SopStage PageList result.
  722. type SopStagePageList struct {
  723. List []*SopStage `json:"list"`
  724. PageDetails *PageDetails `json:"pageDetails"`
  725. }
  726. func (ss *SopStageQuery) Page(
  727. ctx context.Context, pageNum uint64, pageSize uint64, opts ...SopStagePaginateOption,
  728. ) (*SopStagePageList, error) {
  729. pager, err := newSopStagePager(opts)
  730. if err != nil {
  731. return nil, err
  732. }
  733. if ss, err = pager.ApplyFilter(ss); err != nil {
  734. return nil, err
  735. }
  736. ret := &SopStagePageList{}
  737. ret.PageDetails = &PageDetails{
  738. Page: pageNum,
  739. Size: pageSize,
  740. }
  741. query := ss.Clone()
  742. query.ctx.Fields = nil
  743. count, err := query.Count(ctx)
  744. if err != nil {
  745. return nil, err
  746. }
  747. ret.PageDetails.Total = uint64(count)
  748. if pager.Order != nil {
  749. ss = ss.Order(pager.Order)
  750. } else {
  751. ss = ss.Order(DefaultSopStageOrder)
  752. }
  753. ss = ss.Offset(int((pageNum - 1) * pageSize)).Limit(int(pageSize))
  754. list, err := ss.All(ctx)
  755. if err != nil {
  756. return nil, err
  757. }
  758. ret.List = list
  759. return ret, nil
  760. }
  761. type SopTaskPager struct {
  762. Order soptask.OrderOption
  763. Filter func(*SopTaskQuery) (*SopTaskQuery, error)
  764. }
  765. // SopTaskPaginateOption enables pagination customization.
  766. type SopTaskPaginateOption func(*SopTaskPager)
  767. // DefaultSopTaskOrder is the default ordering of SopTask.
  768. var DefaultSopTaskOrder = Desc(soptask.FieldID)
  769. func newSopTaskPager(opts []SopTaskPaginateOption) (*SopTaskPager, error) {
  770. pager := &SopTaskPager{}
  771. for _, opt := range opts {
  772. opt(pager)
  773. }
  774. if pager.Order == nil {
  775. pager.Order = DefaultSopTaskOrder
  776. }
  777. return pager, nil
  778. }
  779. func (p *SopTaskPager) ApplyFilter(query *SopTaskQuery) (*SopTaskQuery, error) {
  780. if p.Filter != nil {
  781. return p.Filter(query)
  782. }
  783. return query, nil
  784. }
  785. // SopTaskPageList is SopTask PageList result.
  786. type SopTaskPageList struct {
  787. List []*SopTask `json:"list"`
  788. PageDetails *PageDetails `json:"pageDetails"`
  789. }
  790. func (st *SopTaskQuery) Page(
  791. ctx context.Context, pageNum uint64, pageSize uint64, opts ...SopTaskPaginateOption,
  792. ) (*SopTaskPageList, error) {
  793. pager, err := newSopTaskPager(opts)
  794. if err != nil {
  795. return nil, err
  796. }
  797. if st, err = pager.ApplyFilter(st); err != nil {
  798. return nil, err
  799. }
  800. ret := &SopTaskPageList{}
  801. ret.PageDetails = &PageDetails{
  802. Page: pageNum,
  803. Size: pageSize,
  804. }
  805. query := st.Clone()
  806. query.ctx.Fields = nil
  807. count, err := query.Count(ctx)
  808. if err != nil {
  809. return nil, err
  810. }
  811. ret.PageDetails.Total = uint64(count)
  812. if pager.Order != nil {
  813. st = st.Order(pager.Order)
  814. } else {
  815. st = st.Order(DefaultSopTaskOrder)
  816. }
  817. st = st.Offset(int((pageNum - 1) * pageSize)).Limit(int(pageSize))
  818. list, err := st.All(ctx)
  819. if err != nil {
  820. return nil, err
  821. }
  822. ret.List = list
  823. return ret, nil
  824. }
  825. type WxPager struct {
  826. Order wx.OrderOption
  827. Filter func(*WxQuery) (*WxQuery, error)
  828. }
  829. // WxPaginateOption enables pagination customization.
  830. type WxPaginateOption func(*WxPager)
  831. // DefaultWxOrder is the default ordering of Wx.
  832. var DefaultWxOrder = Desc(wx.FieldID)
  833. func newWxPager(opts []WxPaginateOption) (*WxPager, error) {
  834. pager := &WxPager{}
  835. for _, opt := range opts {
  836. opt(pager)
  837. }
  838. if pager.Order == nil {
  839. pager.Order = DefaultWxOrder
  840. }
  841. return pager, nil
  842. }
  843. func (p *WxPager) ApplyFilter(query *WxQuery) (*WxQuery, error) {
  844. if p.Filter != nil {
  845. return p.Filter(query)
  846. }
  847. return query, nil
  848. }
  849. // WxPageList is Wx PageList result.
  850. type WxPageList struct {
  851. List []*Wx `json:"list"`
  852. PageDetails *PageDetails `json:"pageDetails"`
  853. }
  854. func (w *WxQuery) Page(
  855. ctx context.Context, pageNum uint64, pageSize uint64, opts ...WxPaginateOption,
  856. ) (*WxPageList, error) {
  857. pager, err := newWxPager(opts)
  858. if err != nil {
  859. return nil, err
  860. }
  861. if w, err = pager.ApplyFilter(w); err != nil {
  862. return nil, err
  863. }
  864. ret := &WxPageList{}
  865. ret.PageDetails = &PageDetails{
  866. Page: pageNum,
  867. Size: pageSize,
  868. }
  869. query := w.Clone()
  870. query.ctx.Fields = nil
  871. count, err := query.Count(ctx)
  872. if err != nil {
  873. return nil, err
  874. }
  875. ret.PageDetails.Total = uint64(count)
  876. if pager.Order != nil {
  877. w = w.Order(pager.Order)
  878. } else {
  879. w = w.Order(DefaultWxOrder)
  880. }
  881. w = w.Offset(int((pageNum - 1) * pageSize)).Limit(int(pageSize))
  882. list, err := w.All(ctx)
  883. if err != nil {
  884. return nil, err
  885. }
  886. ret.List = list
  887. return ret, nil
  888. }