agent_update.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "wechat-api/ent/agent"
  9. "wechat-api/ent/predicate"
  10. "wechat-api/ent/wx"
  11. "entgo.io/ent/dialect/sql"
  12. "entgo.io/ent/dialect/sql/sqlgraph"
  13. "entgo.io/ent/schema/field"
  14. )
  15. // AgentUpdate is the builder for updating Agent entities.
  16. type AgentUpdate struct {
  17. config
  18. hooks []Hook
  19. mutation *AgentMutation
  20. }
  21. // Where appends a list predicates to the AgentUpdate builder.
  22. func (au *AgentUpdate) Where(ps ...predicate.Agent) *AgentUpdate {
  23. au.mutation.Where(ps...)
  24. return au
  25. }
  26. // SetUpdatedAt sets the "updated_at" field.
  27. func (au *AgentUpdate) SetUpdatedAt(t time.Time) *AgentUpdate {
  28. au.mutation.SetUpdatedAt(t)
  29. return au
  30. }
  31. // SetDeletedAt sets the "deleted_at" field.
  32. func (au *AgentUpdate) SetDeletedAt(t time.Time) *AgentUpdate {
  33. au.mutation.SetDeletedAt(t)
  34. return au
  35. }
  36. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  37. func (au *AgentUpdate) SetNillableDeletedAt(t *time.Time) *AgentUpdate {
  38. if t != nil {
  39. au.SetDeletedAt(*t)
  40. }
  41. return au
  42. }
  43. // ClearDeletedAt clears the value of the "deleted_at" field.
  44. func (au *AgentUpdate) ClearDeletedAt() *AgentUpdate {
  45. au.mutation.ClearDeletedAt()
  46. return au
  47. }
  48. // SetName sets the "name" field.
  49. func (au *AgentUpdate) SetName(s string) *AgentUpdate {
  50. au.mutation.SetName(s)
  51. return au
  52. }
  53. // SetNillableName sets the "name" field if the given value is not nil.
  54. func (au *AgentUpdate) SetNillableName(s *string) *AgentUpdate {
  55. if s != nil {
  56. au.SetName(*s)
  57. }
  58. return au
  59. }
  60. // SetRole sets the "role" field.
  61. func (au *AgentUpdate) SetRole(s string) *AgentUpdate {
  62. au.mutation.SetRole(s)
  63. return au
  64. }
  65. // SetNillableRole sets the "role" field if the given value is not nil.
  66. func (au *AgentUpdate) SetNillableRole(s *string) *AgentUpdate {
  67. if s != nil {
  68. au.SetRole(*s)
  69. }
  70. return au
  71. }
  72. // SetStatus sets the "status" field.
  73. func (au *AgentUpdate) SetStatus(i int) *AgentUpdate {
  74. au.mutation.ResetStatus()
  75. au.mutation.SetStatus(i)
  76. return au
  77. }
  78. // SetNillableStatus sets the "status" field if the given value is not nil.
  79. func (au *AgentUpdate) SetNillableStatus(i *int) *AgentUpdate {
  80. if i != nil {
  81. au.SetStatus(*i)
  82. }
  83. return au
  84. }
  85. // AddStatus adds i to the "status" field.
  86. func (au *AgentUpdate) AddStatus(i int) *AgentUpdate {
  87. au.mutation.AddStatus(i)
  88. return au
  89. }
  90. // ClearStatus clears the value of the "status" field.
  91. func (au *AgentUpdate) ClearStatus() *AgentUpdate {
  92. au.mutation.ClearStatus()
  93. return au
  94. }
  95. // SetBackground sets the "background" field.
  96. func (au *AgentUpdate) SetBackground(s string) *AgentUpdate {
  97. au.mutation.SetBackground(s)
  98. return au
  99. }
  100. // SetNillableBackground sets the "background" field if the given value is not nil.
  101. func (au *AgentUpdate) SetNillableBackground(s *string) *AgentUpdate {
  102. if s != nil {
  103. au.SetBackground(*s)
  104. }
  105. return au
  106. }
  107. // ClearBackground clears the value of the "background" field.
  108. func (au *AgentUpdate) ClearBackground() *AgentUpdate {
  109. au.mutation.ClearBackground()
  110. return au
  111. }
  112. // SetExamples sets the "examples" field.
  113. func (au *AgentUpdate) SetExamples(s string) *AgentUpdate {
  114. au.mutation.SetExamples(s)
  115. return au
  116. }
  117. // SetNillableExamples sets the "examples" field if the given value is not nil.
  118. func (au *AgentUpdate) SetNillableExamples(s *string) *AgentUpdate {
  119. if s != nil {
  120. au.SetExamples(*s)
  121. }
  122. return au
  123. }
  124. // ClearExamples clears the value of the "examples" field.
  125. func (au *AgentUpdate) ClearExamples() *AgentUpdate {
  126. au.mutation.ClearExamples()
  127. return au
  128. }
  129. // SetOrganizationID sets the "organization_id" field.
  130. func (au *AgentUpdate) SetOrganizationID(u uint64) *AgentUpdate {
  131. au.mutation.ResetOrganizationID()
  132. au.mutation.SetOrganizationID(u)
  133. return au
  134. }
  135. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  136. func (au *AgentUpdate) SetNillableOrganizationID(u *uint64) *AgentUpdate {
  137. if u != nil {
  138. au.SetOrganizationID(*u)
  139. }
  140. return au
  141. }
  142. // AddOrganizationID adds u to the "organization_id" field.
  143. func (au *AgentUpdate) AddOrganizationID(u int64) *AgentUpdate {
  144. au.mutation.AddOrganizationID(u)
  145. return au
  146. }
  147. // SetDatasetID sets the "dataset_id" field.
  148. func (au *AgentUpdate) SetDatasetID(s string) *AgentUpdate {
  149. au.mutation.SetDatasetID(s)
  150. return au
  151. }
  152. // SetNillableDatasetID sets the "dataset_id" field if the given value is not nil.
  153. func (au *AgentUpdate) SetNillableDatasetID(s *string) *AgentUpdate {
  154. if s != nil {
  155. au.SetDatasetID(*s)
  156. }
  157. return au
  158. }
  159. // SetCollectionID sets the "collection_id" field.
  160. func (au *AgentUpdate) SetCollectionID(s string) *AgentUpdate {
  161. au.mutation.SetCollectionID(s)
  162. return au
  163. }
  164. // SetNillableCollectionID sets the "collection_id" field if the given value is not nil.
  165. func (au *AgentUpdate) SetNillableCollectionID(s *string) *AgentUpdate {
  166. if s != nil {
  167. au.SetCollectionID(*s)
  168. }
  169. return au
  170. }
  171. // AddWxAgentIDs adds the "wx_agent" edge to the Wx entity by IDs.
  172. func (au *AgentUpdate) AddWxAgentIDs(ids ...uint64) *AgentUpdate {
  173. au.mutation.AddWxAgentIDs(ids...)
  174. return au
  175. }
  176. // AddWxAgent adds the "wx_agent" edges to the Wx entity.
  177. func (au *AgentUpdate) AddWxAgent(w ...*Wx) *AgentUpdate {
  178. ids := make([]uint64, len(w))
  179. for i := range w {
  180. ids[i] = w[i].ID
  181. }
  182. return au.AddWxAgentIDs(ids...)
  183. }
  184. // Mutation returns the AgentMutation object of the builder.
  185. func (au *AgentUpdate) Mutation() *AgentMutation {
  186. return au.mutation
  187. }
  188. // ClearWxAgent clears all "wx_agent" edges to the Wx entity.
  189. func (au *AgentUpdate) ClearWxAgent() *AgentUpdate {
  190. au.mutation.ClearWxAgent()
  191. return au
  192. }
  193. // RemoveWxAgentIDs removes the "wx_agent" edge to Wx entities by IDs.
  194. func (au *AgentUpdate) RemoveWxAgentIDs(ids ...uint64) *AgentUpdate {
  195. au.mutation.RemoveWxAgentIDs(ids...)
  196. return au
  197. }
  198. // RemoveWxAgent removes "wx_agent" edges to Wx entities.
  199. func (au *AgentUpdate) RemoveWxAgent(w ...*Wx) *AgentUpdate {
  200. ids := make([]uint64, len(w))
  201. for i := range w {
  202. ids[i] = w[i].ID
  203. }
  204. return au.RemoveWxAgentIDs(ids...)
  205. }
  206. // Save executes the query and returns the number of nodes affected by the update operation.
  207. func (au *AgentUpdate) Save(ctx context.Context) (int, error) {
  208. if err := au.defaults(); err != nil {
  209. return 0, err
  210. }
  211. return withHooks(ctx, au.sqlSave, au.mutation, au.hooks)
  212. }
  213. // SaveX is like Save, but panics if an error occurs.
  214. func (au *AgentUpdate) SaveX(ctx context.Context) int {
  215. affected, err := au.Save(ctx)
  216. if err != nil {
  217. panic(err)
  218. }
  219. return affected
  220. }
  221. // Exec executes the query.
  222. func (au *AgentUpdate) Exec(ctx context.Context) error {
  223. _, err := au.Save(ctx)
  224. return err
  225. }
  226. // ExecX is like Exec, but panics if an error occurs.
  227. func (au *AgentUpdate) ExecX(ctx context.Context) {
  228. if err := au.Exec(ctx); err != nil {
  229. panic(err)
  230. }
  231. }
  232. // defaults sets the default values of the builder before save.
  233. func (au *AgentUpdate) defaults() error {
  234. if _, ok := au.mutation.UpdatedAt(); !ok {
  235. if agent.UpdateDefaultUpdatedAt == nil {
  236. return fmt.Errorf("ent: uninitialized agent.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  237. }
  238. v := agent.UpdateDefaultUpdatedAt()
  239. au.mutation.SetUpdatedAt(v)
  240. }
  241. return nil
  242. }
  243. // check runs all checks and user-defined validators on the builder.
  244. func (au *AgentUpdate) check() error {
  245. if v, ok := au.mutation.Name(); ok {
  246. if err := agent.NameValidator(v); err != nil {
  247. return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Agent.name": %w`, err)}
  248. }
  249. }
  250. if v, ok := au.mutation.Role(); ok {
  251. if err := agent.RoleValidator(v); err != nil {
  252. return &ValidationError{Name: "role", err: fmt.Errorf(`ent: validator failed for field "Agent.role": %w`, err)}
  253. }
  254. }
  255. if v, ok := au.mutation.Status(); ok {
  256. if err := agent.StatusValidator(v); err != nil {
  257. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Agent.status": %w`, err)}
  258. }
  259. }
  260. if v, ok := au.mutation.Background(); ok {
  261. if err := agent.BackgroundValidator(v); err != nil {
  262. return &ValidationError{Name: "background", err: fmt.Errorf(`ent: validator failed for field "Agent.background": %w`, err)}
  263. }
  264. }
  265. if v, ok := au.mutation.Examples(); ok {
  266. if err := agent.ExamplesValidator(v); err != nil {
  267. return &ValidationError{Name: "examples", err: fmt.Errorf(`ent: validator failed for field "Agent.examples": %w`, err)}
  268. }
  269. }
  270. if v, ok := au.mutation.OrganizationID(); ok {
  271. if err := agent.OrganizationIDValidator(v); err != nil {
  272. return &ValidationError{Name: "organization_id", err: fmt.Errorf(`ent: validator failed for field "Agent.organization_id": %w`, err)}
  273. }
  274. }
  275. if v, ok := au.mutation.DatasetID(); ok {
  276. if err := agent.DatasetIDValidator(v); err != nil {
  277. return &ValidationError{Name: "dataset_id", err: fmt.Errorf(`ent: validator failed for field "Agent.dataset_id": %w`, err)}
  278. }
  279. }
  280. if v, ok := au.mutation.CollectionID(); ok {
  281. if err := agent.CollectionIDValidator(v); err != nil {
  282. return &ValidationError{Name: "collection_id", err: fmt.Errorf(`ent: validator failed for field "Agent.collection_id": %w`, err)}
  283. }
  284. }
  285. return nil
  286. }
  287. func (au *AgentUpdate) sqlSave(ctx context.Context) (n int, err error) {
  288. if err := au.check(); err != nil {
  289. return n, err
  290. }
  291. _spec := sqlgraph.NewUpdateSpec(agent.Table, agent.Columns, sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64))
  292. if ps := au.mutation.predicates; len(ps) > 0 {
  293. _spec.Predicate = func(selector *sql.Selector) {
  294. for i := range ps {
  295. ps[i](selector)
  296. }
  297. }
  298. }
  299. if value, ok := au.mutation.UpdatedAt(); ok {
  300. _spec.SetField(agent.FieldUpdatedAt, field.TypeTime, value)
  301. }
  302. if value, ok := au.mutation.DeletedAt(); ok {
  303. _spec.SetField(agent.FieldDeletedAt, field.TypeTime, value)
  304. }
  305. if au.mutation.DeletedAtCleared() {
  306. _spec.ClearField(agent.FieldDeletedAt, field.TypeTime)
  307. }
  308. if value, ok := au.mutation.Name(); ok {
  309. _spec.SetField(agent.FieldName, field.TypeString, value)
  310. }
  311. if value, ok := au.mutation.Role(); ok {
  312. _spec.SetField(agent.FieldRole, field.TypeString, value)
  313. }
  314. if value, ok := au.mutation.Status(); ok {
  315. _spec.SetField(agent.FieldStatus, field.TypeInt, value)
  316. }
  317. if value, ok := au.mutation.AddedStatus(); ok {
  318. _spec.AddField(agent.FieldStatus, field.TypeInt, value)
  319. }
  320. if au.mutation.StatusCleared() {
  321. _spec.ClearField(agent.FieldStatus, field.TypeInt)
  322. }
  323. if value, ok := au.mutation.Background(); ok {
  324. _spec.SetField(agent.FieldBackground, field.TypeString, value)
  325. }
  326. if au.mutation.BackgroundCleared() {
  327. _spec.ClearField(agent.FieldBackground, field.TypeString)
  328. }
  329. if value, ok := au.mutation.Examples(); ok {
  330. _spec.SetField(agent.FieldExamples, field.TypeString, value)
  331. }
  332. if au.mutation.ExamplesCleared() {
  333. _spec.ClearField(agent.FieldExamples, field.TypeString)
  334. }
  335. if value, ok := au.mutation.OrganizationID(); ok {
  336. _spec.SetField(agent.FieldOrganizationID, field.TypeUint64, value)
  337. }
  338. if value, ok := au.mutation.AddedOrganizationID(); ok {
  339. _spec.AddField(agent.FieldOrganizationID, field.TypeUint64, value)
  340. }
  341. if value, ok := au.mutation.DatasetID(); ok {
  342. _spec.SetField(agent.FieldDatasetID, field.TypeString, value)
  343. }
  344. if value, ok := au.mutation.CollectionID(); ok {
  345. _spec.SetField(agent.FieldCollectionID, field.TypeString, value)
  346. }
  347. if au.mutation.WxAgentCleared() {
  348. edge := &sqlgraph.EdgeSpec{
  349. Rel: sqlgraph.O2M,
  350. Inverse: false,
  351. Table: agent.WxAgentTable,
  352. Columns: []string{agent.WxAgentColumn},
  353. Bidi: false,
  354. Target: &sqlgraph.EdgeTarget{
  355. IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
  356. },
  357. }
  358. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  359. }
  360. if nodes := au.mutation.RemovedWxAgentIDs(); len(nodes) > 0 && !au.mutation.WxAgentCleared() {
  361. edge := &sqlgraph.EdgeSpec{
  362. Rel: sqlgraph.O2M,
  363. Inverse: false,
  364. Table: agent.WxAgentTable,
  365. Columns: []string{agent.WxAgentColumn},
  366. Bidi: false,
  367. Target: &sqlgraph.EdgeTarget{
  368. IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
  369. },
  370. }
  371. for _, k := range nodes {
  372. edge.Target.Nodes = append(edge.Target.Nodes, k)
  373. }
  374. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  375. }
  376. if nodes := au.mutation.WxAgentIDs(); len(nodes) > 0 {
  377. edge := &sqlgraph.EdgeSpec{
  378. Rel: sqlgraph.O2M,
  379. Inverse: false,
  380. Table: agent.WxAgentTable,
  381. Columns: []string{agent.WxAgentColumn},
  382. Bidi: false,
  383. Target: &sqlgraph.EdgeTarget{
  384. IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
  385. },
  386. }
  387. for _, k := range nodes {
  388. edge.Target.Nodes = append(edge.Target.Nodes, k)
  389. }
  390. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  391. }
  392. if n, err = sqlgraph.UpdateNodes(ctx, au.driver, _spec); err != nil {
  393. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  394. err = &NotFoundError{agent.Label}
  395. } else if sqlgraph.IsConstraintError(err) {
  396. err = &ConstraintError{msg: err.Error(), wrap: err}
  397. }
  398. return 0, err
  399. }
  400. au.mutation.done = true
  401. return n, nil
  402. }
  403. // AgentUpdateOne is the builder for updating a single Agent entity.
  404. type AgentUpdateOne struct {
  405. config
  406. fields []string
  407. hooks []Hook
  408. mutation *AgentMutation
  409. }
  410. // SetUpdatedAt sets the "updated_at" field.
  411. func (auo *AgentUpdateOne) SetUpdatedAt(t time.Time) *AgentUpdateOne {
  412. auo.mutation.SetUpdatedAt(t)
  413. return auo
  414. }
  415. // SetDeletedAt sets the "deleted_at" field.
  416. func (auo *AgentUpdateOne) SetDeletedAt(t time.Time) *AgentUpdateOne {
  417. auo.mutation.SetDeletedAt(t)
  418. return auo
  419. }
  420. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  421. func (auo *AgentUpdateOne) SetNillableDeletedAt(t *time.Time) *AgentUpdateOne {
  422. if t != nil {
  423. auo.SetDeletedAt(*t)
  424. }
  425. return auo
  426. }
  427. // ClearDeletedAt clears the value of the "deleted_at" field.
  428. func (auo *AgentUpdateOne) ClearDeletedAt() *AgentUpdateOne {
  429. auo.mutation.ClearDeletedAt()
  430. return auo
  431. }
  432. // SetName sets the "name" field.
  433. func (auo *AgentUpdateOne) SetName(s string) *AgentUpdateOne {
  434. auo.mutation.SetName(s)
  435. return auo
  436. }
  437. // SetNillableName sets the "name" field if the given value is not nil.
  438. func (auo *AgentUpdateOne) SetNillableName(s *string) *AgentUpdateOne {
  439. if s != nil {
  440. auo.SetName(*s)
  441. }
  442. return auo
  443. }
  444. // SetRole sets the "role" field.
  445. func (auo *AgentUpdateOne) SetRole(s string) *AgentUpdateOne {
  446. auo.mutation.SetRole(s)
  447. return auo
  448. }
  449. // SetNillableRole sets the "role" field if the given value is not nil.
  450. func (auo *AgentUpdateOne) SetNillableRole(s *string) *AgentUpdateOne {
  451. if s != nil {
  452. auo.SetRole(*s)
  453. }
  454. return auo
  455. }
  456. // SetStatus sets the "status" field.
  457. func (auo *AgentUpdateOne) SetStatus(i int) *AgentUpdateOne {
  458. auo.mutation.ResetStatus()
  459. auo.mutation.SetStatus(i)
  460. return auo
  461. }
  462. // SetNillableStatus sets the "status" field if the given value is not nil.
  463. func (auo *AgentUpdateOne) SetNillableStatus(i *int) *AgentUpdateOne {
  464. if i != nil {
  465. auo.SetStatus(*i)
  466. }
  467. return auo
  468. }
  469. // AddStatus adds i to the "status" field.
  470. func (auo *AgentUpdateOne) AddStatus(i int) *AgentUpdateOne {
  471. auo.mutation.AddStatus(i)
  472. return auo
  473. }
  474. // ClearStatus clears the value of the "status" field.
  475. func (auo *AgentUpdateOne) ClearStatus() *AgentUpdateOne {
  476. auo.mutation.ClearStatus()
  477. return auo
  478. }
  479. // SetBackground sets the "background" field.
  480. func (auo *AgentUpdateOne) SetBackground(s string) *AgentUpdateOne {
  481. auo.mutation.SetBackground(s)
  482. return auo
  483. }
  484. // SetNillableBackground sets the "background" field if the given value is not nil.
  485. func (auo *AgentUpdateOne) SetNillableBackground(s *string) *AgentUpdateOne {
  486. if s != nil {
  487. auo.SetBackground(*s)
  488. }
  489. return auo
  490. }
  491. // ClearBackground clears the value of the "background" field.
  492. func (auo *AgentUpdateOne) ClearBackground() *AgentUpdateOne {
  493. auo.mutation.ClearBackground()
  494. return auo
  495. }
  496. // SetExamples sets the "examples" field.
  497. func (auo *AgentUpdateOne) SetExamples(s string) *AgentUpdateOne {
  498. auo.mutation.SetExamples(s)
  499. return auo
  500. }
  501. // SetNillableExamples sets the "examples" field if the given value is not nil.
  502. func (auo *AgentUpdateOne) SetNillableExamples(s *string) *AgentUpdateOne {
  503. if s != nil {
  504. auo.SetExamples(*s)
  505. }
  506. return auo
  507. }
  508. // ClearExamples clears the value of the "examples" field.
  509. func (auo *AgentUpdateOne) ClearExamples() *AgentUpdateOne {
  510. auo.mutation.ClearExamples()
  511. return auo
  512. }
  513. // SetOrganizationID sets the "organization_id" field.
  514. func (auo *AgentUpdateOne) SetOrganizationID(u uint64) *AgentUpdateOne {
  515. auo.mutation.ResetOrganizationID()
  516. auo.mutation.SetOrganizationID(u)
  517. return auo
  518. }
  519. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  520. func (auo *AgentUpdateOne) SetNillableOrganizationID(u *uint64) *AgentUpdateOne {
  521. if u != nil {
  522. auo.SetOrganizationID(*u)
  523. }
  524. return auo
  525. }
  526. // AddOrganizationID adds u to the "organization_id" field.
  527. func (auo *AgentUpdateOne) AddOrganizationID(u int64) *AgentUpdateOne {
  528. auo.mutation.AddOrganizationID(u)
  529. return auo
  530. }
  531. // SetDatasetID sets the "dataset_id" field.
  532. func (auo *AgentUpdateOne) SetDatasetID(s string) *AgentUpdateOne {
  533. auo.mutation.SetDatasetID(s)
  534. return auo
  535. }
  536. // SetNillableDatasetID sets the "dataset_id" field if the given value is not nil.
  537. func (auo *AgentUpdateOne) SetNillableDatasetID(s *string) *AgentUpdateOne {
  538. if s != nil {
  539. auo.SetDatasetID(*s)
  540. }
  541. return auo
  542. }
  543. // SetCollectionID sets the "collection_id" field.
  544. func (auo *AgentUpdateOne) SetCollectionID(s string) *AgentUpdateOne {
  545. auo.mutation.SetCollectionID(s)
  546. return auo
  547. }
  548. // SetNillableCollectionID sets the "collection_id" field if the given value is not nil.
  549. func (auo *AgentUpdateOne) SetNillableCollectionID(s *string) *AgentUpdateOne {
  550. if s != nil {
  551. auo.SetCollectionID(*s)
  552. }
  553. return auo
  554. }
  555. // AddWxAgentIDs adds the "wx_agent" edge to the Wx entity by IDs.
  556. func (auo *AgentUpdateOne) AddWxAgentIDs(ids ...uint64) *AgentUpdateOne {
  557. auo.mutation.AddWxAgentIDs(ids...)
  558. return auo
  559. }
  560. // AddWxAgent adds the "wx_agent" edges to the Wx entity.
  561. func (auo *AgentUpdateOne) AddWxAgent(w ...*Wx) *AgentUpdateOne {
  562. ids := make([]uint64, len(w))
  563. for i := range w {
  564. ids[i] = w[i].ID
  565. }
  566. return auo.AddWxAgentIDs(ids...)
  567. }
  568. // Mutation returns the AgentMutation object of the builder.
  569. func (auo *AgentUpdateOne) Mutation() *AgentMutation {
  570. return auo.mutation
  571. }
  572. // ClearWxAgent clears all "wx_agent" edges to the Wx entity.
  573. func (auo *AgentUpdateOne) ClearWxAgent() *AgentUpdateOne {
  574. auo.mutation.ClearWxAgent()
  575. return auo
  576. }
  577. // RemoveWxAgentIDs removes the "wx_agent" edge to Wx entities by IDs.
  578. func (auo *AgentUpdateOne) RemoveWxAgentIDs(ids ...uint64) *AgentUpdateOne {
  579. auo.mutation.RemoveWxAgentIDs(ids...)
  580. return auo
  581. }
  582. // RemoveWxAgent removes "wx_agent" edges to Wx entities.
  583. func (auo *AgentUpdateOne) RemoveWxAgent(w ...*Wx) *AgentUpdateOne {
  584. ids := make([]uint64, len(w))
  585. for i := range w {
  586. ids[i] = w[i].ID
  587. }
  588. return auo.RemoveWxAgentIDs(ids...)
  589. }
  590. // Where appends a list predicates to the AgentUpdate builder.
  591. func (auo *AgentUpdateOne) Where(ps ...predicate.Agent) *AgentUpdateOne {
  592. auo.mutation.Where(ps...)
  593. return auo
  594. }
  595. // Select allows selecting one or more fields (columns) of the returned entity.
  596. // The default is selecting all fields defined in the entity schema.
  597. func (auo *AgentUpdateOne) Select(field string, fields ...string) *AgentUpdateOne {
  598. auo.fields = append([]string{field}, fields...)
  599. return auo
  600. }
  601. // Save executes the query and returns the updated Agent entity.
  602. func (auo *AgentUpdateOne) Save(ctx context.Context) (*Agent, error) {
  603. if err := auo.defaults(); err != nil {
  604. return nil, err
  605. }
  606. return withHooks(ctx, auo.sqlSave, auo.mutation, auo.hooks)
  607. }
  608. // SaveX is like Save, but panics if an error occurs.
  609. func (auo *AgentUpdateOne) SaveX(ctx context.Context) *Agent {
  610. node, err := auo.Save(ctx)
  611. if err != nil {
  612. panic(err)
  613. }
  614. return node
  615. }
  616. // Exec executes the query on the entity.
  617. func (auo *AgentUpdateOne) Exec(ctx context.Context) error {
  618. _, err := auo.Save(ctx)
  619. return err
  620. }
  621. // ExecX is like Exec, but panics if an error occurs.
  622. func (auo *AgentUpdateOne) ExecX(ctx context.Context) {
  623. if err := auo.Exec(ctx); err != nil {
  624. panic(err)
  625. }
  626. }
  627. // defaults sets the default values of the builder before save.
  628. func (auo *AgentUpdateOne) defaults() error {
  629. if _, ok := auo.mutation.UpdatedAt(); !ok {
  630. if agent.UpdateDefaultUpdatedAt == nil {
  631. return fmt.Errorf("ent: uninitialized agent.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  632. }
  633. v := agent.UpdateDefaultUpdatedAt()
  634. auo.mutation.SetUpdatedAt(v)
  635. }
  636. return nil
  637. }
  638. // check runs all checks and user-defined validators on the builder.
  639. func (auo *AgentUpdateOne) check() error {
  640. if v, ok := auo.mutation.Name(); ok {
  641. if err := agent.NameValidator(v); err != nil {
  642. return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Agent.name": %w`, err)}
  643. }
  644. }
  645. if v, ok := auo.mutation.Role(); ok {
  646. if err := agent.RoleValidator(v); err != nil {
  647. return &ValidationError{Name: "role", err: fmt.Errorf(`ent: validator failed for field "Agent.role": %w`, err)}
  648. }
  649. }
  650. if v, ok := auo.mutation.Status(); ok {
  651. if err := agent.StatusValidator(v); err != nil {
  652. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Agent.status": %w`, err)}
  653. }
  654. }
  655. if v, ok := auo.mutation.Background(); ok {
  656. if err := agent.BackgroundValidator(v); err != nil {
  657. return &ValidationError{Name: "background", err: fmt.Errorf(`ent: validator failed for field "Agent.background": %w`, err)}
  658. }
  659. }
  660. if v, ok := auo.mutation.Examples(); ok {
  661. if err := agent.ExamplesValidator(v); err != nil {
  662. return &ValidationError{Name: "examples", err: fmt.Errorf(`ent: validator failed for field "Agent.examples": %w`, err)}
  663. }
  664. }
  665. if v, ok := auo.mutation.OrganizationID(); ok {
  666. if err := agent.OrganizationIDValidator(v); err != nil {
  667. return &ValidationError{Name: "organization_id", err: fmt.Errorf(`ent: validator failed for field "Agent.organization_id": %w`, err)}
  668. }
  669. }
  670. if v, ok := auo.mutation.DatasetID(); ok {
  671. if err := agent.DatasetIDValidator(v); err != nil {
  672. return &ValidationError{Name: "dataset_id", err: fmt.Errorf(`ent: validator failed for field "Agent.dataset_id": %w`, err)}
  673. }
  674. }
  675. if v, ok := auo.mutation.CollectionID(); ok {
  676. if err := agent.CollectionIDValidator(v); err != nil {
  677. return &ValidationError{Name: "collection_id", err: fmt.Errorf(`ent: validator failed for field "Agent.collection_id": %w`, err)}
  678. }
  679. }
  680. return nil
  681. }
  682. func (auo *AgentUpdateOne) sqlSave(ctx context.Context) (_node *Agent, err error) {
  683. if err := auo.check(); err != nil {
  684. return _node, err
  685. }
  686. _spec := sqlgraph.NewUpdateSpec(agent.Table, agent.Columns, sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64))
  687. id, ok := auo.mutation.ID()
  688. if !ok {
  689. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Agent.id" for update`)}
  690. }
  691. _spec.Node.ID.Value = id
  692. if fields := auo.fields; len(fields) > 0 {
  693. _spec.Node.Columns = make([]string, 0, len(fields))
  694. _spec.Node.Columns = append(_spec.Node.Columns, agent.FieldID)
  695. for _, f := range fields {
  696. if !agent.ValidColumn(f) {
  697. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  698. }
  699. if f != agent.FieldID {
  700. _spec.Node.Columns = append(_spec.Node.Columns, f)
  701. }
  702. }
  703. }
  704. if ps := auo.mutation.predicates; len(ps) > 0 {
  705. _spec.Predicate = func(selector *sql.Selector) {
  706. for i := range ps {
  707. ps[i](selector)
  708. }
  709. }
  710. }
  711. if value, ok := auo.mutation.UpdatedAt(); ok {
  712. _spec.SetField(agent.FieldUpdatedAt, field.TypeTime, value)
  713. }
  714. if value, ok := auo.mutation.DeletedAt(); ok {
  715. _spec.SetField(agent.FieldDeletedAt, field.TypeTime, value)
  716. }
  717. if auo.mutation.DeletedAtCleared() {
  718. _spec.ClearField(agent.FieldDeletedAt, field.TypeTime)
  719. }
  720. if value, ok := auo.mutation.Name(); ok {
  721. _spec.SetField(agent.FieldName, field.TypeString, value)
  722. }
  723. if value, ok := auo.mutation.Role(); ok {
  724. _spec.SetField(agent.FieldRole, field.TypeString, value)
  725. }
  726. if value, ok := auo.mutation.Status(); ok {
  727. _spec.SetField(agent.FieldStatus, field.TypeInt, value)
  728. }
  729. if value, ok := auo.mutation.AddedStatus(); ok {
  730. _spec.AddField(agent.FieldStatus, field.TypeInt, value)
  731. }
  732. if auo.mutation.StatusCleared() {
  733. _spec.ClearField(agent.FieldStatus, field.TypeInt)
  734. }
  735. if value, ok := auo.mutation.Background(); ok {
  736. _spec.SetField(agent.FieldBackground, field.TypeString, value)
  737. }
  738. if auo.mutation.BackgroundCleared() {
  739. _spec.ClearField(agent.FieldBackground, field.TypeString)
  740. }
  741. if value, ok := auo.mutation.Examples(); ok {
  742. _spec.SetField(agent.FieldExamples, field.TypeString, value)
  743. }
  744. if auo.mutation.ExamplesCleared() {
  745. _spec.ClearField(agent.FieldExamples, field.TypeString)
  746. }
  747. if value, ok := auo.mutation.OrganizationID(); ok {
  748. _spec.SetField(agent.FieldOrganizationID, field.TypeUint64, value)
  749. }
  750. if value, ok := auo.mutation.AddedOrganizationID(); ok {
  751. _spec.AddField(agent.FieldOrganizationID, field.TypeUint64, value)
  752. }
  753. if value, ok := auo.mutation.DatasetID(); ok {
  754. _spec.SetField(agent.FieldDatasetID, field.TypeString, value)
  755. }
  756. if value, ok := auo.mutation.CollectionID(); ok {
  757. _spec.SetField(agent.FieldCollectionID, field.TypeString, value)
  758. }
  759. if auo.mutation.WxAgentCleared() {
  760. edge := &sqlgraph.EdgeSpec{
  761. Rel: sqlgraph.O2M,
  762. Inverse: false,
  763. Table: agent.WxAgentTable,
  764. Columns: []string{agent.WxAgentColumn},
  765. Bidi: false,
  766. Target: &sqlgraph.EdgeTarget{
  767. IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
  768. },
  769. }
  770. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  771. }
  772. if nodes := auo.mutation.RemovedWxAgentIDs(); len(nodes) > 0 && !auo.mutation.WxAgentCleared() {
  773. edge := &sqlgraph.EdgeSpec{
  774. Rel: sqlgraph.O2M,
  775. Inverse: false,
  776. Table: agent.WxAgentTable,
  777. Columns: []string{agent.WxAgentColumn},
  778. Bidi: false,
  779. Target: &sqlgraph.EdgeTarget{
  780. IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
  781. },
  782. }
  783. for _, k := range nodes {
  784. edge.Target.Nodes = append(edge.Target.Nodes, k)
  785. }
  786. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  787. }
  788. if nodes := auo.mutation.WxAgentIDs(); len(nodes) > 0 {
  789. edge := &sqlgraph.EdgeSpec{
  790. Rel: sqlgraph.O2M,
  791. Inverse: false,
  792. Table: agent.WxAgentTable,
  793. Columns: []string{agent.WxAgentColumn},
  794. Bidi: false,
  795. Target: &sqlgraph.EdgeTarget{
  796. IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
  797. },
  798. }
  799. for _, k := range nodes {
  800. edge.Target.Nodes = append(edge.Target.Nodes, k)
  801. }
  802. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  803. }
  804. _node = &Agent{config: auo.config}
  805. _spec.Assign = _node.assignValues
  806. _spec.ScanValues = _node.scanValues
  807. if err = sqlgraph.UpdateNode(ctx, auo.driver, _spec); err != nil {
  808. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  809. err = &NotFoundError{agent.Label}
  810. } else if sqlgraph.IsConstraintError(err) {
  811. err = &ConstraintError{msg: err.Error(), wrap: err}
  812. }
  813. return nil, err
  814. }
  815. auo.mutation.done = true
  816. return _node, nil
  817. }