<?php
/**
 * Created by PhpStorm.
 * User: 86132
 * Date: 2020/11/17
 * Time: 20:51
 */

namespace app\api\controller;

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

/**
 * 商城板块接口
 */
class Shop extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 商城板块接口
     * @ApiTitle    (轮播图)
     * @ApiSummary  (轮播图)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Shop/Banner)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": [
    "/uploads/20201117/673680f3f8eff5f23230f77947b9b092.png",
    "/uploads/20201117/afee095e2ce79ad688bb890d7fb555ad.png"
    ]
    })
     */
    public function Banner()
    {
        $BannerArray = Db::name('shop_banner')->order('weigh desc')->select();
        $Banner = [];

        if (!empty($BannerArray)) {
            foreach ($BannerArray as $k => $v) {
                $Banner[$k]['Id'] = $v['id'];
                $Banner[$k]['Image'] = cdnurl($v['image']);
                $Banner[$k]['Type'] = $v['type'];
                $Banner[$k]['Web'] = $v['web'];
                $Banner[$k]['Page'] = $v['page'];
                $Banner[$k]['GoodsId'] = $v['Goods_id'];
            }
        }
        $this->success('成功', $Banner);
    }


    /**
     * 商城板块接口
     * @ApiTitle    (左侧分类)
     * @ApiSummary  (左侧分类)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Shop/LeftList)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": [
    {
    "Id": 1,
    "Title": "睡美人计划"
    },
    {
    "Id": 2,
    "Title": "日间补充"
    },
    {
    "Id": 3,
    "Title": "能量补充"
    }
    ]
    })
     */
    public function LeftList()
    {
        $Array = Db::name('season_hot')->order('weigh desc')->select();
        $List = [];
        if (!empty($Array)) {
            foreach ($Array as $k => $v) {
                $List[$k]['Id'] = $v['id'];
                $List[$k]['Title'] = $v['title'];
            }
        }
        $this->success('成功', $List);
    }


    /**
     * 商城板块接口
     * @ApiTitle    (商品筛选--有更改【返回一句话介绍+可赚金额】)
     * @ApiSummary  (商品筛选--有更改【返回一句话介绍+可赚金额】)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Shop/ChooseGoods)
     * @ApiParams   (name="keywords", type="string", required=true, description="搜索关键字")
     * @ApiParams   (name="type", type="int", required=true, description="分类ID")
     * @ApiParams   (name="sell", type="int", required=true, description="销量1=升序,2=降序")
     * @ApiParams   (name="price", type="string", required=true, description="价格1=升序,2=降序")
     * @ApiParams   (name="pages", type="string", required=true, description="pages")
     * @ApiParams   (name="rows", type="string", required=true, description="rows")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    "Count": 6,
    "List": [
    {
    "Id": 6,
    "Image": "/uploads/20201113/afee095e2ce79ad688bb890d7fb555ad.png",
    "Title": "悉见复方身体按摩油",
    "Speci": "15ml",
    "Price": "128.00",
    "Sales": 0,
    "Type": 0  1=精选,0=否
    },
    {
    "Id": 1,
    "Image": "/uploads/20201113/afee095e2ce79ad688bb890d7fb555ad.jpg",
    "Title": "晚安复方纯精油",
    "Speci": "15ml",
    "Price": "187.99",
    "Sales": 0,
    "Type": 0
    },
    {
    "Id": 3,
    "Image": "/uploads/20201113/afee095e2ce79ad688bb890d7fb555ad.png",
    "Title": "于意复方纯精油",
    "Speci": "15ml",
    "Price": "208.00",
    "Sales": 0,
    "Type": 0
    },
    {
    "Id": 13,
    "Image": "/uploads/20201113/afee095e2ce79ad688bb890d7fb555ad.png",
    "Title": "悉知复方纯精油",
    "Speci": "15ml",
    "Price": "218.00",
    "Sales": 0,
    "Type": 0
    },
    {
    "Id": 11,
    "Image": "/uploads/20201113/afee095e2ce79ad688bb890d7fb555ad.png",
    "Title": "如是复方纯精油",
    "Speci": "15ml",
    "Price": "238.00",
    "Sales": 0,
    "Type": 0
    },
    {
    "Id": 7,
    "Image": "/uploads/20201113/afee095e2ce79ad688bb890d7fb555ad.png",
    "Title": "安之复方纯精油",
    "Speci": "15ml",
    "Price": "268.00",
    "Sales": 0,
    "Type": 0
    }
    ]
    }
    })
     */
    public function ChooseGoods()
    {
        $params = $this->request->param();
        //查询系列
        $GoodsIds = Db::name('season_hot')->where('id', $params['type'])->find();
        //精品
        $Jing = Db::name('bili')->where('type', 1)->value('first');
        //通品
        $Tong = Db::name('bili')->where('type', 2)->value('first');
        if (empty($GoodsIds)) {
            $data = [
                'Count' => 0,
                'List' => []
            ];
        } else {
            if (strstr($GoodsIds['Goods_ids'], ',')) {
                //商品ID $SeasonGoodsArray
                $GoodsIdsArray = explode(',', $GoodsIds['Goods_ids']);
            } else {
                $GoodsIdsArray = explode(' ', $GoodsIds['Goods_ids']);
            }
            $GoodsArray = [];
            foreach ($GoodsIdsArray as $k => $v) {
                $IfEmptyGoodsArray = Db::name('goods')->where('id', $v)->find();
                if (!empty($IfEmptyGoodsArray)) {
                    $GoodsArray[$k]['Id'] = $IfEmptyGoodsArray['id'];
                    $GoodsArray[$k]['Image'] = cdnurl($IfEmptyGoodsArray['image']);
                    $GoodsArray[$k]['Title'] = $IfEmptyGoodsArray['title'];
                    $GoodsArray[$k]['OneTalk'] = $IfEmptyGoodsArray['one_talk'];
                    $GoodsArray[$k]['Speci'] = $IfEmptyGoodsArray['speci'];
                    $GoodsArray[$k]['Price'] = $IfEmptyGoodsArray['price'];
                    $GoodsArray[$k]['Sales'] = $IfEmptyGoodsArray['sales'];
                    $GoodsArray[$k]['Type'] = $IfEmptyGoodsArray['type'];
                    if ($IfEmptyGoodsArray['type'] == 1) {
                        $GoodsArray[$k]['KeZhuan'] = round($IfEmptyGoodsArray['price'] * $Jing, 1);
                    } else {
                        $GoodsArray[$k]['KeZhuan'] = round($IfEmptyGoodsArray['price'] * $Tong, 1);
                    }
                }
            }
            if (empty($GoodsArray)) {
                $data = [
                    'Count' => 0,
                    'List' => []
                ];
            } else {
                //关键字筛选
                if (!empty($params['keywords']) || $params['keywords'] != '' || $params['keywords'] != "" || $params['keywords'] != null) {
                    $GoodsArrayFirst = [];
                    foreach ($GoodsArray as $k => $v) {
                        if (strstr($v['Title'], $params['keywords'])) {
                            $GoodsArrayFirst[$k]['Id'] = $v['Id'];
                            $GoodsArrayFirst[$k]['Image'] = $v['Image'];
                            $GoodsArrayFirst[$k]['Title'] = $v['Title'];
                            $GoodsArrayFirst[$k]['OneTalk'] = $v['one_talk'];
                            $GoodsArrayFirst[$k]['Speci'] = $v['Speci'];
                            $GoodsArrayFirst[$k]['Price'] = $v['Price'];
                            $GoodsArrayFirst[$k]['Sales'] = $v['Sales'];
                            $GoodsArrayFirst[$k]['Type'] = $v['Type'];
                            if ($v['type'] == 1) {
                                $GoodsArrayFirst[$k]['KeZhuan'] = $v['price'] * $Jing;
                            } else {
                                $GoodsArrayFirst[$k]['KeZhuan'] = $v['price'] * $Tong;
                            }
                        }
                    }
                } else {
                    $GoodsArrayFirst = $GoodsArray;
                }
                if (empty($GoodsArrayFirst)) {
                    $data = [
                        'Count' => 0,
                        'List' => []
                    ];
                } else {
                    //销量排序
                    if ($params['sell'] == 1) {
                        //升序
                        $GoodsArraySell = $this->SalesAsc($GoodsArrayFirst);
                    } else {
                        //降序
                        $GoodsArraySell = $this->SalesDesc($GoodsArrayFirst);
                    }
                    //价格排序
                    if ($params['price'] == 1) {
                        //升序
                        $GoodsArrayPrice = $this->PriceAsc($GoodsArraySell);
                    } else {
                        //降序
                        $GoodsArrayPrice = $this->PriceDesc($GoodsArraySell);
                    }
                    $NewList = $this->page_array($params['rows'], $params['pages'], $GoodsArrayPrice, 0);
                    $data = [
                        'Count' => count($GoodsArrayPrice),
                        'List' => $NewList
                    ];
                }
            }
        }
        $this->success('成功', $data);
    }
}