selection.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package 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_m_api/db"
  11. "youngee_m_api/model/common_model"
  12. "youngee_m_api/model/gorm_model"
  13. "youngee_m_api/model/http_model"
  14. "youngee_m_api/pack"
  15. "youngee_m_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) (*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. // 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. result := util.MergeStructValue(&updateSelection, selectionInfo)
  137. // 利用反射机制将interface类型转换为结构体类型
  138. v := reflect.ValueOf(&result).Elem()
  139. if v.Kind() == reflect.Struct {
  140. updateSelection = v.Interface().(gorm_model.YounggeeSelectionInfo)
  141. //fmt.Println(p)
  142. }
  143. // 3. 更新选品
  144. err = db.UpdateSelection(ctx, updateSelection)
  145. if err != nil {
  146. return nil, err
  147. }
  148. // 4. 更新选品brief和示例
  149. if request.SecBrief != nil {
  150. // 删除已有brief
  151. err = db.DeleteSecBriefBySelectionId(ctx, selectionInfo.SelectionID)
  152. if err != nil {
  153. return nil, err
  154. }
  155. // 插入新的brief
  156. for _, v := range request.SecBrief {
  157. brief := gorm_model.YounggeeSecBrief{
  158. SelectionID: selectionInfo.SelectionID,
  159. FileUid: v.PhotoUid,
  160. FileName: v.Name,
  161. FileUrl: v.PhotoUrl,
  162. CreatedAt: time.Now(),
  163. }
  164. err = db.CreateSecBrief(ctx, brief)
  165. if err != nil {
  166. return nil, err
  167. }
  168. }
  169. }
  170. if request.SecExample != nil {
  171. // 删除已有示例
  172. err = db.DeleteSecExampleBySelectionId(ctx, selectionInfo.SelectionID)
  173. if err != nil {
  174. return nil, err
  175. }
  176. // 插入新的示例
  177. for _, v := range request.SecExample {
  178. Example := gorm_model.YounggeeSecExample{
  179. SelectionID: selectionInfo.SelectionID,
  180. FileUid: v.PhotoUid,
  181. FileName: v.Name,
  182. FileUrl: v.PhotoUrl,
  183. CreatedAt: time.Now(),
  184. }
  185. err = db.CreateSecExample(ctx, Example)
  186. if err != nil {
  187. return nil, err
  188. }
  189. }
  190. }
  191. res := &http_model.UpdateSelectionData{
  192. SelectionId: updateSelection.SelectionID,
  193. }
  194. return res, nil
  195. }
  196. func (*selection) Pay(ctx context.Context, request http_model.PaySelectionRequest, enterpriseId string) (*http_model.PaySelectionData, error) {
  197. // 校验
  198. // 1. 账户余额是否足够
  199. enterprise, err := db.GetEnterpriseByEnterpriseID(ctx, enterpriseId)
  200. if err != nil {
  201. return nil, err
  202. }
  203. if enterprise.AvailableBalance < request.PayMoney {
  204. return nil, errors.New("账户余额不足")
  205. }
  206. // 2. 选品项目状态是否正确
  207. selectionInfo, err := db.GetSelectionById(ctx, request.SelectionId)
  208. if err != nil {
  209. return nil, err
  210. }
  211. if selectionInfo == nil {
  212. return nil, errors.New("选品不存在")
  213. }
  214. if selectionInfo.SelectionStatus != 4 {
  215. return nil, errors.New("选品状态有误")
  216. }
  217. // 支付
  218. err = db.PaySelection(ctx, enterpriseId, request.PayMoney, request.SelectionId)
  219. if err != nil {
  220. return nil, err
  221. }
  222. res := &http_model.PaySelectionData{
  223. SelectionId: request.SelectionId,
  224. }
  225. return res, nil
  226. }
  227. func (*selection) Submit(ctx context.Context, request http_model.SubmitSelectionRequest) (*http_model.SubmitSelectionData, error) {
  228. updateSelection := gorm_model.YounggeeSelectionInfo{
  229. SelectionID: request.SelectionId,
  230. SelectionStatus: request.SelectionStatus,
  231. SubmitAt: time.Now(),
  232. }
  233. err := db.UpdateSelection(ctx, updateSelection)
  234. if err != nil {
  235. return nil, err
  236. }
  237. res := &http_model.SubmitSelectionData{}
  238. return res, nil
  239. }
  240. func (s *selection) GetAllSelection(ctx context.Context, enterpriseID string, pageSize, pageNum int64, conditions *common_model.SelectionConditions) (*http_model.SelectionData, error) {
  241. SelectionList, total, err := db.GetSelectionList(ctx, enterpriseID, pageSize, pageNum, conditions)
  242. if err != nil {
  243. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetAllSelection error,err:%+v", err)
  244. return nil, err
  245. }
  246. SelectionListData := new(http_model.SelectionData)
  247. SelectionListData.SelectionInfo = pack.MGormSelectionToHttpSelectionPreview(SelectionList)
  248. SelectionListData.Total = conv.MustString(total, "")
  249. return SelectionListData, nil
  250. }
  251. func (s *selection) GetSelectionDetail(ctx *gin.Context, selectionId string, enterpriseId string) (*http_model.SelectionDetail, error) {
  252. selectionDetail := http_model.SelectionDetail{}
  253. selectionInfo, err := db.GetSelectionById(ctx, selectionId)
  254. if err != nil {
  255. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionInfo error,err:%+v", err)
  256. return nil, err
  257. }
  258. selectionBriefInfo, err := db.GetSelectionBriefInfo(ctx, selectionId)
  259. if err != nil {
  260. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionBriefInfo error,err:%+v", err)
  261. return nil, err
  262. }
  263. selectionExampleInfo, err := db.GetSelectionExampleInfo(ctx, selectionId)
  264. if err != nil {
  265. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionExampleInfo error,err:%+v", err)
  266. return nil, err
  267. }
  268. productInfo, err := db.GetProductInfoBySelectionId(ctx, selectionId)
  269. if err != nil {
  270. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductInfo error,err:%+v", err)
  271. return nil, err
  272. }
  273. productPhotoInfo, err := db.GetProductPhotoInfoBySelectionId(ctx, selectionId)
  274. if err != nil {
  275. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductPhotoInfo error,err:%+v", err)
  276. return nil, err
  277. }
  278. selectionDetail.SelectionBrief = selectionBriefInfo
  279. selectionDetail.SelectionInfo = selectionInfo
  280. selectionDetail.SelectionExample = selectionExampleInfo
  281. selectionDetail.ProductInfo = productInfo
  282. selectionDetail.ProductPhotoInfo = productPhotoInfo
  283. return &selectionDetail, nil
  284. }
  285. func (*selection) Review(ctx context.Context, request http_model.ReviewSelectionRequest) (*http_model.ReviewSelectionData, error) {
  286. // 根据选品id查询选品信息
  287. selectionInfo, err := db.GetSelectionById(ctx, request.SelectionId)
  288. if err != nil {
  289. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionById error,err:%+v", err)
  290. return nil, err
  291. }
  292. // 计算预估成本
  293. var estimatedCost float64 = 0.0
  294. var estimatedCostToString string = ""
  295. if conv.MustInt(selectionInfo.TaskMode, 0) == 1 {
  296. estimatedCost = conv.MustFloat64(selectionInfo.TaskReward, 0) * conv.MustFloat64(selectionInfo.SampleNum, 0)
  297. estimatedCostToString, _ = conv.String(estimatedCost)
  298. }
  299. // 若审核通过则更新选品阶段为待支付,否则更新为失效并赋值失效原因
  300. if request.IsPass == 1 {
  301. updateSelection := gorm_model.YounggeeSelectionInfo{
  302. SelectionID: request.SelectionId,
  303. SelectionStatus: 4,
  304. PassAt: time.Now(),
  305. EstimatedCost: estimatedCostToString,
  306. }
  307. err = db.UpdateSelection(ctx, updateSelection)
  308. if err != nil {
  309. logrus.WithContext(ctx).Errorf("[selectionDB service] call UpdateSelection error,err:%+v", err)
  310. return nil, err
  311. }
  312. } else {
  313. updateSelection := gorm_model.YounggeeSelectionInfo{
  314. SelectionID: request.SelectionId,
  315. SelectionStatus: 7,
  316. FailReason: 2,
  317. PassAt: time.Now(),
  318. EstimatedCost: estimatedCostToString,
  319. }
  320. err = db.UpdateSelection(ctx, updateSelection)
  321. if err != nil {
  322. logrus.WithContext(ctx).Errorf("[selectionDB service] call UpdateSelection error,err:%+v", err)
  323. return nil, err
  324. }
  325. }
  326. res := &http_model.ReviewSelectionData{}
  327. return res, nil
  328. }