<?php

namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Log;
use URL;

class Httplog
{
    public function handle(Request $request, Closure $next)
    {   
        /****操作地址****/
        $data['url'] = URL::current();
        /****提交信息****/
        $data['form'] = $request->all();

        if(stristr($data['url'],config('route.domain.merchant')) !== false){
            if(isset($data['form']['action'])){
                if(stristr($data['url'],"pollinglist") !== false && $data['form']['action'] == 'open'){
                    $add_data['form']['name'] = $data['form']['username']."-收款账户-开启轮循";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"pollinglist") !== false && $data['form']['action'] == 'close'){
                    $add_data['form']['name'] = $data['form']['username']."-收款账户-关闭轮循";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"pollinglist") !== false && $data['form']['action'] == 'add'){
                    $add_data['form']['name'] = "添加收款账户";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"edit") !== false && $data['form']['action'] == 'edit'){
                    $add_data['form']['name'] = "编辑收款账户";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"diecode") !== false && $data['form']['action'] == 'add'){
                    $add_data['form']['name'] = "添加固码通道";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"diecode") !== false && $data['form']['action'] == 'edit'){
                    $add_data['form']['name'] = "编辑固码通道";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"diecode") !== false && $data['form']['action'] == 'del'){
                    $add_data['form']['name'] = "删除固码通道";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"diecode") !== false && $data['form']['action'] == 'open'){
                    $add_data['form']['name'] = "固码通道开启网关";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"diecode") !== false && $data['form']['action'] == 'close'){
                    $add_data['form']['name'] = "固码通道关闭网关";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"livecode") !== false && $data['form']['action'] == 'add'){
                    $add_data['form']['name'] = "添加活码通道";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"livecode") !== false && $data['form']['action'] == 'edit'){
                    $add_data['form']['name'] = "编辑活码通道";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"livecode") !== false && $data['form']['action'] == 'del'){
                    $add_data['form']['name'] = "删除活码通道";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"livecode") !== false && $data['form']['action'] == 'open'){
                    $add_data['form']['name'] = "活码通道开启网关";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"livecode") !== false && $data['form']['action'] == 'close'){
                    $add_data['form']['name'] = "活码通道关闭网关";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }else if(stristr($data['url'],"order_record") !== false && $data['form']['action'] == 'repair'){
                    $add_data['form']['name'] = "交易补单";
                    $add_data['form']['member_id'] = session()->get('merchant')['Id'];
                }
            }
            if(isset($add_data)){
                $add_data['form']['time'] = time();
                $add_data['form']['ip'] = $request->getClientIp();
                $add_data['type'] = "merchant";
                /****添加至Redis后续定时更新****/
                Redis::rpush('operation_log',json_encode($add_data));
            }
        }else if(stristr($data['url'],config('route.domain.admin')) !== false){
            if(isset($data['form']['action'])){
                if(stristr($data['url'],"merchantlist") !== false && $data['form']['action'] == 'add'){
                    $add_data['form']['name'] = "添加码商";
                }else if(stristr($data['url'],"merchantlist") !== false && $data['form']['action'] == 'edit'){
                    $add_data['form']['name'] = "编辑码商";
                }else if(stristr($data['url'],"merchantlist") !== false && $data['form']['action'] == 'amount'){
                    $add_data['form']['name'] = "码商余额充值";
                }else if(stristr($data['url'],"merchantlist") !== false && $data['form']['action'] == 'frozen_amount'){
                    $add_data['form']['name'] = "释放码商冻结金额";
                }else if(stristr($data['url'],"merchantlist") !== false && $data['form']['action'] == 'del'){
                    $add_data['form']['name'] = "删除码商所有信息";
                }else if(stristr($data['url'],"merchant_payment") !== false && $data['form']['action'] == 'add'){
                    $add_data['form']['name'] = "添加码商APP通道";
                }else if(stristr($data['url'],"merchant_payment") !== false && $data['form']['action'] == 'del'){
                    $add_data['form']['name'] = "删除码商APP通道";
                }else if(stristr($data['url'],"merchant_polling") !== false && $data['form']['action'] == 'add'){
                    $add_data['form']['name'] = "添加码商收款账户";
                }else if(stristr($data['url'],"merchant_polling") !== false && $data['form']['action'] == 'del'){
                    $add_data['form']['name'] = "删除码商收款账户";
                }else if(stristr($data['url'],"merchant_polling") !== false && $data['form']['action'] == 'edit'){
                    $add_data['form']['name'] = "编辑码商收款账户";
                }else if(stristr($data['url'],"merchant_diecode") !== false && $data['form']['action'] == 'add'){
                    $add_data['form']['name'] = "添加码商固码通道";
                }else if(stristr($data['url'],"merchant_diecode") !== false && $data['form']['action'] == 'lose'){
                    $add_data['form']['name'] = "码商固码通道关闭网关";
                }else if(stristr($data['url'],"merchant_diecode") !== false && $data['form']['action'] == 'open'){
                    $add_data['form']['name'] = "码商固码通道开启网关";
                }else if(stristr($data['url'],"merchant_diecode") !== false && $data['form']['action'] == 'del'){
                    $add_data['form']['name'] = "删除码商固码通道";
                }else if(stristr($data['url'],"merchant_diecode") !== false && $data['form']['action'] == 'edit'){
                    $add_data['form']['name'] = "编辑码商固码通道";
                }else if(stristr($data['url'],"merchant_livecode") !== false && $data['form']['action'] == 'add'){
                    $add_data['form']['name'] = "添加码商活码通道";
                }else if(stristr($data['url'],"merchant_livecode") !== false && $data['form']['action'] == 'lose'){
                    $add_data['form']['name'] = "码商活码通道关闭网关";
                }else if(stristr($data['url'],"merchant_livecode") !== false && $data['form']['action'] == 'open'){
                    $add_data['form']['name'] = "码商活码通道开启网关";
                }else if(stristr($data['url'],"merchant_livecode") !== false && $data['form']['action'] == 'del'){
                    $add_data['form']['name'] = "删除码商活码通道";
                }else if(stristr($data['url'],"merchant_livecode") !== false && $data['form']['action'] == 'edit'){
                    $add_data['form']['name'] = "编辑码商活码通道";
                }else if(stristr($data['url'],"memberlist") !== false && $data['form']['action'] == 'add'){
                    $add_data['form']['name'] = "添加商户";
                }else if(stristr($data['url'],"memberlist") !== false && $data['form']['action'] == 'edit'){
                    $add_data['form']['name'] = "编辑商户";
                }else if(stristr($data['url'],"memberlist") !== false && $data['form']['action'] == 'amount'){
                    $add_data['form']['name'] = "商户余额充值";
                }else if(stristr($data['url'],"memberlist") !== false && $data['form']['action'] == 'del'){
                    $add_data['form']['name'] = "删除商户所有信息";
                }else if(stristr($data['url'],"member_payment") !== false && $data['form']['action'] == 'add'){
                    $add_data['form']['name'] = "添加商户通道";
                }else if(stristr($data['url'],"member_payment") !== false && $data['form']['action'] == 'open'){
                    $add_data['form']['name'] = "开启商户通道";
                }else if(stristr($data['url'],"member_payment") !== false && $data['form']['action'] == 'lose'){
                    $add_data['form']['name'] = "暂停商户通道";
                }else if(stristr($data['url'],"member_payment") !== false && $data['form']['action'] == 'del'){
                    $add_data['form']['name'] = "删除商户通道";
                }else if(stristr($data['url'],"membersite") !== false && isset($data['form']['form'])){
                    $add_data['form']['name'] = "修改商户设置";
                }else if(stristr($data['url'],"order_record") !== false && $data['form']['action'] == 'amount'){
                    $add_data['form']['name'] = "修改订单补单";
                }else if(stristr($data['url'],"order_record") !== false && $data['form']['action'] == 'notice'){
                    $add_data['form']['name'] = "订单补发通知";
                }else if(stristr($data['url'],"order_record") !== false && $data['form']['action'] == 'repair'){
                    $add_data['form']['name'] = "订单补单";
                }else if(stristr($data['url'],"order_record") !== false && $data['form']['action'] == 'del'){
                    $add_data['form']['name'] = "删除订单";
                }else if(stristr($data['url'],"drawing") !== false && $data['form']['action'] == 'result'){
                    $add_data['form']['name'] = "处理提现";
                }else if(stristr($data['url'],"drawing") !== false && $data['form']['action'] == 'reject'){
                    $add_data['form']['name'] = "驳回提现";
                }else if(stristr($data['url'],"drawing") !== false && $data['form']['action'] == 'del'){
                    $add_data['form']['name'] = "删除提现记录";
                }else if(stristr($data['url'],"diecode") !== false && $data['form']['action'] == 'edit'){
                    $add_data['form']['name'] = "平台固码编辑";
                }else if(stristr($data['url'],"diecode") !== false && $data['form']['action'] == 'open'){
                    $add_data['form']['name'] = "平台固码开启";
                }else if(stristr($data['url'],"diecode") !== false && $data['form']['action'] == 'close'){
                    $add_data['form']['name'] = "平台固码暂停";
                }else if(stristr($data['url'],"livecode") !== false && $data['form']['action'] == 'edit'){
                    $add_data['form']['name'] = "平台活码编辑";
                }else if(stristr($data['url'],"livecode") !== false && $data['form']['action'] == 'open'){
                    $add_data['form']['name'] = "平台活码开启";
                }else if(stristr($data['url'],"livecode") !== false && $data['form']['action'] == 'close'){
                    $add_data['form']['name'] = "平台活码暂停";
                }else if(stristr($data['url'],"site") !== false && $data['form']['action'] =='edit'){
                    $add_data['form']['name'] = "修改系统设置";
                }else if(stristr($data['url'],"merchantsite") !== false && $data['form']['action'] =='edit'){
                    $add_data['form']['name'] = "修改码商设置";
                }
                if(isset($add_data)){
                    $add_data['form']['time'] = time();
                    $add_data['form']['ip'] = $request->getClientIp();
                    $add_data['type'] = "admin";
                    /****添加至Redis后续定时更新****/
                    Redis::rpush('operation_log',json_encode($add_data));
                }
            }
        }
        return $next($request);
    }
}
