product.go 7.3 KB

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