fix default file reader bug
This commit is contained in:
parent
21109e4bf9
commit
db131d10c0
15 changed files with 636 additions and 254 deletions
|
|
@ -23,13 +23,20 @@ type RecipeWithTimeStamps struct {
|
|||
}
|
||||
|
||||
type Data struct {
|
||||
CurrentFile string
|
||||
CurrentCountryID string
|
||||
AllRecipeFiles map[string][]helpers.RecipePath
|
||||
currentRecipe *models.Recipe
|
||||
recipeMap map[string]RecipeWithTimeStamps
|
||||
Countries []helpers.CountryName
|
||||
taoLogger *logger.TaoLogger
|
||||
CurrentFile map[string]string
|
||||
CurrentCountryID map[string]string
|
||||
DefaultCountryMap []DefaultByCountry
|
||||
AllRecipeFiles map[string][]helpers.RecipePath
|
||||
currentRecipe *models.Recipe
|
||||
recipeMap map[string]RecipeWithTimeStamps
|
||||
Countries []helpers.CountryName
|
||||
taoLogger *logger.TaoLogger
|
||||
}
|
||||
|
||||
type DefaultByCountry struct {
|
||||
CountryShortName string
|
||||
CountryLongName string
|
||||
DefaultFileVersion int
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -53,46 +60,95 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
|
|||
defaultFile := "coffeethai02_600.json"
|
||||
defaultCountry := "tha"
|
||||
|
||||
// TODO: read 'version' file
|
||||
versionPath := path.Join("cofffeemachineConfig", defaultCountry, "version")
|
||||
taoLogger.Log.Debug("version", zap.Any("version path", versionPath))
|
||||
// TODO: read 'version' file by country
|
||||
|
||||
// versionFile, err := os.Open(versionPath)
|
||||
content, err := os.ReadFile(versionPath)
|
||||
// versionPath := path.Join("cofffeemachineConfig", defaultCountry, "version")
|
||||
// taoLogger.Log.Debug("version", zap.Any("version path", versionPath))
|
||||
|
||||
if err != nil {
|
||||
taoLogger.Log.Debug("Error when open version file", zap.Error(err))
|
||||
}
|
||||
// // versionFile, err := os.Open(versionPath)
|
||||
// content, err := os.ReadFile(versionPath)
|
||||
|
||||
initVersion := string(content)
|
||||
// if err != nil {
|
||||
// taoLogger.Log.Debug("Error when open version file", zap.Error(err))
|
||||
// }
|
||||
|
||||
// read latest version
|
||||
// set latest to default version
|
||||
latest_version, err := strconv.Atoi(initVersion)
|
||||
// initVersion := string(content)
|
||||
|
||||
if err != nil {
|
||||
latest_version = 600
|
||||
}
|
||||
// // read latest version
|
||||
// // set latest to default version
|
||||
// latest_version, err := strconv.Atoi(initVersion)
|
||||
|
||||
for _, v := range allRecipeFiles[defaultCountry] {
|
||||
// if err != nil {
|
||||
// latest_version = 600
|
||||
// }
|
||||
|
||||
// extract filename as version
|
||||
current_version_iter, err := strconv.Atoi(strings.Split(strings.Split(v.Name, "_")[1], ".")[0])
|
||||
defaultForEachCountry := []DefaultByCountry{}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
continue
|
||||
taoLogger.Log.Debug("Error when open version file", zap.Error(err))
|
||||
}
|
||||
|
||||
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
|
||||
initVersion := string(content)
|
||||
|
||||
// read latest version
|
||||
latest_version, _ := strconv.Atoi(initVersion)
|
||||
defaultForEachCountry = append(defaultForEachCountry, DefaultByCountry{CountryShortName: elem.CountryID, CountryLongName: elem.CountryName, DefaultFileVersion: latest_version})
|
||||
}
|
||||
|
||||
currentFileMap := make(map[string]string)
|
||||
CurrentCountryIDMap := make(map[string]string)
|
||||
|
||||
// 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) + ","
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
taoLogger.Log.Debug("defaultFile", zap.Any("defaultFile", defaultFile), zap.Any("latest_version", latest_version))
|
||||
// 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
|
||||
// }
|
||||
// }
|
||||
|
||||
taoLogger.Log.Debug("defaultFile", zap.Any("defaultFile", defaultFile), zap.Any("latest_version", versionsString))
|
||||
|
||||
defaultRecipe, err := helpers.ReadRecipeFile(defaultCountry, defaultFile)
|
||||
|
||||
|
|
@ -101,8 +157,8 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
|
|||
}
|
||||
|
||||
return &Data{
|
||||
CurrentFile: defaultFile,
|
||||
CurrentCountryID: defaultCountry,
|
||||
CurrentFile: currentFileMap,
|
||||
CurrentCountryID: CurrentCountryIDMap,
|
||||
AllRecipeFiles: allRecipeFiles,
|
||||
currentRecipe: defaultRecipe,
|
||||
recipeMap: map[string]RecipeWithTimeStamps{
|
||||
|
|
@ -111,8 +167,9 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
|
|||
TimeStamps: time.Now().Unix(),
|
||||
},
|
||||
},
|
||||
Countries: countries,
|
||||
taoLogger: taoLogger,
|
||||
Countries: countries,
|
||||
taoLogger: taoLogger,
|
||||
DefaultCountryMap: defaultForEachCountry,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -122,20 +179,25 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
|
|||
return d.currentRecipe
|
||||
}
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
d.CurrentFile[countryID] = filename
|
||||
d.CurrentCountryID[countryID] = countryID
|
||||
return &recipe.Recipe
|
||||
}
|
||||
|
||||
// change current version and read new recipe
|
||||
d.CurrentFile = filename
|
||||
|
||||
if filename == "default" {
|
||||
filename = d.CurrentFile[countryID]
|
||||
}
|
||||
|
||||
d.CurrentFile[countryID] = filename
|
||||
d.taoLogger.Log.Debug("GetRecipe", zap.String("filename", filename), zap.String("countryID", countryID))
|
||||
d.CurrentCountryID = countryID
|
||||
d.CurrentCountryID[countryID] = countryID
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -178,7 +240,7 @@ func (d *Data) GetCurrentRecipe() *models.Recipe {
|
|||
func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) (models.Recipe01, error) {
|
||||
|
||||
if !strings.Contains(filename, "tmp") {
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
fmt.Println("GetRecipe01ByProductCode.ReadCurrent", filename, d.CurrentFile)
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
|
|
@ -195,12 +257,18 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
|
|||
}
|
||||
}
|
||||
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("filename", filename), zap.Any("countryID", countryID), zap.Any("productCode", productCode))
|
||||
|
||||
if filename == "default" {
|
||||
filename = d.CurrentFile[countryID]
|
||||
}
|
||||
|
||||
d.CurrentFile[countryID] = filename
|
||||
d.CurrentCountryID[countryID] = countryID
|
||||
|
||||
for _, v := range countries {
|
||||
if v.CountryName == countryID {
|
||||
d.CurrentCountryID = v.CountryID
|
||||
d.CurrentCountryID[countryID] = v.CountryID
|
||||
countryID = v.CountryID
|
||||
break
|
||||
}
|
||||
|
|
@ -295,7 +363,7 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
|
|||
}
|
||||
|
||||
if !strings.Contains(filename, "tmp") {
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("result", result))
|
||||
return d.currentRecipe.MaterialSetting
|
||||
|
|
@ -303,14 +371,20 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
|
|||
|
||||
if recipe, ok := d.recipeMap[filename]; ok {
|
||||
copy(result, recipe.Recipe.MaterialSetting)
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
d.CurrentFile[countryID] = filename
|
||||
d.CurrentCountryID[countryID] = countryID
|
||||
return d.currentRecipe.MaterialSetting
|
||||
}
|
||||
}
|
||||
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
if filename == "default" {
|
||||
filename = d.CurrentFile[countryID]
|
||||
}
|
||||
|
||||
d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("filename", filename), zap.Any("countryID", countryID))
|
||||
|
||||
d.CurrentFile[countryID] = filename
|
||||
d.CurrentCountryID[countryID] = countryID
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -349,14 +423,19 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
|
|||
func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []models.MaterialCode {
|
||||
var result []models.MaterialCode
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
result = d.currentRecipe.MaterialCode
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile = filename
|
||||
d.CurrentFile[countryID] = filename
|
||||
return recipe.Recipe.MaterialCode
|
||||
} else {
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
|
||||
if filename == "default" {
|
||||
filename = d.CurrentFile[countryID]
|
||||
}
|
||||
|
||||
d.CurrentFile[countryID] = filename
|
||||
d.CurrentCountryID[countryID] = countryID
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -411,14 +490,19 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
|
|||
|
||||
func (d *Data) GetToppings(countryID, filename string) models.Topping {
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
return d.currentRecipe.Topping
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile = filename
|
||||
d.CurrentFile[countryID] = filename
|
||||
return recipe.Recipe.Topping
|
||||
}
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
|
||||
if filename == "default" {
|
||||
filename = d.CurrentFile[countryID]
|
||||
}
|
||||
|
||||
d.CurrentFile[countryID] = filename
|
||||
d.CurrentCountryID[countryID] = countryID
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -432,6 +516,11 @@ func (d *Data) GetToppings(countryID, filename string) models.Topping {
|
|||
}
|
||||
|
||||
func (d *Data) GetToppingsOfRecipe(countryID, filename string, productCode string) ([]models.ToppingSet, error) {
|
||||
|
||||
if filename == "default" {
|
||||
filename = d.CurrentFile[countryID]
|
||||
}
|
||||
|
||||
recipe, err := d.GetRecipe01ByProductCode(filename, countryID, productCode)
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -443,6 +532,11 @@ func (d *Data) GetToppingsOfRecipe(countryID, filename string, productCode strin
|
|||
}
|
||||
|
||||
func (d *Data) GetSubmenusOfRecipe(countryID, filename, productCode string) ([]models.Recipe01, error) {
|
||||
|
||||
if filename == "default" {
|
||||
filename = d.CurrentFile[countryID]
|
||||
}
|
||||
|
||||
recipe, err := d.GetRecipe01ByProductCode(filename, countryID, productCode)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue