<?php

$is_script = 1;
include_once __DIR__ . '/../../index.php';

// use app\order\model\CashOut as CashMoedl;

use app\agent\model\User;
use app\order\model\User as UserModel;
// use app\order\model\UserScore as ScoreModel;
// use app\order\model\ProductOrder as OrderModel;
// use app\api\model\ProductPackage as ProductPackageModel;
use app\manager\model\ProductAccount;



// $order_model = new OrderModel;
// $package_model = new ProductPackageModel;
$user_model = new UserModel;
$account_model = new ProductAccount;

$list = $account_model->field('user.Name,user.id_code,product_account.Account')
->join('user ON product_account.UserId=user.Id')
->where(['ProductId'=>['in',[1,2]]])
->where("product_account.EndTime>'".date('Y-m-d')."'")
->fetchAll();

$file = fopen('./qzxlsim.csv', 'w');

foreach ($list as &$info) {
    $info['Name'] = mb_substr($info['Name'], 0, 1) . '***';
    $info['id_code'] = substr($info['id_code'], 0, 6) . '***';
    fputcsv($file, $info);
}

fclose($file);

die;



// $file_path = './product_order.csv';
// $file = fopen($file_path, 'r');


// $data = [];
// while ($info = fgetcsv($file)) {

//     $id = $info[0];
//     $temp = explode(',',$info[1]);
//     $refund_time = $temp[0];
//     $create_time = date('Y-m-d H:i:s',strtotime($temp[1]) + 86400 + mt_rand(0,86459));

//     $time = (intval(explode('天',$refund_time)[0])-1).'天'.explode('天',$refund_time)[1];

//     $update_data = [
//         'RefundRestTime' => $time,
//         'CreateTime' => $create_time,
//         'UpdateTime' => $create_time,
//     ];
    
//     die;

//     $order_model->updateOne(['Id'=>$id],$update_data);

//     // print_r($update_data);die;
//     // $time = 
//     // $id = $info[0];
//     // print_r($time);die;
// }

// die;
//强子迅联先锋

$file_path = './qzxlbutui.csv';
$file = fopen($file_path, 'r');


$data = [];
while ($info = fgetcsv($file)) {
    if($info[13] == 2||$info[14] == 2) {
        continue;
    }
    if (strpos($info[11], ',') !== false) {
        $info[12] = explode(',',$info[11])[1];
    }

    $redund_time = strtotime($info[12])+mt_rand(0,86400);
    

    // echo $redund_time;die;

    $real_day = $package_model->getOne(['Id'=>$info[5]])['DayCount'];

    $info10 = explode(' ',$info[10]);
    $info101 = explode('/',$info10[0]);
    $info[10] = $info101[0].'-'.$info101[1].'-'.$info101[2].' '.$info10[1];

    $info[11] = explode(',',$info[11])[0];
    $info[11] =  str_replace('年','-',$info[11]);
    $info[11] =  str_replace('月','-',$info[11]);
    $info[11] =  str_replace('日','',$info[11]);
    

    $end_time = strtotime($info[11]);

    if(($end_time-$redund_time)<0){
        $redund_time = mt_rand(strtotime($info[10]),strtotime($info[10])+$real_day*86400);
    }

    $rest_time = handle_time($end_time-$redund_time);

    $redund_time_real = date('Y-m-d H:i:s',$redund_time);
    $temp = [
        'TenantId'=>0,
        'UserId'=>$info[1],
        'UserName'=>$info[2],
        'ProductId'=>$info[3],
        'ProductName'=>$info[4],
        'PackageId'=>$info[5],
        'PackageName'=>$info[6],
        'CouponId'=>0,
        'OriginKey'=>null,
        'OrderName'=>$info[4],
        'OrderNo'=>'tk2020121322222222',
        'TradeNo'=>null,
        'OrderState'=>30,
        'OrderType'=>5,
        'PayType'=>10,
        'PayState'=>0,
        'ConnectCount'=>$info[9],
        'OrderAmount'=>0,
        'PaymentAmount'=>0,
        'OtherPayAmount'=>0,
        'AccountPayAmount'=>0,
        'CouponAmount'=>null,
        'DayPrice'=>5,
        'DayCount'=>$real_day,
        'RefundCount'=>1,
        'RefundAmount'=>0,
        'RefundRestTime'=>$rest_time,
        'ClientType'=>0,
        'Channel'=>null,
        'ChannelType'=>0,
        'CreateTime'=> $redund_time_real,
        'UpdateTime'=> $redund_time_real,
        'Accounts'=>$info[7],
        'AccountCount'=>1,
        'AccountPwd'=>$info[8],
        'Remark'=>'退款',
        'DeleteTag'=>0,
        'StartTime'=>$info[10],
        'EndTime'=>$info[11],
        'PayChannel'=>30,
        'IsAutoRefund'=>0,
        'BackAmount'=>1001,
        'RefundReason'=>null,
    ];
    // print_r($temp);die;
    $data[] = $temp;
}
// die;
$order_model->addAll($data);


