<?php

function auto_format_list(){
    $product_list = [0, 1, 2, 3, 4, 5, 6, 8, 10, 13, 14, 15, 16, 17, 18, 19,22,23,25,26,27,28,29];
    foreach ($product_list as $product) {
        switch ($product) {
            case 0:
                format_all_product(__DIR__ . '/data/all.csv');
                break;
            case 8:
                format_xingxing(__DIR__ . '/data/xingxing.csv', __DIR__ . '/data/2288.csv', 'xxip.vip');
                break;
            case 6:
                format_product(__DIR__ . '/data/xianfeng.csv', 'xfip.vip');
                break;
            case 13:
                format_product(__DIR__ . '/data/jike.csv', 'jkip.com');
                break;
            case 1:
                format_product(__DIR__ . '/data/qiangzi.csv', 'qzip.vip');
                break;
            case 2:
                format_product(__DIR__ . '/data/xunlian.csv', 'xlip.vip');
                break;
            case 14:
                format_product(__DIR__ . '/data/tiantian.csv', 'ttip.cn');
                break;
            case 5:
                format_product(__DIR__ . '/data/laoying.csv', 'lyip.vip');
                break;
            case 4:
                format_product(__DIR__ . '/data/jinrui.csv', 'jrip.vip');
                break;
            case 10:
                format_product(__DIR__ . '/data/wuxian.csv', 'wxip.net');
                break;
            case 16:
                format_product(__DIR__ . '/data/shihui.csv', 'shihui');
                break;
            case 15:
                format_product(__DIR__ . '/data/gongxiang.csv', 'shihui');
                break;
            case 17:
                format_product(__DIR__ . '/data/xigua.csv', 'xgip.vip');
                break;
            case 18:
                format_product(__DIR__ . '/data/mogu.csv', 'mgip.net');
                break;
            case 19:
                format_product_huohu(__DIR__ . '/data/huohu.csv');
                break;
            case 22:
                format_product(__DIR__ . '/data/huohu.csv','hhip.vip');
                break;
            case 23:
                format_product(__DIR__ . '/data/yunniu.csv','ynip.net');
                break;
            case 25:
                format_product(__DIR__ . '/data/qiangzijt.csv','qzip.vip');
                break;
            
            case 26:
                format_product(__DIR__ . '/data/liebao.csv','lbip.cc');
                break;
            
            case 27:
                format_product(__DIR__ . '/data/qilin.csv','qlip.cc');
                break;
            case 28:
                format_shuidi_product(__DIR__ . '/data/shuidi.csv','sdip.cc');
                break;
            case 29:
                format_product(__DIR__ . '/data/jihu.csv','jhip.net');
                break;
            case 3:
                format_product(__DIR__ . '/data/jiguang.csv','jgip.cc');
                break;
        }
    }
}

function format_all_product($file_path)
{
    $list = [];
    $dns = '';
    $record = [];
    $citys = [];
    $user_count = 0;
    $count = 0;
    $file = fopen($file_path, 'r');
    $file1 = fopen(__DIR__ . '/data/download/all.csv', 'w');
    while ($info = fgetcsv($file)) {
        $data['name'] = $info[0];
        switch ($data['name']) {
            case '星星':
                $dns = 'xxip.vip';
                break;
            case '先锋':
                $dns = 'xfip.vip';
                break;
            case '极客':
                $dns = 'jkip.com';
                break;
            case '强子':
                $dns = 'qzip.vip';
                break;
            case '讯连':
                $dns = 'xlip.vip';
                break;
            case '天天':
                $dns = 'ttip.cn';
                break;
            case '老鹰':
                $dns = 'lyip.vip';
                break;
            case '金瑞':
                $dns = 'jrip.vip';
                break;
            case '无限':
                $dns = 'wxip.net';
                break;
            case '实惠':
                $dns = 'shihui';
                break;
            case '共享':
                $dns = 'shihui';
                break;
            case '西瓜':
                $dns = 'xgip.vip';
                break;
            case '蘑菇':
                $dns = 'mgip.net';
                break;
            case '极狐':
                $dns = 'jhip.net';
                break;
            case '极光':
                $dns = 'jgip.cc';
                break;
            case '火狐静态':
                $dns = 'hhip.vip';
                break;
            
            case '云牛':
                $dns = 'ynip.net';
                break;
            
            case '强子静态':
                $dns = 'qzip.vip';
                break;
            case '猎豹':
                $dns = 'lbip.cc';
                break;
            case '麒麟':
                $dns = 'qlip.cc';
                break;
            case '水滴静态':
                $dns = 'sdip.cc';
                break;
            default:
                $dns = 'xlip.vip';
                break;
        }
        $city = city_exist($citys, $info[1]);
        $citys[] = $city;
        $data['city'] = $city;
        $data['supply'] = $info[2] ?? '';
        $data['ip'] = $info[3] ?? '';
        $data['daikuan'] = $info[4] ?? '';
        $data['onlineuser'] = $info[5] ?? '';
        $data['maxuser'] = $info[6] ?? '';
        $data['online'] = $info[7] ?? '';
        $data['status'] = $info[8] ?? '';
        if ($dns == 'shihui') {
            $data['nasname'] = $info[9] ?? '';
        } else {
            $ip = explode('.', ($info[9] ?? ''));
            if (count($ip) == 4) {
                $data['nasname'] = $info[9];
            } else {
                $data['nasname'] = $ip[0] ? ip_repeat($record, $ip[0], $dns) . '.' . $dns : '';
            }
        }
        fputcsv($file1, $data);
    }
    fclose($file);
    fclose($file1);
}

