<?php
/**
 * Created by PhpStorm.
 * User: chouchou
 * Date: 2019-6-10
 * Time: 19:33
 */

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\db;
use think\facade\Session;
use app\admin\model\BaseStationModel;
class Basestation extends  Base
{
    public function base_export(){
        $map = Session::get('map');
        $BaseStation= new BaseStationModel;
        $BaseStationList=$BaseStation->getAllBaseStation($map);
        if($BaseStationList){
            $excel = new Excel;
            $excel->baseexport($BaseStationList,'基站资料');
        }else{
            $this->error('导出数据不能为空','/base_list');
        }
    }
    // public function base_list(Request $request){
    // 	         $map =[];
    // 	          $user = session('user');
    // 	         $admi[]=['name','=',$user['name']];
    // 	         $name=db::name('admin')->where($admi)->find();
    // 	         if(empty($name)){
    //                  $map['user_id'] =$user['user_id'];
    // 	         }
    //    $search='搜索内容为：';
    //    $search_text = $request->get('search_text');
    //    $search_type = $request->get('search_type');
    //    if($search_text){
    //       switch ($search_type) {
    //       	case '1':
    //         $map['station_name']=$search_text;
    //         $this->assign('search_text',$map['station_name']);
    //         $this->assign('search_type',$search_type);
    //               $search.='  基站名称：'.$search_text;
    //       		break;
    //          case '2':
    //         $map['station_sp_code']=$search_text;
    //         $this->assign('search_text',$map['station_sp_code']);
    //         $this->assign('search_type',$search_type);
    //          $search.='  基站编号：'.$search_text;
    //       		break;
    //          case '3':
    //         $map['proj_number']=$search_text;
    //         $this->assign('search_text',$map['proj_number']);
    //         $this->assign('search_type',$search_type);
    //          $search.='  项目编号：'.$search_text;
    //       		break;
    //       }
    //   }else{
    //   	  $this->assign('search_type','');
    //   	  $this->assign('search_text','');
    //   }
    //       if($request->get('original_sp_id')){
    //       	$map['original_sp_id'] = $request->get('original_sp_id');
    //       	 $this->assign('original_sp_id',$request->get('original_sp_id'));

    //        $name=db::name('operator')->field('operator_name')->where("operator_id={$map['original_sp_id']}")->find();
    //          $search.='   运营商：'.$name['operator_name'];
    //       }else{
    //       	$this->assign('original_sp_id','');
    //       }
    //       if($request->get('area_id')){
    //       	$map['area_id'] = $request->get('area_id');
    //       	 $this->assign('area_id',$request->get('area_id'));

    // $name=db::name('region')->field('region_name')->where("id={$map['area_id']}")->find();
    // 			$search.='  区域：'.$name['region_name'];
    //       }else{
    //       	$this->assign('area_id','');
    //       }

    //       if($request->get('base_station_state')){
    //       	$map['base_station_state'] = $request->get('base_station_state');
    //       	 $this->assign('base_station_state',$request->get('base_station_state'));
    //       	 $name=db::name('station_statuscate')->field('cate_name')->where("cate_id={$map['base_station_state']}")->find();
    //       	 if($request->get('base_station_state')>100){
    // 		$search.='  基站状态：'.'中国铁塔  '.$name['cate_name'];
    //       	 }else{
    //       	 	$search.='  基站状态：'.'中国移动  '.$name['cate_name'];
    //       	 }

    //       }else{
    //       	$this->assign('base_station_state','');
    //       }
    //             Session::set('map',$map);
    //             if($map){
    //             	$name=Session::get('user');
    //             	$log=json_encode($search);
    // 	activity_log($name['name'],$log,$type=1);
    //             }
    // $map['page'] = $request->get('page')?$request->get('page'):1;
    // $map['page_size'] = $request->get('page_size')?$request->get('page_size'):10;

    // $url=config('database.geturl')."v1/baseStation_list";
    // $reqstr = "";
    // foreach ($map as $k => $v) {
    // $reqstr.= "{$k}={$v}&";
    // }