die;
//星星

$file_path = './xingxing.csv';
$file = fopen($file_path, 'r');

$order_model = new OrderModel;

$data = [];
while ($info = fgetcsv($file)) {

    if($info[13] == 2||$info[14] == 2||$info[12] == 2) {
        continue;
    }

    if( $info[3] == 3||$info[3] == 7||$info[3] == 9){
        // print_r($info);
        $test = [];
        // print_r(explode(',',$info[4]));
        if (strpos($info[4], ',') !== false) {
            $ttt = explode(',',$info[4]);
            $info[4] = $ttt[0];
            $test[] = $ttt[1];
        }
        array_splice($info,5,0,$test);
    } 

    
    $real_day = $package_model->getOne(['Id'=>$info[5]])['DayCount'];

    $info10 = explode(' ',$info[10]);
    $info101 = explode('/',$info10[0]);
    $info[10] = $info101[0].'-'.$info101[1].'-'.$info101[2].' '.$info10[1];

    $info11 = explode(' ',$info[11]);
    $info1011 = explode('/',$info11[0]);
    $info[11] = $info1011[0].'-'.$info1011[1].'-'.$info1011[2].' '.$info11[1];
    // echo $info[10];
    
    $time = explode('-',$info[12]);
    $time1 = explode('.',$time[0]);
    $time2 = explode('.',$time[1]);

    // print_r($time1);
    // print_r($time2);
    // die;

    
    $end_time = strtotime($info[11]);

    $redund_time = mt_rand((strtotime('2020-'.$time1[0].'-'.$time1[1])),(strtotime('2020-'.$time2[0].'-'.$time2[1])));

    // echo $redund_time;die;
    
    if(($end_time-$redund_time)<0){
        $redund_time = strtotime('2020-'.$time1[0].'-01')+mt_rand(0,86400);
    } 
    // if((strtotime($info[10])-$redund_time)>0){
    //     $redund_time = mt_rand(strtotime($info[10]),strtotime($info[10])+$real_day*86400);
    // }
    // if((strtotime('2020-'.$time[0].'-'.$time[1])-strtotime($info[10])<0)){
    //     $redund_time = mt_rand(strtotime($info[10]),strtotime($info[10])+$real_day*86400);
    // }
    // if(($end_time-$redund_time)==0){
    //     $redund_time = mt_rand(strtotime($info[10]),strtotime($info[10])+$real_day*86400);
    // } 
        
    $rest_time = handle_time($end_time-$redund_time);
    // echo $rest_time;
    // die;

    $redund_time_real = date('Y-m-d H:i:s',$redund_time);
    $temp = [
        'TenantId'=>0,
        'UserId'=>$info[1],
        'UserName'=>$info[2],
        'ProductId'=>$info[3],
        'ProductName'=>$info[4],
        'PackageId'=>$info[5],
        'PackageName'=>$info[6],
        'CouponId'=>0,
        'OriginKey'=>null,
        'OrderName'=>$info[4],
        'OrderNo'=>'tk2020121322222222',
        'TradeNo'=>null,
        'OrderState'=>30,
        'OrderType'=>5,
        'PayType'=>10,
        'PayState'=>0,
        'ConnectCount'=>$info[9],
        'OrderAmount'=>0,
        'PaymentAmount'=>0,
        'OtherPayAmount'=>0,
        'AccountPayAmount'=>0,
        'CouponAmount'=>null,
        'DayPrice'=>5,
        'DayCount'=>$real_day,
        'RefundCount'=>1,
        'RefundAmount'=>0,
        'RefundRestTime'=>$rest_time,
        'ClientType'=>0,
        'Channel'=>null,
        'ChannelType'=>0,
        'CreateTime'=> $redund_time_real,
        'UpdateTime'=> $redund_time_real,
        'Accounts'=>$info[7],
        'AccountCount'=>1,
        'AccountPwd'=>$info[8],
        'Remark'=>'退款',
        'DeleteTag'=>0,
        'StartTime'=>$info[10],
        'EndTime'=>$info[11],
        'PayChannel'=>30,
        'IsAutoRefund'=>0,
        'BackAmount'=>1001,
        'RefundReason'=>null,
    ];
    // print_r($temp);die;
    $data[] = $temp;
}

$order_model->addAll($data);


die;

//金瑞

$file_path = './jinrui.csv';
$file = fopen($file_path, 'r');

$order_model = new OrderModel;

