package service import ( "encoding/json" "errors" "github.com/caixw/lib.go/conv" "github.com/sirupsen/logrus" "reflect" "time" "youngee_b_api/app/dao" "youngee_b_api/app/entity" "youngee_b_api/app/util" "youngee_b_api/app/vo" ) type SelectionInfoService struct{} //func (s *SelectionInfoService) GetSelectionInfo(ctx *gin.Context, selectionId string) (*http_model.SelectionDetail, error) { // selectionDetail := http_model.SelectionDetail{} // selectionInfo, err := db.GetSelectionById(ctx, selectionId) // // if err != nil { // logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionInfo error,err:%+v", err) // return nil, err // } // selectionBriefInfo, err := db.GetSelectionBriefInfo(ctx, selectionId) // if err != nil { // logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionBriefInfo error,err:%+v", err) // return nil, err // } // selectionExampleInfo, err := db.GetSelectionExampleInfo(ctx, selectionId) // if err != nil { // logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionExampleInfo error,err:%+v", err) // return nil, err // } // productInfo, err := db.GetProductInfoBySelectionId(ctx, selectionId) // if err != nil { // logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductInfo error,err:%+v", err) // return nil, err // } // productPhotoInfo, err := db.GetProductPhotoInfoBySelectionId(ctx, selectionId) // if err != nil { // logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductPhotoInfo error,err:%+v", err) // return nil, err // } // selectionDetail.SelectionBrief = selectionBriefInfo // selectionDetail.SelectionInfo = selectionInfo // selectionDetail.SelectionExample = selectionExampleInfo // selectionDetail.ProductInfo = productInfo // selectionDetail.ProductPhotoInfo = productPhotoInfo // return &selectionDetail, nil //} // 创建带货任务 func (s SelectionInfoService) CreateSelectionInfo(selection *vo.SelectionInfoCreateParam) (*string, error) { // a) 生成选品id selectionId := util.GetSelectionID() // b) 查找关联商品信息 product, err := dao.ProductDAO{}.GetProductByID(conv.MustInt64(selection.ProductId, 0)) if err != nil { return nil, err } if product == nil { return nil, errors.New("未找到关联商品") } productPhotos, err := dao.ProductPhotoDAO{}.GetProductPhotoByProductID(selection.ProductId) productInfoToJson, _ := json.Marshal(product) productPhotosToJson, _ := json.Marshal(productPhotos) // c) 选品名称 selectionName := product.BrandName + "-" + product.ProductName // d)创建选品 t := time.Now() newSelection := entity.SelectionInfo{ SelectionStatus: 1, SelectionID: selectionId, SelectionName: selectionName, ProductID: selection.ProductId, EnterpriseID: selection.EnterpriseId, Platform: selection.Platform, ProductSnap: string(productInfoToJson), ProductPhotoSnap: string(productPhotosToJson), CreatedAt: t, UpdatedAt: t, CommissionRate: 0, EstimatedCost: 0, TaskReward: 0, SettlementAmount: 0, } err = dao.SelectionInfoDAO{}.CreateSelectionInfo(newSelection) if err != nil { return nil, err } return &selectionId, nil } // 更新带货任务(样品奖励、补充信息) func (s SelectionInfoService) UpdateSelectionInfo(selectionUpdateParam *vo.SelectionInfoUpdateParam) (*string, error) { // 1. 检查该企业id和商品id有无选品 selectionID := selectionUpdateParam.SelectionID selectionInfo, err := dao.SelectionInfoDAO{}.GetSelectionInfoById(selectionID) if err != nil { return nil, err } if selectionInfo == nil { return nil, errors.New("选品不存在") } // 2. 数据准备 // a) 查找关联商品信息 product, err := dao.ProductDAO{}.GetProductByID(conv.MustInt64(selectionUpdateParam.ProductId, 0)) if err != nil { return nil, err } productPhotos, err := dao.ProductPhotoDAO{}.GetProductPhotoByProductID(conv.MustInt64(selectionUpdateParam.ProductId, 0)) productInfoToJson, _ := json.Marshal(product) productPhotosToJson, _ := json.Marshal(productPhotos) // b) 选品名称 selectionName := product.BrandName + "-" + product.ProductName // d) 任务截止时间 taskDdl := time.Time{} //赋零值 taskDdl, _ = time.ParseInLocation("2006-01-02 15:04:05", selectionUpdateParam.TaskDdl, time.Local) // f) 更新选品状态 if selectionUpdateParam.SelectionStatus != 2 && selectionUpdateParam.SelectionStatus != 7 { selectionUpdateParam.SelectionStatus = 1 } t := time.Now() updateSelection := entity.SelectionInfo{ SelectionID: selectionUpdateParam.SelectionID, SelectionStatus: selectionUpdateParam.SelectionStatus, SelectionName: selectionName, EnterpriseID: selectionUpdateParam.EnterpriseId, ProductID: selectionUpdateParam.ProductId, ContentType: selectionUpdateParam.ContentType, TaskMode: selectionUpdateParam.TaskMode, Platform: selectionUpdateParam.Platform, SampleMode: selectionUpdateParam.SampleMode, ProductUrl: selectionUpdateParam.ProductUrl, SampleNum: selectionUpdateParam.SampleNum, RemainNum: selectionUpdateParam.SampleNum, CommissionRate: selectionUpdateParam.CommissionRate, TaskReward: selectionUpdateParam.TaskReward, SettlementAmount: selectionUpdateParam.SettlementAmount, EstimatedCost: selectionInfo.EstimatedCost, SampleCondition: selectionUpdateParam.SampleCondition, RewardCondition: selectionUpdateParam.RewardCondition, TaskDdl: taskDdl, Detail: selectionUpdateParam.Detail, ProductSnap: string(productInfoToJson), ProductPhotoSnap: string(productPhotosToJson), CreatedAt: selectionInfo.CreatedAt, UpdatedAt: t, } if selectionUpdateParam.SelectionStatus == 2 { updateSelection.SubmitAt = t } if selectionUpdateParam.Status == 1 { updateSelection.Status = 1 } // 合并传入参数和数据表中原记录,若传入参数字段值为空,则将字段赋值为原记录中值 result := util.MergeStructValue(&updateSelection, selectionInfo) // 利用反射机制将interface类型转换为结构体类型 v := reflect.ValueOf(&result).Elem() if v.Kind() == reflect.Struct { updateSelection = v.Interface().(entity.SelectionInfo) //fmt.Println(p) } // c) 计算预估成本(如果有) /* var estimatedCost float64 if conv.MustInt(updateSelection.TaskMode, 0) == 1 { estimatedCost = conv.MustFloat64(updateSelection.TaskReward, 0) * conv.MustFloat64(updateSelection.SampleNum, 0) } estimatedCostToString, _ := conv.String(estimatedCost) updateSelection.EstimatedCost = estimatedCostToString */ // 3. 更新选品 err = dao.SelectionInfoDAO{}.UpdateSelectionInfo(updateSelection) if err != nil { return nil, err } // 4. 更新选品brief和示例(带货任务补充信息) if selectionUpdateParam.SecBrief != nil { // 删除已有brief err = dao.SecBriefDao{}.DeleteSecBriefBySelectionId(selectionInfo.SelectionID) if err != nil { return nil, err } // 插入新的brief for _, v := range selectionUpdateParam.SecBrief { brief := entity.SecBrief{ SelectionID: selectionInfo.SelectionID, FileUid: v.PhotoUid, FileName: v.Name, FileUrl: v.PhotoUrl, CreatedAt: time.Now(), } err = dao.SecBriefDao{}.CreateSecBrief(brief) if err != nil { return nil, err } } } if selectionUpdateParam.SecExample != nil { // 删除已有示例 err = dao.SecExampleDao{}.DeleteSecExampleBySelectionId(selectionInfo.SelectionID) if err != nil { return nil, err } // 插入新的示例 for _, v := range selectionUpdateParam.SecExample { secExample := entity.SecExample{ SelectionID: selectionInfo.SelectionID, FileUid: v.PhotoUid, FileName: v.Name, FileUrl: v.PhotoUrl, CreatedAt: time.Now(), } err = dao.SecExampleDao{}.CreateSecExample(secExample) if err != nil { return nil, err } } } println("更新带货任务的免费领样策略") // 更新带货任务的免费领样策略 if selectionUpdateParam.FreeStrategys != nil { // 1. 删除已有的免费领样策略 err = dao.FreeStrategyDao{}.DeleteFreeStrategyBySelectionId(selectionUpdateParam.SelectionID) if err != nil { return nil, err } // 2. 接收并创建新的免费领样策略 if selectionUpdateParam.SampleMode == 1 { var frees []entity.FreeStrategy for _, v := range selectionUpdateParam.FreeStrategys { free := entity.FreeStrategy{ SelectionId: selectionInfo.SelectionID, StrategyId: v.StrategyId, FansNum: v.FansNum, SaleNum: v.SaleNum, StrategyStatus: 1, EnrollNum: 0, ChooseNum: 0, } frees = append(frees, free) } err = dao.FreeStrategyDao{}.CreateFreeStrategy(frees) if err != nil { return nil, err } } } println("更新带货任务的悬赏策略") // 更新带货任务的悬赏策略 if selectionUpdateParam.RewardStrategys != nil { // 1. 删除已有的悬赏策略 err = dao.RewardStrategyDao{}.DeleteRewardStrategyBySelectionId(selectionUpdateParam.SelectionID) if err != nil { return nil, err } if selectionUpdateParam.TaskMode == 1 { var rewards []entity.RewardStrategy for _, v := range selectionUpdateParam.RewardStrategys { reward := entity.RewardStrategy{ SelectionId: selectionInfo.SelectionID, Reward: v.Reward, SaleActual: v.SaleActual, PerReward: v.PerReward, StrategyStatus: 1, } rewards = append(rewards, reward) } err = dao.RewardStrategyDao{}.CreateRewardStrategy(rewards) if err != nil { return nil, err } } } return &updateSelection.SelectionID, nil } // 电商带货任务预览 func (s SelectionInfoService) GetSelectionDetail(selectionId string, enterpriseId string) (*vo.ReSelectionDetail, error) { selectionDetail := vo.ReSelectionDetail{} selectionInfo, err := dao.SelectionInfoDAO{}.GetSelectionInfoById(selectionId) if err != nil { logrus.Errorf("[selectionDB service] call GetSelectionInfo error,err:%+v", err) return nil, err } selectionBriefInfos, err := dao.SecBriefDao{}.GetSelectionBriefInfo(selectionId) if err != nil { logrus.Errorf("[selectionDB service] call GetSelectionBriefInfo error,err:%+v", err) return nil, err } selectionExamples, err := dao.SecExampleDao{}.GetSelectionExampleInfo(selectionId) if err != nil { logrus.Errorf("[selectionDB service] call GetSelectionExampleInfo error,err:%+v", err) return nil, err } productInfo, err := dao.ProductDAO{}.GetProductBySelectionId(selectionId) if err != nil { logrus.Errorf("[selectionDB service] call GetProductInfo error,err:%+v", err) return nil, err } productPhotos, err := dao.ProductPhotoDAO{}.GetProductPhotosBySelectionId(selectionId) if err != nil { logrus.Errorf("[selectionDB service] call GetProductPhotoInfo error,err:%+v", err) return nil, err } // 查找免费领样策略 freeStrategys, err := dao.FreeStrategyDao{}.GetFreeStrategyBySelectionId(selectionId) if err != nil { logrus.Errorf("[selectionDB service] call GetFreeStrategy error,err:%+v", err) return nil, err } // 查找悬赏策略 rewardStrategys, err := dao.RewardStrategyDao{}.GetRewardStrategyBySelectionId(selectionId) if err != nil { logrus.Errorf("[selectionDB service] call GetRewardStrategy error,err:%+v", err) return nil, err } selectionDetail.SelectionBriefs = selectionBriefInfos selectionDetail.SelectionInfo = selectionInfo selectionDetail.SelectionExamples = selectionExamples selectionDetail.ProductInfo = productInfo selectionDetail.ProductPhotos = productPhotos selectionDetail.FreeStrategys = freeStrategys selectionDetail.RewardStrategys = rewardStrategys return &selectionDetail, nil }