<?php
class  Service_GenerateBarcode{
   
    public static function splitBarCodeValue($barCodeValue, $delimiter = '|') {
        $barCodeValue = str_replace(array("\r", "\n"), '', $barCodeValue);
        $barCodeValue = trim($barCodeValue);
    
        if ('' === $barCodeValue) {
            return '请提供条码数值';
        }
    
        $barCodeValue  = explode($delimiter, $barCodeValue);
       // $filterCode = array();
    
        foreach ($barCodeValue as $key => $value) { 
            if (strlen(trim($value)) > 15){
                return '输入的数值中含有超过15个字符限制';
            }
            if ('' === trim($value)) {
                unset($barCodeValue[$key]);
            }
           // elseif (!isset($filterCode[$value])) {
            else {
                $barCodeValue[$key]    = trim($value);
                //$filterCode[$value] = 1;
            }
           /*  else {
                unset($barCodeValue[$key]);
            } */
        }
   
        $total = count($barCodeValue);
    
        if ($total > 0 && $total <= 10) {
            return $barCodeValue;
        }
    
        return ($total > 10) ? '最多只接收10个条码数值' : '请提供条码数值';
    }
    
    public static function getCacheDir() {
        return str_replace('\\', '/', realpath(APPLICATION_PATH . '/../data/cache')) . '/';
    }
    
    public static function buildCache($file) {
        $response = array('status' => 0, 'message' => '','data'=>'');
    
        if (!is_file($file)) { // 判断文件是否存在
            $response['message'] = '缓存文件【' . $file . '】不存在';
    
            return $response;
        }
    
        $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); // 文件扩展名
    
        if (!in_array($ext, array('xls', 'xlsx'), TRUE)) { // 允许的文件类型
            $response['message'] = '文件类型【' . $ext . '】错误，仅支持xls或xlsx类型文件';
            @unlink($file); // 删除文件
    
            return $response;
        }
    
        require_once APPLICATION_PATH . '/../libs/PHPExcel.php';
        require_once APPLICATION_PATH . '/../libs/PHPExcel/IOFactory.php';
    
        set_time_limit(0); // 不限制执行时间
        ini_set('memory_limit', '2048M'); // 2G内存限制
    
        $loaderObj = PHPExcel_IOFactory::load($file); // 加载Excel文件
    
        if (!$loaderObj) {
            $response['message'] = '文件解析失败，请确保所上传的文件类型为xls或xlsx';
            @unlink($file); // 删除文件
    
            return $response;
        }
    
        $sheetObj = $loaderObj->getSheet(0); // 获取第一个工作表
        $maxCols  = PHPExcel_Cell::columnIndexFromString($sheetObj->getHighestColumn()); // 最大列数
        $maxRows  = $sheetObj->getHighestRow(); // 最大行数
        $cacheDir = self::getCacheDir();
        $repeat   = array(); // 缓存条码数值，排查重复条码数值
      
        for ($i = 2; $i <= $maxRows; ++$i) { // 排除表头，从第2行开始读取
            $barCodeValue = trim($sheetObj->getCell('A' . $i)->getValue());
     
            if ('' !== $barCodeValue) {
               /*  if (isset($repeat[$barCodeValue])) {
                    continue ;
                } */
                if (strlen(trim($barCodeValue)) > 15){
                   $response['message'] = '输入的数值中含有超过15个字符限制';
                   break;
                }
                if (@preg_match('/[\x80-\xff]+/', trim($barCodeValue))){
                   $response['message'] = '输入的数值中含有中文';
                   break;
                }
                $repeat[] = $barCodeValue;
            }
        }
        if(empty($response['message'])){
            $response['status'] = 1;
            $response['data'] = $repeat;
        }
        unset($repeat);
        @unlink($file); // 删除文件
    
        return $response;
    }
}