xunji_update.go 21 KB

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