<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\Car;
use think\Db;
use think\Validate;
/**
 * 购物车接口**
 */
class Cars extends Api
{
    protected  $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $user_id = '';//token存贮user_id
    protected $normal = '';//商品正常状态,1:下架
    public function _initialize()
    {
        parent::_initialize();
        $this->normal = config('site.goods_status');
        $this->user_id = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (加入购物车)
     * @ApiSummary  (加入购物车)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/cars/addCar)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="商品id")
     * @ApiReturn({
                "code": 1,
                "msg": "加入成功",
                "time": "1553836646",
                "data": null
            })
     */
    public function addCar(){
        if($this->request->isGet()){
            $goods_id = $this->request->get('id');//商品id
            $rule = config('site.goods');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$goods_id])) {
                $this->error($validate->getError());
            }
            $data = ['uid'=>$this->user_id,'p_id'=>$goods_id];
            $car = new Car();
            $cars = Db::table('gc_car')
                ->field('id,num')
                ->where($data)
                ->find();
            if($cars){
                $data['num'] = $cars['num'] + 1;
                $res = $car->where(['id'=>$cars['id']])->update($data);
            }else{
                $res = $car::create($data);
            }
            if($res){
                $this->success('加入成功');
            }else{
                $this->error('加入失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (购物车列表)
     * @ApiSummary  (购物车列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/cars/carList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn({
            "code": 1,
            "msg": "成功",
            "time": "1553836697",
            "data": [
                {
                    "id": 1,
                    "p_id": 1,//商品id
                    "title": "我的垃圾商品1",//商品名称
                    "price": 500,//商品价格
                    "num": 3,/商品数量
                    "images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
                },
                {
                    "id": 2,
                    "p_id": 2,
                    "title": "废弃家电",
                    "price": 1200.08,
                    "num": 3,
                    "images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
                }
            ]
            })
     */
    public function carList(){
        if($this->request->isGet()){
            $data = Db::table('gc_car')
                ->alias('c')
                ->join('gc_product p','c.p_id = p.id','LEFT')
                ->join('gc_user u','p.uid = u.id','LEFT')
                ->where(['c.uid'=>$this->user_id,'p.status'=>$this->normal,'u.status'=>'normal'])
                ->field('c.id,c.p_id,p.title,p.price,c.num,p.images')
                ->order('c.id desc')
                ->select();
            $this->success('成功',$data);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (购物车列表)
     * @ApiSummary  (购物车列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/cars/clearCar)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="string", required=true, description="购物车id(多个以逗号隔开)")
     * @ApiReturn({
        "code": 1,
        "msg": "删除成功",
        "time": "1555504723",
        "data": 2
        })
     */
    public function clearCar(){
        if($this->request->isGet()){
            $c_id = $this->request->get('id');//购物车id,以逗号隔开字符(“1,2,3”)
            $rule = config('site.cars');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$c_id])) {
                $this->error($validate->getError());
            }
            $c_ids = explode(',',$c_id);
            $data = Db::table('gc_car')
                ->whereIn('id',$c_ids)
                ->where(['uid'=>$this->user_id])
                ->delete();
            if($data){
                $this->success('删除成功',$data);
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }
}