creditusage_update.go 24 KB

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