<?php

namespace app\controller\api;

use crmeb\basic\BaseController;
use think\facade\Db;

class Statistics extends BaseController
{
    /**
     * 商户入驻统计
     */
    public function SupplyChainMerCount()
    {
        $parmas = $this->request->param();
        if (isset($parmas['type']) && $parmas['type'] != '') {
            switch ($parmas['type']) {
                case 'area':
                    $type = 'area_id';
                    break;
                case 'street':
                    $type = 'street_id';
                    break;
                default:
                    return app('json')->fail('type:格式错误');
                    break;
            }
        } else {
            $type = 'street_id';
        }
        if (!isset($parmas['start_time']) || $parmas['start_time'] == '') {
            return app('json')->fail('start_time:格式错误');
        }
        if (!isset($parmas['end_time']) || $parmas['end_time'] == '') {
            return app('json')->fail('end_time:格式错误');
        }
        $where[]=['create_time','between time',[date("Y-m-d H:i:s",$parmas['start_time']),date("Y-m-d H:i:s",$parmas['end_time'])]];
        if (isset($parmas['responsible_area']) && $parmas['responsible_area'] != '') {
            $where[] = [$type,'in',explode(',', $parmas['responsible_area'])];
        }
        $where[]=['is_del','=',0];
        $where[]=['status','=',1];
        $count=Db::name('merchant')->where($where)->count();
        return app('json')->success(['count'=>$count]);
    }

    /**
     * 商户商品数量查询
     */
    public function SupplyChainProductCount()
    {
        $parmas = $this->request->param();

        if (!isset($parmas['start_time']) || $parmas['start_time'] == '') {
            return app('json')->fail('start_time:格式错误');
        }
        if (!isset($parmas['end_time']) || $parmas['end_time'] == '') {
            return app('json')->fail('end_time:格式错误');
        }
        if (!isset($parmas['mer_intention_id']) || $parmas['mer_intention_id'] == '') {
            return app('json')->fail('mer_intention_id:格式错误');
        }
        $where[]=['create_time','between time',[date("Y-m-d H:i:s",$parmas['start_time']),date("Y-m-d H:i:s",$parmas['end_time'])]];
        $where[] = ['mer_id','=',$parmas['mer_intention_id']];
        $where[]=['mer_status','=',1];
        $where[]=['status','=',1];
        $where[]=['is_used','=',1];
        $where[]=['is_show','=',1];
        $count=Db::name('store_product')->where($where)->count();
        return app('json')->success(['count'=>$count]);
    }

    /**
     * 商户商品库存更新查询
     */
    public function SupplyChainProductStockCount()
    {
        $parmas = $this->request->param();

        if (!isset($parmas['start_time']) || $parmas['start_time'] == '') {
            return app('json')->fail('start_time:格式错误');
        }
        if (!isset($parmas['end_time']) || $parmas['end_time'] == '') {
            return app('json')->fail('end_time:格式错误');
        }
        if (!isset($parmas['mer_intention_id']) || $parmas['mer_intention_id'] == '') {
            return app('json')->fail('mer_intention_id:格式错误');
        }
        $where[]=['create_time','between time',[date("Y-m-d H:i:s",$parmas['start_time']),date("Y-m-d H:i:s",$parmas['end_time'])]];
        $where[] = ['mer_id','=',$parmas['mer_intention_id']];
        $count=Db::name('store_product_stock')->where($where)->count();
        return app('json')->success(['count'=>$count]);
    }

    /**
     * 商品列表
     */
    public function ProductList(){
        $parmas = $this->request->param();

        if (!isset($parmas['page']) || $parmas['page'] == '') {
            return app('json')->fail('page:格式错误');
        }
        $where[]=['mer_status','=',1];
        $where[]=['status','=',1];
        $where[]=['is_used','=',1];
        $where[]=['is_show','=',1];
        if (isset($parmas['keyword']) &&$parmas['keyword'] != '') {
            $where[] = ['store_name','like','%'.$parmas['keyword'].'%'];
        }
        $list=Db::name('store_product')->where($where)->page($parmas['page'])
        ->field('product_id,store_name,image,price')
        ->limit(10)->select();
        return app('json')->success(['page'=>$parmas['page'],'data'=>$list]);

    }

