12345678910111213141516171819202122232425262728293031 |
- package services
- import (
- "github.com/jinzhu/gorm"
- "goiot-cronapi/models"
- )
- // 保存消息
- func SaveMessage(db *gorm.DB, datas []models.ConsumeData) error {
- var err error
- tx := db.Begin()
- for _, data := range datas {
- rate := data.Rate - data.Ratio
- if rate <= 20 {
- m := models.Message{
- ProjectName: data.ProjectName,
- ConsumeName: data.Name,
- SourceId: data.ID,
- UserId: data.UserId,
- }
- if err = m.Save(tx); err != nil {
- tx.Rollback()
- break
- }
- }
- }
- if nil == err {
- tx.Commit()
- }
- return nil
- }
|