project.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/caixw/lib.go/conv"
  6. "github.com/sirupsen/logrus"
  7. "gorm.io/gorm"
  8. "reflect"
  9. "youngee_m_api/model/common_model"
  10. "youngee_m_api/model/gorm_model"
  11. "youngee_m_api/util"
  12. )
  13. func GetFullProjectList(ctx context.Context, pageSize, pageNum int32, condition *common_model.ProjectCondition) ([]*gorm_model.ProjectInfo, int64, error) {
  14. db := GetReadDB(ctx)
  15. // 根据企业id过滤
  16. db = db.Debug().Model(gorm_model.ProjectInfo{})
  17. // 根据Project条件过滤
  18. conditionType := reflect.TypeOf(condition).Elem()
  19. conditionValue := reflect.ValueOf(condition).Elem()
  20. for i := 0; i < conditionType.NumField(); i++ {
  21. field := conditionType.Field(i)
  22. tag := field.Tag.Get("condition")
  23. value := conditionValue.FieldByName(field.Name)
  24. if tag == "project_status" && util.IsBlank(value) {
  25. db = db.Where(fmt.Sprintf("project_status != 1"))
  26. }
  27. if !util.IsBlank(value) && tag != "updated_at" && tag != "project_name" {
  28. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  29. }
  30. if tag == "updated_at" && value.Interface() != "0" {
  31. db = db.Where(fmt.Sprintf("%s > ?", tag), value.Interface())
  32. }
  33. if tag == "project_name" && !util.IsBlank(value) {
  34. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  35. }
  36. }
  37. // 查询总数
  38. var total int64
  39. var fullProjects []*gorm_model.ProjectInfo
  40. if err := db.Count(&total).Error; err != nil {
  41. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  42. return nil, 0, err
  43. }
  44. // 查询该页数据
  45. limit := pageSize
  46. offset := pageSize * pageNum // assert pageNum start with 0
  47. err := db.Order("project_id").Limit(int(limit)).Offset(int(offset)).Find(&fullProjects).Error
  48. if err != nil {
  49. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  50. return nil, 0, err
  51. }
  52. return fullProjects, total, nil
  53. }
  54. func GetProjectDetail(ctx context.Context, projectID int64) (*gorm_model.ProjectInfo, error) {
  55. db := GetReadDB(ctx)
  56. var ProjectDetail *gorm_model.ProjectInfo
  57. err := db.Where("project_id = ?", projectID).First(&ProjectDetail).Error
  58. if err != nil {
  59. if err == gorm.ErrRecordNotFound {
  60. return nil, nil
  61. } else {
  62. return nil, err
  63. }
  64. }
  65. return ProjectDetail, nil
  66. }
  67. func GetRecruitStrategys(ctx context.Context, ProjectID int64) ([]gorm_model.RecruitStrategy, error) {
  68. db := GetReadDB(ctx)
  69. RecruitStrategys := []gorm_model.RecruitStrategy{}
  70. err := db.Where("project_id=?", ProjectID).Find(&RecruitStrategys).Error
  71. if err != nil {
  72. return nil, err
  73. }
  74. return RecruitStrategys, nil
  75. }
  76. func GetProjectPhoto(ctx context.Context, ProjectID int64) ([]gorm_model.ProjectPhoto, error) {
  77. db := GetReadDB(ctx)
  78. var ProjectPhoto []gorm_model.ProjectPhoto
  79. err := db.Where("project_id=?", ProjectID).Find(&ProjectPhoto).Error
  80. if err != nil {
  81. return nil, err
  82. }
  83. return ProjectPhoto, nil
  84. }
  85. func UpdateProject(ctx context.Context, project gorm_model.ProjectInfo) (*int64, error) {
  86. db := GetReadDB(ctx)
  87. err := db.Model(&project).Updates(project).Error
  88. if err != nil {
  89. return nil, err
  90. }
  91. return &project.ProjectID, nil
  92. }
  93. func ApproveProject(ctx context.Context, projectId string,isApprove int64) (error error,message string) {
  94. project_id := conv.MustInt64(projectId,0)
  95. db := GetReadDB(ctx)
  96. projectInfo := gorm_model.ProjectInfo{}
  97. db = db.Debug().Model(gorm_model.ProjectInfo{}).Where("project_id = ?",project_id).First(&projectInfo)
  98. fmt.Println("isApprove:",isApprove)
  99. if isApprove == 1 && projectInfo.ProjectStatus == 2{
  100. err := db.Update("project_status", 4).Error
  101. message = "审核通过"
  102. if err != nil {
  103. logrus.Println("DB AutoUpdateStatus error :", err)
  104. return err,""
  105. }
  106. }else if isApprove == 2 && projectInfo.ProjectStatus == 2{
  107. err := db.Update("project_status", 8).Error
  108. message = "项目存在风险已禁止发布"
  109. if err != nil {
  110. logrus.Println("DB AutoUpdateStatus error :", err)
  111. return err,""
  112. }
  113. }else {
  114. return nil,"操作失败"
  115. }
  116. return nil,message
  117. }