1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace app\common\model;
- use think\Db;
- class GCart extends Base
- {
- public function add($goodsId,$userId){
- $map[]=['user_id','=',$userId];
- $map[]=['goods_id','=',$goodsId];
- $goodsStock=Db::name('g_goods')->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;
- }
- }
|