Merge branch 'main' of github.com:Poomipat-Ch/taobin_recipe_manager
This commit is contained in:
commit
45851422f7
20 changed files with 1216 additions and 593 deletions
|
|
@ -18,18 +18,25 @@ import (
|
|||
)
|
||||
|
||||
type RecipeWithTimeStamps struct {
|
||||
Recipe models.Recipe
|
||||
Recipe map[string]*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
|
||||
taoLogger *logger.TaoLogger
|
||||
CurrentFile map[string]string
|
||||
CurrentCountryID map[string]string
|
||||
DefaultCountryMap []DefaultByCountry
|
||||
AllRecipeFiles map[string][]helpers.RecipePath
|
||||
currentRecipe map[string]*models.Recipe
|
||||
recipeMap map[string]RecipeWithTimeStamps
|
||||
Countries []helpers.CountryName
|
||||
taoLogger *logger.TaoLogger
|
||||
}
|
||||
|
||||
type DefaultByCountry struct {
|
||||
CountryShortName string
|
||||
CountryLongName string
|
||||
DefaultFileVersion int
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -51,99 +58,165 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
|
|||
allRecipeFiles := helpers.ScanRecipeFiles(countries)
|
||||
|
||||
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)
|
||||
currentDefaultFileForEachCountry := make(map[string]*models.Recipe)
|
||||
|
||||
// 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) + ","
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
currentDefaultFileForEachCountry[v.CountryShortName] = defaultRecipe
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
taoLogger.Log.Debug("defaultFile", zap.Any("defaultFile", defaultFile), zap.Any("latest_version", latest_version))
|
||||
// for _, v := range allRecipeFiles[defaultCountry] {
|
||||
|
||||
defaultRecipe, err := helpers.ReadRecipeFile(defaultCountry, defaultFile)
|
||||
// // extract filename as version
|
||||
// current_version_iter, err := strconv.Atoi(strings.Split(strings.Split(v.Name, "_")[1], ".")[0])
|
||||
|
||||
if err != nil {
|
||||
log.Panic("Error when read default recipe file:", err)
|
||||
}
|
||||
// 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
|
||||
// }
|
||||
// }
|
||||
|
||||
// FIXME: default file bug. do assign each default recipe model to each country
|
||||
|
||||
// taoLogger.Log.Debug("defaultFile", zap.Any("defaultFile", defaultFile), zap.Any("latest_version", versionsString))
|
||||
|
||||
// defaultRecipe, err := helpers.ReadRecipeFile(defaultCountry, defaultFile)
|
||||
|
||||
// if err != nil {
|
||||
// log.Panic("Error when read default recipe file:", err)
|
||||
// }
|
||||
|
||||
return &Data{
|
||||
CurrentFile: defaultFile,
|
||||
CurrentCountryID: defaultCountry,
|
||||
CurrentFile: currentFileMap,
|
||||
CurrentCountryID: CurrentCountryIDMap,
|
||||
AllRecipeFiles: allRecipeFiles,
|
||||
currentRecipe: defaultRecipe,
|
||||
currentRecipe: currentDefaultFileForEachCountry,
|
||||
recipeMap: map[string]RecipeWithTimeStamps{
|
||||
defaultFile: {
|
||||
Recipe: *defaultRecipe,
|
||||
Recipe: currentDefaultFileForEachCountry,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
},
|
||||
},
|
||||
Countries: countries,
|
||||
taoLogger: taoLogger,
|
||||
Countries: countries,
|
||||
taoLogger: taoLogger,
|
||||
DefaultCountryMap: defaultForEachCountry,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
|
||||
|
||||
d.taoLogger.Log.Debug("invoke GetRecipe", zap.String("countryID", countryID), zap.String("filename", filename))
|
||||
|
||||
if countryID == "" {
|
||||
return d.currentRecipe
|
||||
return d.currentRecipe["tha"]
|
||||
}
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
return d.currentRecipe
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
return d.currentRecipe[countryID]
|
||||
}
|
||||
|
||||
if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
return &recipe.Recipe
|
||||
d.CurrentFile[countryID] = filename
|
||||
// d.CurrentCountryID[countryID] = countryID
|
||||
return recipe.Recipe[countryID]
|
||||
}
|
||||
|
||||
// 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 {
|
||||
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
return d.currentRecipe
|
||||
d.taoLogger.Log.Error("GetRecipe: Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
return d.currentRecipe[countryID]
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
d.currentRecipe[countryID] = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
|
|
@ -160,47 +233,73 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
|
|||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
Recipe: d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
return d.currentRecipe
|
||||
return d.currentRecipe[countryID]
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe01() []models.Recipe01 {
|
||||
return d.currentRecipe.Recipe01
|
||||
}
|
||||
// func (d *Data) GetRecipe01() []models.Recipe01 {
|
||||
// return d.currentRecipe.Recipe01
|
||||
// }
|
||||
|
||||
func (d *Data) GetCurrentRecipe() *models.Recipe {
|
||||
return d.currentRecipe
|
||||
}
|
||||
// func (d *Data) GetCurrentRecipe() *models.Recipe {
|
||||
// return d.currentRecipe
|
||||
// }
|
||||
|
||||
func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) (models.Recipe01, error) {
|
||||
|
||||
if !strings.Contains(filename, "tmp") {
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
fmt.Println("GetRecipe01ByProductCode.ReadCurrent", filename, d.CurrentFile)
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
fmt.Println("GetRecipe01ByProductCode.ReadMap", filename, d.CurrentFile)
|
||||
for _, v := range recipe.Recipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
// 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)
|
||||
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
if !strings.Contains(filename, "tmp") {
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
// , d.CurrentFile, countryID, "result by country id", len(d.currentRecipe[countryID].Recipe01)
|
||||
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)
|
||||
|
||||
for _, v := range d.currentRecipe[countryID].Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
fmt.Println("No result in current recipe", countryID)
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
fmt.Println("GetRecipe01ByProductCode.ReadMap", filename, d.CurrentFile, recipe.Recipe[countryID], "countryID=", countryID)
|
||||
for _, v := range recipe.Recipe[countryID].Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getSuccess", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getFail", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
@ -209,8 +308,8 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
|
|||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
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, nil
|
||||
}
|
||||
|
|
@ -219,7 +318,7 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
|
|||
|
||||
d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("version", recipe.MachineSetting.ConfigNumber))
|
||||
|
||||
d.currentRecipe = recipe
|
||||
d.currentRecipe[countryID] = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
|
|
@ -236,11 +335,11 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
|
|||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
Recipe: d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
for _, v := range d.currentRecipe[countryID].Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", v))
|
||||
return v, nil
|
||||
|
|
@ -290,38 +389,44 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
|
|||
result := make([]models.MaterialSetting, 0)
|
||||
|
||||
if countryID == "" {
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
copy(result, d.currentRecipe[countryID].MaterialSetting)
|
||||
return result
|
||||
}
|
||||
|
||||
if !strings.Contains(filename, "tmp") {
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("result", result))
|
||||
return d.currentRecipe.MaterialSetting
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
// copy(result, d.currentRecipe[countryID].MaterialSetting)
|
||||
// d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("result", result))
|
||||
return d.currentRecipe[countryID].MaterialSetting
|
||||
}
|
||||
|
||||
if recipe, ok := d.recipeMap[filename]; ok {
|
||||
copy(result, recipe.Recipe.MaterialSetting)
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
return d.currentRecipe.MaterialSetting
|
||||
copy(result, recipe.Recipe[countryID].MaterialSetting)
|
||||
d.CurrentFile[countryID] = filename
|
||||
// d.CurrentCountryID[countryID] = countryID
|
||||
return d.currentRecipe[countryID].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 {
|
||||
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
return d.currentRecipe.MaterialSetting
|
||||
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
|
||||
}
|
||||
|
||||
d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("recipe", recipe.MaterialSetting))
|
||||
// d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("recipe", recipe.MaterialSetting))
|
||||
|
||||
d.currentRecipe = recipe
|
||||
d.currentRecipe[countryID] = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
|
|
@ -338,7 +443,7 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
|
|||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
Recipe: d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
|
|
@ -349,22 +454,27 @@ 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 {
|
||||
result = d.currentRecipe.MaterialCode
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
result = d.currentRecipe[countryID].MaterialCode
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile = filename
|
||||
return recipe.Recipe.MaterialCode
|
||||
d.CurrentFile[countryID] = filename
|
||||
return recipe.Recipe[countryID].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 {
|
||||
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
return d.currentRecipe.MaterialCode
|
||||
d.taoLogger.Log.Error("GetMaterialCode: Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
return d.currentRecipe[countryID].MaterialCode
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
d.currentRecipe[countryID] = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
|
|
@ -381,11 +491,11 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
|
|||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
Recipe: d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
result = d.currentRecipe.MaterialCode
|
||||
result = d.currentRecipe[countryID].MaterialCode
|
||||
}
|
||||
|
||||
if len(ids) == 0 {
|
||||
|
|
@ -411,31 +521,41 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
|
|||
|
||||
func (d *Data) GetToppings(countryID, filename string) models.Topping {
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
return d.currentRecipe.Topping
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
return d.currentRecipe[countryID].Topping
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile = filename
|
||||
return recipe.Recipe.Topping
|
||||
d.CurrentFile[countryID] = filename
|
||||
return recipe.Recipe[countryID].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 {
|
||||
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
return d.currentRecipe.Topping
|
||||
d.taoLogger.Log.Error("GetToppings: Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
return d.currentRecipe[countryID].Topping
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
d.currentRecipe[countryID] = recipe
|
||||
|
||||
return recipe.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 {
|
||||
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
d.taoLogger.Log.Error("GetToppingOfRecipe: Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
return []models.ToppingSet{}, err
|
||||
}
|
||||
|
||||
|
|
@ -443,10 +563,15 @@ 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 {
|
||||
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
d.taoLogger.Log.Error("GetSubmenusOfRecipe: Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
return []models.Recipe01{}, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,23 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func NewSqliteDatabase() *sqlx.DB {
|
||||
|
||||
// ensure that database exists
|
||||
info, err := os.Stat("./data/database.db")
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Println("No database found. Check path: ", err)
|
||||
} else {
|
||||
fmt.Println("Database existed. ", info)
|
||||
}
|
||||
|
||||
db := sqlx.MustConnect("sqlite3", "./data/database.db")
|
||||
return db
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,17 +75,7 @@ func (mr *MaterialRouter) GetFullMaterialDetail(w http.ResponseWriter, r *http.R
|
|||
})
|
||||
}
|
||||
|
||||
// for _, matCode := range matCodes {
|
||||
// for index, matDetail := range materialDetails {
|
||||
// if matCode.MaterialID == matDetail["materialId"] {
|
||||
// materialDetails[index]["name"] = matCode.PackageDescription
|
||||
// } else if matDetail["materialId"].(uint64) > 8110 && matDetail["materialId"].(uint64) <= 8130 {
|
||||
// slotNum := matDetail["materialId"].(uint64) - 8110
|
||||
// // mr.taoLogger.Log.Debug("GetFullMaterialDetail", zap.Any("slotNum", matDetail["materialId"]), zap.Any("slotNum", slotNum))
|
||||
// materialDetails[index]["name"] = "Topping" + strconv.Itoa(int(slotNum))
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
mr.taoLogger.Log.Debug("GetFullMaterialDetail", zap.Any("materialDetails", materialDetails[0]))
|
||||
|
||||
// send result
|
||||
if err := json.NewEncoder(w).Encode(materialDetails); err != nil {
|
||||
|
|
@ -141,7 +131,7 @@ func (mr *MaterialRouter) getMaterialSettingByMatID(w http.ResponseWriter, r *ht
|
|||
countryID, err := mr.data.GetCountryIDByName(country)
|
||||
|
||||
if err != nil {
|
||||
mr.taoLogger.Log.Error("MaterialRouter.GetMaterialSettingByMatID", zap.Error(err))
|
||||
// mr.taoLogger.Log.Error("MaterialRouter.GetMaterialSettingByMatID", zap.Error(err))
|
||||
http.Error(w, "Country not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
|
@ -152,7 +142,7 @@ func (mr *MaterialRouter) getMaterialSettingByMatID(w http.ResponseWriter, r *ht
|
|||
|
||||
matIDuint, err := strconv.ParseUint(matID, 10, 64)
|
||||
if err != nil {
|
||||
mr.taoLogger.Log.Error("MaterialRouter.GetMaterialSettingByMatID", zap.Error(err))
|
||||
// mr.taoLogger.Log.Error("MaterialRouter.GetMaterialSettingByMatID", zap.Error(err))
|
||||
http.Error(w, "Invalid material id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
|
@ -167,7 +157,7 @@ func (mr *MaterialRouter) getMaterialSettingByMatID(w http.ResponseWriter, r *ht
|
|||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(matSetting); err != nil {
|
||||
mr.taoLogger.Log.Error("MaterialRouter.GetMaterialSettingByMatID", zap.Error(err))
|
||||
// mr.taoLogger.Log.Error("MaterialRouter.GetMaterialSettingByMatID", zap.Error(err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -173,6 +173,8 @@ func (rr *RecipeRouter) dashBoard(w http.ResponseWriter, r *http.Request) {
|
|||
country := r.URL.Query().Get("country")
|
||||
filename := r.URL.Query().Get("filename")
|
||||
|
||||
rr.taoLogger.Log.Debug("RecipeRouter.Dashboard", zap.Any("country", country), zap.Any("filename", filename))
|
||||
|
||||
result, err := rr.recipeService.GetRecipeDashboard(&contracts.RecipeDashboardRequest{
|
||||
Country: country,
|
||||
Filename: filename,
|
||||
|
|
@ -205,6 +207,8 @@ func (rr *RecipeRouter) overview(w http.ResponseWriter, r *http.Request) {
|
|||
filename := r.URL.Query().Get("filename")
|
||||
materialIds := r.URL.Query().Get("materialIds")
|
||||
|
||||
rr.taoLogger.Log.Debug("RecipeRouter.Overview", zap.Any("take", take), zap.Any("offset", offset), zap.Any("country", country), zap.Any("filename", filename), zap.Any("materialIds", materialIds))
|
||||
|
||||
var materialIdsUint []int
|
||||
for _, v := range strings.Split(materialIds, ",") {
|
||||
materialIdUint, err := strconv.ParseUint(v, 10, 64)
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ func (rs *recipeService) GetRecipeDetailMat(request *contracts.RecipeDetailReque
|
|||
return contracts.RecipeDetailMatListResponse{}, fmt.Errorf("country name: %s not found", request.Country)
|
||||
}
|
||||
|
||||
// rs.taoLogger.Log.Debug("GetRecipeDetailMat", zap.Any("request", request))
|
||||
rs.taoLogger.Log.Debug("GetRecipeDetailMat", zap.Any("request", request))
|
||||
|
||||
recipe, err := rs.db.GetRecipe01ByProductCode(request.Filename, request.Country, request.ProductCode)
|
||||
|
||||
|
|
@ -186,6 +186,16 @@ func (rs *recipeService) GetRecipeDashboard(request *contracts.RecipeDashboardRe
|
|||
|
||||
recipe := rs.db.GetRecipe(countryID, request.Filename)
|
||||
|
||||
// recheck if filename is `default`
|
||||
|
||||
if request.Filename == "default" {
|
||||
for _, v := range rs.db.DefaultCountryMap {
|
||||
if v.CountryShortName == request.Country || v.CountryLongName == request.Country {
|
||||
request.Filename = rs.db.CurrentFile[v.CountryShortName]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result := contracts.RecipeDashboardResponse{
|
||||
ConfigNumber: recipe.MachineSetting.ConfigNumber,
|
||||
LastUpdated: recipe.Timestamp,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue