<?php

namespace app\api\controller;

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

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

    /**
     * 首页接口
     * @ApiTitle    (首页接口-轮播图)
     * @ApiSummary  (轮播图)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/Banner)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": [
    {
    "id": 1,
    "image": "http://xiche.qiniu.bronet.cn/uploads/20200831/FsGkAQhqnwBodzLvJyn1rtbZIFUd.png",
    "content": "<p>富文本详情</p>",
    "hot_id": 热点文章ID,
    "seller_id": 门店,
    "type": 类别:1=富文本详情,2=热点文章详情,3=门店详情,
    "createtime": 1598851787,
    "updatetime": 1598852871
    }
    ]
    })
     */
    public function Banner()
    {
        $list = Db::name('index_banner')->select();
        if (empty($list)) {
            $list = [];
        } else {
            foreach ($list as $k => $v) {
                $list[$k]['image'] = cdnurl($v['image']);
            }
        }
        $this->success('成功', $list);
    }


    /**
     * 首页接口
     * @ApiTitle    (首页接口-热点文章列表)
     * @ApiSummary  (热点文章列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/HotList)
     * @ApiParams   (name="pages", type="int", required=true, description="pages")
     * @ApiParams   (name="rows", type="int", 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": 总数,
    "list": [
    {
    "id": 2,
    "avatar": "http://xiche.qiniu.bronet.cn/uploads/20200831/FsGkAQhqnwBodzLvJyn1rtbZIFUd.png",
    "type": 1=置顶,0=不置顶,
    "title": "这是第二个热点新闻",
    "look": 查看数,
    "createtime": "2020-08-31"
    }
    ]
    }
    })
     */
    public function HotList()
    {
        $param = $this->request->param();
        $arr = Db::name('hot')->order('id desc')->select();
        if (empty($arr)) {
            $count = 0;
            $list = [];
        } else {
            foreach ($arr as $k => $v) {
                if ($v['type'] == 1) {
                    $list[$k]['id'] = $v['id'];
                    $list[$k]['avatar'] = cdnurl($v['avatar']);
                    $list[$k]['type'] = $v['type'];
                    $list[$k]['title'] = $v['title'];
                    $list[$k]['look'] = $v['look'];
                    $list[$k]['createtime'] = date('Y-m-d', $v['createtime']);
                }
            }
            foreach ($arr as $k => $v) {
                if ($v['type'] == 0) {
                    $list[$k]['id'] = $v['id'];
                    $list[$k]['avatar'] = cdnurl($v['avatar']);
                    $list[$k]['type'] = $v['type'];
                    $list[$k]['title'] = $v['title'];
                    $list[$k]['look'] = $v['look'];
                    $list[$k]['createtime'] = date('Y-m-d', $v['createtime']);
                }
            }
            $count = count($list);
            $list = $this->page_array($param['rows'], $param['pages'], $list, 0);
        }
        $rult = [
            'count' => $count,
            'list' => $list
        ];
        $this->success('成功', $rult);
    }


    /**
     * 首页接口
     * @ApiTitle    (首页接口-热点文章详情)
     * @ApiSummary  (热点文章详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/HotCon)
     * @ApiParams   (name="id", type="int", required=true, description="热点文章ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    "id": 1,
    "avatar": "http://xiche.qiniu.bronet.cn/uploads/20200831/FsGkAQhqnwBodzLvJyn1rtbZIFUd.png",
    "title": "这是第一个热点新闻",
    "type": 1=置顶,0=不置顶,,
    "content": "<p>这是第一个热点新闻这是第一个热点新闻这是第一个热点新闻这是第一个热点新闻这是第一个热点新闻这是第一个热点新闻这是第一个热点新闻<br></p>",
    "look": 查看数,
    "createtime": "2020-08-31",
    "updatetime": 1598852900
    }
    })
     */
    public function HotCon()
    {
        $id = input('id');
        $look = Db::name('hot')->where('id', $id)->value('look');
        Db::name('hot')->where('id', $id)->update(['look' => $look + 1]);
        $array = Db::name('hot')->where('id', $id)->find();
        if (empty($array)) {
            $this->error('参数错误', 0);
            die;
        }
        $array['avatar'] = cdnurl($array['avatar']);
        $array['createtime'] = date('Y-m-d', $array['createtime']);
        $this->success('成功', $array);
    }


    /**
     * 首页接口
     * @ApiTitle    (首页接口-热点文章查看数增加)
     * @ApiSummary  (热点文章查看数增加)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/HotLookNumSave)
     * @ApiParams   (name="id", type="int", required=true, description="热点文章ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    }
    })
     */
    public function HotLookNumSave()
    {
        $id = input('id');
        $look = Db::name('hot')->where('id', $id)->value('look');
        $res = Db::name('hot')->where('id', $id)->update(['look' => $look + 1]);
        $this->res($res);
    }


    /**
     * 首页接口
     * @ApiTitle    (首页接口-会员购买详情)
     * @ApiSummary  (会员购买详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/BuyVipCon)
     * @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": "名称",
    "con": "权益",
    "type": 1=折扣0=普通,
    "del_price": "划线价格",
    "price": "价格"
    }
    ]
    })
     */
    public function BuyVipCon()
    {
        $rult = Db::name('vip')->select();
        if (empty($rult)) {
            $list = [];
        } else {
            foreach ($rult as $k => $v) {
                $list[$k]['id'] = $v['id'];
                $list[$k]['title'] = $v['title'];
                $list[$k]['con'] = $v['con'];
                $list[$k]['type'] = $v['type'];
                $list[$k]['del_price'] = $v['del_price'];
                $list[$k]['price'] = $v['price'];
            }
        }
        $this->success('成功', $list);
    }


    /**
     * 首页接口
     * @ApiTitle    (首页接口-洗车服务详情)
     * @ApiSummary  (洗车服务详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/CarConWri)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    "fuwu": "<p>服务内容<img alt=\"1111.png\" src=\"http://xiche.qiniu.bronet.cn/uploads/20200831/FsGkAQhqnwBodzLvJyn1rtbZIFUd.png\" width=\"157\" height=\"237\"></p>",
    "qingxi": "<p><span style=\"font-size: 13px;\">清洗方式:</span><img alt=\"1111.png\" src=\"http://xiche.qiniu.bronet.cn/uploads/20200831/FsGkAQhqnwBodzLvJyn1rtbZIFUd.png\" width=\"157\" height=\"237\"><br></p>",
    "zhuyi": "<p>注意事项:</p><div class=\"simditor-wrapper\"><div class=\"simditor-toolbar\" style=\"width: 492.263px;\"><ul><li></li></ul></div><img alt=\"20200523_161817_867.png\" src=\"http://xiche.qiniu.bronet.cn/uploads/20200831/Fpai9ciBOkSDnU3UrAWJ3J0acogV.png\" width=\"728\" height=\"410\"></div>",
    "xiche": "<p><img alt=\"20200523_161817_867.png\" src=\"http://xiche.qiniu.bronet.cn/uploads/20200831/Fpai9ciBOkSDnU3UrAWJ3J0acogV.png\" width=\"728\" height=\"410\"><br></p><p>洗车详情:</p><div class=\"simditor-wrapper\"><div class=\"simditor-toolbar\" style=\"width: 492.263px;\"><ul><li></li></ul></div></div>"
    }
    })
     */
    public function CarConWri()
    {
        $arr = Db::name('vip_con')->order('id desc')->limit(1)->find();
        $data = [
            'fuwu' => $arr['fuwu_content'],
            'qingxi' => $arr['qingxi_content'],
            'zhuyi' => $arr['zhuyi_content'],
            'xiche' => $arr['xiche_content'],
        ];
        $this->success('成功', $data);
    }


    /**
     * 首页接口
     * @ApiTitle    (首页接口-首页爱车养护列表)
     * @ApiSummary  (首页爱车养护列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/IndexLoveCarList)
     * @ApiParams   (name="lat", type="int", required=true, description="纬度")
     * @ApiParams   (name="lng", type="int", required=true, description="经度")
     * @ApiParams   (name="pages", type="int", required=true, description="pages")
     * @ApiParams   (name="rows", type="int", required=true, description="rows")
     * @ApiParams   (name="keywords", type="string", required=true, description="搜索关键字")
     * @ApiParams   (name="city", type="string", required=true, description="区域关键字")
     * @ApiParams   (name="type", type="int", required=true, description="1=首页,2=爱车养护")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    "count": 2,
    "list": [
    {
    "id": 店铺ID,
    "title": "店铺名",
    "address": "店铺地址",
    "avatar": "店铺头图",
    "order": 订单数,
    "from": "距离",
    "talk_num": 评论数
    }
    ]
    }
    })
     */
    public function IndexLoveCarList()
    {
        $param = $this->request->param();
        $map = [];
        $map2 = [];
        $map3 = [];
        if ($param['city'] != '全市') {
            $map['address'] = ['LIKE', '%' . $param['city'] . '%'];
        }
        if (!empty($param['keywords']) || $param['keywords'] != null || $param['keywords'] != '' || $param['keywords'] != "") {
            $map2['title'] = ['LIKE', '%' . $param['keywords'] . '%'];
        }
        if ($param['type'] == 1) {
            $map3['is_show'] = ['eq', 1];
        }
        $rult = Db::name('seller')->where($map3)->where($map)->where($map2)->order('id desc')->page($param['pages'], $param['rows'])->select();
        $count = Db::name('seller')->where($map3)->where($map)->where($map2)->order('id desc')->select();
        if (empty($rult)) {
            //如果为空
            $list = [
                'count' => 0,
                'list' => []
            ];
        } else {
            //计算距离 KM
            foreach ($rult as $k => $v) {
                $ruestul[$k]['id'] = $v['id'];
                $ruestul[$k]['title'] = $v['title'];
                $ruestul[$k]['address'] = $v['address'];
                $ruestul[$k]['avatar'] = cdnurl($v['avatar']);
                $ruestul[$k]['order'] = $v['order'];
                if (empty($param['lng']) || $param['lng'] == null || $param['lng'] == '' || $param['lng'] == "") {
                    $ruestul[$k]['from'] = '暂无距离信息';
                } else {
                    $ruestul[$k]['from'] = $this->getDistance($param['lng'], $param['lat'], $v['lng'], $v['lat']) . 'km';
                }
                $ruestul[$k]['talk_num'] = $v['talk_num'];
            }
            $newarr = array_column($ruestul, 'from');
            array_multisort($newarr, SORT_ASC, $ruestul);
            $list = [
                'count' => count($count),
                'list' => $ruestul
            ];
        }
        $this->success('成功', $list);
    }


    /**
     * 首页接口
     * @ApiTitle    (首页接口-店铺详情)
     * @ApiSummary  (店铺详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/SellerCon)
     * @ApiParams   (name="id", type="int", required=true, description="店铺ID")
     * @ApiParams   (name="pages", type="int", required=true, description="pages")
     * @ApiParams   (name="rows", type="int", 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": {
    "id": 3,
    "avatar": "http://xiche.qiniu.bronet.cn/uploads/20200831/FsGkAQhqnwBodzLvJyn1rtbZIFUd.png",
    "title": "这是第三个店铺",
    "opentime": "8:00-9:00",
    "address": "北京市北京市海淀区吴玉章路",
    "mobile": "1391929399",
    "content": "<p>这是第三个商家的详情</p>",
    "num": 3, //平均评分
    "talkList": {
    "count": 1,//评论总数
    "talk": [
    {
    "id": 1, //评论id
    "num": 3, //评分
    "createtime": "1970-05-09", //评论时间
    "avatar": "https://thirdwx.qlogo.cn/mmopen/vi_32/0wpUDw5m0LRvdDueia3uMWmrYb53GXRaTnYwMWC18kRmX6P6PIaPDgQqarEIfwroicx1z51D6bjQicOV0PibBx2PqQ/132",
    "nickname": "Gody",
    "images": [ //评论图
    "http://xiche.qiniu.bronet.cn/uploads/20200831/FsGkAQhqnwBodzLvJyn1rtbZIFUd.png",
    "http://xiche.qiniu.bronet.cn/uploads/20200831/FsGkAQhqnwBodzLvJyn1rtbZIFUd.png"
    ]
    }
    ]
    }
    }
    })
     */
    public function SellerCon()
    {
        $param = $this->request->param();
        $seller_arr = Db::name('seller')->where('id', $param['id'])->find();
        if (empty($seller_arr)) {
            $this->error('参数错误', 0);
            die;
        }
        $list = [
            'id' => $seller_arr['id'],
            'avatar' => cdnurl($seller_arr['avatar']),
            'title' => $seller_arr['title'],
            'opentime' => $seller_arr['opentime'],
            'address' => $seller_arr['address'],
            'lng' => $seller_arr['lng'],
            'lat' => $seller_arr['lat'],
            'mobile' => $seller_arr['mobile'],
            'content' => $seller_arr['content']
        ];
        $talk_arr = Db::name('seller_talk')
            ->alias('s')
            ->join('user u', 'u.id=s.user_id')
            ->where('seller_id', $param['id'])
            ->order('id desc')
            ->page($param['pages'], $param['rows'])
            ->field('s.id,s.num,s.title,s.images,s.createtime,u.avatar,u.nickname')
            ->select();
        $count = Db::name('seller_talk')
            ->alias('s')
            ->join('user u', 'u.id=s.user_id')
            ->where('seller_id', $param['id'])
            ->order('id desc')
            ->field('s.id,s.num,s.title,s.images,s.createtime,u.avatar,u.nickname')
            ->select();
        if (empty($talk_arr)) {
            $list['num'] = 0;
            $list['talkList'] = [
                'count' => 0,
                'talk' => []
            ];
        } else {
            $sum = array_sum(array_column($talk_arr, 'num'));
            $list['num'] = $sum / count($count);
            foreach ($talk_arr as $k => $v) {
                $talkList[$k]['id'] = $v['id'];
                $talkList[$k]['num'] = $v['num'];
                $talkList[$k]['createtime'] = date('Y-m-d', $v['createtime']);
                $talkList[$k]['avatar'] = $v['avatar'];
                $talkList[$k]['nickname'] = $v['nickname'];
                $talkList[$k]['title'] = $v['title'];
                if (empty($v['images'])) {
                    $talkList[$k]['images'] = [];
                } else {
                    if (strstr($v['images'], ',')) {
                        $image = explode(',', $v['images']);
                    } else {
                        $image = explode(' ', $v['images']);
                    }
                    foreach ($image as $k1 => $v1) {
                        $image[$k1] = cdnurl($v1);
                    }
                    $talkList[$k]['images'] = $image;
                }
            }
            $list['talkList'] = [
                'count' => count($count),
                'talk' => $talkList
            ];
        }
        $this->success('成功', $list);
    }


    /**
     * 首页接口
     * @ApiTitle    (首页接口-添加评论)
     * @ApiSummary  (添加评论)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/TalkSave)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="seller_id", type="int", required=true, description="商家ID")
     * @ApiParams   (name="title", type="int", required=true, description="评论内容")
     * @ApiParams   (name="images", type="int", required=true, description="评论图")
     * @ApiParams   (name="num", type="int", required=true, description="评分")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    })
     */
    public function TalkSave()
    {
        $param = $this->request->param();
        $user_id = $this->is_token($this->request->header());
        $data = [
            'seller_id' => $param['seller_id'],
            'user_id' => $user_id,
            'title' => $param['title'],
            'num' => $param['num'],
            'images' => $param['images'],
            'createtime' => time(),
        ];
        $res = Db::name('seller_talk')->insert($data);
        $this->res($res);
    }
}