123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- package service
- import (
- "encoding/json"
- "errors"
- "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(param *vo.SelectionInfoCreateParam) (*string, error) {
- // a) 生成选品id
- selectionId := util.GetSelectionID()
- // b) 查找关联商品信息
- product, err := dao.ProductDAO{}.GetProductByID(param.ProductId)
- if err != nil {
- return nil, err
- }
- if product == nil {
- return nil, errors.New("未找到关联商品")
- }
- productPhotos, err := dao.ProductPhotoDAO{}.GetProductPhotoByProductID(param.ProductId)
- productInfoToJson, _ := json.Marshal(product)
- productPhotosToJson, _ := json.Marshal(productPhotos)
- // c) 选品名称
- //selectionName := product.ProductName
- // d)创建选品
- // 获取定时任务配置
- infoAutoTask := entity.InfoAutoTask{}
- infoAutoTask = dao.InfoAutoTaskDao{}.GetAutoTaskLast(param.EnterpriseId)
- t := time.Now()
- newSelection := entity.SelectionInfo{
- SelectionStatus: 1,
- SelectionID: selectionId,
- ProductID: param.ProductId,
- ProductCategory: product.ProductCategory,
- EnterpriseID: param.EnterpriseId,
- SubAccountId: param.SubAccountId,
- Platform: param.Platform,
- ProductSnap: string(productInfoToJson),
- ProductPhotoSnap: string(productPhotosToJson),
- CreatedAt: t,
- UpdatedAt: t,
- AutoTaskID: infoAutoTask.AutoTaskID,
- }
- 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(selectionInfo.ProductID)
- if err != nil {
- return nil, err
- }
- productPhotos, err := dao.ProductPhotoDAO{}.GetProductPhotoByProductID(selectionInfo.ProductID)
- productInfoToJson, _ := json.Marshal(product)
- productPhotosToJson, _ := json.Marshal(productPhotos)
- // 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()
- var sampleMode, taskMode int64
- if len(selectionUpdateParam.FreeStrategys) > 0 {
- sampleMode = 1
- } else {
- sampleMode = 3
- }
- if len(selectionUpdateParam.RewardStrategys) > 0 {
- taskMode = 1
- } else {
- taskMode = 2
- }
- updateSelection := entity.SelectionInfo{
- SelectionID: selectionUpdateParam.SelectionID,
- SelectionStatus: selectionUpdateParam.SelectionStatus,
- SelectionName: selectionUpdateParam.SelectionName,
- EnterpriseID: selectionUpdateParam.EnterpriseId,
- SubAccountId: selectionUpdateParam.SubAccountId,
- ProductID: selectionUpdateParam.ProductId,
- ProductUrl: selectionUpdateParam.ProductUrl,
- TaskMode: taskMode,
- SampleMode: sampleMode,
- 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.FileUid,
- FileName: v.Name,
- FileUrl: v.FileUrl,
- CreatedAt: time.Now(),
- }
- err = dao.SecBriefDao{}.CreateSecBrief(brief)
- if err != nil {
- return nil, err
- }
- }
- }
- if selectionUpdateParam.SecMaterial != nil {
- // 删除已有示例
- err = dao.SecMaterialDao{}.DeleteSecMaterialBySelectionId(selectionInfo.SelectionID)
- if err != nil {
- return nil, err
- }
- // 插入新的示例
- for _, v := range selectionUpdateParam.SecMaterial {
- secMaterial := entity.SecMaterial{
- SelectionID: selectionInfo.SelectionID,
- FileUid: v.FileUid,
- FileName: v.Name,
- FileUrl: v.FileUrl,
- CreatedAt: time.Now(),
- }
- err = dao.SecMaterialDao{}.CreateSecMaterial(secMaterial)
- 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 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 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) (*vo.ReSelectionDetail, error) {
- reSelectionDetail := vo.ReSelectionDetail{}
- selection, err := dao.SelectionInfoDAO{}.GetSelectionInfoById(selectionId)
- if err != nil {
- logrus.Errorf("[selectionInfoDB service] call GetSelection error,err:%+v", err)
- return nil, err
- }
- // 系统信息
- reSelectionDetail.SelectionId = selectionId
- reSelectionDetail.SelectionStatus = selection.SelectionStatus
- reSelectionDetail.SelectionPlatform = selection.Platform
- reSelectionDetail.CreatedAt = selection.CreatedAt.Format("2006-01-02 15:04:05")
- reSelectionDetail.SubmitAt = selection.SubmitAt.Format("2006-01-02 15:04:05")
- var creatorName, phone string
- var rewardSum float64
- if selection.SubAccountId == 0 {
- enterprise, err := dao.EnterpriseDao{}.GetEnterprise(selection.EnterpriseID)
- if err == nil && enterprise != nil {
- creatorName = enterprise.BusinessName
- phone, err = dao.UserDao{}.GetPhoneByUserId(enterprise.UserId)
- }
- } else {
- subAccount, err := dao.SubAccountDao{}.GetSubAccount(selection.SubAccountId)
- if err == nil && subAccount != nil {
- creatorName = subAccount.SubAccountName
- phone, err = dao.UserDao{}.GetPhoneByUserId(subAccount.UserId)
- }
- }
- reSelectionDetail.CreatorName = creatorName
- reSelectionDetail.Phone = phone
- // 关联商品
- var reProduct vo.ReTaskProduct
- product, err := dao.ProductDAO{}.GetProductByID(selection.ProductID)
- if err == nil {
- photoUrl, e := dao.ProductPhotoDAO{}.GetMainPhotoByProductID(product.ProductID)
- if e != nil {
- photoUrl = ""
- }
- reProduct = vo.ReTaskProduct{
- ProductID: product.ProductID,
- ProductName: product.ProductName,
- ProductType: product.ProductType,
- ProductCategory: product.ProductCategory,
- ProductPrice: product.ProductPrice,
- ProductDetail: product.ProductDetail,
- CreatedAt: product.CreatedAt.Format("2006-01-02 15:04:05"),
- PhotoUrl: photoUrl,
- }
- }
- reSelectionDetail.ProductInfo = &reProduct
- // 样品奖励
- reSelectionDetail.TaskDdl = selection.TaskDdl.Format("2006-01-02 15:04:05")
- reSelectionDetail.SampleNum = selection.SampleNum
- var freeStrategyPreviews []*vo.FreeStrategyPreview // 领样策略
- freeStrategys, err := dao.FreeStrategyDao{}.GetFreeStrategyBySelectionId(selectionId)
- if err != nil {
- logrus.Errorf("[selectionInfoDB service] call GetFreeStrategy error,err:%+v", err)
- return nil, err
- }
- for _, freeStrategy := range freeStrategys {
- freeStrategyPreview := &vo.FreeStrategyPreview{
- StrategyId: freeStrategy.StrategyId,
- FansNum: freeStrategy.FansNum,
- SaleNum: freeStrategy.SaleNum,
- StrategyStatus: freeStrategy.StrategyStatus,
- }
- freeStrategyPreviews = append(freeStrategyPreviews, freeStrategyPreview)
- }
- reSelectionDetail.FreeStrategys = freeStrategyPreviews
- var rewardStrategyPreviews []*vo.RewardStrategyPreview // 悬赏策略
- rewardStrategys, err := dao.RewardStrategyDao{}.GetRewardStrategyBySelectionId(selectionId)
- if err != nil {
- logrus.Errorf("[selectionInfoDB service] call GetRewardStrategy error,err:%+v", err)
- return nil, err
- }
- for _, rewardStrategy := range rewardStrategys {
- rewardStrategyPreview := &vo.RewardStrategyPreview{
- Reward: rewardStrategy.Reward,
- SaleActual: rewardStrategy.SaleActual,
- PerReward: rewardStrategy.PerReward,
- StrategyStatus: rewardStrategy.StrategyStatus,
- }
- rewardStrategyPreviews = append(rewardStrategyPreviews, rewardStrategyPreview)
- }
- reSelectionDetail.FreeStrategys = freeStrategyPreviews
- reSelectionDetail.RewardStrategys = rewardStrategyPreviews
- for _, rewardStrategy := range rewardStrategys {
- rewardSum += rewardStrategy.Reward
- }
- reSelectionDetail.RewardSum = rewardSum
- // 补充信息
- selectionBriefInfos, err := dao.SecBriefDao{}.GetSelectionBriefInfo(selectionId)
- if err != nil {
- logrus.Errorf("[selectionInfoDB service] call GetSelectionBriefInfo error,err:%+v", err)
- return nil, err
- }
- selectionMaterials, err := dao.SecMaterialDao{}.GetSelectionMaterialInfo(selectionId)
- if err != nil {
- logrus.Errorf("[selectionInfoDB service] call GetSelectionMaterialInfo error,err:%+v", err)
- return nil, err
- }
- reSelectionDetail.SelectionBriefs = selectionBriefInfos
- reSelectionDetail.SelectionMaterials = selectionMaterials
- return &reSelectionDetail, nil
- }
- // 电商带货提交审核
- func (s SelectionInfoService) SelectionToReview(selectionUpdateParam *vo.SelectionInfoUpdateParam) (*string, error) {
- selectionId := selectionUpdateParam.SelectionID
- t := time.Now()
- updateSelection := entity.SelectionInfo{
- SelectionID: selectionId,
- SelectionStatus: 2,
- UpdatedAt: t,
- }
- err := dao.SelectionInfoDAO{}.UpdateSelectionInfo(updateSelection)
- if err != nil {
- return nil, err
- }
- return &selectionId, nil
- }
- // 电商带货任务列表
- func (s SelectionInfoService) GetSelectionTaskList(param *vo.SelectionSearchParam) (vo.ResultVO, error) {
- if param.Page == 0 {
- param.Page = 1
- }
- if param.PageSize == 0 {
- param.PageSize = 10
- }
- var result vo.ResultVO
- reSelectionTaskPreviews, total, err := (&dao.SelectionInfoDAO{}).GetSelectionPreviews(param)
- if err != nil {
- return result, err
- }
- for i := range reSelectionTaskPreviews {
- var creatorName string
- var productName string
- var productPrice float64
- var mainImage string
- var reward float64
- if reSelectionTaskPreviews[i].SubAccountId == 0 {
- enterprise, err := dao.EnterpriseDao{}.GetEnterprise(reSelectionTaskPreviews[i].EnterpriseId)
- if err == nil && enterprise != nil {
- creatorName = enterprise.BusinessName
- }
- } else {
- subAccount, err := dao.SubAccountDao{}.GetSubAccount(reSelectionTaskPreviews[i].SubAccountId)
- if err == nil && subAccount != nil {
- creatorName = subAccount.SubAccountName
- }
- }
- product, err := dao.ProductDAO{}.GetProductByID(reSelectionTaskPreviews[i].ProductId)
- if err == nil && product != nil {
- productName = product.ProductName
- productPrice = product.ProductPrice
- }
- mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(reSelectionTaskPreviews[i].ProductId)
- rewardStrategys, err := dao.RewardStrategyDao{}.GetRewardStrategyBySelectionId(reSelectionTaskPreviews[i].SelectionId)
- for _, rewardStrategy := range rewardStrategys {
- reward += rewardStrategy.Reward
- }
- reSelectionTaskPreviews[i].CreatorName = creatorName
- reSelectionTaskPreviews[i].ProductName = productName
- reSelectionTaskPreviews[i].ProductPrice = productPrice
- reSelectionTaskPreviews[i].MainImage = mainImage
- reSelectionTaskPreviews[i].Reward = reward
- }
- result = vo.ResultVO{
- Page: param.Page,
- PageSize: param.PageSize,
- Total: total,
- Data: reSelectionTaskPreviews,
- }
- return result, nil
- }
- // 删除带货任务
- func (s SelectionInfoService) DeleteSelection(selectionId string) (*string, error) {
- res, err := dao.SelectionInfoDAO{}.DeleteSelection(selectionId)
- if err != nil {
- logrus.Errorf("[projectDB service] call DeleteSelection error,err:%+v", err)
- return res, err
- }
- return res, nil
- }
- // 草稿箱——电商带货
- func (s SelectionInfoService) GetSelectionDraftList(param *vo.SelectionDraftParam) (vo.ResultVO, error) {
- if param.Page == 0 {
- param.Page = 1
- }
- if param.PageSize == 0 {
- param.PageSize = 10
- }
- var result vo.ResultVO
- reSelectionTaskPreviews, total, err := (&dao.SelectionInfoDAO{}).GetSelectionDraftList(param)
- if err != nil {
- return result, err
- }
- for i := range reSelectionTaskPreviews {
- var creatorName string
- var productName string
- var productPrice float64
- var mainImage string
- if reSelectionTaskPreviews[i].SubAccountId == 0 {
- enterprise, err := dao.EnterpriseDao{}.GetEnterprise(reSelectionTaskPreviews[i].EnterpriseId)
- if err == nil && enterprise != nil {
- creatorName = enterprise.BusinessName
- }
- } else {
- subAccount, err := dao.SubAccountDao{}.GetSubAccount(reSelectionTaskPreviews[i].SubAccountId)
- if err == nil && subAccount != nil {
- creatorName = subAccount.SubAccountName
- }
- }
- product, err := dao.ProductDAO{}.GetProductByID(reSelectionTaskPreviews[i].ProductId)
- if err == nil && product != nil {
- productName = product.ProductName
- productPrice = product.ProductPrice
- }
- mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(reSelectionTaskPreviews[i].ProductId)
- reSelectionTaskPreviews[i].CreatorName = creatorName
- reSelectionTaskPreviews[i].ProductName = productName
- reSelectionTaskPreviews[i].ProductPrice = productPrice
- reSelectionTaskPreviews[i].MainImage = mainImage
- }
- result = vo.ResultVO{
- Page: param.Page,
- PageSize: param.PageSize,
- Total: total,
- Data: reSelectionTaskPreviews,
- }
- return result, nil
- }
|