update_employee_logic.go 2.6 KB

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