    // 	$reqstr = substr($reqstr, 0, -1);
    // 	$geturl=$url.'?'.$reqstr;
    //           $result=json_decode(curlGet($geturl),true);
    //           //运营商接口
    // 	$operatorurl=config('database.geturl')."v1/operator_list";
    // 	$operator= curlGet($operatorurl);
    // 	$operatorResult =json_decode($operator,true);
    //           //区域接口
    // 	$regionurl=config('database.geturl')."v1/region_list";
    // 	$regiondata= curlGet($regionurl);
    // 	$regionResult =json_decode($regiondata,true);
    // 	$regionResult =getTree($regionResult['data']);
    //  //基站状态接口
    // 	$basestatusurl=config('database.geturl')."v1/baseStationStatus_list";
    // 	$basestatusdata= curlGet($basestatusurl);
    // 	$basestatusResult =json_decode($basestatusdata,true);
    // 	$basestatusResult =getTreeStatus($basestatusResult['data']);
    // 	if($result['code']==20063){
    // 	  	 $this->success('没有查询到有关信息','/base_list');
    // 	}
    //           if($result['code']==200){
    //          array_walk($result['data'], function (&$v, $k, $p) {$v = array_merge($v, $p);}, array('page' => $map['page']));
    //            if($request->get('page')){
    // 		   return json($result['data']);
    // 		}
    // 		$this->assign('nowpage',$map['page']);
    // 		$this->assign('allpage',$result['total_page']);
    //           	    $this->assign('count',$result['total_count']);
    // 		$this->assign('operators',$operatorResult['data']);
    // 		$this->assign('regions',$regionResult);
    // 		$this->assign('basestatus',$basestatusResult);
    // 		$this->assign('page_size',$map['page_size']);
    //              return $this->fetch('base_list');
    //         }else{
    //            return  jsonErr('无收据');
    //         }

