<?php

namespace app\controller\api\store\product;

use app\common\repositories\user\UserHistoryRepository;
use think\App;
use ln\basic\BaseController;
use app\common\repositories\store\product\SpuRepository;

class StoreSpu extends BaseController
{
    protected $userInfo;
    protected $repository;

    public function __construct(App $app, SpuRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
        $this->userInfo = $this->request->isLogin() ? $this->request->userInfo() : null;
    }

    /**
     * TODO 商品搜索列表
     * @return mixed
     * @author Qinii
     * @day 12/24/20
     */
    public function lst()
    {
        [$page, $limit] = $this->getPage();
        $where = $this->request->params([
            'keyword', 'cate_id', 'cate_pid', 'order', 'price_on', 'price_off', 'brand_id', 'pid','mer_cate_id','product_type','action', 'common'
        ]);
        $where['is_gift_bag'] = 0;
        $where['order'] = $where['order'] ? $where['order'] : 'star';
        $data = $this->repository->getApiSearch($where, $page, $limit, $this->userInfo);
        return app('json')->success($data);
    }

    /**
     * TODO 商户的商品搜索列表
     * @param $id
     * @return mixed
     * @author Qinii
     * @day 12/24/20
     */
    public function merProductLst($id)
    {
        [$page, $limit] = $this->getPage();
        $where = $this->request->params([
            'keyword', 'cate_id','order', 'price_on', 'price_off', 'brand_id', 'pid','mer_cate_id','product_type','action','common'
        ]);
        $where['mer_id'] = $id;
        $where['is_gift_bag'] = 0;
        $where['order'] = $where['order'] ? $where['order'] : 'sort';
        $data = $this->repository->getApiSearch($where, $page, $limit, $this->userInfo);
        return app('json')->success($data);
    }

    /**
     * TODO 推荐列表
     * @return mixed
     * @author Qinii
     * @day 12/24/20
     */
    public function recommend()
    {
        [$page, $limit] = $this->getPage();
        $where = $this->request->params(['common']);
        $where['order'] = 'sales';
        $where['is_gift_bag'] = 0;
        if (!is_null( $this->userInfo)) {
            $cateId= app()->make(UserHistoryRepository::class)->getRecommend($this->userInfo->uid);
            if($cateId && count($cateId) > 5) $where['cate_id'] = $cateId;
        }
        $data = $this->repository->getApiSearch($where, $page, $limit, $this->userInfo);
        return app('json')->success($data);
    }

    /**
     * TODO 热门列表
     * @return mixed
     * @author Qinii
     * @day 12/24/20
     */
    public function hot($type)
    {
        [$page, $limit] = $this->getPage();
        $where = $this->request->params(['common']);
        $where['hot_type'] = $type;
        $where['is_gift_bag'] = 0;
        $where['order'] = 'star';
        $data = $this->repository->getApiSearch($where, $page, $limit, null);
        return app('json')->success($data);
    }

    /**
     * TODO 礼包列表
     * @return mixed
     * @author Qinii
     * @day 12/24/20
     */
    public function bag()
    {
        [$page, $limit] = $this->getPage();
        $where['is_gift_bag'] = 1;
        $where['order'] = 'rank';
        $data = $this->repository->getApiSearch($where, $page, $limit,null);
        return app('json')->success($data);
    }

    /**
     * TODO 礼包推荐列表
     * @return mixed
     * @author Qinii
     * @day 12/24/20
     */
    public function bagRecommend()
    {
        [$page, $limit] = $this->getPage();
        $where['is_gift_bag'] = 1;
        $where['hot_type'] = 'best';
        $data = $this->repository->getApiSearch($where, $page, $limit,null);
        return app('json')->success($data);
    }

    /**
     * TODO 活动分类
     * @param $type
     * @return \think\response\Json
     * @author Qinii
     * @day 1/12/21
     */
    public function activeCategory($type)
    {
        $data = $this->repository->getActiveCaategory($type);
        return app('json')->success($data);
    }
}
