<?php

namespace app\jinyou\model;

use fastphp\base\Model;
use extend\jinyouapi\Jinyoujingtai;
use enum\account\ProductAccount as AccountEnum;

class Account extends Model
{
    protected $table = 'product_account';

    //开通账号
    public function open_account($account_info,$user_info,$package_info) {
        $jinyou = new Jinyoujingtai;
        $return_data = [];

        foreach ($account_info['product_info'] as $key=>$add_city) {
            if ($add_city == 0) {
                unset($account_info['product_info'][$key]);
            }
        }

        $package_origin = explode('-',$package_info['OriginKey']);
        $api_data = $jinyou::createStatic($package_origin['3'],$account_info['game_id'],$account_info['account'],$account_info['password'],$account_info['buy_num'],121,$package_origin['0'],$package_origin['1'],$package_origin['2'],'',$account_info['product_info']);

        $account_list = [];
        if ($api_data->code == 200) {
            $account_enum = new AccountEnum;

            $account_type = $account_enum::$New;
            if ($package_info['IsTest'] == 1) {
                $account_type = $account_enum::$Test;
            } else if ($account_info['buy_num'] > 1) {
                $account_type = $account_enum::$News;
            }
            foreach ($api_data->data as $account) {

                $accounts = [
                    'TenantId' => 0,
                    'UserId' => $user_info['Id'],
                    'UserCode' => $user_info['LoginCode'],
                    'ProductId' => $package_info['ProductId'],
                    'ProductName' => $package_info['ProductName'],
                    'PackageId' => $package_info['Id'],
                    'PackageName' => $package_info['Name'],
                    'AccountType' => $account_type,
                    'Account' => $account->user,
                    'Pwd' => $account->passwd,
                    'ConnectCount' => $account->conn_max,
                    'StartTime' => date('Y-m-d H:i:s',$account->add_time),
                    'EndTime' => date('Y-m-d H:i:s',$account->end_time),
                    'Status' => $account_enum::$Normal,
                    'ChargeStatus' => $account_enum::$Success,
                    'CreateTime' => date('Y-m-d H:i:s',time()),
                    'UpdateTime' => date('Y-m-d H:i:s',time()),
                    'DeleteTag' => 0,
                    'Remark' => $account->region . $account->address,
                    'Raw' => $account->inlet_website,
                    'game_id' => empty($account->game_id)?0:$account->game_id,
                ];
                $account_list[] = $accounts;
            }
            $this->addAll($account_list);
            $return_data['code'] = 1;
            $return_data['msg'] = "账号开通成功！";
        } else  {
            $return_data['code'] = -1;
            $return_data['msg'] = "账号开通失败！请联系客服";
        }
        return $return_data;
    }

}