//火狐
function format_product_huohu($file_path)
{
    $list1 = [];
    $file = fopen($file_path, 'r');
    $file_path1 = explode('/', $file_path);
    $file1 = fopen(__DIR__ . '/data/download/' . $file_path1[8], 'w');
    $record = [];
    while ($info = fgetcsv($file)) {
        if (empty($info[4])) {
            $key = $info[1];
        }
        $record['name'] = $info[0];
        $record['city'] = $info[1]??'';

        $record['supply'] = $info[2]??'';
        $record['ip'] = '';
        $record['daikuan'] = '';
        $record['onlineuser'] = '';
        $record['maxuser'] = '';
        $record['online'] = '正常';
        $record['status'] = '';

        if(!empty($info[4])){
            $record['nasname'] = '1.hhip.vip';
        } else {
            $record['nasname'] = '';
        }
        $list1[$key][] = $record;
    }
    foreach ($list1 as $k => $val) {
        foreach ($val as $net_data) {

            fputcsv($file1, $net_data);
        }
    }
    fclose($file1);
    fclose($file);
}

function format_product($file_path, $dns = '')
{
    $list1 = [];
    $list = [];
    $user_count = 0;
    $count = 0;
    $key = '';
    $citys = [];
    $file = fopen($file_path, 'r');
    $file_path1 = explode('/', $file_path);
    $file1 = fopen(__DIR__ . '/data/download/' . $file_path1[8], 'w');
    $record = [];
    while ($info = fgetcsv($file)) {
        if (in_array($info['1'], ['曾', '龙'])) {
            continue;
        }
        if (empty($info[9])) {
            $key = $info[1];
        }
        $data['name'] = $info[0];
        $city = city_exist($citys, $info[1]);
        $citys[] = $city;
        $data['city'] = $city;
        $data['supply'] = $info[2] ?? '';
        $data['ip'] = $info[3] ?? '';
        $data['daikuan'] = $info[4] ?? '';
        $data['onlineuser'] = $info[5] ?? '';
        $data['maxuser'] = $info[6] ?? '';
        $data['online'] = $info[7] ?? '';
        $data['status'] = $info[8] ?? '';
        if ($dns != 'shihui') {
            $ip = explode('.', ($info[9] ?? ''));
            
            if (count($ip) == 4) {
                $data['nasname'] = $info[9];
            } else {
                $data['nasname'] = $ip[0] ? ip_repeat($record, $ip[0]) . '.' . $dns : '';
            }
        } else {
            $data['nasname'] = $info[9] ?? '';
        }
        $list1[$key][] = $data;
    }

    foreach ($list1 as $k => $val) {
        foreach ($val as $net_data) {

            fputcsv($file1, $net_data);
        }
    }
    fclose($file1);
    fclose($file);
}

