package service import ( "database/sql" "errors" "xiaoniaokuaiyan.com/xiaoniao/entity" "xiaoniaokuaiyan.com/xiaoniao/dal" "xiaoniaokuaiyan.com/xiaoniao/util" ) type CartService struct { dal.ICart } func (srv *CartService) CartList(customId int) (interface{}, error) { return srv.ICart.Get(customId) } func (srv *CartService) CartAdd(customId int, productId int, quantity int, isAdd bool) (interface{}, error) { plist, err := dal.DefaultProductDal.Get([]int{productId}) if err != nil { return nil, err } if len(plist) == 0 { return nil, errors.New("1::wrong product id") } db := util.GetSqlDB() var cartNum int var strSql string if isAdd { strSql = "select quantity from t_cart where cid = ? and pid =?;" err = db.Get(&cartNum, strSql, customId, productId) if err != nil && err != sql.ErrNoRows { return nil, err } } if plist[0].StockSwitch == "ON" && plist[0].Stock < quantity+cartNum { return nil, errors.New("3::out of stock") } strSql = "SELECT sum(t2.quantity) as bought_num from t_order t1 left join t_order_product t2 on t1.id = t2.order_id left join t_product t3 on t2.product_id = t3.id where custom_id = ? and t3.id =? and t1.status not in(7,4,9) and t1.retype in ('100','110');" var bnum int db.Get(&bnum, strSql, customId, productId) if plist[0].PstockSwitch == "ON" && bnum+quantity+cartNum > plist[0].Pstock { return nil, errors.New("4::upper limit") } data, err := srv.ICart.Update(customId, productId, quantity, isAdd) if err != nil { return nil, errors.New("2::购物车已存在该产品") } return data, nil } func (srv *CartService) CartRemove(customId int, productIds []int) (interface{}, error) { return srv.ICart.Del(customId, productIds) } func (srv *CartService) CartClear(customId int) (interface{}, error) { return srv.ICart.Clear(customId) } func (srv *CartService) ZFB_CartList(customId int) (interface{}, 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 = ? and t2.is_zfb=1" plist := []*entity.ProductDB{} err := db.Select(&plist, strSql, customId) if err != nil { return nil, err } entity.ZFBWash(plist) return plist, err }