agent_update.go 23 KB

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