agent_update.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. // SetExamples sets the "examples" field.
  108. func (au *AgentUpdate) SetExamples(s string) *AgentUpdate {
  109. au.mutation.SetExamples(s)
  110. return au
  111. }
  112. // SetNillableExamples sets the "examples" field if the given value is not nil.
  113. func (au *AgentUpdate) SetNillableExamples(s *string) *AgentUpdate {
  114. if s != nil {
  115. au.SetExamples(*s)
  116. }
  117. return au
  118. }
  119. // SetOrganizationID sets the "organization_id" field.
  120. func (au *AgentUpdate) SetOrganizationID(u uint64) *AgentUpdate {
  121. au.mutation.ResetOrganizationID()
  122. au.mutation.SetOrganizationID(u)
  123. return au
  124. }
  125. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  126. func (au *AgentUpdate) SetNillableOrganizationID(u *uint64) *AgentUpdate {
  127. if u != nil {
  128. au.SetOrganizationID(*u)
  129. }
  130. return au
  131. }
  132. // AddOrganizationID adds u to the "organization_id" field.
  133. func (au *AgentUpdate) AddOrganizationID(u int64) *AgentUpdate {
  134. au.mutation.AddOrganizationID(u)
  135. return au
  136. }
  137. // AddWxAgentIDs adds the "wx_agent" edge to the Wx entity by IDs.
  138. func (au *AgentUpdate) AddWxAgentIDs(ids ...uint64) *AgentUpdate {
  139. au.mutation.AddWxAgentIDs(ids...)
  140. return au
  141. }
  142. // AddWxAgent adds the "wx_agent" edges to the Wx entity.
  143. func (au *AgentUpdate) AddWxAgent(w ...*Wx) *AgentUpdate {
  144. ids := make([]uint64, len(w))
  145. for i := range w {
  146. ids[i] = w[i].ID
  147. }
  148. return au.AddWxAgentIDs(ids...)
  149. }
  150. // Mutation returns the AgentMutation object of the builder.
  151. func (au *AgentUpdate) Mutation() *AgentMutation {
  152. return au.mutation
  153. }
  154. // ClearWxAgent clears all "wx_agent" edges to the Wx entity.
  155. func (au *AgentUpdate) ClearWxAgent() *AgentUpdate {
  156. au.mutation.ClearWxAgent()
  157. return au
  158. }
  159. // RemoveWxAgentIDs removes the "wx_agent" edge to Wx entities by IDs.
  160. func (au *AgentUpdate) RemoveWxAgentIDs(ids ...uint64) *AgentUpdate {
  161. au.mutation.RemoveWxAgentIDs(ids...)
  162. return au
  163. }
  164. // RemoveWxAgent removes "wx_agent" edges to Wx entities.
  165. func (au *AgentUpdate) RemoveWxAgent(w ...*Wx) *AgentUpdate {
  166. ids := make([]uint64, len(w))
  167. for i := range w {
  168. ids[i] = w[i].ID
  169. }
  170. return au.RemoveWxAgentIDs(ids...)
  171. }
  172. // Save executes the query and returns the number of nodes affected by the update operation.
  173. func (au *AgentUpdate) Save(ctx context.Context) (int, error) {
  174. if err := au.defaults(); err != nil {
  175. return 0, err
  176. }
  177. return withHooks(ctx, au.sqlSave, au.mutation, au.hooks)
  178. }
  179. // SaveX is like Save, but panics if an error occurs.
  180. func (au *AgentUpdate) SaveX(ctx context.Context) int {
  181. affected, err := au.Save(ctx)
  182. if err != nil {
  183. panic(err)
  184. }
  185. return affected
  186. }
  187. // Exec executes the query.
  188. func (au *AgentUpdate) Exec(ctx context.Context) error {
  189. _, err := au.Save(ctx)
  190. return err
  191. }
  192. // ExecX is like Exec, but panics if an error occurs.
  193. func (au *AgentUpdate) ExecX(ctx context.Context) {
  194. if err := au.Exec(ctx); err != nil {
  195. panic(err)
  196. }
  197. }
  198. // defaults sets the default values of the builder before save.
  199. func (au *AgentUpdate) defaults() error {
  200. if _, ok := au.mutation.UpdatedAt(); !ok {
  201. if agent.UpdateDefaultUpdatedAt == nil {
  202. return fmt.Errorf("ent: uninitialized agent.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  203. }
  204. v := agent.UpdateDefaultUpdatedAt()
  205. au.mutation.SetUpdatedAt(v)
  206. }
  207. return nil
  208. }
  209. // check runs all checks and user-defined validators on the builder.
  210. func (au *AgentUpdate) check() error {
  211. if v, ok := au.mutation.Name(); ok {
  212. if err := agent.NameValidator(v); err != nil {
  213. return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Agent.name": %w`, err)}
  214. }
  215. }
  216. if v, ok := au.mutation.Role(); ok {
  217. if err := agent.RoleValidator(v); err != nil {
  218. return &ValidationError{Name: "role", err: fmt.Errorf(`ent: validator failed for field "Agent.role": %w`, err)}
  219. }
  220. }
  221. if v, ok := au.mutation.Status(); ok {
  222. if err := agent.StatusValidator(v); err != nil {
  223. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Agent.status": %w`, err)}
  224. }
  225. }
  226. if v, ok := au.mutation.Background(); ok {
  227. if err := agent.BackgroundValidator(v); err != nil {
  228. return &ValidationError{Name: "background", err: fmt.Errorf(`ent: validator failed for field "Agent.background": %w`, err)}
  229. }
  230. }
  231. if v, ok := au.mutation.Examples(); ok {
  232. if err := agent.ExamplesValidator(v); err != nil {
  233. return &ValidationError{Name: "examples", err: fmt.Errorf(`ent: validator failed for field "Agent.examples": %w`, err)}
  234. }
  235. }
  236. if v, ok := au.mutation.OrganizationID(); ok {
  237. if err := agent.OrganizationIDValidator(v); err != nil {
  238. return &ValidationError{Name: "organization_id", err: fmt.Errorf(`ent: validator failed for field "Agent.organization_id": %w`, err)}
  239. }
  240. }
  241. return nil
  242. }
  243. func (au *AgentUpdate) sqlSave(ctx context.Context) (n int, err error) {
  244. if err := au.check(); err != nil {
  245. return n, err
  246. }
  247. _spec := sqlgraph.NewUpdateSpec(agent.Table, agent.Columns, sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64))
  248. if ps := au.mutation.predicates; len(ps) > 0 {
  249. _spec.Predicate = func(selector *sql.Selector) {
  250. for i := range ps {
  251. ps[i](selector)
  252. }
  253. }
  254. }
  255. if value, ok := au.mutation.UpdatedAt(); ok {
  256. _spec.SetField(agent.FieldUpdatedAt, field.TypeTime, value)
  257. }
  258. if value, ok := au.mutation.DeletedAt(); ok {
  259. _spec.SetField(agent.FieldDeletedAt, field.TypeTime, value)
  260. }
  261. if au.mutation.DeletedAtCleared() {
  262. _spec.ClearField(agent.FieldDeletedAt, field.TypeTime)
  263. }
  264. if value, ok := au.mutation.Name(); ok {
  265. _spec.SetField(agent.FieldName, field.TypeString, value)
  266. }
  267. if value, ok := au.mutation.Role(); ok {
  268. _spec.SetField(agent.FieldRole, field.TypeString, value)
  269. }
  270. if value, ok := au.mutation.Status(); ok {
  271. _spec.SetField(agent.FieldStatus, field.TypeInt, value)
  272. }
  273. if value, ok := au.mutation.AddedStatus(); ok {
  274. _spec.AddField(agent.FieldStatus, field.TypeInt, value)
  275. }
  276. if au.mutation.StatusCleared() {
  277. _spec.ClearField(agent.FieldStatus, field.TypeInt)
  278. }
  279. if value, ok := au.mutation.Background(); ok {
  280. _spec.SetField(agent.FieldBackground, field.TypeString, value)
  281. }
  282. if value, ok := au.mutation.Examples(); ok {
  283. _spec.SetField(agent.FieldExamples, field.TypeString, value)
  284. }
  285. if value, ok := au.mutation.OrganizationID(); ok {
  286. _spec.SetField(agent.FieldOrganizationID, field.TypeUint64, value)
  287. }
  288. if value, ok := au.mutation.AddedOrganizationID(); ok {
  289. _spec.AddField(agent.FieldOrganizationID, field.TypeUint64, value)
  290. }
  291. if au.mutation.WxAgentCleared() {
  292. edge := &sqlgraph.EdgeSpec{
  293. Rel: sqlgraph.O2M,
  294. Inverse: false,
  295. Table: agent.WxAgentTable,
  296. Columns: []string{agent.WxAgentColumn},
  297. Bidi: false,
  298. Target: &sqlgraph.EdgeTarget{
  299. IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
  300. },
  301. }
  302. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  303. }
  304. if nodes := au.mutation.RemovedWxAgentIDs(); len(nodes) > 0 && !au.mutation.WxAgentCleared() {
  305. edge := &sqlgraph.EdgeSpec{
  306. Rel: sqlgraph.O2M,
  307. Inverse: false,
  308. Table: agent.WxAgentTable,
  309. Columns: []string{agent.WxAgentColumn},
  310. Bidi: false,
  311. Target: &sqlgraph.EdgeTarget{
  312. IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
  313. },
  314. }
  315. for _, k := range nodes {
  316. edge.Target.Nodes = append(edge.Target.Nodes, k)
  317. }
  318. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  319. }
  320. if nodes := au.mutation.WxAgentIDs(); len(nodes) > 0 {
  321. edge := &sqlgraph.EdgeSpec{
  322. Rel: sqlgraph.O2M,
  323. Inverse: false,
  324. Table: agent.WxAgentTable,
  325. Columns: []string{agent.WxAgentColumn},
  326. Bidi: false,
  327. Target: &sqlgraph.EdgeTarget{
  328. IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
  329. },
  330. }
  331. for _, k := range nodes {
  332. edge.Target.Nodes = append(edge.Target.Nodes, k)
  333. }
  334. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  335. }
  336. if n, err = sqlgraph.UpdateNodes(ctx, au.driver, _spec); err != nil {
  337. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  338. err = &NotFoundError{agent.Label}
  339. } else if sqlgraph.IsConstraintError(err) {
  340. err = &ConstraintError{msg: err.Error(), wrap: err}
  341. }
  342. return 0, err
  343. }
  344. au.mutation.done = true
  345. return n, nil
  346. }
  347. // AgentUpdateOne is the builder for updating a single Agent entity.
  348. type AgentUpdateOne struct {
  349. config
  350. fields []string
  351. hooks []Hook
  352. mutation *AgentMutation
  353. }
  354. // SetUpdatedAt sets the "updated_at" field.
  355. func (auo *AgentUpdateOne) SetUpdatedAt(t time.Time) *AgentUpdateOne {
  356. auo.mutation.SetUpdatedAt(t)
  357. return auo
  358. }
  359. // SetDeletedAt sets the "deleted_at" field.
  360. func (auo *AgentUpdateOne) SetDeletedAt(t time.Time) *AgentUpdateOne {
  361. auo.mutation.SetDeletedAt(t)
  362. return auo
  363. }
  364. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  365. func (auo *AgentUpdateOne) SetNillableDeletedAt(t *time.Time) *AgentUpdateOne {
  366. if t != nil {
  367. auo.SetDeletedAt(*t)
  368. }
  369. return auo
  370. }
  371. // ClearDeletedAt clears the value of the "deleted_at" field.
  372. func (auo *AgentUpdateOne) ClearDeletedAt() *AgentUpdateOne {
  373. auo.mutation.ClearDeletedAt()
  374. return auo
  375. }
  376. // SetName sets the "name" field.
  377. func (auo *AgentUpdateOne) SetName(s string) *AgentUpdateOne {
  378. auo.mutation.SetName(s)
  379. return auo
  380. }
  381. // SetNillableName sets the "name" field if the given value is not nil.
  382. func (auo *AgentUpdateOne) SetNillableName(s *string) *AgentUpdateOne {
  383. if s != nil {
  384. auo.SetName(*s)
  385. }
  386. return auo
  387. }
  388. // SetRole sets the "role" field.
  389. func (auo *AgentUpdateOne) SetRole(s string) *AgentUpdateOne {
  390. auo.mutation.SetRole(s)
  391. return auo
  392. }
  393. // SetNillableRole sets the "role" field if the given value is not nil.
  394. func (auo *AgentUpdateOne) SetNillableRole(s *string) *AgentUpdateOne {
  395. if s != nil {
  396. auo.SetRole(*s)
  397. }
  398. return auo
  399. }
  400. // SetStatus sets the "status" field.
  401. func (auo *AgentUpdateOne) SetStatus(i int) *AgentUpdateOne {
  402. auo.mutation.ResetStatus()
  403. auo.mutation.SetStatus(i)
  404. return auo
  405. }
  406. // SetNillableStatus sets the "status" field if the given value is not nil.
  407. func (auo *AgentUpdateOne) SetNillableStatus(i *int) *AgentUpdateOne {
  408. if i != nil {
  409. auo.SetStatus(*i)
  410. }
  411. return auo
  412. }
  413. // AddStatus adds i to the "status" field.
  414. func (auo *AgentUpdateOne) AddStatus(i int) *AgentUpdateOne {
  415. auo.mutation.AddStatus(i)
  416. return auo
  417. }
  418. // ClearStatus clears the value of the "status" field.
  419. func (auo *AgentUpdateOne) ClearStatus() *AgentUpdateOne {
  420. auo.mutation.ClearStatus()
  421. return auo
  422. }
  423. // SetBackground sets the "background" field.
  424. func (auo *AgentUpdateOne) SetBackground(s string) *AgentUpdateOne {
  425. auo.mutation.SetBackground(s)
  426. return auo
  427. }
  428. // SetNillableBackground sets the "background" field if the given value is not nil.
  429. func (auo *AgentUpdateOne) SetNillableBackground(s *string) *AgentUpdateOne {
  430. if s != nil {
  431. auo.SetBackground(*s)
  432. }
  433. return auo
  434. }
  435. // SetExamples sets the "examples" field.
  436. func (auo *AgentUpdateOne) SetExamples(s string) *AgentUpdateOne {
  437. auo.mutation.SetExamples(s)
  438. return auo
  439. }
  440. // SetNillableExamples sets the "examples" field if the given value is not nil.
  441. func (auo *AgentUpdateOne) SetNillableExamples(s *string) *AgentUpdateOne {
  442. if s != nil {
  443. auo.SetExamples(*s)
  444. }
  445. return auo
  446. }
  447. // SetOrganizationID sets the "organization_id" field.
  448. func (auo *AgentUpdateOne) SetOrganizationID(u uint64) *AgentUpdateOne {
  449. auo.mutation.ResetOrganizationID()
  450. auo.mutation.SetOrganizationID(u)
  451. return auo
  452. }
  453. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  454. func (auo *AgentUpdateOne) SetNillableOrganizationID(u *uint64) *AgentUpdateOne {
  455. if u != nil {
  456. auo.SetOrganizationID(*u)
  457. }
  458. return auo
  459. }
  460. // AddOrganizationID adds u to the "organization_id" field.
  461. func (auo *AgentUpdateOne) AddOrganizationID(u int64) *AgentUpdateOne {
  462. auo.mutation.AddOrganizationID(u)
  463. return auo
  464. }
  465. // AddWxAgentIDs adds the "wx_agent" edge to the Wx entity by IDs.
  466. func (auo *AgentUpdateOne) AddWxAgentIDs(ids ...uint64) *AgentUpdateOne {
  467. auo.mutation.AddWxAgentIDs(ids...)
  468. return auo
  469. }
  470. // AddWxAgent adds the "wx_agent" edges to the Wx entity.
  471. func (auo *AgentUpdateOne) AddWxAgent(w ...*Wx) *AgentUpdateOne {
  472. ids := make([]uint64, len(w))
  473. for i := range w {
  474. ids[i] = w[i].ID
  475. }
  476. return auo.AddWxAgentIDs(ids...)
  477. }
  478. // Mutation returns the AgentMutation object of the builder.
  479. func (auo *AgentUpdateOne) Mutation() *AgentMutation {
  480. return auo.mutation
  481. }
  482. // ClearWxAgent clears all "wx_agent" edges to the Wx entity.
  483. func (auo *AgentUpdateOne) ClearWxAgent() *AgentUpdateOne {
  484. auo.mutation.ClearWxAgent()
  485. return auo
  486. }
  487. // RemoveWxAgentIDs removes the "wx_agent" edge to Wx entities by IDs.
  488. func (auo *AgentUpdateOne) RemoveWxAgentIDs(ids ...uint64) *AgentUpdateOne {
  489. auo.mutation.RemoveWxAgentIDs(ids...)
  490. return auo
  491. }
  492. // RemoveWxAgent removes "wx_agent" edges to Wx entities.
  493. func (auo *AgentUpdateOne) RemoveWxAgent(w ...*Wx) *AgentUpdateOne {
  494. ids := make([]uint64, len(w))
  495. for i := range w {
  496. ids[i] = w[i].ID
  497. }
  498. return auo.RemoveWxAgentIDs(ids...)
  499. }
  500. // Where appends a list predicates to the AgentUpdate builder.
  501. func (auo *AgentUpdateOne) Where(ps ...predicate.Agent) *AgentUpdateOne {
  502. auo.mutation.Where(ps...)
  503. return auo
  504. }
  505. // Select allows selecting one or more fields (columns) of the returned entity.
  506. // The default is selecting all fields defined in the entity schema.
  507. func (auo *AgentUpdateOne) Select(field string, fields ...string) *AgentUpdateOne {
  508. auo.fields = append([]string{field}, fields...)
  509. return auo
  510. }
  511. // Save executes the query and returns the updated Agent entity.
  512. func (auo *AgentUpdateOne) Save(ctx context.Context) (*Agent, error) {
  513. if err := auo.defaults(); err != nil {
  514. return nil, err
  515. }
  516. return withHooks(ctx, auo.sqlSave, auo.mutation, auo.hooks)
  517. }
  518. // SaveX is like Save, but panics if an error occurs.
  519. func (auo *AgentUpdateOne) SaveX(ctx context.Context) *Agent {
  520. node, err := auo.Save(ctx)
  521. if err != nil {
  522. panic(err)
  523. }
  524. return node
  525. }
  526. // Exec executes the query on the entity.
  527. func (auo *AgentUpdateOne) Exec(ctx context.Context) error {
  528. _, err := auo.Save(ctx)
  529. return err
  530. }
  531. // ExecX is like Exec, but panics if an error occurs.
  532. func (auo *AgentUpdateOne) ExecX(ctx context.Context) {
  533. if err := auo.Exec(ctx); err != nil {
  534. panic(err)
  535. }
  536. }
  537. // defaults sets the default values of the builder before save.
  538. func (auo *AgentUpdateOne) defaults() error {
  539. if _, ok := auo.mutation.UpdatedAt(); !ok {
  540. if agent.UpdateDefaultUpdatedAt == nil {
  541. return fmt.Errorf("ent: uninitialized agent.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  542. }
  543. v := agent.UpdateDefaultUpdatedAt()
  544. auo.mutation.SetUpdatedAt(v)
  545. }
  546. return nil
  547. }
  548. // check runs all checks and user-defined validators on the builder.
  549. func (auo *AgentUpdateOne) check() error {
  550. if v, ok := auo.mutation.Name(); ok {
  551. if err := agent.NameValidator(v); err != nil {
  552. return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Agent.name": %w`, err)}
  553. }
  554. }
  555. if v, ok := auo.mutation.Role(); ok {
  556. if err := agent.RoleValidator(v); err != nil {
  557. return &ValidationError{Name: "role", err: fmt.Errorf(`ent: validator failed for field "Agent.role": %w`, err)}
  558. }
  559. }
  560. if v, ok := auo.mutation.Status(); ok {
  561. if err := agent.StatusValidator(v); err != nil {
  562. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Agent.status": %w`, err)}
  563. }
  564. }
  565. if v, ok := auo.mutation.Background(); ok {
  566. if err := agent.BackgroundValidator(v); err != nil {
  567. return &ValidationError{Name: "background", err: fmt.Errorf(`ent: validator failed for field "Agent.background": %w`, err)}
  568. }
  569. }
  570. if v, ok := auo.mutation.Examples(); ok {
  571. if err := agent.ExamplesValidator(v); err != nil {
  572. return &ValidationError{Name: "examples", err: fmt.Errorf(`ent: validator failed for field "Agent.examples": %w`, err)}
  573. }
  574. }
  575. if v, ok := auo.mutation.OrganizationID(); ok {
  576. if err := agent.OrganizationIDValidator(v); err != nil {
  577. return &ValidationError{Name: "organization_id", err: fmt.Errorf(`ent: validator failed for field "Agent.organization_id": %w`, err)}
  578. }
  579. }
  580. return nil
  581. }
  582. func (auo *AgentUpdateOne) sqlSave(ctx context.Context) (_node *Agent, err error) {
  583. if err := auo.check(); err != nil {
  584. return _node, err
  585. }
  586. _spec := sqlgraph.NewUpdateSpec(agent.Table, agent.Columns, sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64))
  587. id, ok := auo.mutation.ID()
  588. if !ok {
  589. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Agent.id" for update`)}
  590. }
  591. _spec.Node.ID.Value = id
  592. if fields := auo.fields; len(fields) > 0 {
  593. _spec.Node.Columns = make([]string, 0, len(fields))
  594. _spec.Node.Columns = append(_spec.Node.Columns, agent.FieldID)
  595. for _, f := range fields {
  596. if !agent.ValidColumn(f) {
  597. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  598. }
  599. if f != agent.FieldID {
  600. _spec.Node.Columns = append(_spec.Node.Columns, f)
  601. }
  602. }
  603. }
  604. if ps := auo.mutation.predicates; len(ps) > 0 {
  605. _spec.Predicate = func(selector *sql.Selector) {
  606. for i := range ps {
  607. ps[i](selector)
  608. }
  609. }
  610. }
  611. if value, ok := auo.mutation.UpdatedAt(); ok {
  612. _spec.SetField(agent.FieldUpdatedAt, field.TypeTime, value)
  613. }
  614. if value, ok := auo.mutation.DeletedAt(); ok {
  615. _spec.SetField(agent.FieldDeletedAt, field.TypeTime, value)
  616. }
  617. if auo.mutation.DeletedAtCleared() {
  618. _spec.ClearField(agent.FieldDeletedAt, field.TypeTime)
  619. }
  620. if value, ok := auo.mutation.Name(); ok {
  621. _spec.SetField(agent.FieldName, field.TypeString, value)
  622. }
  623. if value, ok := auo.mutation.Role(); ok {
  624. _spec.SetField(agent.FieldRole, field.TypeString, value)
  625. }
  626. if value, ok := auo.mutation.Status(); ok {
  627. _spec.SetField(agent.FieldStatus, field.TypeInt, value)
  628. }
  629. if value, ok := auo.mutation.AddedStatus(); ok {
  630. _spec.AddField(agent.FieldStatus, field.TypeInt, value)
  631. }
  632. if auo.mutation.StatusCleared() {
  633. _spec.ClearField(agent.FieldStatus, field.TypeInt)
  634. }
  635. if value, ok := auo.mutation.Background(); ok {
  636. _spec.SetField(agent.FieldBackground, field.TypeString, value)
  637. }
  638. if value, ok := auo.mutation.Examples(); ok {
  639. _spec.SetField(agent.FieldExamples, field.TypeString, value)
  640. }
  641. if value, ok := auo.mutation.OrganizationID(); ok {
  642. _spec.SetField(agent.FieldOrganizationID, field.TypeUint64, value)
  643. }
  644. if value, ok := auo.mutation.AddedOrganizationID(); ok {
  645. _spec.AddField(agent.FieldOrganizationID, field.TypeUint64, value)
  646. }
  647. if auo.mutation.WxAgentCleared() {
  648. edge := &sqlgraph.EdgeSpec{
  649. Rel: sqlgraph.O2M,
  650. Inverse: false,
  651. Table: agent.WxAgentTable,
  652. Columns: []string{agent.WxAgentColumn},
  653. Bidi: false,
  654. Target: &sqlgraph.EdgeTarget{
  655. IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
  656. },
  657. }
  658. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  659. }
  660. if nodes := auo.mutation.RemovedWxAgentIDs(); len(nodes) > 0 && !auo.mutation.WxAgentCleared() {
  661. edge := &sqlgraph.EdgeSpec{
  662. Rel: sqlgraph.O2M,
  663. Inverse: false,
  664. Table: agent.WxAgentTable,
  665. Columns: []string{agent.WxAgentColumn},
  666. Bidi: false,
  667. Target: &sqlgraph.EdgeTarget{
  668. IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
  669. },
  670. }
  671. for _, k := range nodes {
  672. edge.Target.Nodes = append(edge.Target.Nodes, k)
  673. }
  674. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  675. }
  676. if nodes := auo.mutation.WxAgentIDs(); len(nodes) > 0 {
  677. edge := &sqlgraph.EdgeSpec{
  678. Rel: sqlgraph.O2M,
  679. Inverse: false,
  680. Table: agent.WxAgentTable,
  681. Columns: []string{agent.WxAgentColumn},
  682. Bidi: false,
  683. Target: &sqlgraph.EdgeTarget{
  684. IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
  685. },
  686. }
  687. for _, k := range nodes {
  688. edge.Target.Nodes = append(edge.Target.Nodes, k)
  689. }
  690. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  691. }
  692. _node = &Agent{config: auo.config}
  693. _spec.Assign = _node.assignValues
  694. _spec.ScanValues = _node.scanValues
  695. if err = sqlgraph.UpdateNode(ctx, auo.driver, _spec); err != nil {
  696. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  697. err = &NotFoundError{agent.Label}
  698. } else if sqlgraph.IsConstraintError(err) {
  699. err = &ConstraintError{msg: err.Error(), wrap: err}
  700. }
  701. return nil, err
  702. }
  703. auo.mutation.done = true
  704. return _node, nil
  705. }