package youngee_task_service import ( "fmt" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/os/glog" "github.com/gogf/gf/os/gtime" "github.com/gogf/gf/util/gconv" "math" "sort" "strconv" "strings" "youngmini_server/app/dao" "youngmini_server/app/model" "youngmini_server/app/model/youngee_talent_model" "youngmini_server/app/utils" ) func GetLocalLifeList(r *ghttp.Request) *TalentHttpResult { pageIndex := r.GetQueryInt("idx", -1) cntPerPage := r.GetQueryInt("cnt", -1) //组合筛选 platform := r.Get("platform", nil) //抖音、快手、红Book、B站、微博 taskForm := r.Get("task_form", nil) //任务形式,1-2分别代表线下探店,素材分发 contentType := r.Get("contenttype", nil) //图文形式、视频形式 createTime := r.Get("createTime", nil) //任务上线时间 //todo 城市参数? //Location := r.GetQueryString("city", "") //门店store所属城市, //排根据浏览量排序 viewOrder := r.GetQueryInt("pageViewOrder", -1) //根据哪个字段排序 //仅稿费”+“稿费+赠品"。这个在recruit表中,得到列表之后进行排序 feeForm := r.GetInt("feeform", 0) searchValue := r.Get("searchvalue") if pageIndex == -1 || cntPerPage == -1 || cntPerPage == 0 { return &TalentHttpResult{Code: -1, Msg: "参数错误"} } // 构造查询的条件 startId := pageIndex * cntPerPage // whereStr := fmt.Sprintf("(project_status >= %d and project_status <> %d and project_type = 1)", projectStatusRecruiting, projectStatusInvalid) whereStr := fmt.Sprintf("(task_status >= %d and local_type = 1)", projectStatusRecruiting) if taskForm != nil { whereStr += " and task_mode = " + taskForm.(string) } if createTime != nil { switch createTime.(string) { case "1": // 近7天 whereStr += " AND created_at >= (NOW() - INTERVAL 7 DAY)" case "2": // 近30天 whereStr += " AND created_at >= (NOW() - INTERVAL 30 DAY)" case "3": // 近90天 whereStr += " AND created_at >= (NOW() - INTERVAL 90 DAY)" default: // 无效的过滤条件,跳过 return &TalentHttpResult{ Code: -2, Msg: "无效的创建时间过滤条件", } } } if contentType != nil { whereStr += " and content_type = " + contentType.(string) } if platform != nil { whereStr += " and local_platform = " + platform.(string) } if searchValue != nil { whereStr += " and local_name like '%" + searchValue.(string) + "%'" } fmt.Println("whereStr:-----》 ", whereStr) // 查询所有project var localList = []youngee_talent_model.YounggeeLocalLifeInfo{} err := g.Model("younggee_local_life_info").Where(whereStr).Scan(&localList) if err != nil { return &TalentHttpResult{Code: -3, Msg: "查询数据库失败"} } // 判断请求页面是否超过最大页面 c, err := g.DB().Model("younggee_local_life_info").Fields("local_id").Where(whereStr).Count() if c <= 0 { return &TalentHttpResult{Code: -4, Msg: "has no fullproject", Data: nil} } maxPage := c / cntPerPage if c%cntPerPage > 0 { maxPage += 1 } if pageIndex+1 > maxPage { return &TalentHttpResult{Code: -5, Msg: "over max page"} } var localInfoList = youngee_talent_model.LocalInfoList{} err = g.DB().Model("younggee_local_life_info").WithAll().Where(whereStr). Order("task_status ASC,recruit_ddl DESC").Limit(startId, cntPerPage).Scan(&localInfoList.LocalInfos) if err != nil { return &TalentHttpResult{Code: -6, Msg: err.Error()} } // 遍历projectList for i, local := range localInfoList.LocalInfos { //处理浏览量 localViewKey := "local:view:" + gconv.String(local.LocalId) //redis中取浏览量 返回的是gvar.Var类型。 不存咋则为nil。经过viewCount.Int变成0 viewCount, err := g.Redis().DoVar("GET", localViewKey) if err != nil { glog.Error(err) return &TalentHttpResult{Code: 0, Msg: "Redis error"} } localInfoList.LocalInfos[i].WatchNum = viewCount.Int() // 根据 WatchedNum 从高到低排序 if viewOrder != -1 { sort.Slice(localInfoList.LocalInfos, func(i, j int) bool { return localInfoList.LocalInfos[i].WatchNum > localInfoList.LocalInfos[j].WatchNum }) } // 统计 RecruitStrategys 数组中的最低粉丝量和最高粉丝量 var minFollowers, maxFollowers int // 初始值可以设为极大/极小值 minFollowers = math.MaxInt32 maxFollowers = math.MinInt32 // 遍历 RecruitStrategys,找出最低粉丝量和最高粉丝量 for _, strategy := range local.RecruitStrategys { if strategy.FollowersLow < minFollowers { minFollowers = strategy.FollowersLow } if strategy.FollowersUp > maxFollowers { maxFollowers = strategy.FollowersUp } } // 如果需要,可以将这些值保存到 ProjectInfo 中,供后续使用 localInfoList.LocalInfos[i].MinFollowers = minFollowers localInfoList.LocalInfos[i].MaxFollowers = maxFollowers //稿费的展示情况 allFeeFormNoFee := true // 假设都是无费置换 var hasSelfPrice bool var minOffer, maxOffer int for _, strategy := range local.RecruitStrategys { if strategy.FeeForm != 1 { // 如果存在非无费置换的策略 allFeeFormNoFee = false } if strategy.FeeForm == 3 { // 存在自报价 hasSelfPrice = true } if strategy.FeeForm == 2 { // 一口价策略 if strategy.TOffer < minOffer || minOffer == 0 { minOffer = strategy.TOffer } if strategy.TOffer > maxOffer { maxOffer = strategy.TOffer } } } // 根据判断结果设置 IsShowOffer 和 HaveSelfPrice if allFeeFormNoFee { local.IsShowOffer = 2 // 不展示稿费 } else { local.IsShowOffer = 1 // 展示稿费 if hasSelfPrice { local.HaveSelfOffer = 1 //存在自报价 } else { local.HaveSelfOffer = 2 //不存在自报价 } localInfoList.LocalInfos[i].MaxOffer = maxOffer localInfoList.LocalInfos[i].MinOffer = minOffer } } localInfoList.MaxPage = maxPage var filteredLocalInfo []*youngee_talent_model.YounggeeLocalLifeInfo //筛选出“仅稿费”+“稿费+赠品”任务, //只要策略中有 无费置换 之外的fee_form就是有稿费,donate=1就是有赠品 if feeForm != 0 { for _, local := range localInfoList.LocalInfos { if local.Donate == 1 { filteredLocalInfo = append(filteredLocalInfo, local) continue // 如果满足条件1,直接跳过后续条件判断 } // 条件2:RecruitStrategy 中存在 fee_form 不等于 1 的数据 for _, strategy := range local.RecruitStrategys { if strategy.FeeForm != 1 { filteredLocalInfo = append(filteredLocalInfo, local) break // 找到符合条件的策略,跳出当前循环,避免重复添加 } } } localInfoList.LocalInfos = filteredLocalInfo } return &TalentHttpResult{Code: 0, Msg: "success", Data: localInfoList} } func GetLocalLifeDetail(r *ghttp.Request) *TalentHttpResult { tid, _ := utils.SessionTalentInfo.GetTalentIdFromSession(r) local_id := r.GetQueryString("local_id", "") s_local_id := r.GetQueryString("s_local_id", "") localInfoSupplier := youngee_talent_model.LocalInfoSupplier{} if s_local_id != "" { // 来自服务商 err := g.DB().Model("younggee_s_local__info").Where("s_local_id=?", s_local_id).Scan(&localInfoSupplier) if err != nil { fmt.Println("projectInfoSupplier err:", err.Error()) } } // Redis key loalViewKey := "local:view:" + local_id userViewedKey := "user:viewed:" + tid + ":" + local_id if local_id == "" { return &TalentHttpResult{Code: -2, Msg: "parse param error"} } //在redis中增加浏览量 // Check if the user has already viewed the product //DoVar方便进行类型转化 viewed, err := g.Redis().DoVar("GET", userViewedKey) if err != nil { glog.Error(err) return &TalentHttpResult{Code: 0, Msg: "Redis error"} } var talentCategory []*youngee_talent_model.YounggeeTalentCategory err = g.DB().Model("younggee_talent_category").Scan(&talentCategory) if err != nil { // 处理查询错误 return &TalentHttpResult{Code: -1, Msg: err.Error()} } // 创建一个数字到汉字的映射 categoryMap := make(map[string]string) for _, category := range talentCategory { categoryMap[fmt.Sprint(category.Id)] = category.Category } var LocalDetail *youngee_talent_model.LocalInfoDetail err = g.DB().Model("younggee_local_life_info").WithAll().Where("local_id", local_id).Scan(&LocalDetail) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } //填充收藏信息 collectionInfo := []youngee_talent_model.LocalCollection{} err = g.DB().Model("younggee_local_collect_info").Where("local_id=? and talent_id = ?", local_id, tid).Scan(&collectionInfo) if err != nil { return &TalentHttpResult{Code: -1, Msg: err.Error()} } //已被收藏 if len(collectionInfo) != 0 && collectionInfo[0].Deleted == 0 { //有数据 且 没取消收藏 LocalDetail.IsCollected = 1 } else { LocalDetail.IsCollected = 0 //没数据 或 有数据但取消了收藏 } // 将 TalentType 转换为逗号隔开的汉字字符串 talentTypes := strings.Split(LocalDetail.TalentType, ",") var result []string for _, t := range talentTypes { if name, ok := categoryMap[t]; ok { result = append(result, name) } } // 将结果拼接成逗号隔开的字符串 resultStr := strings.Join(result, ",") LocalDetail.TalentType = resultStr var younggeePhoto []*youngee_talent_model.YounggeeProductPhoto teamBuyingId := LocalDetail.TeamBuyingId if teamBuyingId != 0 { err := g.DB().Model("younggee_product_photo").Where("team_buying_id", teamBuyingId).Scan(&younggeePhoto) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } } StoreId := LocalDetail.StoreId if StoreId != 0 { err := g.DB().Model("younggee_product_photo").Where("store_id", StoreId).Scan(&younggeePhoto) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } fmt.Println("younggeePhoto", younggeePhoto) } fmt.Println("younggeePhoto", younggeePhoto) LocalDetail.YounggeeProductPhoto = younggeePhoto //违约 //if LocalDetail.AutoTaskId != 0 && LocalDetail.AutoDefaultId != 0 { // one, _ := g.DB().Model("info_auto_default_handle").Where("auto_default_id=?", LocalDetail.AutoDefaultId).One() // one2, _ := g.DB().Model("info_auto_task").Where("auto_task_id=?", LocalDetail.AutoTaskId).One() // LocalDetail.DraftDefault.BreakPecent = one["sketch_replace_not_upload"].Int() // LocalDetail.LinkDefault.BreakPecent = one["link_replace_not_upload"].Int() // LocalDetail.DataDefault.BreakPecent = one["data_replace_not_upload"].Int() // LocalDetail.DraftDefault.BreakTime = one2["draft_default"].Int() // LocalDetail.DraftDefault.BreakTime = one2["link_breach"].Int() // LocalDetail.DraftDefault.BreakTime = one2["case_close_default"].Int() //} if viewed.IsNil() { // User hasn't viewed this product yet, increase the view count _, err = g.Redis().Do("INCR", loalViewKey) if err != nil { glog.Error(err) return &TalentHttpResult{Code: 0, Msg: "Redis error"} } // Mark the product as viewed by this user _, err = g.Redis().Do("SET", userViewedKey, true) if err != nil { glog.Error(err) return &TalentHttpResult{Code: 0, Msg: "Redis error"} } } viewNum, err := g.Redis().DoVar("GET", loalViewKey) if err != nil { fmt.Println("获取浏览量失败") } LocalDetail.WatchedNum = viewNum.Int() //浏览历史 currentDate := gtime.Now().Format("Ymd") // 设计 Redis Key redisBrowseKey := fmt.Sprintf("browseLocal:%s:%s", currentDate, tid) fmt.Println("redis浏览记录的key为——————————", redisBrowseKey) _, err = g.Redis().Do("SADD", redisBrowseKey, local_id) if err != nil { return &TalentHttpResult{Code: 0, Msg: "Redis 存浏览历史数据失败"} } // 设置 Key 的过期时间为 7 天 _, err = g.Redis().Do("EXPIRE", redisBrowseKey, 7*24*60*60) // 7 天的秒数 if err != nil { return &TalentHttpResult{Code: 0, Msg: "Redis 设置过期时间失败"} } if LocalDetail.EnterpriseId != "" { //project来自商家 var enterprise *youngee_talent_model.Enterprise err = g.DB().Model("enterprise").WithAll().Where("enterprise_id", LocalDetail.EnterpriseId).Scan(&enterprise) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } LocalDetail.Enterprise = enterprise } if s_local_id != "" { // 来自服务商 var younggeeSupplier *youngee_talent_model.YounggeeSupplier err = g.DB().Model("younggee_supplier").WithAll().Where("supplier_id", localInfoSupplier.SupplierID).Scan(&younggeeSupplier) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } LocalDetail.YounggeeSupplier = younggeeSupplier LocalDetail.LocalInfoSupplier = &localInfoSupplier } return &TalentHttpResult{Code: 0, Msg: "success", Data: LocalDetail} } // 查询所有任务 func GetLocalTaskBriefList(r *ghttp.Request) *TalentHttpResult { tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r) if err != nil { return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"} } taskType := r.GetInt("task_type", 0) //任务类型 if taskType == 0 { return &TalentHttpResult{Code: -1, Msg: "task_type is nil"} } // 构造查询条件tid和taskType whereStr := fmt.Sprintf("talent_id = %s and local_type = %d", tid, taskType) // 获取任务列表 var taskList []*youngee_talent_model.YoungeeLocalTaskInfo //此达人下,所有快手账号的任务都展示 err = g.Model("youngee_local_task_info").Where(whereStr).Scan(&taskList) if err != nil { return &TalentHttpResult{Code: -1, Msg: "Get task list failed"} } //后续根据 PlatformId 快速查找平台信息。platformMap的key是PlatformId,value是平台具体描述 platformMap := make(map[string]model.InfoThirdPlatform) platformInfo := []*model.InfoThirdPlatform{} if len(taskList) != 0 { err := g.Model(dao.InfoThirdPlatform.Table).Scan(&platformInfo) if err != nil { return &TalentHttpResult{Code: -1, Msg: "Get platform failed"} } for i, _ := range platformInfo { platformMap[strconv.Itoa(platformInfo[i].PlatformId)] = *platformInfo[i] } } //为每个任务根据项目id查询项目名称和主图 //taskBriefList存了各个阶段的tasklist taskBriefList := youngee_talent_model.LocalTaskInfoBriefList{} for _, v := range taskList { //taskList含所有任务 //获取具体的招募策略,taskInfo中 var localDetail *youngee_talent_model.LocalInfoDetail err := g.Model("younggee_local_life_info").WithAll().Where("local_id = ?", v.LocalId).Scan(&localDetail) if err != nil { return &TalentHttpResult{Code: -1, Msg: "Get fullproject info failed"} } var account *youngee_talent_model.KuaishouUserInfo fmt.Println("openid---->", v.OpenId) err = g.Model("platform_kuaishou_user_info").Where("platform_id = ? and talent_id = ? and open_id = ?", localDetail.LocalPlatform, tid, v.OpenId).Scan(&account) //拿快手平台验证是否过期 //expired := youngee_talent_service.CheckKuaishouTokenExp(account.OpenId) //account.Expired = expired if err != nil { return &TalentHttpResult{Code: -1, Msg: "Get account info failed"} } //taskInfoBrief含需要展示在页面的内容,被加入各阶段List taskInfoBrief := &youngee_talent_model.LocalTaskInfoBrief{ TaskId: v.TaskId, PlatformIconUrl: platformMap[strconv.Itoa(localDetail.PlatformInfo.PlatformId)].PlatformIcon, //PlatformName: platformMap[projectInfo[dao.ProjectInfo.Columns.ProjectPlatform].String()].PlatformName, //PlatformNickName: account[dao.YoungeePlatformAccountInfo.Columns.PlatformNickname].String(), ProjectName: localDetail.LocalName, //ProductPhotoSnap: localDetail.ProductPhotoSnap, TaskStatus: v.TaskStatus, TaskStage: v.TaskStage, LinkStatus: v.LinkStatus, DataStatus: v.DataStatus, //ScriptStatus: v.ScriptStatus, SketchStatus: v.SketchStatus, TaskReward: v.TaskReward, BreakRate: v.ScriptBreakRate + v.SketchBreakRate + v.LinkBreakRate + v.DataBreakRate, CurBreakAt: v.CurBreakAt, FeeForm: v.FeeForm, LocalDetail: localDetail, TaskInfo: v, AccountInfo: account, //含是否过期,粉丝数,作品数目 } taskBriefList.AllTaskInfoList = append(taskBriefList.AllTaskInfoList, taskInfoBrief) //1:已报名, 2:申请成功, 3:申请失败, 4:待预约探店, 5:预约确认中 6:预约成功 , 7:待传探底图片, 8:脚本待审, 9:待传初稿, 10:初稿待审, //11:待传链接, 12:链接待审, 13:待传数据, 14:数据待审, 15:已结案, 16:解约, 17:终止合作(过渡态)', if v.TaskStage <= 2 { taskBriefList.SignUpTaskInfoList = append(taskBriefList.SignUpTaskInfoList, taskInfoBrief) } else if v.TaskStage == 4 { //如果是线下探店 taskBriefList.WaitBookList = append(taskBriefList.WaitBookList, taskInfoBrief) } else if v.TaskStage <= 14 && v.TaskStage >= 5 { taskBriefList.GoingOnTaskInfoList = append(taskBriefList.GoingOnTaskInfoList, taskInfoBrief) } else if v.TaskStage == 15 { taskBriefList.WaitToPayInfoList = append(taskBriefList.WaitToPayInfoList, taskInfoBrief) } else { taskBriefList.CompletedTaskInfoList = append(taskBriefList.CompletedTaskInfoList, taskInfoBrief) } } return &TalentHttpResult{Code: 0, Msg: "success", Data: taskBriefList} } func GetLocalTaskDetail(r *ghttp.Request) *TalentHttpResult { taskId := r.Get("task_id", "") if taskId == "" { return &TalentHttpResult{Code: -1, Msg: "task_id is empty"} } var task *youngee_talent_model.YoungeeLocalTaskInfo err := g.Model("youngee_local_task_info").Where("task_id = ?", taskId).Scan(&task) if err != nil { return &TalentHttpResult{Code: -1, Msg: "Get task info failed"} } var localDetail *youngee_talent_model.LocalInfoDetail err = g.Model(youngee_talent_model.LocalInfoDetail{}).WithAll().Where("local_id", task.LocalId).Scan(&localDetail) if err != nil { return &TalentHttpResult{Code: -3, Msg: "data query failed"} } var productPhoto *youngee_talent_model.YounggeeProductPhoto err = g.Model("younggee_product_photo").Where("store_id = ? and symbol = 1", localDetail.StoreId).Scan(&productPhoto) if err != nil { return &TalentHttpResult{Code: -3, Msg: "data query failed"} } var withdrawStatus = 1 var taskIncome *model.YounggeeTalentIncome err = g.Model(dao.YounggeeTalentIncome.Table).Where("task_id = ? and income_type = 1", taskId).Scan(&taskIncome) if err != nil { return &TalentHttpResult{Code: -3, Msg: "Get task income detail failed."} } if taskIncome != nil { withdrawStatus = taskIncome.WithdrawStatus + 1 } taskDetail := &youngee_talent_model.LocalTaskDetail{} if localDetail.LocalType == 1 { var strategy *youngee_talent_model.RecruitStrategy err = g.DB().Model("recruit_strategy").Where("project_id = ? and strategy_id = ?", task.LocalId, task.StrategyId).Scan(&strategy) if err != nil { return &TalentHttpResult{Code: -3, Msg: "data query failed"} } taskDetail = &youngee_talent_model.LocalTaskDetail{ TaskInfo: task, LocalDetail: localDetail, ProductPhoto: productPhoto, //首页图片 Strategy: strategy, WithdrawStatus: withdrawStatus, } } else { taskDetail = &youngee_talent_model.LocalTaskDetail{ TaskInfo: task, LocalDetail: localDetail, ProductPhoto: productPhoto, WithdrawStatus: withdrawStatus, } } return &TalentHttpResult{Code: 0, Msg: "success", Data: taskDetail} }