Taobin-Recipe-Manager/server/data/data.go
2023-10-20 17:51:07 +07:00

275 lines
5.8 KiB
Go

package data
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"recipe-manager/helpers"
"recipe-manager/models"
"recipe-manager/services/logger"
"time"
"go.uber.org/zap"
)
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
}
func NewData() *Data {
countries := []helpers.CountryName{{
CountryID: "thai",
CountryName: "Thailand",
}, {
CountryID: "mys",
CountryName: "Malaysia",
}, {
CountryID: "aus",
CountryName: "Australia",
},
}
allRecipeFiles := helpers.ScanRecipeFiles(countries)
defaultVersion := "coffeethai02_580.json"
defaultRecipe := readFile(defaultVersion)
return &Data{
CurrentVersion: defaultVersion,
AllRecipeFiles: allRecipeFiles,
currentRecipe: defaultRecipe,
recipeMap: map[string]RecipeWithTimeStamps{
defaultVersion: {
Recipe: *defaultRecipe,
TimeStamps: time.Now().Unix(),
},
},
Countries: countries,
}
}
func (d *Data) GetRecipe(version string) models.Recipe {
if version == "" || version == d.CurrentVersion {
return *d.currentRecipe
}
log.Println("Change recipe to version:", version)
if recipe, ok := d.recipeMap[version]; ok {
d.CurrentVersion = version
return recipe.Recipe
}
// change current version and read new recipe
d.CurrentVersion = version
d.currentRecipe = readFile(version)
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
// remove oldest version
var oldestVersion string
var oldestTime int64
for k, v := range d.recipeMap {
if oldestTime == 0 || v.TimeStamps < oldestTime {
oldestTime = v.TimeStamps
oldestVersion = k
}
}
delete(d.recipeMap, oldestVersion)
}
d.recipeMap[version] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
TimeStamps: time.Now().Unix(),
}
return *d.currentRecipe
}
func (d *Data) GetRecipe01() []models.Recipe01 {
return d.currentRecipe.Recipe01
}
func (d *Data) GetRecipe01ByProductCode(code string) models.Recipe01 {
result := make([]models.Recipe01, 0)
for _, v := range d.currentRecipe.Recipe01 {
if v.ProductCode == code {
result = append(result, v)
}
}
return result[0]
}
func (d *Data) SetValuesToRecipe(recipe models.Recipe01) {
for _, v := range d.currentRecipe.Recipe01 {
if v.ProductCode == recipe.ProductCode {
v = recipe
break
}
}
}
func (d *Data) GetMaterialSetting(version string) []models.MaterialSetting {
result := make([]models.MaterialSetting, 0)
if version == "" || version == d.CurrentVersion {
copy(result, d.currentRecipe.MaterialSetting)
return result
}
if recipe, ok := d.recipeMap[version]; ok {
copy(result, recipe.Recipe.MaterialSetting)
d.CurrentVersion = version
return result
}
d.CurrentVersion = version
d.currentRecipe = readFile(version)
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
// remove oldest version
var oldestVersion string
var oldestTime int64
for k, v := range d.recipeMap {
if oldestTime == 0 || v.TimeStamps < oldestTime {
oldestTime = v.TimeStamps
oldestVersion = k
}
}
delete(d.recipeMap, oldestVersion)
}
d.recipeMap[version] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
TimeStamps: time.Now().Unix(),
}
copy(result, d.currentRecipe.MaterialSetting)
return result
}
func (d *Data) GetMaterialCode(ids []uint64, version string) []models.MaterialCode {
var result []models.MaterialCode
if version == "" || version == d.CurrentVersion {
result = d.currentRecipe.MaterialCode
} else if recipe, ok := d.recipeMap[version]; ok {
d.CurrentVersion = version
return recipe.Recipe.MaterialCode
} else {
d.CurrentVersion = version
d.currentRecipe = readFile(version)
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
// remove oldest version
var oldestVersion string
var oldestTime int64
for k, v := range d.recipeMap {
if oldestTime == 0 || v.TimeStamps < oldestTime {
oldestTime = v.TimeStamps
oldestVersion = k
}
}
delete(d.recipeMap, oldestVersion)
}
d.recipeMap[version] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
TimeStamps: time.Now().Unix(),
}
result = d.currentRecipe.MaterialCode
}
if len(ids) == 0 {
return result
}
resultFilter := make([]models.MaterialCode, len(ids))
for _, id := range ids {
if id == 0 {
continue
}
for _, m := range result {
if m.MaterialID == id {
resultFilter = append(resultFilter, m)
break
}
}
}
return resultFilter
}
func (d *Data) GetCountryNameByID(countryID string) (string, error) {
for _, country := range d.Countries {
if country.CountryID == countryID {
return country.CountryName, nil
}
}
return "", fmt.Errorf("country ID: %s not found", countryID)
}
func (d *Data) GetCountryIDByName(countryName string) (string, error) {
for _, country := range d.Countries {
if country.CountryName == countryName {
return country.CountryID, nil
}
}
return "", fmt.Errorf("country name: %s not found", countryName)
}
func (d *Data) ExportToJSON() []byte {
b_recipe, err := json.Marshal(d.currentRecipe)
if err != nil {
Log.Error("Error when marshal recipe", zap.Error(err))
return nil
}
return b_recipe
}