product_service.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package service
  2. import (
  3. "errors"
  4. "time"
  5. "youngee_b_api/app/dao"
  6. "youngee_b_api/app/entity"
  7. "youngee_b_api/app/vo"
  8. )
  9. type ProductService struct{}
  10. func (p ProductService) GetTaskProductsByUserId(param vo.GetAllProductParam) (vo.ResultVO, error) {
  11. if param.Page == 0 {
  12. param.Page = 1
  13. }
  14. if param.PageSize == 0 {
  15. param.PageSize = 10
  16. }
  17. var result vo.ResultVO
  18. var products []entity.Product
  19. var err error
  20. var total int64
  21. //if param.SubAccountId == 0 {
  22. // enterpriseId := param.EnterpriseId
  23. // if enterpriseId == "" {
  24. // return result, errors.New("enterpriseId is empty")
  25. // }
  26. // products, total, err = (&dao.ProductDAO{}).GetProductsByEnterpriseID(enterpriseId, param.Page, param.PageSize)
  27. //} else {
  28. // products, total, err = (&dao.ProductDAO{}).GetProductsBySubAccountId(param.SubAccountId, param.Page, param.PageSize)
  29. //}
  30. enterpriseId := param.EnterpriseId
  31. if enterpriseId == "" {
  32. return result, errors.New("enterpriseId is empty")
  33. }
  34. products, total, err = (&dao.ProductDAO{}).GetProductsByEnterpriseIDAndTypeTitle(enterpriseId, param.ProductType, param.ProductTitle, param.Page, param.PageSize)
  35. if err != nil {
  36. // 数据库查询error
  37. return result, err
  38. }
  39. var reProducts []vo.ReProductPreview
  40. var creatorName string
  41. for _, product := range products {
  42. photoUrl, e := dao.ProductPhotoDAO{}.GetMainPhotoByProductID(product.ProductID)
  43. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(product.EnterpriseID)
  44. if err == nil && enterprise != nil {
  45. creatorName = enterprise.BusinessName
  46. }
  47. if e != nil {
  48. photoUrl = ""
  49. }
  50. reProduct := vo.ReProductPreview{
  51. ProductID: product.ProductID,
  52. ProductName: product.ProductName,
  53. ProductType: product.ProductType,
  54. ProductCategory: product.ProductCategory,
  55. ProductPrice: product.ProductPrice,
  56. ProductDetail: product.ProductDetail,
  57. CreatedAt: product.CreatedAt.Format("2006-01-02 15:04:05"),
  58. PhotoUrl: photoUrl,
  59. CreateName: creatorName,
  60. }
  61. reProducts = append(reProducts, reProduct)
  62. }
  63. result = vo.ResultVO{
  64. Page: param.Page,
  65. PageSize: param.PageSize,
  66. Total: total,
  67. Data: reProducts,
  68. }
  69. return result, nil
  70. }
  71. func (p ProductService) CreateProduct(productCreateParam *vo.ProductCreateParam) (int64, error) {
  72. product := entity.Product{
  73. ProductType: productCreateParam.ProductType,
  74. KuaishouProductId: productCreateParam.ProductId,
  75. ProductName: productCreateParam.ProductName,
  76. ProductCategory: productCreateParam.ProductCategory,
  77. ProductPrice: productCreateParam.ProductPrice,
  78. ProductDetail: productCreateParam.ProductDetail,
  79. EnterpriseID: productCreateParam.EnterpriseId,
  80. SubAccountID: productCreateParam.SubAccountId,
  81. ExclusiveCommission: productCreateParam.ExclusiveCommission,
  82. CommissionPrice: productCreateParam.CommissionPrice,
  83. PublicCommission: productCreateParam.PublicCommission,
  84. ProductUrl: productCreateParam.ProductUrl,
  85. }
  86. productID, err := dao.ProductDAO{}.CreateProduct(product)
  87. if err != nil {
  88. return 0, err
  89. }
  90. if productCreateParam.ProductPhotos != nil {
  91. productPhotos := []entity.ProductPhoto{}
  92. for _, photo := range productCreateParam.ProductPhotos {
  93. productPhoto := entity.ProductPhoto{
  94. PhotoUrl: photo.PhotoUrl,
  95. PhotoUid: photo.PhotoUid,
  96. Symbol: photo.Symbol,
  97. ProductID: productID,
  98. CreatedAt: time.Now(),
  99. }
  100. productPhotos = append(productPhotos, productPhoto)
  101. }
  102. err = dao.ProductPhotoDAO{}.CreateProductPhoto(productPhotos)
  103. if err != nil {
  104. return 0, err
  105. }
  106. }
  107. return productID, nil
  108. }
  109. // 商品详情
  110. func (s ProductService) GetProductDetail(param *vo.ProductSearchParam) (*vo.ReProductInfo, error) {
  111. var reProductInfo *vo.ReProductInfo
  112. product, err := dao.ProductDAO{}.GetProductByID(param.ProductId)
  113. if err != nil {
  114. return nil, err
  115. }
  116. photoMain, _ := dao.ProductPhotoDAO{}.GetMainPhotoByProductID(param.ProductId)
  117. photosAll, _ := dao.ProductPhotoDAO{}.GetProductPhotoByProductID(param.ProductId)
  118. var photoRotates []vo.Photo
  119. var photoDetails []vo.Photo
  120. for _, photosOne := range photosAll {
  121. if photosOne.Symbol == 2 || photosOne.Symbol == 3 {
  122. photo := vo.Photo{
  123. PhotoUrl: photosOne.PhotoUrl,
  124. Symbol: photosOne.Symbol,
  125. }
  126. photoRotates = append(photoRotates, photo)
  127. } else if photosOne.Symbol == 4 || photosOne.Symbol == 5 {
  128. photo := vo.Photo{
  129. PhotoUrl: photosOne.PhotoUrl,
  130. Symbol: photosOne.Symbol,
  131. }
  132. photoDetails = append(photoDetails, photo)
  133. }
  134. }
  135. reProductInfo = &vo.ReProductInfo{
  136. ProductID: product.ProductID,
  137. ProductName: product.ProductName,
  138. ProductCategory: product.ProductCategory,
  139. ProductPrice: product.ProductPrice,
  140. ExclusiveCommission: product.ExclusiveCommission,
  141. CommissionPrice: product.CommissionPrice,
  142. ProductDetail: product.ProductDetail,
  143. ProductUrl: product.ProductUrl,
  144. PhotoMain: photoMain,
  145. PhotoRotates: photoRotates,
  146. PhotoDetails: photoDetails,
  147. CreatedAt: product.CreatedAt.Format("2006-01-02 15:04:05"),
  148. }
  149. return reProductInfo, nil
  150. }
  151. // 更新商品
  152. func (p ProductService) UpdateProduct(param *vo.ProductUpdateParam) (int64, error) {
  153. newProduct := entity.Product{
  154. ProductID: param.ProductId,
  155. ProductName: param.ProductName,
  156. ProductCategory: param.ProductCategory,
  157. ProductPrice: param.ProductPrice,
  158. ExclusiveCommission: param.ExclusiveCommission,
  159. CommissionPrice: param.CommissionPrice,
  160. ProductDetail: param.ProductDetail,
  161. ProductUrl: param.ProductUrl,
  162. UpdatedAt: time.Now(),
  163. }
  164. productId, err := dao.ProductDAO{}.UpdateProduct(newProduct)
  165. if err != nil {
  166. return 0, err
  167. }
  168. if param.ProductPhotos != nil {
  169. productPhotos := []entity.ProductPhoto{}
  170. for _, photo := range param.ProductPhotos {
  171. productPhoto := entity.ProductPhoto{
  172. PhotoUrl: photo.PhotoUrl,
  173. PhotoUid: photo.PhotoUid,
  174. Symbol: photo.Symbol,
  175. StoreID: productId,
  176. ProductPhotoType: 1,
  177. CreatedAt: time.Now(),
  178. }
  179. productPhotos = append(productPhotos, productPhoto)
  180. }
  181. err = dao.ProductPhotoDAO{}.CreateProductPhoto(productPhotos)
  182. if err != nil {
  183. return 0, err
  184. }
  185. }
  186. return productId, nil
  187. }
  188. // 删除商品
  189. func (p ProductService) DeleteProduct(param *vo.ProductUpdateParam) (int64, error) {
  190. err := dao.ProductDAO{}.DeleteProduct(param.ProductId)
  191. if err != nil {
  192. return 0, err
  193. }
  194. return param.ProductId, nil
  195. }
  196. // 获取商品类目
  197. func (p ProductService) GetProductCategorys() ([]string, error) {
  198. var categorys []string
  199. productCategorys, err := dao.ProductDAO{}.GetProductCategorys()
  200. if err != nil {
  201. return nil, err
  202. }
  203. for _, productCategory := range productCategorys {
  204. categorys = append(categorys, productCategory.ProductCategory)
  205. }
  206. return categorys, nil
  207. }