xunjiservice_update.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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/xunjiservice"
  11. "entgo.io/ent/dialect/sql"
  12. "entgo.io/ent/dialect/sql/sqlgraph"
  13. "entgo.io/ent/schema/field"
  14. )
  15. // XunjiServiceUpdate is the builder for updating XunjiService entities.
  16. type XunjiServiceUpdate struct {
  17. config
  18. hooks []Hook
  19. mutation *XunjiServiceMutation
  20. }
  21. // Where appends a list predicates to the XunjiServiceUpdate builder.
  22. func (xsu *XunjiServiceUpdate) Where(ps ...predicate.XunjiService) *XunjiServiceUpdate {
  23. xsu.mutation.Where(ps...)
  24. return xsu
  25. }
  26. // SetUpdatedAt sets the "updated_at" field.
  27. func (xsu *XunjiServiceUpdate) SetUpdatedAt(t time.Time) *XunjiServiceUpdate {
  28. xsu.mutation.SetUpdatedAt(t)
  29. return xsu
  30. }
  31. // SetStatus sets the "status" field.
  32. func (xsu *XunjiServiceUpdate) SetStatus(u uint8) *XunjiServiceUpdate {
  33. xsu.mutation.ResetStatus()
  34. xsu.mutation.SetStatus(u)
  35. return xsu
  36. }
  37. // SetNillableStatus sets the "status" field if the given value is not nil.
  38. func (xsu *XunjiServiceUpdate) SetNillableStatus(u *uint8) *XunjiServiceUpdate {
  39. if u != nil {
  40. xsu.SetStatus(*u)
  41. }
  42. return xsu
  43. }
  44. // AddStatus adds u to the "status" field.
  45. func (xsu *XunjiServiceUpdate) AddStatus(u int8) *XunjiServiceUpdate {
  46. xsu.mutation.AddStatus(u)
  47. return xsu
  48. }
  49. // ClearStatus clears the value of the "status" field.
  50. func (xsu *XunjiServiceUpdate) ClearStatus() *XunjiServiceUpdate {
  51. xsu.mutation.ClearStatus()
  52. return xsu
  53. }
  54. // SetDeletedAt sets the "deleted_at" field.
  55. func (xsu *XunjiServiceUpdate) SetDeletedAt(t time.Time) *XunjiServiceUpdate {
  56. xsu.mutation.SetDeletedAt(t)
  57. return xsu
  58. }
  59. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  60. func (xsu *XunjiServiceUpdate) SetNillableDeletedAt(t *time.Time) *XunjiServiceUpdate {
  61. if t != nil {
  62. xsu.SetDeletedAt(*t)
  63. }
  64. return xsu
  65. }
  66. // ClearDeletedAt clears the value of the "deleted_at" field.
  67. func (xsu *XunjiServiceUpdate) ClearDeletedAt() *XunjiServiceUpdate {
  68. xsu.mutation.ClearDeletedAt()
  69. return xsu
  70. }
  71. // SetXunjiID sets the "xunji_id" field.
  72. func (xsu *XunjiServiceUpdate) SetXunjiID(u uint64) *XunjiServiceUpdate {
  73. xsu.mutation.ResetXunjiID()
  74. xsu.mutation.SetXunjiID(u)
  75. return xsu
  76. }
  77. // SetNillableXunjiID sets the "xunji_id" field if the given value is not nil.
  78. func (xsu *XunjiServiceUpdate) SetNillableXunjiID(u *uint64) *XunjiServiceUpdate {
  79. if u != nil {
  80. xsu.SetXunjiID(*u)
  81. }
  82. return xsu
  83. }
  84. // AddXunjiID adds u to the "xunji_id" field.
  85. func (xsu *XunjiServiceUpdate) AddXunjiID(u int64) *XunjiServiceUpdate {
  86. xsu.mutation.AddXunjiID(u)
  87. return xsu
  88. }
  89. // SetAgentID sets the "agent_id" field.
  90. func (xsu *XunjiServiceUpdate) SetAgentID(u uint64) *XunjiServiceUpdate {
  91. xsu.mutation.SetAgentID(u)
  92. return xsu
  93. }
  94. // SetNillableAgentID sets the "agent_id" field if the given value is not nil.
  95. func (xsu *XunjiServiceUpdate) SetNillableAgentID(u *uint64) *XunjiServiceUpdate {
  96. if u != nil {
  97. xsu.SetAgentID(*u)
  98. }
  99. return xsu
  100. }
  101. // SetOrganizationID sets the "organization_id" field.
  102. func (xsu *XunjiServiceUpdate) SetOrganizationID(u uint64) *XunjiServiceUpdate {
  103. xsu.mutation.ResetOrganizationID()
  104. xsu.mutation.SetOrganizationID(u)
  105. return xsu
  106. }
  107. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  108. func (xsu *XunjiServiceUpdate) SetNillableOrganizationID(u *uint64) *XunjiServiceUpdate {
  109. if u != nil {
  110. xsu.SetOrganizationID(*u)
  111. }
  112. return xsu
  113. }
  114. // AddOrganizationID adds u to the "organization_id" field.
  115. func (xsu *XunjiServiceUpdate) AddOrganizationID(u int64) *XunjiServiceUpdate {
  116. xsu.mutation.AddOrganizationID(u)
  117. return xsu
  118. }
  119. // SetWxid sets the "wxid" field.
  120. func (xsu *XunjiServiceUpdate) SetWxid(s string) *XunjiServiceUpdate {
  121. xsu.mutation.SetWxid(s)
  122. return xsu
  123. }
  124. // SetNillableWxid sets the "wxid" field if the given value is not nil.
  125. func (xsu *XunjiServiceUpdate) SetNillableWxid(s *string) *XunjiServiceUpdate {
  126. if s != nil {
  127. xsu.SetWxid(*s)
  128. }
  129. return xsu
  130. }
  131. // SetAPIBase sets the "api_base" field.
  132. func (xsu *XunjiServiceUpdate) SetAPIBase(s string) *XunjiServiceUpdate {
  133. xsu.mutation.SetAPIBase(s)
  134. return xsu
  135. }
  136. // SetNillableAPIBase sets the "api_base" field if the given value is not nil.
  137. func (xsu *XunjiServiceUpdate) SetNillableAPIBase(s *string) *XunjiServiceUpdate {
  138. if s != nil {
  139. xsu.SetAPIBase(*s)
  140. }
  141. return xsu
  142. }
  143. // ClearAPIBase clears the value of the "api_base" field.
  144. func (xsu *XunjiServiceUpdate) ClearAPIBase() *XunjiServiceUpdate {
  145. xsu.mutation.ClearAPIBase()
  146. return xsu
  147. }
  148. // SetAPIKey sets the "api_key" field.
  149. func (xsu *XunjiServiceUpdate) SetAPIKey(s string) *XunjiServiceUpdate {
  150. xsu.mutation.SetAPIKey(s)
  151. return xsu
  152. }
  153. // SetNillableAPIKey sets the "api_key" field if the given value is not nil.
  154. func (xsu *XunjiServiceUpdate) SetNillableAPIKey(s *string) *XunjiServiceUpdate {
  155. if s != nil {
  156. xsu.SetAPIKey(*s)
  157. }
  158. return xsu
  159. }
  160. // ClearAPIKey clears the value of the "api_key" field.
  161. func (xsu *XunjiServiceUpdate) ClearAPIKey() *XunjiServiceUpdate {
  162. xsu.mutation.ClearAPIKey()
  163. return xsu
  164. }
  165. // SetAgent sets the "agent" edge to the Agent entity.
  166. func (xsu *XunjiServiceUpdate) SetAgent(a *Agent) *XunjiServiceUpdate {
  167. return xsu.SetAgentID(a.ID)
  168. }
  169. // Mutation returns the XunjiServiceMutation object of the builder.
  170. func (xsu *XunjiServiceUpdate) Mutation() *XunjiServiceMutation {
  171. return xsu.mutation
  172. }
  173. // ClearAgent clears the "agent" edge to the Agent entity.
  174. func (xsu *XunjiServiceUpdate) ClearAgent() *XunjiServiceUpdate {
  175. xsu.mutation.ClearAgent()
  176. return xsu
  177. }
  178. // Save executes the query and returns the number of nodes affected by the update operation.
  179. func (xsu *XunjiServiceUpdate) Save(ctx context.Context) (int, error) {
  180. if err := xsu.defaults(); err != nil {
  181. return 0, err
  182. }
  183. return withHooks(ctx, xsu.sqlSave, xsu.mutation, xsu.hooks)
  184. }
  185. // SaveX is like Save, but panics if an error occurs.
  186. func (xsu *XunjiServiceUpdate) SaveX(ctx context.Context) int {
  187. affected, err := xsu.Save(ctx)
  188. if err != nil {
  189. panic(err)
  190. }
  191. return affected
  192. }
  193. // Exec executes the query.
  194. func (xsu *XunjiServiceUpdate) Exec(ctx context.Context) error {
  195. _, err := xsu.Save(ctx)
  196. return err
  197. }
  198. // ExecX is like Exec, but panics if an error occurs.
  199. func (xsu *XunjiServiceUpdate) ExecX(ctx context.Context) {
  200. if err := xsu.Exec(ctx); err != nil {
  201. panic(err)
  202. }
  203. }
  204. // defaults sets the default values of the builder before save.
  205. func (xsu *XunjiServiceUpdate) defaults() error {
  206. if _, ok := xsu.mutation.UpdatedAt(); !ok {
  207. if xunjiservice.UpdateDefaultUpdatedAt == nil {
  208. return fmt.Errorf("ent: uninitialized xunjiservice.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  209. }
  210. v := xunjiservice.UpdateDefaultUpdatedAt()
  211. xsu.mutation.SetUpdatedAt(v)
  212. }
  213. return nil
  214. }
  215. // check runs all checks and user-defined validators on the builder.
  216. func (xsu *XunjiServiceUpdate) check() error {
  217. if v, ok := xsu.mutation.OrganizationID(); ok {
  218. if err := xunjiservice.OrganizationIDValidator(v); err != nil {
  219. return &ValidationError{Name: "organization_id", err: fmt.Errorf(`ent: validator failed for field "XunjiService.organization_id": %w`, err)}
  220. }
  221. }
  222. if _, ok := xsu.mutation.AgentID(); xsu.mutation.AgentCleared() && !ok {
  223. return errors.New(`ent: clearing a required unique edge "XunjiService.agent"`)
  224. }
  225. return nil
  226. }
  227. func (xsu *XunjiServiceUpdate) sqlSave(ctx context.Context) (n int, err error) {
  228. if err := xsu.check(); err != nil {
  229. return n, err
  230. }
  231. _spec := sqlgraph.NewUpdateSpec(xunjiservice.Table, xunjiservice.Columns, sqlgraph.NewFieldSpec(xunjiservice.FieldID, field.TypeUint64))
  232. if ps := xsu.mutation.predicates; len(ps) > 0 {
  233. _spec.Predicate = func(selector *sql.Selector) {
  234. for i := range ps {
  235. ps[i](selector)
  236. }
  237. }
  238. }
  239. if value, ok := xsu.mutation.UpdatedAt(); ok {
  240. _spec.SetField(xunjiservice.FieldUpdatedAt, field.TypeTime, value)
  241. }
  242. if value, ok := xsu.mutation.Status(); ok {
  243. _spec.SetField(xunjiservice.FieldStatus, field.TypeUint8, value)
  244. }
  245. if value, ok := xsu.mutation.AddedStatus(); ok {
  246. _spec.AddField(xunjiservice.FieldStatus, field.TypeUint8, value)
  247. }
  248. if xsu.mutation.StatusCleared() {
  249. _spec.ClearField(xunjiservice.FieldStatus, field.TypeUint8)
  250. }
  251. if value, ok := xsu.mutation.DeletedAt(); ok {
  252. _spec.SetField(xunjiservice.FieldDeletedAt, field.TypeTime, value)
  253. }
  254. if xsu.mutation.DeletedAtCleared() {
  255. _spec.ClearField(xunjiservice.FieldDeletedAt, field.TypeTime)
  256. }
  257. if value, ok := xsu.mutation.XunjiID(); ok {
  258. _spec.SetField(xunjiservice.FieldXunjiID, field.TypeUint64, value)
  259. }
  260. if value, ok := xsu.mutation.AddedXunjiID(); ok {
  261. _spec.AddField(xunjiservice.FieldXunjiID, field.TypeUint64, value)
  262. }
  263. if value, ok := xsu.mutation.OrganizationID(); ok {
  264. _spec.SetField(xunjiservice.FieldOrganizationID, field.TypeUint64, value)
  265. }
  266. if value, ok := xsu.mutation.AddedOrganizationID(); ok {
  267. _spec.AddField(xunjiservice.FieldOrganizationID, field.TypeUint64, value)
  268. }
  269. if value, ok := xsu.mutation.Wxid(); ok {
  270. _spec.SetField(xunjiservice.FieldWxid, field.TypeString, value)
  271. }
  272. if value, ok := xsu.mutation.APIBase(); ok {
  273. _spec.SetField(xunjiservice.FieldAPIBase, field.TypeString, value)
  274. }
  275. if xsu.mutation.APIBaseCleared() {
  276. _spec.ClearField(xunjiservice.FieldAPIBase, field.TypeString)
  277. }
  278. if value, ok := xsu.mutation.APIKey(); ok {
  279. _spec.SetField(xunjiservice.FieldAPIKey, field.TypeString, value)
  280. }
  281. if xsu.mutation.APIKeyCleared() {
  282. _spec.ClearField(xunjiservice.FieldAPIKey, field.TypeString)
  283. }
  284. if xsu.mutation.AgentCleared() {
  285. edge := &sqlgraph.EdgeSpec{
  286. Rel: sqlgraph.M2O,
  287. Inverse: true,
  288. Table: xunjiservice.AgentTable,
  289. Columns: []string{xunjiservice.AgentColumn},
  290. Bidi: false,
  291. Target: &sqlgraph.EdgeTarget{
  292. IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
  293. },
  294. }
  295. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  296. }
  297. if nodes := xsu.mutation.AgentIDs(); len(nodes) > 0 {
  298. edge := &sqlgraph.EdgeSpec{
  299. Rel: sqlgraph.M2O,
  300. Inverse: true,
  301. Table: xunjiservice.AgentTable,
  302. Columns: []string{xunjiservice.AgentColumn},
  303. Bidi: false,
  304. Target: &sqlgraph.EdgeTarget{
  305. IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
  306. },
  307. }
  308. for _, k := range nodes {
  309. edge.Target.Nodes = append(edge.Target.Nodes, k)
  310. }
  311. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  312. }
  313. if n, err = sqlgraph.UpdateNodes(ctx, xsu.driver, _spec); err != nil {
  314. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  315. err = &NotFoundError{xunjiservice.Label}
  316. } else if sqlgraph.IsConstraintError(err) {
  317. err = &ConstraintError{msg: err.Error(), wrap: err}
  318. }
  319. return 0, err
  320. }
  321. xsu.mutation.done = true
  322. return n, nil
  323. }
  324. // XunjiServiceUpdateOne is the builder for updating a single XunjiService entity.
  325. type XunjiServiceUpdateOne struct {
  326. config
  327. fields []string
  328. hooks []Hook
  329. mutation *XunjiServiceMutation
  330. }
  331. // SetUpdatedAt sets the "updated_at" field.
  332. func (xsuo *XunjiServiceUpdateOne) SetUpdatedAt(t time.Time) *XunjiServiceUpdateOne {
  333. xsuo.mutation.SetUpdatedAt(t)
  334. return xsuo
  335. }
  336. // SetStatus sets the "status" field.
  337. func (xsuo *XunjiServiceUpdateOne) SetStatus(u uint8) *XunjiServiceUpdateOne {
  338. xsuo.mutation.ResetStatus()
  339. xsuo.mutation.SetStatus(u)
  340. return xsuo
  341. }
  342. // SetNillableStatus sets the "status" field if the given value is not nil.
  343. func (xsuo *XunjiServiceUpdateOne) SetNillableStatus(u *uint8) *XunjiServiceUpdateOne {
  344. if u != nil {
  345. xsuo.SetStatus(*u)
  346. }
  347. return xsuo
  348. }
  349. // AddStatus adds u to the "status" field.
  350. func (xsuo *XunjiServiceUpdateOne) AddStatus(u int8) *XunjiServiceUpdateOne {
  351. xsuo.mutation.AddStatus(u)
  352. return xsuo
  353. }
  354. // ClearStatus clears the value of the "status" field.
  355. func (xsuo *XunjiServiceUpdateOne) ClearStatus() *XunjiServiceUpdateOne {
  356. xsuo.mutation.ClearStatus()
  357. return xsuo
  358. }
  359. // SetDeletedAt sets the "deleted_at" field.
  360. func (xsuo *XunjiServiceUpdateOne) SetDeletedAt(t time.Time) *XunjiServiceUpdateOne {
  361. xsuo.mutation.SetDeletedAt(t)
  362. return xsuo
  363. }
  364. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  365. func (xsuo *XunjiServiceUpdateOne) SetNillableDeletedAt(t *time.Time) *XunjiServiceUpdateOne {
  366. if t != nil {
  367. xsuo.SetDeletedAt(*t)
  368. }
  369. return xsuo
  370. }
  371. // ClearDeletedAt clears the value of the "deleted_at" field.
  372. func (xsuo *XunjiServiceUpdateOne) ClearDeletedAt() *XunjiServiceUpdateOne {
  373. xsuo.mutation.ClearDeletedAt()
  374. return xsuo
  375. }
  376. // SetXunjiID sets the "xunji_id" field.
  377. func (xsuo *XunjiServiceUpdateOne) SetXunjiID(u uint64) *XunjiServiceUpdateOne {
  378. xsuo.mutation.ResetXunjiID()
  379. xsuo.mutation.SetXunjiID(u)
  380. return xsuo
  381. }
  382. // SetNillableXunjiID sets the "xunji_id" field if the given value is not nil.
  383. func (xsuo *XunjiServiceUpdateOne) SetNillableXunjiID(u *uint64) *XunjiServiceUpdateOne {
  384. if u != nil {
  385. xsuo.SetXunjiID(*u)
  386. }
  387. return xsuo
  388. }
  389. // AddXunjiID adds u to the "xunji_id" field.
  390. func (xsuo *XunjiServiceUpdateOne) AddXunjiID(u int64) *XunjiServiceUpdateOne {
  391. xsuo.mutation.AddXunjiID(u)
  392. return xsuo
  393. }
  394. // SetAgentID sets the "agent_id" field.
  395. func (xsuo *XunjiServiceUpdateOne) SetAgentID(u uint64) *XunjiServiceUpdateOne {
  396. xsuo.mutation.SetAgentID(u)
  397. return xsuo
  398. }
  399. // SetNillableAgentID sets the "agent_id" field if the given value is not nil.
  400. func (xsuo *XunjiServiceUpdateOne) SetNillableAgentID(u *uint64) *XunjiServiceUpdateOne {
  401. if u != nil {
  402. xsuo.SetAgentID(*u)
  403. }
  404. return xsuo
  405. }
  406. // SetOrganizationID sets the "organization_id" field.
  407. func (xsuo *XunjiServiceUpdateOne) SetOrganizationID(u uint64) *XunjiServiceUpdateOne {
  408. xsuo.mutation.ResetOrganizationID()
  409. xsuo.mutation.SetOrganizationID(u)
  410. return xsuo
  411. }
  412. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  413. func (xsuo *XunjiServiceUpdateOne) SetNillableOrganizationID(u *uint64) *XunjiServiceUpdateOne {
  414. if u != nil {
  415. xsuo.SetOrganizationID(*u)
  416. }
  417. return xsuo
  418. }
  419. // AddOrganizationID adds u to the "organization_id" field.
  420. func (xsuo *XunjiServiceUpdateOne) AddOrganizationID(u int64) *XunjiServiceUpdateOne {
  421. xsuo.mutation.AddOrganizationID(u)
  422. return xsuo
  423. }
  424. // SetWxid sets the "wxid" field.
  425. func (xsuo *XunjiServiceUpdateOne) SetWxid(s string) *XunjiServiceUpdateOne {
  426. xsuo.mutation.SetWxid(s)
  427. return xsuo
  428. }
  429. // SetNillableWxid sets the "wxid" field if the given value is not nil.
  430. func (xsuo *XunjiServiceUpdateOne) SetNillableWxid(s *string) *XunjiServiceUpdateOne {
  431. if s != nil {
  432. xsuo.SetWxid(*s)
  433. }
  434. return xsuo
  435. }
  436. // SetAPIBase sets the "api_base" field.
  437. func (xsuo *XunjiServiceUpdateOne) SetAPIBase(s string) *XunjiServiceUpdateOne {
  438. xsuo.mutation.SetAPIBase(s)
  439. return xsuo
  440. }
  441. // SetNillableAPIBase sets the "api_base" field if the given value is not nil.
  442. func (xsuo *XunjiServiceUpdateOne) SetNillableAPIBase(s *string) *XunjiServiceUpdateOne {
  443. if s != nil {
  444. xsuo.SetAPIBase(*s)
  445. }
  446. return xsuo
  447. }
  448. // ClearAPIBase clears the value of the "api_base" field.
  449. func (xsuo *XunjiServiceUpdateOne) ClearAPIBase() *XunjiServiceUpdateOne {
  450. xsuo.mutation.ClearAPIBase()
  451. return xsuo
  452. }
  453. // SetAPIKey sets the "api_key" field.
  454. func (xsuo *XunjiServiceUpdateOne) SetAPIKey(s string) *XunjiServiceUpdateOne {
  455. xsuo.mutation.SetAPIKey(s)
  456. return xsuo
  457. }
  458. // SetNillableAPIKey sets the "api_key" field if the given value is not nil.
  459. func (xsuo *XunjiServiceUpdateOne) SetNillableAPIKey(s *string) *XunjiServiceUpdateOne {
  460. if s != nil {
  461. xsuo.SetAPIKey(*s)
  462. }
  463. return xsuo
  464. }
  465. // ClearAPIKey clears the value of the "api_key" field.
  466. func (xsuo *XunjiServiceUpdateOne) ClearAPIKey() *XunjiServiceUpdateOne {
  467. xsuo.mutation.ClearAPIKey()
  468. return xsuo
  469. }
  470. // SetAgent sets the "agent" edge to the Agent entity.
  471. func (xsuo *XunjiServiceUpdateOne) SetAgent(a *Agent) *XunjiServiceUpdateOne {
  472. return xsuo.SetAgentID(a.ID)
  473. }
  474. // Mutation returns the XunjiServiceMutation object of the builder.
  475. func (xsuo *XunjiServiceUpdateOne) Mutation() *XunjiServiceMutation {
  476. return xsuo.mutation
  477. }
  478. // ClearAgent clears the "agent" edge to the Agent entity.
  479. func (xsuo *XunjiServiceUpdateOne) ClearAgent() *XunjiServiceUpdateOne {
  480. xsuo.mutation.ClearAgent()
  481. return xsuo
  482. }
  483. // Where appends a list predicates to the XunjiServiceUpdate builder.
  484. func (xsuo *XunjiServiceUpdateOne) Where(ps ...predicate.XunjiService) *XunjiServiceUpdateOne {
  485. xsuo.mutation.Where(ps...)
  486. return xsuo
  487. }
  488. // Select allows selecting one or more fields (columns) of the returned entity.
  489. // The default is selecting all fields defined in the entity schema.
  490. func (xsuo *XunjiServiceUpdateOne) Select(field string, fields ...string) *XunjiServiceUpdateOne {
  491. xsuo.fields = append([]string{field}, fields...)
  492. return xsuo
  493. }
  494. // Save executes the query and returns the updated XunjiService entity.
  495. func (xsuo *XunjiServiceUpdateOne) Save(ctx context.Context) (*XunjiService, error) {
  496. if err := xsuo.defaults(); err != nil {
  497. return nil, err
  498. }
  499. return withHooks(ctx, xsuo.sqlSave, xsuo.mutation, xsuo.hooks)
  500. }
  501. // SaveX is like Save, but panics if an error occurs.
  502. func (xsuo *XunjiServiceUpdateOne) SaveX(ctx context.Context) *XunjiService {
  503. node, err := xsuo.Save(ctx)
  504. if err != nil {
  505. panic(err)
  506. }
  507. return node
  508. }
  509. // Exec executes the query on the entity.
  510. func (xsuo *XunjiServiceUpdateOne) Exec(ctx context.Context) error {
  511. _, err := xsuo.Save(ctx)
  512. return err
  513. }
  514. // ExecX is like Exec, but panics if an error occurs.
  515. func (xsuo *XunjiServiceUpdateOne) ExecX(ctx context.Context) {
  516. if err := xsuo.Exec(ctx); err != nil {
  517. panic(err)
  518. }
  519. }
  520. // defaults sets the default values of the builder before save.
  521. func (xsuo *XunjiServiceUpdateOne) defaults() error {
  522. if _, ok := xsuo.mutation.UpdatedAt(); !ok {
  523. if xunjiservice.UpdateDefaultUpdatedAt == nil {
  524. return fmt.Errorf("ent: uninitialized xunjiservice.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  525. }
  526. v := xunjiservice.UpdateDefaultUpdatedAt()
  527. xsuo.mutation.SetUpdatedAt(v)
  528. }
  529. return nil
  530. }
  531. // check runs all checks and user-defined validators on the builder.
  532. func (xsuo *XunjiServiceUpdateOne) check() error {
  533. if v, ok := xsuo.mutation.OrganizationID(); ok {
  534. if err := xunjiservice.OrganizationIDValidator(v); err != nil {
  535. return &ValidationError{Name: "organization_id", err: fmt.Errorf(`ent: validator failed for field "XunjiService.organization_id": %w`, err)}
  536. }
  537. }
  538. if _, ok := xsuo.mutation.AgentID(); xsuo.mutation.AgentCleared() && !ok {
  539. return errors.New(`ent: clearing a required unique edge "XunjiService.agent"`)
  540. }
  541. return nil
  542. }
  543. func (xsuo *XunjiServiceUpdateOne) sqlSave(ctx context.Context) (_node *XunjiService, err error) {
  544. if err := xsuo.check(); err != nil {
  545. return _node, err
  546. }
  547. _spec := sqlgraph.NewUpdateSpec(xunjiservice.Table, xunjiservice.Columns, sqlgraph.NewFieldSpec(xunjiservice.FieldID, field.TypeUint64))
  548. id, ok := xsuo.mutation.ID()
  549. if !ok {
  550. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "XunjiService.id" for update`)}
  551. }
  552. _spec.Node.ID.Value = id
  553. if fields := xsuo.fields; len(fields) > 0 {
  554. _spec.Node.Columns = make([]string, 0, len(fields))
  555. _spec.Node.Columns = append(_spec.Node.Columns, xunjiservice.FieldID)
  556. for _, f := range fields {
  557. if !xunjiservice.ValidColumn(f) {
  558. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  559. }
  560. if f != xunjiservice.FieldID {
  561. _spec.Node.Columns = append(_spec.Node.Columns, f)
  562. }
  563. }
  564. }
  565. if ps := xsuo.mutation.predicates; len(ps) > 0 {
  566. _spec.Predicate = func(selector *sql.Selector) {
  567. for i := range ps {
  568. ps[i](selector)
  569. }
  570. }
  571. }
  572. if value, ok := xsuo.mutation.UpdatedAt(); ok {
  573. _spec.SetField(xunjiservice.FieldUpdatedAt, field.TypeTime, value)
  574. }
  575. if value, ok := xsuo.mutation.Status(); ok {
  576. _spec.SetField(xunjiservice.FieldStatus, field.TypeUint8, value)
  577. }
  578. if value, ok := xsuo.mutation.AddedStatus(); ok {
  579. _spec.AddField(xunjiservice.FieldStatus, field.TypeUint8, value)
  580. }
  581. if xsuo.mutation.StatusCleared() {
  582. _spec.ClearField(xunjiservice.FieldStatus, field.TypeUint8)
  583. }
  584. if value, ok := xsuo.mutation.DeletedAt(); ok {
  585. _spec.SetField(xunjiservice.FieldDeletedAt, field.TypeTime, value)
  586. }
  587. if xsuo.mutation.DeletedAtCleared() {
  588. _spec.ClearField(xunjiservice.FieldDeletedAt, field.TypeTime)
  589. }
  590. if value, ok := xsuo.mutation.XunjiID(); ok {
  591. _spec.SetField(xunjiservice.FieldXunjiID, field.TypeUint64, value)
  592. }
  593. if value, ok := xsuo.mutation.AddedXunjiID(); ok {
  594. _spec.AddField(xunjiservice.FieldXunjiID, field.TypeUint64, value)
  595. }
  596. if value, ok := xsuo.mutation.OrganizationID(); ok {
  597. _spec.SetField(xunjiservice.FieldOrganizationID, field.TypeUint64, value)
  598. }
  599. if value, ok := xsuo.mutation.AddedOrganizationID(); ok {
  600. _spec.AddField(xunjiservice.FieldOrganizationID, field.TypeUint64, value)
  601. }
  602. if value, ok := xsuo.mutation.Wxid(); ok {
  603. _spec.SetField(xunjiservice.FieldWxid, field.TypeString, value)
  604. }
  605. if value, ok := xsuo.mutation.APIBase(); ok {
  606. _spec.SetField(xunjiservice.FieldAPIBase, field.TypeString, value)
  607. }
  608. if xsuo.mutation.APIBaseCleared() {
  609. _spec.ClearField(xunjiservice.FieldAPIBase, field.TypeString)
  610. }
  611. if value, ok := xsuo.mutation.APIKey(); ok {
  612. _spec.SetField(xunjiservice.FieldAPIKey, field.TypeString, value)
  613. }
  614. if xsuo.mutation.APIKeyCleared() {
  615. _spec.ClearField(xunjiservice.FieldAPIKey, field.TypeString)
  616. }
  617. if xsuo.mutation.AgentCleared() {
  618. edge := &sqlgraph.EdgeSpec{
  619. Rel: sqlgraph.M2O,
  620. Inverse: true,
  621. Table: xunjiservice.AgentTable,
  622. Columns: []string{xunjiservice.AgentColumn},
  623. Bidi: false,
  624. Target: &sqlgraph.EdgeTarget{
  625. IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
  626. },
  627. }
  628. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  629. }
  630. if nodes := xsuo.mutation.AgentIDs(); len(nodes) > 0 {
  631. edge := &sqlgraph.EdgeSpec{
  632. Rel: sqlgraph.M2O,
  633. Inverse: true,
  634. Table: xunjiservice.AgentTable,
  635. Columns: []string{xunjiservice.AgentColumn},
  636. Bidi: false,
  637. Target: &sqlgraph.EdgeTarget{
  638. IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
  639. },
  640. }
  641. for _, k := range nodes {
  642. edge.Target.Nodes = append(edge.Target.Nodes, k)
  643. }
  644. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  645. }
  646. _node = &XunjiService{config: xsuo.config}
  647. _spec.Assign = _node.assignValues
  648. _spec.ScanValues = _node.scanValues
  649. if err = sqlgraph.UpdateNode(ctx, xsuo.driver, _spec); err != nil {
  650. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  651. err = &NotFoundError{xunjiservice.Label}
  652. } else if sqlgraph.IsConstraintError(err) {
  653. err = &ConstraintError{msg: err.Error(), wrap: err}
  654. }
  655. return nil, err
  656. }
  657. xsuo.mutation.done = true
  658. return _node, nil
  659. }