<?php

namespace app\controller\api;

use app\common\model\system\merchant\Merchant;
use app\common\model\system\merchant\MerchantType;
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];
        $where[] = ['type_id', '=', $parmas['mer_type_id']];
        $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:格式错误');
        }
        $merchant = Merchant::where('mer_intention_id', $parmas['mer_intention_id'])->find();
        $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', '=', $merchant['mer_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'] . '%'];
        }
        $count = Db::name('store_product')->where($where)->count();
        $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, 'count' => $count]);
    }

    /**
     * 采购金额和销售金额统计
     */
    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', '=', 0];
                    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:格式错误');
        }
        $mer_id = Db::name('merchant_intention')->where('mer_intention_id', $parmas['mer_intention_id'])->value('mer_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=' . $mer_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', '=', 0];
                    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]);
    }


    /**
     * 查询镇农科公司负责片区内的种养殖商户和供应链商户交易额
     *暂无种养殖商户分类
     */
    public function SupplyChainBreedingStreetProductCount()
    {
        $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['responsible_area']) || $parmas['responsible_area'] == '') {
            return app('json')->fail('responsible_area:格式错误');
        }
        // if (!isset($parmas['type']) || $parmas['type'] == '') {
        //     return app('json')->fail('type:格式错误');
        // }
        $area[] = ['street_id', 'in', explode(',', $parmas['responsible_area'])];
        $area[] = ['type_id', '=', 17];

        $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.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(['trade_amount' => $count]);
    }

    /**
     * 查询手机号用户的交易金额
     */
    public function StoreOrderUserTradeAmount()
    {
        $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['phone']) || $parmas['phone'] == '') {
            return app('json')->fail('phone:格式错误');
        }
        $account[] = ['account', 'in', explode(',', $parmas['phone'])];
        $user_id = Db::name('user')->where($account)->value('uid');
        if (!$user_id) {
            return app('json')->fail('查询的手机号用户不存在');
        }
        $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[] = ['paid', '=', 1];
        $where[] = ['status', '<>', -1];
        $where[] = ['uid', '=', $user_id];
        $count = Db::name('store_order')
            ->where($where)
            ->sum('pay_price');
        return app('json')->success(['trade_amount' => $count]);
    }

    /**
     * 查询镇农科公司负责片区内的种养殖商户和供应链商户交易额
     *暂无种养殖商户分类
     */
    public function SupplyChainVillageBreedingPriceCount()
    {
        $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['village']) || $parmas['village'] == '') {
            return app('json')->fail('village:格式错误');
        }
        $village_id = Db::name('geo_village')->where('village_code', $parmas['village'])->value('village_id');
        $merchant_category_id = Db::name('merchant_category')->where('code', 'zhongyangzhi')->value('merchant_category_id');
        $area[] = ['village_id', '=', $village_id];
        $area[] = ['category_id', '=', $merchant_category_id];

        $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.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(['trade_amount' => $count]);
    }

    // 镇级下的镇级供应链商户
    public function SupplyChainMerchant()
    {
        $parmas = $this->request->param();

        $where[] = ['street_id', '=', $parmas['street_code']];
        $where[] = ['is_del', '=', 0];
        $where[] = ['status', '=', 1];
        // 镇级供应链 type_code = TypeTownSupplyChain
        $merchantType = MerchantType::where('type_code', 'TypeTownSupplyChain')->find()->toArray();

        $where[] = ['type_id', '=', $merchantType['mer_type_id']];

        $list = Db::name('merchant')->where($where)->select();

        return app('json')->success(compact('list'));
    }

    public function GeneralMerchant()
    {
        $parmas = $this->request->param();

        $where[] = ['street_id', '=', $parmas['street_code']];
        $where[] = ['is_del', '=', 0];
        $where[] = ['status', '=', 1];
        // 镇级普通商户 type_code = TypeStore
        $merchantType = MerchantType::where('type_code', 'TypeStore')->find();

        $where[] = ['type_id', '=', $merchantType['mer_type_id']];

        $list = Db::name('merchant')->where($where)->select();

        return app('json')->success(compact('list'));
    }



    /**
     * 镇级供应链商户商品数量查询
     */
    public function SupplyChainProduct()
    {
        $parmas = $this->request->param();

        $merchant = Merchant::where('mer_intention_id', $parmas['mer_intention_id'])->find();
        if (empty($merchant) || $merchant['type_id'] != 17) {
            return app('json')->success();
        }
        $merchantName = $merchant['mer_name'];
        $where[] = ['mer_id', '=', $merchant['mer_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(compact('count', 'merchantName'));
    }

    public function GeneralMerchantProduct()
    {
        $parmas = $this->request->param();

        $merchant = Merchant::where('mer_intention_id', $parmas['mer_intention_id'])->find();
        if (empty($merchant) || $merchant['type_id'] != 10) {
            return app('json')->success();
        }
        $merchantName = $merchant['mer_name'];
        $where[] = ['mer_id', '=', $merchant['mer_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(compact('count', 'merchantName'));
    }

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

        $merchant = Merchant::where('mer_intention_id', $parmas['mer_intention_id'])->find();
        if (empty($merchant) || $merchant['type_id'] != 17) {
            return app('json')->success();
        }
        $where[] = ['mer_id', '=', $merchant['mer_id']];
        $count = Db::name('store_product_stock')->where($where)->count();
        return app('json')->success(['count' => $count]);
    }

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

        $merchant = Merchant::where('mer_intention_id', $parmas['mer_intention_id'])->find();
        if (empty($merchant) || $merchant['type_id'] != 10) {
            return app('json')->success();
        }
        $where[] = ['mer_id', '=', $merchant['mer_id']];
        $count = Db::name('store_product_stock')->where($where)->count();
        return app('json')->success(['count' => $count]);
    }

    /**
     * 镇级供应链采购金额和销售金额
     */
    public function SupplyChainProductPrice()
    {
        $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', '=', 0];
                    break;
            }
        } else {
            $where[] = ['p.source', '=', 0];
        }
        $merchant = Merchant::where('mer_intention_id', $parmas['mer_intention_id'])->find();
        if (empty($merchant) || $merchant['type_id'] != 17) {
            return app('json')->success();
        }
        $mer_id = $merchant['mer_id'];
        $where[] = ['p.is_refund', '=', 0];
        $count = Db::name('store_order_product')->alias('p')
            ->where($where)
            ->join('store_order o', 'o.mer_id=' . $mer_id . ' and o.paid=1 and o.is_del=0')
            ->sum('p.total_price');
        $merName = $merchant['mer_name'];
        return app('json')->success(compact('merName', 'count'));
    }

    /**
     * 一般商户采购金额和销售金额
     */
    public function GeneralProductPrice()
    {
        $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', '=', 0];
                    break;
            }
        } else {
            $where[] = ['p.source', '=', 0];
        }
        $merchant = Merchant::where('mer_intention_id', $parmas['mer_intention_id'])->find();
        if (empty($merchant) || $merchant['type_id'] != 10) {
            return app('json')->success();
        }
        $mer_id = $merchant['mer_id'];
        $where[] = ['p.is_refund', '=', 0];
        $count = Db::name('store_order_product')->alias('p')
            ->where($where)
            ->join('store_order o', 'o.mer_id=' . $mer_id . ' and o.paid=1 and o.is_del=0')
            ->sum('p.total_price');
        $merName = $merchant['mer_name'];
        return app('json')->success(compact('merName', 'count'));
    }

    /**
     *  新供销 查询邀请的 用户注册数 商户数 交易金额
     */
    public function InviteUserStatistics()
    {
        $parmas = $this->request->param();
        $promotionCode =  $parmas['promotion_code'];
        $datas = [];
        // 用户注册数
        $datas['user_count'] = Db::name('user')->where('promotion_code', $promotionCode)->count();

        $userIds = Db::name('user')->where('promotion_code', $promotionCode)->column('uid');

        // 商户数
        $datas['merchant_count'] = Merchant::whereIn('uid', $userIds)->count();

        // 交易金额
        $datas['trade_amount'] = Db::name('store_order')->whereIn('uid', $userIds)->where('paid', 1)->sum('pay_price');

        return app('json')->success($datas);
    }
}