package service import ( "errors" "github.com/sirupsen/logrus" "reflect" "time" "youngee_b_api/app/dao" "youngee_b_api/app/entity" "youngee_b_api/app/util" "youngee_b_api/app/vo" ) type LocalLifeService struct{} // 创建本地生活任务 func (s LocalLifeService) CreateLocalLife(param *vo.LocalCreateParam) (*string, error) { // a) 生成本地生活项目id localId := string(util.GenerateUUID(10)) // b) 查找关联门店信息 product, err := dao.StoreDao{}.GetStoreByID(param.StoreId) if err != nil { return nil, err } if product == nil { return nil, errors.New("未找到关联门店") } // c)创建种草任务 var operatorType int64 if param.SubAccountId == 0 { operatorType = 1 } else { operatorType = 2 } // 获取定时任务配置id infoAutoTask := entity.InfoAutoTask{} infoAutoTask = dao.InfoAutoTaskDao{}.GetAutoTaskLast(param.EnterpriseId) infoAutoDefault := entity.InfoAutoDefault{} infoAutoDefault = dao.InfoAutoDefaultDao{}.GetAutoDefaultLast(param.EnterpriseId) t := time.Now() newLocalLife := entity.LocalLifeInfo{ EnterpriseID: param.EnterpriseId, SubAccountID: param.SubAccountId, OperatorType: operatorType, TaskStatus: 1, LocalID: localId, LocalType: param.LocalType, LocalPlatform: param.Platform, StoreID: param.StoreId, StoreRelatedAt: time.Now(), PromoteBody: param.PromoteBody, Donate: param.Donate, TeamBuyingId: param.TeamBuyingId, TeamBuyingRelatedAt: time.Now(), CreatedAt: t, AutoTaskID: infoAutoTask.AutoTaskID, AutoDefaultID: infoAutoDefault.AutoDefaultID, } if param.LocalType == 1 { newLocalLife.ServiceChargeRate = param.ServiceChargeRate } err = dao.LocalLifeDao{}.CreateLocalLife(newLocalLife) if err != nil { return nil, err } return &localId, nil } // 更新公开本地生活任务(招募要求、执行要求) func (s LocalLifeService) UpdateLocal(localUpdateParam *vo.LocalUpdateParam) (*string, error) { // 1. 检查该项目id有无本地任务 localID := localUpdateParam.LocalID localLife, err := dao.LocalLifeDao{}.GetLocalById(localID) if err != nil { return nil, err } if localLife == nil { return nil, errors.New("本地生活项目不存在") } println("更新公开本地生活任务的招募策略") // 更新公开种草任务的招募策略 var totalRecruitNum int64 if localUpdateParam.RecruitStrategys != nil { // 1. 删除已有的招募策略 err = dao.RecruitStrategyDao{}.DeleteRecruitStrategyByProjectID(localUpdateParam.LocalID) if err != nil { return nil, err } // 2. 接收并创建新的招募策略 if len(localUpdateParam.RecruitStrategys) != 0 { var recruits []entity.RecruitStrategy for _, strategy := range localUpdateParam.RecruitStrategys { recruitStrategy := entity.RecruitStrategy{ FeeForm: strategy.FeeForm, StrategyID: strategy.StrategyID, FollowersLow: strategy.FollowersLow, FollowersUp: strategy.FollowersUp, RecruitNumber: strategy.RecruitNumber, ProjectID: localLife.LocalID, StrategyType: 1, ServiceRate: localLife.ServiceChargeRate, } totalRecruitNum += strategy.RecruitNumber if strategy.FeeForm == 2 { recruitStrategy.Offer = strategy.Offer recruitStrategy.ServiceCharge = strategy.Offer * localUpdateParam.ServiceChargeRate recruitStrategy.TOffer = strategy.Offer * (1 - localUpdateParam.ServiceChargeRate) } recruits = append(recruits, recruitStrategy) } err = dao.RecruitStrategyDao{}.CreateRecruitStrategy(recruits) if err != nil { return nil, err } } } // 2. 数据准备 //// a) 查找关联商品信息 //product, err := dao.ProductDAO{}.GetProductByID(project.ProductID) //if err != nil { // return nil, err //} //productPhotos, err := dao.ProductPhotoDAO{}.GetProductPhotoByProductID(project.ProductID) //productInfoToJson, _ := json.Marshal(product) //productPhotosToJson, _ := json.Marshal(productPhotos) // d) 任务截止时间 recruitDdl := time.Time{} //赋零值 recruitDdl, _ = time.ParseInLocation("2006-01-02 15:04:05", localUpdateParam.RecruitDdl, time.Local) // f) 更新选品状态 if localUpdateParam.LocalStatus != 2 && localUpdateParam.LocalStatus != 8 { localUpdateParam.LocalStatus = 1 } t := time.Now() updateLocalLife := entity.LocalLifeInfo{ StoreID: localUpdateParam.StoreId, TeamBuyingId: localUpdateParam.TeamBuyingId, ServiceChargeRate: localUpdateParam.ServiceChargeRate, PromoteBody: localUpdateParam.PromoteBody, Donate: localUpdateParam.Donate, TaskStatus: localUpdateParam.LocalStatus, LocalName: localUpdateParam.LocalName, TalentType: localUpdateParam.TalentType, RecruitDdl: recruitDdl, TaskForm: localUpdateParam.TaskForm, ContentType: localUpdateParam.ContentType, TaskDetail: localUpdateParam.TaskDetail, UpdatedAt: t, } if localUpdateParam.LocalStatus == 2 { updateLocalLife.SubmitAt = t } // 合并传入参数和数据表中原记录,若传入参数字段值为空,则将字段赋值为原记录中值 result := util.MergeStructValue(&updateLocalLife, localLife) // 利用反射机制将interface类型转换为结构体类型 v := reflect.ValueOf(&result).Elem() if v.Kind() == reflect.Struct { updateLocalLife = v.Interface().(entity.LocalLifeInfo) //fmt.Println(p) } // c) 计算预估成本(如果有) /* var estimatedCost float64 if conv.MustInt(updateSelection.TaskMode, 0) == 1 { estimatedCost = conv.MustFloat64(updateSelection.TaskReward, 0) * conv.MustFloat64(updateSelection.SampleNum, 0) } estimatedCostToString, _ := conv.String(estimatedCost) updateSelection.EstimatedCost = estimatedCostToString */ // 3. 更新选品 err = dao.LocalLifeDao{}.UpdateLocal(updateLocalLife) if err != nil { return nil, err } // 4. 更新选品brief和示例(本地生活任务补充信息) if localUpdateParam.LocalBrief != nil { // 删除已有brief err = dao.ProjectBriefDao{}.DeleteSecBriefBySelectionId(localLife.LocalID) if err != nil { return nil, err } // 插入新的brief for _, v := range localUpdateParam.LocalBrief { brief := entity.ProjectBrief{ ProjectID: localLife.LocalID, FileUid: v.FileUid, FileName: v.Name, FileUrl: v.FileUrl, CreatedAt: time.Now(), } err = dao.ProjectBriefDao{}.CreateProjectBrief(brief) if err != nil { return nil, err } } } if localUpdateParam.LocalMaterial != nil { // 删除已有示例 err = dao.ProjectMaterialDao{}.DeleteProjectMaterialByProjectId(localLife.LocalID) if err != nil { return nil, err } // 插入新的示例 for _, v := range localUpdateParam.LocalMaterial { projectMaterial := entity.ProjectMaterial{ ProjectID: localLife.LocalID, FileUid: v.FileUid, FileName: v.Name, FileUrl: v.FileUrl, CreatedAt: time.Now(), } err = dao.ProjectMaterialDao{}.CreateProjectMaterial(projectMaterial) if err != nil { return nil, err } } } return &updateLocalLife.LocalID, nil } // 更新定向本地生活任务(招募要求、执行要求) func (s LocalLifeService) UpdateLocalTarget(localUpdateParam *vo.LocalUpdateParam) (*string, error) { // 1. 检查该项目id有无本地任务 localID := localUpdateParam.LocalID localLife, err := dao.LocalLifeDao{}.GetLocalById(localID) if err != nil { return nil, err } if localLife == nil { return nil, errors.New("本地生活项目不存在") } println("更新定向本地生活任务的招募策略") // 更新公开种草任务的招募策略 var totalRecruitNum int64 if localUpdateParam.RecruitStrategys != nil { // 1. 删除已有的招募策略 err = dao.RecruitStrategyDao{}.DeleteRecruitStrategyByProjectID(localUpdateParam.LocalID) if err != nil { return nil, err } // 2. 接收并创建新的招募策略 if len(localUpdateParam.RecruitStrategys) != 0 { var recruits []entity.RecruitStrategy for _, strategy := range localUpdateParam.RecruitStrategys { recruitStrategy := entity.RecruitStrategy{ FeeForm: strategy.FeeForm, StrategyID: strategy.StrategyID, FollowersLow: strategy.FollowersLow, FollowersUp: strategy.FollowersUp, RecruitNumber: strategy.RecruitNumber, ProjectID: localLife.LocalID, StrategyType: 1, ServiceRate: localLife.ServiceChargeRate, } totalRecruitNum += strategy.RecruitNumber if strategy.FeeForm == 2 { recruitStrategy.Offer = strategy.Offer // 报价 } recruits = append(recruits, recruitStrategy) } err = dao.RecruitStrategyDao{}.CreateRecruitStrategy(recruits) if err != nil { return nil, err } } } // 2. 数据准备 // a) 查找关联商品信息 //product, err := dao.ProductDAO{}.GetProductByID(project.ProductID) //if err != nil { // return nil, err //} //productPhotos, err := dao.ProductPhotoDAO{}.GetProductPhotoByProductID(project.ProductID) //productInfoToJson, _ := json.Marshal(product) //productPhotosToJson, _ := json.Marshal(productPhotos) // d) 任务截止时间 recruitDdl := time.Time{} //赋零值 recruitDdl, _ = time.ParseInLocation("2006-01-02 15:04:05", localUpdateParam.RecruitDdl, time.Local) // f) 更新选品状态 if localUpdateParam.LocalStatus != 2 && localUpdateParam.LocalStatus != 8 { localUpdateParam.LocalStatus = 1 } t := time.Now() updateLocalLife := entity.LocalLifeInfo{ StoreID: localUpdateParam.StoreId, TeamBuyingId: localUpdateParam.TeamBuyingId, ServiceChargeRate: localUpdateParam.ServiceChargeRate, PromoteBody: localUpdateParam.PromoteBody, Donate: localUpdateParam.Donate, TaskStatus: localUpdateParam.LocalStatus, LocalName: localUpdateParam.LocalName, TalentType: localUpdateParam.TalentType, RecruitDdl: recruitDdl, TaskForm: localUpdateParam.TaskForm, ContentType: localUpdateParam.ContentType, TaskDetail: localUpdateParam.TaskDetail, UpdatedAt: t, Tools: localUpdateParam.Tools, } if localUpdateParam.LocalStatus == 2 { updateLocalLife.SubmitAt = t } // 合并传入参数和数据表中原记录,若传入参数字段值为空,则将字段赋值为原记录中值 result := util.MergeStructValue(&updateLocalLife, localLife) // 利用反射机制将interface类型转换为结构体类型 v := reflect.ValueOf(&result).Elem() if v.Kind() == reflect.Struct { updateLocalLife = v.Interface().(entity.LocalLifeInfo) //fmt.Println(p) } // c) 计算预估成本(如果有) /* var estimatedCost float64 if conv.MustInt(updateSelection.TaskMode, 0) == 1 { estimatedCost = conv.MustFloat64(updateSelection.TaskReward, 0) * conv.MustFloat64(updateSelection.SampleNum, 0) } estimatedCostToString, _ := conv.String(estimatedCost) updateSelection.EstimatedCost = estimatedCostToString */ // 3. 更新选品 err = dao.LocalLifeDao{}.UpdateLocal(updateLocalLife) if err != nil { return nil, err } // 4. 更新选品brief和示例(本地生活任务补充信息) if localUpdateParam.LocalBrief != nil { // 删除已有brief err = dao.ProjectBriefDao{}.DeleteSecBriefBySelectionId(localLife.LocalID) if err != nil { return nil, err } // 插入新的brief for _, v := range localUpdateParam.LocalBrief { brief := entity.ProjectBrief{ ProjectID: localLife.LocalID, FileUid: v.FileUid, FileName: v.Name, FileUrl: v.FileUrl, CreatedAt: time.Now(), } err = dao.ProjectBriefDao{}.CreateProjectBrief(brief) if err != nil { return nil, err } } } if localUpdateParam.LocalMaterial != nil { // 删除已有示例 err = dao.ProjectMaterialDao{}.DeleteProjectMaterialByProjectId(localLife.LocalID) if err != nil { return nil, err } // 插入新的示例 for _, v := range localUpdateParam.LocalMaterial { projectMaterial := entity.ProjectMaterial{ ProjectID: localLife.LocalID, FileUid: v.FileUid, FileName: v.Name, FileUrl: v.FileUrl, CreatedAt: time.Now(), } err = dao.ProjectMaterialDao{}.CreateProjectMaterial(projectMaterial) if err != nil { return nil, err } } } return &updateLocalLife.LocalID, nil } // 本地生活任务预览 func (s LocalLifeService) GetLocalLifeDetail(localId string) (*vo.ReLocalDetail, error) { reLocalDetail := vo.ReLocalDetail{} localLife, err := dao.LocalLifeDao{}.GetLocalById(localId) if err != nil { logrus.Errorf("[localLifeDB service] call GetLocalById error,err:%+v", err) return nil, err } // 系统信息 reLocalDetail.LocalId = localId reLocalDetail.LocalStatus = localLife.TaskStatus reLocalDetail.LocalPlatform = localLife.LocalPlatform reLocalDetail.CreatedAt = localLife.CreatedAt.Format("2006-01-02 15:04:05") reLocalDetail.EstimatedCost = localLife.EstimatedCost // 预估成本 reLocalDetail.ServiceChargeRate = localLife.ServiceChargeRate var creatorName, phone string if localLife.OperatorType == 1 && localLife.SubAccountID == 0 { enterprise, err := dao.EnterpriseDao{}.GetEnterprise(localLife.EnterpriseID) if err == nil && enterprise != nil { creatorName = enterprise.BusinessName phone, err = dao.UserDao{}.GetPhoneByUserId(enterprise.UserId) } } else if localLife.OperatorType == 1 && localLife.SubAccountID != 0 { subAccount, err := dao.SubAccountDao{}.GetSubAccount(localLife.SubAccountID) if err == nil && subAccount != nil { creatorName = subAccount.SubAccountName phone, err = dao.UserDao{}.GetPhoneByUserId(subAccount.UserId) } } reLocalDetail.CreatorName = creatorName reLocalDetail.Phone = phone // 关联主体 var reStore vo.ReStorePreview store, err := dao.StoreDao{}.GetStoreByID(localLife.StoreID) if err == nil { photoUrl, e := dao.ProductPhotoDAO{}.GetMainPhotoByStoreID(store.StoreID) if e != nil { photoUrl = "" } reStore = vo.ReStorePreview{ StoreID: store.StoreID, StoreName: store.StoreName, StoreLocation: store.StoreLocation, StoreCategory: store.StoreCategory, TeamNum: store.TeamNum, StoreDetail: store.StoreDetail, CreatedAt: store.CreatedAt.Format("2006-01-02 15:04:05"), PhotoUrl: photoUrl, } } reLocalDetail.StoreInfo = &reStore var reTeamBuying vo.ReTeamBuyingPreview teamBuying, err := dao.TeamBuyingDao{}.GetTeamBuyingByID(localLife.TeamBuyingId) if err == nil { photoUrl, e := dao.ProductPhotoDAO{}.GetMainPhotoByTeamBuyingID(teamBuying.TeamBuyingID) if e != nil { photoUrl = "" } reTeamBuying = vo.ReTeamBuyingPreview{ TeamBuyingID: teamBuying.TeamBuyingID, TeamBuyingName: teamBuying.TeamBuyingName, TeamBuyingPrice: teamBuying.TeamBuyingPrice, TeamBuyingCategory: teamBuying.TeamBuyingCategory, TeamBuyingDetail: teamBuying.TeamBuyingDetail, CreatedAt: teamBuying.CreatedAt.Format("2006-01-02 15:04:05"), PhotoUrl: photoUrl, } } reLocalDetail.TeamBuyingInfo = &reTeamBuying reLocalDetail.PromoteBody = localLife.PromoteBody reLocalDetail.Donate = localLife.Donate // 招募要求 reLocalDetail.TalentType = localLife.TalentType reLocalDetail.RecruitDdl = localLife.RecruitDdl.Format("2006-01-02 15:04:05") var recruitStrategysPreviews []*vo.LocalRecruitStrategy recruitStrategys, err := dao.RecruitStrategyDao{}.GetRecruitStrategyByProjectId(localId) if err != nil { logrus.Errorf("[localLifeDB service] call GetRecruitStrategy error,err:%+v", err) return nil, err } for _, recruitStrategy := range recruitStrategys { recruitStrategysPreview := &vo.LocalRecruitStrategy{ StrategyId: recruitStrategy.StrategyID, FeeForm: recruitStrategy.FeeForm, FollowersLow: recruitStrategy.FollowersLow, FollowersUp: recruitStrategy.FollowersUp, RecruitNumber: recruitStrategy.RecruitNumber, Offer: recruitStrategy.Offer, TOffer: recruitStrategy.TOffer, ServiceCharge: recruitStrategy.ServiceCharge, SelectedNumber: recruitStrategy.SelectedNumber, TotalOffer: recruitStrategy.TotalOffer, } recruitStrategysPreviews = append(recruitStrategysPreviews, recruitStrategysPreview) } reLocalDetail.RecruitStrategys = recruitStrategysPreviews // 执行要求 reLocalDetail.TaskForm = localLife.TaskForm reLocalDetail.ContentType = localLife.ContentType reLocalDetail.TaskDetail = localLife.TaskDetail taskBriefInfos, err := dao.ProjectBriefDao{}.GetProjectBriefInfo(localId) if err != nil { logrus.Errorf("[localLifeDB service] call GetProjectBriefInfo error,err:%+v", err) return nil, err } taskMaterials, err := dao.ProjectMaterialDao{}.GetProjectMaterialInfo(localId) if err != nil { logrus.Errorf("[localLifeDB service] call GetprojectMaterialInfo error,err:%+v", err) return nil, err } reLocalDetail.TaskBriefs = taskBriefInfos reLocalDetail.TaskMaterials = taskMaterials reLocalDetail.Tools = localLife.Tools return &reLocalDetail, nil } // 本地生活提交审核 func (s LocalLifeService) LocalLifeToReview(localUpdateParam *vo.LocalUpdateParam) (*string, error) { localId := localUpdateParam.LocalID t := time.Now() updateLocal := entity.LocalLifeInfo{ LocalID: localId, TaskStatus: 2, UpdatedAt: t, } err := dao.LocalLifeDao{}.UpdateLocal(updateLocal) if err != nil { return nil, err } return &localId, nil } // 种草任务列表 func (s LocalLifeService) GetLocalLifeTaskList(param *vo.LocalSearchParam) (vo.ResultVO, error) { if param.Page == 0 { param.Page = 1 } if param.PageSize == 0 { param.PageSize = 10 } var result vo.ResultVO reLocalTaskPreviews, total, err := (&dao.LocalLifeDao{}).GetLocalPreviews(param) if err != nil { return result, err } for i := range reLocalTaskPreviews { var creatorName string var storeName string var storeLocation string var mainImage string if reLocalTaskPreviews[i].SubAccountId == 0 { enterprise, err := dao.EnterpriseDao{}.GetEnterprise(reLocalTaskPreviews[i].EnterpriseId) if err == nil && enterprise != nil { creatorName = enterprise.BusinessName } } else { subAccount, err := dao.SubAccountDao{}.GetSubAccount(reLocalTaskPreviews[i].SubAccountId) if err == nil && subAccount != nil { creatorName = subAccount.SubAccountName } } store, err := dao.StoreDao{}.GetStoreByID(reLocalTaskPreviews[i].StoreId) if err == nil && store != nil { storeName = store.StoreName storeLocation = store.StoreLocation } mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByStoreID(reLocalTaskPreviews[i].StoreId) reLocalTaskPreviews[i].CreatorName = creatorName reLocalTaskPreviews[i].StoreName = storeName reLocalTaskPreviews[i].StoreLocation = storeLocation reLocalTaskPreviews[i].MainImage = mainImage } result = vo.ResultVO{ Page: param.Page, PageSize: param.PageSize, Total: total, Data: reLocalTaskPreviews, } return result, nil } // 删除种草任务 func (s LocalLifeService) DeleteLocalLife(localId string) (*string, error) { res, err := dao.LocalLifeDao{}.DeleteLocalLife(localId) if err != nil { logrus.Errorf("[localLifeDB service] call DeleteLocalLife error,err:%+v", err) return res, err } return res, nil }