selection_info_service.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/caixw/lib.go/conv"
  6. "github.com/sirupsen/logrus"
  7. "reflect"
  8. "time"
  9. "youngee_b_api/app/dao"
  10. "youngee_b_api/app/entity"
  11. "youngee_b_api/app/util"
  12. "youngee_b_api/app/vo"
  13. )
  14. type SelectionInfoService struct{}
  15. //func (s *SelectionInfoService) GetSelectionInfo(ctx *gin.Context, selectionId string) (*http_model.SelectionDetail, error) {
  16. // selectionDetail := http_model.SelectionDetail{}
  17. // selectionInfo, err := db.GetSelectionById(ctx, selectionId)
  18. //
  19. // if err != nil {
  20. // logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionInfo error,err:%+v", err)
  21. // return nil, err
  22. // }
  23. // selectionBriefInfo, err := db.GetSelectionBriefInfo(ctx, selectionId)
  24. // if err != nil {
  25. // logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionBriefInfo error,err:%+v", err)
  26. // return nil, err
  27. // }
  28. // selectionExampleInfo, err := db.GetSelectionExampleInfo(ctx, selectionId)
  29. // if err != nil {
  30. // logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionExampleInfo error,err:%+v", err)
  31. // return nil, err
  32. // }
  33. // productInfo, err := db.GetProductInfoBySelectionId(ctx, selectionId)
  34. // if err != nil {
  35. // logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductInfo error,err:%+v", err)
  36. // return nil, err
  37. // }
  38. // productPhotoInfo, err := db.GetProductPhotoInfoBySelectionId(ctx, selectionId)
  39. // if err != nil {
  40. // logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductPhotoInfo error,err:%+v", err)
  41. // return nil, err
  42. // }
  43. // selectionDetail.SelectionBrief = selectionBriefInfo
  44. // selectionDetail.SelectionInfo = selectionInfo
  45. // selectionDetail.SelectionExample = selectionExampleInfo
  46. // selectionDetail.ProductInfo = productInfo
  47. // selectionDetail.ProductPhotoInfo = productPhotoInfo
  48. // return &selectionDetail, nil
  49. //}
  50. // 创建带货任务
  51. func (s SelectionInfoService) CreateSelectionInfo(param *vo.SelectionInfoCreateParam) (*string, error) {
  52. // a) 生成选品id
  53. selectionId := util.GetSelectionID()
  54. // b) 查找关联商品信息
  55. product, err := dao.ProductDAO{}.GetProductByID(conv.MustInt64(param.ProductId, 0))
  56. if err != nil {
  57. return nil, err
  58. }
  59. if product == nil {
  60. return nil, errors.New("未找到关联商品")
  61. }
  62. productPhotos, err := dao.ProductPhotoDAO{}.GetProductPhotoByProductID(param.ProductId)
  63. productInfoToJson, _ := json.Marshal(product)
  64. productPhotosToJson, _ := json.Marshal(productPhotos)
  65. // c) 选品名称
  66. //selectionName := product.ProductName
  67. // d)创建选品
  68. t := time.Now()
  69. newSelection := entity.SelectionInfo{
  70. SelectionStatus: 1,
  71. SelectionID: selectionId,
  72. ProductID: param.ProductId,
  73. EnterpriseID: param.EnterpriseId,
  74. Platform: param.Platform,
  75. ProductSnap: string(productInfoToJson),
  76. ProductPhotoSnap: string(productPhotosToJson),
  77. CreatedAt: t,
  78. UpdatedAt: t,
  79. CommissionRate: 0,
  80. EstimatedCost: 0,
  81. TaskReward: 0,
  82. SettlementAmount: 0,
  83. }
  84. err = dao.SelectionInfoDAO{}.CreateSelectionInfo(newSelection)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return &selectionId, nil
  89. }
  90. // 更新带货任务(样品奖励、补充信息)
  91. func (s SelectionInfoService) UpdateSelectionInfo(selectionUpdateParam *vo.SelectionInfoUpdateParam) (*string, error) {
  92. // 1. 检查该企业id和商品id有无选品
  93. selectionID := selectionUpdateParam.SelectionID
  94. selectionInfo, err := dao.SelectionInfoDAO{}.GetSelectionInfoById(selectionID)
  95. if err != nil {
  96. return nil, err
  97. }
  98. if selectionInfo == nil {
  99. return nil, errors.New("选品不存在")
  100. }
  101. // 2. 数据准备
  102. // a) 查找关联商品信息
  103. product, err := dao.ProductDAO{}.GetProductByID(selectionInfo.ProductID)
  104. if err != nil {
  105. return nil, err
  106. }
  107. productPhotos, err := dao.ProductPhotoDAO{}.GetProductPhotoByProductID(selectionInfo.ProductID)
  108. productInfoToJson, _ := json.Marshal(product)
  109. productPhotosToJson, _ := json.Marshal(productPhotos)
  110. // d) 任务截止时间
  111. taskDdl := time.Time{} //赋零值
  112. taskDdl, _ = time.ParseInLocation("2006-01-02 15:04:05", selectionUpdateParam.TaskDdl, time.Local)
  113. // f) 更新选品状态
  114. if selectionUpdateParam.SelectionStatus != 2 && selectionUpdateParam.SelectionStatus != 7 {
  115. selectionUpdateParam.SelectionStatus = 1
  116. }
  117. t := time.Now()
  118. updateSelection := entity.SelectionInfo{
  119. SelectionID: selectionUpdateParam.SelectionID,
  120. SelectionStatus: selectionUpdateParam.SelectionStatus,
  121. SelectionName: selectionUpdateParam.SelectionName,
  122. EnterpriseID: selectionUpdateParam.EnterpriseId,
  123. ProductID: selectionUpdateParam.ProductId,
  124. //ContentType: selectionUpdateParam.ContentType,
  125. TaskMode: selectionUpdateParam.TaskMode,
  126. //Platform: selectionUpdateParam.Platform,
  127. SampleMode: selectionUpdateParam.SampleMode,
  128. ProductUrl: selectionUpdateParam.ProductUrl,
  129. SampleNum: selectionUpdateParam.SampleNum,
  130. RemainNum: selectionUpdateParam.SampleNum,
  131. CommissionRate: selectionUpdateParam.CommissionRate,
  132. TaskReward: selectionUpdateParam.TaskReward,
  133. SettlementAmount: selectionUpdateParam.SettlementAmount,
  134. EstimatedCost: selectionInfo.EstimatedCost,
  135. SampleCondition: selectionUpdateParam.SampleCondition,
  136. RewardCondition: selectionUpdateParam.RewardCondition,
  137. TaskDdl: taskDdl,
  138. Detail: selectionUpdateParam.Detail,
  139. ProductSnap: string(productInfoToJson),
  140. ProductPhotoSnap: string(productPhotosToJson),
  141. CreatedAt: selectionInfo.CreatedAt,
  142. UpdatedAt: t,
  143. }
  144. if selectionUpdateParam.SelectionStatus == 2 {
  145. updateSelection.SubmitAt = t
  146. }
  147. if selectionUpdateParam.Status == 1 {
  148. updateSelection.Status = 1
  149. }
  150. // 合并传入参数和数据表中原记录,若传入参数字段值为空,则将字段赋值为原记录中值
  151. result := util.MergeStructValue(&updateSelection, selectionInfo)
  152. // 利用反射机制将interface类型转换为结构体类型
  153. v := reflect.ValueOf(&result).Elem()
  154. if v.Kind() == reflect.Struct {
  155. updateSelection = v.Interface().(entity.SelectionInfo)
  156. //fmt.Println(p)
  157. }
  158. // c) 计算预估成本(如果有)
  159. /*
  160. var estimatedCost float64
  161. if conv.MustInt(updateSelection.TaskMode, 0) == 1 {
  162. estimatedCost = conv.MustFloat64(updateSelection.TaskReward, 0) * conv.MustFloat64(updateSelection.SampleNum, 0)
  163. }
  164. estimatedCostToString, _ := conv.String(estimatedCost)
  165. updateSelection.EstimatedCost = estimatedCostToString
  166. */
  167. // 3. 更新选品
  168. err = dao.SelectionInfoDAO{}.UpdateSelectionInfo(updateSelection)
  169. if err != nil {
  170. return nil, err
  171. }
  172. // 4. 更新选品brief和示例(带货任务补充信息)
  173. if selectionUpdateParam.SecBrief != nil {
  174. // 删除已有brief
  175. err = dao.SecBriefDao{}.DeleteSecBriefBySelectionId(selectionInfo.SelectionID)
  176. if err != nil {
  177. return nil, err
  178. }
  179. // 插入新的brief
  180. for _, v := range selectionUpdateParam.SecBrief {
  181. brief := entity.SecBrief{
  182. SelectionID: selectionInfo.SelectionID,
  183. FileUid: v.PhotoUid,
  184. FileName: v.Name,
  185. FileUrl: v.PhotoUrl,
  186. CreatedAt: time.Now(),
  187. }
  188. err = dao.SecBriefDao{}.CreateSecBrief(brief)
  189. if err != nil {
  190. return nil, err
  191. }
  192. }
  193. }
  194. if selectionUpdateParam.SecExample != nil {
  195. // 删除已有示例
  196. err = dao.SecExampleDao{}.DeleteSecExampleBySelectionId(selectionInfo.SelectionID)
  197. if err != nil {
  198. return nil, err
  199. }
  200. // 插入新的示例
  201. for _, v := range selectionUpdateParam.SecExample {
  202. secExample := entity.SecExample{
  203. SelectionID: selectionInfo.SelectionID,
  204. FileUid: v.PhotoUid,
  205. FileName: v.Name,
  206. FileUrl: v.PhotoUrl,
  207. CreatedAt: time.Now(),
  208. }
  209. err = dao.SecExampleDao{}.CreateSecExample(secExample)
  210. if err != nil {
  211. return nil, err
  212. }
  213. }
  214. }
  215. println("更新带货任务的免费领样策略")
  216. // 更新带货任务的免费领样策略
  217. if selectionUpdateParam.FreeStrategys != nil {
  218. // 1. 删除已有的免费领样策略
  219. err = dao.FreeStrategyDao{}.DeleteFreeStrategyBySelectionId(selectionUpdateParam.SelectionID)
  220. if err != nil {
  221. return nil, err
  222. }
  223. // 2. 接收并创建新的免费领样策略
  224. if selectionUpdateParam.SampleMode == 1 {
  225. var frees []entity.FreeStrategy
  226. for _, v := range selectionUpdateParam.FreeStrategys {
  227. free := entity.FreeStrategy{
  228. SelectionId: selectionInfo.SelectionID,
  229. StrategyId: v.StrategyId,
  230. FansNum: v.FansNum,
  231. SaleNum: v.SaleNum,
  232. StrategyStatus: 1,
  233. EnrollNum: 0,
  234. ChooseNum: 0,
  235. }
  236. frees = append(frees, free)
  237. }
  238. err = dao.FreeStrategyDao{}.CreateFreeStrategy(frees)
  239. if err != nil {
  240. return nil, err
  241. }
  242. }
  243. }
  244. println("更新带货任务的悬赏策略")
  245. // 更新带货任务的悬赏策略
  246. if selectionUpdateParam.RewardStrategys != nil {
  247. // 1. 删除已有的悬赏策略
  248. err = dao.RewardStrategyDao{}.DeleteRewardStrategyBySelectionId(selectionUpdateParam.SelectionID)
  249. if err != nil {
  250. return nil, err
  251. }
  252. if selectionUpdateParam.TaskMode == 1 {
  253. var rewards []entity.RewardStrategy
  254. for _, v := range selectionUpdateParam.RewardStrategys {
  255. reward := entity.RewardStrategy{
  256. SelectionId: selectionInfo.SelectionID,
  257. Reward: v.Reward,
  258. SaleActual: v.SaleActual,
  259. PerReward: v.PerReward,
  260. StrategyStatus: 1,
  261. }
  262. rewards = append(rewards, reward)
  263. }
  264. err = dao.RewardStrategyDao{}.CreateRewardStrategy(rewards)
  265. if err != nil {
  266. return nil, err
  267. }
  268. }
  269. }
  270. return &updateSelection.SelectionID, nil
  271. }
  272. // 电商带货任务预览
  273. func (s SelectionInfoService) GetSelectionDetail(selectionId string, enterpriseId string) (*vo.ReSelectionDetail, error) {
  274. selectionDetail := vo.ReSelectionDetail{}
  275. selectionInfo, err := dao.SelectionInfoDAO{}.GetSelectionInfoById(selectionId)
  276. if err != nil {
  277. logrus.Errorf("[selectionDB service] call GetSelectionInfo error,err:%+v", err)
  278. return nil, err
  279. }
  280. selectionBriefInfos, err := dao.SecBriefDao{}.GetSelectionBriefInfo(selectionId)
  281. if err != nil {
  282. logrus.Errorf("[selectionDB service] call GetSelectionBriefInfo error,err:%+v", err)
  283. return nil, err
  284. }
  285. selectionExamples, err := dao.SecExampleDao{}.GetSelectionExampleInfo(selectionId)
  286. if err != nil {
  287. logrus.Errorf("[selectionDB service] call GetSelectionExampleInfo error,err:%+v", err)
  288. return nil, err
  289. }
  290. productInfo, err := dao.ProductDAO{}.GetProductBySelectionId(selectionId)
  291. if err != nil {
  292. logrus.Errorf("[selectionDB service] call GetProductInfo error,err:%+v", err)
  293. return nil, err
  294. }
  295. productPhotos, err := dao.ProductPhotoDAO{}.GetProductPhotosBySelectionId(selectionId)
  296. if err != nil {
  297. logrus.Errorf("[selectionDB service] call GetProductPhotoInfo error,err:%+v", err)
  298. return nil, err
  299. }
  300. // 查找免费领样策略
  301. freeStrategys, err := dao.FreeStrategyDao{}.GetFreeStrategyBySelectionId(selectionId)
  302. if err != nil {
  303. logrus.Errorf("[selectionDB service] call GetFreeStrategy error,err:%+v", err)
  304. return nil, err
  305. }
  306. // 查找悬赏策略
  307. rewardStrategys, err := dao.RewardStrategyDao{}.GetRewardStrategyBySelectionId(selectionId)
  308. if err != nil {
  309. logrus.Errorf("[selectionDB service] call GetRewardStrategy error,err:%+v", err)
  310. return nil, err
  311. }
  312. selectionDetail.SelectionBriefs = selectionBriefInfos
  313. selectionDetail.SelectionInfo = selectionInfo
  314. selectionDetail.SelectionExamples = selectionExamples
  315. selectionDetail.ProductInfo = productInfo
  316. selectionDetail.ProductPhotos = productPhotos
  317. selectionDetail.FreeStrategys = freeStrategys
  318. selectionDetail.RewardStrategys = rewardStrategys
  319. return &selectionDetail, nil
  320. }