123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- class WxCart extends Base
- {
- public function add($goodsId,$userId){
- $map[]=['user_id','=',$userId];
- $map[]=['goods_id','=',$goodsId];
- $goodsStock=Db::name('wx_goods')
- ->where('id',$goodsId)->value('stock');
- // if($goodsStock < 1){
- // $this->error='库存不足';
- // return false;
- // }
- $find=Db::name('wx_goods_cart')->where($map)->find();
- if($find){
- $ret=Db::name('wx_goods_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('wx_goods_cart')->insert($data);
- }
- return $ret;
- }
- public function lists($userId,$orgId){
- $ret=Db::name('wx_goods_cart')
- ->alias('c')
- ->join('wx_goods g','c.goods_id=g.id')
- ->field('c.id,c.goods_id,c.nums,g.title,g.img,g.price,g.enable,g.label')
- ->where('c.user_id',$userId)
- ->where('g.del',0)
- ->where('g.org_id',$orgId)
- ->order('c.id','desc')
- ->select();
- $total = 0;
- $num = 0;
- foreach ($ret as $k=>$v){
- $ret[$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
- ->where('id',$v['label'])
- ->value('title'):"";
- $total+=$v['nums']*$v['price'];
- $num+=$v['nums'];
- }
- $data = [
- 'list'=>$ret,
- 'nums'=>$num,
- 'total'=>sprintf("%01.2f", $total),
- ];
- return $data;
- }
- public function updateNum($id,$nums){
- $nums = intval($nums);
- if($nums==0){
- $ret=Db::name('wx_goods_cart')->where('id',$id)
- ->delete();
- return $ret;
- }else{
- $ret=Db::name('wx_goods_cart')->where('id',$id)
- ->update(['nums'=>$nums,'update_time'=>date('Y-m-d H:i:s')]);
- return $ret;
- }
- $ret=Db::name('wx_goods_cart')->where('id',$id)
- ->update(['nums'=>$nums,'update_time'=>date('Y-m-d H:i:s')]);
- return $ret;
- }
- public function setCartNum($userId,$id,$type,$num){
- $map[]=['user_id','=',$userId];
- $map[]=['id','=',$id];
- $find=Db::name('wx_goods_cart')->where($map)->find();
- if(empty($find)){
- HelpHander::error('信息不存在');
- }
- if($type==0){
- $ret=Db::name('wx_goods_cart')
- ->where($map)->inc('nums',$num)
- ->update(['update_time'=>date('Y-m-d H:i:s')]);
- }else{
- if($find['nums']==1){
- $ret=Db::name('wx_goods_cart')->where($map)
- ->delete();
- }else{
- $ret=Db::name('wx_goods_cart')->where($map)
- ->dec('nums')
- ->update(['update_time'=>date('Y-m-d H:i:s')]);
- }
- }
- return $ret;
- }
- public function setGoodsNum($userId,$goodsId,$type,$num){
- $map[]=['user_id','=',$userId];
- $map[]=['goods_id','=',$goodsId];
- $find=Db::name('wx_goods_cart')->where($map)->find();
- if($type==0){
- if($find){
- $ret=Db::name('wx_goods_cart')
- ->where($map)->inc('nums',$num)
- ->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('wx_goods_cart')->insert($data);
- }
- }else{
- if(empty($find)){
- HelpHander::error('信息不存在');
- }
- if($find['nums']==1){
- $ret = Db::name('wx_goods_cart')->where($map)->delete();
- }else{
- $ret=Db::name('wx_goods_cart')->where($map)
- ->dec('nums')
- ->update(['update_time'=>date('Y-m-d H:i:s')]);
- }
- }
- return $ret;
- }
- public function goodsFav($userId,$goodsId){
- $map[]=['user_id','=',$userId];
- $map[]=['goods_id','=',$goodsId];
- $find=Db::name('wx_goods_fav')->where($map)->find();
- if($find){
- $ret = Db::name('wx_goods_fav')->where($map)->delete();
- $p = [
- 'type'=>0,
- 'ret'=>$ret,
- ];
- }else{
- $data=[
- 'user_id'=>$userId,
- 'goods_id'=>$goodsId,
- 'create_time'=>date('Y-m-d H:i:s'),
- ];
- $ret=Db::name('wx_goods_fav')->insert($data);
- $p = [
- 'type'=>1,
- 'ret'=>$ret,
- ];
- }
- return $p;
- }
- public function del($id){
- $ret=Db::name('wx_goods_cart')->where('id',$id)->delete();
- return $ret;
- }
- }
|