    /**
     * 采购金额和销售金额统计
     */
    public function SupplyChainProductPriceCount()
    {
        $parmas = $this->request->param();
        if (isset($parmas['type']) && $parmas['type'] != '') {
            switch ($parmas['type']) {
                case 200:
                    $where[] = ['p.source','=',200];
                    break;
                case 300:
                    $where[] = ['p.source','=',300];
                    break;
                default:
                    return app('json')->fail('type:格式错误');
                    break;
            }
        } else {
            $where[] = ['p.source','=',0];
        }
        if (!isset($parmas['start_time']) || $parmas['start_time'] == '') {
            return app('json')->fail('start_time:格式错误');
        }
        if (!isset($parmas['end_time']) || $parmas['end_time'] == '') {
            return app('json')->fail('end_time:格式错误');
        }
        if (!isset($parmas['mer_intention_id']) || $parmas['mer_intention_id'] == '') {
            return app('json')->fail('mer_intention_id:格式错误');
        }
        if (!isset($parmas['goods_id']) || $parmas['goods_id'] == '') {
            return app('json')->fail('goods_id:格式错误');
        }

        $where[]=['p.create_time','between time',[date("Y-m-d H:i:s",$parmas['start_time']),date("Y-m-d H:i:s",$parmas['end_time'])]];
        $where[] = ['p.product_id','in',explode(',', $parmas['goods_id'])];
        $where[] = ['p.is_refund','=',0];
        $count=Db::name('store_order_product')->alias('p')
        ->where($where)
        ->join('store_order o','o.mer_id='.$parmas['mer_intention_id'].' and o.paid=1 and o.is_del=0')
        ->sum('p.total_price');
        return app('json')->success(['procure_amount'=>$count]);
    }

      /**
     * 镇一般商户采购金额和销售金额统计
     */
    public function SupplyChainStreetProductPriceCount()
    {
        $parmas = $this->request->param();
        if (isset($parmas['type']) && $parmas['type'] != '') {
            switch ($parmas['type']) {
                case 200:
                    $where[] = ['p.source','=',200];
                    break;
                case 300:
                    $where[] = ['p.source','=',300];
                    break;
                default:
                    return app('json')->fail('type:格式错误');
                    break;
            }
        } else {
            $where[] = ['p.source','=',0];
        }
        if (!isset($parmas['start_time']) || $parmas['start_time'] == '') {
            return app('json')->fail('start_time:格式错误');
        }
        if (!isset($parmas['end_time']) || $parmas['end_time'] == '') {
            return app('json')->fail('end_time:格式错误');
        }
        if (!isset($parmas['goods_id']) || $parmas['goods_id'] == '') {
            return app('json')->fail('goods_id:格式错误');
        }
        if (isset($parmas['responsible_area']) && $parmas['responsible_area'] != '') {
            $area[] = ['street_id','in',explode(',', $parmas['responsible_area'])];
        }else{
            return app('json')->fail('responsible_area:格式错误');
        }
        $merchant=Db::name('merchant')->where($area)->column('mer_id');
        if(!$merchant){
            return app('json')->fail('查询商户为空');
        }
        $where[]=['p.create_time','between time',[date("Y-m-d H:i:s",$parmas['start_time']),date("Y-m-d H:i:s",$parmas['end_time'])]];
        $where[] = ['p.product_id','in',explode(',', $parmas['goods_id'])];
        $where[] = ['p.is_refund','=',0];
        $count=Db::name('store_order_product')->alias('p')
        ->where($where)
        ->join('store_order o','o.mer_id in ('.implode(',',$merchant).')  and o.paid=1 and o.is_del=0')
        ->sum('p.total_price');
        return app('json')->success(['procure_amount'=>$count]);
    }

}