package youngee_sectask_service import ( "context" "encoding/json" "fmt" "github.com/gogf/gf/database/gdb" "reflect" "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 ) // 获取项目信息列表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)", 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) if err != nil { return &TalentHttpResult{Code: -6, Msg: "查询数据库失败"} } selectionInfoList.MaxPage = maxPage return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionInfoList} } // 获取单个选品详情service func GetSelectionDetail(r *ghttp.Request) *TalentHttpResult { sid := r.GetQueryString("selectionid", 0) pid := r.GetQueryString("productid", 0) if sid == "" { return &TalentHttpResult{Code: -2, Msg: "data query failed"} } var selectionDetail *youngee_talent_model.SelectionDetail err := g.DB().Model(youngee_talent_model.SelectionDetail{}).WithAll().Where("selection_id", sid).Scan(&selectionDetail) 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) //定义接口存该达人所有的报名任务 task := []model.YounggeeSecTaskInfo{} err = g.Model(dao.YounggeeSecTaskInfo.Table).Where("selection_id = ? and talent_id = ?", selectionId, tid).Scan(&task) if err != nil { return &TalentHttpResult{Code: -1, Msg: err.Error()} } isSign := youngee_talent_model.IsSignSecTask{} //如果有数据且sample_mode==3说明没有报名,否则报名了 //有数据且sample_mode==1说明报名了,否则没报名 &&左边为false就不执行了 if len(task) != 0 && task[0].SampleMode == 1 { isSign.SecTaskInfo = &task[0] isSign.IsSign = 1 } else { isSign.IsSign = 0 } return &TalentHttpResult{Code: 0, Msg: "success", Data: isSign} } // 选品任务报名service func SignUpSecTask(r *ghttp.Request) *TalentHttpResult { tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r) if err != nil { return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"} } // 解析前端传来的参数 var signSecTaskReq *youngee_talent_model.SignSecTaskReq err = r.ParseForm(&signSecTaskReq) if err != nil { return &TalentHttpResult{Code: -2, Msg: "parse param error"} } // 查得的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()} } //获取选品表中的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()} } // 收货地址详情 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, 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, } //删除添加橱窗时创建的不完整的数据 _, 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 { // young之团收益计算 var rewardConfig *model.YounggeeTeamRewardConfig whereCondition := g.Map{ dao.YounggeeTeamRewardConfig.Columns.ProjectType: 2, dao.YounggeeTeamRewardConfig.Columns.TaskForm: selectionDetail.TaskMode + 3, dao.YounggeeTeamRewardConfig.Columns.ContentForm: selectionDetail.ContentType, dao.YounggeeTeamRewardConfig.Columns.RewardReason: talentInfo.UserType, } err = tx.Model(dao.YounggeeTeamRewardConfig.Table).Where(whereCondition).Scan(&rewardConfig) if err != nil { fmt.Println(err) return err } if rewardConfig != nil { secTaskInfo.TeamPoint = rewardConfig.Point secTaskInfo.TeamIncome = rewardConfig.Money whereCondition = g.Map{ dao.YounggeeTalentTeam.Columns.TeamId: signSecTaskReq.LeadTeamId, } whereCondition1 := g.Map{ dao.YounggeeTalentTeam.Columns.TeamId: signSecTaskReq.TeamId, } updateData := g.Map{ dao.YounggeeTalentTeam.Columns.PointIncome: gdb.Raw(fmt.Sprintf("%s + %d", dao.YounggeeTalentTeam.Columns.PointIncome, rewardConfig.Point)), dao.YounggeeTalentTeam.Columns.MoneyIncome: gdb.Raw(fmt.Sprintf("%s + %f", dao.YounggeeTalentTeam.Columns.MoneyIncome, float64(rewardConfig.Money)*secTaskInfo.TaskReward/100)), } _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Data(updateData).Where(whereCondition).Update() if err != nil { return err } _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Data(updateData).Where(whereCondition1).Update() if err != nil { return err } // young之团状态变更 if signSecTaskReq.LeadTeamId != "" { // 更新young之团状态 whereCondition2 := g.Map{ dao.YounggeeTalentTeam.Columns.TeamStatus: 1, } updateData = g.Map{ dao.YounggeeTalentTeam.Columns.TeamStatus: 2, } _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Where(whereCondition).Where(whereCondition2).Data(updateData).Update() if err != nil { return err } } if signSecTaskReq.TeamId != "" { // 更新young之团状态 whereCondition2 := g.Map{ dao.YounggeeTalentTeam.Columns.TeamStatus: 1, } updateData = g.Map{ dao.YounggeeTalentTeam.Columns.TeamStatus: 2, } _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Where(whereCondition1).Where(whereCondition2).Data(updateData).Update() if err != nil { return err } } } if selectionDetail.SampleMode == 2 { // 减少选品库存 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 } // 新建任务,初始化状态为待发货 secTaskInfo.TaskStage = 6 _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo).Insert() if err != nil { return err } } else { // 新建任务,初始化状态为待确认 _, 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, 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, } //如果已经有数据了,删除。模拟在快手侧里把橱窗商品删了,想再加回来 //删除添加橱窗时创建的不完整的数据 _, 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} }