264 lines
5.8 KiB
Go
264 lines
5.8 KiB
Go
package data
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"recipe-manager/helpers"
|
|
"recipe-manager/models"
|
|
"recipe-manager/services/logger"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var (
|
|
Log = logger.GetInstance()
|
|
)
|
|
|
|
type RecipeWithTimeStamps struct {
|
|
Recipe models.Recipe
|
|
TimeStamps int64
|
|
}
|
|
|
|
type Data struct {
|
|
CurrentFile string
|
|
CurrentCountryID 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)
|
|
|
|
defaultFile := "coffeethai02_580.json"
|
|
defaultCountry := "thai"
|
|
defaultRecipe := helpers.ReadRecipeFile(defaultCountry, defaultFile)
|
|
|
|
return &Data{
|
|
CurrentFile: defaultFile,
|
|
CurrentCountryID: defaultCountry,
|
|
AllRecipeFiles: allRecipeFiles,
|
|
currentRecipe: defaultRecipe,
|
|
recipeMap: map[string]RecipeWithTimeStamps{
|
|
defaultFile: {
|
|
Recipe: *defaultRecipe,
|
|
TimeStamps: time.Now().Unix(),
|
|
},
|
|
},
|
|
Countries: countries,
|
|
}
|
|
}
|
|
|
|
func (d *Data) GetRecipe(countryID, filename string) models.Recipe {
|
|
|
|
if countryID == "" {
|
|
return *d.currentRecipe
|
|
}
|
|
|
|
if filename == "" || filename == d.CurrentFile {
|
|
return *d.currentRecipe
|
|
}
|
|
|
|
if recipe, ok := d.recipeMap[filename]; ok {
|
|
d.CurrentFile = filename
|
|
d.CurrentCountryID = countryID
|
|
return recipe.Recipe
|
|
}
|
|
|
|
// change current version and read new recipe
|
|
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
|
|
// 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[filename] = 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(countryID, filename string) []models.MaterialSetting {
|
|
result := make([]models.MaterialSetting, 0)
|
|
|
|
if countryID == "" {
|
|
copy(result, d.currentRecipe.MaterialSetting)
|
|
return result
|
|
}
|
|
|
|
if filename == "" || filename == d.CurrentFile {
|
|
copy(result, d.currentRecipe.MaterialSetting)
|
|
return result
|
|
}
|
|
|
|
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
|
|
// 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[filename] = RecipeWithTimeStamps{
|
|
Recipe: *d.currentRecipe,
|
|
TimeStamps: time.Now().Unix(),
|
|
}
|
|
|
|
copy(result, d.currentRecipe.MaterialSetting)
|
|
return result
|
|
}
|
|
|
|
func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []models.MaterialCode {
|
|
var result []models.MaterialCode
|
|
|
|
if filename == "" || filename == d.CurrentFile {
|
|
result = d.currentRecipe.MaterialCode
|
|
} else if recipe, ok := d.recipeMap[filename]; ok {
|
|
d.CurrentFile = filename
|
|
return recipe.Recipe.MaterialCode
|
|
} else {
|
|
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
|
|
// 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[filename] = 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
|
|
}
|