// private func setProductCSValue(productId int, color string, size string, field string, stock int, price float64) { conn := db.Connectp() defer db.CloseConn(conn) _sql := fmt.Sprintf("insert into product_sku (product_id, color, size, %v) values (?,?,?,?) on duplicate key update %v = ?", field, field) stmt, err := conn.Prepare(_sql) defer db.CloseStmt(stmt) // the safe way to close. if db.Err(err) { panic(err.Error()) } if field == "stock" { _, err := stmt.Exec(productId, color, size, stock, stock) if err != nil { debug.Error(err) } } else if field == "price" { _, err := stmt.Exec(productId, color, size, price, stock) if err != nil { debug.Error(err) } } }
// get properties by product and properties' name func GetProductProperties(propertyId int, propertiesName string) (values []string) { conn, _ := db.Connect() defer db.CloseConn(conn) // should use db.CloseConn or conn.Close()? stmt, err := conn.Prepare("select value from product_property where " + "product_id=? and property_name=? order by id asc") defer db.CloseStmt(stmt) if db.Err(err) { panic(err.Error()) // should here be empty return? } rows, err := stmt.Query(propertyId, propertiesName) defer db.CloseRows(rows) if db.Err(err) { return nil } // big performance issue, maybe. for rows.Next() { var propertyValue string rows.Scan(&propertyValue) values = append(values, propertyValue) } return values }
func GetProductStocks(productId int64, color, size string) (int, error) { var err error conn := db.Connectp() defer db.CloseConn(conn) // 1. query var queryString = "select `stock` from `product_sku` where product_id = ? and color=? and size=? " // 2. prepare stmt, err := conn.Prepare(queryString) if err != nil { panic(err.Error()) } defer stmt.Close() // 3. query rows, err := stmt.Query(productId, color, size) if err != nil { panic(err.Error()) } defer rows.Close() // 4. process results. var stock int = 0 if rows.Next() { rows.Scan(&stock) } return stock, nil }
// Delete Product Property by product, property name and value func DeleteProductProperty(productId int, propertyName string, property string) { conn := db.Connectp() defer db.CloseConn(conn) stmt, err := conn.Prepare("delete from product_property where " + "product_id = ? and property_name = ? and value = ? limit 1") if db.Err(err) { panic(err.Error()) } defer stmt.Close() _, err = stmt.Exec(productId, propertyName, property) if db.Err(err) { panic(err.Error()) } }
func ClearProductStock(productId int) error { conn := db.Connectp() defer db.CloseConn(conn) stmt, err := conn.Prepare("delete from product_sku where product_id = ?") if db.Err(err) { return err } defer stmt.Close() _, err = stmt.Exec(productId) if db.Err(err) { return err } return nil }
// Set customer private price, panic if any error occurs. func AddProductProperty(productId int, propertyName string, property string) { conn := db.Connectp() defer db.CloseConn(conn) sql := "insert into product_property " + "(product_id, property_name, value) values(?,?,?)" stmt, err := conn.Prepare(sql) if db.Err(err) { panic(err.Error()) } defer stmt.Close() _, err = stmt.Exec(productId, propertyName, property) if db.Err(err) { panic(err.Error()) } }
// // Delete Product Property by product, property name and value // func DeleteOneProductProperty(productId int, propertyName string) { // fmt.Println("______________________________________________________________") // fmt.Println(productId) // fmt.Println(propertyName) if productId <= 0 { panic("Error when DeleteOneProductProperty: productId: " + string(productId)) } conn := db.Connectp() defer db.CloseConn(conn) stmt, err := conn.Prepare("delete from product_property where " + "product_id = ? and property_name = ?") if db.Err(err) { panic(err.Error()) } defer stmt.Close() _, err = stmt.Exec(productId, propertyName) if db.Err(err) { panic(err.Error()) } }
/* Set customer private price */ func SetCustomerPrice(personId int, productId int, price float64) error { if logdebug { log.Printf("[dal] Set customer price for %v on %v, $%v", personId, productId, price) } conn := db.Connectp() defer db.CloseConn(conn) // first get price xxx. TODO performance. stmt, err := conn.Prepare("insert into customer_special_price " + "(person_id, product_id, price, create_time, last_use_time) " + "values(?,?,?,?,?)") if db.Err(err) { return err } defer stmt.Close() _, err = stmt.Exec(personId, productId, price, time.Now(), nil) if err != nil { return err } return nil }
/*_______________________________________________________________________________ Fill order Lists. */ func filter(productId int) *map[string]int { var err error conn := db.Connectp() defer db.CloseConn(conn) // 1. query var queryString = "select color,size,stock from `product_sku` where product_id = ?" // 2. prepare stmt, err := conn.Prepare(queryString) if err != nil { panic(err.Error()) } defer stmt.Close() // 3. query rows, err := stmt.Query(productId) if err != nil { panic(err.Error()) } defer rows.Close() // 4. process results. // big performance issue, maybe. who knows. var ( color string size string stock int stocks = map[string]int{} ) for rows.Next() { rows.Scan(&color, &size, &stock) stocks[fmt.Sprintf("%v__%v", color, size)] = stock } return &stocks }