sopnode_create.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/custom_types"
  11. "github.com/suyuan32/simple-admin-job/ent/messagerecords"
  12. "github.com/suyuan32/simple-admin-job/ent/sopnode"
  13. "github.com/suyuan32/simple-admin-job/ent/sopstage"
  14. )
  15. // SopNodeCreate is the builder for creating a SopNode entity.
  16. type SopNodeCreate struct {
  17. config
  18. mutation *SopNodeMutation
  19. hooks []Hook
  20. }
  21. // SetCreatedAt sets the "created_at" field.
  22. func (snc *SopNodeCreate) SetCreatedAt(t time.Time) *SopNodeCreate {
  23. snc.mutation.SetCreatedAt(t)
  24. return snc
  25. }
  26. // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
  27. func (snc *SopNodeCreate) SetNillableCreatedAt(t *time.Time) *SopNodeCreate {
  28. if t != nil {
  29. snc.SetCreatedAt(*t)
  30. }
  31. return snc
  32. }
  33. // SetUpdatedAt sets the "updated_at" field.
  34. func (snc *SopNodeCreate) SetUpdatedAt(t time.Time) *SopNodeCreate {
  35. snc.mutation.SetUpdatedAt(t)
  36. return snc
  37. }
  38. // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
  39. func (snc *SopNodeCreate) SetNillableUpdatedAt(t *time.Time) *SopNodeCreate {
  40. if t != nil {
  41. snc.SetUpdatedAt(*t)
  42. }
  43. return snc
  44. }
  45. // SetStatus sets the "status" field.
  46. func (snc *SopNodeCreate) SetStatus(u uint8) *SopNodeCreate {
  47. snc.mutation.SetStatus(u)
  48. return snc
  49. }
  50. // SetNillableStatus sets the "status" field if the given value is not nil.
  51. func (snc *SopNodeCreate) SetNillableStatus(u *uint8) *SopNodeCreate {
  52. if u != nil {
  53. snc.SetStatus(*u)
  54. }
  55. return snc
  56. }
  57. // SetStageID sets the "stage_id" field.
  58. func (snc *SopNodeCreate) SetStageID(u uint64) *SopNodeCreate {
  59. snc.mutation.SetStageID(u)
  60. return snc
  61. }
  62. // SetParentID sets the "parent_id" field.
  63. func (snc *SopNodeCreate) SetParentID(u uint64) *SopNodeCreate {
  64. snc.mutation.SetParentID(u)
  65. return snc
  66. }
  67. // SetName sets the "name" field.
  68. func (snc *SopNodeCreate) SetName(s string) *SopNodeCreate {
  69. snc.mutation.SetName(s)
  70. return snc
  71. }
  72. // SetNillableName sets the "name" field if the given value is not nil.
  73. func (snc *SopNodeCreate) SetNillableName(s *string) *SopNodeCreate {
  74. if s != nil {
  75. snc.SetName(*s)
  76. }
  77. return snc
  78. }
  79. // SetConditionType sets the "condition_type" field.
  80. func (snc *SopNodeCreate) SetConditionType(i int) *SopNodeCreate {
  81. snc.mutation.SetConditionType(i)
  82. return snc
  83. }
  84. // SetNillableConditionType sets the "condition_type" field if the given value is not nil.
  85. func (snc *SopNodeCreate) SetNillableConditionType(i *int) *SopNodeCreate {
  86. if i != nil {
  87. snc.SetConditionType(*i)
  88. }
  89. return snc
  90. }
  91. // SetConditionList sets the "condition_list" field.
  92. func (snc *SopNodeCreate) SetConditionList(s []string) *SopNodeCreate {
  93. snc.mutation.SetConditionList(s)
  94. return snc
  95. }
  96. // SetNoReplyCondition sets the "no_reply_condition" field.
  97. func (snc *SopNodeCreate) SetNoReplyCondition(u uint64) *SopNodeCreate {
  98. snc.mutation.SetNoReplyCondition(u)
  99. return snc
  100. }
  101. // SetNillableNoReplyCondition sets the "no_reply_condition" field if the given value is not nil.
  102. func (snc *SopNodeCreate) SetNillableNoReplyCondition(u *uint64) *SopNodeCreate {
  103. if u != nil {
  104. snc.SetNoReplyCondition(*u)
  105. }
  106. return snc
  107. }
  108. // SetActionMessage sets the "action_message" field.
  109. func (snc *SopNodeCreate) SetActionMessage(ct []custom_types.Action) *SopNodeCreate {
  110. snc.mutation.SetActionMessage(ct)
  111. return snc
  112. }
  113. // SetActionLabel sets the "action_label" field.
  114. func (snc *SopNodeCreate) SetActionLabel(u []uint64) *SopNodeCreate {
  115. snc.mutation.SetActionLabel(u)
  116. return snc
  117. }
  118. // SetDeletedAt sets the "deleted_at" field.
  119. func (snc *SopNodeCreate) SetDeletedAt(t time.Time) *SopNodeCreate {
  120. snc.mutation.SetDeletedAt(t)
  121. return snc
  122. }
  123. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  124. func (snc *SopNodeCreate) SetNillableDeletedAt(t *time.Time) *SopNodeCreate {
  125. if t != nil {
  126. snc.SetDeletedAt(*t)
  127. }
  128. return snc
  129. }
  130. // SetID sets the "id" field.
  131. func (snc *SopNodeCreate) SetID(u uint64) *SopNodeCreate {
  132. snc.mutation.SetID(u)
  133. return snc
  134. }
  135. // SetSopStageID sets the "sop_stage" edge to the SopStage entity by ID.
  136. func (snc *SopNodeCreate) SetSopStageID(id uint64) *SopNodeCreate {
  137. snc.mutation.SetSopStageID(id)
  138. return snc
  139. }
  140. // SetSopStage sets the "sop_stage" edge to the SopStage entity.
  141. func (snc *SopNodeCreate) SetSopStage(s *SopStage) *SopNodeCreate {
  142. return snc.SetSopStageID(s.ID)
  143. }
  144. // AddNodeMessageIDs adds the "node_messages" edge to the MessageRecords entity by IDs.
  145. func (snc *SopNodeCreate) AddNodeMessageIDs(ids ...uint64) *SopNodeCreate {
  146. snc.mutation.AddNodeMessageIDs(ids...)
  147. return snc
  148. }
  149. // AddNodeMessages adds the "node_messages" edges to the MessageRecords entity.
  150. func (snc *SopNodeCreate) AddNodeMessages(m ...*MessageRecords) *SopNodeCreate {
  151. ids := make([]uint64, len(m))
  152. for i := range m {
  153. ids[i] = m[i].ID
  154. }
  155. return snc.AddNodeMessageIDs(ids...)
  156. }
  157. // Mutation returns the SopNodeMutation object of the builder.
  158. func (snc *SopNodeCreate) Mutation() *SopNodeMutation {
  159. return snc.mutation
  160. }
  161. // Save creates the SopNode in the database.
  162. func (snc *SopNodeCreate) Save(ctx context.Context) (*SopNode, error) {
  163. snc.defaults()
  164. return withHooks(ctx, snc.sqlSave, snc.mutation, snc.hooks)
  165. }
  166. // SaveX calls Save and panics if Save returns an error.
  167. func (snc *SopNodeCreate) SaveX(ctx context.Context) *SopNode {
  168. v, err := snc.Save(ctx)
  169. if err != nil {
  170. panic(err)
  171. }
  172. return v
  173. }
  174. // Exec executes the query.
  175. func (snc *SopNodeCreate) Exec(ctx context.Context) error {
  176. _, err := snc.Save(ctx)
  177. return err
  178. }
  179. // ExecX is like Exec, but panics if an error occurs.
  180. func (snc *SopNodeCreate) ExecX(ctx context.Context) {
  181. if err := snc.Exec(ctx); err != nil {
  182. panic(err)
  183. }
  184. }
  185. // defaults sets the default values of the builder before save.
  186. func (snc *SopNodeCreate) defaults() {
  187. if _, ok := snc.mutation.CreatedAt(); !ok {
  188. v := sopnode.DefaultCreatedAt()
  189. snc.mutation.SetCreatedAt(v)
  190. }
  191. if _, ok := snc.mutation.UpdatedAt(); !ok {
  192. v := sopnode.DefaultUpdatedAt()
  193. snc.mutation.SetUpdatedAt(v)
  194. }
  195. if _, ok := snc.mutation.Status(); !ok {
  196. v := sopnode.DefaultStatus
  197. snc.mutation.SetStatus(v)
  198. }
  199. if _, ok := snc.mutation.Name(); !ok {
  200. v := sopnode.DefaultName
  201. snc.mutation.SetName(v)
  202. }
  203. if _, ok := snc.mutation.ConditionType(); !ok {
  204. v := sopnode.DefaultConditionType
  205. snc.mutation.SetConditionType(v)
  206. }
  207. if _, ok := snc.mutation.NoReplyCondition(); !ok {
  208. v := sopnode.DefaultNoReplyCondition
  209. snc.mutation.SetNoReplyCondition(v)
  210. }
  211. }
  212. // check runs all checks and user-defined validators on the builder.
  213. func (snc *SopNodeCreate) check() error {
  214. if _, ok := snc.mutation.CreatedAt(); !ok {
  215. return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "SopNode.created_at"`)}
  216. }
  217. if _, ok := snc.mutation.UpdatedAt(); !ok {
  218. return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "SopNode.updated_at"`)}
  219. }
  220. if _, ok := snc.mutation.StageID(); !ok {
  221. return &ValidationError{Name: "stage_id", err: errors.New(`ent: missing required field "SopNode.stage_id"`)}
  222. }
  223. if _, ok := snc.mutation.ParentID(); !ok {
  224. return &ValidationError{Name: "parent_id", err: errors.New(`ent: missing required field "SopNode.parent_id"`)}
  225. }
  226. if _, ok := snc.mutation.Name(); !ok {
  227. return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "SopNode.name"`)}
  228. }
  229. if _, ok := snc.mutation.ConditionType(); !ok {
  230. return &ValidationError{Name: "condition_type", err: errors.New(`ent: missing required field "SopNode.condition_type"`)}
  231. }
  232. if _, ok := snc.mutation.NoReplyCondition(); !ok {
  233. return &ValidationError{Name: "no_reply_condition", err: errors.New(`ent: missing required field "SopNode.no_reply_condition"`)}
  234. }
  235. if _, ok := snc.mutation.SopStageID(); !ok {
  236. return &ValidationError{Name: "sop_stage", err: errors.New(`ent: missing required edge "SopNode.sop_stage"`)}
  237. }
  238. return nil
  239. }
  240. func (snc *SopNodeCreate) sqlSave(ctx context.Context) (*SopNode, error) {
  241. if err := snc.check(); err != nil {
  242. return nil, err
  243. }
  244. _node, _spec := snc.createSpec()
  245. if err := sqlgraph.CreateNode(ctx, snc.driver, _spec); err != nil {
  246. if sqlgraph.IsConstraintError(err) {
  247. err = &ConstraintError{msg: err.Error(), wrap: err}
  248. }
  249. return nil, err
  250. }
  251. if _spec.ID.Value != _node.ID {
  252. id := _spec.ID.Value.(int64)
  253. _node.ID = uint64(id)
  254. }
  255. snc.mutation.id = &_node.ID
  256. snc.mutation.done = true
  257. return _node, nil
  258. }
  259. func (snc *SopNodeCreate) createSpec() (*SopNode, *sqlgraph.CreateSpec) {
  260. var (
  261. _node = &SopNode{config: snc.config}
  262. _spec = sqlgraph.NewCreateSpec(sopnode.Table, sqlgraph.NewFieldSpec(sopnode.FieldID, field.TypeUint64))
  263. )
  264. if id, ok := snc.mutation.ID(); ok {
  265. _node.ID = id
  266. _spec.ID.Value = id
  267. }
  268. if value, ok := snc.mutation.CreatedAt(); ok {
  269. _spec.SetField(sopnode.FieldCreatedAt, field.TypeTime, value)
  270. _node.CreatedAt = value
  271. }
  272. if value, ok := snc.mutation.UpdatedAt(); ok {
  273. _spec.SetField(sopnode.FieldUpdatedAt, field.TypeTime, value)
  274. _node.UpdatedAt = value
  275. }
  276. if value, ok := snc.mutation.Status(); ok {
  277. _spec.SetField(sopnode.FieldStatus, field.TypeUint8, value)
  278. _node.Status = value
  279. }
  280. if value, ok := snc.mutation.ParentID(); ok {
  281. _spec.SetField(sopnode.FieldParentID, field.TypeUint64, value)
  282. _node.ParentID = value
  283. }
  284. if value, ok := snc.mutation.Name(); ok {
  285. _spec.SetField(sopnode.FieldName, field.TypeString, value)
  286. _node.Name = value
  287. }
  288. if value, ok := snc.mutation.ConditionType(); ok {
  289. _spec.SetField(sopnode.FieldConditionType, field.TypeInt, value)
  290. _node.ConditionType = value
  291. }
  292. if value, ok := snc.mutation.ConditionList(); ok {
  293. _spec.SetField(sopnode.FieldConditionList, field.TypeJSON, value)
  294. _node.ConditionList = value
  295. }
  296. if value, ok := snc.mutation.NoReplyCondition(); ok {
  297. _spec.SetField(sopnode.FieldNoReplyCondition, field.TypeUint64, value)
  298. _node.NoReplyCondition = value
  299. }
  300. if value, ok := snc.mutation.ActionMessage(); ok {
  301. _spec.SetField(sopnode.FieldActionMessage, field.TypeJSON, value)
  302. _node.ActionMessage = value
  303. }
  304. if value, ok := snc.mutation.ActionLabel(); ok {
  305. _spec.SetField(sopnode.FieldActionLabel, field.TypeJSON, value)
  306. _node.ActionLabel = value
  307. }
  308. if value, ok := snc.mutation.DeletedAt(); ok {
  309. _spec.SetField(sopnode.FieldDeletedAt, field.TypeTime, value)
  310. _node.DeletedAt = value
  311. }
  312. if nodes := snc.mutation.SopStageIDs(); len(nodes) > 0 {
  313. edge := &sqlgraph.EdgeSpec{
  314. Rel: sqlgraph.M2O,
  315. Inverse: true,
  316. Table: sopnode.SopStageTable,
  317. Columns: []string{sopnode.SopStageColumn},
  318. Bidi: false,
  319. Target: &sqlgraph.EdgeTarget{
  320. IDSpec: sqlgraph.NewFieldSpec(sopstage.FieldID, field.TypeUint64),
  321. },
  322. }
  323. for _, k := range nodes {
  324. edge.Target.Nodes = append(edge.Target.Nodes, k)
  325. }
  326. _node.StageID = nodes[0]
  327. _spec.Edges = append(_spec.Edges, edge)
  328. }
  329. if nodes := snc.mutation.NodeMessagesIDs(); len(nodes) > 0 {
  330. edge := &sqlgraph.EdgeSpec{
  331. Rel: sqlgraph.O2M,
  332. Inverse: false,
  333. Table: sopnode.NodeMessagesTable,
  334. Columns: []string{sopnode.NodeMessagesColumn},
  335. Bidi: false,
  336. Target: &sqlgraph.EdgeTarget{
  337. IDSpec: sqlgraph.NewFieldSpec(messagerecords.FieldID, field.TypeUint64),
  338. },
  339. }
  340. for _, k := range nodes {
  341. edge.Target.Nodes = append(edge.Target.Nodes, k)
  342. }
  343. _spec.Edges = append(_spec.Edges, edge)
  344. }
  345. return _node, _spec
  346. }
  347. // SopNodeCreateBulk is the builder for creating many SopNode entities in bulk.
  348. type SopNodeCreateBulk struct {
  349. config
  350. err error
  351. builders []*SopNodeCreate
  352. }
  353. // Save creates the SopNode entities in the database.
  354. func (sncb *SopNodeCreateBulk) Save(ctx context.Context) ([]*SopNode, error) {
  355. if sncb.err != nil {
  356. return nil, sncb.err
  357. }
  358. specs := make([]*sqlgraph.CreateSpec, len(sncb.builders))
  359. nodes := make([]*SopNode, len(sncb.builders))
  360. mutators := make([]Mutator, len(sncb.builders))
  361. for i := range sncb.builders {
  362. func(i int, root context.Context) {
  363. builder := sncb.builders[i]
  364. builder.defaults()
  365. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  366. mutation, ok := m.(*SopNodeMutation)
  367. if !ok {
  368. return nil, fmt.Errorf("unexpected mutation type %T", m)
  369. }
  370. if err := builder.check(); err != nil {
  371. return nil, err
  372. }
  373. builder.mutation = mutation
  374. var err error
  375. nodes[i], specs[i] = builder.createSpec()
  376. if i < len(mutators)-1 {
  377. _, err = mutators[i+1].Mutate(root, sncb.builders[i+1].mutation)
  378. } else {
  379. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  380. // Invoke the actual operation on the latest mutation in the chain.
  381. if err = sqlgraph.BatchCreate(ctx, sncb.driver, spec); err != nil {
  382. if sqlgraph.IsConstraintError(err) {
  383. err = &ConstraintError{msg: err.Error(), wrap: err}
  384. }
  385. }
  386. }
  387. if err != nil {
  388. return nil, err
  389. }
  390. mutation.id = &nodes[i].ID
  391. if specs[i].ID.Value != nil && nodes[i].ID == 0 {
  392. id := specs[i].ID.Value.(int64)
  393. nodes[i].ID = uint64(id)
  394. }
  395. mutation.done = true
  396. return nodes[i], nil
  397. })
  398. for i := len(builder.hooks) - 1; i >= 0; i-- {
  399. mut = builder.hooks[i](mut)
  400. }
  401. mutators[i] = mut
  402. }(i, ctx)
  403. }
  404. if len(mutators) > 0 {
  405. if _, err := mutators[0].Mutate(ctx, sncb.builders[0].mutation); err != nil {
  406. return nil, err
  407. }
  408. }
  409. return nodes, nil
  410. }
  411. // SaveX is like Save, but panics if an error occurs.
  412. func (sncb *SopNodeCreateBulk) SaveX(ctx context.Context) []*SopNode {
  413. v, err := sncb.Save(ctx)
  414. if err != nil {
  415. panic(err)
  416. }
  417. return v
  418. }
  419. // Exec executes the query.
  420. func (sncb *SopNodeCreateBulk) Exec(ctx context.Context) error {
  421. _, err := sncb.Save(ctx)
  422. return err
  423. }
  424. // ExecX is like Exec, but panics if an error occurs.
  425. func (sncb *SopNodeCreateBulk) ExecX(ctx context.Context) {
  426. if err := sncb.Exec(ctx); err != nil {
  427. panic(err)
  428. }
  429. }