    // }
    public function base_list(Request $request){
        $map =[];
        //权限检测
        $user = Session::get('user');
        if(!$user['is_admin']){
            $permissionData=check_data();
            if($permissionData){
                $map[]=$permissionData;
            }
        }
        $search='搜索内容为：';
        $search_text = $request->get('search_text');
        $search_type = $request->get('search_type');
        if($search_text){
            switch ($search_type) {
                case '1':
                    $map[]=['station_name','like',"%$search_text%"];
                    $this->assign('search_text',$search_text);
                    $this->assign('search_type',$search_type);
                    $search.='  基站名称：'.$search_text;
                    break;
                case '2':
                    $map[]=['station_sp_code','like',"%$search_text%"];
                    $this->assign('search_text',$search_text);
                    $this->assign('search_type',$search_type);
                    $search.='  基站编号：'.$search_text;
                    break;
                case '3':
                    $map[]=['proj_number','like',"%$search_text%"];
                    $this->assign('search_text',$search_text);
                    $this->assign('search_type',$search_type);
                    $search.='  项目编号：'.$search_text;
                    break;
            }
        }else{
            $this->assign('search_type','');
            $this->assign('search_text','');
        }
        if($request->get('original_sp_id')){
            $map[]=['original_sp_id','=',$request->get('original_sp_id')];
            $this->assign('original_sp_id',$request->get('original_sp_id'));

            $name=db::name('operator')->field('operator_name')->where("operator_id={$request->get('original_sp_id')}")->find();
            $search.='   运营商：'.$name['operator_name'];
        }else{
            $this->assign('original_sp_id','');
        }
        if($request->get('area_id')){
            $map[]=['area_id','=',$request->get('area_id')];
            $this->assign('area_id',$request->get('area_id'));

            $name=db::name('region')->field('region_name')->where("id={$request->get('area_id')}")->find();
            $search.='  区域：'.$name['region_name'];
        }else{
            $this->assign('area_id','');
        }
        if($request->get('base_station_state')){
            $map[]=['base_station_state','=',$request->get('base_station_state')];
            $this->assign('base_station_state',$request->get('base_station_state'));
            $name=db::name('station_statuscate')->field('cate_name')->where("cate_id={$request->get('base_station_state')}")->find();
            if($request->get('base_station_state')>100){
                $search.='  基站状态：'.'中国铁塔  '.$name['cate_name'];
            }else{
                $search.='  基站状态：'.'中国移动  '.$name['cate_name'];
            }

        }else{
            $this->assign('base_station_state','');
        }
        $map[] = ['status','=',1];
        Session::set('map',$map);
        if($map){
            $name=Session::get('user');
            $log=json_encode($search);
            activity_log($name['name'],$log,$type=1);
        }
        $page = $request->get('page')?$request->get('page'):1;
        $limit = $request->get('limit')?$request->get('limit'):10;
        $BaseStation= new BaseStationModel;
        $BaseStationList= $BaseStation->selectBaseStationList($map,$page,$limit);
        $BaseStationCount= $BaseStation->selectBaseStationCount($map);
        if($request->get('page')){
            return ['code'=>0,'msg'=>'','count'=>$BaseStationCount,'data'=>$BaseStationList];
        }
        // 运营商接口
        $operatorurl=config('database.geturl')."v1/operator_list";
        $operator= curlGet($operatorurl);
        $operatorResult =json_decode($operator,true);
        //区域接口
        $regionurl=config('database.geturl')."v1/region_list";
        $regiondata= curlGet($regionurl);
        $regionResult =json_decode($regiondata,true);
        $regionResult =getTree($regionResult['data']);
        //基站状态接口
        $basestatusurl=config('database.geturl')."v1/baseStationStatus_list";
        $basestatusdata= curlGet($basestatusurl);
        $basestatusResult =json_decode($basestatusdata,true);
        $basestatusResult =getTreeStatus($basestatusResult['data']);
        $this->assign('operators',$operatorResult['data']);
        $this->assign('regions',$regionResult);
        $this->assign('basestatus',$basestatusResult);
        return $this->fetch('base_list');
    }
    //明细
    public function base_detai(Request $request){

        $data['station_id'] = $request->get('id');
        $url=config('database.geturl')."v1/getOneBaseStation";
        $deresult=json_decode(httpPOST($url,$data),true);
        if($deresult['code']==200){

            $this->assign('data',$deresult['data']);
            return $this->fetch('base_detai');
        }else{
            return  jsonErr('无收据');
        }

    }
    public function base_detai_contract(Request $request){

        $proj_number = $request->get('proj_number');
        $url="http://36.110.39.211:8989/gonn/Home/Index/index/number/{$proj_number}";
        $opts = array(
            'http'=>array(
                'method'=>"GET",
                'timeout'=>3,
            )
        );
        $context = stream_context_create($opts);
        $result =file_get_contents($url, true, $context);
        $arr=json_decode($result,true);
        if($arr['code']==500){
            return ['code'=>0,'msg'=>'未查询合同相关信息','data'=>''];
        }
        if($arr['code']==200){
            $cloudUrl = "http://36.110.39.211/k3cloud/";
            $number=$arr['message'][0]['fbillno'];
            //登陆参数

            $data = array(
                '5865e29cb51ecf',//帐套Id
                'administrator',//用户名
                'kJyGjIQ5DW0tpd@B',//密码
                '2052'//语言标识359010087
            );
            //定义记录Cloud服务端返回的Session17070038
            $cookie_jar = tempnam('/tmp','CloudSession');
            $post_content = self::create_postdata($data);
            $result = self::invoke_login($cloudUrl,$post_content,$cookie_jar);
            $data_model = "{'CreateOrgId': 0,'Number':$number,'Id': ''}";
            $data = array(
                'PAEZ_001',//业务对象标识FormId
                $data_model//具体Json字串
            );

            $post_content = self::create_postdata($data);
            $result = self::invoke_save($cloudUrl,$post_content,$cookie_jar);

            $array = json_decode($result,true);
            if($array['Result']["ResponseStatus"]["ErrorCode"]==500){
                die;
            }

            if(empty($array['Result']['Result']['ContractInfor'])){
                die;
            }

            foreach ($array['Result']['Result']['ContractInfor'] as $key => $value) {
                $Result[]=$value;
                $Result[$key]['abc']=$array['Result']['Result']['Id'];
                $Result[$key]['name1'] =$value["F_ContractID"]["MultiLanguageText"][0]['Name'];
                $Result[$key]['ContactCompany'] =$value["F_AContactCompany"]["MultiLanguageText"][0]['Name'];
                $Result[$key]['Number'] =$value["F_ContractID"]["Number"];
                $Result[$key]['contractId'] =$value["Id"];

                $Result[$key]['F_SignDate'] =substr($value["F_SignDate"],0,strpos($value["F_SignDate"], 'T'));

            }
            return ['code'=>0,'msg'=>'','count'=>100,'data'=>$Result];

            // $this->assign('result',$Result);
        }
    }
    public function base_contract(Request $request){

        $id = $request->get('id');

        $contractid = $request->get('contractid');
        $cloudUrl = "http://36.110.39.211/k3cloud/";
        //登陆参数
        $data = array(
            '5865e29cb51ecf',//帐套Id
            'administrator',//用户名
            'kJyGjIQ5DW0tpd@B',//密码
            '2052'//语言标识359010087
        );
        //定义记录Cloud服务端返回的Session
        $cookie_jar = tempnam('/tmp','CloudSession');
        $post_content = self::create_postdata($data);
        $result = self::invoke_login($cloudUrl,$post_content,$cookie_jar);
        $data_model = "{'CreateOrgId':0,'Number':'','Id':$id}";

        $data = array(
            'PAEZ_001',//业务对象标识FormId
            $data_model//具体Json字串
        );
        $list=array();
        //合同性质  合同名称   合同编号   合同金额  往来单位类型  签订日期  持续时间
        $post_content = self::create_postdata($data);
        header("Content-type: text/html; charset=utf8");
        $result = self::invoke_save($cloudUrl,$post_content,$cookie_jar);
        $array = json_decode($result,true);
        if($array['Result']["ResponseStatus"]["ErrorCode"]==500){
            $res='服务端异常';
            $this->assign('mess',$res);
            return $this->fetch('base_contract');die;
        }
        if(empty($array['Result']['Result']['ContractInfor'])){
            $res='请求超时，请检查网络后重试';
            $this->assign('mess',$res);

            return $this->fetch('base_contract');die;
        }


        $Revenue=[];
        foreach ($array['Result']['Result']['ContractInfor'] as $key => $value) {
            if($value['Id']==$contractid){
                $Result[]=$value;
                $Result[$key]['name1'] =$value["F_ContractID"]["MultiLanguageText"][0]['Name'];
                $Result[$key]['ContactCompany'] =$value["F_AContactCompany"]["MultiLanguageText"][0]['Name'];
                $list['contract_name']=$Result[$key]['name1'];
            }
        }
        $list['id']=$Result[0]['Id'];
        $list['Number']=$Result[0]["F_ContractID"]["Number"];
        $list['ContactCompan']=$Result[0]["F_AContactCompany"]["MultiLanguageText"][0]['Name'];
        $list['Name']=$Result[0]["F_ContractID"]["MultiLanguageText"][0]['Name'];
        $list['F_SignDate']= substr($Result[0]['F_SignDate'],0,strrpos($Result[0]['F_SignDate'],'T'));
        $list['F_ContractPeriod']=$Result[0]['F_ContractPeriod'];
        $list['F_AConNature']=$Result[0]['F_AConNature'];
        if($list['F_AConNature']==1){
            foreach ($Result[0]['FSubCostInforEntity'] as $ky => $ve) {
                $Revenue[]=$ve;
                $Revenue[$ky]['leixing'] =$ve["F_CostType"]["MultiLanguageText"][0]['Name'];
                $Revenue[$ky]['moshi'] =$ve["F_CostModel"]["MultiLanguageText"][0]['Name'];
                $Revenue[$ky]['FCostsureDate'] =substr($ve["FCostsureDate"],0,strpos($ve["FCostsureDate"], 'T'));
            }
        }else{
            foreach ($Result[0]['FSubRevenueEntity'] as $ke => $vo) {
                $Revenue[]=$vo;
                $Revenue[$ke]['leixing'] =$vo["F_IncomeType"]["MultiLanguageText"][0]['Name'];
                $Revenue[$ke]['moshi'] =$vo["F_IncomeModel"]["MultiLanguageText"][0]['Name'];
                $Revenue[$ke]['FSureDate'] =substr($vo["FSureDate"],0,strpos($vo["FSureDate"], 'T'));
            }
        }
        $list['F_AContactCompanyType']=$Result[0]['F_AContactCompanyType'];
        $list['bianhao']=$Result[0]['F_AContactCompany']['Number'];

        $list['F_ConAmount']=$Result[0]['F_ConAmount'];
        $list['F_AConStarDate']=substr($Result[0]['F_AConStarDate'],0,strpos($Result[0]['F_AConStarDate'], 'T'));
        $list['F_AConEndDate']=substr($Result[0]['F_AConEndDate'],0,strpos($Result[0]['F_AConEndDate'], 'T'));
        $list['F_ContractPeriod']=$Result[0]['F_ContractPeriod'];
        //进项税额
        $list['F_TDInputTax']=$Result[0]['F_TDInputTax'];
        $this->assign('Revenue',$Revenue);

        $this->assign('list',$list);
        return $this->fetch('base_contract');
    }

