selection_info_service.go 11 KB

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