123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 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(param *vo.SelectionInfoCreateParam) (*string, error) {
- // a) 生成选品id
- selectionId := util.GetSelectionID()
- // b) 查找关联商品信息
- product, err := dao.ProductDAO{}.GetProductByID(conv.MustInt64(param.ProductId, 0))
- 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)创建选品
- t := time.Now()
- newSelection := entity.SelectionInfo{
- SelectionStatus: 1,
- SelectionID: selectionId,
- ProductID: param.ProductId,
- EnterpriseID: param.EnterpriseId,
- Platform: param.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(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()
- updateSelection := entity.SelectionInfo{
- SelectionID: selectionUpdateParam.SelectionID,
- SelectionStatus: selectionUpdateParam.SelectionStatus,
- SelectionName: selectionUpdateParam.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
- }
|