    public function basestation_add(Request $request){
        if($request->isPost()){
            $data=$this->request->param();
            $url=config('database.geturl')."v1/baseStation_add";
            $result=json_decode(httpPOST($url,$data),true);
            if($result['code'] == 200){
                return  jsonSuc('添加基站信息成功');
            }else{
                return	jsonErr('添加基站信息失败');
            }
        }else{
            //运营商接口
            $businessurl=config('database.geturl')."v1/business_list";
            $business= curlGet($businessurl);
            $businessResult =json_decode($business,true);
            //业务线接口
            $operatorurl=config('database.geturl')."v1/operator_list";
            $operator= curlGet($operatorurl);
            $operatorResult =json_decode($operator,true);
            //区域接口
            $regionurl=config('database.geturl')."v1/region_list";
            $regiondata= curlGet($regionurl);
            $regionResult =json_decode($regiondata,true);
            //基站状态接口
            $basestatusurl=config('database.geturl')."v1/baseStationStatus_list";
            $basestatusdata= curlGet($basestatusurl);
            $basestatusResult =json_decode($basestatusdata,true);
            $this->assign('business',$businessResult['data']);
            $this->assign('operators',$operatorResult['data']);
            $this->assign('regions',$regionResult['data']);
            $this->assign('basestatus',$basestatusResult['data']);
            return $this->fetch('base_add');
        }

    }
    public function basestation_geteditinfo(Request $request){

        $data['station_id'] = $request->get('id');
        $url=config('database.geturl')."v1/getOneBaseStation";
        $deresult=json_decode(httpPOST($url,$data),true);
        if($deresult['code']==200){
            $this->assign('data',$deresult['data']);
            return $this->fetch('base_edit');
        }else{
            return  jsonErr('无收据');
        }
    }
    public function basestation_edit(Request $request){
        $data=$this->request->post();
        unset($data['frequency_band']);
        unset($data['type']);
        unset($data['construction_number']);
        unset($data['department_id']);
        unset($data['three_level_manager']);
        unset($data['salesman']);
        $url=config('database.geturl')."v1/baseStation_edit";
        $result=json_decode(httpPOST($url,$data),true);
        if($result['code'] == 200){
            return  jsonSuc('修改基站基础信息成功');
        }else{
            return	jsonErr('修改基站基础信息失败');
        }
    }
    public function basestation_map(Request $request){
        $id= $request->get('station_name');
        $db = Db::connect([
            'type'            => 'mysql',
            'hostname'        => '127.0.0.1',
            'database'        => 'jzdb',
            'username'        => 'root',
            'password'        => 'pvv5718J0CwhNfn4B^8^H3igPj#oiQkX',
            'hostport'        => '10036',
            'charset'         => 'utf8',
        ]);
        $data=$db->table('jz_message')->field('business_id')->where(['jz_name'=>$id])->find();
        $this->assign('data',$data['business_id']);
        return $this->fetch('base_map');
    }

