<?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()
    {
        $statisticsKey = 'supplier_list_statistics_' . request()->user->userId;
        $redis = new RedisModel();
        $result = json_decode($redis->get($statisticsKey), 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 = $this->getStatisticsCount('has_supplier_tag');
//        //联系人未完善
//        $concatNoComplete = $this->getStatisticsCount('contact_no_complete');
//        //历史检测异常
//        $historyAbnormal = $this->getStatisticsCount('history_abnormal');
        //战略供应商(等级为A)
        $payTypeTerm = $this->getStatisticsCount('pay_type_term');
        //账期供应商
        $levelA = $this->getStatisticsCount('level_a');
        //缺少平台合作协议(线上)
        //缺少品质协议(线上)
        $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,
//            'history_abnormal' => $historyAbnormal,
            'pay_type_term' => $payTypeTerm,
            'level_a' => $levelA,
            //所有采购员离职
            'all_channel_user_resigned' => null,
            //历史平台合作供应商
            'uploaded_sku' => null,
            // 非芯链供应商
            'not_yunxin' => null,
            //已上架(非芯链)供应商
            'not_yunxin_and_has_sku' => null,
            //已下架(非芯链)供应商
            'not_yunxin_and_has_no_sku' => null,
            //芯链供应商
            'yunxin' => null,
            //已上架(芯链)供应商
            'yunxin_and_has_sku' => null,
            //已下架(芯链)供应商
            'yunxin_and_has_no_sku' => null,
            //3天内到期(芯链)供应商
            'yunxin_expired_at_days' => null,
            //3天内到期(非芯链)供应商
            'not_yunxin_expired_at_days' => null,
            //已过期(芯链)供应商
            'yunxin_expired' => null,
            //已过期(非芯链)供应商
            'not_yunxin_expired' => null,
            //缺少平台合作协议(线上)
            'no_cooperation_agreement' => null,
        ];
        $result = array_map(function ($value) {
            if ($value > 999) {
                $value = '999+';
            }
            return $value;
        }, $result);
        $redis->set($statisticsKey, json_encode($result));
        $redis->expire($statisticsKey, 60);
        return $result;
    }

    public function getStatisticsCount($type)
    {
        $model = new SupplierChannelModel();
        //显示默认的数据(有权限逻辑)
        $filter = new SupplierFilter();
        $model = $model->where('supplier_code', '!=', '');
        if ($type=='contact_no_complete') {
            $subQuery = $filter->defaultFilter($model, $type);
            return $subQuery->get()->count();
        }
        return $filter->defaultFilter($model, $type)->count();
    }
}