teambuying_service.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.ReTeamBuyingPreview
  27. for _, teamBuying := range teamBuyings {
  28. var createrName string
  29. photoUrl, e := dao.ProductPhotoDAO{}.GetMainPhotoByTeamBuyingID(teamBuying.TeamBuyingID)
  30. if e != nil {
  31. photoUrl = ""
  32. }
  33. if teamBuying.SubAccountID == 0 {
  34. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(teamBuying.EnterpriseID)
  35. if err == nil && enterprise != nil {
  36. createrName = enterprise.BusinessName
  37. }
  38. } else {
  39. subAccount, err := dao.SubAccountDao{}.GetSubAccount(teamBuying.SubAccountID)
  40. if err == nil && subAccount != nil {
  41. createrName = subAccount.SubAccountName
  42. }
  43. }
  44. reTeamBuying := vo.ReTeamBuyingPreview{
  45. TeamBuyingID: teamBuying.TeamBuyingID,
  46. TeamBuyingName: teamBuying.TeamBuyingName,
  47. TeamBuyingPrice: teamBuying.TeamBuyingPrice,
  48. TeamBuyingCategory: teamBuying.TeamBuyingCategory,
  49. TeamBuyingDetail: teamBuying.TeamBuyingDetail,
  50. CreatedAt: teamBuying.CreatedAt.Format("2006-01-02 15:04:05"),
  51. PhotoUrl: photoUrl,
  52. CreateName: createrName,
  53. }
  54. reTeamBuyings = append(reTeamBuyings, reTeamBuying)
  55. }
  56. result = vo.ResultVO{
  57. Page: param.Page,
  58. PageSize: param.PageSize,
  59. Total: total,
  60. Data: reTeamBuyings,
  61. }
  62. return result, nil
  63. }
  64. // 新建团购
  65. func (p TeamBuyingService) CreateTeamBuying(param *vo.TeamBuyingCreateParam) (int64, error) {
  66. newTeamBuying := entity.TeamBuying{
  67. TeamBuyingName: param.TeamBuyingName,
  68. TeamBuyingCategory: param.TeamBuyingCategory,
  69. TeamBuyingPrice: param.TeamBuyingPrice,
  70. PublicCommission: param.PublicCommission,
  71. TeamBuyingDetail: param.TeamBuyingDetail,
  72. TeamBuyingLink: param.TeamBuyingLink,
  73. StoreID: param.StoreId,
  74. CreatedAt: time.Now(),
  75. EnterpriseID: param.EnterpriseId,
  76. SubAccountID: param.SubAccountId,
  77. OperateType: 1,
  78. }
  79. teamBuyingId, err := dao.TeamBuyingDao{}.CreateTeamBuying(newTeamBuying)
  80. if err != nil {
  81. return 0, err
  82. }
  83. err = dao.StoreDao{}.IncrementTeamNum(param.StoreId)
  84. if err != nil {
  85. return 0, err
  86. }
  87. if param.TeamBuyingPhotos != nil {
  88. teamBuyingPhotos := []entity.ProductPhoto{}
  89. for _, photo := range param.TeamBuyingPhotos {
  90. teamBuyingPhoto := entity.ProductPhoto{
  91. PhotoUrl: photo.PhotoUrl,
  92. PhotoUid: photo.PhotoUid,
  93. Symbol: photo.Symbol,
  94. TeamBuyingID: teamBuyingId,
  95. ProductPhotoType: 3,
  96. CreatedAt: time.Now(),
  97. }
  98. teamBuyingPhotos = append(teamBuyingPhotos, teamBuyingPhoto)
  99. }
  100. err = dao.ProductPhotoDAO{}.CreateProductPhoto(teamBuyingPhotos)
  101. if err != nil {
  102. return 0, err
  103. }
  104. }
  105. return teamBuyingId, nil
  106. }
  107. // 团购详情
  108. func (s TeamBuyingService) GetTeamBuyingDetail(param *vo.TeamBuyingSearchParam) (*vo.ReTeamBuyingInfo, error) {
  109. var reTeamBuyingInfo *vo.ReTeamBuyingInfo
  110. teamBuying, err := dao.TeamBuyingDao{}.GetTeamBuyingByID(param.TeamBuyingId)
  111. if err != nil {
  112. return nil, err
  113. }
  114. photoMain, _ := dao.ProductPhotoDAO{}.GetMainPhotoByTeamBuyingID(param.TeamBuyingId)
  115. photosAll, _ := dao.ProductPhotoDAO{}.GetProductPhotoByTeamBuyingID(param.TeamBuyingId)
  116. var photoRotates []vo.Photo
  117. var photoDetails []vo.Photo
  118. for _, photosOne := range photosAll {
  119. if photosOne.Symbol == 2 || photosOne.Symbol == 3 {
  120. photo := vo.Photo{
  121. PhotoUrl: photosOne.PhotoUrl,
  122. Symbol: photosOne.Symbol,
  123. }
  124. photoRotates = append(photoRotates, photo)
  125. } else if photosOne.Symbol == 4 || photosOne.Symbol == 5 {
  126. photo := vo.Photo{
  127. PhotoUrl: photosOne.PhotoUrl,
  128. Symbol: photosOne.Symbol,
  129. }
  130. photoDetails = append(photoDetails, photo)
  131. }
  132. }
  133. reTeamBuyingInfo = &vo.ReTeamBuyingInfo{
  134. TeamBuyingID: teamBuying.StoreID,
  135. TeamBuyingName: teamBuying.TeamBuyingName,
  136. TeamBuyingPrice: teamBuying.TeamBuyingPrice,
  137. PublicCommission: teamBuying.PublicCommission,
  138. TeamBuyingCategory: teamBuying.TeamBuyingCategory,
  139. TeamBuyingDetail: teamBuying.TeamBuyingDetail,
  140. TeamBuyingLink: teamBuying.TeamBuyingLink,
  141. PhotoMain: photoMain,
  142. PhotoRotates: photoRotates,
  143. PhotoDetails: photoDetails,
  144. CreatedAt: teamBuying.CreatedAt.Format("2006-01-02 15:04:05"),
  145. }
  146. return reTeamBuyingInfo, nil
  147. }
  148. // 更新团购
  149. func (p TeamBuyingService) UpdateTeamBuying(param *vo.TeamBuyingUpdateParam) (int64, error) {
  150. newTeamBuying := entity.TeamBuying{
  151. TeamBuyingID: param.TeamBuyingId,
  152. TeamBuyingName: param.TeamBuyingName,
  153. TeamBuyingCategory: param.TeamBuyingCategory,
  154. TeamBuyingPrice: param.TeamBuyingPrice,
  155. PublicCommission: param.PublicCommission,
  156. TeamBuyingDetail: param.TeamBuyingDetail,
  157. TeamBuyingLink: param.TeamBuyingLink,
  158. UpdatedAt: time.Now(),
  159. }
  160. teamBuyingId, err := dao.TeamBuyingDao{}.UpdateTeamBuying(newTeamBuying)
  161. if err != nil {
  162. return 0, err
  163. }
  164. if param.TeamBuyingPhotos != nil {
  165. teamBuyingPhotos := []entity.ProductPhoto{}
  166. for _, photo := range param.TeamBuyingPhotos {
  167. teamBuyingPhoto := entity.ProductPhoto{
  168. PhotoUrl: photo.PhotoUrl,
  169. PhotoUid: photo.PhotoUid,
  170. Symbol: photo.Symbol,
  171. StoreID: teamBuyingId,
  172. ProductPhotoType: 3,
  173. CreatedAt: time.Now(),
  174. }
  175. teamBuyingPhotos = append(teamBuyingPhotos, teamBuyingPhoto)
  176. }
  177. err = dao.ProductPhotoDAO{}.CreateProductPhoto(teamBuyingPhotos)
  178. if err != nil {
  179. return 0, err
  180. }
  181. }
  182. return teamBuyingId, nil
  183. }
  184. // 删除团购
  185. func (p TeamBuyingService) DeleteTeamBuying(param *vo.TeamBuyingUpdateParam) (int64, error) {
  186. err := dao.TeamBuyingDao{}.DeleteTeamBuying(param.TeamBuyingId)
  187. if err != nil {
  188. return 0, err
  189. }
  190. return param.TeamBuyingId, nil
  191. }