where('id',$goodsId)->value('stock'); if($goodsStock < 1){ $this->error='库存不足'; return false; } $find=Db::name('g_cart')->where($map)->find(); if($find){ $ret=Db::name('g_cart')->where($map)->inc('nums')->update(['update_time'=>date('Y-m-d H:i:s')]); }else{ $data=[ 'user_id'=>$userId, 'goods_id'=>$goodsId, 'nums'=>1, 'create_time'=>date('Y-m-d H:i:s'), ]; $ret=Db::name('g_cart')->insert($data); } return $ret; } public function lists($userId){ $ret=Db::name('g_cart') ->alias('c') ->join('g_goods g','c.goods_id=g.id') ->field('c.id,c.nums,g.title,g.img,g.price,g.sale_price,g.type') ->where('c.user_id',$userId) ->where('g.del',0) ->where('g.enable',1) ->select(); foreach ($ret as $k=>$v){ if($v['type'] == 1){ $ret[$k]['price']=$v['sale_price']; } unset($ret[$k]['sale_price']); } return $ret?$ret:[]; } public function updateNum($id,$nums){ $ret=Db::name('g_cart')->where('id',$id)->update(['nums'=>$nums]); return $ret; } public function del($id){ $ret=Db::name('g_cart')->delete($id); return $ret; } }