creditusage_update.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. // SetPayNumber sets the "pay_number" field.
  83. func (cuu *CreditUsageUpdate) SetPayNumber(f float32) *CreditUsageUpdate {
  84. cuu.mutation.ResetPayNumber()
  85. cuu.mutation.SetPayNumber(f)
  86. return cuu
  87. }
  88. // SetNillablePayNumber sets the "pay_number" field if the given value is not nil.
  89. func (cuu *CreditUsageUpdate) SetNillablePayNumber(f *float32) *CreditUsageUpdate {
  90. if f != nil {
  91. cuu.SetPayNumber(*f)
  92. }
  93. return cuu
  94. }
  95. // AddPayNumber adds f to the "pay_number" field.
  96. func (cuu *CreditUsageUpdate) AddPayNumber(f float32) *CreditUsageUpdate {
  97. cuu.mutation.AddPayNumber(f)
  98. return cuu
  99. }
  100. // SetBeforeNumber sets the "before_number" field.
  101. func (cuu *CreditUsageUpdate) SetBeforeNumber(f float32) *CreditUsageUpdate {
  102. cuu.mutation.ResetBeforeNumber()
  103. cuu.mutation.SetBeforeNumber(f)
  104. return cuu
  105. }
  106. // SetNillableBeforeNumber sets the "before_number" field if the given value is not nil.
  107. func (cuu *CreditUsageUpdate) SetNillableBeforeNumber(f *float32) *CreditUsageUpdate {
  108. if f != nil {
  109. cuu.SetBeforeNumber(*f)
  110. }
  111. return cuu
  112. }
  113. // AddBeforeNumber adds f to the "before_number" field.
  114. func (cuu *CreditUsageUpdate) AddBeforeNumber(f float32) *CreditUsageUpdate {
  115. cuu.mutation.AddBeforeNumber(f)
  116. return cuu
  117. }
  118. // SetAfterNumber sets the "after_number" field.
  119. func (cuu *CreditUsageUpdate) SetAfterNumber(f float32) *CreditUsageUpdate {
  120. cuu.mutation.ResetAfterNumber()
  121. cuu.mutation.SetAfterNumber(f)
  122. return cuu
  123. }
  124. // SetNillableAfterNumber sets the "after_number" field if the given value is not nil.
  125. func (cuu *CreditUsageUpdate) SetNillableAfterNumber(f *float32) *CreditUsageUpdate {
  126. if f != nil {
  127. cuu.SetAfterNumber(*f)
  128. }
  129. return cuu
  130. }
  131. // AddAfterNumber adds f to the "after_number" field.
  132. func (cuu *CreditUsageUpdate) AddAfterNumber(f float32) *CreditUsageUpdate {
  133. cuu.mutation.AddAfterNumber(f)
  134. return cuu
  135. }
  136. // SetPayMethod sets the "pay_method" field.
  137. func (cuu *CreditUsageUpdate) SetPayMethod(s string) *CreditUsageUpdate {
  138. cuu.mutation.SetPayMethod(s)
  139. return cuu
  140. }
  141. // SetNillablePayMethod sets the "pay_method" field if the given value is not nil.
  142. func (cuu *CreditUsageUpdate) SetNillablePayMethod(s *string) *CreditUsageUpdate {
  143. if s != nil {
  144. cuu.SetPayMethod(*s)
  145. }
  146. return cuu
  147. }
  148. // SetStatus sets the "status" field.
  149. func (cuu *CreditUsageUpdate) SetStatus(i int) *CreditUsageUpdate {
  150. cuu.mutation.ResetStatus()
  151. cuu.mutation.SetStatus(i)
  152. return cuu
  153. }
  154. // SetNillableStatus sets the "status" field if the given value is not nil.
  155. func (cuu *CreditUsageUpdate) SetNillableStatus(i *int) *CreditUsageUpdate {
  156. if i != nil {
  157. cuu.SetStatus(*i)
  158. }
  159. return cuu
  160. }
  161. // AddStatus adds i to the "status" field.
  162. func (cuu *CreditUsageUpdate) AddStatus(i int) *CreditUsageUpdate {
  163. cuu.mutation.AddStatus(i)
  164. return cuu
  165. }
  166. // ClearStatus clears the value of the "status" field.
  167. func (cuu *CreditUsageUpdate) ClearStatus() *CreditUsageUpdate {
  168. cuu.mutation.ClearStatus()
  169. return cuu
  170. }
  171. // SetNtype sets the "ntype" field.
  172. func (cuu *CreditUsageUpdate) SetNtype(i int) *CreditUsageUpdate {
  173. cuu.mutation.ResetNtype()
  174. cuu.mutation.SetNtype(i)
  175. return cuu
  176. }
  177. // SetNillableNtype sets the "ntype" field if the given value is not nil.
  178. func (cuu *CreditUsageUpdate) SetNillableNtype(i *int) *CreditUsageUpdate {
  179. if i != nil {
  180. cuu.SetNtype(*i)
  181. }
  182. return cuu
  183. }
  184. // AddNtype adds i to the "ntype" field.
  185. func (cuu *CreditUsageUpdate) AddNtype(i int) *CreditUsageUpdate {
  186. cuu.mutation.AddNtype(i)
  187. return cuu
  188. }
  189. // SetTable sets the "table" field.
  190. func (cuu *CreditUsageUpdate) SetTable(s string) *CreditUsageUpdate {
  191. cuu.mutation.SetTable(s)
  192. return cuu
  193. }
  194. // SetNillableTable sets the "table" field if the given value is not nil.
  195. func (cuu *CreditUsageUpdate) SetNillableTable(s *string) *CreditUsageUpdate {
  196. if s != nil {
  197. cuu.SetTable(*s)
  198. }
  199. return cuu
  200. }
  201. // SetOrganizationID sets the "organization_id" field.
  202. func (cuu *CreditUsageUpdate) SetOrganizationID(u uint64) *CreditUsageUpdate {
  203. cuu.mutation.ResetOrganizationID()
  204. cuu.mutation.SetOrganizationID(u)
  205. return cuu
  206. }
  207. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  208. func (cuu *CreditUsageUpdate) SetNillableOrganizationID(u *uint64) *CreditUsageUpdate {
  209. if u != nil {
  210. cuu.SetOrganizationID(*u)
  211. }
  212. return cuu
  213. }
  214. // AddOrganizationID adds u to the "organization_id" field.
  215. func (cuu *CreditUsageUpdate) AddOrganizationID(u int64) *CreditUsageUpdate {
  216. cuu.mutation.AddOrganizationID(u)
  217. return cuu
  218. }
  219. // ClearOrganizationID clears the value of the "organization_id" field.
  220. func (cuu *CreditUsageUpdate) ClearOrganizationID() *CreditUsageUpdate {
  221. cuu.mutation.ClearOrganizationID()
  222. return cuu
  223. }
  224. // SetNid sets the "nid" field.
  225. func (cuu *CreditUsageUpdate) SetNid(u uint64) *CreditUsageUpdate {
  226. cuu.mutation.ResetNid()
  227. cuu.mutation.SetNid(u)
  228. return cuu
  229. }
  230. // SetNillableNid sets the "nid" field if the given value is not nil.
  231. func (cuu *CreditUsageUpdate) SetNillableNid(u *uint64) *CreditUsageUpdate {
  232. if u != nil {
  233. cuu.SetNid(*u)
  234. }
  235. return cuu
  236. }
  237. // AddNid adds u to the "nid" field.
  238. func (cuu *CreditUsageUpdate) AddNid(u int64) *CreditUsageUpdate {
  239. cuu.mutation.AddNid(u)
  240. return cuu
  241. }
  242. // SetReason sets the "reason" field.
  243. func (cuu *CreditUsageUpdate) SetReason(s string) *CreditUsageUpdate {
  244. cuu.mutation.SetReason(s)
  245. return cuu
  246. }
  247. // SetNillableReason sets the "reason" field if the given value is not nil.
  248. func (cuu *CreditUsageUpdate) SetNillableReason(s *string) *CreditUsageUpdate {
  249. if s != nil {
  250. cuu.SetReason(*s)
  251. }
  252. return cuu
  253. }
  254. // SetOperator sets the "operator" field.
  255. func (cuu *CreditUsageUpdate) SetOperator(s string) *CreditUsageUpdate {
  256. cuu.mutation.SetOperator(s)
  257. return cuu
  258. }
  259. // SetNillableOperator sets the "operator" field if the given value is not nil.
  260. func (cuu *CreditUsageUpdate) SetNillableOperator(s *string) *CreditUsageUpdate {
  261. if s != nil {
  262. cuu.SetOperator(*s)
  263. }
  264. return cuu
  265. }
  266. // Mutation returns the CreditUsageMutation object of the builder.
  267. func (cuu *CreditUsageUpdate) Mutation() *CreditUsageMutation {
  268. return cuu.mutation
  269. }
  270. // Save executes the query and returns the number of nodes affected by the update operation.
  271. func (cuu *CreditUsageUpdate) Save(ctx context.Context) (int, error) {
  272. if err := cuu.defaults(); err != nil {
  273. return 0, err
  274. }
  275. return withHooks(ctx, cuu.sqlSave, cuu.mutation, cuu.hooks)
  276. }
  277. // SaveX is like Save, but panics if an error occurs.
  278. func (cuu *CreditUsageUpdate) SaveX(ctx context.Context) int {
  279. affected, err := cuu.Save(ctx)
  280. if err != nil {
  281. panic(err)
  282. }
  283. return affected
  284. }
  285. // Exec executes the query.
  286. func (cuu *CreditUsageUpdate) Exec(ctx context.Context) error {
  287. _, err := cuu.Save(ctx)
  288. return err
  289. }
  290. // ExecX is like Exec, but panics if an error occurs.
  291. func (cuu *CreditUsageUpdate) ExecX(ctx context.Context) {
  292. if err := cuu.Exec(ctx); err != nil {
  293. panic(err)
  294. }
  295. }
  296. // defaults sets the default values of the builder before save.
  297. func (cuu *CreditUsageUpdate) defaults() error {
  298. if _, ok := cuu.mutation.UpdatedAt(); !ok {
  299. if creditusage.UpdateDefaultUpdatedAt == nil {
  300. return fmt.Errorf("ent: uninitialized creditusage.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  301. }
  302. v := creditusage.UpdateDefaultUpdatedAt()
  303. cuu.mutation.SetUpdatedAt(v)
  304. }
  305. return nil
  306. }
  307. // check runs all checks and user-defined validators on the builder.
  308. func (cuu *CreditUsageUpdate) check() error {
  309. if v, ok := cuu.mutation.UserID(); ok {
  310. if err := creditusage.UserIDValidator(v); err != nil {
  311. return &ValidationError{Name: "user_id", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.user_id": %w`, err)}
  312. }
  313. }
  314. if v, ok := cuu.mutation.Status(); ok {
  315. if err := creditusage.StatusValidator(v); err != nil {
  316. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.status": %w`, err)}
  317. }
  318. }
  319. if v, ok := cuu.mutation.Reason(); ok {
  320. if err := creditusage.ReasonValidator(v); err != nil {
  321. return &ValidationError{Name: "reason", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.reason": %w`, err)}
  322. }
  323. }
  324. if v, ok := cuu.mutation.Operator(); ok {
  325. if err := creditusage.OperatorValidator(v); err != nil {
  326. return &ValidationError{Name: "operator", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.operator": %w`, err)}
  327. }
  328. }
  329. return nil
  330. }
  331. func (cuu *CreditUsageUpdate) sqlSave(ctx context.Context) (n int, err error) {
  332. if err := cuu.check(); err != nil {
  333. return n, err
  334. }
  335. _spec := sqlgraph.NewUpdateSpec(creditusage.Table, creditusage.Columns, sqlgraph.NewFieldSpec(creditusage.FieldID, field.TypeUint64))
  336. if ps := cuu.mutation.predicates; len(ps) > 0 {
  337. _spec.Predicate = func(selector *sql.Selector) {
  338. for i := range ps {
  339. ps[i](selector)
  340. }
  341. }
  342. }
  343. if value, ok := cuu.mutation.UpdatedAt(); ok {
  344. _spec.SetField(creditusage.FieldUpdatedAt, field.TypeTime, value)
  345. }
  346. if value, ok := cuu.mutation.DeletedAt(); ok {
  347. _spec.SetField(creditusage.FieldDeletedAt, field.TypeTime, value)
  348. }
  349. if cuu.mutation.DeletedAtCleared() {
  350. _spec.ClearField(creditusage.FieldDeletedAt, field.TypeTime)
  351. }
  352. if value, ok := cuu.mutation.UserID(); ok {
  353. _spec.SetField(creditusage.FieldUserID, field.TypeString, value)
  354. }
  355. if cuu.mutation.UserIDCleared() {
  356. _spec.ClearField(creditusage.FieldUserID, field.TypeString)
  357. }
  358. if value, ok := cuu.mutation.Number(); ok {
  359. _spec.SetField(creditusage.FieldNumber, field.TypeFloat32, value)
  360. }
  361. if value, ok := cuu.mutation.AddedNumber(); ok {
  362. _spec.AddField(creditusage.FieldNumber, field.TypeFloat32, value)
  363. }
  364. if value, ok := cuu.mutation.PayNumber(); ok {
  365. _spec.SetField(creditusage.FieldPayNumber, field.TypeFloat32, value)
  366. }
  367. if value, ok := cuu.mutation.AddedPayNumber(); ok {
  368. _spec.AddField(creditusage.FieldPayNumber, field.TypeFloat32, value)
  369. }
  370. if value, ok := cuu.mutation.BeforeNumber(); ok {
  371. _spec.SetField(creditusage.FieldBeforeNumber, field.TypeFloat32, value)
  372. }
  373. if value, ok := cuu.mutation.AddedBeforeNumber(); ok {
  374. _spec.AddField(creditusage.FieldBeforeNumber, field.TypeFloat32, value)
  375. }
  376. if value, ok := cuu.mutation.AfterNumber(); ok {
  377. _spec.SetField(creditusage.FieldAfterNumber, field.TypeFloat32, value)
  378. }
  379. if value, ok := cuu.mutation.AddedAfterNumber(); ok {
  380. _spec.AddField(creditusage.FieldAfterNumber, field.TypeFloat32, value)
  381. }
  382. if value, ok := cuu.mutation.PayMethod(); ok {
  383. _spec.SetField(creditusage.FieldPayMethod, field.TypeString, value)
  384. }
  385. if value, ok := cuu.mutation.Status(); ok {
  386. _spec.SetField(creditusage.FieldStatus, field.TypeInt, value)
  387. }
  388. if value, ok := cuu.mutation.AddedStatus(); ok {
  389. _spec.AddField(creditusage.FieldStatus, field.TypeInt, value)
  390. }
  391. if cuu.mutation.StatusCleared() {
  392. _spec.ClearField(creditusage.FieldStatus, field.TypeInt)
  393. }
  394. if value, ok := cuu.mutation.Ntype(); ok {
  395. _spec.SetField(creditusage.FieldNtype, field.TypeInt, value)
  396. }
  397. if value, ok := cuu.mutation.AddedNtype(); ok {
  398. _spec.AddField(creditusage.FieldNtype, field.TypeInt, value)
  399. }
  400. if value, ok := cuu.mutation.Table(); ok {
  401. _spec.SetField(creditusage.FieldTable, field.TypeString, value)
  402. }
  403. if value, ok := cuu.mutation.OrganizationID(); ok {
  404. _spec.SetField(creditusage.FieldOrganizationID, field.TypeUint64, value)
  405. }
  406. if value, ok := cuu.mutation.AddedOrganizationID(); ok {
  407. _spec.AddField(creditusage.FieldOrganizationID, field.TypeUint64, value)
  408. }
  409. if cuu.mutation.OrganizationIDCleared() {
  410. _spec.ClearField(creditusage.FieldOrganizationID, field.TypeUint64)
  411. }
  412. if value, ok := cuu.mutation.Nid(); ok {
  413. _spec.SetField(creditusage.FieldNid, field.TypeUint64, value)
  414. }
  415. if value, ok := cuu.mutation.AddedNid(); ok {
  416. _spec.AddField(creditusage.FieldNid, field.TypeUint64, value)
  417. }
  418. if value, ok := cuu.mutation.Reason(); ok {
  419. _spec.SetField(creditusage.FieldReason, field.TypeString, value)
  420. }
  421. if value, ok := cuu.mutation.Operator(); ok {
  422. _spec.SetField(creditusage.FieldOperator, field.TypeString, value)
  423. }
  424. if n, err = sqlgraph.UpdateNodes(ctx, cuu.driver, _spec); err != nil {
  425. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  426. err = &NotFoundError{creditusage.Label}
  427. } else if sqlgraph.IsConstraintError(err) {
  428. err = &ConstraintError{msg: err.Error(), wrap: err}
  429. }
  430. return 0, err
  431. }
  432. cuu.mutation.done = true
  433. return n, nil
  434. }
  435. // CreditUsageUpdateOne is the builder for updating a single CreditUsage entity.
  436. type CreditUsageUpdateOne struct {
  437. config
  438. fields []string
  439. hooks []Hook
  440. mutation *CreditUsageMutation
  441. }
  442. // SetUpdatedAt sets the "updated_at" field.
  443. func (cuuo *CreditUsageUpdateOne) SetUpdatedAt(t time.Time) *CreditUsageUpdateOne {
  444. cuuo.mutation.SetUpdatedAt(t)
  445. return cuuo
  446. }
  447. // SetDeletedAt sets the "deleted_at" field.
  448. func (cuuo *CreditUsageUpdateOne) SetDeletedAt(t time.Time) *CreditUsageUpdateOne {
  449. cuuo.mutation.SetDeletedAt(t)
  450. return cuuo
  451. }
  452. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  453. func (cuuo *CreditUsageUpdateOne) SetNillableDeletedAt(t *time.Time) *CreditUsageUpdateOne {
  454. if t != nil {
  455. cuuo.SetDeletedAt(*t)
  456. }
  457. return cuuo
  458. }
  459. // ClearDeletedAt clears the value of the "deleted_at" field.
  460. func (cuuo *CreditUsageUpdateOne) ClearDeletedAt() *CreditUsageUpdateOne {
  461. cuuo.mutation.ClearDeletedAt()
  462. return cuuo
  463. }
  464. // SetUserID sets the "user_id" field.
  465. func (cuuo *CreditUsageUpdateOne) SetUserID(s string) *CreditUsageUpdateOne {
  466. cuuo.mutation.SetUserID(s)
  467. return cuuo
  468. }
  469. // SetNillableUserID sets the "user_id" field if the given value is not nil.
  470. func (cuuo *CreditUsageUpdateOne) SetNillableUserID(s *string) *CreditUsageUpdateOne {
  471. if s != nil {
  472. cuuo.SetUserID(*s)
  473. }
  474. return cuuo
  475. }
  476. // ClearUserID clears the value of the "user_id" field.
  477. func (cuuo *CreditUsageUpdateOne) ClearUserID() *CreditUsageUpdateOne {
  478. cuuo.mutation.ClearUserID()
  479. return cuuo
  480. }
  481. // SetNumber sets the "number" field.
  482. func (cuuo *CreditUsageUpdateOne) SetNumber(f float32) *CreditUsageUpdateOne {
  483. cuuo.mutation.ResetNumber()
  484. cuuo.mutation.SetNumber(f)
  485. return cuuo
  486. }
  487. // SetNillableNumber sets the "number" field if the given value is not nil.
  488. func (cuuo *CreditUsageUpdateOne) SetNillableNumber(f *float32) *CreditUsageUpdateOne {
  489. if f != nil {
  490. cuuo.SetNumber(*f)
  491. }
  492. return cuuo
  493. }
  494. // AddNumber adds f to the "number" field.
  495. func (cuuo *CreditUsageUpdateOne) AddNumber(f float32) *CreditUsageUpdateOne {
  496. cuuo.mutation.AddNumber(f)
  497. return cuuo
  498. }
  499. // SetPayNumber sets the "pay_number" field.
  500. func (cuuo *CreditUsageUpdateOne) SetPayNumber(f float32) *CreditUsageUpdateOne {
  501. cuuo.mutation.ResetPayNumber()
  502. cuuo.mutation.SetPayNumber(f)
  503. return cuuo
  504. }
  505. // SetNillablePayNumber sets the "pay_number" field if the given value is not nil.
  506. func (cuuo *CreditUsageUpdateOne) SetNillablePayNumber(f *float32) *CreditUsageUpdateOne {
  507. if f != nil {
  508. cuuo.SetPayNumber(*f)
  509. }
  510. return cuuo
  511. }
  512. // AddPayNumber adds f to the "pay_number" field.
  513. func (cuuo *CreditUsageUpdateOne) AddPayNumber(f float32) *CreditUsageUpdateOne {
  514. cuuo.mutation.AddPayNumber(f)
  515. return cuuo
  516. }
  517. // SetBeforeNumber sets the "before_number" field.
  518. func (cuuo *CreditUsageUpdateOne) SetBeforeNumber(f float32) *CreditUsageUpdateOne {
  519. cuuo.mutation.ResetBeforeNumber()
  520. cuuo.mutation.SetBeforeNumber(f)
  521. return cuuo
  522. }
  523. // SetNillableBeforeNumber sets the "before_number" field if the given value is not nil.
  524. func (cuuo *CreditUsageUpdateOne) SetNillableBeforeNumber(f *float32) *CreditUsageUpdateOne {
  525. if f != nil {
  526. cuuo.SetBeforeNumber(*f)
  527. }
  528. return cuuo
  529. }
  530. // AddBeforeNumber adds f to the "before_number" field.
  531. func (cuuo *CreditUsageUpdateOne) AddBeforeNumber(f float32) *CreditUsageUpdateOne {
  532. cuuo.mutation.AddBeforeNumber(f)
  533. return cuuo
  534. }
  535. // SetAfterNumber sets the "after_number" field.
  536. func (cuuo *CreditUsageUpdateOne) SetAfterNumber(f float32) *CreditUsageUpdateOne {
  537. cuuo.mutation.ResetAfterNumber()
  538. cuuo.mutation.SetAfterNumber(f)
  539. return cuuo
  540. }
  541. // SetNillableAfterNumber sets the "after_number" field if the given value is not nil.
  542. func (cuuo *CreditUsageUpdateOne) SetNillableAfterNumber(f *float32) *CreditUsageUpdateOne {
  543. if f != nil {
  544. cuuo.SetAfterNumber(*f)
  545. }
  546. return cuuo
  547. }
  548. // AddAfterNumber adds f to the "after_number" field.
  549. func (cuuo *CreditUsageUpdateOne) AddAfterNumber(f float32) *CreditUsageUpdateOne {
  550. cuuo.mutation.AddAfterNumber(f)
  551. return cuuo
  552. }
  553. // SetPayMethod sets the "pay_method" field.
  554. func (cuuo *CreditUsageUpdateOne) SetPayMethod(s string) *CreditUsageUpdateOne {
  555. cuuo.mutation.SetPayMethod(s)
  556. return cuuo
  557. }
  558. // SetNillablePayMethod sets the "pay_method" field if the given value is not nil.
  559. func (cuuo *CreditUsageUpdateOne) SetNillablePayMethod(s *string) *CreditUsageUpdateOne {
  560. if s != nil {
  561. cuuo.SetPayMethod(*s)
  562. }
  563. return cuuo
  564. }
  565. // SetStatus sets the "status" field.
  566. func (cuuo *CreditUsageUpdateOne) SetStatus(i int) *CreditUsageUpdateOne {
  567. cuuo.mutation.ResetStatus()
  568. cuuo.mutation.SetStatus(i)
  569. return cuuo
  570. }
  571. // SetNillableStatus sets the "status" field if the given value is not nil.
  572. func (cuuo *CreditUsageUpdateOne) SetNillableStatus(i *int) *CreditUsageUpdateOne {
  573. if i != nil {
  574. cuuo.SetStatus(*i)
  575. }
  576. return cuuo
  577. }
  578. // AddStatus adds i to the "status" field.
  579. func (cuuo *CreditUsageUpdateOne) AddStatus(i int) *CreditUsageUpdateOne {
  580. cuuo.mutation.AddStatus(i)
  581. return cuuo
  582. }
  583. // ClearStatus clears the value of the "status" field.
  584. func (cuuo *CreditUsageUpdateOne) ClearStatus() *CreditUsageUpdateOne {
  585. cuuo.mutation.ClearStatus()
  586. return cuuo
  587. }
  588. // SetNtype sets the "ntype" field.
  589. func (cuuo *CreditUsageUpdateOne) SetNtype(i int) *CreditUsageUpdateOne {
  590. cuuo.mutation.ResetNtype()
  591. cuuo.mutation.SetNtype(i)
  592. return cuuo
  593. }
  594. // SetNillableNtype sets the "ntype" field if the given value is not nil.
  595. func (cuuo *CreditUsageUpdateOne) SetNillableNtype(i *int) *CreditUsageUpdateOne {
  596. if i != nil {
  597. cuuo.SetNtype(*i)
  598. }
  599. return cuuo
  600. }
  601. // AddNtype adds i to the "ntype" field.
  602. func (cuuo *CreditUsageUpdateOne) AddNtype(i int) *CreditUsageUpdateOne {
  603. cuuo.mutation.AddNtype(i)
  604. return cuuo
  605. }
  606. // SetTable sets the "table" field.
  607. func (cuuo *CreditUsageUpdateOne) SetTable(s string) *CreditUsageUpdateOne {
  608. cuuo.mutation.SetTable(s)
  609. return cuuo
  610. }
  611. // SetNillableTable sets the "table" field if the given value is not nil.
  612. func (cuuo *CreditUsageUpdateOne) SetNillableTable(s *string) *CreditUsageUpdateOne {
  613. if s != nil {
  614. cuuo.SetTable(*s)
  615. }
  616. return cuuo
  617. }
  618. // SetOrganizationID sets the "organization_id" field.
  619. func (cuuo *CreditUsageUpdateOne) SetOrganizationID(u uint64) *CreditUsageUpdateOne {
  620. cuuo.mutation.ResetOrganizationID()
  621. cuuo.mutation.SetOrganizationID(u)
  622. return cuuo
  623. }
  624. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  625. func (cuuo *CreditUsageUpdateOne) SetNillableOrganizationID(u *uint64) *CreditUsageUpdateOne {
  626. if u != nil {
  627. cuuo.SetOrganizationID(*u)
  628. }
  629. return cuuo
  630. }
  631. // AddOrganizationID adds u to the "organization_id" field.
  632. func (cuuo *CreditUsageUpdateOne) AddOrganizationID(u int64) *CreditUsageUpdateOne {
  633. cuuo.mutation.AddOrganizationID(u)
  634. return cuuo
  635. }
  636. // ClearOrganizationID clears the value of the "organization_id" field.
  637. func (cuuo *CreditUsageUpdateOne) ClearOrganizationID() *CreditUsageUpdateOne {
  638. cuuo.mutation.ClearOrganizationID()
  639. return cuuo
  640. }
  641. // SetNid sets the "nid" field.
  642. func (cuuo *CreditUsageUpdateOne) SetNid(u uint64) *CreditUsageUpdateOne {
  643. cuuo.mutation.ResetNid()
  644. cuuo.mutation.SetNid(u)
  645. return cuuo
  646. }
  647. // SetNillableNid sets the "nid" field if the given value is not nil.
  648. func (cuuo *CreditUsageUpdateOne) SetNillableNid(u *uint64) *CreditUsageUpdateOne {
  649. if u != nil {
  650. cuuo.SetNid(*u)
  651. }
  652. return cuuo
  653. }
  654. // AddNid adds u to the "nid" field.
  655. func (cuuo *CreditUsageUpdateOne) AddNid(u int64) *CreditUsageUpdateOne {
  656. cuuo.mutation.AddNid(u)
  657. return cuuo
  658. }
  659. // SetReason sets the "reason" field.
  660. func (cuuo *CreditUsageUpdateOne) SetReason(s string) *CreditUsageUpdateOne {
  661. cuuo.mutation.SetReason(s)
  662. return cuuo
  663. }
  664. // SetNillableReason sets the "reason" field if the given value is not nil.
  665. func (cuuo *CreditUsageUpdateOne) SetNillableReason(s *string) *CreditUsageUpdateOne {
  666. if s != nil {
  667. cuuo.SetReason(*s)
  668. }
  669. return cuuo
  670. }
  671. // SetOperator sets the "operator" field.
  672. func (cuuo *CreditUsageUpdateOne) SetOperator(s string) *CreditUsageUpdateOne {
  673. cuuo.mutation.SetOperator(s)
  674. return cuuo
  675. }
  676. // SetNillableOperator sets the "operator" field if the given value is not nil.
  677. func (cuuo *CreditUsageUpdateOne) SetNillableOperator(s *string) *CreditUsageUpdateOne {
  678. if s != nil {
  679. cuuo.SetOperator(*s)
  680. }
  681. return cuuo
  682. }
  683. // Mutation returns the CreditUsageMutation object of the builder.
  684. func (cuuo *CreditUsageUpdateOne) Mutation() *CreditUsageMutation {
  685. return cuuo.mutation
  686. }
  687. // Where appends a list predicates to the CreditUsageUpdate builder.
  688. func (cuuo *CreditUsageUpdateOne) Where(ps ...predicate.CreditUsage) *CreditUsageUpdateOne {
  689. cuuo.mutation.Where(ps...)
  690. return cuuo
  691. }
  692. // Select allows selecting one or more fields (columns) of the returned entity.
  693. // The default is selecting all fields defined in the entity schema.
  694. func (cuuo *CreditUsageUpdateOne) Select(field string, fields ...string) *CreditUsageUpdateOne {
  695. cuuo.fields = append([]string{field}, fields...)
  696. return cuuo
  697. }
  698. // Save executes the query and returns the updated CreditUsage entity.
  699. func (cuuo *CreditUsageUpdateOne) Save(ctx context.Context) (*CreditUsage, error) {
  700. if err := cuuo.defaults(); err != nil {
  701. return nil, err
  702. }
  703. return withHooks(ctx, cuuo.sqlSave, cuuo.mutation, cuuo.hooks)
  704. }
  705. // SaveX is like Save, but panics if an error occurs.
  706. func (cuuo *CreditUsageUpdateOne) SaveX(ctx context.Context) *CreditUsage {
  707. node, err := cuuo.Save(ctx)
  708. if err != nil {
  709. panic(err)
  710. }
  711. return node
  712. }
  713. // Exec executes the query on the entity.
  714. func (cuuo *CreditUsageUpdateOne) Exec(ctx context.Context) error {
  715. _, err := cuuo.Save(ctx)
  716. return err
  717. }
  718. // ExecX is like Exec, but panics if an error occurs.
  719. func (cuuo *CreditUsageUpdateOne) ExecX(ctx context.Context) {
  720. if err := cuuo.Exec(ctx); err != nil {
  721. panic(err)
  722. }
  723. }
  724. // defaults sets the default values of the builder before save.
  725. func (cuuo *CreditUsageUpdateOne) defaults() error {
  726. if _, ok := cuuo.mutation.UpdatedAt(); !ok {
  727. if creditusage.UpdateDefaultUpdatedAt == nil {
  728. return fmt.Errorf("ent: uninitialized creditusage.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  729. }
  730. v := creditusage.UpdateDefaultUpdatedAt()
  731. cuuo.mutation.SetUpdatedAt(v)
  732. }
  733. return nil
  734. }
  735. // check runs all checks and user-defined validators on the builder.
  736. func (cuuo *CreditUsageUpdateOne) check() error {
  737. if v, ok := cuuo.mutation.UserID(); ok {
  738. if err := creditusage.UserIDValidator(v); err != nil {
  739. return &ValidationError{Name: "user_id", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.user_id": %w`, err)}
  740. }
  741. }
  742. if v, ok := cuuo.mutation.Status(); ok {
  743. if err := creditusage.StatusValidator(v); err != nil {
  744. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.status": %w`, err)}
  745. }
  746. }
  747. if v, ok := cuuo.mutation.Reason(); ok {
  748. if err := creditusage.ReasonValidator(v); err != nil {
  749. return &ValidationError{Name: "reason", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.reason": %w`, err)}
  750. }
  751. }
  752. if v, ok := cuuo.mutation.Operator(); ok {
  753. if err := creditusage.OperatorValidator(v); err != nil {
  754. return &ValidationError{Name: "operator", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.operator": %w`, err)}
  755. }
  756. }
  757. return nil
  758. }
  759. func (cuuo *CreditUsageUpdateOne) sqlSave(ctx context.Context) (_node *CreditUsage, err error) {
  760. if err := cuuo.check(); err != nil {
  761. return _node, err
  762. }
  763. _spec := sqlgraph.NewUpdateSpec(creditusage.Table, creditusage.Columns, sqlgraph.NewFieldSpec(creditusage.FieldID, field.TypeUint64))
  764. id, ok := cuuo.mutation.ID()
  765. if !ok {
  766. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "CreditUsage.id" for update`)}
  767. }
  768. _spec.Node.ID.Value = id
  769. if fields := cuuo.fields; len(fields) > 0 {
  770. _spec.Node.Columns = make([]string, 0, len(fields))
  771. _spec.Node.Columns = append(_spec.Node.Columns, creditusage.FieldID)
  772. for _, f := range fields {
  773. if !creditusage.ValidColumn(f) {
  774. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  775. }
  776. if f != creditusage.FieldID {
  777. _spec.Node.Columns = append(_spec.Node.Columns, f)
  778. }
  779. }
  780. }
  781. if ps := cuuo.mutation.predicates; len(ps) > 0 {
  782. _spec.Predicate = func(selector *sql.Selector) {
  783. for i := range ps {
  784. ps[i](selector)
  785. }
  786. }
  787. }
  788. if value, ok := cuuo.mutation.UpdatedAt(); ok {
  789. _spec.SetField(creditusage.FieldUpdatedAt, field.TypeTime, value)
  790. }
  791. if value, ok := cuuo.mutation.DeletedAt(); ok {
  792. _spec.SetField(creditusage.FieldDeletedAt, field.TypeTime, value)
  793. }
  794. if cuuo.mutation.DeletedAtCleared() {
  795. _spec.ClearField(creditusage.FieldDeletedAt, field.TypeTime)
  796. }
  797. if value, ok := cuuo.mutation.UserID(); ok {
  798. _spec.SetField(creditusage.FieldUserID, field.TypeString, value)
  799. }
  800. if cuuo.mutation.UserIDCleared() {
  801. _spec.ClearField(creditusage.FieldUserID, field.TypeString)
  802. }
  803. if value, ok := cuuo.mutation.Number(); ok {
  804. _spec.SetField(creditusage.FieldNumber, field.TypeFloat32, value)
  805. }
  806. if value, ok := cuuo.mutation.AddedNumber(); ok {
  807. _spec.AddField(creditusage.FieldNumber, field.TypeFloat32, value)
  808. }
  809. if value, ok := cuuo.mutation.PayNumber(); ok {
  810. _spec.SetField(creditusage.FieldPayNumber, field.TypeFloat32, value)
  811. }
  812. if value, ok := cuuo.mutation.AddedPayNumber(); ok {
  813. _spec.AddField(creditusage.FieldPayNumber, field.TypeFloat32, value)
  814. }
  815. if value, ok := cuuo.mutation.BeforeNumber(); ok {
  816. _spec.SetField(creditusage.FieldBeforeNumber, field.TypeFloat32, value)
  817. }
  818. if value, ok := cuuo.mutation.AddedBeforeNumber(); ok {
  819. _spec.AddField(creditusage.FieldBeforeNumber, field.TypeFloat32, value)
  820. }
  821. if value, ok := cuuo.mutation.AfterNumber(); ok {
  822. _spec.SetField(creditusage.FieldAfterNumber, field.TypeFloat32, value)
  823. }
  824. if value, ok := cuuo.mutation.AddedAfterNumber(); ok {
  825. _spec.AddField(creditusage.FieldAfterNumber, field.TypeFloat32, value)
  826. }
  827. if value, ok := cuuo.mutation.PayMethod(); ok {
  828. _spec.SetField(creditusage.FieldPayMethod, field.TypeString, value)
  829. }
  830. if value, ok := cuuo.mutation.Status(); ok {
  831. _spec.SetField(creditusage.FieldStatus, field.TypeInt, value)
  832. }
  833. if value, ok := cuuo.mutation.AddedStatus(); ok {
  834. _spec.AddField(creditusage.FieldStatus, field.TypeInt, value)
  835. }
  836. if cuuo.mutation.StatusCleared() {
  837. _spec.ClearField(creditusage.FieldStatus, field.TypeInt)
  838. }
  839. if value, ok := cuuo.mutation.Ntype(); ok {
  840. _spec.SetField(creditusage.FieldNtype, field.TypeInt, value)
  841. }
  842. if value, ok := cuuo.mutation.AddedNtype(); ok {
  843. _spec.AddField(creditusage.FieldNtype, field.TypeInt, value)
  844. }
  845. if value, ok := cuuo.mutation.Table(); ok {
  846. _spec.SetField(creditusage.FieldTable, field.TypeString, value)
  847. }
  848. if value, ok := cuuo.mutation.OrganizationID(); ok {
  849. _spec.SetField(creditusage.FieldOrganizationID, field.TypeUint64, value)
  850. }
  851. if value, ok := cuuo.mutation.AddedOrganizationID(); ok {
  852. _spec.AddField(creditusage.FieldOrganizationID, field.TypeUint64, value)
  853. }
  854. if cuuo.mutation.OrganizationIDCleared() {
  855. _spec.ClearField(creditusage.FieldOrganizationID, field.TypeUint64)
  856. }
  857. if value, ok := cuuo.mutation.Nid(); ok {
  858. _spec.SetField(creditusage.FieldNid, field.TypeUint64, value)
  859. }
  860. if value, ok := cuuo.mutation.AddedNid(); ok {
  861. _spec.AddField(creditusage.FieldNid, field.TypeUint64, value)
  862. }
  863. if value, ok := cuuo.mutation.Reason(); ok {
  864. _spec.SetField(creditusage.FieldReason, field.TypeString, value)
  865. }
  866. if value, ok := cuuo.mutation.Operator(); ok {
  867. _spec.SetField(creditusage.FieldOperator, field.TypeString, value)
  868. }
  869. _node = &CreditUsage{config: cuuo.config}
  870. _spec.Assign = _node.assignValues
  871. _spec.ScanValues = _node.scanValues
  872. if err = sqlgraph.UpdateNode(ctx, cuuo.driver, _spec); err != nil {
  873. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  874. err = &NotFoundError{creditusage.Label}
  875. } else if sqlgraph.IsConstraintError(err) {
  876. err = &ConstraintError{msg: err.Error(), wrap: err}
  877. }
  878. return nil, err
  879. }
  880. cuuo.mutation.done = true
  881. return _node, nil
  882. }