soptask_create.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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/sopstage"
  11. "github.com/suyuan32/simple-admin-job/ent/soptask"
  12. )
  13. // SopTaskCreate is the builder for creating a SopTask entity.
  14. type SopTaskCreate struct {
  15. config
  16. mutation *SopTaskMutation
  17. hooks []Hook
  18. }
  19. // SetCreatedAt sets the "created_at" field.
  20. func (stc *SopTaskCreate) SetCreatedAt(t time.Time) *SopTaskCreate {
  21. stc.mutation.SetCreatedAt(t)
  22. return stc
  23. }
  24. // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
  25. func (stc *SopTaskCreate) SetNillableCreatedAt(t *time.Time) *SopTaskCreate {
  26. if t != nil {
  27. stc.SetCreatedAt(*t)
  28. }
  29. return stc
  30. }
  31. // SetUpdatedAt sets the "updated_at" field.
  32. func (stc *SopTaskCreate) SetUpdatedAt(t time.Time) *SopTaskCreate {
  33. stc.mutation.SetUpdatedAt(t)
  34. return stc
  35. }
  36. // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
  37. func (stc *SopTaskCreate) SetNillableUpdatedAt(t *time.Time) *SopTaskCreate {
  38. if t != nil {
  39. stc.SetUpdatedAt(*t)
  40. }
  41. return stc
  42. }
  43. // SetStatus sets the "status" field.
  44. func (stc *SopTaskCreate) SetStatus(u uint8) *SopTaskCreate {
  45. stc.mutation.SetStatus(u)
  46. return stc
  47. }
  48. // SetNillableStatus sets the "status" field if the given value is not nil.
  49. func (stc *SopTaskCreate) SetNillableStatus(u *uint8) *SopTaskCreate {
  50. if u != nil {
  51. stc.SetStatus(*u)
  52. }
  53. return stc
  54. }
  55. // SetName sets the "name" field.
  56. func (stc *SopTaskCreate) SetName(s string) *SopTaskCreate {
  57. stc.mutation.SetName(s)
  58. return stc
  59. }
  60. // SetBotWxidList sets the "bot_wxid_list" field.
  61. func (stc *SopTaskCreate) SetBotWxidList(s []string) *SopTaskCreate {
  62. stc.mutation.SetBotWxidList(s)
  63. return stc
  64. }
  65. // SetType sets the "type" field.
  66. func (stc *SopTaskCreate) SetType(i int) *SopTaskCreate {
  67. stc.mutation.SetType(i)
  68. return stc
  69. }
  70. // SetNillableType sets the "type" field if the given value is not nil.
  71. func (stc *SopTaskCreate) SetNillableType(i *int) *SopTaskCreate {
  72. if i != nil {
  73. stc.SetType(*i)
  74. }
  75. return stc
  76. }
  77. // SetPlanStartTime sets the "plan_start_time" field.
  78. func (stc *SopTaskCreate) SetPlanStartTime(t time.Time) *SopTaskCreate {
  79. stc.mutation.SetPlanStartTime(t)
  80. return stc
  81. }
  82. // SetNillablePlanStartTime sets the "plan_start_time" field if the given value is not nil.
  83. func (stc *SopTaskCreate) SetNillablePlanStartTime(t *time.Time) *SopTaskCreate {
  84. if t != nil {
  85. stc.SetPlanStartTime(*t)
  86. }
  87. return stc
  88. }
  89. // SetPlanEndTime sets the "plan_end_time" field.
  90. func (stc *SopTaskCreate) SetPlanEndTime(t time.Time) *SopTaskCreate {
  91. stc.mutation.SetPlanEndTime(t)
  92. return stc
  93. }
  94. // SetNillablePlanEndTime sets the "plan_end_time" field if the given value is not nil.
  95. func (stc *SopTaskCreate) SetNillablePlanEndTime(t *time.Time) *SopTaskCreate {
  96. if t != nil {
  97. stc.SetPlanEndTime(*t)
  98. }
  99. return stc
  100. }
  101. // SetCreatorID sets the "creator_id" field.
  102. func (stc *SopTaskCreate) SetCreatorID(s string) *SopTaskCreate {
  103. stc.mutation.SetCreatorID(s)
  104. return stc
  105. }
  106. // SetNillableCreatorID sets the "creator_id" field if the given value is not nil.
  107. func (stc *SopTaskCreate) SetNillableCreatorID(s *string) *SopTaskCreate {
  108. if s != nil {
  109. stc.SetCreatorID(*s)
  110. }
  111. return stc
  112. }
  113. // SetDeletedAt sets the "deleted_at" field.
  114. func (stc *SopTaskCreate) SetDeletedAt(t time.Time) *SopTaskCreate {
  115. stc.mutation.SetDeletedAt(t)
  116. return stc
  117. }
  118. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  119. func (stc *SopTaskCreate) SetNillableDeletedAt(t *time.Time) *SopTaskCreate {
  120. if t != nil {
  121. stc.SetDeletedAt(*t)
  122. }
  123. return stc
  124. }
  125. // SetID sets the "id" field.
  126. func (stc *SopTaskCreate) SetID(u uint64) *SopTaskCreate {
  127. stc.mutation.SetID(u)
  128. return stc
  129. }
  130. // AddTaskStageIDs adds the "task_stages" edge to the SopStage entity by IDs.
  131. func (stc *SopTaskCreate) AddTaskStageIDs(ids ...uint64) *SopTaskCreate {
  132. stc.mutation.AddTaskStageIDs(ids...)
  133. return stc
  134. }
  135. // AddTaskStages adds the "task_stages" edges to the SopStage entity.
  136. func (stc *SopTaskCreate) AddTaskStages(s ...*SopStage) *SopTaskCreate {
  137. ids := make([]uint64, len(s))
  138. for i := range s {
  139. ids[i] = s[i].ID
  140. }
  141. return stc.AddTaskStageIDs(ids...)
  142. }
  143. // Mutation returns the SopTaskMutation object of the builder.
  144. func (stc *SopTaskCreate) Mutation() *SopTaskMutation {
  145. return stc.mutation
  146. }
  147. // Save creates the SopTask in the database.
  148. func (stc *SopTaskCreate) Save(ctx context.Context) (*SopTask, error) {
  149. stc.defaults()
  150. return withHooks(ctx, stc.sqlSave, stc.mutation, stc.hooks)
  151. }
  152. // SaveX calls Save and panics if Save returns an error.
  153. func (stc *SopTaskCreate) SaveX(ctx context.Context) *SopTask {
  154. v, err := stc.Save(ctx)
  155. if err != nil {
  156. panic(err)
  157. }
  158. return v
  159. }
  160. // Exec executes the query.
  161. func (stc *SopTaskCreate) Exec(ctx context.Context) error {
  162. _, err := stc.Save(ctx)
  163. return err
  164. }
  165. // ExecX is like Exec, but panics if an error occurs.
  166. func (stc *SopTaskCreate) ExecX(ctx context.Context) {
  167. if err := stc.Exec(ctx); err != nil {
  168. panic(err)
  169. }
  170. }
  171. // defaults sets the default values of the builder before save.
  172. func (stc *SopTaskCreate) defaults() {
  173. if _, ok := stc.mutation.CreatedAt(); !ok {
  174. v := soptask.DefaultCreatedAt()
  175. stc.mutation.SetCreatedAt(v)
  176. }
  177. if _, ok := stc.mutation.UpdatedAt(); !ok {
  178. v := soptask.DefaultUpdatedAt()
  179. stc.mutation.SetUpdatedAt(v)
  180. }
  181. if _, ok := stc.mutation.Status(); !ok {
  182. v := soptask.DefaultStatus
  183. stc.mutation.SetStatus(v)
  184. }
  185. if _, ok := stc.mutation.GetType(); !ok {
  186. v := soptask.DefaultType
  187. stc.mutation.SetType(v)
  188. }
  189. }
  190. // check runs all checks and user-defined validators on the builder.
  191. func (stc *SopTaskCreate) check() error {
  192. if _, ok := stc.mutation.CreatedAt(); !ok {
  193. return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "SopTask.created_at"`)}
  194. }
  195. if _, ok := stc.mutation.UpdatedAt(); !ok {
  196. return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "SopTask.updated_at"`)}
  197. }
  198. if _, ok := stc.mutation.Name(); !ok {
  199. return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "SopTask.name"`)}
  200. }
  201. if v, ok := stc.mutation.Name(); ok {
  202. if err := soptask.NameValidator(v); err != nil {
  203. return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "SopTask.name": %w`, err)}
  204. }
  205. }
  206. if _, ok := stc.mutation.GetType(); !ok {
  207. return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "SopTask.type"`)}
  208. }
  209. return nil
  210. }
  211. func (stc *SopTaskCreate) sqlSave(ctx context.Context) (*SopTask, error) {
  212. if err := stc.check(); err != nil {
  213. return nil, err
  214. }
  215. _node, _spec := stc.createSpec()
  216. if err := sqlgraph.CreateNode(ctx, stc.driver, _spec); err != nil {
  217. if sqlgraph.IsConstraintError(err) {
  218. err = &ConstraintError{msg: err.Error(), wrap: err}
  219. }
  220. return nil, err
  221. }
  222. if _spec.ID.Value != _node.ID {
  223. id := _spec.ID.Value.(int64)
  224. _node.ID = uint64(id)
  225. }
  226. stc.mutation.id = &_node.ID
  227. stc.mutation.done = true
  228. return _node, nil
  229. }
  230. func (stc *SopTaskCreate) createSpec() (*SopTask, *sqlgraph.CreateSpec) {
  231. var (
  232. _node = &SopTask{config: stc.config}
  233. _spec = sqlgraph.NewCreateSpec(soptask.Table, sqlgraph.NewFieldSpec(soptask.FieldID, field.TypeUint64))
  234. )
  235. if id, ok := stc.mutation.ID(); ok {
  236. _node.ID = id
  237. _spec.ID.Value = id
  238. }
  239. if value, ok := stc.mutation.CreatedAt(); ok {
  240. _spec.SetField(soptask.FieldCreatedAt, field.TypeTime, value)
  241. _node.CreatedAt = value
  242. }
  243. if value, ok := stc.mutation.UpdatedAt(); ok {
  244. _spec.SetField(soptask.FieldUpdatedAt, field.TypeTime, value)
  245. _node.UpdatedAt = value
  246. }
  247. if value, ok := stc.mutation.Status(); ok {
  248. _spec.SetField(soptask.FieldStatus, field.TypeUint8, value)
  249. _node.Status = value
  250. }
  251. if value, ok := stc.mutation.Name(); ok {
  252. _spec.SetField(soptask.FieldName, field.TypeString, value)
  253. _node.Name = value
  254. }
  255. if value, ok := stc.mutation.BotWxidList(); ok {
  256. _spec.SetField(soptask.FieldBotWxidList, field.TypeJSON, value)
  257. _node.BotWxidList = value
  258. }
  259. if value, ok := stc.mutation.GetType(); ok {
  260. _spec.SetField(soptask.FieldType, field.TypeInt, value)
  261. _node.Type = value
  262. }
  263. if value, ok := stc.mutation.PlanStartTime(); ok {
  264. _spec.SetField(soptask.FieldPlanStartTime, field.TypeTime, value)
  265. _node.PlanStartTime = value
  266. }
  267. if value, ok := stc.mutation.PlanEndTime(); ok {
  268. _spec.SetField(soptask.FieldPlanEndTime, field.TypeTime, value)
  269. _node.PlanEndTime = value
  270. }
  271. if value, ok := stc.mutation.CreatorID(); ok {
  272. _spec.SetField(soptask.FieldCreatorID, field.TypeString, value)
  273. _node.CreatorID = value
  274. }
  275. if value, ok := stc.mutation.DeletedAt(); ok {
  276. _spec.SetField(soptask.FieldDeletedAt, field.TypeTime, value)
  277. _node.DeletedAt = value
  278. }
  279. if nodes := stc.mutation.TaskStagesIDs(); len(nodes) > 0 {
  280. edge := &sqlgraph.EdgeSpec{
  281. Rel: sqlgraph.O2M,
  282. Inverse: false,
  283. Table: soptask.TaskStagesTable,
  284. Columns: []string{soptask.TaskStagesColumn},
  285. Bidi: false,
  286. Target: &sqlgraph.EdgeTarget{
  287. IDSpec: sqlgraph.NewFieldSpec(sopstage.FieldID, field.TypeUint64),
  288. },
  289. }
  290. for _, k := range nodes {
  291. edge.Target.Nodes = append(edge.Target.Nodes, k)
  292. }
  293. _spec.Edges = append(_spec.Edges, edge)
  294. }
  295. return _node, _spec
  296. }
  297. // SopTaskCreateBulk is the builder for creating many SopTask entities in bulk.
  298. type SopTaskCreateBulk struct {
  299. config
  300. err error
  301. builders []*SopTaskCreate
  302. }
  303. // Save creates the SopTask entities in the database.
  304. func (stcb *SopTaskCreateBulk) Save(ctx context.Context) ([]*SopTask, error) {
  305. if stcb.err != nil {
  306. return nil, stcb.err
  307. }
  308. specs := make([]*sqlgraph.CreateSpec, len(stcb.builders))
  309. nodes := make([]*SopTask, len(stcb.builders))
  310. mutators := make([]Mutator, len(stcb.builders))
  311. for i := range stcb.builders {
  312. func(i int, root context.Context) {
  313. builder := stcb.builders[i]
  314. builder.defaults()
  315. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  316. mutation, ok := m.(*SopTaskMutation)
  317. if !ok {
  318. return nil, fmt.Errorf("unexpected mutation type %T", m)
  319. }
  320. if err := builder.check(); err != nil {
  321. return nil, err
  322. }
  323. builder.mutation = mutation
  324. var err error
  325. nodes[i], specs[i] = builder.createSpec()
  326. if i < len(mutators)-1 {
  327. _, err = mutators[i+1].Mutate(root, stcb.builders[i+1].mutation)
  328. } else {
  329. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  330. // Invoke the actual operation on the latest mutation in the chain.
  331. if err = sqlgraph.BatchCreate(ctx, stcb.driver, spec); err != nil {
  332. if sqlgraph.IsConstraintError(err) {
  333. err = &ConstraintError{msg: err.Error(), wrap: err}
  334. }
  335. }
  336. }
  337. if err != nil {
  338. return nil, err
  339. }
  340. mutation.id = &nodes[i].ID
  341. if specs[i].ID.Value != nil && nodes[i].ID == 0 {
  342. id := specs[i].ID.Value.(int64)
  343. nodes[i].ID = uint64(id)
  344. }
  345. mutation.done = true
  346. return nodes[i], nil
  347. })
  348. for i := len(builder.hooks) - 1; i >= 0; i-- {
  349. mut = builder.hooks[i](mut)
  350. }
  351. mutators[i] = mut
  352. }(i, ctx)
  353. }
  354. if len(mutators) > 0 {
  355. if _, err := mutators[0].Mutate(ctx, stcb.builders[0].mutation); err != nil {
  356. return nil, err
  357. }
  358. }
  359. return nodes, nil
  360. }
  361. // SaveX is like Save, but panics if an error occurs.
  362. func (stcb *SopTaskCreateBulk) SaveX(ctx context.Context) []*SopTask {
  363. v, err := stcb.Save(ctx)
  364. if err != nil {
  365. panic(err)
  366. }
  367. return v
  368. }
  369. // Exec executes the query.
  370. func (stcb *SopTaskCreateBulk) Exec(ctx context.Context) error {
  371. _, err := stcb.Save(ctx)
  372. return err
  373. }
  374. // ExecX is like Exec, but panics if an error occurs.
  375. func (stcb *SopTaskCreateBulk) ExecX(ctx context.Context) {
  376. if err := stcb.Exec(ctx); err != nil {
  377. panic(err)
  378. }
  379. }