    public function basestation_region(Request $request){
        $db = Db::connect([
            'type'            => 'mysql',
            'hostname'        => '127.0.0.1',
            'database'        => 'jzdb',
            'username'        => 'root',
            'password'        => 'pvv5718J0CwhNfn4B^8^H3igPj#oiQkX',
            'hostport'        => '10036',
            'charset'         => 'utf8',
        ]);
        $data['business_id']= $request->get('station_id');
        if(empty($data['business_id'])){
            return json_encode(['code'=>5001,'message'=>'ID不能为空']);
        }
        $data=$db->table('jz_message')->field('jz_name,project_code,jz_code,jz_longitude,jz_latitude')->where(['business_id'=>$data['business_id']])->find();

        return json($data);

    }

    public function base_region(Request $request){
        $search_text = $request->get('search_text');
        $search_type = $request->get('search_type');
        if($search_text){
            switch ($search_type) {
                case '1':
                    $map['station_name']=$search_text;
                    $this->assign('search_text',$map['station_name']);
                    $this->assign('search_type',$search_type);
                    break;
                case '2':
                    $map['station_sp_code']=$search_text;
                    $this->assign('search_text',$map['station_sp_code']);
                    $this->assign('search_type',$search_type);
                    break;
                case '3':
                    $map['proj_number']=$search_text;
                    $this->assign('search_text',$map['proj_number']);
                    $this->assign('search_type',$search_type);
                    break;

            }
        }else{
            $this->assign('search_type','');
            $this->assign('search_text','');
        }
        if($request->get('original_sp_id')){
            $map['original_sp_id'] = $request->get('original_sp_id');
            $this->assign('original_sp_id',$request->get('original_sp_id'));
        }else{
            $this->assign('original_sp_id','');
        }
        if($request->get('area_id')){
            $map['area_id'] = $request->get('area_id');
            $this->assign('area_id',$request->get('area_id'));
        }else{
            $this->assign('area_id','');
        }
        if($request->get('base_station_state')){
            $map['base_station_state'] = $request->get('base_station_state');
            $this->assign('base_station_state',$request->get('base_station_state'));
        }else{
            $this->assign('base_station_state','');
        }
        //运营商接口
        $operatorurl=config('database.geturl')."v1/operator_list";
        $operator= curlGet($operatorurl);
        $operatorResult =json_decode($operator,true);
        //区域接口
        $regionurl=config('database.geturl')."v1/region_list";
        $regiondata= curlGet($regionurl);
        $regionResult =json_decode($regiondata,true);
        $regionResult =getTree($regionResult['data']);
        //基站状态接口
        $basestatusurl=config('database.geturl')."v1/baseStationStatus_list";
        $basestatusdata= curlGet($basestatusurl);
        $basestatusResult =json_decode($basestatusdata,true);
        $basestatusResult =getTreeStatus($basestatusResult['data']);

        $this->assign('operators',$operatorResult['data']);
        $this->assign('regions',$regionResult);
        $this->assign('basestatus',$basestatusResult);
        return $this->fetch('base_region');
    }

