<?php

namespace app\api\controller;

use addons\shopro\exception\Exception;

/**
 * 商品接口
 */
class Goods extends Base
{

    protected $noNeedLogin = ['index', 'detail', 'lists', 'activity', 'seckillList', 'grouponList', 'store'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiInternal
     * 测试
     */
    public function index()
    {
        // 测试,下面代码可删除
        // $redis = $this->getRedis();

        // $redis->HSET('aaaaa', 'bbb', 'smallnews');

        // // 获取活动集合
        // $hashList = $redis->ZRANGE('aaaaa', 0, 999999999);
        // var_dump($hashList);


        // $user = \addons\shopro\model\User::where('id', 57)->find();

        // $user->notify(
        //     new \addons\shopro\notifications\Order([
        //         // 'order' => \addons\shopro\model\Order::where('id', 359)->find(),
        //         // 'item' => \addons\shopro\model\OrderItem::where('id', 390)->find(),
        //         'order' => \addons\shopro\model\Order::where('id', 349)->find(),
        //         'item' => \addons\shopro\model\OrderItem::where('id', 380)->find(),
        //         'event' => 'order_sended'
        //     ])
        // );

        // $user = \addons\shopro\model\User::where('id', 2)->find();
        // $user->notify(
        //     new \addons\shopro\notifications\Aftersale([
        //         'aftersale' => \addons\shopro\model\OrderAftersale::get(6),
        //         'order' => \addons\shopro\model\Order::get(115),
        //         'aftersaleLog' => \addons\shopro\model\OrderAftersaleLog::get(9),
        //         'event' => 'aftersale_change'
        //     ])
        // );


        // $user = \addons\shopro\model\User::where('id', 2)->find();
        // $user->notify(
        //     new \addons\shopro\notifications\Wallet([
        //         'apply' => \addons\shopro\model\UserWalletApply::get(1),
        //         'event' => 'wallet_apply'
        //     ])
        // );

        // $user = \addons\shopro\model\User::where('id', 2)->find();
        // $user->notify(
        //     new \addons\shopro\notifications\Wallet([
        //         'walletLog' => \addons\shopro\model\UserWalletLog::get(1),
        //         'event' => 'wallet_change'
        //     ])
        // );
        // $user = \addons\shopro\model\User::where('id', 2)->find();
        // $user->notify(
        //     new \addons\shopro\notifications\store\Order([
        //         'store' => \addons\shopro\model\Store::get(1),
        //         'order' => \addons\shopro\model\Order::get(83),
        //         'event' => 'store_order_new'
        //     ])
        // );

    }

    /**
     * @ApiWeigh    (97)
     * @ApiTitle    (商品详情)
     * @ApiSummary  (商品详情)
     * @ApiMethod   (GET)
     *
     * @ApiHeaders (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams  (name=id, type=inter, required=true, description="商品ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "商品详情",
        "time": "1607678789",
        "data": {
            "id": 1, //商品ID
            "type": "normal",
            "title": "测试商品", //商品名称
            "subtitle": "测试啊", //副标题
            "weigh": 0,
            "category_ids": "3",
            "image": "http://www.ant.top/assets/img/qrcode.png", //商品主图
            "images": [ //轮播图
                "http://www.ant.top/uploads/20201211/ceaf516172280f5da413fb020bf777c9.png"
            ],
            "params": [{
                "title": "50",
                "content": "123"
            }],
            "content": "", //详情
            "price": "0.01", //现价
            "original_price": "10000.00", //划线价
            "is_sku": 0, //是否多规格:0=否,1=是
            "likes": 0, //收藏数量
            "views": 2, //浏览量
            "sales": 0, //销量
            "show_sales": 0, //显示销量
            "service_ids": "",
            "dispatch_type": "express",
            "dispatch_ids": "1",
            "deletetime": null,
            "activity": { //活动信息
                "id": 7,
                "title": "秒杀测试",
                "type": "seckill",
                "richtext_id": 0,
                "richtext_title": "",
                "starttime": 1607616000,
                "endtime": 1607702400,
                "rules": {
                    "limit_buy": "1",
                    "order_auto_close": "",
                    "activity_auto_close": ""
                }
            },
            "activity_type": "seckill",
            "sku_price": [{ //规格信息
                "id": 1,
                "goods_sku_ids": null,
                "goods_id": 1,
                "weigh": 0,
                "image": null,
                "stock": 20,
                "sales": 0,
                "sn": "",
                "weight": 0,
                "price": "0.01",
                "goods_sku_text": null,
                "status": "up",
                "activity_type": "seckill",
                "activity_id": 7,
                "item_goods_sku_price": {
                    "id": 4,
                    "activity_id": 7,
                    "sku_price_id": 1,
                    "goods_id": 1,
                    "stock": 20,
                    "sales": 0,
                    "price": "0.01",
                    "status": "up"
                },
                "goods_sku_id_arr": [
                    ""
                ]
            }],
            "stock": 20,
            "favorite": null,
            "dispatch_type_arr": [
                "express"
            ],
            "service": [{ //服务标签
                "id": 4, //标签ID
                "name": "极速退款", //名称
                "image": "http://api.7wpp.com/uploads/20200222/57d971eb5984317ee794e531ea0f9207.png", //图片
                "description": "超快无审核退款流程" //描述
            }],
            "sku": [{ //规格
                "id": 1,
                "name": "颜色", //一级规格名称
                "pid": 0,
                "goods_id": 4,
                "weigh": 0,
                "content": [{ //二级规格
                    "id": 2, //规格ID
                    "name": "黄色", //名称
                    "pid": 1,
                    "goods_id": 4,
                    "weigh": 0
                }]
            }],
            "coupons": []
        }
    })
     */
    public function detail()
    {
        $id = $this->request->get('id');
        $detail = \addons\shopro\model\Goods::getGoodsDetail($id);
        
        // 记录足记
        \addons\shopro\model\UserView::addView($detail);

        $sku_price = $detail['sku_price'];      // 处理过的规格
        // tp bug json_encode 或者 toArray 的时候 sku_price 会重新查询数据库,导致被处理过的规格又还原回去了
        $detail = json_decode(json_encode($detail), true);
        $detail['sku_price'] = $sku_price;

        $this->success('商品详情', $detail);
    }

    /**
     * @ApiWeigh    (99)
     * @ApiTitle    (商品列表)
     * @ApiSummary  (商品列表)
     * @ApiMethod   (GET)
     *
     * @ApiHeaders (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams  (name=order, type=string, required=false, description="排序:{'defaultOrder':0,'priceOrder':0,'salesOrder':1,'newProdcutOrder':0},defaultOrder为1=默认,priceOrder为1=价格从低到高,priceOrder为2=价格从高到低,salesOrder为1=销量从低到高,salesOrder为2=销量从高到低,newProdcutOrder为1=最新上架")
     * @ApiParams  (name=keyword, type=string, required=false, description="搜索关键字")
     * @ApiParams  (name=category_id, type=inter, required=false, description="分类ID")
     * @ApiParams  (name=page, type=inter, required=false, description="分页页码")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "商品列表",
        "time": "1607675191",
        "data": {
            "total": 2,
            "per_page": 10,
            "current_page": 1,
            "last_page": 1,
            "data": [{
                "id": 1, //商品ID
                "type": "normal", //类型:nomal=正常,hidden=隐藏
                "title": "测试商品", //商品名称
                "subtitle": "测试啊", //商品小名称
                "weigh": 0, //权重
                "category_ids": "3", //所属分类ID
                "image": "http://www.ant.top/assets/img/qrcode.png", //商品主图
                "price": "0.01", //现价
                "original_price": "10000.00", //划线价格
                "is_sku": 0, //是否多规格:0=否,1=是
                "likes": 0, //收藏人数
                "views": 2, //浏览人数
                "sales": 0, //销量
                "show_sales": 0, //显示销量
                "dispatch_type": "express", //发货方式:express=物流快递
                "dispatch_ids": "1", //发货模板ID
                "deletetime": null,
                "activity": { //活动
                    "id": 7, //活动ID
                    "title": "秒杀测试", //活动名称
                    "type": "seckill", //活动类型:seckill=秒杀,groupon=拼团
                    "richtext_id": 0,
                    "richtext_title": "",
                    "starttime": 1607616000,
                    "endtime": 1607702400,
                    "rules": {
                        "limit_buy": "1",
                        "order_auto_close": "",
                        "activity_auto_close": ""
                    }
                },
                "activity_type": "seckill", //活动类型:seckill=秒杀,groupon=拼团
                "sku_price": [{ //规格价格
                    "id": 1, //规格价格ID
                    "goods_sku_ids": null, //商品规格ID集合
                    "goods_id": 1, //商品ID
                    "weigh": 0,
                    "image": null, //规格图片
                    "stock": 20, //库存
                    "sales": 0, //销量
                    "sn": "",
                    "weight": 0,
                    "price": "0.01", //价格
                    "goods_sku_text": null, //中文规格
                    "status": "up", //状态:up=上架
                    "activity_type": "seckill", //活动类型:seckill=秒杀,grupon=拼团
                    "activity_id": 7, //活动ID
                    "item_goods_sku_price": {
                        "id": 4,
                        "activity_id": 7,
                        "sku_price_id": 1,
                        "goods_id": 1,
                        "stock": 20,
                        "sales": 0,
                        "price": "0.01",
                        "status": "up"
                    },
                    "goods_sku_id_arr": [
                        ""
                    ]
                }],
                "stock": 20, //库存
                "dispatch_type_arr": [
                    "express"
                ]
            }]
        }
    })
     */
    public function lists()
    {
        $params = $this->request->get();
        $data = \addons\shopro\model\Goods::getGoodsList($params);

        $this->success('商品列表', $data);

    }


    /**
     * @ApiInternal
     * 获取商品支持的 自提点
     */
    public function store()
    {
        $params = $this->request->get();
        $data = \addons\shopro\model\Goods::getGoodsStore($params);

        $this->success('自提列表', $data);
    }

    /**
     * @ApiWeigh    (95)
     * @ApiTitle    (秒杀列表)
     * @ApiSummary  (秒杀列表)
     * @ApiMethod   (GET)
     *
     * @ApiHeaders (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams  (name=type, type=string, required=false, description="类型:all=全部,ing=进行中,nostart=未开始,ended=已结束,tomorrow=明日预告")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "秒杀商品列表",
        "time": "1607734877",
        "data": {
            "total": 1, //数据总数
            "per_page": 10,
            "current_page": 1,
            "last_page": 1,
            "data": [{
                "id": 1, //商品ID
                "type": "normal",
                "title": "测试商品", //标题
                "subtitle": "测试啊", //副标题
                "weigh": 0,
                "category_ids": "3", //所属分类ID
                "image": "http://www.ant.top/assets/img/qrcode.png", //商品主图
                "price": "0.01", //商品价格
                "original_price": "10000.00", //原价
                "is_sku": 0,
                "likes": 0,
                "views": 3,
                "sales": 0, //销量
                "show_sales": 0, //显示销量
                "dispatch_type": "express",
                "dispatch_ids": "1",
                "deletetime": null,
                "activity": {
                    "id": 7, //活动ID
                    "title": "秒杀测试", //活动标题
                    "type": "seckill", //活动类型:seckill=秒杀,groupon=拼团
                    "richtext_id": 0,
                    "richtext_title": "",
                    "starttime": 1607616000,
                    "endtime": 1607702400,
                    "rules": {
                        "limit_buy": "1",  //每人限购件数
                        "order_auto_close": "",
                        "activity_auto_close": ""
                    }
                },
                "activity_type": "seckill",
                "sku_price": [{
                    "id": 1,
                    "goods_sku_ids": null,
                    "goods_id": 1,
                    "weigh": 0,
                    "image": null,
                    "stock": 20,
                    "sales": 0,
                    "sn": "",
                    "weight": 0,
                    "price": "0.01",
                    "goods_sku_text": null,
                    "status": "up",
                    "activity_type": "seckill",
                    "activity_id": 7,
                    "item_goods_sku_price": {
                        "id": 4,
                        "activity_id": 7,
                        "sku_price_id": 1,
                        "goods_id": 1,
                        "stock": 20,
                        "sales": 0,
                        "price": "0.01",
                        "status": "up"
                    },
                    "goods_sku_id_arr": [
                        ""
                    ]
                }],
                "stock": 20,
                "dispatch_type_arr": [
                    "express"
                ]
            }]
        }
    })
     */
    public function seckillList() {
        $params = $this->request->get();

        $this->success('秒杀商品列表', \addons\shopro\model\Goods::getSeckillGoodsList($params));
    }

    /**
     * @ApiWeigh    (93)
     * @ApiTitle    (拼团列表)
     * @ApiSummary  (拼团列表)
     * @ApiMethod   (GET)
     *
     * @ApiHeaders (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams  (name=type, type=string, required=false, description="类型:all=全部,ing=进行中,nostart=未开始,ended=已结束,tomorrow=明日预告")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "拼团商品列表",
        "time": "1608039147",
        "data": {
            "total": 1, //数据总数
            "per_page": 10,
            "current_page": 1,
            "last_page": 1,
            "data": [{
                "id": 2, //商品ID
                "type": "normal",
                "title": "测试商品2", //商品标题
                "subtitle": "测试啊啊", //商品副标题
                "weigh": 0,
                "category_ids": "3",
                "image": "http://www.ant.top/uploads/20201211/8894d62100f2f920ffb2f38063b63f2d.jpg", //商品主图
                "price": "0.02", //现价
                "original_price": "1000.00", //原价
                "is_sku": 0, //是否多规格:0=否,1=是
                "likes": 0,
                "views": 1,
                "sales": 0,
                "show_sales": 0, //显示销量
                "dispatch_type": "express",
                "dispatch_ids": "1",
                "deletetime": null,
                "activity": {
                    "id": 9, //活动ID
                    "title": "拼团测试", //活动标题
                    "type": "groupon", //活动类型:seckill=秒杀,groupon=拼团
                    "richtext_id": 0,
                    "richtext_title": "",
                    "starttime": 1607616000,
                    "endtime": 1607702400,
                    "rules": { //活动规则
                        "limit_buy": "1", //每人限购件数
                        "order_auto_close": "",
                        "activity_auto_close": "",
                        "team_num": "2", //成团人数
                        "is_alone": "0", //是否允许单独购买
                        "is_fictitious": "0",
                        "fictitious_num": "",
                        "valid_time": "",
                        "team_card": "0"
                    }
                },
                "activity_type": "groupon",
                "groupon_price": "0.01",
                "sku_price": [{
                    "id": 2,
                    "goods_sku_ids": null,
                    "goods_id": 2,
                    "weigh": 0,
                    "image": null,
                    "stock": 10,
                    "sales": 0,
                    "sn": "",
                    "weight": 0,
                    "price": "0.02",
                    "goods_sku_text": null,
                    "status": "up",
                    "groupon_price": "0.01",
                    "activity_type": "groupon",
                    "activity_id": 9,
                    "item_goods_sku_price": {
                        "id": 5,
                        "activity_id": 9,
                        "sku_price_id": 2,
                        "goods_id": 2,
                        "stock": 10,
                        "sales": 0,
                        "price": "0.01",
                        "status": "up"
                    },
                    "goods_sku_id_arr": [
                        ""
                    ]
                }],
                "stock": 10,
                "dispatch_type_arr": [
                    "express"
                ]
            }]
        }
    })
     */
    public function grouponList() {
        $params = $this->request->get();

        $this->success('拼团商品列表', \addons\shopro\model\Goods::getGrouponGoodsList($params));
    }

    /**
     * @ApiInternal
     * 活动列表
     */
    public function activity()
    {
        $activity_id = $this->request->get('activity_id');
        $activity = \addons\shopro\model\Activity::get($activity_id);
        if (!$activity) {
            throw new Exception('活动不存在', -1);
        }
        
        $goods = \addons\shopro\model\Goods::getGoodsList(['goods_ids' => $activity->goods_ids]);
        $activity->goods = $goods;
        
        $this->success('活动列表', $activity);
    }


    /**
     * @ApiWeigh    (93)
     * @ApiTitle    (收藏或取消收藏)
     * @ApiSummary  (收藏或取消收藏)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams  (name=goods_id, type=inter, required=false, description="商品ID")
     * @ApiParams  (name=goods_ids, type=inter, required=false, description="商品ID集合,批量删除时可用")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "收藏成功|取消收藏",
        "time": "1608039147",
        "data": null
    })
     */
    public function favorite()
    {
        $params = $this->request->post();
        $result = \addons\shopro\model\UserFavorite::edit($params);
        $this->success($result ? '收藏成功' : '取消收藏', $result);
    }

    /**
     * @ApiInternal
     * 商品收藏列表
     */
    public function favoriteList()
    {
        $data = \addons\shopro\model\UserFavorite::getGoodsList();
        $this->success('商品收藏列表', $data);
    }

    /**
     * @ApiInternal
     * 浏览记录删除
     */
    public function viewDelete()
    {
        $params = $this->request->post();
        $result = \addons\shopro\model\UserView::del($params);
        $this->success('删除成功', $result);
    }

    /**
     * @ApiInternal
     * 浏览记录列表
     */
    public function viewList()
    {
        $data = \addons\shopro\model\UserView::getGoodsList();
        $this->success('商品浏览列表', $data);
    }



}