<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/11
 * Time: 11:41
 */

namespace app\index\controller;


use app\common\controller\Frontend;
use app\common\controller\HomeBase;
use app\index\model\Store;

class Car extends Frontend
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->view->assign('is_search', 0);
        $this->view->assign('is_active', 0);
        $this->view->assign('title', '购物车');
    }

    /**
     * 购物车页面
     * @return mixed
     */
    public function index(){
        $user_id = $this->auth->id;
        $carModel = new \app\index\model\Car();
        $car1 = $carModel->where(['user_id'=>$user_id])->group('store_id')->select();
        $store_ids = [];
        foreach($car1 as $key => $c1){
            $store_ids[] = $c1['store_id'];
        }
        $goodsModel = new \app\index\model\Goods();
        $storeModel = new Store();
        $store = $storeModel->selectData(['id'=>['in',$store_ids]]);
        $store = collection($store)->toArray();
        foreach($store as $key1 => $s){
            $car = $carModel->selectData(['store_id'=>$s['id'],'user_id'=>$user_id]);
            foreach($car as $key2 => $c){
                $goods = $goodsModel->findData(['g.id'=>$c['goods_id']]);
                $car[$key2]['goods'] = $goods;
            }
            $store[$key1]['car'] = collection($car)->toArray();
        }
        $this->assign('data',$store);
        $this->assign('title','购物车');
        return $this->fetch();
    }

    /**
     * 更新购物车
     */
    public function add(){
        $user_id = $this->auth->id;
        $goods_id = $this->request->param('goods_id',0,'intval');
        $number = $this->request->param('number',1,'intval');
        $type = $this->request->param('type',0,'intval');//1增,2减
        $province_id = $this->request->param('province_id',0,'intval');
        if(empty($goods_id) || empty($number) || empty($type) || empty($province_id)){
            $this->error("缺少必要参数");
        }
        $goodsModel = new \app\index\model\Goods();
        $data = $goodsModel->findData(['g.id'=>$goods_id]);
        if(empty($data)){
            $this->error('查询为空');
        }
        if($data['status'] != '1'){
            $this->error('该商品已下架');
        }
        $storeModel = new Store();
        $store = $storeModel->findData(['id'=>$data['store_id']]);
        //判断该区域是否为会员
        if($store['is_svip'] != '1'){
            if($store['is_vip'] != '1'){
                $this->error('该商品不可以进行该操作~');
            }else{
                if(!in_array($province_id,$store['province_ids'])){
                    $this->error('该商品不可以进行该操作~');
                }
            }
        }
        $carModel = new \app\index\model\Car();
        $arr['user_id'] = $user_id;
        $arr['goods_id'] = $goods_id;
        $arr['store_id'] = $data['store_id'];
        $arr['number'] = $number;
        $car = $carModel->findData(['user_id'=>$user_id,'goods_id'=>$goods_id]);
        if(empty($car)){
            //新增
            //判断库存是否充足
            if($data['inventory'] < 1){
                $this->error("商品 【$data[goodsname]】 库存不足~");
            }
            $arr['createtime'] = time();
            $result = $carModel->insertData($arr);
        }else{
            //更新
            $arr['updatetime'] = time();
            if($type == 1){
                //增加数量
                //判断库存是否充足
                if($data['inventory'] < 1){
                    $this->error("商品 【$data[goodsname]】 库存不足~");
                }
                $result = $carModel->where(['id'=>$car['id']])->setInc('number',$number);
            }else{
                //减少数量
                if($car['number'] <= 1){
                    $this->error('不能再减了~');
                }
                $result = $carModel->where(['id'=>$car['id']])->setDec('number',$number);
            }
        }
        if(empty($result)){
            $this->error('sql执行失败');
        }
        $car = $carModel->findData(['user_id'=>$user_id,'goods_id'=>$goods_id]);
        $this->success('SUCCESS','',['number'=>$car['number']]);
    }

    /**
     * 删除购物车
     */
    public function del(){
        $car_ids = $this->request->param('car_ids');
        $car_ids = explode(',',$car_ids);
        if(empty($car_ids)){
            $this->error('缺少必要参数');
        }
        $carModel = new \app\index\model\Car();
        $result = $carModel->where(['id'=>['in',$car_ids]])->delete();
        $this->success('SUCCESS');
    }
}