adjust mat selection
This commit is contained in:
parent
17030c72ce
commit
bf693aab2a
15 changed files with 548 additions and 186 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
|
@ -16,7 +17,8 @@ CREATE TABLE IF NOT EXISTS commit_log (
|
|||
msg VARCHAR(255),
|
||||
created_at DATETIME,
|
||||
editor VARCHAR(255),
|
||||
change_file VARCHAR(255)
|
||||
change_file VARCHAR(255),
|
||||
relation VARCHAR(255)
|
||||
);
|
||||
`
|
||||
|
||||
|
|
@ -26,6 +28,7 @@ type CommitLog struct {
|
|||
Created_at string `db:"created_at"`
|
||||
Editor string `db:"editor"`
|
||||
Change_file string `db:"change_file"`
|
||||
Relation string `db:"relation"`
|
||||
}
|
||||
|
||||
func HashCommit(n int) (string, error) {
|
||||
|
|
@ -52,7 +55,7 @@ func Insert(c *CommitLog) error {
|
|||
// init table in db
|
||||
commit_db.MustExec(schema)
|
||||
|
||||
_, err = commit_db.NamedExec("INSERT INTO commit_log (id, msg, created_at, editor, change_file) VALUES (:id, :msg, :created_at, :editor, :change_file)", c)
|
||||
_, err = commit_db.NamedExec("INSERT INTO commit_log (id, msg, created_at, editor, change_file, relation) VALUES (:id, :msg, :created_at, :editor, :change_file, :relation)", c)
|
||||
if err != nil {
|
||||
|
||||
return err
|
||||
|
|
@ -74,8 +77,10 @@ func GetCommitLogOfFilename(countryId string, filename string) ([]CommitLog, err
|
|||
}
|
||||
|
||||
commitDB, err := sqlx.Connect("sqlite3", "./data/database.db")
|
||||
// fmt.Println("GetCommitLogOfFilename", err)
|
||||
if err != nil {
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var commits []CommitLog
|
||||
|
|
@ -84,6 +89,12 @@ func GetCommitLogOfFilename(countryId string, filename string) ([]CommitLog, err
|
|||
|
||||
err = commitDB.Select(&commits, "SELECT * FROM commit_log WHERE change_file LIKE ?", "%"+filename+"%")
|
||||
|
||||
// fmt.Println("commits", err)
|
||||
if err != nil {
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var commitsByCountryID []CommitLog
|
||||
|
||||
for _, v := range commits {
|
||||
|
|
@ -101,6 +112,12 @@ func GetCommitLogOfFilename(countryId string, filename string) ([]CommitLog, err
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// fmt.Println("commitsByCountryID", len(commitsByCountryID) == 0)
|
||||
if len(commitsByCountryID) == 0 {
|
||||
|
||||
return nil, fmt.Errorf("no commit found for %s", filename)
|
||||
}
|
||||
|
||||
return commitsByCountryID, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"recipe-manager/helpers"
|
||||
"recipe-manager/models"
|
||||
"recipe-manager/services/logger"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"reflect"
|
||||
|
|
@ -28,9 +29,8 @@ type Data struct {
|
|||
taoLogger *logger.TaoLogger
|
||||
}
|
||||
|
||||
func NewData(taoLogger *logger.TaoLogger) *Data {
|
||||
|
||||
countries := []helpers.CountryName{{
|
||||
var (
|
||||
countries = []helpers.CountryName{{
|
||||
CountryID: "tha",
|
||||
CountryName: "Thailand",
|
||||
}, {
|
||||
|
|
@ -41,6 +41,9 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
|
|||
CountryName: "Australia",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func NewData(taoLogger *logger.TaoLogger) *Data {
|
||||
|
||||
allRecipeFiles := helpers.ScanRecipeFiles(countries)
|
||||
|
||||
|
|
@ -129,22 +132,35 @@ func (d *Data) GetCurrentRecipe() *models.Recipe {
|
|||
|
||||
func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) (models.Recipe01, error) {
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
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 {
|
||||
for _, v := range recipe.Recipe.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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
|
||||
for _, v := range countries {
|
||||
if v.CountryName == countryID {
|
||||
d.CurrentCountryID = v.CountryID
|
||||
countryID = v.CountryID
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -179,6 +195,7 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
|
|||
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", v))
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
|
|
@ -186,7 +203,6 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
|
|||
return models.Recipe01{}, fmt.Errorf("product code: %s not found", productCode)
|
||||
}
|
||||
|
||||
// FIXME: saved in log but not actual file
|
||||
func (d *Data) SetValuesToRecipe(base_recipe []models.Recipe01, recipe models.Recipe01) {
|
||||
not_found := false
|
||||
global_idx := 0
|
||||
|
|
@ -203,7 +219,7 @@ func (d *Data) SetValuesToRecipe(base_recipe []models.Recipe01, recipe models.Re
|
|||
|
||||
for k, v := range recipe01_Map {
|
||||
if !reflect.DeepEqual(base_recipe01_Map[k], v) {
|
||||
d.taoLogger.Log.Debug("SetValuesToRecipe", zap.Any("key", k), zap.Any("value", v), zap.Any("old", base_recipe01_Map[k]), zap.Any("new", recipe01_Map[k]))
|
||||
d.taoLogger.Log.Debug("SetValuesToRecipe", zap.Any("key", k))
|
||||
base_recipe01_Map[k] = v
|
||||
}
|
||||
}
|
||||
|
|
@ -231,16 +247,19 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
|
|||
return result
|
||||
}
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
copy(result, d.currentRecipe.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 recipe, ok := d.recipeMap[filename]; ok {
|
||||
copy(result, recipe.Recipe.MaterialSetting)
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
return result
|
||||
if recipe, ok := d.recipeMap[filename]; ok {
|
||||
copy(result, recipe.Recipe.MaterialSetting)
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
return d.currentRecipe.MaterialSetting
|
||||
}
|
||||
}
|
||||
|
||||
d.CurrentFile = filename
|
||||
|
|
@ -250,9 +269,11 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
|
|||
if err != nil {
|
||||
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
return result
|
||||
return d.currentRecipe.MaterialSetting
|
||||
}
|
||||
|
||||
d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("recipe", recipe.MaterialSetting))
|
||||
|
||||
d.currentRecipe = recipe
|
||||
|
||||
// save to map
|
||||
|
|
@ -274,8 +295,8 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
|
|||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
return result
|
||||
// copy(result, recipe.MaterialSetting)
|
||||
return recipe.MaterialSetting
|
||||
}
|
||||
|
||||
func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []models.MaterialCode {
|
||||
|
|
@ -341,6 +362,39 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
|
|||
return resultFilter
|
||||
}
|
||||
|
||||
func (d *Data) GetToppings(countryID, filename string) models.Topping {
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
return d.currentRecipe.Topping
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile = filename
|
||||
return recipe.Recipe.Topping
|
||||
}
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = 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.currentRecipe = recipe
|
||||
|
||||
return recipe.Topping
|
||||
}
|
||||
|
||||
func (d *Data) GetToppingsOfRecipe(countryID, filename string, productCode string) ([]models.ToppingSet, error) {
|
||||
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))
|
||||
return []models.ToppingSet{}, err
|
||||
}
|
||||
|
||||
return recipe.ToppingSet, nil
|
||||
}
|
||||
|
||||
func (d *Data) GetCountryNameByID(countryID string) (string, error) {
|
||||
for _, country := range d.Countries {
|
||||
if country.CountryID == countryID {
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue