selection.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package selection_service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "github.com/gin-gonic/gin"
  7. "github.com/sirupsen/logrus"
  8. "reflect"
  9. "time"
  10. "youngee_b_api/db"
  11. "youngee_b_api/model/common_model"
  12. "youngee_b_api/model/gorm_model"
  13. "youngee_b_api/model/http_model"
  14. "youngee_b_api/pack"
  15. "youngee_b_api/util"
  16. "github.com/caixw/lib.go/conv"
  17. )
  18. var Selection *selection
  19. type selection struct {
  20. }
  21. func (*selection) Create(ctx context.Context, request http_model.CreateSelectionRequest, enterpriseId string) (*http_model.CreateSelectionData, error) {
  22. // 1. 检查该企业id和商品id有无选品
  23. selectionInfo, err := db.GetSelectionByEnterpiseIdAndProductId(ctx, enterpriseId, conv.MustInt(request.ProductId, 0))
  24. if err != nil {
  25. return nil, err
  26. }
  27. if selectionInfo != nil {
  28. return nil, errors.New("该商品下选品已存在")
  29. }
  30. // 2. 数据准备
  31. // a) 生成选品id
  32. selectionId := util.GetSelectionID()
  33. // b) 查找关联商品信息
  34. product, err := db.GetProductByID(ctx, conv.MustInt64(request.ProductId, 0))
  35. if err != nil {
  36. return nil, err
  37. }
  38. productPhotos, err := db.GetProductPhotoByProductID(ctx, conv.MustInt64(request.ProductId, 0))
  39. productInfoToJson, _ := json.Marshal(product)
  40. productPhotosToJson, _ := json.Marshal(productPhotos)
  41. // c) 选品名称
  42. selectionName := product.BrandName + "-" + product.ProductName
  43. // 3. 创建选品
  44. newSelection := gorm_model.YounggeeSelectionInfo{
  45. SelectionID: selectionId,
  46. SelectionName: selectionName,
  47. ProductID: conv.MustInt(request.ProductId, 0),
  48. EnterpriseID: enterpriseId,
  49. Platform: conv.MustInt(request.Platform, 0),
  50. ProductSnap: string(productInfoToJson),
  51. ProductPhotoSnap: string(productPhotosToJson),
  52. CreatedAt: time.Now(),
  53. }
  54. err = db.CreateSelection(ctx, newSelection)
  55. if err != nil {
  56. return nil, err
  57. }
  58. res := &http_model.CreateSelectionData{
  59. SelectionId: selectionId,
  60. }
  61. return res, nil
  62. }
  63. func (*selection) Update(ctx context.Context, request http_model.UpdateSelectionRequest, enterpriseId string) (*http_model.UpdateSelectionData, error) {
  64. // 1. 检查该企业id和商品id有无选品
  65. selectionInfo, err := db.GetSelectionByEnterpiseIdAndProductId(ctx, enterpriseId, conv.MustInt(request.ProductId, 0))
  66. if err != nil {
  67. return nil, err
  68. }
  69. if selectionInfo == nil {
  70. return nil, errors.New("选品不存在")
  71. }
  72. // 2. 数据准备
  73. // a) 查找关联商品信息
  74. product, err := db.GetProductByID(ctx, conv.MustInt64(request.ProductId, 0))
  75. if err != nil {
  76. return nil, err
  77. }
  78. productPhotos, err := db.GetProductPhotoByProductID(ctx, conv.MustInt64(request.ProductId, 0))
  79. productInfoToJson, _ := json.Marshal(product)
  80. productPhotosToJson, _ := json.Marshal(productPhotos)
  81. // b) 选品名称
  82. selectionName := product.BrandName + "-" + product.ProductName
  83. // c) 计算预估成本(如果有)
  84. var estimatedCost float64
  85. if conv.MustInt(request.TaskMode, 0) == 1 {
  86. estimatedCost = conv.MustFloat64(request.TaskReward, 0) * conv.MustFloat64(request.SampleNum, 0)
  87. }
  88. estimatedCostToString, _ := conv.String(estimatedCost)
  89. // d) 任务截止时间
  90. taskDdl := time.Time{} //赋零值
  91. if request.TaskDdl != "" {
  92. taskDdl, _ = time.ParseInLocation("2006-01-02 15:04:05", request.TaskDdl, time.Local)
  93. }
  94. updateSelection := gorm_model.YounggeeSelectionInfo{
  95. SelectionID: request.SelectionID,
  96. SelectionName: selectionName,
  97. EnterpriseID: enterpriseId,
  98. ProductID: conv.MustInt(request.ProductId, 0),
  99. ContentType: conv.MustInt(request.ContentType, 0),
  100. SelectionStatus: 1,
  101. TaskMode: conv.MustInt(request.TaskMode, 0),
  102. Platform: conv.MustInt(request.Platform, 0),
  103. SampleMode: conv.MustInt(request.SampleMode, 0),
  104. ProductUrl: request.ProductUrl,
  105. SampleNum: conv.MustInt(request.SampleNum, 0),
  106. RemainNum: conv.MustInt(request.SampleNum, 0),
  107. CommissionRate: conv.MustInt(request.CommissionRate, 0),
  108. EstimatedCost: estimatedCostToString,
  109. SampleCondition: request.SampleCondition,
  110. RewardCondition: request.RewardCondition,
  111. TaskDdl: taskDdl,
  112. Detail: request.Detail,
  113. ProductSnap: string(productInfoToJson),
  114. ProductPhotoSnap: string(productPhotosToJson),
  115. CreatedAt: selectionInfo.CreatedAt,
  116. UpdatedAt: time.Now(),
  117. SubmitAt: time.Now(),
  118. }
  119. // 合并传入参数和数据表中原记录,若传入参数字段值为空,则将字段赋值为原记录中值
  120. result := util.MergeStructValue(&updateSelection, &selectionInfo)
  121. // 利用反射机制将interface类型转换为结构体类型
  122. v := reflect.ValueOf(result).Elem()
  123. if v.Kind() == reflect.Struct {
  124. updateSelection = v.Interface().(gorm_model.YounggeeSelectionInfo)
  125. //fmt.Println(p)
  126. }
  127. // 3. 更新选品
  128. err = db.UpdateSelection(ctx, updateSelection)
  129. if err != nil {
  130. return nil, err
  131. }
  132. res := &http_model.UpdateSelectionData{
  133. SelectionId: updateSelection.SelectionID,
  134. }
  135. return res, nil
  136. }
  137. func (*selection) Pay(ctx context.Context, request http_model.PaySelectionRequest, enterpriseId string) (*http_model.PaySelectionData, error) {
  138. // 校验
  139. // 1. 账户余额是否足够
  140. enterprise, err := db.GetEnterpriseByEnterpriseID(ctx, enterpriseId)
  141. if err != nil {
  142. return nil, err
  143. }
  144. if enterprise.AvailableBalance < request.PayMoney {
  145. return nil, errors.New("账户余额不足")
  146. }
  147. // 2. 选品项目状态是否正确
  148. selectionInfo, err := db.GetSelectionById(ctx, request.SelectionId)
  149. if err != nil {
  150. return nil, err
  151. }
  152. if selectionInfo == nil {
  153. return nil, errors.New("选品不存在")
  154. }
  155. if selectionInfo.SelectionStatus != 4 {
  156. return nil, errors.New("选品状态有误")
  157. }
  158. // 支付
  159. err = db.PaySelection(ctx, enterpriseId, request.PayMoney, request.SelectionId)
  160. if err != nil {
  161. return nil, err
  162. }
  163. res := &http_model.PaySelectionData{
  164. SelectionId: request.SelectionId,
  165. }
  166. return res, nil
  167. }
  168. func (s *selection) GetAllSelection(ctx context.Context, enterpriseID string, pageSize, pageNum int64, conditions *common_model.SelectionConditions) (*http_model.SelectionData, error) {
  169. SelectionList, total, err := db.GetSelectionList(ctx, enterpriseID, pageSize, pageNum, conditions)
  170. if err != nil {
  171. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetAllSelection error,err:%+v", err)
  172. return nil, err
  173. }
  174. SelectionListData := new(http_model.SelectionData)
  175. SelectionListData.SelectionInfo = pack.MGormSelectionToHttpSelectionPreview(SelectionList)
  176. SelectionListData.Total = conv.MustString(total, "")
  177. return SelectionListData, nil
  178. }
  179. func (s *selection) GetSelectionDetail(ctx *gin.Context, selectionId, enterpriseID string) (*http_model.SelectionDetail, error) {
  180. selectionDetail := http_model.SelectionDetail{}
  181. selectionInfo, err := db.GetSelectionById(ctx, selectionId)
  182. if err != nil {
  183. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionInfo error,err:%+v", err)
  184. return nil, err
  185. }
  186. selectionBriefInfo, err := db.GetSelectionBriefInfo(ctx, selectionId)
  187. if err != nil {
  188. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionBriefInfo error,err:%+v", err)
  189. return nil, err
  190. }
  191. selectionExampleInfo, err := db.GetSelectionExampleInfo(ctx, selectionId)
  192. if err != nil {
  193. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionExampleInfo error,err:%+v", err)
  194. return nil, err
  195. }
  196. productInfo, err := db.GetProductInfoBySelectionId(ctx, selectionId)
  197. if err != nil {
  198. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductInfo error,err:%+v", err)
  199. return nil, err
  200. }
  201. productPhotoInfo, err := db.GetProductPhotoInfoBySelectionId(ctx, selectionId)
  202. if err != nil {
  203. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductPhotoInfo error,err:%+v", err)
  204. return nil, err
  205. }
  206. selectionDetail.SelectionBrief = selectionBriefInfo
  207. selectionDetail.SelectionInfo = selectionInfo
  208. selectionDetail.SelectionExample = selectionExampleInfo
  209. selectionDetail.ProductInfo = productInfo
  210. selectionDetail.ProductPhotoInfo = productPhotoInfo
  211. return &selectionDetail, nil
  212. }