messagerecords_create.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "entgo.io/ent/dialect/sql/sqlgraph"
  9. "entgo.io/ent/schema/field"
  10. "github.com/suyuan32/simple-admin-job/ent/messagerecords"
  11. )
  12. // MessageRecordsCreate is the builder for creating a MessageRecords entity.
  13. type MessageRecordsCreate struct {
  14. config
  15. mutation *MessageRecordsMutation
  16. hooks []Hook
  17. }
  18. // SetCreatedAt sets the "created_at" field.
  19. func (mrc *MessageRecordsCreate) SetCreatedAt(t time.Time) *MessageRecordsCreate {
  20. mrc.mutation.SetCreatedAt(t)
  21. return mrc
  22. }
  23. // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
  24. func (mrc *MessageRecordsCreate) SetNillableCreatedAt(t *time.Time) *MessageRecordsCreate {
  25. if t != nil {
  26. mrc.SetCreatedAt(*t)
  27. }
  28. return mrc
  29. }
  30. // SetUpdatedAt sets the "updated_at" field.
  31. func (mrc *MessageRecordsCreate) SetUpdatedAt(t time.Time) *MessageRecordsCreate {
  32. mrc.mutation.SetUpdatedAt(t)
  33. return mrc
  34. }
  35. // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
  36. func (mrc *MessageRecordsCreate) SetNillableUpdatedAt(t *time.Time) *MessageRecordsCreate {
  37. if t != nil {
  38. mrc.SetUpdatedAt(*t)
  39. }
  40. return mrc
  41. }
  42. // SetStatus sets the "status" field.
  43. func (mrc *MessageRecordsCreate) SetStatus(u uint8) *MessageRecordsCreate {
  44. mrc.mutation.SetStatus(u)
  45. return mrc
  46. }
  47. // SetNillableStatus sets the "status" field if the given value is not nil.
  48. func (mrc *MessageRecordsCreate) SetNillableStatus(u *uint8) *MessageRecordsCreate {
  49. if u != nil {
  50. mrc.SetStatus(*u)
  51. }
  52. return mrc
  53. }
  54. // SetBotWxid sets the "bot_wxid" field.
  55. func (mrc *MessageRecordsCreate) SetBotWxid(s string) *MessageRecordsCreate {
  56. mrc.mutation.SetBotWxid(s)
  57. return mrc
  58. }
  59. // SetContactID sets the "contact_id" field.
  60. func (mrc *MessageRecordsCreate) SetContactID(u uint64) *MessageRecordsCreate {
  61. mrc.mutation.SetContactID(u)
  62. return mrc
  63. }
  64. // SetNillableContactID sets the "contact_id" field if the given value is not nil.
  65. func (mrc *MessageRecordsCreate) SetNillableContactID(u *uint64) *MessageRecordsCreate {
  66. if u != nil {
  67. mrc.SetContactID(*u)
  68. }
  69. return mrc
  70. }
  71. // SetContactType sets the "contact_type" field.
  72. func (mrc *MessageRecordsCreate) SetContactType(i int) *MessageRecordsCreate {
  73. mrc.mutation.SetContactType(i)
  74. return mrc
  75. }
  76. // SetNillableContactType sets the "contact_type" field if the given value is not nil.
  77. func (mrc *MessageRecordsCreate) SetNillableContactType(i *int) *MessageRecordsCreate {
  78. if i != nil {
  79. mrc.SetContactType(*i)
  80. }
  81. return mrc
  82. }
  83. // SetContactWxid sets the "contact_wxid" field.
  84. func (mrc *MessageRecordsCreate) SetContactWxid(s string) *MessageRecordsCreate {
  85. mrc.mutation.SetContactWxid(s)
  86. return mrc
  87. }
  88. // SetNillableContactWxid sets the "contact_wxid" field if the given value is not nil.
  89. func (mrc *MessageRecordsCreate) SetNillableContactWxid(s *string) *MessageRecordsCreate {
  90. if s != nil {
  91. mrc.SetContactWxid(*s)
  92. }
  93. return mrc
  94. }
  95. // SetContentType sets the "content_type" field.
  96. func (mrc *MessageRecordsCreate) SetContentType(i int) *MessageRecordsCreate {
  97. mrc.mutation.SetContentType(i)
  98. return mrc
  99. }
  100. // SetNillableContentType sets the "content_type" field if the given value is not nil.
  101. func (mrc *MessageRecordsCreate) SetNillableContentType(i *int) *MessageRecordsCreate {
  102. if i != nil {
  103. mrc.SetContentType(*i)
  104. }
  105. return mrc
  106. }
  107. // SetContent sets the "content" field.
  108. func (mrc *MessageRecordsCreate) SetContent(s string) *MessageRecordsCreate {
  109. mrc.mutation.SetContent(s)
  110. return mrc
  111. }
  112. // SetNillableContent sets the "content" field if the given value is not nil.
  113. func (mrc *MessageRecordsCreate) SetNillableContent(s *string) *MessageRecordsCreate {
  114. if s != nil {
  115. mrc.SetContent(*s)
  116. }
  117. return mrc
  118. }
  119. // SetErrorDetail sets the "error_detail" field.
  120. func (mrc *MessageRecordsCreate) SetErrorDetail(s string) *MessageRecordsCreate {
  121. mrc.mutation.SetErrorDetail(s)
  122. return mrc
  123. }
  124. // SetNillableErrorDetail sets the "error_detail" field if the given value is not nil.
  125. func (mrc *MessageRecordsCreate) SetNillableErrorDetail(s *string) *MessageRecordsCreate {
  126. if s != nil {
  127. mrc.SetErrorDetail(*s)
  128. }
  129. return mrc
  130. }
  131. // SetSendTime sets the "send_time" field.
  132. func (mrc *MessageRecordsCreate) SetSendTime(t time.Time) *MessageRecordsCreate {
  133. mrc.mutation.SetSendTime(t)
  134. return mrc
  135. }
  136. // SetNillableSendTime sets the "send_time" field if the given value is not nil.
  137. func (mrc *MessageRecordsCreate) SetNillableSendTime(t *time.Time) *MessageRecordsCreate {
  138. if t != nil {
  139. mrc.SetSendTime(*t)
  140. }
  141. return mrc
  142. }
  143. // SetSourceType sets the "source_type" field.
  144. func (mrc *MessageRecordsCreate) SetSourceType(i int) *MessageRecordsCreate {
  145. mrc.mutation.SetSourceType(i)
  146. return mrc
  147. }
  148. // SetNillableSourceType sets the "source_type" field if the given value is not nil.
  149. func (mrc *MessageRecordsCreate) SetNillableSourceType(i *int) *MessageRecordsCreate {
  150. if i != nil {
  151. mrc.SetSourceType(*i)
  152. }
  153. return mrc
  154. }
  155. // SetSourceID sets the "source_id" field.
  156. func (mrc *MessageRecordsCreate) SetSourceID(u uint64) *MessageRecordsCreate {
  157. mrc.mutation.SetSourceID(u)
  158. return mrc
  159. }
  160. // SetNillableSourceID sets the "source_id" field if the given value is not nil.
  161. func (mrc *MessageRecordsCreate) SetNillableSourceID(u *uint64) *MessageRecordsCreate {
  162. if u != nil {
  163. mrc.SetSourceID(*u)
  164. }
  165. return mrc
  166. }
  167. // SetSubSourceID sets the "sub_source_id" field.
  168. func (mrc *MessageRecordsCreate) SetSubSourceID(u uint64) *MessageRecordsCreate {
  169. mrc.mutation.SetSubSourceID(u)
  170. return mrc
  171. }
  172. // SetNillableSubSourceID sets the "sub_source_id" field if the given value is not nil.
  173. func (mrc *MessageRecordsCreate) SetNillableSubSourceID(u *uint64) *MessageRecordsCreate {
  174. if u != nil {
  175. mrc.SetSubSourceID(*u)
  176. }
  177. return mrc
  178. }
  179. // SetID sets the "id" field.
  180. func (mrc *MessageRecordsCreate) SetID(u uint64) *MessageRecordsCreate {
  181. mrc.mutation.SetID(u)
  182. return mrc
  183. }
  184. // Mutation returns the MessageRecordsMutation object of the builder.
  185. func (mrc *MessageRecordsCreate) Mutation() *MessageRecordsMutation {
  186. return mrc.mutation
  187. }
  188. // Save creates the MessageRecords in the database.
  189. func (mrc *MessageRecordsCreate) Save(ctx context.Context) (*MessageRecords, error) {
  190. mrc.defaults()
  191. return withHooks(ctx, mrc.sqlSave, mrc.mutation, mrc.hooks)
  192. }
  193. // SaveX calls Save and panics if Save returns an error.
  194. func (mrc *MessageRecordsCreate) SaveX(ctx context.Context) *MessageRecords {
  195. v, err := mrc.Save(ctx)
  196. if err != nil {
  197. panic(err)
  198. }
  199. return v
  200. }
  201. // Exec executes the query.
  202. func (mrc *MessageRecordsCreate) Exec(ctx context.Context) error {
  203. _, err := mrc.Save(ctx)
  204. return err
  205. }
  206. // ExecX is like Exec, but panics if an error occurs.
  207. func (mrc *MessageRecordsCreate) ExecX(ctx context.Context) {
  208. if err := mrc.Exec(ctx); err != nil {
  209. panic(err)
  210. }
  211. }
  212. // defaults sets the default values of the builder before save.
  213. func (mrc *MessageRecordsCreate) defaults() {
  214. if _, ok := mrc.mutation.CreatedAt(); !ok {
  215. v := messagerecords.DefaultCreatedAt()
  216. mrc.mutation.SetCreatedAt(v)
  217. }
  218. if _, ok := mrc.mutation.UpdatedAt(); !ok {
  219. v := messagerecords.DefaultUpdatedAt()
  220. mrc.mutation.SetUpdatedAt(v)
  221. }
  222. if _, ok := mrc.mutation.Status(); !ok {
  223. v := messagerecords.DefaultStatus
  224. mrc.mutation.SetStatus(v)
  225. }
  226. if _, ok := mrc.mutation.ContactType(); !ok {
  227. v := messagerecords.DefaultContactType
  228. mrc.mutation.SetContactType(v)
  229. }
  230. if _, ok := mrc.mutation.ContactWxid(); !ok {
  231. v := messagerecords.DefaultContactWxid
  232. mrc.mutation.SetContactWxid(v)
  233. }
  234. if _, ok := mrc.mutation.ContentType(); !ok {
  235. v := messagerecords.DefaultContentType
  236. mrc.mutation.SetContentType(v)
  237. }
  238. if _, ok := mrc.mutation.Content(); !ok {
  239. v := messagerecords.DefaultContent
  240. mrc.mutation.SetContent(v)
  241. }
  242. if _, ok := mrc.mutation.ErrorDetail(); !ok {
  243. v := messagerecords.DefaultErrorDetail
  244. mrc.mutation.SetErrorDetail(v)
  245. }
  246. if _, ok := mrc.mutation.SourceType(); !ok {
  247. v := messagerecords.DefaultSourceType
  248. mrc.mutation.SetSourceType(v)
  249. }
  250. if _, ok := mrc.mutation.SourceID(); !ok {
  251. v := messagerecords.DefaultSourceID
  252. mrc.mutation.SetSourceID(v)
  253. }
  254. if _, ok := mrc.mutation.SubSourceID(); !ok {
  255. v := messagerecords.DefaultSubSourceID
  256. mrc.mutation.SetSubSourceID(v)
  257. }
  258. }
  259. // check runs all checks and user-defined validators on the builder.
  260. func (mrc *MessageRecordsCreate) check() error {
  261. if _, ok := mrc.mutation.CreatedAt(); !ok {
  262. return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "MessageRecords.created_at"`)}
  263. }
  264. if _, ok := mrc.mutation.UpdatedAt(); !ok {
  265. return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "MessageRecords.updated_at"`)}
  266. }
  267. if _, ok := mrc.mutation.BotWxid(); !ok {
  268. return &ValidationError{Name: "bot_wxid", err: errors.New(`ent: missing required field "MessageRecords.bot_wxid"`)}
  269. }
  270. if _, ok := mrc.mutation.ContactType(); !ok {
  271. return &ValidationError{Name: "contact_type", err: errors.New(`ent: missing required field "MessageRecords.contact_type"`)}
  272. }
  273. if _, ok := mrc.mutation.ContactWxid(); !ok {
  274. return &ValidationError{Name: "contact_wxid", err: errors.New(`ent: missing required field "MessageRecords.contact_wxid"`)}
  275. }
  276. if _, ok := mrc.mutation.ContentType(); !ok {
  277. return &ValidationError{Name: "content_type", err: errors.New(`ent: missing required field "MessageRecords.content_type"`)}
  278. }
  279. if _, ok := mrc.mutation.Content(); !ok {
  280. return &ValidationError{Name: "content", err: errors.New(`ent: missing required field "MessageRecords.content"`)}
  281. }
  282. if _, ok := mrc.mutation.ErrorDetail(); !ok {
  283. return &ValidationError{Name: "error_detail", err: errors.New(`ent: missing required field "MessageRecords.error_detail"`)}
  284. }
  285. if _, ok := mrc.mutation.SourceType(); !ok {
  286. return &ValidationError{Name: "source_type", err: errors.New(`ent: missing required field "MessageRecords.source_type"`)}
  287. }
  288. return nil
  289. }
  290. func (mrc *MessageRecordsCreate) sqlSave(ctx context.Context) (*MessageRecords, error) {
  291. if err := mrc.check(); err != nil {
  292. return nil, err
  293. }
  294. _node, _spec := mrc.createSpec()
  295. if err := sqlgraph.CreateNode(ctx, mrc.driver, _spec); err != nil {
  296. if sqlgraph.IsConstraintError(err) {
  297. err = &ConstraintError{msg: err.Error(), wrap: err}
  298. }
  299. return nil, err
  300. }
  301. if _spec.ID.Value != _node.ID {
  302. id := _spec.ID.Value.(int64)
  303. _node.ID = uint64(id)
  304. }
  305. mrc.mutation.id = &_node.ID
  306. mrc.mutation.done = true
  307. return _node, nil
  308. }
  309. func (mrc *MessageRecordsCreate) createSpec() (*MessageRecords, *sqlgraph.CreateSpec) {
  310. var (
  311. _node = &MessageRecords{config: mrc.config}
  312. _spec = sqlgraph.NewCreateSpec(messagerecords.Table, sqlgraph.NewFieldSpec(messagerecords.FieldID, field.TypeUint64))
  313. )
  314. if id, ok := mrc.mutation.ID(); ok {
  315. _node.ID = id
  316. _spec.ID.Value = id
  317. }
  318. if value, ok := mrc.mutation.CreatedAt(); ok {
  319. _spec.SetField(messagerecords.FieldCreatedAt, field.TypeTime, value)
  320. _node.CreatedAt = value
  321. }
  322. if value, ok := mrc.mutation.UpdatedAt(); ok {
  323. _spec.SetField(messagerecords.FieldUpdatedAt, field.TypeTime, value)
  324. _node.UpdatedAt = value
  325. }
  326. if value, ok := mrc.mutation.Status(); ok {
  327. _spec.SetField(messagerecords.FieldStatus, field.TypeUint8, value)
  328. _node.Status = value
  329. }
  330. if value, ok := mrc.mutation.BotWxid(); ok {
  331. _spec.SetField(messagerecords.FieldBotWxid, field.TypeString, value)
  332. _node.BotWxid = value
  333. }
  334. if value, ok := mrc.mutation.ContactID(); ok {
  335. _spec.SetField(messagerecords.FieldContactID, field.TypeUint64, value)
  336. _node.ContactID = value
  337. }
  338. if value, ok := mrc.mutation.ContactType(); ok {
  339. _spec.SetField(messagerecords.FieldContactType, field.TypeInt, value)
  340. _node.ContactType = value
  341. }
  342. if value, ok := mrc.mutation.ContactWxid(); ok {
  343. _spec.SetField(messagerecords.FieldContactWxid, field.TypeString, value)
  344. _node.ContactWxid = value
  345. }
  346. if value, ok := mrc.mutation.ContentType(); ok {
  347. _spec.SetField(messagerecords.FieldContentType, field.TypeInt, value)
  348. _node.ContentType = value
  349. }
  350. if value, ok := mrc.mutation.Content(); ok {
  351. _spec.SetField(messagerecords.FieldContent, field.TypeString, value)
  352. _node.Content = value
  353. }
  354. if value, ok := mrc.mutation.ErrorDetail(); ok {
  355. _spec.SetField(messagerecords.FieldErrorDetail, field.TypeString, value)
  356. _node.ErrorDetail = value
  357. }
  358. if value, ok := mrc.mutation.SendTime(); ok {
  359. _spec.SetField(messagerecords.FieldSendTime, field.TypeTime, value)
  360. _node.SendTime = value
  361. }
  362. if value, ok := mrc.mutation.SourceType(); ok {
  363. _spec.SetField(messagerecords.FieldSourceType, field.TypeInt, value)
  364. _node.SourceType = value
  365. }
  366. if value, ok := mrc.mutation.SourceID(); ok {
  367. _spec.SetField(messagerecords.FieldSourceID, field.TypeUint64, value)
  368. _node.SourceID = value
  369. }
  370. if value, ok := mrc.mutation.SubSourceID(); ok {
  371. _spec.SetField(messagerecords.FieldSubSourceID, field.TypeUint64, value)
  372. _node.SubSourceID = value
  373. }
  374. return _node, _spec
  375. }
  376. // MessageRecordsCreateBulk is the builder for creating many MessageRecords entities in bulk.
  377. type MessageRecordsCreateBulk struct {
  378. config
  379. err error
  380. builders []*MessageRecordsCreate
  381. }
  382. // Save creates the MessageRecords entities in the database.
  383. func (mrcb *MessageRecordsCreateBulk) Save(ctx context.Context) ([]*MessageRecords, error) {
  384. if mrcb.err != nil {
  385. return nil, mrcb.err
  386. }
  387. specs := make([]*sqlgraph.CreateSpec, len(mrcb.builders))
  388. nodes := make([]*MessageRecords, len(mrcb.builders))
  389. mutators := make([]Mutator, len(mrcb.builders))
  390. for i := range mrcb.builders {
  391. func(i int, root context.Context) {
  392. builder := mrcb.builders[i]
  393. builder.defaults()
  394. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  395. mutation, ok := m.(*MessageRecordsMutation)
  396. if !ok {
  397. return nil, fmt.Errorf("unexpected mutation type %T", m)
  398. }
  399. if err := builder.check(); err != nil {
  400. return nil, err
  401. }
  402. builder.mutation = mutation
  403. var err error
  404. nodes[i], specs[i] = builder.createSpec()
  405. if i < len(mutators)-1 {
  406. _, err = mutators[i+1].Mutate(root, mrcb.builders[i+1].mutation)
  407. } else {
  408. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  409. // Invoke the actual operation on the latest mutation in the chain.
  410. if err = sqlgraph.BatchCreate(ctx, mrcb.driver, spec); err != nil {
  411. if sqlgraph.IsConstraintError(err) {
  412. err = &ConstraintError{msg: err.Error(), wrap: err}
  413. }
  414. }
  415. }
  416. if err != nil {
  417. return nil, err
  418. }
  419. mutation.id = &nodes[i].ID
  420. if specs[i].ID.Value != nil && nodes[i].ID == 0 {
  421. id := specs[i].ID.Value.(int64)
  422. nodes[i].ID = uint64(id)
  423. }
  424. mutation.done = true
  425. return nodes[i], nil
  426. })
  427. for i := len(builder.hooks) - 1; i >= 0; i-- {
  428. mut = builder.hooks[i](mut)
  429. }
  430. mutators[i] = mut
  431. }(i, ctx)
  432. }
  433. if len(mutators) > 0 {
  434. if _, err := mutators[0].Mutate(ctx, mrcb.builders[0].mutation); err != nil {
  435. return nil, err
  436. }
  437. }
  438. return nodes, nil
  439. }
  440. // SaveX is like Save, but panics if an error occurs.
  441. func (mrcb *MessageRecordsCreateBulk) SaveX(ctx context.Context) []*MessageRecords {
  442. v, err := mrcb.Save(ctx)
  443. if err != nil {
  444. panic(err)
  445. }
  446. return v
  447. }
  448. // Exec executes the query.
  449. func (mrcb *MessageRecordsCreateBulk) Exec(ctx context.Context) error {
  450. _, err := mrcb.Save(ctx)
  451. return err
  452. }
  453. // ExecX is like Exec, but panics if an error occurs.
  454. func (mrcb *MessageRecordsCreateBulk) ExecX(ctx context.Context) {
  455. if err := mrcb.Exec(ctx); err != nil {
  456. panic(err)
  457. }
  458. }