project.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "youngee_b_api/util"
  10. )
  11. func CreateProject(ctx context.Context, projectInfo gorm_model.ProjectInfo) (*int64, error) {
  12. db := GetWriteDB(ctx)
  13. err := db.Create(&projectInfo).Error
  14. if err != nil {
  15. return nil, err
  16. }
  17. return &projectInfo.ProjectID, nil
  18. }
  19. func GetFullProjectList(ctx context.Context, enterpriseID int64, pageSize, pageNum int32, condition *common_model.ProjectCondition) ([]*gorm_model.ProjectInfo, int64, error) {
  20. db := GetReadDB(ctx)
  21. // 根据企业id过滤
  22. db = db.Debug().Model(gorm_model.ProjectInfo{}).Where("enterprise_id = ?", enterpriseID)
  23. // 根据Project条件过滤
  24. conditionType := reflect.TypeOf(condition).Elem()
  25. conditionValue := reflect.ValueOf(condition).Elem()
  26. for i := 0; i < conditionType.NumField(); i++ {
  27. field := conditionType.Field(i)
  28. tag := field.Tag.Get("condition")
  29. value := conditionValue.FieldByName(field.Name)
  30. //if ! isBlank(value) {
  31. // db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  32. //}
  33. if ! util.IsBlank(value) && tag != "updated_at"&& tag != "project_name"{
  34. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  35. }
  36. if tag == "updated_at"{
  37. fmt.Println(value.Interface())
  38. db = db.Where(fmt.Sprintf("%s > ?", tag), value.Interface())
  39. }
  40. if tag == "project_name" && ! util.IsBlank(value){
  41. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  42. }
  43. }
  44. // 查询总数
  45. var total int64
  46. var fullProjects []*gorm_model.ProjectInfo
  47. if err := db.Count(&total).Error; err != nil {
  48. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  49. return nil, 0, err
  50. }
  51. // 查询该页数据
  52. limit := pageSize
  53. offset := pageSize * pageNum // assert pageNum start with 0
  54. err := db.Order("project_id").Limit(int(limit)).Offset(int(offset)).Find(&fullProjects).Error
  55. if err != nil {
  56. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  57. return nil, 0, err
  58. }
  59. return fullProjects, total, nil
  60. }