123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- namespace app\api\controller;
- use app\common\model\StoreGoods;
- use app\common\model\StoreGoodsItem;
- use app\common\service\ExpressService;
- use think\Db;
- use app\common\model\ShoppingTrolley;
- use app\common\model\DeliveryAddress;
- use think\Exception;
- class Trolley extends Base
- {
- protected $no_login = ['delComment'];
- public function initialize()
- {
- parent::initialize();
- $path = explode('/',$this->request->path());
- if(!in_array(end($path),$this->no_login)) parent::checkLogin();
- }
-
- public function getTrolleyNum()
- {
- $where = [];
- $where['user_id'] = $this->user_id;
- if($goods_id = input('get.goods_id',0)) $where['goods_id'] = $goods_id;
- $total_num = ShoppingTrolley::where($where)->sum('num');
- $this->success('ok',['total_num'=>$total_num]);
- }
-
- public function getTrolleyList()
- {
- $join_field = 't.id,t.num,t.spec_id,t.goods_id,i.goods_spec,g.cover,i.goods_no,i.is_deleted,i.original_price,i.sell_price,i.weight,g.name goods_name,g.status goods_status,g.is_deleted deleted_status';
- $list = ShoppingTrolley::field($join_field)
- ->alias('t')
- ->where(['t.user_id'=>$this->user_id])
- ->leftJoin('StoreGoodsItem i','i.id = t.spec_id')
- ->leftJoin('StoreGoods g','t.goods_id = g.id')
- ->order('t.id desc')
- ->select()->toArray();
- $this->success('ok',['list'=>$list]);
- }
-
- public function addTrolley()
- {
- $goods_id = input('post.goods_id');
- $spec_id = input('post.spec_id');
- $num = input('post.num',1);
- if(!$goods_id || !$spec_id) $this->error('请选择商品');
- if($num <=0 ) $this->error('添加数量有误');
- Db::startTrans();
- try {
-
- $goods_info = StoreGoods::with(['itemList'=>function($query)use($spec_id){
- return $query->where('id',$spec_id)->where('is_deleted',0);
- }])->where('id',$goods_id)->where('is_deleted',0)->where('status',1)->find();
- if(!$goods_info) throw new Exception('商品已下架');
- $goods_info = $goods_info->toArray();
- if(empty($goods_info['item_list'])) throw new Exception('该规格已下架');
- if($goods_info['item_list'][0]['stock'] < $num) throw new Exception('库存不足');
- $trolley_id = ShoppingTrolley::checkTrolley($this->user_id,$goods_id,$spec_id);
-
- if($trolley_id) {
- ShoppingTrolley::where('id',$trolley_id)->setInc('num',$num);
- }else{
- $trolley_info = [
- 'user_id' => $this->user_id,
- 'goods_id' => $goods_id,
- 'spec_id' => $spec_id,
- 'goods_no' => $goods_info['item_list'][0]['goods_no'],
- 'num' => $num,
- ];
- ShoppingTrolley::create($trolley_info);
- }
-
- StoreGoods::where('id',$goods_id)->setDec('stock',$num);
- StoreGoodsItem::where('id',$spec_id)->setDec('stock',$num);
- Db::commit();
- }catch (\Exception $e){
- $this->is_commit = false;
- $this->ret_msg = $e->getMessage();
- Db::rollback();
- }
- $this->is_commit ? $this->success('添加成功'):$this->error($this->ret_msg);
- }
-
- public function changeTrolleyNum()
- {
- $trolley_id = input('post.trolley_id');
- $num = input('post.num',1);
- $type = input('post.type',1);
- if(!in_array($type,[1,2])) $this->error('变更可类型错误');
- Db::startTrans();
- try {
- $trolley_info = ShoppingTrolley::get($trolley_id);
- if(!$trolley_info) $this->exception('记录不存在!');
- $goods_info = StoreGoods::getGoodsSpec($trolley_info->goods_id,$trolley_info->spec_id);
- if($type == 1){
- if(!$goods_info) $this->exception('商品已下架');
- if(empty($goods_info['item_list'])) $this->exception('该规格已下架');
- if($goods_info['item_list'][0]['stock'] < $num)$this->exception('库存不足');
- ShoppingTrolley::where('id',$trolley_id)->setInc('num',$num);
- StoreGoods::where('id',$trolley_info->goods_id)->setDec('stock',$num);
- StoreGoodsItem::where('id',$trolley_info->spec_id)->setDec('stock',$num);
- }else{
- if($trolley_info->num < $num) $this->exception('扣减数量有误');
- if($trolley_info->num > $num){
- ShoppingTrolley::where('id',$trolley_id)->setDec('num',$num);
- }else{
- ShoppingTrolley::where('id',$trolley_id)->delete();
- }
- StoreGoods::where('id',$trolley_info->goods_id)->setInc('stock',$num);
- StoreGoodsItem::where('id',$trolley_info->spec_id)->setInc('stock',$num);
- }
- Db::commit();
- }catch (\Exception $e){
- $this->ret_msg = $e->getMessage();
- $this->is_commit = false;
- Db::rollback();
- }
- $this->is_commit ? $this->success('操作成功') : $this->error($this->ret_msg);
- }
-
- public function cancelTrolleyList()
- {
- $ids = input('post.ids');
- if(!$ids) $this->error('请选择要删除的数据');
- $trolley_list = ShoppingTrolley::where('id','in',$ids)->select();
- Db::startTrans();
- foreach ($trolley_list as $trolley_info) {
- StoreGoods::where('id',$trolley_info->goods_id)->setInc('stock',$trolley_info->num);
- StoreGoodsItem::where('id',$trolley_info->spec_id)->setInc('stock',$trolley_info->num);
- }
- ShoppingTrolley::where('id','in',$ids)->delete();
- Db::commit();
- $this->success('删除成功');
- }
-
- public function getTrolleyByGoodsId()
- {
- $goods_id = input('get.goods_id');
- $list = ShoppingTrolley::field('id,goods_id,spec_id,goods_no,num')
- ->where(['user_id'=>$this->user_id,'goods_id'=>$goods_id])
- ->order('id desc')
- ->select();
- $this->success('ok',['list'=>$list?$list->toArray() : null ]);
- }
-
- public function confirmTrolleyList()
- {
- $ids = input('get.ids');
- $trolley_goods = ShoppingTrolley::field('goods_id')
- ->where('user_id',$this->user_id)
- ->where('id','in',$ids)
- ->group('goods_id')
- ->select();
- if(!$trolley_goods) $this->error('请选择商品');
- $goods_ids = array_column($trolley_goods->toArray(),'goods_id');
- $freight = 0;
- $list = [];
- $total_money = 0;
- $goods_list = StoreGoods::where('id','in',$goods_ids)->select()->toArray();
- $address_info = DeliveryAddress::where(['user_id'=>$this->user_id,'is_mr'=>1])->find();
- foreach ($goods_list as $goods_info){
- $trolley_data = ['goods_id'=>$goods_info['id'],'goods_name'=>$goods_info['name'],'goods_status'=>$goods_info['status'],'cover'=>$goods_info['cover'],'deleted_status'=>$goods_info['is_deleted']];
- $trolley_data['item_list'] = ShoppingTrolley::field('t.*,i.original_price,i.sell_price,i.goods_spec')
- ->alias('t')
- ->where('t.id','in',$ids)
- ->where('t.goods_id',$goods_info['id'])
- ->leftJoin('StoreGoodsItem i','i.id = t.spec_id')
- ->select()->toArray();
- foreach ($trolley_data['item_list'] as $iv){
- $total_money = bcadd(bcmul($iv['num'],$iv['sell_price'],2),$total_money,2);
- }
- $list[] = $trolley_data;
- $fre_num = array_sum(array_column($trolley_data['item_list'],'num'));
- if($address_info) {
- $goods_freight = ExpressService::getGoodsExpressPrice($goods_info,$address_info->id,$fre_num);
- if($goods_freight['code'] !=200){
- $this->is_commit = false;
- $this->ret_msg = $goods_freight['msg'];
- }
- $freight += $goods_freight['freight'];
- }
- }
- $this->success('ok',[
- 'list'=>$list,
- 'total_money'=>$total_money,
- 'address_info'=>$address_info ? $address_info->toArray():null,
- 'freight' =>$freight,
- 'freight_msg' => $this->ret_msg,
- 'freight_code' => $this->is_commit ? 1 :0
- ]);
- }
- }
|