1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package db
- import (
- "context"
- "github.com/sirupsen/logrus"
- "gorm.io/gorm"
- "youngee_b_api/model/gorm_model"
- )
- // CreateSTCooperateInfo 创建服务商-达人合作关系
- func CreateSTCooperateInfo(ctx context.Context, cooperateInfo *gorm_model.SupplierTalentCooperate) error {
- db := GetWriteDB(ctx)
- err := db.Create(&cooperateInfo).Error
- if err != nil {
- return err
- }
- return nil
- }
- // CountCooperateInfoBySupplierAndPlatform 查找服务商-商家是否建立合作关系
- func CountCooperateInfoBySupplierAndPlatform(ctx context.Context, supplierId int, platformUserId int) (int64, error) {
- db := GetReadDB(ctx)
- whereCondition := gorm_model.SupplierTalentCooperate{
- PlatformUserId: platformUserId,
- SupplierId: supplierId,
- }
- db = db.Debug().Model(gorm_model.SupplierTalentCooperate{}).Where(whereCondition)
- var total int64
- if err := db.Count(&total).Error; err != nil {
- logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
- return 0, err
- }
- return total, nil
- }
- // UpdateSTCooperateInfo 更新服务商-达人合作关系
- func UpdateSTCooperateInfo(ctx context.Context, supplierId int, platformUserId int) error {
- db := GetWriteDB(ctx)
- whereCondition := gorm_model.SupplierTalentCooperate{SupplierId: supplierId, PlatformUserId: platformUserId}
- err := db.Model(&gorm_model.SupplierTalentCooperate{}).
- Where(whereCondition).
- Update("cooperate_num", gorm.Expr("cooperate_num + 1")).
- Error
- if err != nil {
- return err
- }
- return nil
- }
- // GetSTCooperateInfo 查找合作列表
- func GetSTCooperateInfo(ctx context.Context, supplierId int, taskType int, platform int, nickname string, pageSize, pageNum int64) ([]*gorm_model.SupplierTalentCooperate, int64, error) {
- db := GetReadDB(ctx)
- whereCondition := gorm_model.SupplierTalentCooperate{
- SupplierId: supplierId,
- Platform: platform,
- TaskType: taskType,
- }
- // 1. 按条件过滤
- db = db.Debug().Model(gorm_model.SupplierTalentCooperate{}).Where(whereCondition)
- // 2. 返回当前页数据并统计总数
- var total int64
- var cooperateInfp []*gorm_model.SupplierTalentCooperate
- if err := db.Count(&total).Error; err != nil {
- logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
- return nil, 0, err
- }
- limit := pageSize
- offset := pageSize * pageNum // assert pageNum start with 0
- err := db.Order("supplier_id desc").Limit(int(limit)).Offset(int(offset)).Find(&cooperateInfp).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
- return nil, 0, err
- }
- return cooperateInfp, total, nil
- }
|