0
0

GCart.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class GCart extends Base
  5. {
  6. public function add($goodsId,$userId){
  7. $map[]=['user_id','=',$userId];
  8. $map[]=['goods_id','=',$goodsId];
  9. $goodsStock=Db::name('g_goods')->where('id',$goodsId)->value('stock');
  10. if($goodsStock < 1){
  11. $this->error='库存不足';
  12. return false;
  13. }
  14. $find=Db::name('g_cart')->where($map)->find();
  15. if($find){
  16. $ret=Db::name('g_cart')->where($map)->inc('nums')->update(['update_time'=>date('Y-m-d H:i:s')]);
  17. }else{
  18. $data=[
  19. 'user_id'=>$userId,
  20. 'goods_id'=>$goodsId,
  21. 'nums'=>1,
  22. 'create_time'=>date('Y-m-d H:i:s'),
  23. ];
  24. $ret=Db::name('g_cart')->insert($data);
  25. }
  26. return $ret;
  27. }
  28. public function lists($userId){
  29. $ret=Db::name('g_cart')
  30. ->alias('c')
  31. ->join('g_goods g','c.goods_id=g.id')
  32. ->field('c.id,c.nums,g.title,g.img,g.price,g.sale_price,g.type')
  33. ->where('c.user_id',$userId)
  34. ->where('g.del',0)
  35. ->where('g.enable',1)
  36. ->select();
  37. foreach ($ret as $k=>$v){
  38. if($v['type'] == 1){
  39. $ret[$k]['price']=$v['sale_price'];
  40. }
  41. unset($ret[$k]['sale_price']);
  42. }
  43. return $ret?$ret:[];
  44. }
  45. public function updateNum($id,$nums){
  46. $ret=Db::name('g_cart')->where('id',$id)->update(['nums'=>$nums]);
  47. return $ret;
  48. }
  49. public function del($id){
  50. $ret=Db::name('g_cart')->delete($id);
  51. return $ret;
  52. }
  53. }