<?php


namespace App\Http\Services;

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

class SkuStatisticsService
{
    //获取供应商列表需要统计的信息
    public function getSkuListStatistics()
    {
        $total = $this->getStatisticsCount('all');
        //七天内过期的
        $expire = $this->getStatisticsCount('expire');
        //下架的
        $offShelf = $this->getStatisticsCount('off_shelf');

        $result = [
            'all' => $total,
            'expire' => $expire,
            'off_shelf' => $offShelf,
        ];
        $result = array_map(function ($value) {
            if ($value > 999) {
                $value = '999+';
            }
            return $value;
        }, $result);

        return $result;
    }

    private function getStatisticsCount($type)
    {
        $url = env('ES_SKU_URL', '');
        $map['supplier_id'] = 17;
        switch ($type) {
            case "all":
                break;
            case "expire":
                $map['expire_day'] = 7;
                break;
            case "off_shelf":
                $map["goods_status/condition"] = 3;
                break;
        }
        $result = curl($url, $map);
        $result = json_decode($result, true);
        $total = 0;
        if (isset($result['error_code']) && $result['error_code'] == 0) {
            $total = $result['data']['total'];
        }
        return $total;
    }
}