s_t_cooperate.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package db
  2. import (
  3. "context"
  4. "github.com/sirupsen/logrus"
  5. "gorm.io/gorm"
  6. "youngee_b_api/model/gorm_model"
  7. )
  8. // CreateSTCooperateInfo 创建服务商-达人合作关系
  9. func CreateSTCooperateInfo(ctx context.Context, cooperateInfo *gorm_model.SupplierTalentCooperate) error {
  10. db := GetWriteDB(ctx)
  11. err := db.Create(&cooperateInfo).Error
  12. if err != nil {
  13. return err
  14. }
  15. return nil
  16. }
  17. // CountCooperateInfoBySupplierAndPlatform 查找服务商-商家是否建立合作关系
  18. func CountCooperateInfoBySupplierAndPlatform(ctx context.Context, supplierId int, platformUserId int) (int64, error) {
  19. db := GetReadDB(ctx)
  20. whereCondition := gorm_model.SupplierTalentCooperate{
  21. PlatformUserId: platformUserId,
  22. SupplierId: supplierId,
  23. }
  24. db = db.Debug().Model(gorm_model.SupplierTalentCooperate{}).Where(whereCondition)
  25. var total int64
  26. if err := db.Count(&total).Error; err != nil {
  27. logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
  28. return 0, err
  29. }
  30. return total, nil
  31. }
  32. // UpdateSTCooperateInfo 更新服务商-达人合作关系
  33. func UpdateSTCooperateInfo(ctx context.Context, supplierId int, platformUserId int) error {
  34. db := GetWriteDB(ctx)
  35. whereCondition := gorm_model.SupplierTalentCooperate{SupplierId: supplierId, PlatformUserId: platformUserId}
  36. err := db.Model(&gorm_model.SupplierTalentCooperate{}).
  37. Where(whereCondition).
  38. Update("cooperate_num", gorm.Expr("cooperate_num + 1")).
  39. Error
  40. if err != nil {
  41. return err
  42. }
  43. return nil
  44. }
  45. // GetSTCooperateInfo 查找合作列表
  46. func GetSTCooperateInfo(ctx context.Context, supplierId int, taskType int, platform int, nickname string, pageSize, pageNum int64) ([]*gorm_model.SupplierTalentCooperate, int64, error) {
  47. db := GetReadDB(ctx)
  48. whereCondition := gorm_model.SupplierTalentCooperate{
  49. SupplierId: supplierId,
  50. Platform: platform,
  51. TaskType: taskType,
  52. }
  53. // 1. 按条件过滤
  54. db = db.Debug().Model(gorm_model.SupplierTalentCooperate{}).Where(whereCondition)
  55. // 2. 返回当前页数据并统计总数
  56. var total int64
  57. var cooperateInfp []*gorm_model.SupplierTalentCooperate
  58. if err := db.Count(&total).Error; err != nil {
  59. logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
  60. return nil, 0, err
  61. }
  62. limit := pageSize
  63. offset := pageSize * pageNum // assert pageNum start with 0
  64. err := db.Order("supplier_id desc").Limit(int(limit)).Offset(int(offset)).Find(&cooperateInfp).Error
  65. if err != nil {
  66. logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
  67. return nil, 0, err
  68. }
  69. return cooperateInfp, total, nil
  70. }