package services

import (
	"encoding/json"
	"fmt"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/utils"
)

// 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)
			}
		})
	}
}