    public function base_regiondata(Request $request){
        $map =[];
        $user = session('user');
        $admi[]=['name','=',$user['name']];
        $name=db::name('admin')->where($admi)->find();
        if(empty($name)){
            $map['user_id'] =$user['user_id'];
        }

        $search_text = $request->get('search_text');
        $search_type = $request->get('search_type');
        if($search_text){
            switch ($search_type) {
                case '1':
                    $map['station_name']=$search_text;
                    $this->assign('search_text',$map['station_name']);
                    $this->assign('search_type',$search_type);
                    break;
                case '2':
                    $map['station_sp_code']=$search_text;
                    $this->assign('search_text',$map['station_sp_code']);
                    $this->assign('search_type',$search_type);
                    break;
                case '3':
                    $map['proj_number']=$search_text;
                    $this->assign('search_text',$map['proj_number']);
                    $this->assign('search_type',$search_type);
                    break;

            }
        }else{
            $this->assign('search_type','');
            $this->assign('search_text','');
        }
        if($request->get('original_sp_id')){
            $map['original_sp_id'] = $request->get('original_sp_id');
            $this->assign('original_sp_id',$request->get('original_sp_id'));
        }else{
            $this->assign('original_sp_id','');
        }
        if($request->get('area_id')){
            $map['area_id'] = $request->get('area_id');
            $this->assign('area_id',$request->get('area_id'));
        }else{
            $this->assign('area_id','');
        }
        if($request->get('base_station_state')){
            $map['base_station_state'] = $request->get('base_station_state');
            $this->assign('base_station_state',$request->get('base_station_state'));
        }else{
            $this->assign('base_station_state','');
        }
        $map['status']=1;
        $url=config('database.geturl')."v1/getbaseMapList";
        $reqstr = "";
        foreach ($map as $k => $v) {
            $reqstr.= "{$k}={$v}&";
        }
        $reqstr = substr($reqstr, 0, -1);
        $geturl=$url.'?'.$reqstr;
        $result=json_decode(curlGet($geturl),true);
        return json($result['data']);
    }

