create_employee_logic.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package employee
  2. import (
  3. "context"
  4. "time"
  5. "wechat-api/ent"
  6. "wechat-api/internal/svc"
  7. "wechat-api/internal/types"
  8. "wechat-api/internal/utils/dberrorhandler"
  9. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type CreateEmployeeLogic struct {
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. logx.Logger
  16. }
  17. func NewCreateEmployeeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateEmployeeLogic {
  18. return &CreateEmployeeLogic{
  19. ctx: ctx,
  20. svcCtx: svcCtx,
  21. Logger: logx.WithContext(ctx),
  22. }
  23. }
  24. func (l *CreateEmployeeLogic) CreateEmployee(req *types.EmployeeInfo) (*types.BaseMsgResp, error) {
  25. organizationId := l.ctx.Value("organizationId").(uint64)
  26. // 开始事务
  27. tx, err := l.svcCtx.DB.Tx(context.Background())
  28. employee, err := tx.Employee.Create().
  29. SetNotNilTitle(req.Title).
  30. SetNotNilAvatar(req.Avatar).
  31. SetNotNilTags(req.Tags).
  32. SetNotNilHireCount(req.HireCount).
  33. SetNotNilServiceCount(req.ServiceCount).
  34. SetNotNilAchievementCount(req.AchievementCount).
  35. SetNotNilIntro(req.Intro).
  36. SetNotNilEstimate(req.Estimate).
  37. SetNotNilSkill(req.Skill).
  38. SetNotNilAbilityType(req.AbilityType).
  39. SetNotNilScene(req.Scene).
  40. SetNotNilSwitchIn(req.SwitchIn).
  41. SetNotNilVideoURL(req.VideoUrl).
  42. SetNotNilOrganizationID(&organizationId).
  43. SetNotNilCategoryID(req.CategoryId).
  44. SetNotNilAPIBase(req.ApiBase).
  45. SetNotNilAPIKey(req.ApiKey).
  46. SetNotNilChatURL(req.ChatUrl).
  47. Save(l.ctx)
  48. if err != nil {
  49. _ = tx.Rollback()
  50. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  51. }
  52. // 批量插入工作经历
  53. if len(req.WorkExperience) > 0 {
  54. bulkCreateWork := make([]*ent.WorkExperienceCreate, 0, len(req.WorkExperience))
  55. for _, work := range req.WorkExperience {
  56. var startDate, endDate *time.Time
  57. startDateStr, _ := time.Parse("2006-01-02 15:04:05", *work.StartDateStr)
  58. startDate = &startDateStr
  59. if work.EndDateStr != nil && *work.EndDateStr != "" {
  60. endDateStr, _ := time.Parse("2006-01-02 15:04:05", *work.EndDateStr)
  61. endDate = &endDateStr
  62. }
  63. bulkCreateWork = append(bulkCreateWork, tx.WorkExperience.Create().
  64. SetNotNilEmployeeID(&employee.ID).
  65. SetNotNilStartDate(startDate).
  66. SetNotNilEndDate(endDate).
  67. SetNotNilCompany(work.Company).
  68. SetNotNilOrganizationID(&organizationId).
  69. SetNotNilExperience(work.Experience),
  70. )
  71. }
  72. if len(bulkCreateWork) > 0 {
  73. err = tx.WorkExperience.CreateBulk(bulkCreateWork...).Exec(l.ctx)
  74. if err != nil {
  75. _ = tx.Rollback()
  76. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  77. }
  78. }
  79. }
  80. // 批量插入使用教程
  81. if len(req.Tutorial) > 0 {
  82. bulkCreateTutorial := make([]*ent.TutorialCreate, 0, len(req.Tutorial))
  83. for _, work := range req.Tutorial {
  84. bulkCreateTutorial = append(bulkCreateTutorial, tx.Tutorial.Create().
  85. SetNotNilEmployeeID(&employee.ID).
  86. SetNotNilOrganizationID(&organizationId).
  87. SetNotNilIndex(work.Index).
  88. SetNotNilTitle(work.Title).
  89. SetNotNilContent(work.Content),
  90. )
  91. }
  92. if len(bulkCreateTutorial) > 0 {
  93. err = tx.Tutorial.CreateBulk(bulkCreateTutorial...).Exec(l.ctx)
  94. if err != nil {
  95. _ = tx.Rollback()
  96. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  97. }
  98. }
  99. }
  100. _ = tx.Commit()
  101. return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
  102. }