$data = [];
while ($info = fgetcsv($file)) {
    if($info[13] == 2||$info[14] == 2||$info[12] == 2) {
        continue;
    }

    if( $info[3] == 3||$info[3] == 7||$info[3] == 9){
        // print_r($info);
        $test = [];
        // print_r(explode(',',$info[4]));
        if (strpos($info[4], ',') !== false) {
            $ttt = explode(',',$info[4]);
            $info[4] = $ttt[0];
            $test[] = $ttt[1];
        }
        array_splice($info,5,0,$test);
    } 

    
    $real_day = $package_model->getOne(['Id'=>$info[5]])['DayCount'];

    $info10 = explode(' ',$info[10]);
    $info101 = explode('/',$info10[0]);
    $info[10] = $info101[0].'-'.$info101[1].'-'.$info101[2].' '.$info10[1];

    $info11 = explode(' ',$info[11]);
    $info1011 = explode('/',$info11[0]);
    $info[11] = $info1011[0].'-'.$info1011[1].'-'.$info1011[2].' '.$info11[1];
    // echo $info[10];
    
    $time = explode('-',$info[12]);
    $time1 = explode('.',$time[0]);
    $time2 = explode('.',$time[1]);

    // print_r($time1);
    // print_r($time2);
    // die;

    
    $end_time = strtotime($info[11]);

    $redund_time = mt_rand((strtotime('2020-'.$time1[0].'-'.$time1[1])),(strtotime('2020-'.$time2[0].'-'.$time2[1])));

    // echo $redund_time;die;
    
    if(($end_time-$redund_time)<0){
        $redund_time = strtotime('2020-'.$time1[0].'-01')+mt_rand(0,86400);
    } 
    // if((strtotime($info[10])-$redund_time)>0){
    //     $redund_time = mt_rand(strtotime($info[10]),strtotime($info[10])+$real_day*86400);
    // }
    // if((strtotime('2020-'.$time[0].'-'.$time[1])-strtotime($info[10])<0)){
    //     $redund_time = mt_rand(strtotime($info[10]),strtotime($info[10])+$real_day*86400);
    // }
    // if(($end_time-$redund_time)==0){
    //     $redund_time = mt_rand(strtotime($info[10]),strtotime($info[10])+$real_day*86400);
    // } 
        
    $rest_time = handle_time($end_time-$redund_time);
    // echo $rest_time;
    // die;

    $redund_time_real = date('Y-m-d H:i:s',$redund_time);
    $temp = [
        'TenantId'=>0,
        'UserId'=>$info[1],
        'UserName'=>$info[2],
        'ProductId'=>$info[3],
        'ProductName'=>$info[4],
        'PackageId'=>$info[5],
        'PackageName'=>$info[6],
        'CouponId'=>0,
        'OriginKey'=>null,
        'OrderName'=>$info[4],
        'OrderNo'=>'tk2020121322222222',
        'TradeNo'=>null,
        'OrderState'=>30,
        'OrderType'=>5,
        'PayType'=>10,
        'PayState'=>0,
        'ConnectCount'=>$info[9],
        'OrderAmount'=>0,
        'PaymentAmount'=>0,
        'OtherPayAmount'=>0,
        'AccountPayAmount'=>0,
        'CouponAmount'=>null,
        'DayPrice'=>5,
        'DayCount'=>$real_day,
        'RefundCount'=>1,
        'RefundAmount'=>0,
        'RefundRestTime'=>$rest_time,
        'ClientType'=>0,
        'Channel'=>null,
        'ChannelType'=>0,
        'CreateTime'=> $redund_time_real,
        'UpdateTime'=> $redund_time_real,
        'Accounts'=>$info[7],
        'AccountCount'=>1,
        'AccountPwd'=>$info[8],
        'Remark'=>'退款',
        'DeleteTag'=>0,
        'StartTime'=>$info[10],
        'EndTime'=>$info[11],
        'PayChannel'=>30,
        'IsAutoRefund'=>0,
        'BackAmount'=>1001,
        'RefundReason'=>null,
    ];
    $data[] = $temp;
}

$order_model->addAll($data);

//将剩余时间进行处理切割转换为秒
function handle_time($rest_time)
{
    $time = '';
    $day = floor($rest_time/86400);
    $h = floor(($rest_time - $day*86400)/3600);
    $s = floor(($rest_time - $day*86400 - $h*3600)/60);
    return $day.'天'.$h.'时'.$s.'分';
}





//补款
// $file_path = './qzxlrefund.csv';
// $file = fopen($file_path, 'r');
// while ($info = fgetcsv($file)) {
//     $user_model = new UserModel;
//     $user_info = $user_model->getOne(['LoginCode' => $info['0']]);

//     $user_rest = $user_info['RestAmount'];

