package controllers import ( "fmt" "github.com/gin-gonic/gin" "goiot-cronapi/common" "goiot-cronapi/global/orm" "goiot-cronapi/pkg/logger" "goiot-cronapi/services" "math" ) // MaxCountOnce 定义每次计算多少条数据 const MaxCountOnce = 100 // Consume 每天23:00计算一次所有耗材的消耗率 func Consume(c *gin.Context) { // 先查询出总共有多少条记录 var totalCount int64 sql := `SELECT COUNT(id) FROM btk_consumables_management WHERE LENGTH(box_id) > 0 AND utilization_rate > 0 AND consumption_cycle > 0 AND is_delete = 0` orm.Eloquent.Raw(sql).Count(&totalCount) pageCount := int(math.Ceil(float64(totalCount) / float64(MaxCountOnce))) logger.Info(fmt.Sprintf("查询到有%d条记录, 每页处理%d条, 共需处理%d次", totalCount, MaxCountOnce, pageCount)) // 获取每个盒子对应的运行总时长 timeMap := services.GetBoxRunTime(orm.Eloquent) // 开始同步消耗率 for page := 0; page < pageCount; page++ { datas := services.GetConsumeDatas(orm.Eloquent, page, MaxCountOnce) if err := services.UpdateConsumeRate(orm.Eloquent, datas, timeMap); err != nil { logger.Info(fmt.Sprintf("第%d页数据同步失败, 已回滚!!!", page+1)) continue } if err := services.SaveMessage(orm.Eloquent, datas); err != nil { logger.Info(fmt.Sprintf("第%d页数据同步失败, 已回滚!!!", page+1)) continue } logger.Info(fmt.Sprintf("第%d页数据同步成功", page+1)) } logger.Info("同步完成...") common.ReturnSuccess(c, nil) }