<?php
class Warehouse_GenerateBarcodeController extends Ec_Controller_Action{
    
    public function preDispatch(){
        $this->tplDirectory = "warehouse/views/default/";
    }
    
    public function listAction(){
        echo Ec::renderTpl($this->tplDirectory . "generate-barcode.tpl", 'layout');
    }
    
    public function generateAction(){ 
        $barCodeValue = '';
        $total = 0;
        $barCodeValue = $this->_request->getParam('barCodeValue', '');
        if ($barCodeValue) {
            $barCodeValueArray = Service_GenerateBarcode::splitBarCodeValue($barCodeValue);
            $response = array(
                'state' => 0,
                'total' => 0,
                'data' => array(),
                'msg' => '找不到相匹配的数据',
            );
            if (!is_array($barCodeValueArray)) {
                $response['msg'] = $barCodeValueArray;
    
                exit(json_encode($response));
            }
            $this->view->barCodeValueArray = $barCodeValueArray;
            echo Ec::renderTpl($this->tplDirectory . "show-barcode.tpl", 'layout');
        } 
    }
    
    public function downLoadTemplatAction(){
        $fullPath = APPLICATION_PATH."/../data/xls".'/batch-generate-template.xls';
        Common_Common::downloadFile($fullPath);
    }
    
    public function importBatchGenerateAction(){

        if (!$this->getRequest()->isPost()) {
            header('content-type: text/html; charset=utf-8');
            exit('非法请求：请求方式不是POST');
        }
        
        set_time_limit(0); // 不限执行时间
        ini_set('memory_limit', '2048M');
        
        $fileInfo = $_FILES['BarCodeValue_file'];
        $response = array(
            'status'=>0,
            'data'=>array(),
            'msg'=>''
        );
        $error = '';
        $table = '';
        
        if (0 !== $fileInfo['error']) {
            $error = '上传数据文件失败';
        
            switch ($fileInfo['error']) {
                case UPLOAD_ERR_INI_SIZE:
                    $error .= '：文件大小超过系统配置指定值' . ini_get('upload_max_filesize');
                    break;
                case UPLOAD_ERR_FORM_SIZE:
                    $error .= '：文件大小超过表单指定值（MAX_FILE_SIZE）';
                    break;
                case UPLOAD_ERR_PARTIAL:
                    $error .= '：文件上传不完整';
                    break;
                case UPLOAD_ERR_NO_FILE:
                    $error .= '：没有文件上传';
                    break;
                case UPLOAD_ERR_NO_TMP_DIR:
                    $error .= '：找不到临时目录';
                    break;
                case UPLOAD_ERR_CANT_WRITE:
                    $error .= '：文件写入失败';
                    break;
                default:
                    break;
            }
        } else {
            $ext = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
            $tmp = Service_GenerateBarcode::getCacheDir() . date('YmdHis-') . md5('ORDER-BATCHSEARCH-IMPORT-FILE-' . microtime(TRUE)) . '.' . $ext;
            if (!in_array(strtoupper($ext), array('XLS', 'XLSX'), TRUE)) {
                $error = '不受支持的文件类型：' . $ext;
            } elseif (!move_uploaded_file($fileInfo['tmp_name'], $tmp)) {
                $error = '移动上传条码数值数据文件时发生异常';
            } else {
        
                $result = Service_GenerateBarcode::buildCache($tmp);
        
                if (0 === $result['status']) { 
                    $error = $result['message'];
                } else { 
                    $response['data'] = $result['data'];
                    $response['status'] = 1;
                }
            }
        } 
        $response['msg'] = $error;// var_dump($response);
        exit(json_encode($response));
        //返回数据后，生成条码操作
        //$this->view->barCodeValueArray = $response['data'];
        //echo   Ec::renderTpl($this->tplDirectory . "show-barcode.tpl", 'layout');
    }
}