<?php

namespace app\api\controller;

use app\api\BaseController;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;
use think\Request;
use app\api\model\AreaManager as AreaManagerModel; // 区域负责人信息模型
use app\api\model\ProductOrderLog as ProductOrderLogModel; // 订单快照模型
use app\api\model\StoreProduct as StoreProductModel; // 商品模型
use app\api\model\AgricultureProducts as AgricultureProductsModel; // 农产品结构模型
use app\api\model\Area as AreaModel; // 市场区域模型
/**
 * 时间:2023年02月16日
 * 作者:墨楠小
 * 邮箱:monanxiao@qq.com
 * 可视化数据接口文件
 * 
 */
class Visual extends BaseController
{

    /**
     * @var \think\Request Request实例
     */
    protected $request;
    
    /**
     * 构造方法
     * @param Request $request Request对象
     * @access public
     */
    public function __construct(Request $request)
    {
		$this->request = $request;
        // 记录请求api路径
        // Log::recod($this->request->url());
    }

    /**
     * 测试文件
     * 
     */
    public function test()
    {   
    
    }   
   
    /**
     * 
     * 促销产品
     * 
     */
    public function discountsGoods()
    {
        $num = 10;
        $areaId = null;

        // 是否获取区域信息
        if($this->request->param('areaId'))
        {
           $query->where('area_id', $areaId);
        }
        
        // 获取商品数据
        $product = StoreProductModel::where('is_show', 1)
            ->where('is_benefit', 1)
            ->field('product_id, store_name, ot_price, price, cost')
            ->select();

        $this->apiSuccess('获取成功', $product);
    }

    /**
     * 
     * 交易趋势
     * 
     */
    public function trendtrading()
    {
        $num = 10;
        $areaId = null;

        // 是否获取区域信息
        if($this->request->param('areaId'))
        {
           $query->where('area_id', $areaId);
        }

        // 获取商品订单日志->订单快照
        $productOrderLog = ProductOrderLogModel::where('status', 1)
            ->fieldRaw("SUM(total_price) as month_price, month(create_time) as months")
            ->group('months')
            ->select();

        $this->apiSuccess('获取成功', $productOrderLog);
    }

    /**
     * 
     * 热销农副产品
     * 
     */
    public function sellLikeGoods()
    {
        $num = 10;
        $areaId = null;

        // 是否获取区域信息
        if($this->request->param('areaId'))
        {
           $query->where('area_id', $areaId);
        }

        // 获取商品数据
        $product = StoreProductModel::where('is_show', 1)
            ->where('is_hot', 1)
            ->field('product_id, store_name, ot_price, price, cost')
            ->select();

        $this->apiSuccess('获取成功', $product);

    }

    /**
     * 
     * 区域负责人信息获取控制器
     * num 获取数量 默认为10条
     * areaId 区域ID 默认为null 获取全部
     * AreaManagerModel 隐形路由绑定
     * 
     */
    public function arealist(AreaManagerModel $query)
    {   
        $num = 10;
        $areaId = null;

        // 是否获取区域负责人信息
        if($this->request->param('areaId'))
        {
           $query->where('area_id', $areaId);
        }

        // 判断是否获取数量
        if($this->request->param('number') != 10)
        {
            $query->limit($num);
        }

        // 农贸市场开放时间
        $marketOpenTime = [
            'start_time' => '7:00',
            'end_time' => '19:00'
        ];

        $result = [
            'dataList' => $query->select(),
            'maktetOpenTime' => $marketOpenTime
        ];

        $this->apiSuccess('获取成功', $result);
    }

    /**
     * 
     * 实时直播
     * 
     */
    public function realTimeLive()
    {
        $num = 10;
        $areaId = null;

        // 是否获取区域信息
        if($this->request->param('areaId'))
        {
           $query->where('area_id', $areaId);
        }

        // 推流地址
        $live = [
            'rtmp' => 'rtmp://pull.aliyunlive.com/live/0000?auth_key={鉴权串}',
            'flv' => 'http://pull.aliyunlive.com/live/0000.flv?auth_key={鉴权串}', 
            'm3u8' => 'http://pull.aliyunlive.com/live/0000.m3u8?auth_key={鉴权串}',
            'rts' => 'artc://pull.aliyunlive.com/live/0000?auth_key={鉴权串}',
            'mp4' => 'https://nk.lihaink.cn/video/test.mp4'
        ];

        $this->apiSuccess('获取成功', $live);
    }

    /**
     * 
     * 市场人流量
     * 
     */
    public function bazaarPeopleFlow()
    {   

        $num = 10;
        $areaId = null;

        // 是否获取区域信息
        if($this->request->param('areaId'))
        {
           $query->where('area_id', $areaId);
        }

        $number = rand(1000000, 9999999); // 当前人流量
        $yesterDayNumber = rand(3000000, 9999999); // 昨日人流量

        // 人浏览预留接口
        $visitorsFlow = [
            'number' => $number, // 浏览人流量
            'yesterDayNumber' => $yesterDayNumber, // 昨日浏览人流量
        ];

        $this->apiSuccess('获取成功', $visitorsFlow);
    }
    
