product.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package db
  2. import (
  3. "context"
  4. "github.com/sirupsen/logrus"
  5. "youngee_b_api/consts"
  6. "youngee_b_api/model/gorm_model"
  7. "youngee_b_api/model/http_model"
  8. "gorm.io/gorm"
  9. )
  10. func CreateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
  11. db := GetReadDB(ctx)
  12. err := db.Create(&product).Error
  13. if err != nil {
  14. return nil, err
  15. }
  16. return &product.ProductID, nil
  17. }
  18. func UpdateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
  19. db := GetReadDB(ctx)
  20. err := db.Model(&product).Updates(product).Error
  21. if err != nil {
  22. return nil, err
  23. }
  24. return &product.ProductID, nil
  25. }
  26. func GetProductByEnterpriseID(ctx context.Context, enterpriseID string) ([]gorm_model.YounggeeProduct, error) {
  27. db := GetReadDB(ctx)
  28. products := []gorm_model.YounggeeProduct{}
  29. err := db.Where("enterprise_id = ?", enterpriseID).Find(&products).Error
  30. if err != nil {
  31. return nil, err
  32. }
  33. return products, nil
  34. }
  35. func GetProductByID(ctx context.Context, productID int64) (*gorm_model.YounggeeProduct, error) {
  36. db := GetReadDB(ctx)
  37. product := &gorm_model.YounggeeProduct{}
  38. err := db.First(&product, productID).Error
  39. if err != nil {
  40. if err == gorm.ErrRecordNotFound {
  41. return nil, nil
  42. } else {
  43. return nil, err
  44. }
  45. }
  46. return product, nil
  47. }
  48. func GetProductIDByName(ctx context.Context, brandName string, productName string) (*int64, error) {
  49. db := GetReadDB(ctx)
  50. product := &gorm_model.YounggeeProduct{}
  51. err := db.Where("product_name = ? AND brand_name = ?", productName, brandName).First(&product).Error
  52. if err != nil {
  53. if err == gorm.ErrRecordNotFound {
  54. return nil, nil
  55. } else {
  56. return nil, err
  57. }
  58. }
  59. return &product.ProductID, nil
  60. }
  61. func GetProductInfoBySelectionId(ctx context.Context, selectionId string) (*gorm_model.YounggeeProduct, error) {
  62. db := GetReadDB(ctx)
  63. productId := 0
  64. err := db.Model(gorm_model.YounggeeSelectionInfo{}).Select("product_id").Where("selection_id = ?", selectionId).Find(&productId).Error
  65. if err != nil {
  66. logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err)
  67. return nil, err
  68. }
  69. productInfo := gorm_model.YounggeeProduct{}
  70. err = db.Model(gorm_model.YounggeeProduct{}).Where("product_id = ?", productId).Find(&productInfo).Error
  71. if err != nil {
  72. logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err)
  73. return nil, err
  74. }
  75. return &productInfo, nil
  76. }
  77. func GetProductPhotoInfoBySelectionId(ctx context.Context, selectionId string) ([]*gorm_model.YounggeeProductPhoto, error) {
  78. db := GetReadDB(ctx)
  79. productId := 0
  80. err := db.Model(gorm_model.YounggeeSelectionInfo{}).Select("product_id").Where("selection_id = ?", selectionId).Find(&productId).Error
  81. if err != nil {
  82. logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err)
  83. return nil, err
  84. }
  85. var productPhotoInfo []*gorm_model.YounggeeProductPhoto
  86. err = db.Model(gorm_model.YounggeeProductPhoto{}).Where("product_id = ?", productId).Find(&productPhotoInfo).Error
  87. if err != nil {
  88. logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err)
  89. return nil, err
  90. }
  91. return productPhotoInfo, nil
  92. }
  93. func GetAllProduct(ctx context.Context, req *http_model.GetAllProductRequest, enterpriseID string) ([]*http_model.ProjectBriefInfo, int64, error) {
  94. var ProjectBriefInfos []*http_model.ProjectBriefInfo
  95. db := GetReadDB(ctx)
  96. var projectInfos []*gorm_model.ProjectInfo
  97. db = db.Debug().Model(gorm_model.ProjectInfo{}).Where("project_status>2")
  98. if req.Platform != 0 {
  99. db = db.Model(gorm_model.ProjectInfo{}).Where("project_platform = ?", req.Platform)
  100. }
  101. if req.FeeForm != 0 {
  102. db = db.Model(gorm_model.ProjectInfo{}).Where("fee_form = ?", req.FeeForm)
  103. }
  104. if req.ProjectForm != 0 {
  105. db = db.Model(gorm_model.ProjectInfo{}).Where("project_form = ?", req.ProjectForm)
  106. }
  107. // 查询总数
  108. var total int64
  109. if err := db.Count(&total).Error; err != nil {
  110. logrus.WithContext(ctx).Errorf("[GetAllProduct] error query mysql total, err:%+v", err)
  111. return nil, 0, err
  112. }
  113. // 查询该页数据
  114. limit := req.PageSize
  115. offset := req.PageSize * req.PageNum // assert pageNum start with 0
  116. err := db.Order("submit_at desc").Limit(int(limit)).Offset(int(offset)).Find(&projectInfos).Error
  117. if err != nil {
  118. logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
  119. return nil, 0, err
  120. }
  121. projectIdToRecruitStrategy := make(map[string][]*gorm_model.RecruitStrategy)
  122. platformToPlatformIcon := make(map[int64]string)
  123. projectIdToSignNum := make(map[string]int64)
  124. for _, projectInfo := range projectInfos {
  125. var RecruitStrategys []*gorm_model.RecruitStrategy
  126. db1 := GetReadDB(ctx)
  127. err := db1.Debug().Model(gorm_model.RecruitStrategy{}).Where("project_id = ?", projectInfo.ProjectID).Find(&RecruitStrategys).Error
  128. if err != nil {
  129. logrus.WithContext(ctx).Errorf("[GetAllProduct] error query RecruitStrategys, err:%+v", err)
  130. return nil, 0, err
  131. }
  132. projectIdToRecruitStrategy[projectInfo.ProjectID] = RecruitStrategys
  133. }
  134. for _, projectInfo := range projectInfos {
  135. PlatformIcon := ""
  136. db1 := GetReadDB(ctx)
  137. err := db1.Debug().Model(gorm_model.InfoThirdPlatform{}).Select("platform_icon").Where("platform_id = ?", projectInfo.ProjectPlatform).Find(&PlatformIcon).Error
  138. if err != nil {
  139. logrus.WithContext(ctx).Errorf("[GetAllProduct] error query PlatformIcon, err:%+v", err)
  140. return nil, 0, err
  141. }
  142. platformToPlatformIcon[projectInfo.ProjectPlatform] = PlatformIcon
  143. }
  144. for _, projectInfo := range projectInfos {
  145. var SignNum int64
  146. db1 := GetReadDB(ctx)
  147. err := db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("project_id = ?", projectInfo.ProjectID).Count(&SignNum).Error
  148. if err != nil {
  149. logrus.WithContext(ctx).Errorf("[GetAllProduct] error query SignNum, err:%+v", err)
  150. return nil, 0, err
  151. }
  152. projectIdToSignNum[projectInfo.ProjectID] = SignNum
  153. }
  154. for _, projectInfo := range projectInfos {
  155. ProjectBriefInfo := new(http_model.ProjectBriefInfo)
  156. ProjectBriefInfo.ProjectID = projectInfo.ProjectID
  157. ProjectBriefInfo.ProjectForm = projectInfo.ProjectForm
  158. ProjectBriefInfo.ProjectName = projectInfo.ProjectName
  159. ProjectBriefInfo.ProductSnap = projectInfo.ProductSnap
  160. ProjectBriefInfo.ProductPhotoSnap = projectInfo.ProductPhotoSnap
  161. ProjectBriefInfo.Platform = consts.GetProjectPlatform(projectInfo.ProjectPlatform)
  162. ProjectBriefInfo.PlatformIcon = platformToPlatformIcon[projectInfo.ProjectPlatform]
  163. ProjectBriefInfo.RecruitStrategys = projectIdToRecruitStrategy[projectInfo.ProjectID]
  164. ProjectBriefInfo.SignNum = projectIdToSignNum[projectInfo.ProjectID]
  165. ProjectBriefInfos = append(ProjectBriefInfos, ProjectBriefInfo)
  166. }
  167. return ProjectBriefInfos, total, nil
  168. }
  169. func GetProductType(ctx context.Context, selectionId string) (*int, error) {
  170. db := GetReadDB(ctx)
  171. var productId int
  172. err := db.Model(gorm_model.YounggeeSelectionInfo{}).Select("product_id").Where("selection_id = ?", selectionId).Find(&productId).Error
  173. if err != nil || productId == 0 {
  174. logrus.WithContext(ctx).Errorf("[GetProductType] error query mysql, err:%+v", err)
  175. return nil, err
  176. }
  177. var productType int
  178. err = db.Model(gorm_model.YounggeeProduct{}).Select("product_type").Where("product_id = ?", productId).Find(&productType).Error
  179. if err != nil || productType == 0 {
  180. logrus.WithContext(ctx).Errorf("[GetProductType] error query mysql, err:%+v", err)
  181. return nil, err
  182. }
  183. return &productType, nil
  184. }