s_t_cooperate.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // 1. 按条件过滤 - 必填条件
  50. db = db.Debug().Model(gorm_model.SupplierTalentCooperate{}).
  51. Where("supplier_id = ?", supplierId).
  52. Where("task_type = ?", taskType)
  53. // 可选条件
  54. if platform != 0 {
  55. db = db.Where("platform = ?", platform)
  56. }
  57. // if nickname != "" {
  58. // db = db.Where("nickname LIKE ?", "%"+nickname+"%") // 假设是模糊查询
  59. // }
  60. // 2. 返回当前页数据并统计总数
  61. var total int64
  62. var cooperateInfo []*gorm_model.SupplierTalentCooperate
  63. // 先统计总数
  64. if err := db.Count(&total).Error; err != nil {
  65. logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
  66. return nil, 0, err
  67. }
  68. // 再查询分页数据
  69. limit := pageSize
  70. offset := pageSize * pageNum // assert pageNum start with 0
  71. err := db.Order("supplier_id desc").
  72. Limit(int(limit)).
  73. Offset(int(offset)).
  74. Find(&cooperateInfo).Error
  75. if err != nil {
  76. logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
  77. return nil, 0, err
  78. }
  79. return cooperateInfo, total, nil
  80. }