Skip to content

Commit

Permalink
fix: api rate
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Oct 10, 2024
1 parent 0fc9d55 commit 5547fca
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions internal/collections/infra/mysql_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ func (r mysqlRepo) updateOrCreateSubjectCollection(
return errgo.Trace(err)
}

originalCollection := *obj

if err = r.updateSubjectCollection(obj, &original, s, at, ip, created); err != nil {
return errgo.Trace(err)
}
Expand Down Expand Up @@ -194,6 +196,11 @@ func (r mysqlRepo) updateOrCreateSubjectCollection(
}

r.updateSubject(ctx, subject.ID)

if obj.Rate != originalCollection.Rate {
r.reCountSubjectRate(ctx, subject.ID, originalCollection.Rate, obj.Rate)

Check failure on line 201 in internal/collections/infra/mysql_repo.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `r.reCountSubjectRate` is not checked (errcheck)
}

return nil
}

Expand Down Expand Up @@ -587,6 +594,59 @@ func (r mysqlRepo) reCountSubjectCollection(ctx context.Context, subjectID model
})
}

func (r mysqlRepo) reCountSubjectRate(ctx context.Context, subjectID model.SubjectID, before uint8, after uint8) error {
var counts []struct {
Rate uint8 `gorm:"rate"`
Total uint32 `gorm:"total"`
}

return r.q.Transaction(func(tx *query.Query) error {
err := tx.DB().WithContext(ctx).Raw(`
select interest_rate as rate, count(interest_rate) as total from chii_subject_interests
where interest_subject_id = ? and interest_private = 0 and interest_rate != 0 and ((interest_rate = ?) or (interest_rate = ?))
group by interest_rate
`, subjectID, before, after).Scan(&counts).Error
if err != nil {
return errgo.Wrap(err, "dal")
}

var updater = make([]field.AssignExpr, 0, 2)
for _, count := range counts {
switch count.Rate {
case 0:
continue
case 1:
updater = append(updater, tx.SubjectField.Rate1.Value(count.Total))
case 2:
updater = append(updater, tx.SubjectField.Rate2.Value(count.Total))
case 3:
updater = append(updater, tx.SubjectField.Rate3.Value(count.Total))
case 4:

Check failure on line 624 in internal/collections/infra/mysql_repo.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 4, in <case> detected (mnd)
updater = append(updater, tx.SubjectField.Rate4.Value(count.Total))
case 5:

Check failure on line 626 in internal/collections/infra/mysql_repo.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 5, in <case> detected (mnd)
updater = append(updater, tx.SubjectField.Rate5.Value(count.Total))
case 6:

Check failure on line 628 in internal/collections/infra/mysql_repo.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 6, in <case> detected (mnd)
updater = append(updater, tx.SubjectField.Rate6.Value(count.Total))
case 7:

Check failure on line 630 in internal/collections/infra/mysql_repo.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 7, in <case> detected (mnd)
updater = append(updater, tx.SubjectField.Rate7.Value(count.Total))
case 8:

Check failure on line 632 in internal/collections/infra/mysql_repo.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 8, in <case> detected (mnd)
updater = append(updater, tx.SubjectField.Rate8.Value(count.Total))
case 9:

Check failure on line 634 in internal/collections/infra/mysql_repo.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 9, in <case> detected (mnd)
updater = append(updater, tx.SubjectField.Rate9.Value(count.Total))
case 10:
updater = append(updater, tx.SubjectField.Rate10.Value(count.Total))
}
}

_, err = tx.SubjectField.WithContext(ctx).Where(r.q.SubjectField.Sid.Eq(subjectID)).UpdateSimple(updater...)
if err != nil {
return errgo.Wrap(err, "dal")
}

return nil
})
}

func (r mysqlRepo) updateCollectionTime(obj *dao.SubjectCollection,
t collection.SubjectCollection, at time.Time) error {
switch t {
Expand Down

0 comments on commit 5547fca

Please sign in to comment.