php 上传xsl文件,内容重复过滤

			//过滤后的数据  $excel_array xsl中的数据  $field_list 字段数据
            $final = CustomerService::filterRegular($excel_array, $field_list);

            $total = $final['total'];
            $successNum = count($final['final']);
            $failNum = count($final['info']);
            if ($total == 0) {
                return '上传文件内容不能为空';
            }
            if ($total > 200000) {
                return '本次上传名单数量为{$total},单次上传名单数量大于20万,请拆分后导入';
            }

            $head = ['公司名称', '联系人', '手机号', '客户类型', '所在城市', '所在区域', '责任业务员', '合作类型', '错误信息'];
            $fileInfo = self::exportExcel($head, $final['info']);
            $jobData = [
                'name' => '批量导入客户管理',
                'total' => $total,
                'success' => $successNum,
                'fail' => $failNum,
                'down_path' => $fileInfo['path'],
                'create_time' => time()
            ];

			//将上传文件记录数据库种
            Db::table('down')->insert($jobData);

            $lists = array_chunk($final['final'], 30000);
            foreach ($lists as $list) {
                $result = Db::table($table)->limit($limit)->insertAll($list);
                if (!$result) {
                    return '导入失败,请重新尝试';
                }
            }
	public static function filterRegular($excel_array,$field_list){
        $isMob = "/^1[345789]{1}\d{9}$/";
        $time     = time();
        $all_data = [];
        foreach ($excel_array as $key => $value) {
            $data = [];
            foreach ($field_list as $field_key => $field_value) {
                if (!isset($value[$field_key])) {
                    throw new RuntimeException('第 ' . ($key + 2) . ' 行第 ' . $field_key . ' 列缺少数据');
                }
                $data[$field_value] = $value[$field_key];
            }
            // 有创建时间和更新时间的处理
            $data['create_time'] = $time;
            $data['update_time'] = $time;

            $all_data[] = $data;
        }

        //检查号码
        $fileData = [];
        $errorInfos = [];
        $i = 0; //总条数
        foreach ($all_data as &$item) {
            if (!$item['company_name']) {
                $item['reason'] = '缺少公司信息';
                $errorInfos[] = $item;
            } elseif (!$item['tel']) {
                $item['reason'] = '缺少号码信息';
                $errorInfos[] = $item;
            } elseif (!preg_match($isMob, (string)$item['tel'])) {
                $item['reason'] = '该号码不正确,请修改后上传';
                $errorInfos[] = $item;
            } else {
                $fileData[] = $item;
            }
            $i++;
        }

        //过滤重复号码
        $selfFilter = self::checkUnique($fileData, 'tel');

        //过滤自身的重复数据
        $selfUnique = $selfFilter['unique'];//自身唯一的数据
        $selfRepeat = $selfFilter['repeat'];//从excel中过滤掉的自身重复的客户


        $errorInfos = array_merge($errorInfos, $selfRepeat);

        //最终的数据
        //todo $selfUnique
        //过滤的数据
        //todo (错误号码 $errorInfos) (自身唯一的数据 $selfRepeat) (excel自身 $selfRepeat)
        return ['final' => $selfUnique, 'info' => $errorInfos, 'total' => $i];

    }


    /**
     * 获得重复的元素和唯一的元素
     * @param $arr
     * @param $key
     * @return array[]
     */
    public static function checkUnique($arr, $key): array
    {
        $key_array = [];
        $temp_array = $arr;
        $repeat_array = [];

        foreach ($arr as $value) {
            if (!isset($temp_array[$value[$key]])) {
                $key_array[] = $value;
                $temp_array[$value[$key]] = $value[$key];
            } else {
                $value['reason'] = '该号码在excel表中重复';
                $repeat_array[] = $value;
            }
        }
        return ['unique' => $key_array, 'repeat' => $repeat_array];
    }
/**
     * 创建表格
     * @param $head
     * @param $body
     * @param string $title
     * @return array
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
    public static function exportExcel($head, $body, string $title = 'Sheet1')
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->setActiveSheetIndex(0);
        $char_index = range('A', 'Z');

        // 处理超过26列
        $a = 'A';
        foreach ($char_index as $item) {
            $char_index[] = $a . $item;
        }

        // Excel 表格头
        foreach ($head as $key => $val) {
            $sheet->setCellValue($char_index[$key] . '1', $val);
        }

        // Excel body 部分
        foreach ($body as $key => $val) {
            unset($val['create_time']);
            unset($val['update_time']);
            $row = $key + 2;
            $col = 0;
            foreach ($val as $v) {
                $sheet->setCellValue($char_index[$col] . $row, $v);
                $col++;
            }
        }

        $spreadsheet->getActiveSheet()->setTitle($title);

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . '批量导入客户管理' . '.xlsx"');
        header('Cache-Control: max-age=0');

        $dir = 'public/uploads/error/' . date('Ymd');

        $filePath = $dir . DIRECTORY_SEPARATOR . uniqid(time(), true) . '.xlsx';

        $savePath = app()->getRootPath() . $filePath;

        $objWriter = IOFactory::createWriter($spreadsheet, 'Xlsx');

        $objWriter->save($savePath);

        return ['path' => substr($filePath,6)];
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值