package youngee_task_service import ( "fmt" "github.com/gogf/gf/os/glog" "github.com/gogf/gf/os/gtime" "github.com/gogf/gf/util/gconv" "reflect" "strconv" "strings" "youngmini_server/app/dao" "youngmini_server/app/model" "youngmini_server/app/model/youngee_talent_model" "youngmini_server/app/service/youngee_talent_service" "youngmini_server/app/utils" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ghttp" "github.com/wxnacy/wgo/arrays" ) type projectStatus int const ( projectStatusCreating = iota + 1 projectStatusReviewing projectStatusReviewed projectStatusRecruiting projectStatusRecruited projectStatusPaying projectStatusPaid projectStatusInvalid projectStatusInProgress projectStatusClosed ) // 获取项目信息列表service func GetProjectInfoList(r *ghttp.Request) *TalentHttpResult { fmt.Println("进入种草主页") pageIndex := r.GetQueryInt("idx", -1) cntPerPage := r.GetQueryInt("cnt", -1) platform := r.Get("platform") projectForm := r.Get("projectform") feeForm := r.Get("feeform") searchValue := r.Get("searchvalue") if pageIndex == -1 || cntPerPage == -1 || cntPerPage == 0 { return &TalentHttpResult{Code: -1, Msg: "参数错误"} } // 如果有稿费形式的过滤条件,则将过滤条件保存于taskModeList var feeFormList []interface{} if feeForm != nil { if reflect.TypeOf(feeForm).Kind() != reflect.Slice { return &TalentHttpResult{Code: -2, Msg: "搜索条件稿费形式错误"} } feeFormList = make([]interface{}, 0) feeFormList = feeForm.([]interface{}) } // 如果有任务形式的过滤条件,则将过滤条件保存于taskModeList var projectFormList []interface{} if projectForm != nil { if reflect.TypeOf(projectForm).Kind() != reflect.Slice { return &TalentHttpResult{Code: -2, Msg: "搜索条件任务形式错误"} } projectFormList = make([]interface{}, 0) projectFormList = projectForm.([]interface{}) } // 如果有平台的过滤条件,则将平台列表保存于platformList var platformList []interface{} if platform != nil { if reflect.TypeOf(platform).Kind() != reflect.Slice { return &TalentHttpResult{Code: -2, Msg: "搜索条件平台类型错误"} } platformList = make([]interface{}, 0) platformList = platform.([]interface{}) } // 构造查询的条件 startId := pageIndex * cntPerPage // whereStr := fmt.Sprintf("(project_status >= %d and project_status <> %d and project_type = 1)", projectStatusRecruiting, projectStatusInvalid) whereStr := fmt.Sprintf("(project_status >= %d and fail_reason <> 2 and project_type = 1)", projectStatusRecruiting) if platformList != nil { whereStr = whereStr + " and project_platform in (" for _, v := range platformList { whereStr += v.(string) + ", " } whereStr = whereStr[0 : len(whereStr)-2] whereStr += ")" } if projectFormList != nil { whereStr += " and project_form in (" for _, v := range projectFormList { whereStr += v.(string) + ", " } whereStr = whereStr[0 : len(whereStr)-2] whereStr += ")" } if searchValue != nil { whereStr += " and project_name like '%" + searchValue.(string) + "%'" } // 查询所有project var projectList = []model.ProjectInfo{} var projectIdList []string err := g.Model(dao.ProjectInfo.Table).Where(whereStr).Scan(&projectList) if err != nil { return &TalentHttpResult{Code: -3, Msg: "查询数据库失败"} } fmt.Println("searchValue:", searchValue) fmt.Println("feeFormList:", feeFormList) // 筛选出满足稿费形式条件的项目id列表 for _, v := range projectList { string_slice := strings.Split(v.FeeForm, ",") // fmt.Println("str:", string_slice) tmp := 0 for _, w := range feeFormList { i := arrays.ContainsString(string_slice, w.(string)) if i < 0 { tmp = 0 break } tmp = 1 } if tmp == 1 { projectIdList = append(projectIdList, v.ProjectId) } } if projectIdList != nil { whereStr += " and project_id in (" for _, v := range projectIdList { whereStr += v + ", " } whereStr = whereStr[0 : len(whereStr)-2] whereStr += ")" } else if feeForm != nil { return &TalentHttpResult{Code: -4, Msg: "has no fullproject", Data: nil} } fmt.Println("whereStr: ", whereStr) // 判断请求页面是否超过最大页面 c, err := g.DB().Model(dao.ProjectInfo.Table).Fields("project_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 projectInfoList = youngee_talent_model.ProjectInfoList{} err = g.DB().Model("project_info").WithAll().Where(whereStr). Order("project_status ASC,recruit_ddl DESC").Limit(startId, cntPerPage).Scan(&projectInfoList.ProjectInfos) if err != nil { return &TalentHttpResult{Code: -6, Msg: "查询数据库失败"} } // 遍历projectList for i, project := range projectInfoList.ProjectInfos { fmt.Println("project.EnterpriseId---", project.EnterpriseId) //处理浏览量 projectViewKey := "project:view:" + gconv.String(project.ProjectId) //redis中取浏览量 返回的是gvar.Var类型。 不存咋则为nil。经过viewCount.Int变成0 viewCount, err := g.Redis().DoVar("GET", projectViewKey) if err != nil { glog.Error(err) return &TalentHttpResult{Code: 0, Msg: "Redis error"} } projectInfoList.ProjectInfos[i].WatchedNum = viewCount.Int() // 如果 enterprise_id 有值,查询 Enterprise 信息并关联,服务商的任务不会出现在首页 //if project.EnterpriseId != "" { // var enterprise *youngee_talent_model.Enterprise // err := g.DB().Model("enterprise").Where("enterprise_id", project.EnterpriseId).Scan(&enterprise) // if err != nil { // fmt.Println("查询失败") // continue // 如果查询失败,跳过当前 project 继续处理下一个 // } // // fmt.Println("查询结果---", enterprise) // // // 将查询到的 Enterprise 赋值给 projectInfo 的 Enterprise 字段 // projectInfoList.ProjectInfos[i].Enterprise = enterprise //} } projectInfoList.MaxPage = maxPage return &TalentHttpResult{Code: 0, Msg: "success", Data: projectInfoList} } // 获取单个项目详情service func GetProjectDetail(r *ghttp.Request) *TalentHttpResult { tid, _ := utils.SessionTalentInfo.GetTalentIdFromSession(r) pid := r.GetQueryInt("project_id", 0) //通过首页进入的详情页,获取服务商id,younggee_supplier表 enterprise_id := r.GetQueryString("enterprise_id", "") //通过扫服务商码进入的详情页,获取服务商id,younggee_supplier表 supplier_id := r.GetQueryString("supplier_id", "") fmt.Println("enterprise_id----:", enterprise_id) fmt.Println("supplier_id----:", supplier_id) // Redis key projectViewKey := "project:view:" + strconv.Itoa(pid) userViewedKey := "user:viewed:" + tid + ":" + strconv.Itoa(pid) if pid == 0 { 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"} } if viewed.IsNil() { // User hasn't viewed this product yet, increase the view count _, err = g.Redis().Do("INCR", projectViewKey) 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"} } } //浏览历史 currentDate := gtime.Now().Format("Ymd") // 设计 Redis Key redisBrowseKey := fmt.Sprintf("browseProject:%s:%s", currentDate, tid) fmt.Println("redis浏览记录的key为——————————", redisBrowseKey) // 将 project_id 添加到 Redis 中的 SET _, err = g.Redis().Do("SADD", redisBrowseKey, pid) 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 设置过期时间失败"} } var ProjectDetail *youngee_talent_model.ProjectDetail err = g.DB().Model("project_info").WithAll().Where("project_id", pid).Scan(&ProjectDetail) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } if enterprise_id != "" { //project来自商家 var enterprise *youngee_talent_model.Enterprise err = g.DB().Model("enterprise").WithAll().Where("enterprise_id", enterprise_id).Scan(&enterprise) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } ProjectDetail.Enterprise = enterprise } if supplier_id != "" { // 来自服务商 var younggeeSupplier *youngee_talent_model.YounggeeSupplier err = g.DB().Model("younggee_supplier").WithAll().Where("supplier_id", supplier_id).Scan(&younggeeSupplier) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } ProjectDetail.YounggeeSupplier = younggeeSupplier } return &TalentHttpResult{Code: 0, Msg: "success", Data: ProjectDetail} } func SignUpProjKuaishouList(r *ghttp.Request) *TalentHttpResult { tid, _ := utils.SessionTalentInfo.GetTalentIdFromSession(r) pid := r.GetQueryString("project_id", 0) // 定义用于存储查询结果的结构体切片 var results []*youngee_talent_model.KuaishouUserInfo // 查询此达人下,platform_id 为 8 或 4 的所有数据,并将结果扫描到结构体切片中 err := g.DB().Model(&youngee_talent_model.KuaishouUserInfo{}). Where("talent_id = ?", tid). Where("platform_id IN (?, ?)", 4, 8). Order("open_id ASC, platform_id DESC"). // 按 open_id 排序,确保每组的数据连续,并且 platform_id=4 的数据排在前面 Scan(&results) if err != nil { return &TalentHttpResult{Code: 1, Msg: "查询失败", Data: nil} } // 创建一个 map,用于记录每个 open_id 对应的记录 //key是openid,value是数组,从而实现一对多 openIdMap := make(map[string][]*youngee_talent_model.KuaishouUserInfo) // 将查询结果按 open_id 分组 for _, record := range results { openIdMap[record.OpenId] = append(openIdMap[record.OpenId], record) } // 筛选出 open_id 对应两条数据且 platform_id = 4 的记录 var resInfo []*youngee_talent_model.KuaishouUserInfo for _, records := range openIdMap { if len(records) == 2 { // 确保有两条数据,排除只有一个 platform_id 的情况 for _, record := range records { if record.PlatformId == 4 { // 选取 platform_id = 4(电商) 的记录,含有粉丝数就好,因为需要加入橱窗 resInfo = append(resInfo, record) break } } } } // 遍历 resInfo 检查过期和是否报名 for _, record := range resInfo { // 调用 CheckKuaishouTokenExp 函数,检查 openId 是否过期 expired := youngee_talent_service.CheckKuaishouTokenExp(record.OpenId) // 将过期检查结果赋值给 expired 属性 ,过期则不能被选中用于加入橱窗 record.Expired = expired //是否报名 isSignResult, err := g.DB().Model("younggee_task_info").Where("project_id=? AND open_id=? talent_id=?", pid, record.OpenId, tid).One() // 根据查询结果设置 IsSign 属性 if err != nil || isSignResult.IsEmpty() { record.IsSign = 0 // 没有查到数据,设置为 0 } else { record.IsSign = 1 // 查到数据,设置为 1 } } return &TalentHttpResult{Code: 0, Msg: "success", Data: resInfo} }