Taobin-Recipe-Manager/server/data/data.go

918 lines
27 KiB
Go
Raw Normal View History

2023-12-06 10:05:16 +07:00
package data
import (
"fmt"
"log"
2024-01-16 15:16:15 +07:00
"os"
"path"
2023-12-06 10:05:16 +07:00
"recipe-manager/helpers"
"recipe-manager/models"
"recipe-manager/services/logger"
2024-01-11 08:12:19 +07:00
"strconv"
2023-12-29 16:10:57 +07:00
"strings"
2023-12-06 10:05:16 +07:00
"time"
2023-12-27 08:38:14 +07:00
"reflect"
2023-12-06 10:05:16 +07:00
"go.uber.org/zap"
)
type RecipeWithTimeStamps struct {
2024-01-18 16:59:06 +07:00
Recipe map[string]*models.Recipe
2023-12-06 10:05:16 +07:00
TimeStamps int64
}
type Data struct {
2024-01-17 17:38:23 +07:00
CurrentFile map[string]string
CurrentCountryID map[string]string
DefaultCountryMap []DefaultByCountry
AllRecipeFiles map[string][]helpers.RecipePath
2024-02-05 17:07:04 +07:00
CurrentRecipe map[string]*models.Recipe
2024-01-17 17:38:23 +07:00
recipeMap map[string]RecipeWithTimeStamps
Countries []helpers.CountryName
taoLogger *logger.TaoLogger
2024-02-05 17:07:04 +07:00
redisClient *RedisCli
2024-01-17 17:38:23 +07:00
}
type DefaultByCountry struct {
CountryShortName string
CountryLongName string
DefaultFileVersion int
2023-12-06 10:05:16 +07:00
}
2023-12-29 16:10:57 +07:00
var (
countries = []helpers.CountryName{{
2023-12-06 10:05:16 +07:00
CountryID: "tha",
CountryName: "Thailand",
}, {
CountryID: "mys",
CountryName: "Malaysia",
}, {
CountryID: "aus",
CountryName: "Australia",
},
}
2023-12-29 16:10:57 +07:00
)
2024-02-05 17:07:04 +07:00
func NewData(taoLogger *logger.TaoLogger, redisClient *RedisCli) *Data {
2023-12-06 10:05:16 +07:00
allRecipeFiles := helpers.ScanRecipeFiles(countries)
defaultFile := "coffeethai02_600.json"
2024-01-11 08:12:19 +07:00
2024-01-17 17:38:23 +07:00
// TODO: read 'version' file by country
2024-01-16 15:16:15 +07:00
2024-01-17 17:38:23 +07:00
// versionPath := path.Join("cofffeemachineConfig", defaultCountry, "version")
// taoLogger.Log.Debug("version", zap.Any("version path", versionPath))
2024-01-16 15:16:15 +07:00
2024-01-17 17:38:23 +07:00
// // versionFile, err := os.Open(versionPath)
// content, err := os.ReadFile(versionPath)
2024-01-16 15:16:15 +07:00
2024-01-17 17:38:23 +07:00
// if err != nil {
// taoLogger.Log.Debug("Error when open version file", zap.Error(err))
// }
2024-01-16 15:16:15 +07:00
2024-01-17 17:38:23 +07:00
// initVersion := string(content)
2024-01-11 08:12:19 +07:00
2024-01-17 17:38:23 +07:00
// // read latest version
// // set latest to default version
// latest_version, err := strconv.Atoi(initVersion)
2024-01-11 08:12:19 +07:00
2024-01-17 17:38:23 +07:00
// if err != nil {
// latest_version = 600
// }
2024-01-11 08:12:19 +07:00
2024-01-17 17:38:23 +07:00
defaultForEachCountry := []DefaultByCountry{}
2024-01-11 08:12:19 +07:00
2024-01-17 17:38:23 +07:00
for _, elem := range countries {
// generate default of all countries
currentVersionPath := path.Join("cofffeemachineConfig", elem.CountryID, "version")
// this is default version for each country
content, err := os.ReadFile(currentVersionPath)
2024-01-11 08:12:19 +07:00
if err != nil {
2024-01-17 17:38:23 +07:00
taoLogger.Log.Debug("Error when open version file", zap.Error(err))
2024-01-11 08:12:19 +07:00
}
2024-01-17 17:38:23 +07:00
initVersion := string(content)
// read latest version
latest_version, _ := strconv.Atoi(initVersion)
defaultForEachCountry = append(defaultForEachCountry, DefaultByCountry{CountryShortName: elem.CountryID, CountryLongName: elem.CountryName, DefaultFileVersion: latest_version})
2024-02-08 13:18:03 +07:00
// emit out latest version
taoLogger.Log.Info("Latest version", zap.Any("country", elem.CountryID), zap.Any("version", latest_version))
2024-01-17 17:38:23 +07:00
}
currentFileMap := make(map[string]string)
CurrentCountryIDMap := make(map[string]string)
2024-01-18 16:59:06 +07:00
currentDefaultFileForEachCountry := make(map[string]*models.Recipe)
2024-01-17 17:38:23 +07:00
// all default versions as string
versionsString := ""
// loop default for each country
for _, v := range defaultForEachCountry {
for _, v2 := range allRecipeFiles[v.CountryShortName] {
// extract filename as version
current_version_iter, err := strconv.Atoi(strings.Split(strings.Split(v2.Name, "_")[1], ".")[0])
if err != nil {
continue
}
if current_version_iter == v.DefaultFileVersion {
currentFileMap[v.CountryShortName] = v2.Name
CurrentCountryIDMap[v.CountryShortName] = v.CountryLongName
versionsString = versionsString + v.CountryShortName + ":" + strconv.Itoa(current_version_iter) + ","
2024-01-18 16:59:06 +07:00
// do read default
defaultRecipe, err := helpers.ReadRecipeFile(v.CountryShortName, v2.Name)
if err != nil {
log.Panic("Error when read default recipe file for each country:", v.CountryShortName, err)
}
2024-02-05 17:07:04 +07:00
redisClient.SetToKey(v2.Name, defaultRecipe)
2024-01-18 16:59:06 +07:00
currentDefaultFileForEachCountry[v.CountryShortName] = defaultRecipe
2024-01-17 17:38:23 +07:00
break
}
2024-01-11 08:12:19 +07:00
}
}
2024-01-17 17:38:23 +07:00
// for _, v := range allRecipeFiles[defaultCountry] {
// // extract filename as version
// current_version_iter, err := strconv.Atoi(strings.Split(strings.Split(v.Name, "_")[1], ".")[0])
// if err != nil {
// continue
// }
// if current_version_iter == latest_version {
// // taoLogger.Log.Debug("current_version_iter", zap.Any("current_version_iter", current_version_iter))
// // set latest
// latest_version = current_version_iter
// defaultFile = v.Name
// break
// }
// }
2024-01-18 16:59:06 +07:00
// FIXME: default file bug. do assign each default recipe model to each country
2024-01-11 08:12:19 +07:00
2024-01-18 16:59:06 +07:00
// taoLogger.Log.Debug("defaultFile", zap.Any("defaultFile", defaultFile), zap.Any("latest_version", versionsString))
2023-12-06 10:05:16 +07:00
2024-01-18 16:59:06 +07:00
// defaultRecipe, err := helpers.ReadRecipeFile(defaultCountry, defaultFile)
// if err != nil {
// log.Panic("Error when read default recipe file:", err)
// }
2023-12-06 10:05:16 +07:00
return &Data{
2024-01-17 17:38:23 +07:00
CurrentFile: currentFileMap,
CurrentCountryID: CurrentCountryIDMap,
2023-12-06 10:05:16 +07:00
AllRecipeFiles: allRecipeFiles,
2024-02-05 17:07:04 +07:00
CurrentRecipe: currentDefaultFileForEachCountry,
2023-12-06 10:05:16 +07:00
recipeMap: map[string]RecipeWithTimeStamps{
defaultFile: {
2024-01-18 16:59:06 +07:00
Recipe: currentDefaultFileForEachCountry,
2023-12-06 10:05:16 +07:00
TimeStamps: time.Now().Unix(),
},
},
2024-01-17 17:38:23 +07:00
Countries: countries,
taoLogger: taoLogger,
DefaultCountryMap: defaultForEachCountry,
2024-02-05 17:07:04 +07:00
redisClient: redisClient,
2023-12-06 10:05:16 +07:00
}
}
func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
2024-01-19 14:59:21 +07:00
d.taoLogger.Log.Debug("invoke GetRecipe", zap.String("countryID", countryID), zap.String("filename", filename))
2024-01-23 16:23:38 +07:00
// TODO: concat submenu into recipe
2023-12-06 10:05:16 +07:00
if countryID == "" {
2024-02-05 17:07:04 +07:00
d.taoLogger.Log.Debug("GetRecipe", zap.Any("EmptyCountryId", "return default country = tha"))
return d.CurrentRecipe["tha"]
2023-12-06 10:05:16 +07:00
}
2024-02-05 17:07:04 +07:00
if filename == "" {
d.taoLogger.Log.Debug("GetRecipe", zap.Any("EmptyFilename", filename))
return d.CurrentRecipe[countryID]
}
// do check if match the current pointer
if d.CurrentFile[countryID] == filename {
d.taoLogger.Log.Debug("GetRecipe",
zap.Any("FileMatchCurrent", "return from stored "+filename),
zap.Any("CurrentFile", d.CurrentFile),
zap.Any("countryID", countryID))
d.taoLogger.Log.Debug("CurrentRecipeOK?", zap.Any("CurrentRecipe", d.CurrentRecipe[countryID] != nil))
2024-02-05 17:07:04 +07:00
// make sure recipe vesion is equal
currentConfig := d.CurrentRecipe[countryID].MachineSetting.ConfigNumber
// get requested version
requestedConfig, _ := strconv.Atoi(strings.Split(strings.Split(filename, "_")[1], ".")[0])
if currentConfig != requestedConfig {
d.taoLogger.Log.Debug("GetRecipe", zap.Any("ActualFileNotMatch", "Skip this!"))
} else {
// detect patches, return original
var cached_original_recipe models.Recipe
err := d.redisClient.GetKeyTo(filename, &cached_original_recipe)
if err == nil && d.redisClient.HealthCheck() == nil {
d.taoLogger.Log.Debug("GetRecipe.NoReturnUpdated", zap.Any("target", filename))
return &cached_original_recipe
}
2024-02-05 17:07:04 +07:00
// if equal, OK
return d.CurrentRecipe[countryID]
}
2023-12-06 10:05:16 +07:00
}
2024-02-05 17:07:04 +07:00
if recipe, ok := d.recipeMap[filename]; ok && d.redisClient.HealthCheck() != nil {
d.taoLogger.Log.Debug("GetRecipe", zap.Any("ValidOnStored", "return from stored "+filename))
// d.CurrentFile[countryID] = filename
2024-01-19 14:59:21 +07:00
// d.CurrentCountryID[countryID] = countryID
2024-02-05 17:07:04 +07:00
// make sure recipe vesion is equal
currentConfig := d.CurrentRecipe[countryID].MachineSetting.ConfigNumber
// get requested version
requestedConfig, _ := strconv.Atoi(strings.Split(strings.Split(filename, "_")[1], ".")[0])
if currentConfig != requestedConfig {
d.taoLogger.Log.Debug("GetRecipe", zap.Any("InvalidOnStored", "Skip this!"))
} else {
// detect patches, return original
var cached_original_recipe models.Recipe
err := d.redisClient.GetKeyTo(filename, &cached_original_recipe)
if err == nil && d.redisClient.HealthCheck() == nil {
d.taoLogger.Log.Debug("GetRecipe.NoReturnUpdated", zap.Any("target", filename))
return &cached_original_recipe
}
d.taoLogger.Log.Debug("GetRecipe.ReturnDefault", zap.Any("error_cache_recipe", err))
2024-02-05 17:07:04 +07:00
// if equal, OK
return recipe.Recipe[countryID]
}
2023-12-06 10:05:16 +07:00
}
// change current version and read new recipe
2024-01-17 17:38:23 +07:00
if filename == "default" {
filename = d.CurrentFile[countryID]
}
2024-02-05 17:07:04 +07:00
// var recipe *models.Recipe = &models.Recipe{}
// do check if redis contains the recipe
var cached_recipe *models.Recipe = &models.Recipe{}
if err := d.redisClient.GetKeyTo(filename, cached_recipe); err != nil {
d.taoLogger.Log.Debug("GetRecipe.Cached", zap.Any("GetCacheRecipeError", err))
d.taoLogger.Log.Debug("GetRecipe", zap.String("filename", filename), zap.String("countryID", countryID))
// d.CurrentCountryID[countryID] = countryID
cached_recipe = nil
}
if cached_recipe != nil {
d.taoLogger.Log.Debug("GetRecipe", zap.Any("Check on cached recipe invalid", cached_recipe == nil), zap.Any("test config number", cached_recipe.MachineSetting.ConfigNumber))
// set to current
// d.CurrentRecipe[countryID] = cached_recipe
return cached_recipe
2024-02-05 17:07:04 +07:00
}
2023-12-06 10:05:16 +07:00
recipe, err := helpers.ReadRecipeFile(countryID, filename)
2024-02-05 17:07:04 +07:00
if err != nil {
d.taoLogger.Log.Debug("GetRecipe", zap.Any("ReadRecipeError -> return default", err))
return d.CurrentRecipe[countryID]
}
// cache to redis
d.redisClient.SetToKey(filename, recipe)
2023-12-06 10:05:16 +07:00
if err != nil {
2024-01-19 14:59:21 +07:00
d.taoLogger.Log.Error("GetRecipe: Error when read recipe file, Return default recipe", zap.Error(err))
2024-02-05 17:07:04 +07:00
return d.CurrentRecipe[countryID]
2023-12-06 10:05:16 +07:00
}
2024-02-05 17:07:04 +07:00
//. service is connected. Use from cache
// check healthcheck redis
var return_recipe *models.Recipe = &models.Recipe{}
2024-02-05 17:07:04 +07:00
err = d.redisClient.HealthCheck()
d.taoLogger.Log.Info("GetRecipe: HealthCheck", zap.Any("result", err))
if d.redisClient.HealthCheck() == nil && cached_recipe != nil {
d.taoLogger.Log.Debug("GetRecipeCached", zap.Any("cached_recipe", "yes"))
// d.CurrentRecipe[countryID] = cached_recipe
return_recipe = cached_recipe
2024-02-05 17:07:04 +07:00
} else {
d.taoLogger.Log.Debug("GetRecipeCached", zap.Any("cached_recipe", "no"))
// d.CurrentRecipe[countryID] = recipe
return_recipe = recipe
2024-02-05 17:07:04 +07:00
}
2023-12-06 10:05:16 +07:00
// 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{
2024-02-05 17:07:04 +07:00
Recipe: d.CurrentRecipe,
2023-12-06 10:05:16 +07:00
TimeStamps: time.Now().Unix(),
}
return return_recipe
2023-12-06 10:05:16 +07:00
}
2024-01-18 16:59:06 +07:00
// func (d *Data) GetRecipe01() []models.Recipe01 {
// return d.currentRecipe.Recipe01
// }
2023-12-06 10:05:16 +07:00
2024-01-18 16:59:06 +07:00
// func (d *Data) GetCurrentRecipe() *models.Recipe {
// return d.currentRecipe
// }
2023-12-06 10:05:16 +07:00
func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) (models.Recipe01, error) {
2024-01-19 14:59:21 +07:00
// try convert
if len(countryID) != 3 {
for k, v := range d.CurrentCountryID {
fmt.Println("GetRecipe01ByProductCode.Iterate", k, v, v == countryID)
if v == countryID {
countryID = k
break
}
}
}
fmt.Println("GetRecipe01ByProductCode", filename, countryID, productCode)
2023-12-29 16:10:57 +07:00
if !strings.Contains(filename, "tmp") {
2024-01-17 17:38:23 +07:00
if filename == "" || filename == d.CurrentFile[countryID] {
2024-01-19 14:59:21 +07:00
// , d.CurrentFile, countryID, "result by country id", len(d.currentRecipe[countryID].Recipe01)
2024-02-02 17:07:49 +07:00
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::filename", filename)
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::countryID", countryID)
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentFile", d.CurrentFile)
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentCountryID", d.CurrentCountryID)
2024-01-19 14:59:21 +07:00
2024-02-05 17:07:04 +07:00
for _, v := range d.CurrentRecipe[countryID].Recipe01 {
2023-12-29 16:10:57 +07:00
if v.ProductCode == productCode {
return v, nil
2024-01-23 16:23:38 +07:00
} else if len(v.SubMenu) > 0 {
for _, subMenu := range v.SubMenu {
if subMenu.ProductCode == productCode {
return subMenu, nil
}
}
2023-12-29 16:10:57 +07:00
}
2023-12-06 10:05:16 +07:00
}
2024-02-02 17:07:49 +07:00
// fmt.Println("No result in current recipe", countryID)
2023-12-29 16:10:57 +07:00
} else if recipe, ok := d.recipeMap[filename]; ok {
2024-02-02 17:07:49 +07:00
// fmt.Println("GetRecipe01ByProductCode.ReadMap", filename, d.CurrentFile, recipe.Recipe[countryID], "countryID=", countryID)
2024-01-18 16:59:06 +07:00
for _, v := range recipe.Recipe[countryID].Recipe01 {
2023-12-29 16:10:57 +07:00
if v.ProductCode == productCode {
2024-01-19 14:59:21 +07:00
d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getSuccess", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
2023-12-29 16:10:57 +07:00
return v, nil
2024-01-23 16:23:38 +07:00
} else if len(v.SubMenu) > 0 {
for _, subMenu := range v.SubMenu {
if subMenu.ProductCode == productCode {
d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getSuccess", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
return subMenu, nil
}
}
2023-12-29 16:10:57 +07:00
}
2023-12-06 10:05:16 +07:00
}
2024-01-19 14:59:21 +07:00
d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getFail", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
2023-12-06 10:05:16 +07:00
}
}
2024-01-17 17:38:23 +07:00
d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("filename", filename), zap.Any("countryID", countryID), zap.Any("productCode", productCode))
if filename == "default" {
filename = d.CurrentFile[countryID]
}
2024-01-19 14:59:21 +07:00
// d.CurrentFile[countryID] = filename
// d.CurrentCountryID[countryID] = countryID
2023-12-29 16:10:57 +07:00
for _, v := range countries {
if v.CountryName == countryID {
2024-01-19 14:59:21 +07:00
// d.CurrentCountryID[countryID] = v.CountryID
2023-12-29 16:10:57 +07:00
countryID = v.CountryID
break
}
}
2024-02-05 17:07:04 +07:00
recipe := d.GetRecipe(countryID, filename)
2023-12-06 10:05:16 +07:00
2024-02-05 17:07:04 +07:00
// if err != nil {
// d.taoLogger.Log.Error("GetRecipe01ByProductCode: Error when read recipe file, Return default recipe", zap.Error(err))
// for _, v := range d.CurrentRecipe[countryID].Recipe01 {
// if v.ProductCode == productCode {
// return v, fmt.Errorf("[DEFAULT]-ERR")
// } else if len(v.SubMenu) > 0 {
// for _, subMenu := range v.SubMenu {
// if subMenu.ProductCode == productCode {
// return subMenu, fmt.Errorf("[DEFAULT]-ERR")
// }
// }
// }
// }
// }
2023-12-06 10:05:16 +07:00
2024-01-11 08:12:19 +07:00
d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("version", recipe.MachineSetting.ConfigNumber))
2024-02-05 17:07:04 +07:00
d.CurrentRecipe[countryID] = recipe
2023-12-06 10:05:16 +07:00
// 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{
2024-02-05 17:07:04 +07:00
Recipe: d.CurrentRecipe,
2023-12-06 10:05:16 +07:00
TimeStamps: time.Now().Unix(),
}
2024-02-05 17:07:04 +07:00
for _, v := range d.CurrentRecipe[countryID].Recipe01 {
2023-12-06 10:05:16 +07:00
if v.ProductCode == productCode {
2023-12-29 16:10:57 +07:00
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", v))
2023-12-06 10:05:16 +07:00
return v, nil
2024-01-23 16:23:38 +07:00
} else if len(v.SubMenu) > 0 {
for _, subMenu := range v.SubMenu {
if subMenu.ProductCode == productCode {
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", subMenu))
return subMenu, nil
}
}
2023-12-06 10:05:16 +07:00
}
}
return models.Recipe01{}, fmt.Errorf("product code: %s not found", productCode)
}
2023-12-27 08:38:14 +07:00
func (d *Data) SetValuesToRecipe(base_recipe []models.Recipe01, recipe models.Recipe01) {
not_found := false
global_idx := 0
for index, v := range base_recipe {
2023-12-06 10:05:16 +07:00
if v.ProductCode == recipe.ProductCode {
// Log.Debug("SetValuesToRecipe", zap.Any("old", v), zap.Any("new", recipe))
// v = recipe
2023-12-27 08:38:14 +07:00
// TODO: change only changed values
// transform to map
base_recipe01_Map := v.ToMap()
recipe01_Map := recipe.ToMap()
for k, v := range recipe01_Map {
if !reflect.DeepEqual(base_recipe01_Map[k], v) {
2024-01-21 15:18:33 +07:00
d.taoLogger.Log.Debug("SetValuesToRecipe", zap.Any("key", k), zap.Any("old", base_recipe01_Map[k]), zap.Any("new", v))
2023-12-27 08:38:14 +07:00
base_recipe01_Map[k] = v
}
}
base_recipe[index] = base_recipe[index].FromMap(base_recipe01_Map)
not_found = false
2023-12-06 10:05:16 +07:00
break
2024-01-23 16:23:38 +07:00
} else if len(v.SubMenu) > 0 {
for _, sub := range v.SubMenu {
if sub.ProductCode == recipe.ProductCode {
// Log.Debug("SetValuesToRecipe.SubMenu", zap.Any("old", sub), zap.Any("new", recipe))
// sub = recipe
// TODO: change only changed values
// transform to map
base_recipe01_Map := sub.ToMap()
recipe01_Map := recipe.ToMap()
for k, v := range recipe01_Map {
if !reflect.DeepEqual(base_recipe01_Map[k], v) {
d.taoLogger.Log.Debug("SetValuesToRecipe.SubMenu", zap.Any("key", k), zap.Any("old", base_recipe01_Map[k]), zap.Any("new", v))
base_recipe01_Map[k] = v
}
}
}
}
2023-12-27 08:38:14 +07:00
} else {
not_found = true
global_idx = index
2023-12-06 10:05:16 +07:00
}
}
2023-12-27 08:38:14 +07:00
if not_found {
base_recipe[global_idx+1] = recipe
}
2023-12-06 10:05:16 +07:00
}
2024-02-07 15:39:19 +07:00
func (d *Data) SetValuesToMaterialSetting(base_mat_setting []models.MaterialSetting, updated_mat_setting models.MaterialSetting) {
not_found := false
global_idx := 0
for index, v := range base_mat_setting {
// find matched id
if v.ID == updated_mat_setting.ID {
// change only changed values
for k, v := range updated_mat_setting.ToMap() {
if !reflect.DeepEqual(base_mat_setting[index].ToMap()[k], v) {
d.taoLogger.Log.Debug("SetValuesToMaterialSetting", zap.Any("key", k), zap.Any("old", base_mat_setting[index].ToMap()[k]), zap.Any("new", v))
base_mat_setting[index].ToMap()[k] = v
}
}
} else {
not_found = true
global_idx = index
}
}
// is new value
if not_found {
base_mat_setting[global_idx+1] = updated_mat_setting
}
}
func (d *Data) SetValuesToToppingList(base_topping_list []models.ToppingList, updated_topping_list models.ToppingList) {
not_found := false
global_idx := 0
for index, v := range base_topping_list {
// find matched id
if v.ID == updated_topping_list.ID {
// change only changed values
for k, v := range updated_topping_list.ToMap() {
if !reflect.DeepEqual(base_topping_list[index].ToMap()[k], v) {
d.taoLogger.Log.Debug("SetValuesToToppingList", zap.Any("key", k), zap.Any("old", base_topping_list[index].ToMap()[k]), zap.Any("new", v))
base_topping_list[index].ToMap()[k] = v
}
}
} else {
not_found = true
global_idx = index
}
}
// is new value
if not_found {
base_topping_list[global_idx+1] = updated_topping_list
}
}
func (d *Data) SetValuesToToppingGroupList(base_topping_group_list []models.ToppingGroup, updated_topping_group_list models.ToppingGroup) {
not_found := false
global_idx := 0
for index, v := range base_topping_group_list {
// find matched id
if v.GroupID == updated_topping_group_list.GroupID {
// change only changed values
for k, v := range updated_topping_group_list.ToMap() {
if !reflect.DeepEqual(base_topping_group_list[index].ToMap()[k], v) {
d.taoLogger.Log.Debug("SetValuesToToppingGroup", zap.Any("key", k), zap.Any("old", base_topping_group_list[index].ToMap()[k]), zap.Any("new", v))
base_topping_group_list[index].ToMap()[k] = v
}
}
} else {
not_found = true
global_idx = index
}
}
// is new value
if not_found {
base_topping_group_list[global_idx+1] = updated_topping_group_list
}
}
2023-12-06 10:05:16 +07:00
func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialSetting {
result := make([]models.MaterialSetting, 0)
if countryID == "" {
2024-01-25 13:39:49 +07:00
// copy(result, d.currentRecipe[countryID].MaterialSetting)
2024-02-05 17:07:04 +07:00
return d.CurrentRecipe[countryID].MaterialSetting
2023-12-06 10:05:16 +07:00
}
2023-12-29 16:10:57 +07:00
if !strings.Contains(filename, "tmp") {
2024-01-17 17:38:23 +07:00
if filename == "" || filename == d.CurrentFile[countryID] {
2024-01-19 14:59:21 +07:00
// copy(result, d.currentRecipe[countryID].MaterialSetting)
// d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("result", result))
2024-02-05 17:07:04 +07:00
return d.CurrentRecipe[countryID].MaterialSetting
2023-12-29 16:10:57 +07:00
}
2023-12-06 10:05:16 +07:00
2023-12-29 16:10:57 +07:00
if recipe, ok := d.recipeMap[filename]; ok {
2024-01-18 16:59:06 +07:00
copy(result, recipe.Recipe[countryID].MaterialSetting)
2024-01-17 17:38:23 +07:00
d.CurrentFile[countryID] = filename
2024-01-19 14:59:21 +07:00
// d.CurrentCountryID[countryID] = countryID
2024-02-05 17:07:04 +07:00
return d.CurrentRecipe[countryID].MaterialSetting
2023-12-29 16:10:57 +07:00
}
2023-12-06 10:05:16 +07:00
}
2024-01-17 17:38:23 +07:00
if filename == "default" {
filename = d.CurrentFile[countryID]
}
2024-01-19 14:59:21 +07:00
// d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("filename", filename), zap.Any("countryID", countryID))
2024-01-17 17:38:23 +07:00
2024-01-19 14:59:21 +07:00
// d.CurrentFile[countryID] = filename
// d.CurrentCountryID[countryID] = countryID
2024-02-05 17:07:04 +07:00
recipe := d.GetRecipe(countryID, filename)
2023-12-06 10:05:16 +07:00
2024-02-05 17:07:04 +07:00
// if err != nil {
// d.taoLogger.Log.Error("GetMaterialSetting: Error when read recipe file, Return default recipe", zap.Error(err))
// copy(result, d.CurrentRecipe[countryID].MaterialSetting)
// return d.CurrentRecipe[countryID].MaterialSetting
// }
2023-12-06 10:05:16 +07:00
2024-01-19 14:59:21 +07:00
// d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("recipe", recipe.MaterialSetting))
2023-12-29 16:10:57 +07:00
2024-02-05 17:07:04 +07:00
d.CurrentRecipe[countryID] = recipe
2023-12-06 10:05:16 +07:00
// 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{
2024-02-05 17:07:04 +07:00
Recipe: d.CurrentRecipe,
2023-12-06 10:05:16 +07:00
TimeStamps: time.Now().Unix(),
}
2023-12-29 16:10:57 +07:00
// copy(result, recipe.MaterialSetting)
return recipe.MaterialSetting
2023-12-06 10:05:16 +07:00
}
2024-02-02 17:07:49 +07:00
func (d *Data) GetAllToppingGroups(countryID, filename string) []models.ToppingGroup {
if countryID == "" {
2024-02-05 17:07:04 +07:00
return d.CurrentRecipe[countryID].Topping.ToppingGroup
2024-02-02 17:07:49 +07:00
}
if !strings.Contains(filename, ".tmp") {
if filename == "" || filename == d.CurrentFile[countryID] {
2024-02-05 17:07:04 +07:00
return d.CurrentRecipe[countryID].Topping.ToppingGroup
2024-02-02 17:07:49 +07:00
}
if _, ok := d.recipeMap[countryID]; ok {
d.CurrentFile[countryID] = filename
2024-02-05 17:07:04 +07:00
return d.CurrentRecipe[countryID].Topping.ToppingGroup
2024-02-02 17:07:49 +07:00
}
}
if filename == "default" {
filename = d.CurrentFile[countryID]
}
2024-02-05 17:07:04 +07:00
recipe := d.GetRecipe(countryID, filename)
2024-02-02 17:07:49 +07:00
2024-02-05 17:07:04 +07:00
d.CurrentRecipe[countryID] = recipe
2024-02-02 17:07:49 +07:00
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{
2024-02-05 17:07:04 +07:00
Recipe: d.CurrentRecipe,
2024-02-02 17:07:49 +07:00
TimeStamps: time.Now().Unix(),
}
return recipe.Topping.ToppingGroup
}
func (d *Data) GetToppingsList(countryID, filename string) []models.ToppingList {
// do return default
if countryID == "" {
2024-02-05 17:07:04 +07:00
return d.CurrentRecipe[countryID].Topping.ToppingList
2024-02-02 17:07:49 +07:00
}
// handle temporary file
if !strings.Contains(filename, ".tmp") {
if filename == "" || filename == d.CurrentFile[countryID] {
2024-02-05 17:07:04 +07:00
return d.CurrentRecipe[countryID].Topping.ToppingList
2024-02-02 17:07:49 +07:00
}
if _, ok := d.recipeMap[countryID]; ok {
d.CurrentFile[countryID] = filename
2024-02-05 17:07:04 +07:00
return d.CurrentRecipe[countryID].Topping.ToppingList
2024-02-02 17:07:49 +07:00
}
}
if filename == "default" {
filename = d.CurrentFile[countryID]
}
2024-02-05 17:07:04 +07:00
recipe := d.GetRecipe(countryID, filename)
2024-02-02 17:07:49 +07:00
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{
2024-02-05 17:07:04 +07:00
Recipe: d.CurrentRecipe,
2024-02-02 17:07:49 +07:00
TimeStamps: time.Now().Unix(),
}
return recipe.Topping.ToppingList
}
2023-12-06 10:05:16 +07:00
func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []models.MaterialCode {
var result []models.MaterialCode
2024-01-17 17:38:23 +07:00
if filename == "" || filename == d.CurrentFile[countryID] {
2024-02-05 17:07:04 +07:00
result = d.CurrentRecipe[countryID].MaterialCode
2023-12-06 10:05:16 +07:00
} else if recipe, ok := d.recipeMap[filename]; ok {
2024-01-17 17:38:23 +07:00
d.CurrentFile[countryID] = filename
2024-01-18 16:59:06 +07:00
return recipe.Recipe[countryID].MaterialCode
2023-12-06 10:05:16 +07:00
} else {
2024-01-17 17:38:23 +07:00
if filename == "default" {
filename = d.CurrentFile[countryID]
}
2024-01-19 14:59:21 +07:00
// d.CurrentFile[countryID] = filename
// d.CurrentCountryID[countryID] = countryID
2024-02-05 17:07:04 +07:00
recipe := d.GetRecipe(countryID, filename)
2023-12-06 10:05:16 +07:00
2024-02-05 17:07:04 +07:00
// if err != nil {
// d.taoLogger.Log.Error("GetMaterialCode: Error when read recipe file, Return default recipe", zap.Error(err))
// return d.CurrentRecipe[countryID].MaterialCode
// }
2023-12-06 10:05:16 +07:00
2024-02-05 17:07:04 +07:00
d.CurrentRecipe[countryID] = recipe
2023-12-06 10:05:16 +07:00
// 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{
2024-02-05 17:07:04 +07:00
Recipe: d.CurrentRecipe,
2023-12-06 10:05:16 +07:00
TimeStamps: time.Now().Unix(),
}
2024-02-05 17:07:04 +07:00
result = d.CurrentRecipe[countryID].MaterialCode
2023-12-06 10:05:16 +07:00
}
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
}
2023-12-29 16:10:57 +07:00
func (d *Data) GetToppings(countryID, filename string) models.Topping {
2024-01-17 17:38:23 +07:00
if filename == "" || filename == d.CurrentFile[countryID] {
2024-02-05 17:07:04 +07:00
return d.CurrentRecipe[countryID].Topping
2023-12-29 16:10:57 +07:00
} else if recipe, ok := d.recipeMap[filename]; ok {
2024-01-17 17:38:23 +07:00
d.CurrentFile[countryID] = filename
2024-01-18 16:59:06 +07:00
return recipe.Recipe[countryID].Topping
2023-12-29 16:10:57 +07:00
}
2024-01-17 17:38:23 +07:00
if filename == "default" {
filename = d.CurrentFile[countryID]
}
2024-01-19 14:59:21 +07:00
// d.CurrentFile[countryID] = filename
// d.CurrentCountryID[countryID] = countryID
2024-02-05 17:07:04 +07:00
recipe := d.GetRecipe(countryID, filename)
2023-12-29 16:10:57 +07:00
2024-02-05 17:07:04 +07:00
d.CurrentRecipe[countryID] = recipe
2023-12-29 16:10:57 +07:00
return recipe.Topping
}
func (d *Data) GetToppingsOfRecipe(countryID, filename string, productCode string) ([]models.ToppingSet, error) {
2024-01-17 17:38:23 +07:00
if filename == "default" {
filename = d.CurrentFile[countryID]
}
2023-12-29 16:10:57 +07:00
recipe, err := d.GetRecipe01ByProductCode(filename, countryID, productCode)
if err != nil {
2024-01-19 14:59:21 +07:00
d.taoLogger.Log.Error("GetToppingOfRecipe: Error when read recipe file, Return default recipe", zap.Error(err))
2023-12-29 16:10:57 +07:00
return []models.ToppingSet{}, err
}
return recipe.ToppingSet, nil
}
2024-01-15 11:48:25 +07:00
func (d *Data) GetSubmenusOfRecipe(countryID, filename, productCode string) ([]models.Recipe01, error) {
2024-01-17 17:38:23 +07:00
if filename == "default" {
filename = d.CurrentFile[countryID]
}
2024-01-15 11:48:25 +07:00
recipe, err := d.GetRecipe01ByProductCode(filename, countryID, productCode)
if err != nil {
2024-01-19 14:59:21 +07:00
d.taoLogger.Log.Error("GetSubmenusOfRecipe: Error when read recipe file, Return default recipe", zap.Error(err))
2024-01-15 11:48:25 +07:00
return []models.Recipe01{}, err
}
submenu := recipe.SubMenu
if submenu == nil {
return []models.Recipe01{}, fmt.Errorf("no submenu")
}
return submenu, nil
}
2023-12-06 10:05:16 +07:00
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)
}