adjust mat selection
This commit is contained in:
parent
17030c72ce
commit
bf693aab2a
15 changed files with 548 additions and 186 deletions
|
|
@ -2,14 +2,15 @@ package routers
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
"recipe-manager/data"
|
||||
"recipe-manager/models"
|
||||
"recipe-manager/services/logger"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type MaterialRouter struct {
|
||||
|
|
@ -29,9 +30,72 @@ func (mr *MaterialRouter) Route(r chi.Router) {
|
|||
r.Get("/code", mr.getMaterialCode)
|
||||
|
||||
r.Get("/setting/{mat_id}", mr.getMaterialSettingByMatID)
|
||||
|
||||
r.Get("/full/{country}/{filename}", mr.GetFullMaterialDetail)
|
||||
})
|
||||
}
|
||||
|
||||
func (mr *MaterialRouter) GetFullMaterialDetail(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
country := chi.URLParam(r, "country")
|
||||
filename := chi.URLParam(r, "filename")
|
||||
|
||||
mr.taoLogger.Log.Debug("GetFullMaterialDetail", zap.Any("country", country), zap.Any("filename", filename))
|
||||
|
||||
// get material setting and code
|
||||
matSettings := mr.data.GetMaterialSetting(country, filename)
|
||||
matCodes := mr.data.GetRecipe(country, filename).MaterialCode
|
||||
|
||||
// combine
|
||||
materialDetails := []map[string]interface{}{}
|
||||
|
||||
for _, matSetting := range matSettings {
|
||||
|
||||
// if material name exist
|
||||
mat_name := ""
|
||||
if matSetting.MaterialName != "" {
|
||||
mat_name = matSetting.MaterialName
|
||||
// mr.taoLogger.Log.Debug("GetFullMaterialDetail", zap.Any("mat_name", mat_name))
|
||||
} else {
|
||||
|
||||
for _, matCode := range matCodes {
|
||||
if matCode.MaterialID == matSetting.ID {
|
||||
mat_name = matCode.PackageDescription
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
materialDetails = append(materialDetails, map[string]interface{}{
|
||||
"materialId": matSetting.ID,
|
||||
"name": mat_name,
|
||||
"type": "powder:" + strconv.FormatBool(matSetting.PowderChannel) + ",syrup:" + strconv.FormatBool(matSetting.SyrupChannel) + ",bean:" + strconv.FormatBool(matSetting.BeanChannel) + ",equipment:" + strconv.FormatBool(matSetting.IsEquipment),
|
||||
})
|
||||
}
|
||||
|
||||
// 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))
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// send result
|
||||
if err := json.NewEncoder(w).Encode(materialDetails); err != nil {
|
||||
mr.taoLogger.Log.Error("MaterialRouter.GetFullMaterialDetail", zap.Error(err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (mr *MaterialRouter) getMaterialCode(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
|
|
|
|||
|
|
@ -53,12 +53,12 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
|
||||
r.Get("/{product_code}/mat", rr.getRecipeMatByProductCode)
|
||||
|
||||
r.Get("/{country}/{filename}/toppings", rr.getToppings)
|
||||
|
||||
r.Get("/{country}/{filename}/json", rr.getRecipeJson)
|
||||
|
||||
r.Post("/edit/{country}/{filename}", rr.updateRecipe)
|
||||
|
||||
r.Post("/upgrade/{country}/{filename}", rr.ApplyTmpChanges)
|
||||
|
||||
r.Get("/saved/{country}/{filename_version_only}", rr.getSavedRecipes)
|
||||
|
||||
r.Get("/countries", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -336,7 +336,7 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
rr.taoLogger.Log.Debug("Changes: ", zap.Any("changes", changes))
|
||||
// TODO: find the matched pd
|
||||
targetMenu, err := rr.data.GetRecipe01ByProductCode(filename, countryID, changes.ProductCode)
|
||||
_, err = rr.data.GetRecipe01ByProductCode(filename, countryID, changes.ProductCode)
|
||||
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when get recipe by product code")))
|
||||
|
|
@ -344,28 +344,28 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
menuMap := targetMenu.ToMap()
|
||||
// menuMap := targetMenu.ToMap()
|
||||
changeMap := changes.ToMap()
|
||||
|
||||
// Find changes
|
||||
for key, val := range menuMap {
|
||||
// rr.taoLogger.Log.Debug("..DynamicCompare.key", zap.Any("key", key))
|
||||
testBool, err := helpers.DynamicCompare(val, changeMap[key])
|
||||
// for key, val := range menuMap {
|
||||
// // rr.taoLogger.Log.Debug("..DynamicCompare.key", zap.Any("key", key))
|
||||
// testBool, err := helpers.DynamicCompare(val, changeMap[key])
|
||||
|
||||
// if err != nil {
|
||||
// rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "DynamicCompare in request failed")))
|
||||
// http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
// // if err != nil {
|
||||
// // rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "DynamicCompare in request failed")))
|
||||
// // http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
// // return
|
||||
// // }
|
||||
|
||||
if !testBool && err == nil {
|
||||
menuMap[key] = changeMap[key]
|
||||
}
|
||||
}
|
||||
// if !testBool && err == nil {
|
||||
// menuMap[key] = changeMap[key]
|
||||
// }
|
||||
// }
|
||||
|
||||
// Apply changes
|
||||
tempRecipe := models.Recipe01{}
|
||||
tempRecipe = tempRecipe.FromMap(menuMap)
|
||||
tempRecipe = tempRecipe.FromMap(changeMap)
|
||||
rr.data.SetValuesToRecipe(targetRecipe.Recipe01, tempRecipe)
|
||||
rr.taoLogger.Log.Debug("ApplyChange", zap.Any("status", "passed"))
|
||||
|
||||
|
|
@ -390,6 +390,7 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
|
|||
Created_at: time.Now().Local().Format("2006-01-02 15:04:05"),
|
||||
Editor: editor,
|
||||
Change_file: temp_file_name,
|
||||
Relation: filename,
|
||||
}
|
||||
|
||||
err = data.Insert(&commit)
|
||||
|
|
@ -434,10 +435,10 @@ func (rr *RecipeRouter) getSavedRecipes(w http.ResponseWriter, r *http.Request)
|
|||
}
|
||||
|
||||
commits, err := data.GetCommitLogOfFilename(countryID, file_version)
|
||||
|
||||
// fmt.Println("commits", commits)
|
||||
rr.taoLogger.Log.Debug("RecipeRouter.getSavedRecipes", zap.Any("commits", commits))
|
||||
|
||||
if err != nil {
|
||||
if err != nil || len(commits) == 0 {
|
||||
|
||||
return
|
||||
}
|
||||
|
|
@ -448,92 +449,46 @@ func (rr *RecipeRouter) getSavedRecipes(w http.ResponseWriter, r *http.Request)
|
|||
json.NewEncoder(w).Encode(map[string]interface{}{"files": commits})
|
||||
}
|
||||
|
||||
func (rr *RecipeRouter) ApplyTmpChanges(w http.ResponseWriter, r *http.Request) {
|
||||
func (rr *RecipeRouter) getToppings(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
countryID := chi.URLParam(r, "country")
|
||||
filename := chi.URLParam(r, "filename")
|
||||
country := chi.URLParam(r, "country")
|
||||
|
||||
countryID, err := rr.data.GetCountryIDByName(country)
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(err))
|
||||
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
var legit_changes map[string]interface{}
|
||||
|
||||
target_recipe := rr.data.GetRecipe(countryID, filename)
|
||||
new_file_version := target_recipe.MachineSetting.ConfigNumber + 1
|
||||
|
||||
show_to_current := legit_changes["no_upgrade"].(bool)
|
||||
if show_to_current {
|
||||
new_file_version = target_recipe.MachineSetting.ConfigNumber
|
||||
}
|
||||
|
||||
err = json.NewDecoder(r.Body).Decode(&legit_changes)
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("ApplyErr", zap.Any("err", err))
|
||||
return
|
||||
}
|
||||
|
||||
user_selected_tmp := legit_changes["selected_files"].([]string)
|
||||
|
||||
for select_tmp := range user_selected_tmp {
|
||||
// open selected
|
||||
current_temp_file, err := os.ReadFile(user_selected_tmp[select_tmp])
|
||||
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("TmpFile", zap.Any("Open", "tried to open but failed => "+user_selected_tmp[select_tmp]))
|
||||
continue
|
||||
}
|
||||
// load to model
|
||||
|
||||
temp_recipe := models.Recipe01{}
|
||||
json.Unmarshal(current_temp_file, &temp_recipe)
|
||||
|
||||
// apply set value
|
||||
rr.data.SetValuesToRecipe(target_recipe.Recipe01, temp_recipe)
|
||||
rr.taoLogger.Log.Debug("ApplyTmpChanges", zap.Any("Update|Push", string(rune(temp_recipe.ID))+":"+temp_recipe.ProductCode))
|
||||
}
|
||||
|
||||
// export
|
||||
exported_filename := "coffeethai02_" + string(rune(new_file_version))
|
||||
|
||||
if countryID != "tha" {
|
||||
exported_filename += "_" + countryID
|
||||
}
|
||||
|
||||
exported_filename += ".json"
|
||||
|
||||
full_exported_path := "cofffeemachineConfig/" + countryID + "/" + exported_filename
|
||||
|
||||
outfile, _ := os.Create(full_exported_path)
|
||||
|
||||
target_recipe.MachineSetting.ConfigNumber = new_file_version
|
||||
encoder := json.NewEncoder(outfile)
|
||||
encoder.SetIndent("", " ")
|
||||
|
||||
err = encoder.Encode(target_recipe)
|
||||
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("UpgradeToFullRecipeFailed", zap.Any("File", err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"status": "OK",
|
||||
"version": new_file_version,
|
||||
"path": full_exported_path,
|
||||
})
|
||||
json.NewEncoder(w).Encode(rr.data.GetToppings(countryID, filename))
|
||||
|
||||
delete_files := legit_changes["del_after_upgrade"].(bool)
|
||||
}
|
||||
|
||||
if delete_files && !show_to_current {
|
||||
for seleted_file := range user_selected_tmp {
|
||||
os.Remove(user_selected_tmp[seleted_file])
|
||||
}
|
||||
}
|
||||
func (rr *RecipeRouter) getToppingsOfRecipe(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// countryID := chi.URLParam(r, "country")
|
||||
// filename := chi.URLParam(r, "filename")
|
||||
// productCode := chi.URLParam(r, "product_code")
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
// all toppings
|
||||
// allToppings := rr.data.GetToppings(countryID, filename)
|
||||
|
||||
// topps, err := rr.data.GetToppingsOfRecipe(countryID, filename, productCode)
|
||||
|
||||
// expandedToppings := map[string]interface{}{}
|
||||
|
||||
// for _, v := range allToppings.ToppingGroup {
|
||||
// for _, t := range topps {
|
||||
// if v.GroupID == t.ListGroupID[0] {
|
||||
// expandedToppings[v.GroupID] = v
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// if err != nil {
|
||||
// http.Error(w, err.Error(), http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
|
||||
// json.NewEncoder(w).Encode()
|
||||
}
|
||||
|
||||
func (rr *RecipeRouter) doMergeJson(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue