update_employee_logic.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package employee
  2. import (
  3. "context"
  4. "time"
  5. "wechat-api/internal/svc"
  6. "wechat-api/internal/types"
  7. "wechat-api/internal/utils/dberrorhandler"
  8. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. type UpdateEmployeeLogic struct {
  12. ctx context.Context
  13. svcCtx *svc.ServiceContext
  14. logx.Logger
  15. }
  16. func NewUpdateEmployeeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateEmployeeLogic {
  17. return &UpdateEmployeeLogic{
  18. ctx: ctx,
  19. svcCtx: svcCtx,
  20. Logger: logx.WithContext(ctx),
  21. }
  22. }
  23. func (l *UpdateEmployeeLogic) UpdateEmployee(req *types.EmployeeInfo) (*types.BaseMsgResp, error) {
  24. // 开始事务
  25. tx, err := l.svcCtx.DB.Tx(context.Background())
  26. _, err = tx.Employee.UpdateOneID(*req.Id).
  27. SetNotNilTitle(req.Title).
  28. SetNotNilAvatar(req.Avatar).
  29. SetNotNilTags(req.Tags).
  30. SetNotNilHireCount(req.HireCount).
  31. SetNotNilServiceCount(req.ServiceCount).
  32. SetNotNilAchievementCount(req.AchievementCount).
  33. SetNotNilIntro(req.Intro).
  34. SetNotNilEstimate(req.Estimate).
  35. SetNotNilSkill(req.Skill).
  36. SetNotNilAbilityType(req.AbilityType).
  37. SetNotNilScene(req.Scene).
  38. SetNotNilSwitchIn(req.SwitchIn).
  39. SetNotNilVideoURL(req.VideoUrl).
  40. Save(l.ctx)
  41. if err != nil {
  42. _ = tx.Rollback()
  43. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  44. }
  45. // 批量修改工作经历
  46. if len(req.WorkExperience) > 0 {
  47. for _, work := range req.WorkExperience {
  48. var startDate, endDate *time.Time
  49. startDateStr, _ := time.Parse("2006-01-02 15:04:05", *work.StartDateStr)
  50. startDate = &startDateStr
  51. if work.EndDateStr != nil && *work.EndDateStr != "" {
  52. endDateStr, _ := time.Parse("2006-01-02 15:04:05", *work.EndDateStr)
  53. endDate = &endDateStr
  54. }
  55. _, err = tx.WorkExperience.UpdateOneID(*work.Id).
  56. SetNotNilStartDate(startDate).
  57. SetNotNilEndDate(endDate).
  58. SetNotNilCompany(work.Company).
  59. SetNotNilExperience(work.Experience).
  60. Save(l.ctx)
  61. if err != nil {
  62. _ = tx.Rollback()
  63. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  64. }
  65. }
  66. }
  67. // 批量修改使用教程
  68. if len(req.Tutorial) > 0 {
  69. for _, tt := range req.Tutorial {
  70. _, err = tx.Tutorial.UpdateOneID(*tt.Id).
  71. SetNotNilIndex(tt.Index).
  72. SetNotNilTitle(tt.Title).
  73. SetNotNilContent(tt.Content).
  74. Save(l.ctx)
  75. if err != nil {
  76. _ = tx.Rollback()
  77. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  78. }
  79. }
  80. }
  81. _ = tx.Commit()
  82. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  83. }