selection_info_service.go 12 KB

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