teambuying_service.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package service
  2. import (
  3. "time"
  4. "youngee_b_api/app/dao"
  5. "youngee_b_api/app/entity"
  6. "youngee_b_api/app/vo"
  7. )
  8. type TeamBuyingService struct{}
  9. func (p TeamBuyingService) GetTeamBuyingsByStoreId(param vo.GetAllTeamBuyingParam) (vo.ResultVO, error) {
  10. if param.Page == 0 {
  11. param.Page = 1
  12. }
  13. if param.PageSize == 0 {
  14. param.PageSize = 10
  15. }
  16. var result vo.ResultVO
  17. var teamBuyings []entity.TeamBuying
  18. var err error
  19. var total int64
  20. storeId := param.StoreId
  21. teamBuyings, total, err = (&dao.TeamBuyingDao{}).GetTeamBuyingsByStoreIdAndTeamBuyingTitle(storeId, param.TeamBuyingTitle, param.Page, param.PageSize)
  22. if err != nil {
  23. // 数据库查询error
  24. return result, err
  25. }
  26. var reTeamBuyings []vo.ReTeamBuying
  27. for _, teamBuying := range teamBuyings {
  28. photoUrl, e := dao.ProductPhotoDAO{}.GetMainPhotoByTeamBuyingID(teamBuying.TeamBuyingID)
  29. if e != nil {
  30. photoUrl = ""
  31. }
  32. reTeamBuying := vo.ReTeamBuying{
  33. TeamBuyingID: teamBuying.TeamBuyingID,
  34. TeamBuyingName: teamBuying.TeamBuyingName,
  35. TeamBuyingPrice: teamBuying.TeamBuyingPrice,
  36. TeamBuyingCategory: teamBuying.TeamBuyingCategory,
  37. TeamBuyingDetail: teamBuying.TeamBuyingDetail,
  38. CreatedAt: teamBuying.CreatedAt.Format("2006-01-02 15:04:05"),
  39. PhotoUrl: photoUrl,
  40. }
  41. reTeamBuyings = append(reTeamBuyings, reTeamBuying)
  42. }
  43. result = vo.ResultVO{
  44. Page: param.Page,
  45. PageSize: param.PageSize,
  46. Total: total,
  47. Data: reTeamBuyings,
  48. }
  49. return result, nil
  50. }
  51. func (p TeamBuyingService) CreateTeamBuying(param *vo.TeamBuyingCreateParam) (int64, error) {
  52. newTeamBuying := entity.TeamBuying{
  53. TeamBuyingName: param.TeamBuyingName,
  54. TeamBuyingCategory: param.TeamBuyingCategory,
  55. TeamBuyingPrice: param.TeamBuyingPrice,
  56. PublicCommission: param.PublicCommission,
  57. TeamBuyingDetail: param.TeamBuyingDetail,
  58. TeamBuyingLink: param.TeamBuyingLink,
  59. StoreID: param.StoreId,
  60. CreatedAt: time.Now(),
  61. EnterpriseID: param.EnterpriseId,
  62. SubAccountID: param.SubAccountId,
  63. OperateType: 1,
  64. }
  65. teamBuyingId, err := dao.TeamBuyingDao{}.CreateTeamBuying(newTeamBuying)
  66. if err != nil {
  67. return 0, err
  68. }
  69. if param.TeamBuyingPhotos != nil {
  70. teamBuyingPhotos := []entity.ProductPhoto{}
  71. for _, photo := range param.TeamBuyingPhotos {
  72. teamBuyingPhoto := entity.ProductPhoto{
  73. PhotoUrl: photo.PhotoUrl,
  74. PhotoUid: photo.PhotoUid,
  75. Symbol: photo.Symbol,
  76. TeamBuyingID: teamBuyingId,
  77. ProductPhotoType: 3,
  78. CreatedAt: time.Now(),
  79. }
  80. teamBuyingPhotos = append(teamBuyingPhotos, teamBuyingPhoto)
  81. }
  82. err = dao.ProductPhotoDAO{}.CreateProductPhoto(teamBuyingPhotos)
  83. if err != nil {
  84. return 0, err
  85. }
  86. }
  87. return teamBuyingId, nil
  88. }