selection.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/caixw/lib.go/conv"
  6. "github.com/sirupsen/logrus"
  7. "strconv"
  8. "youngee_m_api/consts"
  9. "youngee_m_api/model/gorm_model"
  10. "youngee_m_api/model/http_model"
  11. )
  12. func SelectionReviewNumber(ctx context.Context) (*http_model.ReviewNums, error) {
  13. var reviewNums int64
  14. db := GetReadDB(ctx)
  15. err := db.Model(gorm_model.YounggeeSelectionInfo{}).Where("selection_status = 2").Count(&reviewNums).Error
  16. if err != nil {
  17. return nil, err
  18. }
  19. ReviewNums := new(http_model.ReviewNums)
  20. ReviewNums.ReviewNums = reviewNums
  21. return ReviewNums, err
  22. }
  23. func GetSelectionInfo(ctx context.Context, req *http_model.GetSelectionInfoRequest) (selectionInfoData http_model.SelectionInfoData, err error) {
  24. db := GetReadDB(ctx)
  25. db = db.Debug().Model(gorm_model.YounggeeSelectionInfo{}).Where("enterprise_id = ?", req.EnterpriseId)
  26. if req.UpdateAt != "" {
  27. db = db.Where(fmt.Sprintf("updated_at like '%s%%'", req.UpdateAt))
  28. }
  29. // 查询总数
  30. var total int64
  31. if err = db.Count(&total).Error; err != nil {
  32. logrus.WithContext(ctx).Errorf("[GetSelectionInfo] error query mysql total, err:%+v", err)
  33. return
  34. }
  35. var selectionInfos []*gorm_model.YounggeeSelectionInfo
  36. // 查询该页数据
  37. limit := req.PageSize
  38. offset := req.PageSize * req.PageNum // assert pageNum start with 0
  39. err = db.Order("updated_at desc").Limit(int(limit)).Offset(int(offset)).Find(&selectionInfos).Error
  40. if err != nil {
  41. logrus.WithContext(ctx).Errorf("[GetSelectionInfo] error query mysql limit, err:%+v", err)
  42. return
  43. }
  44. var selectionInfoPreviews []*http_model.SelectionInfoPreview
  45. for _, selectionInfo := range selectionInfos {
  46. selectionInfoPreview := new(http_model.SelectionInfoPreview)
  47. selectionInfoPreview.SelectionId = selectionInfo.SelectionID
  48. selectionInfoPreview.SelectionName = selectionInfo.SelectionName
  49. selectionInfoPreview.UpdateAt = conv.MustString(selectionInfo.UpdatedAt, "")[:19]
  50. selectionInfoPreview.TaskModel = consts.GetTaskModel(selectionInfo.TaskMode)
  51. selectionInfoPreview.SampleModel = consts.GetSampleModel(selectionInfo.SampleMode)
  52. selectionInfoPreviews = append(selectionInfoPreviews, selectionInfoPreview)
  53. }
  54. selectionInfoData.SelectionInfoPreview = selectionInfoPreviews
  55. selectionInfoData.Total = strconv.FormatInt(total, 10)
  56. return
  57. }