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" "reflect" "sort" "strconv" "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" ) func GetLocalLifeList(r *ghttp.Request) *TalentHttpResult { pageIndex := r.GetQueryInt("idx", -1) cntPerPage := r.GetQueryInt("cnt", -1) //组合筛选 platform := r.Get("platform", "") //抖音、快手、红Book、B站、微博 taskForm := r.Get("task_form", "") //任务形式,1-2分别代表线下探店,素材分发 contentType := r.Get("contenttype", "") //图文形式、视频形式 createTime := r.Get("createTime", "") //任务上线时间 //categoryForm := r.Get("categoryform", "") ////商品类目 智能家居、食品饮料等20种 //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: "参数错误"} } // 如果有任务形式的过滤条件,则将过滤条件保存于taskModeList var taskFormList []interface{} if taskForm != nil { if reflect.TypeOf(taskForm).Kind() != reflect.Slice { return &TalentHttpResult{Code: -2, Msg: "搜索条件任务形式错误"} } taskFormList = make([]interface{}, 0) taskFormList = taskForm.([]interface{}) } else { taskFormList = make([]interface{}, 0) } // 如果有平台的过滤条件,则将平台列表保存于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{}) } // 如果有内容形式的过滤条件,则将过滤条件保存于taskModeList var contentTypeList []interface{} if contentType != nil { if reflect.TypeOf(contentType).Kind() != reflect.Slice { return &TalentHttpResult{Code: -2, Msg: "搜索条件任务形式错误"} } contentTypeList = make([]interface{}, 0) contentTypeList = contentType.([]interface{}) } // 上线时间 var createTimeList []interface{} if createTime != nil { if reflect.TypeOf(createTime).Kind() != reflect.Slice { return &TalentHttpResult{Code: -2, Msg: "搜索条件任务形式错误"} } createTimeList = make([]interface{}, 0) createTimeList = createTime.([]interface{}) } // 如果有商品类目的过滤条件,则将过滤条件保存于categoryFormList //var categoryFormList []interface{} //if categoryForm != nil { // if reflect.TypeOf(categoryForm).Kind() != reflect.Slice { // return &TalentHttpResult{Code: -2, Msg: "搜索条件任务形式错误"} // } // // categoryFormList = make([]interface{}, 0) // categoryFormList = categoryForm.([]interface{}) //} //if len(categoryFormList) > 0 { // // 使用 categoryFormList 进行某些操作 // fmt.Println("Filtered categoryFormList:", categoryFormList) //} // 构造查询的条件 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 platformList != nil { whereStr = whereStr + " and local_platform in (" for _, v := range platformList { whereStr += v.(string) + ", " } whereStr = whereStr[0 : len(whereStr)-2] whereStr += ")" } if contentTypeList != nil { whereStr = whereStr + " and content_type in (" for _, v := range contentTypeList { whereStr += v.(string) + ", " } whereStr = whereStr[0 : len(whereStr)-2] whereStr += ")" } // 处理 selectionFormList (创建时间的过滤条件) if createTimeList != nil { whereStr += " AND created_at >= (NOW() - INTERVAL " for _, v := range createTimeList { switch v.(string) { case "1": // 近7天 whereStr += "7 DAY" case "2": // 近30天 whereStr += "30 DAY" case "3": // 近90天 whereStr += "90 DAY" default: continue // 无效的过滤条件,跳过 } } whereStr += ")" } if taskFormList != nil { whereStr += " and task_form in (" for _, v := range taskFormList { whereStr += v.(string) + ", " } whereStr = whereStr[0 : len(whereStr)-2] whereStr += ")" } if searchValue != nil { whereStr += " and local_name like '%" + searchValue.(string) + "%'" } fmt.Println("whereStr:-----》 ", whereStr) // 查询所有project var localList = []youngee_talent_model.YounggeeLocalLifeInfo{} err := g.Model(dao.ProjectInfo.Table).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: "查询数据库失败"} } // 遍历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 }) } } 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.GetQueryInt("id", 0) //通过首页进入的详情页,获取服务商id,younggee_supplier表 enterprise_id := r.GetQueryString("enterprise_id", "") //通过扫服务商码进入的详情页,获取服务商id,younggee_supplier表 //supplier_id := r.GetQueryString("supplier_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:" + strconv.Itoa(local_id) userViewedKey := "user:viewed:" + tid + ":" + strconv.Itoa(local_id) if local_id == 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"} } var LocalDetail *youngee_talent_model.LocalInfoDetail err = g.DB().Model("younggee_local_life_info").WithAll().Where("id", local_id).Scan(&LocalDetail) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } //违约 one, err := g.DB().Model("info_auto_default_handle").Where("auto_default_id=?", LocalDetail.AutoDefaultId).One() one2, err := 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 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()} } 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"} } // 构造查询条件 whereStr := fmt.Sprintf("talent_id = '%s'", tid) // 获取任务列表 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) 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 { taskBriefList.CompletedTaskInfoList = append(taskBriefList.CompletedTaskInfoList, taskInfoBrief) } } return &TalentHttpResult{Code: 0, Msg: "success", Data: taskBriefList} } func GetLocalTaskDetail(r *ghttp.Request) *TalentHttpResult { taskId := r.GetQueryInt("task_id", -1) 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.ProjectDetail{}).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.Model(dao.RecruitStrategy.Table).Where("local_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} } func GetLocalTaskBook(r *ghttp.Request) *TalentHttpResult { taskId := r.Get("task_id") res, err := g.DB().Model("younggee_book_info").Where("is_review = 1 and task_id = ?", taskId).OrderDesc("create_at").All() if err != nil { return &TalentHttpResult{Code: -1, Msg: err.Error()} } return &TalentHttpResult{Code: 0, Msg: "success", Data: res} } // 添加初稿service done func AddLocalTaskBook(r *ghttp.Request) *TalentHttpResult { var bookInfoReq *youngee_talent_model.AddLocalTaskBookRequest //解析添加初稿的图文 err := r.ParseForm(&bookInfoReq) if err != nil { return &TalentHttpResult{Code: -1, Msg: err.Error()} } taskbookInfo := []youngee_talent_model.LocalTaskBookInfo{} err = g.DB().Model("younggee_book_info").Where("task_id = ? ", bookInfoReq.TaskId).OrderDesc("create_at").Scan(&taskbookInfo) if err != nil { return &TalentHttpResult{Code: -2, Msg: "YounggeeSketchInfo find failed"} } //上传过但是被拒了 var condition1 bool = len(taskbookInfo) != 0 && taskbookInfo[0].IsReview == 1 && taskbookInfo[0].IsOk == 0 //没有上传过初稿 var condition2 bool = len(taskbookInfo) == 0 bookInfo := youngee_talent_model.LocalTaskBookInfo{ TaskId: bookInfoReq.TaskId, Day: bookInfoReq.Day, Time: bookInfoReq.Time, CreateAt: gtime.Now(), IsReview: 0, IsSubmit: 0, //忽略 IsOk: 0, } //只有这两种情况才能上传探店时间 if condition1 || condition2 { //插入新数据 _, err := g.DB().Model("younggee_book_info").Data(bookInfo).InsertAndGetId() if err != nil { return &TalentHttpResult{Code: -3, Msg: "YounggeeSketchInfo insert failed"} } } //修改task表中的字段待添加变成已添加,待修改变成已修改 bookStatus, err := g.DB().Model(model.YoungeeTaskInfo{}).Fields("book_status").Where("task_id = ?", bookInfoReq.TaskId).Value() if err != nil { return &TalentHttpResult{Code: -5, Msg: "Get task info failed"} } if bookStatus.Int64() == 1 { _, err = g.Model(dao.YoungeeTaskInfo.Table).Where("task_id = ?", bookInfoReq.TaskId).Update(g.Map{"bookStatus": 2}) if err != nil { return &TalentHttpResult{Code: -6, Msg: "YoungeeTaskInfo update failed"} } } else if bookStatus.Int64() == 3 { _, err = g.Model(dao.YoungeeTaskInfo.Table).Where("task_id = ?", bookInfoReq.TaskId).Update(g.Map{"bookStatus": 4}) if err != nil { return &TalentHttpResult{Code: -6, Msg: "YoungeeTaskInfo update failed"} } } //task设置为预约确认中 _, err = g.DB().Model("youngee_local_task_info").Data(g.Map{"task_stage": "5"}).Where("task_id = ?", bookInfoReq.TaskId).Update() return &TalentHttpResult{Code: 0, Msg: "success"} } func AddLocalTaskPhoto(r *ghttp.Request) *TalentHttpResult { var bookInfoReq *youngee_talent_model.AddLocalTaskPhotoRequest //解析添加初稿的图文 err := r.ParseForm(&bookInfoReq) if err != nil { return &TalentHttpResult{Code: -1, Msg: err.Error()} } bookPhotoInfo := youngee_talent_model.YounggeeBookPhoto{ TaskId: bookInfoReq.TaskId, PhotoUrl: bookInfoReq.PhotoUrl, CreateAt: gtime.Now(), } //修改task表中的字段待添加变成已添加,待修改变成已修改 _, err = g.DB().Model("younggee_book_photo").Data(&bookPhotoInfo).Insert() if err != nil { return &TalentHttpResult{Code: -5, Msg: "Get task info failed"} } _, err = g.DB().Model("youngee_local_task_info").Where("task_id = ?", bookInfoReq.TaskId).Data(g.Map{"task_stage": 9}).Update() _, err = g.DB().Model("youngee_local_task_info").Where("task_id = ?", bookInfoReq.TaskId).Data(g.Map{"book_status": 5}).Update() if err != nil { return &TalentHttpResult{Code: -5, Msg: "Get task info failed"} } return &TalentHttpResult{Code: 0, Msg: "success"} }