selection.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "reflect"
  8. "time"
  9. "youngee_m_api/db"
  10. "youngee_m_api/model/common_model"
  11. "youngee_m_api/model/gorm_model"
  12. "youngee_m_api/model/http_model"
  13. "youngee_m_api/pack"
  14. "youngee_m_api/util"
  15. "github.com/gin-gonic/gin"
  16. "github.com/sirupsen/logrus"
  17. "github.com/caixw/lib.go/conv"
  18. )
  19. var Selection *selection
  20. type selection struct {
  21. }
  22. func (*selection) Create(ctx context.Context, request http_model.CreateSelectionRequest) (*http_model.CreateSelectionData, error) {
  23. enterpriseId := request.EnterpriseId
  24. // 1. 检查该企业id和商品id有无选品
  25. //selectionInfo, err := db.GetSelectionByEnterpiseIdAndProductId(ctx, enterpriseId, conv.MustInt(request.ProductId, 0))
  26. //if err != nil {
  27. // return nil, err
  28. //}
  29. //if selectionInfo != nil {
  30. // return nil, errors.New("该商品下选品已存在")
  31. //}
  32. // 2. 数据准备
  33. // a) 生成选品id
  34. selectionId := util.GetSelectionID()
  35. // b) 查找关联商品信息
  36. product, err := db.GetProductByID(ctx, conv.MustInt64(request.ProductId, 0))
  37. if err != nil {
  38. return nil, err
  39. }
  40. productPhotos, err := db.GetProductPhotoByProductID(ctx, conv.MustInt64(request.ProductId, 0))
  41. productInfoToJson, _ := json.Marshal(product)
  42. productPhotosToJson, _ := json.Marshal(productPhotos)
  43. // c) 选品名称
  44. selectionName := product.BrandName + "-" + product.ProductName
  45. // 3. 创建选品
  46. //taskDdl := time.Time{} //赋零值
  47. //taskDdl, _ = time.ParseInLocation("2006-01-02 15:04:05", "2026-01-01 08:00:00", time.Local)
  48. t := time.Now()
  49. newSelection := gorm_model.YounggeeSelectionInfo{
  50. SelectionStatus: 1,
  51. SelectionID: selectionId,
  52. SelectionName: selectionName,
  53. ProductID: conv.MustInt(request.ProductId, 0),
  54. EnterpriseID: enterpriseId,
  55. Platform: conv.MustInt(request.Platform, 0),
  56. ProductSnap: string(productInfoToJson),
  57. ProductPhotoSnap: string(productPhotosToJson),
  58. CreatedAt: &t,
  59. UpdatedAt: &t,
  60. CommissionRate: "0",
  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. println("awdawdawdawdwad", request.EnterpriseId)
  78. selectionInfo, err := db.GetSelectionById(ctx, request.SelectionID)
  79. if err != nil {
  80. return nil, err
  81. }
  82. if selectionInfo == nil {
  83. return nil, errors.New("选品不存在")
  84. }
  85. // 2. 数据准备
  86. // a) 查找关联商品信息
  87. product, err := db.GetProductByID(ctx, conv.MustInt64(request.ProductId, 0))
  88. if err != nil {
  89. return nil, err
  90. }
  91. productPhotos, err := db.GetProductPhotoByProductID(ctx, conv.MustInt64(request.ProductId, 0))
  92. productInfoToJson, _ := json.Marshal(product)
  93. productPhotosToJson, _ := json.Marshal(productPhotos)
  94. // b) 选品名称
  95. selectionName := product.BrandName + "-" + product.ProductName
  96. // d) 任务截止时间
  97. fmt.Println("taskDdl:", request.TaskDdl)
  98. taskDdl := time.Time{} //赋零值
  99. taskDdl, _ = time.ParseInLocation("2006-01-02 15:04:05", request.TaskDdl, time.Local)
  100. // f) 更新选品状态
  101. if request.SelectionStatus != 2 && request.SelectionStatus != 7 {
  102. request.SelectionStatus = 1
  103. }
  104. t := time.Now()
  105. updateSelection := gorm_model.YounggeeSelectionInfo{
  106. SelectionID: request.SelectionID,
  107. SelectionStatus: request.SelectionStatus,
  108. SelectionName: selectionName,
  109. EnterpriseID: enterpriseId,
  110. ProductID: conv.MustInt(request.ProductId, 0),
  111. ContentType: conv.MustInt(request.ContentType, 0),
  112. TaskMode: conv.MustInt(request.TaskMode, 0),
  113. Platform: conv.MustInt(request.Platform, 0),
  114. SampleMode: conv.MustInt(request.SampleMode, 0),
  115. ProductUrl: request.ProductUrl,
  116. SampleNum: conv.MustInt(request.SampleNum, 0),
  117. RemainNum: conv.MustInt(request.SampleNum, 0),
  118. CommissionRate: conv.MustString(request.CommissionRate, "0"),
  119. TaskReward: conv.MustString(request.TaskReward, "0"),
  120. SettlementAmount: conv.MustString(request.SettlementAmount, "0"),
  121. EstimatedCost: selectionInfo.EstimatedCost,
  122. SampleCondition: request.SampleCondition,
  123. RewardCondition: request.RewardCondition,
  124. TaskDdl: &taskDdl,
  125. Detail: request.Detail,
  126. ProductSnap: string(productInfoToJson),
  127. ProductPhotoSnap: string(productPhotosToJson),
  128. CreatedAt: selectionInfo.CreatedAt,
  129. UpdatedAt: &t,
  130. }
  131. if request.SelectionStatus == 2 {
  132. updateSelection.SubmitAt = &t
  133. }
  134. // 合并传入参数和数据表中原记录,若传入参数字段值为空,则将字段赋值为原记录中值
  135. result := util.MergeStructValue(&updateSelection, selectionInfo)
  136. // 利用反射机制将interface类型转换为结构体类型
  137. v := reflect.ValueOf(&result).Elem()
  138. if v.Kind() == reflect.Struct {
  139. updateSelection = v.Interface().(gorm_model.YounggeeSelectionInfo)
  140. //fmt.Println(p)
  141. }
  142. // c) 计算预估成本(如果有)
  143. var estimatedCost float64
  144. if conv.MustInt(updateSelection.TaskMode, 0) == 1 {
  145. estimatedCost = conv.MustFloat64(updateSelection.TaskReward, 0) * conv.MustFloat64(updateSelection.SampleNum, 0)
  146. }
  147. estimatedCostToString, _ := conv.String(estimatedCost)
  148. updateSelection.EstimatedCost = estimatedCostToString
  149. // 3. 更新选品
  150. err = db.UpdateSelection(ctx, updateSelection)
  151. if err != nil {
  152. return nil, err
  153. }
  154. // 4. 更新选品brief和示例
  155. if request.SecBrief != nil {
  156. // 删除已有brief
  157. err = db.DeleteSecBriefBySelectionId(ctx, selectionInfo.SelectionID)
  158. if err != nil {
  159. return nil, err
  160. }
  161. // 插入新的brief
  162. for _, v := range request.SecBrief {
  163. brief := gorm_model.YounggeeSecBrief{
  164. SelectionID: selectionInfo.SelectionID,
  165. FileUid: v.PhotoUid,
  166. FileName: v.Name,
  167. FileUrl: v.PhotoUrl,
  168. CreatedAt: time.Now(),
  169. }
  170. err = db.CreateSecBrief(ctx, brief)
  171. if err != nil {
  172. return nil, err
  173. }
  174. }
  175. }
  176. if request.SecExample != nil {
  177. // 删除已有示例
  178. err = db.DeleteSecExampleBySelectionId(ctx, selectionInfo.SelectionID)
  179. if err != nil {
  180. return nil, err
  181. }
  182. // 插入新的示例
  183. for _, v := range request.SecExample {
  184. Example := gorm_model.YounggeeSecExample{
  185. SelectionID: selectionInfo.SelectionID,
  186. FileUid: v.PhotoUid,
  187. FileName: v.Name,
  188. FileUrl: v.PhotoUrl,
  189. CreatedAt: time.Now(),
  190. }
  191. err = db.CreateSecExample(ctx, Example)
  192. if err != nil {
  193. return nil, err
  194. }
  195. }
  196. }
  197. res := &http_model.UpdateSelectionData{
  198. SelectionId: updateSelection.SelectionID,
  199. }
  200. return res, nil
  201. }
  202. func (*selection) Pay(ctx context.Context, request http_model.PaySelectionRequest, enterpriseId string) (*http_model.PaySelectionData, error) {
  203. // 校验
  204. // 1. 账户余额是否足够
  205. enterprise, err := db.GetEnterpriseByEnterpriseID(ctx, enterpriseId)
  206. if err != nil {
  207. return nil, err
  208. }
  209. if enterprise.AvailableBalance < request.PayMoney {
  210. return nil, errors.New("账户余额不足")
  211. }
  212. // 2. 选品项目状态是否正确
  213. selectionInfo, err := db.GetSelectionById(ctx, request.SelectionId)
  214. if err != nil {
  215. return nil, err
  216. }
  217. if selectionInfo == nil {
  218. return nil, errors.New("选品不存在")
  219. }
  220. if selectionInfo.SelectionStatus != 4 {
  221. return nil, errors.New("选品状态有误")
  222. }
  223. // 支付
  224. err = db.PaySelection(ctx, enterpriseId, request.PayMoney, request.SelectionId)
  225. if err != nil {
  226. return nil, err
  227. }
  228. res := &http_model.PaySelectionData{
  229. SelectionId: request.SelectionId,
  230. }
  231. return res, nil
  232. }
  233. func (*selection) Submit(ctx context.Context, request http_model.SubmitSelectionRequest) (*http_model.SubmitSelectionData, error) {
  234. t := time.Now()
  235. updateSelection := gorm_model.YounggeeSelectionInfo{
  236. SelectionID: request.SelectionId,
  237. SelectionStatus: request.SelectionStatus,
  238. SubmitAt: &t,
  239. }
  240. err := db.UpdateSelection(ctx, updateSelection)
  241. if err != nil {
  242. return nil, err
  243. }
  244. res := &http_model.SubmitSelectionData{}
  245. return res, nil
  246. }
  247. func (s *selection) GetAllSelection(ctx context.Context, enterpriseID string, pageSize, pageNum int64, conditions *common_model.SelectionConditions) (*http_model.SelectionData, error) {
  248. SelectionList, total, err := db.GetSelectionList(ctx, enterpriseID, pageSize, pageNum, conditions)
  249. if err != nil {
  250. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetAllSelection error,err:%+v", err)
  251. return nil, err
  252. }
  253. SelectionListData := new(http_model.SelectionData)
  254. SelectionListData.SelectionInfo = pack.MGormSelectionToHttpSelectionPreview(SelectionList)
  255. SelectionListData.Total = conv.MustString(total, "")
  256. return SelectionListData, nil
  257. }
  258. func (s *selection) GetSelectionDetail(ctx *gin.Context, selectionId string, enterpriseId string) (*http_model.SelectionDetail, error) {
  259. selectionDetail := http_model.SelectionDetail{}
  260. selectionInfo, err := db.GetSelectionById(ctx, selectionId)
  261. if err != nil {
  262. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionInfo error,err:%+v", err)
  263. return nil, err
  264. }
  265. selectionBriefInfo, err := db.GetSelectionBriefInfo(ctx, selectionId)
  266. if err != nil {
  267. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionBriefInfo error,err:%+v", err)
  268. return nil, err
  269. }
  270. selectionExampleInfo, err := db.GetSelectionExampleInfo(ctx, selectionId)
  271. if err != nil {
  272. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionExampleInfo error,err:%+v", err)
  273. return nil, err
  274. }
  275. productInfo, err := db.GetProductInfoBySelectionId(ctx, selectionId)
  276. if err != nil {
  277. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductInfo error,err:%+v", err)
  278. return nil, err
  279. }
  280. productPhotoInfo, err := db.GetProductPhotoInfoBySelectionId(ctx, selectionId)
  281. if err != nil {
  282. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetProductPhotoInfo error,err:%+v", err)
  283. return nil, err
  284. }
  285. selectionDetail.SelectionBrief = selectionBriefInfo
  286. selectionDetail.SelectionInfo = selectionInfo
  287. selectionDetail.SelectionExample = selectionExampleInfo
  288. selectionDetail.ProductInfo = productInfo
  289. selectionDetail.ProductPhotoInfo = productPhotoInfo
  290. return &selectionDetail, nil
  291. }
  292. func (*selection) Review(ctx context.Context, request http_model.ReviewSelectionRequest) (*http_model.ReviewSelectionData, error) {
  293. // 根据选品id查询选品信息
  294. selectionInfo, err := db.GetSelectionById(ctx, request.SelectionId)
  295. if err != nil {
  296. logrus.WithContext(ctx).Errorf("[selectionDB service] call GetSelectionById error,err:%+v", err)
  297. return nil, err
  298. }
  299. // 计算预估成本
  300. var estimatedCost float64 = 0.0
  301. var estimatedCostToString string = ""
  302. if conv.MustInt(selectionInfo.TaskMode, 0) == 1 {
  303. estimatedCost = conv.MustFloat64(selectionInfo.TaskReward, 0) * conv.MustFloat64(selectionInfo.SampleNum, 0)
  304. estimatedCostToString, _ = conv.String(estimatedCost)
  305. }
  306. // 若审核通过则更新选品阶段为待支付,否则更新为失效并赋值失效原因
  307. t := time.Now()
  308. AutoTaskID, err := db.GetLastAutoTaskID()
  309. updateSelection := gorm_model.YounggeeSelectionInfo{}
  310. if request.IsPass == 1 {
  311. if conv.MustInt(selectionInfo.TaskMode, 0) == 1 {
  312. //悬赏任务
  313. updateSelection = gorm_model.YounggeeSelectionInfo{
  314. SelectionID: request.SelectionId,
  315. SelectionStatus: 4,
  316. PassAt: &t,
  317. EstimatedCost: estimatedCostToString,
  318. AutoTaskID: int(AutoTaskID),
  319. }
  320. } else {
  321. // 纯佣带货
  322. updateSelection = gorm_model.YounggeeSelectionInfo{
  323. SelectionID: request.SelectionId,
  324. SelectionStatus: 6,
  325. PassAt: &t,
  326. EstimatedCost: estimatedCostToString,
  327. }
  328. }
  329. } else {
  330. updateSelection = gorm_model.YounggeeSelectionInfo{
  331. SelectionID: request.SelectionId,
  332. SelectionStatus: 7,
  333. FailReason: 2,
  334. FinishAt: &t,
  335. EstimatedCost: estimatedCostToString,
  336. }
  337. }
  338. err = db.UpdateSelection(ctx, updateSelection)
  339. if err != nil {
  340. logrus.WithContext(ctx).Errorf("[selectionDB service] call UpdateSelection error,err:%+v", err)
  341. return nil, err
  342. }
  343. res := &http_model.ReviewSelectionData{}
  344. return res, nil
  345. }