123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758 |
- package youngee_sectask_service
- import (
- "context"
- "encoding/json"
- "fmt"
- "github.com/gogf/gf/database/gdb"
- "github.com/lin-jim-leon/kuaishou/open/merchant"
- "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
- //快手电商
- 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)
- //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)
- 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 delete = ?", 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)
- 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"}
- }
- // 查得的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()}
- }
- // 收货地址详情 生成的结果变成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
- }
- // 新建任务,初始化状态为待发货
- 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}
- }
- }
|