Fastexport.php 44.0 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use addons\fastexport\library\ExportLib;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

/**
 * 数据导出管理
 *
 * @icon fa fa-circle-o
 */
class Fastexport extends Backend
{

    /**
     * FastExport模型对象
     * @var \app\admin\model\FastExport
     */
    protected $model = null;
    protected $database = [];
    public $save_dir = RUNTIME_PATH . 'fastexport' . DS;// 临时文件保存位置(每次开始任务时清空)
    public $fastExportDir;// ZIP包保存位置(重新初始化/删除任务时删除ZIP包)

    public function _initialize()
    {
        parent::_initialize();
        $rootPath            = str_replace('/', DS, ROOT_PATH);
        $this->model         = new \app\admin\model\FastExport;
        $this->database      = config('database');
        $this->fastExportDir = $rootPath . 'public' . DS . 'fastexport';

        $tableList = [];
        $list      = Db::query("SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema = ? ", [$this->database['database']]);

        // 需要排除的数据表
        $rule_out_table = [
            $this->database['prefix'] . 'config',
            $this->database['prefix'] . 'version',
            // $this->database['prefix'] . 'admin',
            $this->database['prefix'] . 'user_token',
            $this->database['prefix'] . 'fast_auth_group_access',
        ];

        foreach ($list as $key => $row) {
            if (!in_array($row['TABLE_NAME'], $rule_out_table)) {
                $tableList[] = [
                    'name'    => $row['TABLE_NAME'],
                    'comment' => $row['TABLE_COMMENT'] ? str_replace('表', '', $row['TABLE_COMMENT']) : '',
                ];
            }
        }

        $this->view->assign("tableList", $tableList);
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $pk       = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();

            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {

                    // 同时删除临时文件和zip包
                    if (is_dir($this->save_dir . $v->id)) {
                        $this->emptydir($this->save_dir . $v->id, true);
                    }

                    if ($v->file) {
                        $file_name = explode(DS, str_replace('/', DS, $v->file));
                        if (file_exists($this->fastExportDir . DS . end($file_name))) {
                            unlink($this->fastExportDir . DS . end($file_name));
                        }
                    }

                    $count += $v->delete();
                }

                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    /**
     * 测试任务-导出前10条数据
     * @param int $ids [description]
     */
    public function testTask($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $ExportLib = new ExportLib($row);

        try {

            $export_number  = $row['export_number'] ? $row['export_number'] : $ExportLib->buildSql('count');
            $xls_max_number = ($export_number >= $row['xls_max_number']) ? $row['xls_max_number'] : $export_number;
            if (!$export_number) {
                $this->error('没有数据需要导出~');
            }

            $min = 0;
            $max = 10;

            $subtask = [
                1 => [
                    'id'     => 1,
                    'status' => 0,// 0准备好,1进行中,2完成,3失败
                    'min'    => $min,
                    'max'    => $max,
                    'sql'    => $ExportLib->buildSql('limit', [$min, $max]),
                ],
            ];

            // 测试sql
            Db::query($subtask[1]['sql']);

            $row->subtask = $subtask;
            $row->save();

        } catch (PDOException $e) {
            $this->error($e->getMessage());
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('导出任务测试准备好~', null, [
            'direct_export' => true,
            'subtask'       => $subtask,
            'id'            => $ids,
        ]);
    }

    /**
     * 开始任务->初始化任务配置
     * @return bool
     */
    public function performTask($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $ExportLib = new ExportLib($row);

        try {

            $export_number  = $row['export_number'] ? $row['export_number'] : $ExportLib->buildSql('count');
            $xls_max_number = ($export_number >= $row['xls_max_number']) ? $row['xls_max_number'] : $export_number;
            if (!$export_number) {
                $this->error('没有数据需要导出~');
            }

            $subtask       = [];
            $subtask_count = ceil($export_number / $xls_max_number);// 子任务数量
            for ($i = 1; $i <= $subtask_count; $i++) {

                if ($i == 1) {
                    $min = 0;
                } else {
                    $min = ($i - 1) * $xls_max_number;
                }

                $subtask[$i] = [
                    'id'     => $i,
                    'status' => 0,// 0准备好,1进行中,2完成,3失败
                    'min'    => $min,
                    'max'    => $xls_max_number,
                    'sql'    => $ExportLib->buildSql('limit', [$min, $xls_max_number]),
                ];

                // 测试sql
                if ($i == 1) {
                    Db::query($subtask[$i]['sql']);
                }
            }

            if ($subtask_count > 1) {
                // 清理任务临时文件目录
                if (!is_dir($this->save_dir . $ids)) {
                    mkdir($this->save_dir . $ids, 0777, true);
                } else {
                    $this->emptydir($this->save_dir . $ids);
                }

                $row->progress = 5;
            } else {
                $row->progress = 0;
            }

            // 删除上次任务的zip包
            if ($row->file) {

                if ($row->file) {
                    $file_name = explode(DS, str_replace('/', DS, $row->file));
                    if (file_exists($this->fastExportDir . DS . end($file_name))) {
                        unlink($this->fastExportDir . DS . end($file_name));
                    }
                }
                $row->file = '';
            }

            $row->subtask = $subtask;
            $row->save();

        } catch (PDOException $e) {
            $this->error($e->getMessage());
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('导出任务初始化成功!', null, [
            'direct_export' => ($subtask_count == 1) ? true : false,
            'subtask'       => $subtask,
            'id'            => $ids,
        ]);
    }

    /**
     * 子任务控制器
     * @param  [type] $task_id 任务ID
     */
    public function taskControl($task_id = null)
    {
        $row = $this->model->get($task_id);
        if (!$row) {
            $this->error(__('任务找不到啦~'));
        }

        if (!isset($row['subtask']) || !is_array($row['subtask'])) {
            $this->error('子任务找不到啦,请重新开始任务~');
        }
        $row = $row->toArray();

        // 子任务状态:0准备好,1进行中,2完成,3失败
        foreach ($row['subtask'] as $key => $value) {
            switch ($value['status']) {
                case '0':
                    $row['subtask'][$key]['status_text'] = '等待中';
                    break;
                case '1':
                    $row['subtask'][$key]['status_text'] = '进行中';
                    break;
                case '2':
                    $row['subtask'][$key]['status_text'] = '已完成';
                    break;

                default:
                    $row['subtask'][$key]['status_text'] = '失败';
                    break;
            }
        }

        $current_page = 1;
        $subtask_page = [];
        foreach ($row['subtask'] as $key => $value) {
            $value['status']               = 0;// 用户可能会刷新任务控制页面,js将重新确定状态
            $subtask_page[$current_page][] = $value;
            if (count($subtask_page[$current_page]) >= $row['xls_create_concurrent']) {
                $current_page++;
            }
        }

        $subtask_count                = count($row['subtask']);
        $row['item_subtask_progress'] = round(92 / $subtask_count, 2);
        $row['progress']              = 5;

        $this->assignconfig('subtask_page', $subtask_page);
        $this->assignconfig('task', $row);
        $this->view->assign("task", $row);
        return $this->view->fetch();
    }

    /**
     * 执行自定义导出
     * @param int $task_id 任务ID
     * @param int $subtask_id 子任务ID
     * @param boolean $direct_export 是否直接反回文件
     */
    public function zdyTask()
    {
        $task_id       = 1;
        $subtask_id    = 1;
        $direct_export = true;
        \think\Config::set('default_return_type', 'json');
        $row = $this->model->get($task_id);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if (isset($row['subtask'][$subtask_id]) && is_array($row['subtask'][$subtask_id])) {
            $subtask   = $row['subtask'][$subtask_id];
            $task_name = $row['name'];
        } else {
            $this->result(['subtask_id' => $subtask_id], 0, '子任务找不到啦~', 'json');
        }

        set_time_limit(0);// 脚本执行时间限制
        ini_set('memory_limit', $row['memory_limit'] . 'M');// 脚本内存限制
        $ExportLib = new ExportLib($row);
        unset($row);

        // 检查任务状态
        if (!$direct_export) {
            if ($subtask['status'] == 1) {
                $this->result(['subtask_id' => $subtask_id], 0, '此子任务正在执行中~', 'json');
            } else if ($subtask['status'] == 2) {
                if (file_exists($this->save_dir . $task_id . DS . $subtask_id . '.xlsx')) {
                    $this->result(['subtask_id' => $subtask_id], 1, '此子任务已经处理过啦~', 'json');
                }
            }
        }

        $spreadsheet = new Spreadsheet();
        $worksheet   = $spreadsheet->getActiveSheet();
        $worksheet->setTitle($task_name);

        //设置表头
        $head       = $ExportLib->getXlsTitle();
        $head_count = count($head);
        for ($i = 0; $i < $head_count; $i++) {
            $worksheet->setCellValueByColumnAndRow($i + 1, 1, $head[$i]);
        }

        // 写人数据
        try {
            $data = Db::query($subtask['sql']);
        } catch (PDOException $e) {
            $this->result([
                'subtask_id' => $subtask_id,
                'error_msg'  => $e->getMessage(),
            ], 0, '任务失败!', 'json');
        }

        $y = 2;
        foreach ($data as $row_key => $row) {
            $i = 1;
            foreach ($ExportLib->fields as $key => $value) {

                $field = $value['field'];

                if ($value['discerns'] == 0) {
                    // 文本
                    $worksheet->setCellValueByColumnAndRow($i, $y, $row[$field], DataType::TYPE_STRING);
                } else if ($value['discerns'] == 1) {
                    // 数字
                    $worksheet->setCellValueByColumnAndRow($i, $y, $row[$field] . "\t");
                } else if ($value['discerns'] == 2) {
                    // 日期时间
                    if ($row[$field]) {

                        $excelDateValue = date('Y-m-d H:i:s', $row[$field]);
                        $worksheet->setCellValueByColumnAndRow($i, $y, $excelDateValue, DataType::TYPE_STRING);

                        /*$excelDateValue = PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $row[$field] );
                        if ($excelDateValue) {
                            // 以日期日期格式写入,没区别,有需要的可以打开
                            $worksheet->setCellValueByColumnAndRow($i, $y, $excelDateValue);
                            $worksheet->getStyleByColumnAndRow($i,$y)
                            ->getNumberFormat()
                            ->setFormatCode('yyyy-mm-dd h:mm:ss');
                            $worksheet->getStyleByColumnAndRow($i,$y)->getFont()->setBold(true);
                            $worksheet->getStyleByColumnAndRow($i,$y)->getFont()->setBold(false);
                        }*/
                    } else {
                        $worksheet->setCellValueByColumnAndRow($i, $y, '-', DataType::TYPE_STRING);
                    }
                } else if ($value['discerns'] == 3) {
                    // 图片-太慢,太耗资源,弃用
                    /*$file_info  = pathinfo($row[$field]);
                    $coorrow    = $worksheet->getCellByColumnAndRow($i, $y)->getRow();
                    $coorcolumn = $worksheet->getCellByColumnAndRow($i, $y)->getColumn();

                    if ($y == 2) {
                        $worksheet->getColumnDimension($coorcolumn)->setWidth(8);
                    }

                    $worksheet->getRowDimension($coorrow)->setRowHeight(40);

                    if (!empty($file_info['basename'])) {
                        $basename = $file_info['basename'];
                        $drawing  = new Drawing();
                        $drawing->setName('图片');
                        $drawing->setPath($remote_save_dir . $row[$field]);
                        $drawing->setWidth(40);
                        $drawing->setHeight(40);
                        $drawing->setCoordinates($coorcolumn . $coorrow);
                        $drawing->setWorksheet($worksheet);
                    } else {
                        $worksheet->setCellValueByColumnAndRow($i, $y, $row[$field], DataType::TYPE_STRING);
                    }*/

                    // 以链接导出图片
                    $field_value = cdnurl($row[$field], true);
                    $worksheet->setCellValueByColumnAndRow($i, $y, $field_value, DataType::TYPE_STRING);
                    $worksheet->getCellByColumnAndRow($i, $y)->getHyperlink()->setUrl($field_value);

                } else if ($value['discerns'] == 4) {
                    // 文件
                    $field_value = cdnurl($row[$field], true);
                    $worksheet->setCellValueByColumnAndRow($i, $y, $field_value, DataType::TYPE_STRING);
                    $worksheet->getCellByColumnAndRow($i, $y)->getHyperlink()->setUrl($field_value);
                } else if ($value['discerns'] == 5) {
                    // 赋值
                    $field_value = $ExportLib->assignment($row[$field], $value['scheme']);
                    $worksheet->setCellValueByColumnAndRow($i, $y, $field_value, DataType::TYPE_STRING);
                }

                $i++;
            }
            $y++;
            unset($data[$row_key]); // 能节约一点内存
        }

        // xls文件处理
        if ($direct_export) {
            // 直接下载
            ob_end_clean();
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
            header("Content-Type:application/force-download");
            header("Content-Type:application/vnd.ms-execl");
            header("Content-Type:application/octet-stream");
            header("Content-Type:application/download");

            $task_name        = $task_id . '.' . $task_name . '.xlsx';
            $encoded_filename = urlencode($task_name);
            $ua               = $_SERVER["HTTP_USER_AGENT"];
            if (preg_match("/MSIE/", $ua)) {
                header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
            } else if (preg_match("/Firefox/", $ua)) {
                header('Content-Disposition: attachment; filename*="utf8\'\'' . $task_name . '"');
            } else {
                header('Content-Disposition: attachment; filename="' . $task_name . '"');
            }
            header("Content-Transfer-Encoding:binary");
            header('Cache-Control: max-age=0');// 禁止缓存

            $writer = new Xlsx($spreadsheet);
            $writer->save('php://output');
            $spreadsheet->disconnectWorksheets();
            unset($spreadsheet);
        } else {
            // 保存
            $writer = new Xlsx($spreadsheet);
            $writer->save($this->save_dir . $task_id . DS . $subtask_id . '.xlsx');

            // 设置任务状态
            $result = false;
            Db::startTrans();
            try {
                // 读取最新的 subtask 数据
                $row      = $this->model->get($task_id);
                $progress = $row->progress + round(92 / count($row['subtask']), 2);
                $progress = ($progress > 100) ? 100 : $progress;

                if (isset($row['subtask'][$subtask_id]) && is_array($row['subtask'][$subtask_id])) {
                    $subtask                        = $row['subtask'];
                    $subtask[$subtask_id]['status'] = 2;
                    $row->subtask                   = $subtask;
                    $row->progress                  = $progress;
                    $row->save();
                    $result = true;
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();

                $this->result([
                    'subtask_id' => $subtask_id,
                    'error_msg'  => $e->getMessage(),
                ], 0, '任务失败!', 'json');

            } catch (Exception $e) {
                Db::rollback();

                $this->result([
                    'subtask_id' => $subtask_id,
                    'error_msg'  => $e->getMessage(),
                ], 0, '任务失败!', 'json');
            }

            $spreadsheet->disconnectWorksheets();
            unset($spreadsheet);

            if ($result) {
                $this->result(['subtask_id' => $subtask_id], 1, '子任务处理成功!', 'json');
            } else {
                $this->result(['subtask_id' => $subtask_id], 0, '子任务找不到啦~', 'json');
            }
        }
    }

    /**
     * 执行子任务
     * @param int $task_id 任务ID
     * @param int $subtask_id 子任务ID
     * @param boolean $direct_export 是否直接反回文件
     */
    public function performSubTask($task_id, $subtask_id, $direct_export = false)
    {
        \think\Config::set('default_return_type', 'json');
        $row = $this->model->get($task_id);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if (isset($row['subtask'][$subtask_id]) && is_array($row['subtask'][$subtask_id])) {
            $subtask   = $row['subtask'][$subtask_id];
            $task_name = $row['name'];
        } else {
            $this->result(['subtask_id' => $subtask_id], 0, '子任务找不到啦~', 'json');
        }

        set_time_limit(0);// 脚本执行时间限制
        ini_set('memory_limit', $row['memory_limit'] . 'M');// 脚本内存限制
        $ExportLib = new ExportLib($row);
        unset($row);

        // 检查任务状态
        if (!$direct_export) {
            if ($subtask['status'] == 1) {
                $this->result(['subtask_id' => $subtask_id], 0, '此子任务正在执行中~', 'json');
            } else if ($subtask['status'] == 2) {
                if (file_exists($this->save_dir . $task_id . DS . $subtask_id . '.xlsx')) {
                    $this->result(['subtask_id' => $subtask_id], 1, '此子任务已经处理过啦~', 'json');
                }
            }
        }

        $spreadsheet = new Spreadsheet();
        $worksheet   = $spreadsheet->getActiveSheet();
        $worksheet->setTitle($task_name);

        //设置表头
        $head       = $ExportLib->getXlsTitle();
        $head_count = count($head);
        for ($i = 0; $i < $head_count; $i++) {
            $worksheet->setCellValueByColumnAndRow($i + 1, 1, $head[$i]);
        }

        // 写人数据
        try {
            $data = Db::query($subtask['sql']);
        } catch (PDOException $e) {
            $this->result([
                'subtask_id' => $subtask_id,
                'error_msg'  => $e->getMessage(),
            ], 0, '任务失败!', 'json');
        }

        $y = 2;
        foreach ($data as $row_key => $row) {
            $i = 1;
            foreach ($ExportLib->fields as $key => $value) {

                $field = $value['field'];

                if ($value['discerns'] == 0) {
                    // 文本
                    $worksheet->setCellValueByColumnAndRow($i, $y, $row[$field], DataType::TYPE_STRING);
                } else if ($value['discerns'] == 1) {
                    // 数字
                    $worksheet->setCellValueByColumnAndRow($i, $y, $row[$field] . "\t");
                } else if ($value['discerns'] == 2) {
                    // 日期时间
                    if ($row[$field]) {

                        $excelDateValue = date('Y-m-d H:i:s', $row[$field]);
                        $worksheet->setCellValueByColumnAndRow($i, $y, $excelDateValue, DataType::TYPE_STRING);

                        /*$excelDateValue = PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $row[$field] );
                        if ($excelDateValue) {
                            // 以日期日期格式写入,没区别,有需要的可以打开
                            $worksheet->setCellValueByColumnAndRow($i, $y, $excelDateValue);
                            $worksheet->getStyleByColumnAndRow($i,$y)
                            ->getNumberFormat()
                            ->setFormatCode('yyyy-mm-dd h:mm:ss');
                            $worksheet->getStyleByColumnAndRow($i,$y)->getFont()->setBold(true);
                            $worksheet->getStyleByColumnAndRow($i,$y)->getFont()->setBold(false);
                        }*/
                    } else {
                        $worksheet->setCellValueByColumnAndRow($i, $y, '-', DataType::TYPE_STRING);
                    }
                } else if ($value['discerns'] == 3) {
                    // 图片-太慢,太耗资源,弃用
                    /*$file_info  = pathinfo($row[$field]);
                    $coorrow    = $worksheet->getCellByColumnAndRow($i, $y)->getRow();
                    $coorcolumn = $worksheet->getCellByColumnAndRow($i, $y)->getColumn();

                    if ($y == 2) {
                        $worksheet->getColumnDimension($coorcolumn)->setWidth(8);
                    }

                    $worksheet->getRowDimension($coorrow)->setRowHeight(40);

                    if (!empty($file_info['basename'])) {
                        $basename = $file_info['basename'];
                        $drawing  = new Drawing();
                        $drawing->setName('图片');
                        $drawing->setPath($remote_save_dir . $row[$field]);
                        $drawing->setWidth(40);
                        $drawing->setHeight(40);
                        $drawing->setCoordinates($coorcolumn . $coorrow);
                        $drawing->setWorksheet($worksheet);
                    } else {
                        $worksheet->setCellValueByColumnAndRow($i, $y, $row[$field], DataType::TYPE_STRING);
                    }*/

                    // 以链接导出图片
                    $field_value = cdnurl($row[$field], true);
                    $worksheet->setCellValueByColumnAndRow($i, $y, $field_value, DataType::TYPE_STRING);
                    $worksheet->getCellByColumnAndRow($i, $y)->getHyperlink()->setUrl($field_value);

                } else if ($value['discerns'] == 4) {
                    // 文件
                    $field_value = cdnurl($row[$field], true);
                    $worksheet->setCellValueByColumnAndRow($i, $y, $field_value, DataType::TYPE_STRING);
                    $worksheet->getCellByColumnAndRow($i, $y)->getHyperlink()->setUrl($field_value);
                } else if ($value['discerns'] == 5) {
                    // 赋值
                    $field_value = $ExportLib->assignment($row[$field], $value['scheme']);
                    $worksheet->setCellValueByColumnAndRow($i, $y, $field_value, DataType::TYPE_STRING);
                }

                $i++;
            }
            $y++;
            unset($data[$row_key]); // 能节约一点内存
        }

        // xls文件处理
        if ($direct_export) {
            // 直接下载
            ob_end_clean();
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
            header("Content-Type:application/force-download");
            header("Content-Type:application/vnd.ms-execl");
            header("Content-Type:application/octet-stream");
            header("Content-Type:application/download");

            $task_name        = $task_id . '.' . $task_name . '.xlsx';
            $encoded_filename = urlencode($task_name);
            $ua               = $_SERVER["HTTP_USER_AGENT"];
            if (preg_match("/MSIE/", $ua)) {
                header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
            } else if (preg_match("/Firefox/", $ua)) {
                header('Content-Disposition: attachment; filename*="utf8\'\'' . $task_name . '"');
            } else {
                header('Content-Disposition: attachment; filename="' . $task_name . '"');
            }
            header("Content-Transfer-Encoding:binary");
            header('Cache-Control: max-age=0');// 禁止缓存

            $writer = new Xlsx($spreadsheet);
            $writer->save('php://output');
            $spreadsheet->disconnectWorksheets();
            unset($spreadsheet);
        } else {
            // 保存
            $writer = new Xlsx($spreadsheet);
            $writer->save($this->save_dir . $task_id . DS . $subtask_id . '.xlsx');

            // 设置任务状态
            $result = false;
            Db::startTrans();
            try {
                // 读取最新的 subtask 数据
                $row      = $this->model->get($task_id);
                $progress = $row->progress + round(92 / count($row['subtask']), 2);
                $progress = ($progress > 100) ? 100 : $progress;

                if (isset($row['subtask'][$subtask_id]) && is_array($row['subtask'][$subtask_id])) {
                    $subtask                        = $row['subtask'];
                    $subtask[$subtask_id]['status'] = 2;
                    $row->subtask                   = $subtask;
                    $row->progress                  = $progress;
                    $row->save();
                    $result = true;
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();

                $this->result([
                    'subtask_id' => $subtask_id,
                    'error_msg'  => $e->getMessage(),
                ], 0, '任务失败!', 'json');

            } catch (Exception $e) {
                Db::rollback();

                $this->result([
                    'subtask_id' => $subtask_id,
                    'error_msg'  => $e->getMessage(),
                ], 0, '任务失败!', 'json');
            }

            $spreadsheet->disconnectWorksheets();
            unset($spreadsheet);

            if ($result) {
                $this->result(['subtask_id' => $subtask_id], 1, '子任务处理成功!', 'json');
            } else {
                $this->result(['subtask_id' => $subtask_id], 0, '子任务找不到啦~', 'json');
            }
        }
    }

    /**
     * 打包ZIP
     */
    public function taskFilePack($task_id)
    {
        $row = $this->model->get($task_id);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if (!isset($row['subtask']) || !is_array($row['subtask'])) {
            $this->result(null, 0, '打包失败,任务找不到啦~', 'json');
        }

        if ($row['file']) {
            $this->result(['file_name' => $row['file']], 1, '此任务已经打过包了!', 'json');
        }

        is_dir($this->fastExportDir) or mkdir($this->fastExportDir, 0755, true);

        foreach ($row['subtask'] as $key => $subtask) {
            if (!file_exists($this->save_dir . $task_id . DS . $subtask['id'] . '.xlsx')) {
                $this->result(null, 0, '子任务未处理完毕!', 'json');
            }
        }

        $task_dir = $this->save_dir . $task_id . DS;// 导出任务的临时文件目录
        $zipname  = $task_id . '.export_' . \fast\Random::alnum(6) . '.zip';
        $zipurl   = request()->domain() . DS . 'fastexport' . DS . $zipname;// 绝对地址,以便各处直接点击下载
        $zipname  = $this->fastExportDir . DS . $zipname;

        $zip = new \ZipArchive();
        $res = $zip->open($zipname, \ZipArchive::CREATE);

        if ($res === TRUE) {
            $dh = opendir($task_dir);
            while ($file = readdir($dh)) {
                if ($file != "." && $file != "..") {
                    $fullpath = $task_dir . $file;
                    if (!is_dir($fullpath)) {
                        $zip->addFile($fullpath, $file);
                    }
                }
            }
            closedir($dh);
        }

        $zip->close();
        $row->file     = $zipurl;
        $row->progress = 100;
        $row->save();

        $this->result(['file_name' => $zipurl], 1, '文件打包完成!', 'json');
    }

    /**
     * 获取字段列表、字段注释、表主键
     * @internal
     */
    public function getTableFields($table = null)
    {
        if ($this->request->isAjax()) {
            $table = $this->request->request('table');
        }

        //从数据库中获取表字段信息
        $sql = "SELECT * FROM `information_schema`.`columns` "
            . "WHERE TABLE_SCHEMA = ? AND table_name = ? "
            . "ORDER BY ORDINAL_POSITION";

        //加载主表的列
        $columnList        = Db::query($sql, [$this->database['database'], $table]);
        $fieldlist_comment = [];

        foreach ($columnList as $index => $item) {
            $fieldlist_comment[$item['COLUMN_NAME']] = $this->FieldComment($item['COLUMN_NAME'], $item['DATA_TYPE'], $item['COLUMN_COMMENT']);
        }

        if ($this->request->isAjax()) {
            $this->success("", null, ['fieldlist_comment' => $fieldlist_comment]);
        } else {
            return $fieldlist_comment;
        }
    }

    /**
     * 字段分析
     * @param String $field 字段名称
     * @param String $data_type 字段数据类型
     * @param String $comment 字段注释
     * @return Array  可用的字段数据
     */
    protected function FieldComment($field, $data_type, $comment)
    {
        // 字段类型识别
        $data_type_int = [
            'tinyint',
            'int',
            'smallint',
            'mediumint',
            'integer',
            'bigint',
        ];

        $discerns = 0;

        if (in_array($data_type, $data_type_int)) {
            $discerns = 1;// 数字
        }

        if (preg_match("/time$/i", $field)) {
            $discerns = 2;// 日期时间
        } else if (preg_match("/image$|avatar$/i", $field)) {
            $discerns = 4;// 3为图片-导出慢弃用
        } else if (preg_match("/file$/i", $field)) {
            $discerns = 4;// 文件
        }

        /*else if (preg_match("/list$|data$|switch$/i", $field)) {
            $discerns = 5;// 赋值
        }*/


        // 字段标题和赋值分析
        if (!$comment) {
            return [
                'discerns' => $discerns,
                'name'     => $field,
                'comment'  => '',
            ];
        }

        $comment = str_replace(',', ',', $comment);
        $comment = str_replace(['(多选)', '(单选)', '(多选)', '(单选)'], '', $comment);

        if (stripos($comment, ':') !== false && stripos($comment, ',') && stripos($comment, '=') !== false) {
            list($field_name, $field_comment) = explode(':', $comment);
            return [
                'discerns' => 5,
                'name'     => $field_name ? $field_name : $field,
                'comment'  => $field_comment,
            ];
        } else {

            if (preg_match("/switch$/i", $field)) {
                $discerns     = 5;// 赋值
                $comment_temp = '0=关闭,1=开启';
            }

            return [
                'discerns' => $discerns,
                'name'     => $comment ? $comment : $field,
                'comment'  => isset($comment_temp) ? $comment_temp : '',
            ];
        }
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with(['admin'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['admin'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach ($list as $row) {

                $row->getRelation('admin')->visible(['username']);
            }
            $list   = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if ($params) {
                $params = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;

                $params['admin_id'] = $this->auth->id;// 记录导出人
                if (isset($params['where_field']['op']) && is_array($params['where_field']['op'])) {
                    foreach ($params['where_field']['op'] as $key => $value) {
                        $params['where_field']['op'][$key] = trim($value);
                    }
                }

                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name     = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                        $this->model->validateFailException(true)->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params                = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;
                $params['join_table']  = isset($params['join_table']) ? $params['join_table'] : [];
                $params['where_field'] = isset($params['where_field']) ? $params['where_field'] : [];
                if (isset($params['where_field']['op']) && is_array($params['where_field']['op'])) {
                    foreach ($params['where_field']['op'] as $key => $value) {
                        $params['where_field']['op'][$key] = trim($value);
                    }
                }

                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name     = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $row                      = $row->toArray();
        $row['join_table_number'] = count($row['join_table']);

        // 源表字段列表
        $table_field = $this->getTableFields($row['main_table']);

        // 准备源表和关联表的所有字段-带表名-供筛选和排序
        $allField = [];
        foreach ($table_field as $key => $value) {
            $field_name            = $row['main_table'] . '.' . $key;
            $value['field_name']   = $field_name . ' - ' . $value['name'];
            $allField[$field_name] = $value;
        }

        // 要导出的字段
        $fields = [];
        if ($row['field_config'] && isset($row['field_config']['title'])) {
            foreach ($row['field_config']['title'] as $key => $value) {
                $fields[$key]['title']    = $value;
                $fields[$key]['discerns'] = $row['field_config']['discerns'][$key];
                $fields[$key]['scheme']   = $row['field_config']['scheme'][$key];
            }
        }
        $row['fields'] = $fields;

        // 源表和关联表的字段列表透传数据准备
        $join_field_count = 0;
        if (isset($row['join_table'])) {
            foreach ($row['join_table'] as $key => $value) {
                $join_table_name = isset($value['table']) ? $value['table'] : false;
                if ($join_table_name) {

                    $row['join_table'][$key]['field_list'] = $join_table_field[$join_table_name] = $this->getTableFields($join_table_name);

                    $join_table_name_as = $value['join_as'] ? $value['join_as'] : $join_table_name;
                    foreach ($join_table_field[$join_table_name] as $join_field_key => $join_field_value) {
                        $field_name                     = $join_table_name_as . '.' . $join_field_key;
                        $join_field_value['field_name'] = $field_name . ' - ' . $join_field_value['name'];
                        $allField[$field_name]          = $join_field_value;
                    }

                }

                // 关联表取值字段准备
                $field_name_arr        = [];
                $field_name_arr_select = [];
                if (isset($row['join_table'][$key]['fields']) && isset($row['join_table'][$key]['fields']['title'])) {
                    foreach ($row['join_table'][$key]['fields']['title'] as $fkey => $fvalue) {
                        $field_name_arr[$fkey]['title']    = $fvalue;
                        $field_name_arr[$fkey]['discerns'] = $row['join_table'][$key]['fields']['discerns'][$fkey];
                        $field_name_arr[$fkey]['scheme']   = $row['join_table'][$key]['fields']['scheme'][$fkey];
                        $field_name_arr_select[]           = $fkey;
                    }
                }
                $row['join_table'][$key]['field_name_arr']        = $field_name_arr;
                $row['join_table'][$key]['field_name_arr_select'] = $field_name_arr_select;// 供selectpicker初始化
                $join_field_count                                 += count($field_name_arr_select);
            }
        }

        $row['where_field_arr_select'] = [];
        $row['where_field_arr']        = [];
        if ($row['where_field'] && isset($row['where_field']['op'])) {
            foreach ($row['where_field']['op'] as $key => $value) {
                $row['where_field_arr_select'][] = $key;
                $row['where_field_arr'][$key]    = [
                    'op'        => $value,
                    'condition' => $row['where_field']['condition'][$key],
                ];
            }
        }

        $this->assignconfig('fields', [
            'table_field'       => $table_field,
            'join_table_field'  => isset($join_table_field) ? $join_table_field : [],
            'join_table_number' => $row['join_table_number'],
            'field_count'       => count($row['fields']) + $join_field_count,
        ]);

        $this->view->assign("row", $row);
        $this->view->assign("allField", $allField);
        $this->view->assign("table_field", $table_field);
        return $this->view->fetch();
    }

    public function emptydir($dir, $del_current_dir = false)
    {
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . DS . $file;
                if (!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    $this->emptydir($fullpath, true);
                }
            }
        }
        closedir($dh);

        // 删除当前文件夹
        if ($del_current_dir) {
            rmdir($dir);
        }
    }
}