//     $rest = $user_rest + $info['1'];
//     $user_model->where(['Id' => $user_info['Id']])->update(['RestAmount' => $rest]);
//     $score = new ScoreModel;
//     $score_data = [
//         'UserId' => $user_info['Id'],
//         'ScoreType' => 1,
//         'ScoreTypeName' => '强子迅联失败订单补款金额',
//         'ScoreValue' => $info[1],
//         'UserName' => $info[0],
//         'OperateUserName' => 'script',
//         'RestAmount1' => $user_rest,
//         'RestAmount2' => $rest,
//         'CreateTime' => date('Y-m-d H:i:s', time()),
//         'UpdateTime' => date('Y-m-d H:i:s', time()),
//         'TenantId' => 0,
//         'DeleteTag' => 0,
//     ];
//     $score->add($score_data);

//     // print_r($info);die;
// }
// fclose($file);
// foreach ($tixian as $info){
//     $id = $info['id'];
//     $user_model = new UserModel;
//     $user_rest = $user_model->getOne(['id' => $info['userid']], 'RestAmount')['RestAmount'];
    // $rest = $user_rest + $info['money'];
    // $user_model->where(['id' => $info['userid']])->update(['RestAmount' => $rest]);
    // $score = new ScoreModel;
    // $score_data = [
    //     'UserId' => $info['userid'],
    //     'ScoreType' => 9,
    //     'ScoreTypeName' => '提现被拒退还',
    //     'ScoreValue' => $info['money'],
    //     'UserName' => $info['username'],
    //     'OperateUserName' => 'auto',
    //     'RestAmount1' => $user_rest,
    //     'RestAmount2' => $rest,
    //     'CreateTime' => date('Y-m-d H:i:s', time()),
    //     'UpdateTime' => date('Y-m-d H:i:s', time()),
    //     'TenantId' => 0,
    //     'DeleteTag' => 0,
    // ];
    // $score->add($score_data);
//     $data['status'] == '1';
//     $data['op_user'] = 'auto';
//     $data['remark'] = '因我司支付宝账户被公安暂时冻结，故无法资金转出，我司正与公安协调处理此事，你可用余额消费，如需继续提现，您可再次申请，我司将在账户解冻后第一时间处理。';
//     $cash->where(['id' => $id])->update($data);
// }

//星星服务器
// $file_path = './xingxing.csv';
// $file = fopen($file_path, 'r');
// $data = [];
// while ($info = fgetcsv($file)) {
//     $data[] = $info;
// }
// fclose($file);

// $xingxing_f = fopen(__DIR__ . '/xingxingformat.csv', 'w');
// //写入星星文件
// xingxing_getList($data, $xingxing_f);
// fclose($xingxing_f);

// //星星服务器
// //星星服务器解析
// function xingxing_getList($data, &$file)
// {
//     $province_record = [];
//     $key = '';
//     $list1 = [];
//     //省会
//     $province = ['黑龙', '内蒙'];
//     $citys = ['北京', '上海', '天津', '重庆'];
//     foreach ($data as $info) {

//         $record = [];
//         $record['name'] = '星星';
//         $province_detail = mb_substr($info['1'], 0, 2);
//         if (in_array($province_detail, $province)) {
//             $province_detail = mb_substr($info['1'], 0, 3);
//             $info['1'] = mb_substr($info['1'], 3);
//         } elseif (!in_array($province_detail, $citys)) {
//             $info['1'] = mb_substr($info['1'], 2);
//         }
//         if (!in_array($province_detail, $province_record)) {
//             $record['city'] = $province_detail;
//             $province_record[] = $province_detail;
//             $key = $province_detail;
//             $list1[$key][] = $record;
//         }
//         $city_ex = explode('电信', $info['1']);
//         if (strpos($info['1'], '电信') !== false) {
//             $record['city'] = $city_ex[0] . ($city_ex[1] ?? '');
//             $record['supply'] = '电信';
//         } elseif (strpos($info['1'], '联通') !== false) {
//             $record['city'] = $city_ex[0] . ($city_ex[1] ?? '');
//             $record['supply'] = '联通';
//         } elseif (strpos($info['1'], '移动') !== false) {
//             $record['city'] = $city_ex[0] . ($city_ex[1] ?? '');
//             $record['supply'] = '移动';
//         } else {
//             $record['city'] = $info['1'];
//             $record['supply'] = '电信/联通';
//         }

//         $record['ip'] = '';
//         $record['daikuan'] = '50M';
//         $record['onlineuser'] = '';
//         $record['maxuser'] = '';
//         $record['online'] = '正常';
//         $record['status'] = '正常';
//         $record['nasname'] = $info[0];
//         $key = $province_detail;
//         $list1[$key][] = $record;
//     }
//     // print_r($list1);die;
//     foreach ($list1 as $val) {
//         foreach ($val as $net_data) {

//             fputcsv($file, $net_data);
//         }
//     }

// }