func (p *ProductEdit) OnSuccessFromProductForm() *exit.Exit { // clear values p.Product.ClearValues() // transfer stocks value to product.Stocks if p.Stocks != nil { // p.Product.Stocks = make([]*model.ProductStockItem, len(p.Product.Colors)*len(p.Product.Sizes)) p.Product.Stocks = model.NewStocks() i := 0 for _, color := range p.Product.Colors { for _, size := range p.Product.Sizes { p.Product.Stocks.Set(color, size, p.Stocks[i]) i = i + 1 } } } // transfer pictures value to pictures. if p.Pictures != nil { p.Product.Pictures = strings.Join(p.Pictures, ";") } // write to db if p.Id != nil { service.Product.UpdateProduct(p.Product) } else { service.Product.CreateProduct(p.Product) } if p.Referer == "view" { return exit.Redirect(fmt.Sprintf("/product/detail/%v", p.Product.Id)) } // TODO: return to original page. return exit.Redirect("/product/list") }
// ---------------------------------------------------------------------------------------------------- // fill stocks to inventory func GetAllStocksByIdSet(idset map[int64]bool) (map[int64]model.Stocks, error) { if nil == idset || len(idset) == 0 { return nil, nil } var conn *sql.DB var stmt *sql.Stmt var err error if conn, err = db.Connect(); err != nil { return nil, err } defer conn.Close() var _sql bytes.Buffer // sql buffer var params = []interface{}{} // params // var index = map[int]*model.Product{} _sql.WriteString("select id, product_id, color, size, stock from product_sku where ") _sql.WriteString("product_id in (") var idx = 0 for id, b := range idset { if b { if idx > 0 { _sql.WriteRune(',') } _sql.WriteRune('?') params = append(params, id) // index[id] = m idx += 1 } } _sql.WriteRune(')') if stmt, err = conn.Prepare(_sql.String()); err != nil { return nil, err } defer stmt.Close() // 3. execute rows, err := stmt.Query(params...) if err != nil { return nil, err } defer rows.Close() // execute var ( id int productId int color string size string // price float32 stock int ) returns := map[int64]model.Stocks{} // productId -> color -> size : stock for rows.Next() { err := rows.Scan(&id, &productId, &color, &size /*&price,*/, &stock) if err != nil { return nil, err } colors, ok := returns[int64(productId)] if !ok { colors = model.NewStocks() returns[int64(productId)] = colors } colors.Set(color, size, stock) } return returns, nil }