get_employee_by_id_logic.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package employee
  2. import (
  3. "context"
  4. "github.com/spf13/cast"
  5. "strings"
  6. "wechat-api/ent/employee"
  7. "wechat-api/ent/employeeconfig"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "wechat-api/internal/utils/dberrorhandler"
  11. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  12. "github.com/suyuan32/simple-admin-common/utils/pointy"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type GetEmployeeByIdLogic struct {
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. logx.Logger
  19. }
  20. func NewGetEmployeeByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetEmployeeByIdLogic {
  21. return &GetEmployeeByIdLogic{
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. Logger: logx.WithContext(ctx),
  25. }
  26. }
  27. func (l *GetEmployeeByIdLogic) GetEmployeeById(req *types.IDReq) (*types.EmployeeInfoResp, error) {
  28. organizationId := l.ctx.Value("organizationId").(uint64)
  29. data, err := l.svcCtx.DB.Employee.Query().WithEmWorkExperiences().WithEmTutorial().Where(employee.ID(req.Id), employee.OrganizationIDEQ(organizationId)).Only(l.ctx)
  30. if err != nil {
  31. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  32. }
  33. workExperience := make([]types.WorkExperienceInfo, 0, len(data.Edges.EmWorkExperiences))
  34. for _, work := range data.Edges.EmWorkExperiences {
  35. workExperience = append(workExperience, types.WorkExperienceInfo{
  36. BaseIDInfo: types.BaseIDInfo{
  37. Id: &work.ID,
  38. CreatedAt: pointy.GetPointer(work.CreatedAt.UnixMilli()),
  39. UpdatedAt: pointy.GetPointer(work.UpdatedAt.UnixMilli()),
  40. },
  41. EmployeeId: &work.EmployeeID,
  42. Company: &work.Company,
  43. StartDate: pointy.GetPointer(work.StartDate.UnixMilli()),
  44. StartDateStr: pointy.GetPointer(work.StartDate.Format("2006-01-02 15:04:05")),
  45. EndDate: pointy.GetPointer(work.EndDate.UnixMilli()),
  46. EndDateStr: pointy.GetPointer(work.EndDate.Format("2006-01-02 15:04:05")),
  47. Experience: &work.Experience,
  48. })
  49. }
  50. tutorial := make([]types.TutorialInfo, 0, len(data.Edges.EmTutorial))
  51. for _, tt := range data.Edges.EmTutorial {
  52. tutorial = append(tutorial, types.TutorialInfo{
  53. BaseIDInfo: types.BaseIDInfo{
  54. Id: &tt.ID,
  55. CreatedAt: pointy.GetPointer(tt.CreatedAt.UnixMilli()),
  56. UpdatedAt: pointy.GetPointer(tt.UpdatedAt.UnixMilli()),
  57. },
  58. EmployeeId: &tt.EmployeeID,
  59. Content: &tt.Content,
  60. Title: &tt.Title,
  61. Index: &tt.Index,
  62. })
  63. }
  64. // 分别获得 scene 和 switch_in 的列表
  65. sceneList := getEmployeeConfigList(l, data.Scene)
  66. switchInList := getEmployeeConfigList(l, data.SwitchIn)
  67. return &types.EmployeeInfoResp{
  68. BaseDataInfo: types.BaseDataInfo{
  69. Code: 0,
  70. Msg: errormsg.Success,
  71. },
  72. Data: types.EmployeeInfo{
  73. BaseIDInfo: types.BaseIDInfo{
  74. Id: &data.ID,
  75. CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
  76. UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
  77. },
  78. Title: &data.Title,
  79. Avatar: &data.Avatar,
  80. Tags: &data.Tags,
  81. HireCount: &data.HireCount,
  82. ServiceCount: &data.ServiceCount,
  83. AchievementCount: &data.AchievementCount,
  84. Intro: &data.Intro,
  85. Estimate: &data.Estimate,
  86. Skill: &data.Skill,
  87. AbilityType: &data.AbilityType,
  88. Scene: &data.Scene,
  89. SceneList: sceneList,
  90. Tutorial: tutorial,
  91. SwitchIn: &data.SwitchIn,
  92. SwitchInList: switchInList,
  93. VideoUrl: &data.VideoURL,
  94. WorkExperience: workExperience,
  95. CategoryId: &data.CategoryID,
  96. ApiBase: &data.APIBase,
  97. ApiKey: &data.APIKey,
  98. ChatUrl: &data.ChatURL,
  99. },
  100. }, nil
  101. }
  102. func FormatIds(str string) []uint64 {
  103. str = strings.Trim(str, " ")
  104. idsArray := strings.Split(str, ",")
  105. idArray := make([]uint64, 0, len(idsArray))
  106. for _, idstr := range idsArray {
  107. idArray = append(idArray, cast.ToUint64(idstr))
  108. }
  109. return idArray
  110. }
  111. func getEmployeeConfigList(l *GetEmployeeByIdLogic, ids string) []types.EmployeeConfigInfo {
  112. sceneList := make([]types.EmployeeConfigInfo, 0)
  113. sceneIds := FormatIds(ids)
  114. if len(sceneIds) == 0 {
  115. return sceneList
  116. }
  117. employeeConfigList, err := l.svcCtx.DB.EmployeeConfig.Query().Where(
  118. employeeconfig.IDIn(sceneIds...),
  119. ).All(l.ctx)
  120. if err == nil && len(employeeConfigList) > 0 {
  121. for _, val := range employeeConfigList {
  122. sceneList = append(sceneList, types.EmployeeConfigInfo{
  123. BaseIDInfo: types.BaseIDInfo{
  124. Id: &val.ID,
  125. CreatedAt: pointy.GetPointer(val.CreatedAt.UnixMilli()),
  126. UpdatedAt: pointy.GetPointer(val.UpdatedAt.UnixMilli()),
  127. },
  128. Stype: &val.Stype,
  129. Title: &val.Title,
  130. Photo: &val.Photo,
  131. })
  132. }
  133. }
  134. return sceneList
  135. }