package services

import (
	"encoding/json"
	"eta/eta_api/models"
	"eta/eta_api/utils"
	"fmt"
)

// the service for log
func AutoInsertLogToDB() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("[AutoInsertLogToDB]", err)
		}
	}()
	for {
		utils.Rc.Brpop(utils.CACHE_KEY_LOGS, func(b []byte) {
			var log models.OperateLogs
			if err := json.Unmarshal(b, &log); err != nil {
				fmt.Println("json unmarshal wrong!")
			}
			if _, err := models.AddLogs(&log); err != nil {
				fmt.Println("AddLogs:", err.Error(), log)
			}
		})
	}
}
