<?php
namespace App\Domains\Real\Services;

use App\Dicts;
use App\Domains\Real\Repositories\CompanyRepository;
use App\Domains\Real\Repositories\PackageRepository;

class CommonService
{
    protected static $companies;
    protected static $packages;

    /**
     * 格式化支付频道
     *
     * @param int $int
     * @return string
     */
    public static function transformerPayChannel($payChannel)
    {
        switch ($payChannel) {
            case 'tmxx':
                $payChannel = 'tmall';
                break;
            case 'yhzz':
                $payChannel = 'bank';
                break;
            default:
                break;
        }

        return $payChannel;
    }

    /**
     * 获取支付方式中文
     *
     * @param string $payChannel
     * @return string
     */
    public static function namePayChannel($payChannel)
    {
        $payChannels = app(Dicts::class)->get('pay_channel');

        foreach ($payChannels as $key => $value) {
            if (in_array($payChannel, $value)) {
                return $key;
            }
        }

        return '';
    }

    public static function company($id)
    {
        if (!self::$companies) {
            self::$companies = app(CompanyRepository::class)->select(['id', 'name'])->get()->keyBy('id');
        }

        return self::$companies[$id];
    }

    public static function package($id)
    {
        if (!self::$packages) {
            self::$packages = app(PackageRepository::class)->select(['id', 'sn', 'name', 'carrier_operator'])->get()->keyBy('id');
        }

        return self::$packages[$id];
    }
}
