package service import ( "context" "fmt" "github.com/caixw/lib.go/conv" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "strconv" "strings" "youngee_m_api/db" "youngee_m_api/model/common_model" "youngee_m_api/model/gorm_model" "youngee_m_api/model/http_model" "youngee_m_api/pack" ) var Project *project type project struct { } func (*project) GetFullProjectList(ctx context.Context, pageSize, pageNum int32, condition *common_model.ProjectCondition) (*http_model.FullProjectListData, error) { fullProjects, total, err := db.GetFullProjectList(ctx, pageSize, pageNum, condition) if err != nil { logrus.WithContext(ctx).Errorf("[project service] call GetFullProjectList error,err:%+v", err) return nil, err } fullProjectListData := new(http_model.FullProjectListData) fullProjectListData.FullProjectPreview = pack.MGormFullProjectToHttpFullProjectPreview(fullProjects) fullProjectListData.Total = conv.MustString(total, "") return fullProjectListData, nil } func (*project) GetProjectDetail(ctx context.Context, EnterpriseID string, projectID int64) (*http_model.ShowProjectData, error) { project, err := db.GetProjectDetail(ctx, projectID) if err != nil { logrus.WithContext(ctx).Errorf("[project service] call GetPorjectDetail error,err:%+v", err) return nil, err } enterprise, err1 := db.GetEnterpriseByEnterpriseID(ctx, project.EnterpriseID) fmt.Println("%+v", enterprise.UserID) if err1 != nil { logrus.WithContext(ctx).Errorf("[project service] call GetEnterpriseByEnterpriseID error,err:%+v", err1) return nil, err1 } user, err2 := db.GetUserByID(ctx, enterprise.UserID) if err2 != nil { logrus.WithContext(ctx).Errorf("[project service] call GetUserByID error,err:%+v", err2) return nil, err2 } fmt.Println("%+v", user.Phone) //var RecruitStrategys []http_model.ShowRecruitStrategy ProjectDetail := http_model.ShowProjectData{ ProjectID: conv.MustString(project.ProjectID, ""), ProjectName: conv.MustString(project.ProjectName, ""), ProjectStatus: conv.MustString(project.ProjectStatus, ""), ProjectType: conv.MustString(project.ProjectType, ""), ProjectPlatform: conv.MustString(project.ProjectPlatform, ""), ProjectForm: conv.MustString(project.ProjectForm, ""), TalentType: conv.MustString(project.TalentType, ""), RecruitDdl: project.RecruitDdl, ContentType: conv.MustString(project.ContentType, ""), ProjectDetail: conv.MustString(project.ProjectDetail, ""), ProductID: conv.MustString(project.ProductID, ""), EnterpriseID: conv.MustString(project.EnterpriseID, ""), Balance: conv.MustString(enterprise.Balance, ""), CreateAt: project.CreatedAt, UpdateAt: project.UpdatedAt, Phone: user.Phone, } Strategys, err3 := db.GetRecruitStrategys(ctx, projectID) if err3 != nil { logrus.WithContext(ctx).Error() return nil, err3 } for _, strategy := range Strategys { RecruitStrategy := http_model.ShowRecruitStrategy{ FeeForm: conv.MustString(strategy.FeeForm, ""), StrategyID: conv.MustString(strategy.StrategyID, ""), FollowersLow: conv.MustString(strategy.FollowersLow, ""), FollowersUp: conv.MustString(strategy.FollowersUp, ""), RecruitNumber: conv.MustString(strategy.RecruitNumber, ""), Offer: conv.MustString(strategy.Offer, ""), } ProjectDetail.RecruitStrategys = append(ProjectDetail.RecruitStrategys, RecruitStrategy) } Photos, err4 := db.GetProjectPhoto(ctx, projectID) if err4 != nil { logrus.WithContext(ctx).Error() return nil, err4 } for _, Photo := range Photos { ProjectPhoto := http_model.ShowProjectPhoto{ PhotoUrl: Photo.PhotoUrl, PhotoUid: Photo.PhotoUid, } ProjectDetail.ProjectPhotos = append(ProjectDetail.ProjectPhotos, ProjectPhoto) } return &ProjectDetail, nil } func (*project) Update(ctx context.Context, newProject http_model.UpdateProjectRequest, enterpriseID string) (*http_model.UpdateProjectData, error) { fmt.Println("newproject:", newProject) project := gorm_model.ProjectInfo{ ProjectID: conv.MustInt64(newProject.ProjectID, 0), RecruitDdl: newProject.RecruitDdl, TalentType: newProject.TalentType, ContentType: conv.MustInt64(newProject.ContentType, 0), ProjectDetail: newProject.ProjectDetail, ProjectForm: conv.MustInt64(newProject.ProjectForm, 0), EnterpriseID: conv.MustInt64(enterpriseID, 0), ProjectStatus: 4, } projectID, err := db.UpdateProject(ctx, project) if err != nil { return nil, err } // 删除该项目之前的所有图片 err = db.DeleteProjectPhotoByProjecttID(ctx, *projectID) if err != nil { return nil, err } fmt.Printf("照片:\t %+v", newProject.ProjectPhotos) if newProject.ProjectPhotos != nil { // 新增图片 projectPhotos := []gorm_model.ProjectPhoto{} for _, photo := range newProject.ProjectPhotos { projectPhoto := gorm_model.ProjectPhoto{ ProjectID: project.ProjectID, PhotoUrl: photo.PhotoUrl, PhotoUid: photo.PhotoUid, } projectPhotos = append(projectPhotos, projectPhoto) } err = db.CreateProjectPhoto(ctx, projectPhotos) if err != nil { return nil, err } } // 删除该项目之前的所有策略 err = db.DeleteRecruitStrategyByProjectID(ctx, *projectID) if err != nil { return nil, err } fmt.Printf("策略:\t %+v", newProject.RecruitStrategys) if newProject.RecruitStrategys != nil { // 新增策略 RecruitStrategys := []gorm_model.RecruitStrategy{} for _, Strategy := range newProject.RecruitStrategys { RecruitStrategy := gorm_model.RecruitStrategy{ FeeForm: conv.MustInt64(Strategy.FeeForm, 0), StrategyID: conv.MustInt64(Strategy.StrategyID, 0), FollowersLow: conv.MustInt64(Strategy.FollowersLow, 0), FollowersUp: conv.MustInt64(Strategy.FollowersUp, 0), RecruitNumber: conv.MustInt64(Strategy.RecruitNumber, 0), Offer: conv.MustInt64(Strategy.Offer, 0), ProjectID: project.ProjectID, } fmt.Printf("Offer:\t %+v", Strategy.Offer) RecruitStrategys = append(RecruitStrategys, RecruitStrategy) } err = db.CreateRecruitStrategy(ctx, RecruitStrategys) if err != nil { return nil, err } } res := &http_model.UpdateProjectData{ ProjectID: *projectID, } return res, nil } func (*project) ApproveProject(ctx *gin.Context, data http_model.ApproveProjectRequest) (error, string) { fmt.Println("data.IsApprove:", data.IsApprove) err, message := db.ApproveProject(ctx, data.ProjectId, data.IsApprove) if err != nil { logrus.WithContext(ctx).Errorf("[project service] call ChangeTaskStatus error,err:%+v", err) return err, "" } return nil, message } func (*project) GetAllProject(ctx context.Context, pageSize, pageNum int32) (*http_model.GetAllProjectData, error) { allProjectPreviews, total, err := db.GetAllProject(ctx, pageSize, pageNum) if err != nil { logrus.WithContext(ctx).Errorf("[project service] call GetAllProject error,err:%+v", err) return nil, err } allProjects := new(http_model.GetAllProjectData) allProjects.AllProjectPreview = pack.MGormAllProjectToHttpAllProjectPreview(allProjectPreviews) allProjects.Total = conv.MustString(total, "") return allProjects, nil } func (*project) GetProjectTaskList(ctx context.Context, projectID string, pageSize, pageNum int64, conditions *common_model.TaskConditions) (*http_model.ProjectTaskListData, error) { projectTasks, total, err := db.GetProjectTaskList(ctx, projectID, pageSize, pageNum, conditions) if err != nil { logrus.WithContext(ctx).Errorf("[project service] call GetProjectTaskList error,err:%+v", err) return nil, err } projectTaskListData := new(http_model.ProjectTaskListData) projectTaskListData.ProjectTaskPreview = pack.MGormProjectTaskToHttpProjectTaskPreview(projectTasks) projectTaskListData.Total = conv.MustString(total, "") return projectTaskListData, nil } func (*project) Create(ctx context.Context, newProject http_model.CreateProjectRequest, enterpriseID int64) (*http_model.CreateProjectData, error) { // build gorm_model.ProjectInfo // 查询关联商品信息 product, err := db.GetProductByID(ctx, newProject.ProductID) if err != nil { return nil, err } // 按照品牌名-商品名对项目进行命名 projectName := product.BrandName + "-" + product.ProductName //feeForm := fmt.Sprintf("[") feeFrom := []string{} for _, strategy := range newProject.RecruitStrategys { //if strategy.StrategyID feeFrom = append(feeFrom, strconv.FormatInt(strategy.FeeForm, 10)) //feeForm += string(strategy.StrategyID) } feeFroms := strings.Join(feeFrom, ",") projectInfo := gorm_model.ProjectInfo{ ProjectName: projectName, ProjectStatus: 1, ProjectType: newProject.ProjectType, TalentType: newProject.TalentType, ProjectPlatform: newProject.ProjectPlatform, ProjectForm: newProject.ProjectForm, RecruitDdl: newProject.RecruitDdl, ProjectDetail: newProject.ProjectDetail, ContentType: newProject.ContentType, EnterpriseID: enterpriseID, ProductID: newProject.ProductID, FeeForm: feeFroms, } // db create ProjectInfo projectID, err := db.CreateProject(ctx, projectInfo) if err != nil { return nil, err } if newProject.ProjectPhotos != nil { // build []gorm_model.ProjectPhoto projectPhotos := []gorm_model.ProjectPhoto{} for _, photo := range newProject.ProjectPhotos { projectPhoto := gorm_model.ProjectPhoto{ PhotoUrl: photo.PhotoUrl, PhotoUid: photo.PhotoUid, ProjectID: *projectID, } projectPhotos = append(projectPhotos, projectPhoto) } // db create ProjectPhoto err = db.CreateProjectPhoto(ctx, projectPhotos) if err != nil { return nil, err } } // build recruitStrategys := []gorm_model.RecruitStrategy{} for _, strategy := range newProject.RecruitStrategys { recruitStrategy := gorm_model.RecruitStrategy{ FeeForm: strategy.FeeForm, StrategyID: strategy.StrategyID, FollowersLow: strategy.FollowersLow, FollowersUp: strategy.FollowersUp, RecruitNumber: strategy.RecruitNumber, Offer: strategy.Offer, ProjectID: *projectID, } recruitStrategys = append(recruitStrategys, recruitStrategy) } err = db.CreateRecruitStrategy(ctx, recruitStrategys) if err != nil { return nil, err } res := &http_model.CreateProjectData{ ProjectID: *projectID, } fmt.Printf("%+v", res) return res, nil }