<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 首页-banner图列表
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bannerList()
    {
        $list = Db::name("unishop_banner")->where('status',1)->whereNull("deletetime")->order('weigh desc')->select();

        $this->success('请求成功',$list);
    }

    /**
     * 首页-四方商品图列表
     * @return void
     */
    public function GoodSquare(){

        $list_new = Db::name("unishop_product")->field('image')->where("switch",1)->where("is_new",1)->whereNull("deletetime")->order('weigh desc')->limit(1)->select();
        $list_preferred = Db::name("unishop_product")->field('image')->where("switch",1)->where("is_preferred",1)->whereNull("deletetime")->order('weigh desc')->limit(1)->select();
        $list_hot = Db::name("unishop_product")->field('image')->where("switch",1)->where("is_hot",1)->whereNull("deletetime")->order('weigh desc')->limit(1)->select();
        $list_discount = Db::name("unishop_product")->field('image')->where("switch",1)->where("is_discount",1)->whereNull("deletetime")->order('weigh desc')->limit(1)->select();

        $data = [
            "new"=>$list_new,
            "preferred"=>$list_preferred,
            "hot"=>$list_hot,
            "discount"=>$list_discount,
        ];

        $this->success("四方商品图列表",$data);

    }

    /**
     * 首页-分类模块
     * @return void
     */
    public function getCatList(){

        $list = Db::name("unishop_category")->field('id,name,keywords,description')->where('status','normal')->whereNull("deletetime")->order('weigh desc')->select();

        $this->success("分类商品",$list);

    }

    /**
     * 首页-商品列表
     * @return void
     */
    public function getGoodsList(){
        $page = input('page',1); //页码
        $limit = input('limit',10); //条数
        $offset = ($page - 1) * $limit;
        $cid = input('cid',0); //分类ID

        $where = [];
        $where['switch'] = 1;
        if(!empty($cid)){
            $where['category_id'] = $cid;
        }
        $list = Db::name("unishop_product")->field('id,title,image,sales_price')->where($where)->whereNull("deletetime")->order("weigh desc")->limit($offset,$limit)->select();

        $this->success("分类商品列表",$list);


    }

    /**
     * 首页-推荐模版商品列表
     * @return void
     */
    public function getModList(){

        $type = input('type',1); //类型 1=今日推荐，2=热门榜单，3=首发新品，4=促销单品
        $page = input('page',1); //页码
        $limit = input('limit',10); //条数
        $offset = ($page - 1) * $limit;
        $where = [];
        $where['switch'] = 1;
        switch ($type) {
            case 1:
                $where['is_preferred'] = 1;
            case 2:
                $where['is_hot'] = 1;
            case 3:
                $where['is_new'] = 1;
            case 4:
                $where['is_discount'] = 1;
            default:
                $where['is_preferred'] = 1;

        }
        $list = Db::name("unishop_product")->field('id,title,image,sales_price')->where($where)->whereNull("deletetime")->order('weigh desc')->limit($offset,$limit)->select();

        $this->success("推荐商品列表",$list);
    }

    /**
     * 零售商品列表
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRetailList(){

        $page = input('page',1); //页码
        $limit = input('limit',10); //条数
        $offset = ($page - 1) * $limit;
        $where = [];
        $where['switch'] = 1;
        $list = Db::name("unishop_product")->field('id,title,image,sales_price')->where($where)->whereNull("deletetime")->order('weigh desc')->limit($offset,$limit)->select();

        $this->success("零售商品列表",$list);

    }

    /**
     * 批发商品列表
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getWholesaleList(){

        $page = input('page',1); //页码
        $limit = input('limit',10); //条数
        $offset = ($page - 1) * $limit;
        $where = [];
        $where['p.switch'] = 1;
        $list = Db::name("unishop_wholesale_goods")->alias('p')->join('fa_user u','u.id=p.user_id')->field('p.user_id,ANY_VALUE(p.id) as id,ANY_VALUE(p.title) as  title,ANY_VALUE(p.image) as image,ANY_VALUE( p.sales_price) as sales_price,p.goods_id,ANY_VALUE( u.nickname) as nickname,ANY_VALUE( u.avatar) as avatar')->where($where)->whereNull("p.deletetime")->limit($offset,$limit)->group('p.user_id,p.goods_id')->select();

        if(!empty($list)){
            foreach ($list as $key=>$value){

                $cout = Db::name("unishop_wholesale_goods")->where("switch",1)->where("user_id",$value['user_id'])->where("goods_id",$value['goods_id'])->whereNull("deletetime")->sum("stock");

                $list[$key]['sales'] = $cout;

            }
        }

        $this->success("批发商品列表",$list);


    }


}
