s_t_cooperate.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, taskType int) (int64, error) {
  19. db := GetReadDB(ctx)
  20. whereCondition := gorm_model.SupplierTalentCooperate{
  21. PlatformUserId: platformUserId,
  22. SupplierId: supplierId,
  23. TaskType: taskType,
  24. }
  25. db = db.Debug().Model(gorm_model.SupplierTalentCooperate{}).Where(whereCondition)
  26. var total int64
  27. if err := db.Count(&total).Error; err != nil {
  28. logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
  29. return 0, err
  30. }
  31. return total, nil
  32. }
  33. // UpdateSTCooperateInfo 更新服务商-达人合作关系
  34. func UpdateSTCooperateInfo(ctx context.Context, supplierId int, platformUserId int, taskType int) error {
  35. db := GetWriteDB(ctx)
  36. whereCondition := gorm_model.SupplierTalentCooperate{SupplierId: supplierId, PlatformUserId: platformUserId, TaskType: taskType}
  37. err := db.Model(&gorm_model.SupplierTalentCooperate{}).
  38. Where(whereCondition).
  39. Update("cooperate_num", gorm.Expr("cooperate_num + 1")).
  40. Error
  41. if err != nil {
  42. return err
  43. }
  44. return nil
  45. }
  46. // GetSTCooperateInfo 查找合作列表
  47. func GetSTCooperateInfo(ctx context.Context, supplierId int, taskType int, platform int, nickname string, pageSize, pageNum int64) ([]*gorm_model.SupplierTalentCooperate, int64, error) {
  48. db := GetReadDB(ctx)
  49. whereCondition := gorm_model.SupplierTalentCooperate{
  50. SupplierId: supplierId,
  51. Platform: platform,
  52. TaskType: taskType,
  53. }
  54. // 1. 按条件过滤
  55. db = db.Debug().Model(gorm_model.SupplierTalentCooperate{}).Where(whereCondition)
  56. // 2. 返回当前页数据并统计总数
  57. var total int64
  58. var cooperateInfp []*gorm_model.SupplierTalentCooperate
  59. if err := db.Count(&total).Error; err != nil {
  60. logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
  61. return nil, 0, err
  62. }
  63. limit := pageSize
  64. offset := pageSize * pageNum // assert pageNum start with 0
  65. err := db.Order("supplier_id desc").Limit(int(limit)).Offset(int(offset)).Find(&cooperateInfp).Error
  66. if err != nil {
  67. logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
  68. return nil, 0, err
  69. }
  70. return cooperateInfp, total, nil
  71. }