selection.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. taskDdl := time.Time{} //赋零值
  45. taskDdl, _ = time.ParseInLocation("2006-01-02 15:04:05", "2026-01-01 08:00:00", time.Local)
  46. newSelection := gorm_model.YounggeeSelectionInfo{
  47. SelectionID: selectionId,
  48. SelectionName: selectionName,
  49. SelectionStatus: 1,
  50. ProductID: conv.MustInt(request.ProductId, 0),
  51. EnterpriseID: enterpriseId,
  52. Platform: conv.MustInt(request.Platform, 0),
  53. ProductSnap: string(productInfoToJson),
  54. ProductPhotoSnap: string(productPhotosToJson),
  55. CreatedAt: time.Now(),
  56. SubmitAt: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
  57. PassAt: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
  58. PayAt: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
  59. FinishAt: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
  60. TaskDdl: taskDdl,
  61. EstimatedCost: "0",
  62. TaskReward: "0",
  63. SettlementAmount: "0",
  64. }
  65. //Selection := gorm_model.YounggeeSelectionInfo{}
  66. err = db.CreateSelection(ctx, newSelection)
  67. if err != nil {
  68. return nil, err
  69. }
  70. res := &http_model.CreateSelectionData{
  71. SelectionId: selectionId,
  72. }
  73. return res, nil
  74. }
  75. func (*selection) Update(ctx context.Context, request http_model.UpdateSelectionRequest, enterpriseId string) (*http_model.UpdateSelectionData, error) {
  76. // 1. 检查该选品是否存在
  77. selectionInfo, err := db.GetSelectionById(ctx, request.SelectionID)
  78. if err != nil {
  79. return nil, err
  80. }
  81. if selectionInfo == nil {
  82. return nil, errors.New("选品不存在")
  83. }
  84. // 2. 数据准备
  85. // a) 查找关联商品信息
  86. product, err := db.GetProductByID(ctx, conv.MustInt64(request.ProductId, 0))
  87. if err != nil {
  88. return nil, err
  89. }
  90. productPhotos, err := db.GetProductPhotoByProductID(ctx, conv.MustInt64(request.ProductId, 0))
  91. productInfoToJson, _ := json.Marshal(product)
  92. productPhotosToJson, _ := json.Marshal(productPhotos)
  93. // b) 选品名称
  94. selectionName := product.BrandName + "-" + product.ProductName
  95. // c) 计算预估成本(如果有)
  96. var estimatedCost float64
  97. if conv.MustInt(request.TaskMode, 0) == 1 {
  98. estimatedCost = conv.MustFloat64(request.TaskReward, 0) * conv.MustFloat64(request.SampleNum, 0)
  99. }
  100. estimatedCostToString, _ := conv.String(estimatedCost)
  101. // d) 任务截止时间
  102. taskDdl := time.Time{} //赋零值
  103. taskDdl, _ = time.ParseInLocation("2006-01-02 15:04:05", request.TaskDdl, time.Local)
  104. // f) 更新选品状态
  105. if request.SelectionStatus != 2 {
  106. request.SelectionStatus = 1
  107. }
  108. updateSelection := gorm_model.YounggeeSelectionInfo{
  109. SelectionID: request.SelectionID,
  110. SelectionStatus: request.SelectionStatus,
  111. SelectionName: selectionName,
  112. EnterpriseID: enterpriseId,
  113. ProductID: conv.MustInt(request.ProductId, 0),
  114. ContentType: conv.MustInt(request.ContentType, 0),
  115. TaskMode: conv.MustInt(request.TaskMode, 0),
  116. Platform: conv.MustInt(request.Platform, 0),
  117. SampleMode: conv.MustInt(request.SampleMode, 0),
  118. ProductUrl: request.ProductUrl,
  119. SampleNum: conv.MustInt(request.SampleNum, 0),
  120. RemainNum: conv.MustInt(request.SampleNum, 0),
  121. CommissionRate: conv.MustInt(request.CommissionRate, 0),
  122. TaskReward: conv.MustString(request.TaskReward, "0"),
  123. SettlementAmount: conv.MustString(request.SettlementAmount, "0"),
  124. EstimatedCost: estimatedCostToString,
  125. SampleCondition: request.SampleCondition,
  126. RewardCondition: request.RewardCondition,
  127. TaskDdl: taskDdl,
  128. Detail: request.Detail,
  129. ProductSnap: string(productInfoToJson),
  130. ProductPhotoSnap: string(productPhotosToJson),
  131. CreatedAt: selectionInfo.CreatedAt,
  132. UpdatedAt: time.Now(),
  133. //SubmitAt: time.Now(),
  134. }
  135. // 合并传入参数和数据表中原记录,若传入参数字段值为空,则将字段赋值为原记录中值
  136. //fmt.Printf("MergeTest %+v %+v", updateSelection, selectionInfo)
  137. result := util.MergeStructValue(&updateSelection, selectionInfo)
  138. // 利用反射机制将interface类型转换为结构体类型
  139. v := reflect.ValueOf(&result).Elem()
  140. if v.Kind() == reflect.Struct {
  141. updateSelection = v.Interface().(gorm_model.YounggeeSelectionInfo)
  142. //fmt.Println(p)
  143. }
  144. // 3. 更新选品
  145. err = db.UpdateSelection(ctx, updateSelection)
  146. if err != nil {
  147. return nil, err
  148. }
  149. // 4. 更新选品brief和示例
  150. if request.SecBrief != nil {
  151. // 删除已有brief
  152. err = db.DeleteSecBriefBySelectionId(ctx, selectionInfo.SelectionID)
  153. if err != nil {
  154. return nil, err
  155. }
  156. // 插入新的biref
  157. for _, v := range request.SecBrief {
  158. brief := gorm_model.YounggeeSecBrief{
  159. SelectionID: selectionInfo.SelectionID,
  160. FileUid: v.PhotoUid,
  161. FileName: v.Name,
  162. FileUrl: v.PhotoUrl,
  163. CreatedAt: time.Now(),
  164. }
  165. err = db.CreateSecBrief(ctx, brief)
  166. if err != nil {
  167. return nil, err
  168. }
  169. }
  170. }
  171. if request.SecExample != nil {
  172. // 删除已有示例
  173. err = db.DeleteSecExampleBySelectionId(ctx, selectionInfo.SelectionID)
  174. if err != nil {
  175. return nil, err
  176. }
  177. // 插入新的示例
  178. for _, v := range request.SecExample {
  179. Example := gorm_model.YounggeeSecExample{
  180. SelectionID: selectionInfo.SelectionID,
  181. FileUid: v.PhotoUid,
  182. FileName: v.Name,
  183. FileUrl: v.PhotoUrl,
  184. CreatedAt: time.Now(),
  185. }
  186. err = db.CreateSecExample(ctx, Example)
  187. if err != nil {
  188. return nil, err
  189. }
  190. }
  191. }
  192. res := &http_model.UpdateSelectionData{
  193. SelectionId: updateSelection.SelectionID,
  194. }
  195. return res, nil
  196. }
  197. func (*selection) Pay(ctx context.Context, request http_model.PaySelectionRequest, enterpriseId string) (*http_model.PaySelectionData, error) {
  198. // 校验
  199. // 1. 账户余额是否足够
  200. enterprise, err := db.GetEnterpriseByEnterpriseID(ctx, enterpriseId)
  201. if err != nil {
  202. return nil, err
  203. }
  204. if enterprise.AvailableBalance < request.PayMoney {
  205. return nil, errors.New("账户余额不足")
  206. }
  207. // 2. 选品项目状态是否正确
  208. selectionInfo, err := db.GetSelectionById(ctx, request.SelectionId)
  209. if err != nil {
  210. return nil, err
  211. }
  212. if selectionInfo == nil {
  213. return nil, errors.New("选品不存在")
  214. }
  215. if selectionInfo.SelectionStatus != 4 {
  216. return nil, errors.New("选品状态有误")
  217. }
  218. // 支付
  219. err = db.PaySelection(ctx, enterpriseId, request.PayMoney, request.SelectionId)
  220. if err != nil {
  221. return nil, err
  222. }
  223. res := &http_model.PaySelectionData{
  224. SelectionId: request.SelectionId,
  225. }
  226. return res, nil
  227. }
  228. func (s *selection) GetAllSelection(ctx context.Context, enterpriseID string, pageSize, pageNum int64, conditions *common_model.SelectionConditions) (*http_model.SelectionData, error) {
  229. SelectionList, total, err := db.GetSelectionList(ctx, enterpriseID, pageSize, pageNum, conditions)
  230. if err != nil {
  231. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetAllSelection error,err:%+v", err)
  232. return nil, err
  233. }
  234. SelectionListData := new(http_model.SelectionData)
  235. SelectionListData.SelectionInfo = pack.MGormSelectionToHttpSelectionPreview(SelectionList)
  236. SelectionListData.Total = conv.MustString(total, "")
  237. return SelectionListData, nil
  238. }
  239. func (s *selection) GetSelectionDetail(ctx *gin.Context, selectionId, enterpriseID string) (*http_model.SelectionDetail, error) {
  240. selectionDetail := http_model.SelectionDetail{}
  241. selectionInfo, err := db.GetSelectionById(ctx, selectionId)
  242. if err != nil {
  243. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionInfo error,err:%+v", err)
  244. return nil, err
  245. }
  246. selectionBriefInfo, err := db.GetSelectionBriefInfo(ctx, selectionId)
  247. if err != nil {
  248. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionBriefInfo error,err:%+v", err)
  249. return nil, err
  250. }
  251. selectionExampleInfo, err := db.GetSelectionExampleInfo(ctx, selectionId)
  252. if err != nil {
  253. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionExampleInfo error,err:%+v", err)
  254. return nil, err
  255. }
  256. productInfo, err := db.GetProductInfoBySelectionId(ctx, selectionId)
  257. if err != nil {
  258. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductInfo error,err:%+v", err)
  259. return nil, err
  260. }
  261. productPhotoInfo, err := db.GetProductPhotoInfoBySelectionId(ctx, selectionId)
  262. if err != nil {
  263. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductPhotoInfo error,err:%+v", err)
  264. return nil, err
  265. }
  266. selectionDetail.SelectionBrief = selectionBriefInfo
  267. selectionDetail.SelectionInfo = selectionInfo
  268. selectionDetail.SelectionExample = selectionExampleInfo
  269. selectionDetail.ProductInfo = productInfo
  270. selectionDetail.ProductPhotoInfo = productPhotoInfo
  271. return &selectionDetail, nil
  272. }