WxCart.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. class WxCart extends Base
  6. {
  7. public function add($goodsId,$userId){
  8. $map[]=['user_id','=',$userId];
  9. $map[]=['goods_id','=',$goodsId];
  10. $goodsStock=Db::name('wx_goods')
  11. ->where('id',$goodsId)->value('stock');
  12. // if($goodsStock < 1){
  13. // $this->error='库存不足';
  14. // return false;
  15. // }
  16. $find=Db::name('wx_goods_cart')->where($map)->find();
  17. if($find){
  18. $ret=Db::name('wx_goods_cart')->where($map)->inc('nums')->update(['update_time'=>date('Y-m-d H:i:s')]);
  19. }else{
  20. $data=[
  21. 'user_id'=>$userId,
  22. 'goods_id'=>$goodsId,
  23. 'nums'=>1,
  24. 'create_time'=>date('Y-m-d H:i:s'),
  25. ];
  26. $ret=Db::name('wx_goods_cart')->insert($data);
  27. }
  28. return $ret;
  29. }
  30. public function lists($userId,$orgId){
  31. $ret=Db::name('wx_goods_cart')
  32. ->alias('c')
  33. ->join('wx_goods g','c.goods_id=g.id')
  34. ->field('c.id,c.goods_id,c.nums,g.title,g.img,g.price,g.enable,g.label')
  35. ->where('c.user_id',$userId)
  36. ->where('g.del',0)
  37. ->where('g.org_id',$orgId)
  38. ->order('c.id','desc')
  39. ->select();
  40. $total = 0;
  41. $num = 0;
  42. foreach ($ret as $k=>$v){
  43. $ret[$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
  44. ->where('id',$v['label'])
  45. ->value('title'):"";
  46. $total+=$v['nums']*$v['price'];
  47. $num+=$v['nums'];
  48. }
  49. $data = [
  50. 'list'=>$ret,
  51. 'nums'=>$num,
  52. 'total'=>sprintf("%01.2f", $total),
  53. ];
  54. return $data;
  55. }
  56. public function updateNum($id,$nums){
  57. $nums = intval($nums);
  58. if($nums==0){
  59. $ret=Db::name('wx_goods_cart')->where('id',$id)
  60. ->delete();
  61. return $ret;
  62. }else{
  63. $ret=Db::name('wx_goods_cart')->where('id',$id)
  64. ->update(['nums'=>$nums,'update_time'=>date('Y-m-d H:i:s')]);
  65. return $ret;
  66. }
  67. $ret=Db::name('wx_goods_cart')->where('id',$id)
  68. ->update(['nums'=>$nums,'update_time'=>date('Y-m-d H:i:s')]);
  69. return $ret;
  70. }
  71. public function setCartNum($userId,$id,$type,$num){
  72. $map[]=['user_id','=',$userId];
  73. $map[]=['id','=',$id];
  74. $find=Db::name('wx_goods_cart')->where($map)->find();
  75. if(empty($find)){
  76. HelpHander::error('信息不存在');
  77. }
  78. if($type==0){
  79. $ret=Db::name('wx_goods_cart')
  80. ->where($map)->inc('nums',$num)
  81. ->update(['update_time'=>date('Y-m-d H:i:s')]);
  82. }else{
  83. if($find['nums']==1){
  84. $ret=Db::name('wx_goods_cart')->where($map)
  85. ->delete();
  86. }else{
  87. $ret=Db::name('wx_goods_cart')->where($map)
  88. ->dec('nums')
  89. ->update(['update_time'=>date('Y-m-d H:i:s')]);
  90. }
  91. }
  92. return $ret;
  93. }
  94. public function setGoodsNum($userId,$goodsId,$type,$num){
  95. $map[]=['user_id','=',$userId];
  96. $map[]=['goods_id','=',$goodsId];
  97. $find=Db::name('wx_goods_cart')->where($map)->find();
  98. if($type==0){
  99. if($find){
  100. $ret=Db::name('wx_goods_cart')
  101. ->where($map)->inc('nums',$num)
  102. ->update(['update_time'=>date('Y-m-d H:i:s')]);
  103. }else{
  104. $data=[
  105. 'user_id'=>$userId,
  106. 'goods_id'=>$goodsId,
  107. 'nums'=>1,
  108. 'create_time'=>date('Y-m-d H:i:s'),
  109. ];
  110. $ret=Db::name('wx_goods_cart')->insert($data);
  111. }
  112. }else{
  113. if(empty($find)){
  114. HelpHander::error('信息不存在');
  115. }
  116. if($find['nums']==1){
  117. $ret = Db::name('wx_goods_cart')->where($map)->delete();
  118. }else{
  119. $ret=Db::name('wx_goods_cart')->where($map)
  120. ->dec('nums')
  121. ->update(['update_time'=>date('Y-m-d H:i:s')]);
  122. }
  123. }
  124. return $ret;
  125. }
  126. public function goodsFav($userId,$goodsId){
  127. $map[]=['user_id','=',$userId];
  128. $map[]=['goods_id','=',$goodsId];
  129. $find=Db::name('wx_goods_fav')->where($map)->find();
  130. if($find){
  131. $ret = Db::name('wx_goods_fav')->where($map)->delete();
  132. $p = [
  133. 'type'=>0,
  134. 'ret'=>$ret,
  135. ];
  136. }else{
  137. $data=[
  138. 'user_id'=>$userId,
  139. 'goods_id'=>$goodsId,
  140. 'create_time'=>date('Y-m-d H:i:s'),
  141. ];
  142. $ret=Db::name('wx_goods_fav')->insert($data);
  143. $p = [
  144. 'type'=>1,
  145. 'ret'=>$ret,
  146. ];
  147. }
  148. return $p;
  149. }
  150. public function del($id){
  151. $ret=Db::name('wx_goods_cart')->where('id',$id)->delete();
  152. return $ret;
  153. }
  154. }