    /**
     * 
     * 实时监控
     * 
     */
    public function realTimeMonitoring()
    {

        $num = 10;
        $areaId = null;

        // 是否获取区域信息
        if($this->request->param('areaId'))
        {
           $query->where('area_id', $areaId);
        }

        // 推流地址
        $live = [
            [
                'rtmp' => 'rtmp://pull.aliyunlive.com/live/0000?auth_key={鉴权串}',
                'flv' => 'http://pull.aliyunlive.com/live/0000.flv?auth_key={鉴权串}', 
                'm3u8' => 'http://pull.aliyunlive.com/live/0000.m3u8?auth_key={鉴权串}',
                'rts' => 'artc://pull.aliyunlive.com/live/0000?auth_key={鉴权串}',
                'mp4' => 'https://nk.lihaink.cn/video/test.mp4'
            ],
            [
                'rtmp' => 'rtmp://pull.aliyunlive.com/live/0000?auth_key={鉴权串}',
                'flv' => 'http://pull.aliyunlive.com/live/0000.flv?auth_key={鉴权串}', 
                'm3u8' => 'http://pull.aliyunlive.com/live/0000.m3u8?auth_key={鉴权串}',
                'rts' => 'artc://pull.aliyunlive.com/live/0000?auth_key={鉴权串}',
                'mp4' => 'https://nk.lihaink.cn/video/test.mp4'
            ],
            [
                'rtmp' => 'rtmp://pull.aliyunlive.com/live/0000?auth_key={鉴权串}',
                'flv' => 'http://pull.aliyunlive.com/live/0000.flv?auth_key={鉴权串}', 
                'm3u8' => 'http://pull.aliyunlive.com/live/0000.m3u8?auth_key={鉴权串}',
                'rts' => 'artc://pull.aliyunlive.com/live/0000?auth_key={鉴权串}',
                'mp4' => 'https://nk.lihaink.cn/video/test.mp4'
            ],
            [
                'rtmp' => 'rtmp://pull.aliyunlive.com/live/0000?auth_key={鉴权串}',
                'flv' => 'http://pull.aliyunlive.com/live/0000.flv?auth_key={鉴权串}', 
                'm3u8' => 'http://pull.aliyunlive.com/live/0000.m3u8?auth_key={鉴权串}',
                'rts' => 'artc://pull.aliyunlive.com/live/0000?auth_key={鉴权串}',
                'mp4' => 'https://nk.lihaink.cn/video/test.mp4'
            ],
            [
                'rtmp' => 'rtmp://pull.aliyunlive.com/live/0000?auth_key={鉴权串}',
                'flv' => 'http://pull.aliyunlive.com/live/0000.flv?auth_key={鉴权串}', 
                'm3u8' => 'http://pull.aliyunlive.com/live/0000.m3u8?auth_key={鉴权串}',
                'rts' => 'artc://pull.aliyunlive.com/live/0000?auth_key={鉴权串}',
                'mp4' => 'https://nk.lihaink.cn/video/test.mp4'
            ],
        ];

        $this->apiSuccess('获取成功', $live);

    }

    /**
     * 
     * 镇农副业特色产品
     * 
     */
    public function featureGoods()
    {

        $num = 10;
        $areaId = null;

        // 是否获取区域信息
        if($this->request->param('areaId'))
        {
           $query->where('area_id', $areaId);
        }

        // 农产品结构
        $agricultureProducts = AgricultureProductsModel::where('status', 1)
            ->field('title, yield, cycle')
            ->select();
        
        $this->apiSuccess('获取成功', $agricultureProducts);

    }


    /**
     * 
     * 产量分析
     * 
     */
    public function yieldAnalysis($area_id=0,$street_id=0,$village_id=0)
    {
        $where = [];

        if ($area_id){
            $where[] = ['area_id', '=', $area_id];
        }

        if ($street_id){
            $where[] =  ['street_id', '=', $street_id];
        }

        if ($village_id){
            $where[] =  ['village_id', '=', $village_id];
        }

        $where[] =  ['mer_type_id', '=', 7];

        $m=Db::connect('shop')->table('eb_product_order_log')
            ->where($where)
            ->count();

        $max=Db::connect('shop')->table('eb_product_order_log')
            ->where('mer_type_id',7)
            ->count();

        if ($m!=0 && $max!=0){

            $m = bcmul(bcdiv($m,$max,3),100,1);
            $max=100-$m;

        }else{

            $m=0;
            $max=0;

        }

        // 覆盖人数
        $number= Db::connect('shop')->table('eb_user')
            ->count();

        $return = [
            ['name' => '种植','value'=>$m],
            ['name' => '种植总销量','value'=>$max],
        ];

        $this->apiSuccess('获取成功', ['number' => $number, 'chanliang' => $return]);
    }

    /**
     * 
     * 电视右下角二维码
     * 
     */
    public function telQrcode()
    {
        $this->apiSuccess('获取成功', ['img' => 'https://nk.lihaink.cn/img/ShopQrcode.jpg']);
    }
}