<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 15-11-25
 * Time: 上午10:24
 * To change this template use File | Settings | File Templates.
 */
class Ship_RemoteController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "ship/views/";
        $this->serviceClass = new Service_Remote();
    }

    public function listAction(){
        if ($this->_request->isPost()) {
            $page = $this->_request->getParam('page', 1);
            $pageSize = $this->_request->getParam('pageSize', 20);

            $page = $page ? $page : 1;
            $pageSize = $pageSize ? $pageSize : 20;

            $return = array(
                "state" => 0,
                "message" => "No Data"
            );

            $params = $this->_request->getParams();

            $condition = $this->serviceClass->getMatchFields($params);
            unset($params);
            $count = $this->serviceClass->getByCondition($condition, 'count(*)');
            $return['total'] = $count;

            if ($count) {
                $showFields=array(
                    'r_id',
                    'city_name',
                    'country_id',
                    'country_name_en',
                    'postcode_no',
                    'postcode_no_end',
                    'sm_id',
                    'sm_code',
                    'warehouse_id',
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $rows = $this->serviceClass->getByCondition($condition,$showFields, $pageSize, $page);
                foreach($rows as $key=>$row){
                    $warehouse = Service_Warehouse::getByField($row['E7'],"warehouse_id","*");
                    $rows[$key]['warehouse_code'] = $warehouse['warehouse_code'];
                }
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        $this->view->sm = Service_ShippingMethod::getByCondition(array('sm_status'=>'1'),"*");
        $this->view->warehouse = Service_Warehouse::getAll();
        $this->view->country = Service_Country::getAll();
        echo Ec::renderTpl($this->tplDirectory . "remote.tpl", 'layout');
    }

    public function uploadTemplateAction(){
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT');
        header('Cache-Control: no-cache, must-revalidate');
        header('Pragma: no-cache');
        $fullPath = APPLICATION_PATH."/../data/xls".'/remote.xls';
        //方法2
        $filename = basename($fullPath);
        header("Content-Type: APPLICATION/OCTET-STREAM");
        //Force the download
        $header="Content-Disposition: attachment; filename=".$filename.";";
        header($header );
        //     header("Content-Transfer-Encoding: binary");
        //     header("Content-Length: ".$len);
        echo file_get_contents($fullPath);
        exit;
    }

    public function batchUploadAction(){
        $result = array("ask"=>1,"message"=>"",'error'=>array(),'data'=>array());
        $xlsData = Service_Product::getXLSData($_FILES,'filename');
        if(count($xlsData[0])!=6||($xlsData[0][0]!="国家"||$xlsData[0][1]!="城市"||$xlsData[0][2]!="邮政编码起"||$xlsData[0][3]!="邮政编码止"||$xlsData[0][4]!="运输方式"||$xlsData[0][5]!="仓库")){
            $result['ask'] = "0";
            $result['error'][] = "文件格式错误，首行不是按照国家,城市,邮政编码起,邮政编码止,运输方式,仓库,排列!";
            die(json_encode($result));
        }

        //print_r($xlsData);exit;
        foreach($xlsData as $key=>$value){
            if($key==0) continue;
            $existError = 0;
            $country_code = trim($value[0]);
            $city_name = trim($value[1]);
            $postcode_no = trim($value[2]);
            $postcode_no_end = trim($value[3]);
            $sm_code = trim($value[4]);
            $warehouse_code = trim($value[5]);

            $resultData['message'] = "";
            if($country_code==""){
                $existError = 1;
                $resultData['message'] .= "国家为空;";
            }
            if($sm_code==""){
                $existError = 1;
                $resultData['message'] .= "运输方式为空;";
            }
            if($warehouse_code==""){
                $existError = 1;
                $resultData['message'] .= "仓库为空;";
            }
            if($existError) {
                $resultData['ok'] = '0';
                $result['data'][] = $resultData;
                continue;
            }

            $country = Service_Country::getByField($country_code,"country_code","*");
            if(empty($country)){
                $resultData['message'] .= "国家简码[".$country_code."]不存在";
                $resultData['ok'] = '0';
                $result['data'][] = $resultData;
                continue;
            }
            $warehouse = Service_Warehouse::getByField($warehouse_code,"warehouse_code","*");
            if(empty($warehouse)){
                $resultData['message'] .= "仓库[".$warehouse_code."]不存在";
                $resultData['ok'] = '0';
                $result['data'][] = $resultData;
                continue;
            }
            $smRow = Service_ShippingMethod::getByCondition(array('warehouse_id_arr'=>array(0,$warehouse['warehouse_id']),"sm_code"=>$sm_code),"*");
            if(empty($smRow)){
                $resultData['message'] .= "仓库[".$warehouse_code."]不存在运输方式[".$sm_code."]";
                $resultData['ok'] = '0';
                $result['data'][] = $resultData;
                continue;
            }
            $remoteRow = array(
                'city_name'=>$city_name,
                'country_id'=>$country['country_id'],
                'country_name_en'=>$country['country_name_en'],
                'postcode_no'=>$postcode_no,
                'postcode_no_end'=>$postcode_no_end,
                'sm_id'=>$smRow[0]['sm_id'],
                'sm_code'=>$sm_code,
                'warehouse_id'=>$warehouse['warehouse_id'],
            );
            $checkExist = Service_Remote::getByCondition($remoteRow,"count(*)");
            if($checkExist){
                $remoteRow['ok'] = '0';
                $remoteRow['message'] = "已存在";
                $remoteRow['warehouse_code'] = $warehouse_code;
                $result['data'][] = $remoteRow;
                continue;
            }else{
                $re = Service_Remote::add($remoteRow);
                if ($re) {
                    $remoteRow['ok'] = '1';
                    $remoteRow['message'] = "添加成功";
                    $remoteRow['warehouse_code'] = $warehouse_code;
                    $result['data'][] = $remoteRow;
                    continue;
                }else{
                    $remoteRow['ok'] = '0';
                    $remoteRow['message'] = "数据保存失败!";
                    $remoteRow['warehouse_code'] = $warehouse_code;
                    $result['data'][] = $remoteRow;
                    continue;
                }
            }
        }
        die(json_encode($result));
    }

}