function format_shuidi_product($file_path, $dns = '')
{
    $list1 = [];
    $list = [];
    $user_count = 0;
    $count = 0;
    $key = '';
    $citys = [];
    $file = fopen($file_path, 'r');
    $file_path1 = explode('/', $file_path);
    $file1 = fopen(__DIR__ . '/data/download/' . $file_path1[8], 'w');
    $record = [];
    while ($info = fgetcsv($file)) {
        if (in_array($info['1'], ['曾', '龙'])) {
            continue;
        }
        if (empty($info[9])) {
            $key = $info[1];
        }
        $data['name'] = $info[0];
        $city = city_exist($citys, $info[1]);
        $citys[] = $city;
        $data['city'] = $city;
        $data['supply'] = $info[2] ?? '';
        $data['ip'] = $info[3] ?? '';
        $data['daikuan'] = $info[4] ?? '';
        $data['onlineuser'] = $info[5] ?? '';
        $data['maxuser'] = $info[6] ?? '';
        $data['online'] = $info[7] ?? '';
        $data['status'] = $info[8] ?? '';
        $data['nasname'] = $info[9] ?? '';
        $list1[$key][] = $data;
    }

    foreach ($list1 as $k => $val) {
        foreach ($val as $net_data) {

            fputcsv($file1, $net_data);
        }
    }
    fclose($file1);
    fclose($file);
}
function format_xingxing($file_path1, $file_path2, $dns)
{
    $list1 = [];
    $list = [];
    $user_count = 0;
    $count = 0;
    $key = '';
    $citys = [];
    $file1 = fopen($file_path1, 'r');
    $file2 = fopen($file_path2, 'r');
    $record = [];

    $keys = [];
    $file_path = explode('/', $file_path1);
    $file3 = fopen(__DIR__ . '/data/download/' . $file_path[8], 'w');
    while ($info = fgetcsv($file1)) {
        if (empty($info[9])) {
            $key = $info[1];
            $keys[] = $key;
        }
        $data['name'] = $info[0];
        $city = city_exist($citys, $info[1]);
        $citys[] = $city;
        $data['city'] = $city;
        $data['supply'] = $info[2] ?? '';
        $data['ip'] = $info[3] ?? '';
        $data['daikuan'] = $info[4] ?? '';
        $data['onlineuser'] = $info[5] ?? '';
        $data['maxuser'] = $info[6] ?? '';
        $data['online'] = $info[7] ?? '';
        if (!empty($info[9])) {
            $user_count += $data['online'] == '正常' ? 1 : 0;
            $count++;
        }
        $data['status'] = $info[8] ?? '';
        $ip = explode('.', ($info[9] ?? ''));
        $data['nasname'] = $ip[0] ? ip_repeat($record, $ip[0]) . '.' . $dns : '';
        $list1[$key][] = $data;
    }
    while ($info = fgetcsv($file2)) {
        if (empty($info[9])) {
            $key = $info[1];
            if (in_array($key, $keys)) {
                continue;
            }
        }
        $data['name'] = $info[0];
        $city = city_exist($citys, $info[1]);
        $citys[] = $city;
        $data['city'] = $city;
        $data['supply'] = $info[2] ?? '';
        $data['ip'] = $info[3] ?? '';
        $data['daikuan'] = $info[4] ?? '';
        $data['onlineuser'] = $info[5] ?? '';
        $data['maxuser'] = $info[6] ?? '';
        $data['online'] = $info[7] ?? '';
        $data['status'] = $info[8] ?? '';
        $ip = explode('.', ($info[9] ?? ''));
        $data['nasname'] = $ip[0] ? ip_repeat($record, $ip[0]) . '.' . $dns : '';
        $list1[$key][] = $data;
    }

    foreach ($list1 as $k => $val) {
        foreach ($val as $net_data) {

            fputcsv($file3, $net_data);
        }
    }

    fclose($file1);
    fclose($file2);
    fclose($file3);

}

function ip_repeat(&$record, $ip, $dns = '')
{
    for ($i = 2; $i < 100; $i++) {
        if (in_array($ip . $dns, $record)) {
            if (in_array($ip . $dns . 'v' . $i, $record)) {
                continue;
            } else {
                $record[] = $ip . $dns . 'v' . $i;
                return $ip . 'v' . $i;
            }
        } else {
            $record[] = $ip . $dns;
            return $ip;
        }
    }
}

function city_exist($citys, $city)
{
    for ($i = 1; $i < 100; $i++) {
        if (in_array($city, $citys)) {
            if (in_array($city . $i, $citys)) {
                continue;
            } else {
                return $city . $i;
            }
        } else {
            return $city;
        }
    }
}
