agent_update.go 30 KB

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