selection.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "reflect"
  7. "time"
  8. "youngee_m_api/db"
  9. "youngee_m_api/model/common_model"
  10. "youngee_m_api/model/gorm_model"
  11. "youngee_m_api/model/http_model"
  12. "youngee_m_api/pack"
  13. "youngee_m_api/util"
  14. "github.com/gin-gonic/gin"
  15. "github.com/sirupsen/logrus"
  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) (*http_model.CreateSelectionData, error) {
  22. enterpriseId := request.EnterpriseId
  23. // 1. 检查该企业id和商品id有无选品
  24. //selectionInfo, err := db.GetSelectionByEnterpiseIdAndProductId(ctx, enterpriseId, conv.MustInt(request.ProductId, 0))
  25. //if err != nil {
  26. // return nil, err
  27. //}
  28. //if selectionInfo != nil {
  29. // return nil, errors.New("该商品下选品已存在")
  30. //}
  31. // 2. 数据准备
  32. // a) 生成选品id
  33. selectionId := util.GetSelectionID()
  34. // b) 查找关联商品信息
  35. product, err := db.GetProductByID(ctx, conv.MustInt64(request.ProductId, 0))
  36. if err != nil {
  37. return nil, err
  38. }
  39. productPhotos, err := db.GetProductPhotoByProductID(ctx, conv.MustInt64(request.ProductId, 0))
  40. productInfoToJson, _ := json.Marshal(product)
  41. productPhotosToJson, _ := json.Marshal(productPhotos)
  42. // c) 选品名称
  43. selectionName := product.BrandName + "-" + product.ProductName
  44. // 3. 创建选品
  45. taskDdl := time.Time{} //赋零值
  46. taskDdl, _ = time.ParseInLocation("2006-01-02 15:04:05", "2026-01-01 08:00:00", time.Local)
  47. newSelection := gorm_model.YounggeeSelectionInfo{
  48. SelectionID: selectionId,
  49. SelectionName: selectionName,
  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.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
  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. 检查该企业id和商品id有无选品
  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. // d) 任务截止时间
  96. taskDdl := time.Time{} //赋零值
  97. taskDdl, _ = time.ParseInLocation("2006-01-02 15:04:05", request.TaskDdl, time.Local)
  98. // f) 更新选品状态
  99. if request.SelectionStatus != 2 && request.SelectionStatus != 7 {
  100. request.SelectionStatus = 1
  101. }
  102. updateSelection := gorm_model.YounggeeSelectionInfo{
  103. SelectionID: request.SelectionID,
  104. SelectionStatus: request.SelectionStatus,
  105. SelectionName: selectionName,
  106. EnterpriseID: enterpriseId,
  107. ProductID: conv.MustInt(request.ProductId, 0),
  108. ContentType: conv.MustInt(request.ContentType, 0),
  109. TaskMode: conv.MustInt(request.TaskMode, 0),
  110. Platform: conv.MustInt(request.Platform, 0),
  111. SampleMode: conv.MustInt(request.SampleMode, 0),
  112. ProductUrl: request.ProductUrl,
  113. SampleNum: conv.MustInt(request.SampleNum, 0),
  114. RemainNum: conv.MustInt(request.SampleNum, 0),
  115. CommissionRate: conv.MustInt(request.CommissionRate, 0),
  116. TaskReward: conv.MustString(request.TaskReward, "0"),
  117. SettlementAmount: conv.MustString(request.SettlementAmount, "0"),
  118. EstimatedCost: selectionInfo.EstimatedCost,
  119. SampleCondition: request.SampleCondition,
  120. RewardCondition: request.RewardCondition,
  121. TaskDdl: taskDdl,
  122. Detail: request.Detail,
  123. ProductSnap: string(productInfoToJson),
  124. ProductPhotoSnap: string(productPhotosToJson),
  125. CreatedAt: selectionInfo.CreatedAt,
  126. UpdatedAt: time.Now(),
  127. SubmitAt: time.Now(),
  128. }
  129. // 合并传入参数和数据表中原记录,若传入参数字段值为空,则将字段赋值为原记录中值
  130. result := util.MergeStructValue(&updateSelection, selectionInfo)
  131. // 利用反射机制将interface类型转换为结构体类型
  132. v := reflect.ValueOf(&result).Elem()
  133. if v.Kind() == reflect.Struct {
  134. updateSelection = v.Interface().(gorm_model.YounggeeSelectionInfo)
  135. //fmt.Println(p)
  136. }
  137. // c) 计算预估成本(如果有)
  138. var estimatedCost float64
  139. if conv.MustInt(updateSelection.TaskMode, 0) == 1 {
  140. estimatedCost = conv.MustFloat64(updateSelection.TaskReward, 0) * conv.MustFloat64(updateSelection.SampleNum, 0)
  141. }
  142. estimatedCostToString, _ := conv.String(estimatedCost)
  143. updateSelection.EstimatedCost = estimatedCostToString
  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. // 插入新的brief
  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 (*selection) Submit(ctx context.Context, request http_model.SubmitSelectionRequest) (*http_model.SubmitSelectionData, error) {
  229. updateSelection := gorm_model.YounggeeSelectionInfo{
  230. SelectionID: request.SelectionId,
  231. SelectionStatus: request.SelectionStatus,
  232. SubmitAt: time.Now(),
  233. }
  234. err := db.UpdateSelection(ctx, updateSelection)
  235. if err != nil {
  236. return nil, err
  237. }
  238. res := &http_model.SubmitSelectionData{}
  239. return res, nil
  240. }
  241. func (s *selection) GetAllSelection(ctx context.Context, enterpriseID string, pageSize, pageNum int64, conditions *common_model.SelectionConditions) (*http_model.SelectionData, error) {
  242. SelectionList, total, err := db.GetSelectionList(ctx, enterpriseID, pageSize, pageNum, conditions)
  243. if err != nil {
  244. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetAllSelection error,err:%+v", err)
  245. return nil, err
  246. }
  247. SelectionListData := new(http_model.SelectionData)
  248. SelectionListData.SelectionInfo = pack.MGormSelectionToHttpSelectionPreview(SelectionList)
  249. SelectionListData.Total = conv.MustString(total, "")
  250. return SelectionListData, nil
  251. }
  252. func (s *selection) GetSelectionDetail(ctx *gin.Context, selectionId string, enterpriseId string) (*http_model.SelectionDetail, error) {
  253. selectionDetail := http_model.SelectionDetail{}
  254. selectionInfo, err := db.GetSelectionById(ctx, selectionId)
  255. if err != nil {
  256. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionInfo error,err:%+v", err)
  257. return nil, err
  258. }
  259. selectionBriefInfo, err := db.GetSelectionBriefInfo(ctx, selectionId)
  260. if err != nil {
  261. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionBriefInfo error,err:%+v", err)
  262. return nil, err
  263. }
  264. selectionExampleInfo, err := db.GetSelectionExampleInfo(ctx, selectionId)
  265. if err != nil {
  266. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionExampleInfo error,err:%+v", err)
  267. return nil, err
  268. }
  269. productInfo, err := db.GetProductInfoBySelectionId(ctx, selectionId)
  270. if err != nil {
  271. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductInfo error,err:%+v", err)
  272. return nil, err
  273. }
  274. productPhotoInfo, err := db.GetProductPhotoInfoBySelectionId(ctx, selectionId)
  275. if err != nil {
  276. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductPhotoInfo error,err:%+v", err)
  277. return nil, err
  278. }
  279. selectionDetail.SelectionBrief = selectionBriefInfo
  280. selectionDetail.SelectionInfo = selectionInfo
  281. selectionDetail.SelectionExample = selectionExampleInfo
  282. selectionDetail.ProductInfo = productInfo
  283. selectionDetail.ProductPhotoInfo = productPhotoInfo
  284. return &selectionDetail, nil
  285. }
  286. func (*selection) Review(ctx context.Context, request http_model.ReviewSelectionRequest) (*http_model.ReviewSelectionData, error) {
  287. // 根据选品id查询选品信息
  288. selectionInfo, err := db.GetSelectionById(ctx, request.SelectionId)
  289. if err != nil {
  290. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionById error,err:%+v", err)
  291. return nil, err
  292. }
  293. // 计算预估成本
  294. var estimatedCost float64 = 0.0
  295. var estimatedCostToString string = ""
  296. if conv.MustInt(selectionInfo.TaskMode, 0) == 1 {
  297. estimatedCost = conv.MustFloat64(selectionInfo.TaskReward, 0) * conv.MustFloat64(selectionInfo.SampleNum, 0)
  298. estimatedCostToString, _ = conv.String(estimatedCost)
  299. }
  300. // 若审核通过则更新选品阶段为待支付,否则更新为失效并赋值失效原因
  301. if request.IsPass == 1 {
  302. updateSelection := gorm_model.YounggeeSelectionInfo{
  303. SelectionID: request.SelectionId,
  304. SelectionStatus: 4,
  305. PassAt: time.Now(),
  306. EstimatedCost: estimatedCostToString,
  307. }
  308. err = db.UpdateSelection(ctx, updateSelection)
  309. if err != nil {
  310. logrus.WithContext(ctx).Errorf("[selectionDB service] call UpdateSelection error,err:%+v", err)
  311. return nil, err
  312. }
  313. } else {
  314. updateSelection := gorm_model.YounggeeSelectionInfo{
  315. SelectionID: request.SelectionId,
  316. SelectionStatus: 7,
  317. FailReason: 2,
  318. PassAt: time.Now(),
  319. EstimatedCost: estimatedCostToString,
  320. }
  321. err = db.UpdateSelection(ctx, updateSelection)
  322. if err != nil {
  323. logrus.WithContext(ctx).Errorf("[selectionDB service] call UpdateSelection error,err:%+v", err)
  324. return nil, err
  325. }
  326. }
  327. res := &http_model.ReviewSelectionData{}
  328. return res, nil
  329. }