project.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/sirupsen/logrus"
  6. "reflect"
  7. "youngee_b_api/model/common_model"
  8. "youngee_b_api/model/gorm_model"
  9. )
  10. func CreateProject(ctx context.Context, projectInfo gorm_model.ProjectInfo) (*int64, error) {
  11. db := GetWriteDB(ctx)
  12. err := db.Create(&projectInfo).Error
  13. if err != nil {
  14. return nil, err
  15. }
  16. return &projectInfo.ProjectID, nil
  17. }
  18. func GetFullProjectList(ctx context.Context, enterpriseID int64, pageSize, pageNum int32, condition *common_model.ProjectCondition) ([]*gorm_model.ProjectInfo, int64, error) {
  19. db := GetReadDB(ctx)
  20. // 根据企业id过滤
  21. db = db.Debug().Model(gorm_model.ProjectInfo{}).Where("enterprise_id = ?", enterpriseID)
  22. // 根据Project条件过滤
  23. conditionType := reflect.TypeOf(condition)
  24. conditionValue := reflect.ValueOf(condition)
  25. for i := 0; i < conditionValue.NumField(); i++ {
  26. field := conditionType.Field(i)
  27. tag := field.Tag.Get("condition")
  28. value := conditionValue.FieldByName(field.Name)
  29. db = db.Where(fmt.Sprintf("%s = ?", tag), value)
  30. }
  31. // 查询总数
  32. var total int64
  33. var fullProjects []*gorm_model.ProjectInfo
  34. if err := db.Count(&total).Error; err != nil {
  35. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  36. return nil, 0, err
  37. }
  38. // 查询该页数据
  39. limit := pageNum
  40. offset := pageSize * pageNum // assert pageNum start with 0
  41. err := db.Order("updated_at desc").Limit(int(limit)).Offset(int(offset)).Find(&fullProjects).Error
  42. if err != nil {
  43. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  44. return nil, 0, err
  45. }
  46. return fullProjects, total, nil
  47. }