package youngee_sectask_service import ( "context" "encoding/json" "fmt" "github.com/gogf/gf/database/gdb" "github.com/lin-jim-leon/kuaishou/open/merchant" "reflect" "sort" "youngmini_server/app/dao" "youngmini_server/app/model" "youngmini_server/app/model/youngee_talent_model" "youngmini_server/app/utils" "github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/os/gtime" ) const ( selectionStatusCreating = iota + 1 selectionStatusReviewing selectionStatusReviewed selectionStatusPaying selectionStatusPaid selectionStatusInProgress selectionStatusInvalid selectionStatusClosed //快手电商 ClientKey = "ks651333097154138217" ClientSecret = "dBt0rVRhTpUqcrOYGGpv0A" SignSecret = "bf6393dce0a2b669ee348bebb837b0da" //快手平台 ClientKey1 = "ks671599294546520767" ClientSecret1 = "8VSrp3O09nunjLMXR1uotg" //SignSecret1 = "bf6393dce0a2b669ee348bebb837b0da" //抖音平台 ClientKey2 = "awi77xl5kpl16hmi" ClientSecret2 = "7ce6d2531bd4489122d89658063fd76e" //SignSecret1 = "bf6393dce0a2b669ee348bebb837b0da" ) // 获取项目信息列表service func GetSelectionList(r *ghttp.Request) *TalentHttpResult { pageIndex := r.GetQueryInt("idx", -1) cntPerPage := r.GetQueryInt("cnt", -1) //排序字段 sortField := r.GetString("sortField", "") //根据哪个字段排序 orderTag := r.GetQueryInt("sortOrder", -1) //1降序,0升序 //领样和悬赏条件 secForm := r.Get("secform") taskForm := r.Get("taskform") //任务上线时间 createTime := r.Get("createTime") //商品类目 智能家居、食品饮料等20种 categoryForm := r.Get("categoryform") //搜索条件 searchValue := r.Get("searchvalue") // 如果有领样形式的过滤条件,则将过滤条件保存于secFormList var secFormList []interface{} if secForm != nil { if reflect.TypeOf(secForm).Kind() != reflect.Slice { return &TalentHttpResult{Code: -2, Msg: "搜索条件领样形式错误"} } secFormList = make([]interface{}, 0) secFormList = secForm.([]interface{}) } // 如果有任务形式的过滤条件,则将过滤条件保存于taskFormList 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{}) } // 上线时间 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{}) } // 构造查询的条件 startId := pageIndex * cntPerPage whereStr := fmt.Sprintf("(selection_status >= %d) AND (status = 0)", selectionStatusInProgress) if taskFormList != nil { whereStr += " and task_mode in (" for _, v := range taskFormList { whereStr += v.(string) + ", " } whereStr = whereStr[0 : len(whereStr)-2] whereStr += ")" } if secFormList != nil { whereStr += " and sample_mode in (" for _, v := range secFormList { 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 += ")" } //商品类目 来自product if categoryFormList != nil { whereStr += " and product_category in (" for _, v := range categoryFormList { whereStr += v.(string) + ", " } whereStr = whereStr[0 : len(whereStr)-2] whereStr += ")" } //搜索栏 if searchValue != nil { whereStr += " and selection_name like '%" + searchValue.(string) + "%'" } fmt.Println("whereStr----->:", whereStr) // 判断请求页面是否超过最大页面 c, err := g.DB().Model("younggee_selection_info").WithAll().Where(whereStr).Count() fmt.Println("满足条件的selection数目--", c) if err != nil { return &TalentHttpResult{Code: -4, Msg: err.Error(), Data: nil} } maxPage := c / cntPerPage if c%cntPerPage > 0 { maxPage += 1 } if pageIndex+1 > maxPage { return &TalentHttpResult{Code: -5, Msg: "over max page"} } fmt.Println("hereiiiii") var selectionInfoList = youngee_talent_model.SelectionInfoList{ Count: c, } err = g.DB().Model("younggee_selection_info").WithAll(). Where(whereStr). Order("task_ddl DESC").Limit(startId, cntPerPage).Scan(&selectionInfoList.SelectionDetail) if err != nil { return &TalentHttpResult{Code: -6, Msg: "查询数据库失败"} } selectionInfoList.MaxPage = maxPage //已经拿到了数据(where之后)。根据product中的字段排序 sort.Slice(selectionInfoList.SelectionDetail, func(i, j int) bool { // 获取当前的产品信息 productI := selectionInfoList.SelectionDetail[i].YounggeeProduct productJ := selectionInfoList.SelectionDetail[j].YounggeeProduct // 计算 commission_price commissionPriceI := productI.ExclusiveCommission * 0.01 * productI.ProductPrice commissionPriceJ := productJ.ExclusiveCommission * 0.01 * productJ.ProductPrice // 根据 sortField 选择排序的字段 switch sortField { case "sales_count": if orderTag == 1 { return productI.SalesCount > productJ.SalesCount // 降序 } return productI.SalesCount < productJ.SalesCount // 升序 case "exclusive_commission": if orderTag == 1 { return productI.ExclusiveCommission > productJ.ExclusiveCommission // 降序 } return productI.ExclusiveCommission < productJ.ExclusiveCommission // 升序 case "commission_price": if orderTag == 1 { return commissionPriceI > commissionPriceJ // 降序 } return commissionPriceI < commissionPriceJ // 升序 case "product_price": if orderTag == 1 { return productI.ProductPrice > productJ.ProductPrice // 降序 } return productI.ProductPrice < productJ.ProductPrice // 升序 default: // 如果没有匹配的字段,则不做排序,默认排序 return false } }) return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionInfoList} } // 获取项目信息列表service //func GetSelectionList(r *ghttp.Request) *TalentHttpResult { // // pageIndex := r.GetQueryInt("idx", -1) // cntPerPage := r.GetQueryInt("cnt", -1) // //platform := r.Get("platform") // //secForm := r.Get("secform") // //taskForm := r.Get("taskform") // //categoryForm := r.Get("categoryform") // //searchValue := r.Get("searchvalue") // //if pageIndex == -1 || cntPerPage == -1 || cntPerPage == 0 { // // return &TalentHttpResult{Code: -1, Msg: "参数错误"} // //} // // // //// 如果有领样形式的过滤条件,则将过滤条件保存于secFormList // //var secFormList []interface{} // //if secForm != nil { // // if reflect.TypeOf(secForm).Kind() != reflect.Slice { // // return &TalentHttpResult{Code: -2, Msg: "搜索条件领样形式错误"} // // } // // // // secFormList = make([]interface{}, 0) // // secFormList = secForm.([]interface{}) // //} // // // //// 如果有任务形式的过滤条件,则将过滤条件保存于taskFormList // //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{}) // //} // // // //// 如果有商品类目的过滤条件,则将过滤条件保存于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{}) // //} // // // 如果有平台的过滤条件,则将平台列表保存于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("(selection_status >= %d) AND (status = 0) ", selectionStatusInProgress) // /* if platformList != nil { // whereStr = whereStr + " and platform in (" // for _, v := range platformList { // whereStr += v.(string) + ", " // } // // whereStr = whereStr[0 : len(whereStr)-2] // whereStr += ")" // }*/ // // //if taskFormList != nil { // // whereStr += " and task_mode in (" // // for _, v := range taskFormList { // // whereStr += v.(string) + ", " // // } // // // // whereStr = whereStr[0 : len(whereStr)-2] // // whereStr += ")" // //} // //if secFormList != nil { // // whereStr += " and sample_mode in (" // // for _, v := range secFormList { // // whereStr += v.(string) + ", " // // } // // // // whereStr = whereStr[0 : len(whereStr)-2] // // whereStr += ")" // //} // // // //if categoryFormList != nil { // // whereStr += " and product_category in (" // // for _, v := range categoryFormList { // // whereStr += v.(string) + ", " // // } // // // // whereStr = whereStr[0 : len(whereStr)-2] // // whereStr += ")" // //} // // ////搜索栏 // //if searchValue != nil { // // whereStr += " and selection_name like '%" + searchValue.(string) + "%'" // //} // // // 查询所有selection // //YounggeeSelectionInfo含有表中的所有属性 // var selectionList = []model.YounggeeSelectionInfo{} // //err := g.Model(dao.YounggeeSelectionInfo.Table).Where(whereStr).Scan(&selectionList) // //展示带货商品的排序规则 预估赚、ddl未处理 // err := g.Model(dao.YounggeeSelectionInfo.Table).Where(whereStr).Order("commission_rate DESC , task_reward DESC ").Scan(&selectionList) // if err != nil { // return &TalentHttpResult{Code: -3, Msg: "查询数据库失败"} // } // //fmt.Println("****searchValue:", searchValue) // //fmt.Println("****secFormList:", secFormList) // //fmt.Println("****taskFormList:", taskFormList) // // fmt.Println("****whereStr: ", whereStr) // // // 判断请求页面是否超过最大页面 // c, err := g.DB().Model(dao.YounggeeSelectionInfo.Table).Where(whereStr).Count() // if err != nil { // return &TalentHttpResult{Code: -4, Msg: err.Error(), Data: nil} // } // // maxPage := c / cntPerPage // if c%cntPerPage > 0 { // maxPage += 1 // } // // if pageIndex+1 > maxPage { // return &TalentHttpResult{Code: -5, Msg: "over max page"} // } // // var selectionInfoList = youngee_talent_model.SelectionInfoList{ // Count: c, // } // // err = g.DB().Model(dao.YounggeeSelectionInfo.Table).WithAll().Where(whereStr). // //Order("selection_status ASC , task_ddl DESC , commission_rate DESC , task_reward DESC, selection_id").Limit(startId, cntPerPage).Scan(&selectionInfoList.SelectionDetail) // Order("commission_rate DESC").Limit(startId, cntPerPage).Scan(&selectionInfoList.SelectionDetail) // if err != nil { // return &TalentHttpResult{Code: -6, Msg: "查询数据库失败"} // } // // selectionInfoList.MaxPage = maxPage // // return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionInfoList} //} // 获取带货收藏列表 func GetSelectionCollectionList(r *ghttp.Request) *TalentHttpResult { // 从 session 中获取 talent_id tId, err := utils.SessionTalentInfo.GetTalentIdFromSession(r) if err != nil { return nil } // 获取 younggee_selection_collect_info 表中符合条件的 selection_id 列表 var selectionIds []string type SelectionInfo struct { SelectionID string `json:"selection_id"` } var selectionInfos []SelectionInfo err = g.DB().Model("younggee_selection_collect_info").Where("talent_id = ? AND deleted = ?", tId, 0).Fields("selection_id").Scan(&selectionInfos) fmt.Println("-----", selectionInfos) if err != nil { fmt.Println("查询 selection_id 失败---", err.Error()) return &TalentHttpResult{Code: -6, Msg: "查询数据库失败"} } for _, info := range selectionInfos { selectionIds = append(selectionIds, info.SelectionID) } // 如果没有符合条件的 selection_id,则直接返回空的列表 if len(selectionIds) == 0 { return &TalentHttpResult{Code: 0, Msg: "success", Data: youngee_talent_model.SelectionCollectionInfoList{Count: 0}} } // 根据获取到的 selection_id 列表,从 younggee_selection_info 表中获取对应的信息 var selectionCollectionInfoList = youngee_talent_model.SelectionCollectionInfoList{ Count: len(selectionInfos), } err = g.DB().Model("younggee_selection_info").WithAll().Where("selection_id IN(?)", selectionIds).Scan(&selectionCollectionInfoList.SelectionCollectionList) if err != nil { return &TalentHttpResult{Code: -6, Msg: "查询数据库失败"} } // 返回结果 return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionCollectionInfoList} } // 获取种草收藏列表 func GetProjectCollectionList(r *ghttp.Request) *TalentHttpResult { // 从 session 中获取 talent_id tId, err := utils.SessionTalentInfo.GetTalentIdFromSession(r) if err != nil { return nil } // 获取 younggee_selection_collect_info 表中符合条件的 selection_id 列表 var projectIds []string type ProjectInfo struct { ProjectID string `json:"project_id"` } var projectInfos []ProjectInfo err = g.DB().Model("younggee_project_collect_info").Where("talent_id = ? AND deleted = ?", tId, 0).Fields("project_id").Scan(&projectInfos) fmt.Println("-----", projectInfos) if err != nil { fmt.Println("查询 project_id 失败---", err.Error()) return &TalentHttpResult{Code: -6, Msg: "查询数据库失败"} } for _, info := range projectInfos { projectIds = append(projectIds, info.ProjectID) } // 如果没有符合条件的 selection_id,则直接返回空的列表 if len(projectIds) == 0 { return &TalentHttpResult{Code: 0, Msg: "success", Data: youngee_talent_model.ProjectCollectionInfoList{Count: 0}} } // 根据获取到的 selection_id 列表,从 younggee_selection_info 表中获取对应的信息 var projectCollectionInfoList = youngee_talent_model.ProjectCollectionInfoList{ Count: len(projectIds), } err = g.DB().Model("project_info").WithAll().Where("project_id IN(?)", projectIds).Scan(&projectCollectionInfoList.ProjectCollectionList) if err != nil { return &TalentHttpResult{Code: -6, Msg: "查询数据库失败"} } // 返回结果 return &TalentHttpResult{Code: 0, Msg: "success", Data: projectCollectionInfoList} } // 获取单个选品详情service func GetSelectionDetail(r *ghttp.Request) *TalentHttpResult { tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r) if err != nil { return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"} } sid := r.GetQueryString("selectionid", 0) pid := r.GetQueryString("productid", 0) if sid == "" { return &TalentHttpResult{Code: -2, Msg: "data query failed"} } //带货浏览历史 //浏览历史 currentDate := gtime.Now().Format("Ymd") fmt.Println("date-----:", currentDate) // 设计 Redis Key redisBrowseKey := fmt.Sprintf("browseSelection:%s:%s", currentDate, tid) fmt.Println("redis浏览记录的key为——————————", redisBrowseKey) // 将 selection_id 添加到 Redis 中的 SET _, err = g.Redis().Do("SADD", redisBrowseKey, sid) if err != nil { return &TalentHttpResult{Code: -1, Msg: "Redis 存浏览历史数据失败"} } // 设置 Key 的过期时间为 7 天 _, err = g.Redis().Do("EXPIRE", redisBrowseKey, 7*24*60*60) // 7 天的秒数 if err != nil { return &TalentHttpResult{Code: -2, Msg: "Redis 设置过期时间失败"} } var selectionDetail *youngee_talent_model.SelectionDetail err = g.DB().Model(youngee_talent_model.SelectionDetail{}).WithAll().Where("selection_id", sid).Scan(&selectionDetail) //填充收藏信息 collectionInfo := []youngee_talent_model.SelectionCollection{} err = g.DB().Model("younggee_selection_collect_info").Where("selection_id=? and talent_id = ?", sid, tid).Scan(&collectionInfo) if err != nil { return &TalentHttpResult{Code: -1, Msg: err.Error()} } //已被收藏 if len(collectionInfo) != 0 && collectionInfo[0].Deleted == 0 { //有数据 且 没取消收藏 selectionDetail.IsCollected = 1 } else { selectionDetail.IsCollected = 0 //没数据 或 有数据但取消了收藏 } //返回的data作为收藏接口的is_collect字段传入 if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } // 查询younggee_product表数据 var younggeeProduct *youngee_talent_model.YounggeeProduct err = g.DB().Model(youngee_talent_model.YounggeeProduct{}).WithAll().Where("product_id", pid).Scan(&younggeeProduct) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } //查询younggee_product_photo表数据 var younggeeProductPhoto []*youngee_talent_model.YounggeeProductPhoto err = g.DB().Model(youngee_talent_model.YounggeeProductPhoto{}).WithAll().Where("product_id", pid).Scan(&younggeeProductPhoto) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } //selectionDetail.FreeStrategy = freeStrategy //selectionDetail.RewardStrategy = rewardStrategy selectionDetail.YounggeeProduct = younggeeProduct selectionDetail.YounggeeProductPhoto = younggeeProductPhoto //selectionDetail.SelectionBrief = selectionBrief //selectionDetail.SelectionExample = selectionExample return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionDetail} } func GetProductDetail(r *ghttp.Request) *TalentHttpResult { pid := r.GetQueryString("productid", 0) if pid == "" { return &TalentHttpResult{Code: -2, Msg: "data query failed"} } var productDetail *youngee_talent_model.YounggeeProduct err := g.DB().Model(youngee_talent_model.YounggeeProduct{}).WithAll().Where("product_id", pid).Scan(&productDetail) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } return &TalentHttpResult{Code: 0, Msg: "success", Data: productDetail} } func GetProductPhoto(r *ghttp.Request) *TalentHttpResult { //访问表,获取图片 return nil } // 判断是否已报名任务,如果是提供免费领样(按钮既然有肯定是免费领样),sectask中的sample_mode==3,或者没有数据。表示还没报名,只是添加橱窗了 func IsSignUpSecTask(r *ghttp.Request) *TalentHttpResult { tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r) if err != nil { return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"} } selectionId := r.GetQueryInt("selection_id", -1) openId := r.GetQueryString("open_id", "") //定义接口存该达人所有的报名任务 //1.查task表全部数据 var task []youngee_talent_model.SecTaskInfoDetail err = g.DB().Model(youngee_talent_model.SecTaskInfoDetail{}).WithAll(). Where("talent_id = ? AND selection_id = ? AND open_id = ?", tid, selectionId, openId). Scan(&task) if err != nil { return &TalentHttpResult{Code: 0, Msg: err.Error(), Data: nil} } isSign := youngee_talent_model.IsSignSecTask{} //有数据且sample_mode==1说明报名了,否则没报名 &&左边为false就不执行了 free_stage!=0 if len(task) != 0 && task[0].FreeStage != 0 { isSign.IsSign = 1 isSign.SecTaskInfo = &task[0] } else { isSign.IsSign = 0 } return &TalentHttpResult{Code: 0, Msg: "success", Data: isSign} } // 添加橱窗--选中账号--此账号是否有报名信息 func IsCreateSecTask(r *ghttp.Request) *TalentHttpResult { tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r) openId := r.GetString("open_id", "") if err != nil { return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"} } selectionId := r.GetQueryInt("selection_id", -1) //定义接口存该达人所有的报名任务 //1.查task表全部数据 var task []youngee_talent_model.SecTaskInfoDetail err = g.DB().Model(youngee_talent_model.SecTaskInfoDetail{}).WithAll(). Where("talent_id = ? AND selection_id = ? open_id = ?", tid, selectionId, openId). Scan(&task) if err != nil { return &TalentHttpResult{Code: 0, Msg: err.Error(), Data: nil} } isSign := youngee_talent_model.IsSignSecTask{} //添加橱窗时,有task了就不再创建 if len(task) != 0 { isSign.IsSign = 1 isSign.SecTaskInfo = &task[0] return &TalentHttpResult{Code: 1, Msg: "存在报名信息", Data: isSign} } else { isSign.IsSign = 0 return &TalentHttpResult{Code: 2, Msg: "不存在报名信息", Data: isSign} } } // 已选中账号--点击立即报名并加入橱窗--此时肯定未过期,未报名,提供免费领 func SignUpSecTaskWithKsAccount(r *ghttp.Request) *TalentHttpResult { tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r) if err != nil { return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"} } // 解析前端post请求传来的参数, var signSecTaskReq *youngee_talent_model.SignSecTaskReq err = r.ParseForm(&signSecTaskReq) if err != nil { return &TalentHttpResult{Code: -2, Msg: "parse param error"} } if signSecTaskReq.IsOk == 0 { return &TalentHttpResult{Code: -10, Msg: "此账号领样条件不满足"} } // 查得的selectionDetail包含product、photo、策略表等综合数据 var selectionDetail *youngee_talent_model.SelectionDetail err = g.DB().Model(youngee_talent_model.SelectionDetail{}).WithAll().Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionDetail) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } //根据freeStrage判断当前的粉丝数是否满足条件 //前端传递根据粉丝数目和悬赏策略对比。传递满足条件是否满足。 //获取选品表中的ddl var selectionInfo *model.YounggeeSelectionInfo err = g.DB().Model("younggee_selection_info").Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionInfo) var product model.YounggeeProduct err = json.Unmarshal([]byte(selectionDetail.ProductSnap), &product) if err != nil { return &TalentHttpResult{Code: -3, Msg: "json Unmarshal failed"} } // 查询达人详情 var talentInfo *youngee_talent_model.TalentInfo err = g.DB().Model("youngee_talent_info").WithAll().Where("id", tid).Scan(&talentInfo) if err != nil { return &TalentHttpResult{Code: -4, Msg: "Get talent info failed"} } // 社媒账号详情 var accountInfo *youngee_talent_model.PlatformAccountInfo err = g.DB().Model("youngee_platform_account_info").WithAll().Where("talent_id = ? and platform_id = ?", tid, selectionDetail.Platform).Scan(&accountInfo) if err != nil { return &TalentHttpResult{Code: -5, Msg: err.Error()} } // 收货地址详情 生成的结果变成snap存在task表中,供快递100使用 address, err := g.DB().Model(dao.YoungeeTalentDeliveryAddress.Table).One("talent_id = ? and address_id = ?", tid, signSecTaskReq.AddressId) if err != nil { return &TalentHttpResult{Code: -6, Msg: err.Error()} } var newTaskId string // 首先生成任务id newTaskId = utils.GetUuid.GetTaskId(selectionDetail.SelectionId, selectionDetail.EnterpriseId, tid) // 生成达人平台账号信息快照 accountSnap, err := gjson.Encode(accountInfo) if err != nil { return &TalentHttpResult{Code: -7, Msg: "encode platform snap failed"} } // 生成达人信息快照 talentSnap, err := gjson.Encode(talentInfo) if err != nil { return &TalentHttpResult{Code: -8, Msg: "encode talent info snap failed"} } // 生成收货地址快照 addrSnap, err := gjson.Encode(address) if err != nil { return &TalentHttpResult{Code: -9, Msg: "encode delivery address snap failed"} } secTaskInfo := youngee_talent_model.SecTaskInfoDetail{ TaskId: newTaskId, SelectionId: signSecTaskReq.SelectionId, ProductId: signSecTaskReq.ProductId, SaleNum: signSecTaskReq.SaleNum, FansNum: signSecTaskReq.FansNum, OpenId: signSecTaskReq.OpenId, TalentId: tid, AccountId: accountInfo.AccountId, TalentPlatformInfoSnap: string(accountSnap), TalentPersonalInfoSnap: string(talentSnap), TalentPostAddrSnap: string(addrSnap), TaskReward: selectionDetail.TaskReward, TalentPayment: product.ProductPrice, IsPayPayment: 0, IsPayReward: 0, TaskMode: selectionDetail.TaskMode, SampleMode: selectionDetail.SampleMode, FreeStage: 1, //领样状态:已申请 悬赏状态默认为0 PlatformId: 4, //快手平台 TaskStage: 3, TaskStatus: 1, CreateDate: gtime.Now(), CompleteStatus: 1, LogisticsStatus: 1, AssignmentStatus: 1, UpdateAt: gtime.Now(), WithdrawStatus: 1, //LeadTeamId: signSecTaskReq.LeadTeamId, //TeamId: signSecTaskReq.TeamId, TeamIncome: 0, TeamPoint: 0, TaskDdl: selectionInfo.TaskDdl, } //加入橱窗逻辑, productId := signSecTaskReq.ProductId pIdSlice := []string{productId} value, err := g.DB().Model("platform_kuaishou_user_info").Fields("open_id").Where("talent_id=? AND open_id = ?", tid, signSecTaskReq.OpenId).Value() if err != nil { fmt.Println("query db fail") } accessToken := value.String() _, err = merchant.AddItemsToShelf(ClientKey, SignSecret, accessToken, pIdSlice) if err != nil { //表示添加失敗,沒有開通 return &TalentHttpResult{Code: -1, Msg: "快手未開通橱窗、商品不存在或已下线", Data: nil} } // 查询成功,返回成功结果和数据 fmt.Println("加入橱窗成功") //删除仅点击添加橱窗时创建的不完整的数据 _, err = g.DB().Model("younggee_sec_task_info"). Where("talent_id = ? AND selection_id = ? AND open_id=?", tid, signSecTaskReq.SelectionId, signSecTaskReq.OpenId). Delete() if err != nil { return &TalentHttpResult{Code: -17, Msg: "younggee_sec_task_info delete failed"} } err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error { //// 减少选品库存 //whereCondition1 := g.Map{ // dao.YounggeeSelectionInfo.Columns.SelectionId: selectionDetail.SelectionId, //} //updateData := g.Map{ // dao.YounggeeSelectionInfo.Columns.RemainNum: gdb.Raw(fmt.Sprintf("%s - 1", dao.YounggeeSelectionInfo.Columns.RemainNum)), // //} //_, err = tx.Ctx(ctx).Model(dao.YounggeeSelectionInfo.Table).Where(whereCondition1).Data(updateData).Update() //if err != nil { // return err //} updateData := g.Map{ "remain_num": selectionDetail.RemainNum - 1, "enroll_num": selectionDetail.EnrollNum + 1, } _, err = tx.Ctx(ctx).Model("younggee_selection_info").Where("selection_id=?", selectionDetail.SelectionId).Data(updateData).Update() if err != nil { return err } // 新建任务,初始化状态为待发货 secTaskInfo.TaskStage = 6 _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo).Insert() if err != nil { return err } return nil }) if err != nil { return &TalentHttpResult{Code: -18, Msg: "add Task data failed"} } signSecTaskResp := youngee_talent_model.SignSecTaskResp{ TaskId: newTaskId, } return &TalentHttpResult{Code: 0, Msg: "success", Data: signSecTaskResp} //return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionDetail} } // 加入橱窗--没有报名信息则创建报名信息 func SignUpSecTaskFromWindow(r *ghttp.Request) *TalentHttpResult { tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r) if err != nil { return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"} } // 解析body/json参数 var signSecTaskReq *youngee_talent_model.SignSecTaskReq err = r.ParseForm(&signSecTaskReq) if err != nil { return &TalentHttpResult{Code: -2, Msg: "parse param error"} } // 查询选品详情 var selectionDetail *youngee_talent_model.SelectionDetail err = g.DB().Model(youngee_talent_model.SelectionDetail{}).WithAll().Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionDetail) if err != nil { return &TalentHttpResult{Code: -3, Msg: err.Error()} } //获取选品表中的ddl var selectionInfo *model.YounggeeSelectionInfo err = g.DB().Model("younggee_selection_info").Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionInfo) var product model.YounggeeProduct err = json.Unmarshal([]byte(selectionDetail.ProductSnap), &product) if err != nil { return &TalentHttpResult{Code: -3, Msg: "json Unmarshal failed"} } // 查询达人详情 var talentInfo *youngee_talent_model.TalentInfo err = g.DB().Model("youngee_talent_info").WithAll().Where("id", tid).Scan(&talentInfo) if err != nil { return &TalentHttpResult{Code: -4, Msg: "Get talent info failed"} } // 社媒账号详情 var accountInfo *youngee_talent_model.PlatformAccountInfo err = g.DB().Model("youngee_platform_account_info").WithAll().Where("talent_id = ? and platform_id = ?", tid, selectionDetail.Platform).Scan(&accountInfo) if err != nil { return &TalentHttpResult{Code: -5, Msg: err.Error()} } var newTaskId string // 首先生成任务id newTaskId = utils.GetUuid.GetTaskId(selectionDetail.SelectionId, selectionDetail.EnterpriseId, tid) // 生成达人平台账号信息快照 accountSnap, err := gjson.Encode(accountInfo) if err != nil { return &TalentHttpResult{Code: -7, Msg: "encode platform snap failed"} } // 生成达人信息快照 talentSnap, err := gjson.Encode(talentInfo) if err != nil { return &TalentHttpResult{Code: -8, Msg: "encode talent info snap failed"} } //没有任务被创建时,用此数据 secTaskInfo := youngee_talent_model.SecTaskInfoWindowDetail{ TaskId: newTaskId, SelectionId: signSecTaskReq.SelectionId, ProductId: signSecTaskReq.ProductId, SaleNum: signSecTaskReq.SaleNum, FansNum: signSecTaskReq.FansNum, TalentId: tid, OpenId: signSecTaskReq.OpenId, AccountId: accountInfo.AccountId, TalentPlatformInfoSnap: string(accountSnap), TalentPersonalInfoSnap: string(talentSnap), //TalentPostAddrSnap: string(addrSnap), TaskReward: selectionInfo.TaskReward, TalentPayment: product.ProductPrice, IsPayPayment: 0, IsPayReward: 0, TaskMode: selectionInfo.TaskMode, SampleMode: 3, //添加橱窗,当作不提供领样处理 PlatformId: 4, //快手电商平台 TaskStage: 3, TaskStatus: 1, CreateDate: gtime.Now(), CompleteStatus: 1, LogisticsStatus: 1, AssignmentStatus: 1, UpdateAt: gtime.Now(), WithdrawStatus: 1, LeadTeamId: signSecTaskReq.LeadTeamId, TeamId: signSecTaskReq.TeamId, TeamIncome: 0, TeamPoint: 0, TaskDdl: selectionInfo.TaskDdl, } //如果已经有数据了,删除。模拟在快手侧里把橱窗商品删了,想再加回来 //1.查task表全部数据 , 有的话,只会有一条数据 var secTaskInfoList []youngee_talent_model.SecTaskInfoDetail err = g.DB().Model(youngee_talent_model.SecTaskInfoDetail{}).WithAll(). Where("talent_id = ? AND selection_id = ? AND open_id = ?", tid, signSecTaskReq.SelectionId, signSecTaskReq.OpenId). Scan(&secTaskInfoList) if err != nil { return &TalentHttpResult{Code: -16, Msg: err.Error(), Data: nil} } //2.如果task不为空。取出free_stage的值。插入的值含有删除数据的free_stage //这种情况出现在报名之后又点击加入橱窗 if len(secTaskInfoList) != 0 { free_stage := secTaskInfoList[0].FreeStage //删除旧的, _, err = g.DB().Model("younggee_sec_task_info"). Where("talent_id = ? AND selection_id = ? AND open_id = ?", tid, signSecTaskReq.SelectionId, signSecTaskReq.OpenId). Delete() if err != nil { return &TalentHttpResult{Code: -17, Msg: "younggee_sec_task_info delete failed"} } //插入新的(含free_stage) secTaskInfo_new := youngee_talent_model.SecTaskInfoWindowDetail{ TaskId: newTaskId, SelectionId: signSecTaskReq.SelectionId, ProductId: signSecTaskReq.ProductId, SaleNum: signSecTaskReq.SaleNum, FansNum: signSecTaskReq.FansNum, TalentId: tid, AccountId: accountInfo.AccountId, TalentPlatformInfoSnap: string(accountSnap), TalentPersonalInfoSnap: string(talentSnap), //TalentPostAddrSnap: string(addrSnap), TaskReward: selectionInfo.TaskReward, TalentPayment: product.ProductPrice, IsPayPayment: 0, IsPayReward: 0, TaskMode: selectionInfo.TaskMode, SampleMode: 3, //添加橱窗,当作不提供领样处理 PlatformId: 4, //快手平台 TaskStage: 3, TaskStatus: 1, CreateDate: gtime.Now(), CompleteStatus: 1, LogisticsStatus: 1, AssignmentStatus: 1, UpdateAt: gtime.Now(), WithdrawStatus: 1, LeadTeamId: signSecTaskReq.LeadTeamId, TeamId: signSecTaskReq.TeamId, TeamIncome: 0, TeamPoint: 0, TaskDdl: selectionInfo.TaskDdl, FreeStage: free_stage, } err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error { // 新建任务,初始化状态为待确认 _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo_new).Insert() if err != nil { return err } return nil }) if err != nil { return &TalentHttpResult{Code: -18, Msg: "add Task data failed"} } signSecTaskResp := youngee_talent_model.SignSecTaskResp{ TaskId: newTaskId, } return &TalentHttpResult{Code: 0, Msg: "success", Data: signSecTaskResp} } else { //3.如果已经报过名了需要传递给新的数据。删除添加橱窗时创建的不完整的数据 _, err = g.DB().Model("younggee_sec_task_info"). Where("talent_id = ? AND selection_id = ? ", tid, signSecTaskReq.SelectionId). Delete() if err != nil { return &TalentHttpResult{Code: -17, Msg: "younggee_sec_task_info delete failed"} } err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error { // 新建任务,初始化状态为待确认 _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo).Insert() if err != nil { return err } return nil }) if err != nil { return &TalentHttpResult{Code: -18, Msg: "add Task data failed"} } signSecTaskResp := youngee_talent_model.SignSecTaskResp{ TaskId: newTaskId, } return &TalentHttpResult{Code: 0, Msg: "success", Data: signSecTaskResp} } }