<?php


namespace App\Http\Services;

use App\Http\Controllers\Filter\SupplierFilter;
use App\Model\RedisModel;
use App\Model\SupplierChannelModel;
use Illuminate\Support\Facades\DB;

class SupplierStatisticsService
{
    //获取供应商列表需要统计的信息
    public function getSupplierListStatistics()
    {
        $redis = new RedisModel();
        $result = json_decode($redis->get('supplier_list_statistics'), true);
        if ($result) {
            return $result;
        }
        $total = $this->getStatisticsCount('all');
        //待复审
        $needReview = $this->getStatisticsCount('need_review');
        //待审核
        $pending = $this->getStatisticsCount('pending');
        //审核中
        $inReview = $this->getStatisticsCount('in_review');
        //通过
        $passed = $this->getStatisticsCount('passed');
        //未通过
        $rejected = $this->getStatisticsCount('rejected');
        //禁用
        $disable = $this->getStatisticsCount('disable');
        //没有渠道开发员
        $noPurchaseUid = $this->getStatisticsCount('no_purchase_uid');
        //没有采购
        $noChannelUid = $this->getStatisticsCount('no_channel_uid');
        //存在无效(离职)采购员
        $invalidChannelUid = $this->getStatisticsCount('invalid_channel_uid');
        //存在无效(离职)开发员
        $invalidPurchaseUid = $this->getStatisticsCount('invalid_purchase_uid');
        //需要跟进的
        $toFollowUp = $this->getStatisticsCount('to_follow_up');
        //没有sku的
        $noSku = $this->getStatisticsCount('no_sku');
        //拉黑的
        $block = $this->getStatisticsCount('block');
        //没有品牌协议附件的
        $noQualityAssuranceAgreement = $this->getStatisticsCount('no_quality_assurance_agreement');
        //有标签的(客户指定供应商)
        $hasTagSupplier = 0;
        //联系人未完善
        $concatNoComplete = $this->getStatisticsCount('contact_no_complete');

        $result = [
            'total' => $total,
            'need_review' => $needReview,
            'pending' => $pending,
            'in_review' => $inReview,
            'passed' => $passed,
            'rejected' => $rejected,
            'disable' => $disable,
            'block' => $block,
            'no_purchase_uid' => $noPurchaseUid,
            'no_channel_uid' => $noChannelUid,
            'invalid_channel_uid' => $invalidChannelUid,
            'invalid_purchase_uid' => $invalidPurchaseUid,
            'to_follow_up' => $toFollowUp,
            'no_sku' => $noSku,
            'no_quality_assurance_agreement' => $noQualityAssuranceAgreement,
            'has_supplier_tag' => $hasTagSupplier,
            'contact_no_complete' => $concatNoComplete,
        ];
        $result = array_map(function ($value) {
            if ($value > 999) {
                $value = '999+';
            }
            return $value;
        }, $result);
        $redis->set('supplier_list_statistics', json_encode($result));
        $redis->expire('supplier_list_statistics', 100);
        return $result;
    }

    private function getStatisticsCount($type)
    {
        $model = new SupplierChannelModel();
        //显示默认的数据(有权限逻辑)
        $filter = new SupplierFilter();
        $model = $model->where('supplier_id', '>', 0);
        return $filter->defaultFilter($model, $type)->count();
    }
}