| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 | package dalimport (	"errors"	"fmt"	"xiaoniaokuaiyan.com/xiaoniao/entity"	"xiaoniaokuaiyan.com/xiaoniao/util")type Cart struct{}func (c *Cart) Get(customId int) ([]entity.ProductDB, error) {	db := util.GetSqlDB()	strSql := "select t1.quantity, t2.* from t_cart t1 left join t_product t2 on t1.pid = t2.id where cid = ?"	plist := []entity.ProductDB{}	err := db.Select(&plist, strSql, customId)	if err != nil {		return nil, err	}	return plist, nil}func (c *Cart) Update(customId, productId, quantity int, isAdd bool) (bool, error) {	var strSql string = "select quantity from t_cart where cid = ? and pid =?"	db := util.GetWriteSqlDB()	var (		quan int		err  error	)	db.Get(&quan, strSql, customId, productId)	if quan <= 0 {		if quantity < 1 {			return false, errors.New("wrong param quantity")		}		strSql = "insert into t_cart(cid, pid, quantity) values(?,?,?);"		_, err = db.Exec(strSql, customId, productId, quantity)	} else {		if isAdd {			quantity = quan + quantity		}		if quantity < 1 {			return false, errors.New("wrong param quantity")		}		strSql = "update t_cart set quantity = ? where cid = ? and pid = ?"		_, err = db.Exec(strSql, quantity, customId, productId)	}	if err != nil {		return false, err	}	return true, nil}func (c *Cart) Del(customId int, productIds []int) (bool, error) {	if len(productIds) == 0 {		return false, errors.New("wrong product id param")	}	var strSql = fmt.Sprintf("delete from t_cart where cid = %d and pid in(", customId)	for _, pid := range productIds {		strSql += fmt.Sprintf("%d,", pid)	}	strSql = strSql[0:len(strSql)-1] + ");"	db := util.GetWriteSqlDB()	_, err := db.Exec(strSql)	if err != nil {		return false, err	}	return true, nil}func (c *Cart) Clear(customId int) (bool, error) {	strSql := "delete from t_cart where cid = ?"	db := util.GetWriteSqlDB()	_, err := db.Exec(strSql, customId)	if err != nil {		return false, err	}	return true, nil}var DefaultCartDal = &Cart{}
 |