<?php

namespace App\Providers;

use App\Customer;
use App\Http\Controllers\Controller;
use App\Services\AuthorityService;
use App\Services\CacheService;
use App\Services\CommodityService;
use App\Services\common\BatchUpdateService;
use App\Services\CommodityBarcodeService;
use App\Services\common\DataHandlerService;
use App\Services\CustomerService;
use App\Services\DepositoryService;
use App\Services\FeatureService;
use App\Services\InventoryAccountMissionService;
use App\Services\InventoryCompareService;
use App\Services\LaborReportsCountingRecordService;
use App\Services\LogService;
use App\Services\OracleBasCustomerService;
use App\Services\OracleBasSkuService;
use App\Services\OracleDocAsnDetailService;
use App\Services\OracleDOCOrderHeaderService;
use App\Services\OrderCountingRecordService;
use App\Services\OracleDocAsnHerderService;
use App\Services\OrderIssuePerformanceService;
use App\Services\AllInventoryService;
use App\Services\InventoryDailyLogService;
use App\Services\LogisticService;
use App\Services\OracleActAllocationDetailService;
use App\Services\OrderIssueProcessLogService;
use App\Services\OrderIssueService;
use App\Services\OrderPackageService;
use App\Services\OrderService;
use App\Services\OrderIssueWorkLoadService;
use App\Services\OrderPackageCommoditiesService;
use App\Services\OrderTrackingService;
use App\Services\OwnerAreaReportService;
use App\Services\OwnerBillReportService;
use App\Services\OwnerFeeDetailService;
use App\Services\OwnerOutStorageRuleService;
use App\Services\OwnerPriceDirectLogisticService;
use App\Services\OwnerPriceExpressService;
use App\Services\OwnerPriceLogisticService;
use App\Services\OwnerPriceOperationService;
use App\Services\OwnerReportService;
use App\Services\OwnerService;
use App\Services\OwnerStoragePriceModelService;
use App\Services\PackageService;
use App\Services\PackageStatisticsService;
use App\Services\ProcessesContentService;
use App\Services\ProcessMethodService;
use App\Services\ProcessService;
use App\Services\ProcessStatisticService;
use App\Services\RealtimePendingOrdersService;
use App\Services\RejectedBillItemService;
use App\Services\RejectedBillService;
use App\Services\ShopService;
use App\Services\RejectedService;
use App\Services\StoreCheckingReceiveItemService;
use App\Services\StoreCheckingReceiveService;
use App\Services\StoreItemService;
use App\Services\StoreService;
use App\Services\CheckActiveMenuService;
use App\Services\UnitService;
use App\Services\UserOwnerGroupService;
use App\Services\UserService;
use App\Services\WarehouseService;
use App\Services\WaybillFinancialService;
use App\Services\WeighExceptedService;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Ramsey\Uuid\Uuid;
use Validator;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
        app()->singleton('OrderIssuePerformanceService',OrderIssuePerformanceService::class);
        app()->singleton('InventoryCompareService',InventoryCompareService::class);
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->loadingService();
        //
        Schema::defaultStringLength(191);
        Queue::failing(function (JobFailed $event) {
            (new Controller())->log(__METHOD__,'EventError_',json_encode($event));
        });
        //扩展身份证验证规则
        Validator::extend('identity_cards', function($attribute, $value, $parameters) {
            return preg_match('/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/', $value);
        });
        View::share('pageUuid',Uuid::uuid4());

        \Illuminate\Database\Query\Builder::macro('sql', function () {
            $bindings = $this->getBindings();
            $sql = str_replace('?',"'%s'",$this->toSql());
            return vsprintf($sql, $bindings);
        });

        \Illuminate\Database\Eloquent\Builder::macro('sql', function(){
            return ($this->getQuery()->sql());
        });
    }

    private function loadingService(){
        app()->singleton('CacheService',CacheService::class);
        app()->singleton('UserService',UserService::class);
        app()->singleton('AuthorityService',AuthorityService::class);
        app()->singleton('WaybillFinancialService',WaybillFinancialService::class);
        app()->singleton('WeighExceptedService',WeighExceptedService::class);
        app()->singleton('AllInventoryService',AllInventoryService::class);
        app()->singleton('InventoryDailyLogService',InventoryDailyLogService::class);
        app()->singleton('LogService',LogService::class);
        app()->singleton('ProcessService',ProcessService::class);
        app()->singleton('ProcessStatisticService',ProcessStatisticService::class);
        app()->singleton('OwnerService',OwnerService::class);
        app()->singleton('ProcessMethodService',ProcessMethodService::class);
        app()->singleton('LogisticService',LogisticService::class);
        app()->singleton('PackageStatisticsService',PackageStatisticsService::class);
        app()->singleton('OracleActAllocationDetailService',OracleActAllocationDetailService::class);
        app()->singleton('ShopService',ShopService::class);
        app()->singleton('OrderPackageService',OrderPackageService::class);
        app()->singleton('OrderIssueService',OrderIssueService::class);
        app()->singleton('OrderService',OrderService::class);
        app()->singleton('OracleDocOrderHeaderService',OracleDOCOrderHeaderService::class);
        app()->singleton('OwnerReportService',OwnerReportService::class);
        app()->singleton('RejectedBillService',RejectedBillService::class);
        app()->singleton('RejectedBillItemService',RejectedBillItemService::class);
        app()->singleton('StoreCheckingReceiveService',StoreCheckingReceiveService::class);
        app()->singleton('StoreCheckingReceiveItemService',StoreCheckingReceiveItemService::class);
        app()->singleton('CommodityBarcodeService',CommodityBarcodeService::class);
        app()->singleton('OracleBasCustomerService',OracleBasCustomerService::class);
        app()->singleton('OracleBasSkuService',OracleBasSkuService::class);
        app()->singleton('OracleDocAsnDetailService',OracleDocAsnDetailService::class);
        app()->singleton('InventoryAccountMissionService',InventoryAccountMissionService::class);
        app()->singleton('ProcessesContentService',ProcessesContentService::class);
        app()->singleton('StoreService',StoreService::class);
        app()->singleton('WarehouseService',WarehouseService::class);
        app()->singleton('StoreItemService',StoreItemService::class);
        app()->singleton('PackageService',PackageService::class);
        app()->singleton('ProcessMethodService',ProcessMethodService::class);
        app()->singleton('OwnerReportService',OwnerReportService::class);
        app()->singleton('OwnerAreaReportService',OwnerAreaReportService::class);
        app()->singleton('OwnerFeeDetailService',OwnerFeeDetailService::class);
        app()->singleton('OwnerBillReportService',OwnerBillReportService::class);
        app()->singleton('OwnerPriceExpressService',OwnerPriceExpressService::class);
        app()->singleton('OwnerPriceLogisticService',OwnerPriceLogisticService::class);
        app()->singleton('OwnerPriceDirectLogisticService',OwnerPriceDirectLogisticService::class);

        $this->loadingOrderModuleService();
        $this->loadingBasedModuleService();
        $this->loadingRejectedModuleService();
        $this->loadingCheckActiveMenuService();
        $this->loadingRealtimePendingOrdersService();
        $this->loadingOrderCountingRecordService();
        $this->loadingLaborReportsCountingRecordService();
    }

    private function loadingOrderModuleService(){
        app()->singleton('OrderTrackingService',OrderTrackingService::class);
        app()->singleton('OrderService',OrderService::class);
        app()->singleton('OrderPackageService',OrderPackageService::class);
        app()->singleton('OrderPackageCommoditiesService',OrderPackageCommoditiesService::class);
        app()->singleton('OrderIssueService',OrderIssueService::class);
        app()->singleton('OrderIssueWorkLoadService',OrderIssueWorkLoadService::class);
        app()->singleton('OrderIssuePerformanceService',OrderIssuePerformanceService::class);
        app()->singleton('OracleDocOrderHeaderService',OracleDOCOrderHeaderService::class);
        app()->singleton('OracleActAllocationDetailService',OracleActAllocationDetailService::class);
        app()->singleton('OrderIssueProcessLogService',OrderIssueProcessLogService::class);
    }

    private function loadingBasedModuleService(){
        app()->singleton("ShopService", ShopService::class);
        app()->singleton('CommodityService', CommodityService::class);
        app()->singleton('BatchUpdateService', BatchUpdateService::class);
        app()->singleton('DataHandlerService',DataHandlerService::class);
        app()->singleton('DepositoryService',DepositoryService::class);
        app()->singleton('UserOwnerGroupService',UserOwnerGroupService::class);
        app()->singleton('CustomerService',CustomerService::class);
        app()->singleton('OwnerStoragePriceModelService',OwnerStoragePriceModelService::class);
        app()->singleton('UnitService',UnitService::class);
        app()->singleton('OwnerPriceOperationService',OwnerPriceOperationService::class);
        app()->singleton('FeatureService',FeatureService::class);
        app()->singleton('OwnerOutStorageRuleService',OwnerOutStorageRuleService::class);
    }

    private function loadingRejectedModuleService(){
        app()->singleton('RejectedService',RejectedService::class);
    }

    private function loadingCheckActiveMenuService()
    {
        app()->singleton('CheckActiveMenuService',CheckActiveMenuService::class);
    }

    private function loadingRealtimePendingOrdersService()
    {
        app()->singleton('RealtimePendingOrdersService',RealtimePendingOrdersService::class);
    }
    private function loadingOrderCountingRecordService()
    {
        app()->singleton('OrderCountingRecordService',OrderCountingRecordService::class);
    }
    private function loadingLaborReportsCountingRecordService()
    {
        app()->singleton('LaborReportsCountingRecordService',LaborReportsCountingRecordService::class);
    }
}
