update_employee_logic.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. SetNotNilCategoryID(req.CategoryId).
  41. Save(l.ctx)
  42. if err != nil {
  43. _ = tx.Rollback()
  44. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  45. }
  46. // 批量修改工作经历
  47. if len(req.WorkExperience) > 0 {
  48. for _, work := range req.WorkExperience {
  49. var startDate, endDate *time.Time
  50. startDateStr, _ := time.Parse("2006-01-02 15:04:05", *work.StartDateStr)
  51. startDate = &startDateStr
  52. if work.EndDateStr != nil && *work.EndDateStr != "" {
  53. endDateStr, _ := time.Parse("2006-01-02 15:04:05", *work.EndDateStr)
  54. endDate = &endDateStr
  55. }
  56. _, err = tx.WorkExperience.UpdateOneID(*work.Id).
  57. SetNotNilStartDate(startDate).
  58. SetNotNilEndDate(endDate).
  59. SetNotNilCompany(work.Company).
  60. SetNotNilExperience(work.Experience).
  61. Save(l.ctx)
  62. if err != nil {
  63. _ = tx.Rollback()
  64. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  65. }
  66. }
  67. }
  68. // 批量修改使用教程
  69. if len(req.Tutorial) > 0 {
  70. for _, tt := range req.Tutorial {
  71. _, err = tx.Tutorial.UpdateOneID(*tt.Id).
  72. SetNotNilIndex(tt.Index).
  73. SetNotNilTitle(tt.Title).
  74. SetNotNilContent(tt.Content).
  75. Save(l.ctx)
  76. if err != nil {
  77. _ = tx.Rollback()
  78. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  79. }
  80. }
  81. }
  82. _ = tx.Commit()
  83. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  84. }