    //数据导入
    public function base_import(Request $request){
        $file = request()->file('file_name');
        $upload = new Upload($file,2);
        $result = $upload->upload();
        $fileurl=$_SERVER['DOCUMENT_ROOT'].$result['src'];
        $excel = new Excel;
        $result=$excel->baseExecl($fileurl);
        if($result['code']==200){
            return jsonSuc('导入成功!  总导入条数为 '.$result['data']['total'].' 条'.'  成功导入条数为  '.$result['data']['suc']. '  条'.'   失败'.$result['data']['er']. '条');
        }elseif($result['code']==7){
            return jsonErr($result['message']);
        }elseif($result['code']==8){
            return jsonErr($result['message']);
        }else{
            return jsonErr('模板格式错误');
        }

    }

    //登陆
    static     function invoke_login($cloudUrl,$post_content,$cookie_jar)
    {
        $loginurl = $cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc';
        return self::invoke_post($loginurl,$post_content,$cookie_jar,TRUE);
    }

    //查询
    static    function invoke_save($cloudUrl,$post_content,$cookie_jar)
    {
        $invokeurl = $cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc';
        return self::invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
    }

    //审核
    static     function invoke_audit($cloudUrl,$post_content,$cookie_jar)
    {
        $invokeurl = $cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc';
        return self::invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
    }

    static   function invoke_post($url,$post_content,$cookie_jar,$isLogin)
    {
        $ch = curl_init($url);

        $this_header = array(
            'Content-Type: application/json',
            'Content-Length: '.strlen($post_content)
        );

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_content);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if($isLogin){
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
        }
        else{
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
        }
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }

    //构造Web API请求格式
    static     function create_postdata($args) {
        $postdata = array(
            'format'=>1,
            'useragent'=>'ApiClient',
            'rid'=>self::create_guid(),
            'parameters'=>$args,
            'timestamp'=>date('Y-m-d'),
            'v'=>'1.0'
        );
        return json_encode($postdata);
    }

    //生成guid
    static    function create_guid() {
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = chr(123)// "{"
            .substr($charid, 0, 8).$hyphen
            .substr($charid, 8, 4).$hyphen
            .substr($charid,12, 4).$hyphen
            .substr($charid,16, 4).$hyphen
            .substr($charid,20,12)
            .chr(125);// "}"
        return $uuid;
    }
}
