creditusage_update.go 23 KB

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