update getting file recipe

This commit is contained in:
Kenta420 2023-10-24 18:01:52 +07:00
parent ea506b8128
commit 3bfbbd778a
10 changed files with 243 additions and 152 deletions

View file

@ -3,9 +3,6 @@ package data
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"recipe-manager/helpers"
"recipe-manager/models"
"recipe-manager/services/logger"
@ -18,40 +15,18 @@ var (
Log = logger.GetInstance()
)
func readFile(version string) *models.Recipe {
path := filepath.Join("cofffeemachineConfig", version)
file, err := os.Open(path)
if err != nil {
log.Fatalf("Error when open file: %s", err)
return nil
}
defer file.Close()
var data *models.Recipe
err = json.NewDecoder(file).Decode(&data)
if err != nil {
log.Fatalf("Error when decode file: %s", err)
return nil
}
return data
}
type RecipeWithTimeStamps struct {
Recipe models.Recipe
TimeStamps int64
}
type Data struct {
CurrentVersion string
AllRecipeFiles map[string][]helpers.RecipePath
currentRecipe *models.Recipe
recipeMap map[string]RecipeWithTimeStamps
Countries []helpers.CountryName
CurrentFile string
CurrentCountryID string
AllRecipeFiles map[string][]helpers.RecipePath
currentRecipe *models.Recipe
recipeMap map[string]RecipeWithTimeStamps
Countries []helpers.CountryName
}
func NewData() *Data {
@ -70,15 +45,17 @@ func NewData() *Data {
allRecipeFiles := helpers.ScanRecipeFiles(countries)
defaultVersion := "coffeethai02_580.json"
defaultRecipe := readFile(defaultVersion)
defaultFile := "coffeethai02_580.json"
defaultCountry := "thai"
defaultRecipe := helpers.ReadRecipeFile(defaultCountry, defaultFile)
return &Data{
CurrentVersion: defaultVersion,
AllRecipeFiles: allRecipeFiles,
currentRecipe: defaultRecipe,
CurrentFile: defaultFile,
CurrentCountryID: defaultCountry,
AllRecipeFiles: allRecipeFiles,
currentRecipe: defaultRecipe,
recipeMap: map[string]RecipeWithTimeStamps{
defaultVersion: {
defaultFile: {
Recipe: *defaultRecipe,
TimeStamps: time.Now().Unix(),
},
@ -87,22 +64,26 @@ func NewData() *Data {
}
}
func (d *Data) GetRecipe(version string) models.Recipe {
func (d *Data) GetRecipe(countryID, filename string) models.Recipe {
if version == "" || version == d.CurrentVersion {
if countryID == "" {
return *d.currentRecipe
}
log.Println("Change recipe to version:", version)
if filename == "" || filename == d.CurrentFile {
return *d.currentRecipe
}
if recipe, ok := d.recipeMap[version]; ok {
d.CurrentVersion = version
if recipe, ok := d.recipeMap[filename]; ok {
d.CurrentFile = filename
d.CurrentCountryID = countryID
return recipe.Recipe
}
// change current version and read new recipe
d.CurrentVersion = version
d.currentRecipe = readFile(version)
d.CurrentFile = filename
d.CurrentCountryID = countryID
d.currentRecipe = helpers.ReadRecipeFile(countryID, filename)
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -118,7 +99,7 @@ func (d *Data) GetRecipe(version string) models.Recipe {
delete(d.recipeMap, oldestVersion)
}
d.recipeMap[version] = RecipeWithTimeStamps{
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
TimeStamps: time.Now().Unix(),
}
@ -151,22 +132,29 @@ func (d *Data) SetValuesToRecipe(recipe models.Recipe01) {
}
}
func (d *Data) GetMaterialSetting(version string) []models.MaterialSetting {
func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialSetting {
result := make([]models.MaterialSetting, 0)
if version == "" || version == d.CurrentVersion {
if countryID == "" {
copy(result, d.currentRecipe.MaterialSetting)
return result
}
if recipe, ok := d.recipeMap[version]; ok {
copy(result, recipe.Recipe.MaterialSetting)
d.CurrentVersion = version
if filename == "" || filename == d.CurrentFile {
copy(result, d.currentRecipe.MaterialSetting)
return result
}
d.CurrentVersion = version
d.currentRecipe = readFile(version)
if recipe, ok := d.recipeMap[filename]; ok {
copy(result, recipe.Recipe.MaterialSetting)
d.CurrentFile = filename
d.CurrentCountryID = countryID
return result
}
d.CurrentFile = filename
d.CurrentCountryID = countryID
d.currentRecipe = helpers.ReadRecipeFile(countryID, filename)
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -182,7 +170,7 @@ func (d *Data) GetMaterialSetting(version string) []models.MaterialSetting {
delete(d.recipeMap, oldestVersion)
}
d.recipeMap[version] = RecipeWithTimeStamps{
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
TimeStamps: time.Now().Unix(),
}
@ -191,17 +179,18 @@ func (d *Data) GetMaterialSetting(version string) []models.MaterialSetting {
return result
}
func (d *Data) GetMaterialCode(ids []uint64, version string) []models.MaterialCode {
func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []models.MaterialCode {
var result []models.MaterialCode
if version == "" || version == d.CurrentVersion {
if filename == "" || filename == d.CurrentFile {
result = d.currentRecipe.MaterialCode
} else if recipe, ok := d.recipeMap[version]; ok {
d.CurrentVersion = version
} else if recipe, ok := d.recipeMap[filename]; ok {
d.CurrentFile = filename
return recipe.Recipe.MaterialCode
} else {
d.CurrentVersion = version
d.currentRecipe = readFile(version)
d.CurrentFile = filename
d.CurrentCountryID = countryID
d.currentRecipe = helpers.ReadRecipeFile(countryID, filename)
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -217,7 +206,7 @@ func (d *Data) GetMaterialCode(ids []uint64, version string) []models.MaterialCo
delete(d.recipeMap, oldestVersion)
}
d.recipeMap[version] = RecipeWithTimeStamps{
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
TimeStamps: time.Now().Unix(),
}