Match.php 51.7 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 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
<?php

namespace app\school\controller\match;

use app\common\controller\ManystoreBase;
use app\common\model\Member;
use app\common\model\Territory;
use app\school\controller\basic\User;
use app\school\model\basic\Teacher;
use app\school\model\match\MatchFormSet;
use app\school\model\match\MatchItems;
use app\school\model\match\MatchItemsDetails;
use app\school\model\match\MatchTags;
use app\school\model\match\MatchType;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use app\school\model\Manystore;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;


/**
 * 竞赛管理
 *
 * @icon fa fa-circle-o
 */
class Match extends ManystoreBase
{
    /**
     * Match模型对象
     * @var \app\school\model\match\Match
     */
    protected $model = null;
    protected $matchGroup = null;
    protected $idsx = 0;
    protected $ypDomain = 'http://chuangxinchuangye.posedu.com.cn/';
    protected $noNeedRight = ['*'];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\school\model\match\Match;
        $this->matchGroup = new \app\school\model\match\Matchgroup;
        $groups = $this->matchGroup->where('school_id',SCHOOL_ID)->select();
        $groups = collection($groups)->toArray();
        foreach ($groups as $k=>$val){
            $val['group_count'] =Db::name('match_items')->where(['match_group_id'=>$val['id']])->count();
//            $student_ids = Db::name('match_items')
//                ->alias('i')
//                ->join("fa_match_items_details d","d.match_items_id = i.id")
//                ->where(['i.match_group_id'=>$val['id']])
//                ->column('d.student_ids');
//            $student_ids_str = str_replace(',','',implode(",",$student_ids) );
//            $val['people_count'] = strlen($student_ids_str);
            $groups[$k] = $val;
        }
        $this->view->assign("matchLevelList", $this->model->getMatchLevelList());
        $this->view->assign("groups", $groups);
        $active  = request()->param('active','home');
        $this->idsx  = request()->param('idx','home');
        $match = \app\school\model\match\Match::get($this->idsx);
        $this->assign('match',$match);
        $this->assignconfig('active',$active);
        $this->assignconfig('id',$this->idsx);
    }


    public function index()
    {
        //设置过滤方法
        $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();
            $where = [];
            if($this->auth->rule_type == Manystore::IS_COLLEGE) {
                $where['college'] =$this->auth->college_id;
            }else {
                $where['school_id'] = SCHOOL_ID;
            }
            $params = [];
            $total = $this->matchGroup
                ->with('match')
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->matchGroup
                ->with(['match' => function($query) {
                    $andWhere = [];
                    if($this->auth->rule_type == Manystore::IS_COLLEGE) {
                        $andWhere = ['college_id' => $this->auth->college_id];
                    }
                    return  $query->where($andWhere)->order('createtime','desc');
                }])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $memberModel = new Member();
            foreach ($list  as $k=>&$val){
                $match = $val['match'];
                foreach ( $match as $key=>&$item) {
                    $people_count = [];
                    $item['match_type_str'] = '';
                    $item['people_count']  = $memberModel->where(['container_id' => $item['id'],'member_type' => Member::STUDENT,'member_source' => Member::MATCH])->count();
                    $item['group_count'] = $memberModel->where(['container_id' => $item['id'],'member_type' => Member::STUDENT,'member_source' => Member::MATCH,'is_captain' => Member::CAPTAIN_Y])->count();//参加竞赛组数
//                    $student_ids = Db::name('match_items_details')
//                        ->alias('d')
//                        ->join("fa_match_items i","i.id = d.match_items_id")
//                        ->where(['i.match_id'=>$item['id']])
//                        ->column('d.student_ids');
                    //赛道相关
                    $match_type = Db::name('match_type')->where(['match_id'=>$item['id']])->select();
                    $arr = [];
                    foreach ($match_type as $index => &$b) {
                        $number = (new MatchItems())->where(['match_type' => $b['id']])->count();
                        $arr[$index]['desc'] = $b['name'].':'.$number.'组';
                    }
                    $arr = array_column($arr,'desc');
                    $item['match_type_str'] = implode('>',$arr);;
                    $match[$key] = $item;
                }
                $val['match'] =  $match ;
                $list[$k] = $val;
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    
    //删除作品
    public function del_items()
    {
        $ids = request()->param('id');
        if($ids) {
            Db::startTrans();
            try {
                Db::name('match_items')->where(['id' => $ids])->delete();
                Db::name('members')->where(['item_id' => $ids,'member_source' => 1])->delete();
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error('删除失败请重试');
            }
            $this->success('删除成功');
        }
    }
    /**
     * [add 新增比赛  新增 赛道 新增 赛道 下的 表单(报名)]
     * @Author   七
     * @DateTime 2020-12-16-17:03
     * @return string
     */
    public function add()
    {
        $group_id  = request()->param('group_id');
        $this->assign('group_id',$group_id);
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $project_types = $this->request->param("project_types/a"); //赛道
            $type = $this->request->param("type/a"); //赛道

            if ($params) {
                if($params['match_type'] == 1 && !$project_types) {
                    $this->error('请添加类别');
                }
                $params = $this->preExcludeFields($params);

                if($this->storeIdFieldAutoFill && STORE_ID ){
                    $params['store_id'] = STORE_ID;
                }
                if($this->shopIdAutoCondition && SCHOOL_ID){
                    $params['school_id'] = SCHOOL_ID;
                }
                $params['is_school'] = $this->auth->rule_type;
                //如果是院级管理员
                if($this->auth->rule_type == Manystore::IS_COLLEGE){
                    $params['college_id'] = $this->auth->college_id;
                }
                if(isset($params['rule_resources']) && !empty($params['rule_resources'])) {
                    $params['rule_resources'] = json_encode($params['rule_resources'],true);
                }
                $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); //新增一场比赛
                    if($result !== false){
                        //比赛新增 要新加一个或多个 赛道,并根据赛道 进行添加 默认的 报名表单
                        $row = $this->model->get($this->model->id);
                        //判断是否为单一类别赛道
                        if($params['match_type'] == 1){
                            $matchType = $project_types;
                        }else{
                            $matchType[0] = [
                                'name' => '单类别竞赛',
                                'desc_content' => '单类别竞赛',
                                'id' => '',
                            ];
                        }
                        $been_fields = $type['been_fields'];
                        $this->editMatchType($row,$matchType,$been_fields);
                    }
                    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();
    }

    /**
     * [edit 修改比赛信息]
     * @Author   七
     * @DateTime 2021-01-04-13:26
     * @param null $ids
     * @return string
     */
    public function edit($ids = null)
    {
        $row = $this->model->where(array('id'=>$ids,'school_id'=>SCHOOL_ID))->find();
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if(isset($params['rule_resources']) && !empty($params['rule_resources'])) {
                $params['rule_resources'] = json_encode($params['rule_resources'],true);
            }

            $types = $this->request->post('type/a');

            if ($params) {
                $params = $this->preExcludeFields($params);

                extract($types);
                $match_types = json_decode($match_types , true);
                $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);

                    //print_r(json_encode($types));die;

                    //修改赛道信息
                    $this->editMatchType($row , $match_types , $been_fields);
                    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->getTrace());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


    public function getMatchType(){
        $match_id = request()->param('match_id',0);
        $types = MatchType::all(['match_id'=>$match_id]);
        $this->success('查询成功','',$types);
    }


    /**
     *  处理比赛赛道信息
     */
    public function editMatchType($row,$matchType,$been_fields){
        $adds = [];
        $save = [];
        $MatchType = new MatchType();
        $ids  = $MatchType->where('match_id',$row->id)->column('id');
        $ids2 = [];
        //循环赛道

        if(!empty($matchType)){
            foreach ($matchType as $k=>$v){
                $v['match_id'] = $row->id; //比赛id
                if ($v['id']){
                    $ids2[] = $v['id'];
                    $save[] = $v;
                    continue;
                }
                unset($v['id']);
                $adds[] = $v;
            }
        }

        $xids = array_diff($ids,$ids2); //获取 新增 与原有的 不同 删除
        if(!empty($xids)){
            foreach ($xids as $k=>$v){
                $del = MatchType::get($v);
                if ($del){
                    $del->delete();
                }
            }
        }
        if(!empty($save)){
            $MatchType->isUpdate(false)->saveAll($save);  //添加赛道
        }
        $addret = [];
        if(!empty($adds)){
            $addret = $MatchType->isUpdate(false)->saveAll($adds); //新增赛道
        }
        $formJson = json_decode(file_get_contents('assets/js/school/match/newFields.json', FILE_USE_INCLUDE_PATH), true);
        $formSetModel = new MatchFormSet();
        //单类别赛道,只创建一个赛道
        if ($addret){ //新增不为空 则 添加报名信息
            foreach (collection($addret)->toArray() as $k=>$add){
                $arr = [];
                $i = 0;
                //循环初始化的标签
                foreach ($formJson as $k => $v) {
                    $i ++;
                    $arr[$i]['match_type_id'] = $add['id'];
                    $arr[$i]['match_id']        = $row->id;
                    $arr[$i]['can_edit_data']   = '0';
                    $arr[$i]['project_starttime']   = 0;
                    $arr[$i]['project_endtime']     = 0;
                    $arr[$i]['school_id']       =  SCHOOL_ID;
                    $arr[$i]['stage_type']      = $k;

                    if($k == 'reg') {
                        $arr[$i]['stage_title'] ='报名';
                    }else if($k == 'defualt') {
                        $arr[$i]['stage_title'] ='自定义表单';
                    }else if($k == 'members') {
                        $arr[$i]['stage_title'] ='团队成员';
                    }else{
                        $arr[$i]['stage_title'] ='指导教师';
                    }
                    $arr[$i]['form_data'] =  json_encode($v,true);
                }
                $formSetModel->insertAll($arr);
              /*  $arr['match_type_id'] = $add['id'];
                $arr['match_id'] = $row->id;
                $arr['stage_title'] ='报名';
                $arr['stage_type'] ='reg';
                $arr['can_edit_data'] = '0';
                $arr['project_starttime'] = 0;
                $arr['project_endtime'] =  0;
                $arr['school_id'] =  SCHOOL_ID;
                $arr['form_data'] =  $been_fields;
                MatchFormSet::create($arr);*/
                $this->createTags($row->id,$add['id']);
            }
        }
    }

    /**
     * 创建比赛默认赛道标签
     */
    public function createTags($matchId,$matchTypeId)
    {
        $tagModel = new MatchTags();
	$tagInfo = $tagModel->where(['match_id'=>$matchId,'match_type_id' => $matchTypeId])->find();
        //如果没有标签信息  添加默认标签
        if(!$tagInfo) {
            $currentTime = time();
            for ($i=1; $i <=2; $i++)
            {
                $arr[$i]['tag_name']        = ($i == 1) ? '报名' : '淘汰';
                $arr[$i]['match_id']        = $matchId;
                $arr[$i]['match_type_id']   = $matchTypeId;
                $arr[$i]['default']         = 1;
                //$arr[$i]['createtime']      = $currentTime;
		        //$tagModel->insert($arr);
            }
		$tagModel->insertAll($arr);
        }
    }


    /**
     *  按赛道获取自定义表单组
     * @param  index
     */
    public function getFormTypes(){
        $MatchFormSet = new MatchFormSet();
        $index = request()->param('id',false);
        if (!$index) $this->error("系统错误");
        $formSets = $MatchFormSet->where('match_type_id',$index)->paginate(1000);
        $this->success('ok','',$formSets);
    }

    /**
     *  编辑比赛等级状态
     */
    public function change_match_level(){
        $params = request()->param();
        $ret = \app\school\model\match\Match::changeLevel($params);
        if (!$ret){
            $this->error("操作失败");
        }
        $this->success("操作成功");
    }


    /**
     *  新增比赛标签
     */
    public function newMatchTag(){
        $params = request()->param();
        extract($params);
        try {
            MatchTags::create([
                'tag_name'=>$tagValue,
                'match_id'=>$match_id,
                'match_type_id'=>$match_type_id,
                'school_id'=>SCHOOL_ID
            ]);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("标签新增成功");
    }

    /**
     *  获取比赛标签
     * @throws \think\exception\DbException
     */
    public function getMatchTags(){
        $matchItems = new MatchItems();

        $params = request()->param();
        // 根据比赛查询 比赛下的标签,中间还有一层赛道的概念
        extract($params);
        $tags = MatchTags::all(['match_type_id'=>$match_type_id]);
        //给定一个默认的 未设置标签 0
       /* $data = [
            'id' => 0,
            'match_id' => $match_id,
            'match_type_id' => $match_type_id,
            'school_id' => SCHOOL_ID,
            'tag_name' => '未设置标签',
        ];*/
        //查询未设置标签的 报名数
//        $data['item_count'] = $matchItems->where('match_type',$match_type_id)->where('match_tag_id',0)->count();
        //array_push($tags,$data);
        $map = [];
        if ($this->auth->rule_type == 1){
            //所属学院或部门
            $map['college_id'] =  $this->auth->college_id;
        }
        $data = [
            'id' => '',
            'match_id' => $match_id,
            'match_type_id' => $match_type_id,
            'school_id' => SCHOOL_ID,
            'tag_name' => '全部数据',
        ];
        //查询赛道下全部数据
//        $data['item_count'] = $matchItems->where($map)->where(['match_type'=>$match_type_id,'school_id' => SCHOOL_ID])->count();
        array_push($tags,$data);
//        $item_total_count  = $matchItems->where('match_id',$match_id)->count(); //比赛总人数
        $this->success("查询成功",'',['data'=>$tags /*, 'item_total_count'=>$data['item_count']*/]);
    }

    /**
     * [setMoveToTag 移动到标签]
     * [将报名数据 移动到选中标签内]
     * @Author   七
     * @DateTime 2021-01-04-14:35
     */
    public function setMoveToTag(){
        if($this->request->isAjax()){
            //获取传递的值
            $params = $this->request->param();
            $ids = $params['ids'];
            $data = [
                'match_tag_id' => $params['match_tag_id']
            ];
            $result = false;
            $matchItems = new MatchItems();
            Db::startTrans();
            try {
                foreach ($ids as $v){
                    $log = [
                        'match_items_id' => $v,
                        'match_tag_id'   => $params['match_tag_id'],
                        'createtime'     => time(),
                    ];
                    Db::name('match_tag_log')->insertGetId($log);
                    $row = $matchItems->get($v);
                    $result = $row->save($data); //修改归属标签
                }
                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('移动失败!');
    }

    /**
     * [match_container 竞赛容器]
     * @Author   七
     * @DateTime 2020-12-15-9:04
     * @param $idx  比赛id
     * @return string
     */
    public function match_container($idx){
        //查询竞赛 赛道 默认赛道
        $types = MatchType::all(['match_id'=>$idx]);
        list($first) = $types; //取第一个赛道 id

        $row = $this->model->where(array('id'=>$idx,'school_id'=>SCHOOL_ID))->find();
        $row['group_count']= Db::name("match_items")->where(['match_id'=>$row['id']])->count();
        $countRegister = (new Member())->where(['container_id' => $row['id'],'member_type' => Member::STUDENT,'member_source' => Member::MATCH])->count();
        $row['people_count'] = $countRegister;
        if(!empty($row) && $row['rule_resources']) {
            $row['rule_resources'] = json_decode($row['rule_resources'],true);
        }
        $this->view->assign('row',$row);
        $this->assignconfig('homeMatchTypeIndex',$first['id']);

        $active = $this->request->param('type');
        if(!empty($active)){
            //重新给定 需要显示的 tab 
            $this->assignconfig('active',$active);
        }
        return $this->view->fetch();
    }

    /**
     * [create_form 创建表单使用]
     * @Author   七
     * @DateTime 2020-12-14-15:16
     */
    public function create_form(){
        $MatchFormSet = new MatchFormSet();
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params['school_id'] = $this->auth->school_id;
                //创建空表单
                $params['can_edit_data'] = 0; //默认不可修改
                $params['icon_image'] = '/assets/img/school/'.$params['stage_type'].'.png'; //默认图标
                $json = json_decode(file_get_contents('assets/js/school/match/formData.json', FILE_USE_INCLUDE_PATH), true);
                $params['project_starttime'] = time();
                $params['project_endtime'] = time();

                switch($params['stage_type']) {
                    case 'teachers': //指导教师
                        $params['stage_title'] = '指导教师';
                        break;
                    case 'members': //团队
                        $params['stage_title'] = '团队';
                        break;
                    default: //自定义表单
                        $params['stage_title'] = '自定义表单';
                }
                $params['form_data'] = json_encode($json[$params['stage_type']],true);
                $result = false;
                Db::startTrans();
                try {
                    $result = $MatchFormSet->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', ''));
        }
        $this->view->assign('match_id',$this->request->param('match_id'));
        $this->view->assign('match_type_id',$this->request->param('match_type_id'));
        return $this->view->fetch();
    }

    /**
     * [formData 自定义表单]
     * @Author   七
     * @DateTime 2020-12-16-10:05
     */
    public function formData(){
        $stageType = $this->request->param('type');
        $formSetId = $this->request->param('form_set_id');
        try {
            $fieldsData = Db::name('match_form_set')->where(['id' => $formSetId])->find(); //查询已有的 表单项
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

        if ($this->request->isAjax()) {
            $data['form_data'] = $this->request->param('setting');

            $result = false;
            Db::startTrans();
            try {
                $result = Db::name('match_form_set')->where(['id' => $formSetId])->update($data);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if($result !== false){
                $this->success('修改成功!');
            }else{
                $this->error('修改失败!');
            }
        }

        $fieldsData = json_decode($fieldsData['form_data'], true);
        $this->assignconfig('form_type', $stageType);
        $this->assignconfig('fields_data', $fieldsData);
        $this->assignconfig('form_set_id', $formSetId);
        return $this->view->fetch();
    }

    /**
     * [setFormApplication 设置表单 应用]
     * @Author   七
     * @DateTime 2020-12-16-14:54
     */
    public function setFormApplication($ids = NULL){
        if ($this->request->isAjax()) {
            $rows = $this->request->param('row/a');
            $data = [];
            $data['stage_title'] = $rows['stage_title'];
            $data['can_edit_data'] = $rows['can_edit_data'];
            $data['project_starttime'] = empty($rows['project_starttime']) ? NULL : strtotime($rows['project_starttime']);
            $data['project_endtime'] = empty($rows['project_endtime']) ? NULL : strtotime($rows['project_endtime']);
            //剔除无用数据
            unset($rows['project_endtime']);
            unset($rows['project_starttime']);
            unset($rows['can_edit_data']);
            unset($rows['stage_title']);
            $matchInfo = (new MatchFormSet())->alias('f')->join('fa_match m','m.id = f.match_id','inner')->where('f.id',$ids)->find();

            if(!$matchInfo) {
                $this->error('比赛不存在');
            }
            if($data['project_starttime']) {
                if($data['project_starttime'] > $matchInfo['match_endtime']) {
                    $this->error('开始时间不能大于比赛结束时间');
                }
            }else {
                $data['project_starttime'] = $matchInfo['match_starttime'];
            }
            if($data['project_endtime']) {
                if($data['project_endtime'] < $matchInfo['match_starttime']) {
                    $this->error('结束时间不能小于比赛开始时间');
                }
                if($data['project_endtime'] > $matchInfo['match_endtime']) {
                    $this->error('结束时间不能大于比赛结束时间');
                }
            }else{
                $data['project_endtime'] = $matchInfo['match_endtime'];
            }
            $data['setting'] = json_encode($rows);
            Db::startTrans();
            try {
                Db::name('match_form_set')->where(['id' => $ids])->update($data);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('修改成功', '');
        }
        $row = $this->getFormSetData($ids);
        $this->assign('row', $row);
        return $this->view->fetch();
    }

    /**
     * 获取项目报名设置数据
     * @return Array
     */
    private function getFormSetData($ids = NULL) {
        $row = Db::name('match_form_set')->where(['id' => $ids])->find();
        if (!$row) {
            $this->error('没有找到该条记录');
        }
        $row['setting'] = json_decode($row['setting'], true);
        return $row;
    }


    /**
     *  数据
     */
    public function match_data(){
    }


    /**
     * [addMatchRegistration 新增比赛报名信息]
     * @Author   七
     * @DateTime 2020-12-18-10:28
     */
    public function addMatchRegistration(){
        $this->matchTypeModel = new MatchType(); //赛道模型
        $this->matchFormSetModel = new MatchFormSet(); //赛道下的表单模型
        $this->matchItemsModel = new MatchItems(); //报名表模型
        $this->matchItemsDetailsModel = new MatchItemsDetails(); //报名详情模型 (用于存储表单数据)
        $match_id = $this->request->param('match_id'); //比赛id
        $match_type_id = $this->request->param('match_type_id'); //赛道id
//        //根据赛道 查询 赛道下的 表单信息
//        $where = [
//            'school_id' => $this->auth->school_id, //取当前登录者id
//            'match_id' => $match_id,
//            'match_type_id' => $match_type_id,
//        ];
//        $form_list = $this->matchFormSetModel->where($where)->select(); //查询表单列表
//
//        if($this->request->isAjax()){
//            //新增报名信息  需要存储基础报名信息 以及 报名的 自定义菜单信息
//            $params = $this->request->param('row/a');//获取表单所有
//            $items_where = [
//                'school_id' => $this->auth->school_id, //取当前登录者id
//                'match_id' => $params['match_id'],
//                'match_type' => $params['match_type_id'],
//            ];
//            //首先 查看是否已经存在 此表单的 记录
//            $match_items_id = $this->matchItemsModel->where($items_where)->value('id');
//            if(empty($match_items_id)){ // 当查询不到 报名信息 则创建
//                $data = [ //组建报名信息
//
//                ];
//                $match_items_id =  $this->matchItemsModel->insertGetId($data); //新增返回主键id
//            }
//
//            //组建 报名详情 先进行查询 是否存在 表单的详情,存在则修改,不存在 则新增
//            $items_details_where = [ //新增数据
//
//            ];
//            $items_details_id = $this->matchItemsDetailsModel->where($items_details_where)->value('id');
//            //存储完 基础的报名相关信息 我们则将 报名的大部分 使用json的形式存储到 另一个关联表中
//            if(empty($items_details_id)){ //为空则新增
//
//            }else{ //走修改方法
//
//            }

//        }
//        $this->view->assign('form_list',$form_list); //报名需要填写信息表单列表
        return $this->view->fetch();
    }


    //联合查询 学校下的 学号是否存在 给定 固定姓名
    public function delFormSet(){
        $param = $this->request->param();
        $this->matchFormSetModel = new MatchFormSet(); //赛道下的表单模型
//        $row = $this->matchFormSetModel->where(['id' => $param['form_set_id']])->find();
        $row = $this->matchFormSetModel::get($param['form_set_id']);
//        dump( $this->matchFormSetModel::get($param['form_set_id']));die;
        if (!$row) {
            $this->error('没有找到该条记录');
        }
       $status =$row->delete();
        if($status){
            $this->success();
        }else{
            $this->error();
        }
    }

    /**
     * 赛事报名详情
     * @param $id
     */
    public function register_detail($id)
    {
        $itemModel = new MatchItems;
        $info = $itemModel
            ->field('i.*,u.username,u.entry_year,u.mobile,c.college_name,s.specialty_name,d.match_details,t.id as tag_id,t.tag_name')
            ->alias('i')
            ->join('match m','i.match_id = m.id','left')
            ->join('user u','i.user_id = u.id','left')
            ->join('college c','u.college = c.id','left')
            ->join('specialty s','u.specialty = s.id','left')
            ->join('match_items_details d','i.id = d.match_items_id','left')
            ->join('match_tags t','i.match_tag_id = t.id','left')
            ->where(['i.id' => $id])
            ->find()
            ->toArray();
        $detail     = json_decode($info['match_details'],true);
        $teacher    = isset($detail['teachers']) ? $detail['teachers'] : '';
        $defaultInfo = isset($detail['defualt']) ? $detail['defualt'] : '';
        $regInfo    = $detail['reg'];
        // $studentIds = $teacherIds = [];
        // $membersIds = (new Member()) -> where(['item_id' => $id,'member_source' => Member::MATCH])->order('sort','asc')->select();

        // if($membersIds) {
        //     foreach ($membersIds as $val) {
        //         if($val['member_type'] == Member::STUDENT) {
        //             $studentIds[] = $val['member_id'];
        //         }else{
        //             $teacherIds[] = $val['member_id'];
        //         }
        //     }
        // }
        
        // $studentInfo =  (new \app\admin\model\User())->where('id','in',$studentIds)->select();
        // $teacherInfo =  (new \app\school\model\basic\Teacher())->where('id','in',$teacherIds)->select();
        
        $studentInfo = (new Member())->alias('m')
            ->field('u.*,m.*,c.college_name as college_text,s.specialty_name as specialty_text,z.clazz_name as clazz_text')
            ->join('user u','m.member_id = u.id','inner')
            ->join('college c','u.college = c.id','left')
            ->join('specialty s','u.specialty = s.id','left')
            ->join('clazz z','u.clazz = z.id','left')
            ->where(['m.item_id' => $id,'m.member_source' => Member::MATCH,'m.member_type' => 1])
            ->order('m.sort','asc')
            ->select();
        
        $teacherInfo = (new Member())->alias('m')
            ->field('m.*,t.*,c.college_name as college_text')
            ->join('teacher t','m.member_id = t.id','inner')
            ->join('college c','t.college_id = c.id','left')
            ->where(['m.item_id' => $id,'m.member_source' => Member::MATCH,'m.member_type' => 2])
            ->order('m.sort','asc')
            ->select();
            
            // dump(Teacher::EDUCATION);die;
        if($teacherInfo) {
            foreach ($teacherInfo as &$t) {
                $t['position_text']     = Teacher::POSITION[$t['position']];
                $t['education_text']    = Teacher::EDUCATION[$t['education']];
                $t['degree_text']       = Teacher::DEGREE[$t['degree']];
            }
        }




        $formSets = MatchFormSet::all(['match_type_id'=>$info['match_type']]);
        $formSets = collection($formSets)->toArray();
        foreach ($formSets as $k=>&$v){
            if ($v['form_data']){
                $v['form_data'] = json_decode($v['form_data'],true);
            }
            if($v['form_data']) {
                foreach ($v['form_data'] as &$value) {
                    if($value['type'] == 'file') {
                        $value['mimeType'] = implode(',',$value['mimeType']);
                    }
                }
            }
        }
        $tag = (new MatchTags())->where('match_type_id',$info['match_type'])->select();
        $matchTag = collection($tag)->toArray();
        //项目领域
        $territory = Territory::all();
        foreach ($territory as &$value) {
            $value['id'] = strval($value['id']);
        }
        $this->assignconfig('studentInfo',$studentInfo);
        $this->assignconfig('cdn_url','http://chuangxinchuangye.posedu.com.cn/');
        $this->assignconfig('teacherInfo',$teacherInfo);
        $this->assignconfig('regInfo',$regInfo);
        $this->assignconfig('defaultInfo',$defaultInfo);
        $this->assignconfig('formSets',$formSets);
        $this->assignconfig('row',$info);
        $this->assignconfig('matchTag',$matchTag);
        $this->assignconfig('territory',$territory);
        return $this->view->fetch();
    }


    //更改表单状态
    public function formSetStatus()
    {
        $id = request()->param('id');
        $status = request()->param('status');

        $formSetModel = new MatchFormSet();
        $info = $formSetModel->where(['id' => $id])->find();
        $info->is_open = $status;
        if($info ->save()) {
            $this->success('成功');
        }
        $this->error('操作失败,稍后请重试');
    }

    public function edutItemTag()
    {
        $matchTagId = request()->param('match_tag_id');
        $itemId     = request()->param('itemId');
        if(empty($matchTagId)) {
            $this->error('请选择标签');
        }
        $itemInfo = (new MatchItems())->find($itemId);
        if(!$itemInfo) {
            $this->error('报名信息不存在');
        }
        if($itemInfo['match_tag_id'] == $matchTagId) {
            $this->success('成功');
        }
        Db::startTrans();
        try {
            $itemInfo->match_tag_id = $matchTagId;
            $itemInfo->save();
            $log = [
                'match_items_id' => $itemId,
                'match_tag_id'   => $matchTagId,
                'createtime'   => time(),
            ];
            Db::name('match_tag_log')->insertGetId($log);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error('操作失败,稍后请重试');
        }
        $this->success('成功');

    }
   public function registerExport()
    {
        if ($this->request->isPost()) {
            set_time_limit(0);
            $ids = $this->request->param('ids');
            $college_id = $this->request->param('college_id');
            $specialty_id = $this->request->param('specialty_id');
            $student_name = $this->request->param('student_name');
            $match_tag_id = $this->request->param('match_tag_id');
            $match_type_id = $this->request->param('match_type_id');
            $excel = new Spreadsheet();
            $excel->getProperties()
                ->setCreator("WEILAI")
                ->setLastModifiedBy("WEILAI")
                ->setTitle("报名数据导出")
                ->setSubject("Subject");
            $excel->getDefaultStyle()->getFont()->setName('Microsoft Yahei');
            $excel->getDefaultStyle()->getFont()->setSize(12);

            $excel->getDefaultStyle()->applyFromArray(
                array(
                    'fill'      => array(
                        'type'  => Fill::FILL_SOLID,
                        'color' => array('rgb' => '000000')
                    ),
                    'font'      => array(
                        'color' => array('rgb' => "000000"),
                    ),
                    'alignment' => array(
                        'vertical'   => Alignment::VERTICAL_CENTER,
                        'horizontal' => Alignment::HORIZONTAL_CENTER,
                        'indent'     => 1
                    ),
                    'borders'   => array(
                        'allborders' => array('style' => Border::BORDER_THIN),
                    )
                ));
            $spreadsheet = new Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();


            $college_id = $this->request->param('college_id');
            $specialty_id = $this->request->param('specialty_id');
            $student_name = $this->request->param('student_name');
            $match_type_id = $this->request->param('match_type_id');
            $whereIds = $ids == 'all' ? '1=1' : ['m.id' => ['in', explode(',', $ids)]];
            $line = 1;
            $list = [];
            if (!empty($match_tag_id)){
                //标签id
                $map['m.match_tag_id'] = $match_tag_id;
            }
            //赛道id
            $map['m.match_type'] = $match_type_id;
            //学校id
            $map['m.school_id'] = SCHOOL_ID;
            $model = new MatchItems();
            $mTable = (new MatchItems())->getTable();
            $dTable = (new MatchItemsDetails())->getTable();
            //查询要导出的数据
            $items = $model -> table("{$mTable} m")
                ->field('m.id,m.match_id,m.territory_id,m.school_id,m.user_id,m.match_type,m.match_tag_id,m.college_id,m.specialty_id,m.clazz_id,d.match_details,c.college_name,s.specialty_name,u.username,u.student_no,g.match_name,t.name as match_type_name')
                ->with(['remark','remark.judge'])
                ->join("{$dTable} d",'m.id = d.match_items_id','left')
                ->join('user u','m.user_id = u.id','left')
                ->join('college c','c.id = u.college','left')
                ->join('specialty s','s.id = u.specialty','left')
                ->join('match_group g','m.match_group_id = g.id','left')
                ->join('match_type t','m.match_type = t.id','left')
                ->where($whereIds)
                ->where($map)
                ->select();
            $list = collection($items)->toArray();
            $title = [['username' => '姓名'],['student_no' => '学号'],['college_name' => '学院名称'],['specialty_name' => '专业名称'],['match_name' => '赛事'],['remark' => '评语'],['territory_name' => '应用技术'],['match_type_name' => '赛道'],['members' => '成员'],['teachers' => '教师']];

            $formSets = MatchFormSet::all(['match_type_id'=>$match_type_id]);
            $formSets = collection($formSets)->toArray();
            //自定义表单title
            $formTitle = [];
            foreach ($formSets as $k=>&$v){
                if($v['stage_type'] == 'teachers' || $v['stage_type'] == 'members') {
                    continue;
                }
                if ($v['form_data']){
                    $v['form_data'] = json_decode($v['form_data'],true);
                }
                if($v['form_data']) {
                    foreach ($v['form_data'] as $key=> &$value) {
                        $tmp = [];
                        $tmp[$value['name']] = $value['title'];
                        $formTitle[] = $tmp;
                    }
                }
            }
            $newTitle = array_merge($title,$formTitle);
            $territoryModel = new Territory();
            $MemberModel = new Member();
            $newList = [];
            if(!empty($list)) {
                foreach ($list as $op) {
                    $mIds  = $tIds = [];
                    $tmp = [];
                    $tmp['id']              =   $op['id'];
                    $tmp['username']        =   $op['username'];
                    $tmp['student_no']      =   $op['student_no'];
                    $tmp['college_name']    =   $op['college_name'];
                    $tmp['specialty_name']  =   $op['specialty_name'];
                    $tmp['match_name']      =   $op['match_name'];
                    $remarkStr = '暂无';
                    if(!empty($op['remark'])) {
                        $remarkStr = '';
                        foreach ($op['remark'] as $k => $re) {
                            $remark = $re['remark'] ? $re['remark'] : '暂无';
                            $remarkStr .= $re['judge']['username'].':'.$remark.";";
                        }
                    }
                    //应用技术
                    if(!empty($op['territory_id'])) {
                        $territoryIds = explode(',',$op['territory_id']);
                        $territoryInfo = $territoryModel->whereIn('id',$op['territory_id'])->column('territory_name');
                        $tmp['territory_name'] = $territoryInfo ? implode(',',$territoryInfo) : '--';
                    }
                    $tmp['remark'] = $remarkStr;
                    $tmp['match_type_name'] =   $op['match_type_name'];
                    $membersInfo  = '暂无';
                    $teacherInfo  = '暂无';
                    $membersIds = $MemberModel -> where(['item_id' => $op['id'],'member_source' => Member::MATCH])->select();

                    if($membersIds) {
                        foreach ($membersIds as $val) {
                            if($val['member_type'] == Member::STUDENT) {
                                $mIds[] = $val['member_id'];
                            }else{
                                $tIds[] = $val['member_id'];
                            }
                        }
                    }
                    if($mIds) {
                        $members = Db::name("user")->field('username,student_no')->whereIn('id',$mIds)->select();
                        if($members) {
                            $membersInfo = '';
                            foreach ($members as $info) {
                                $membersInfo .= '姓名:'.$info['username'].';学号:'.$info['student_no']."\n";
                            }
                        }
                    }
                    $tmp['members'] = $membersInfo;

                    if($tIds) {
                        $teachers = Db::name("teacher")->field('username,job_num')->whereIn('id',$tIds)->select();
                        if($teachers) {
                            $teacherInfo = '';
                            foreach ($teachers as $info1) {
                                $teacherInfo .= '姓名:'.$info1['username'].';工号:'.$info1['job_num']."\n";
                            }
                        }
                    }
                    $tmp['teachers'] = $teacherInfo;
                    $formData = json_decode($op['match_details'],true);
                    $formData['reg'] = isset($formData['reg']) ? $formData['reg'] : [];
                    $formData['defualt'] = isset($formData['defualt']) ? $formData['defualt'] : [];
                    $formData = array_merge($formData['reg'],$formData['defualt']);
                    $test = array_column($formSets,'form_data');

                    $fieldArr = [];
                    foreach ($formSets as $k => $val) {

                        if(is_array($val['form_data'])) {
                            foreach ($val['form_data'] as  $r) {
                                array_push($fieldArr,$r);
                            }
                        }
                    }
                    foreach ($fieldArr as $field) {
                        foreach ($formData as $dk => $fieldValue) {
                            if($dk == 'territory_id') {
                                continue;
                            }
                            if($field['name'] == $dk){
                                if($field['type'] == 'radio') {
                                    foreach ($field['data'] as $radio) {
                                        if($radio['value'] == $fieldValue){
                                            $tmp[$dk] = $radio['label'];
                                        }
                                    }

                                }else if($field['type'] == 'checkbox') {
                                    $str = '';
                                    foreach ($field['data'] as $checkbox) {
                                        foreach ($fieldValue as $checked) {
                                            if($checkbox['value'] == $checked) {
                                                $str .= $checkbox['label']."\n";
                                            }
                                        }
                                        $tmp[$dk] =$str;
                                    }

                                }else if($field['type'] == 'file') {
                                    $tmp[$dk] = 'http://chuangxinchuangye.posedu.com.cn'.$fieldValue;
                                }else if($field['type'] == 'select') {
                                    foreach ($field['data'] as $select) {
                                        if($select['value'] == $fieldValue){
                                            $tmp[$dk] = $select['label'];
                                        }
                                    }
                                }else {
                                    $tmp[$dk] = $fieldValue;
                                }
                            }
                        }
                    }
                    $newList[] = $tmp;
                }
            }
            //组装写入excel title
            foreach ($newTitle as $key => $value) {
                foreach ($value as $item) {
                    // 单元格内容写入
                    $sheet->setCellValueByColumnAndRow($key + 1, 1, $item);
                }
            }
            //组装写入的数据根据title的key值
            $new1 = [];
            if($newList) {
                foreach ($newList as $dkey => $data) {
                    $new = [];
                    foreach ($newTitle as $tkey => $title) {
                        $key = key($title);
                        // $key = array_key_first($title);
                        $new[$key] = isset($data[$key]) ? $data[$key] : '';
                    }
                    $new1[] = $new;
                }
            }
            $row = 2; // 从第二行开始
            foreach ($new1 as $item) {
                $column = 1;
                foreach ($item as $value) {
                    // 单元格内容写入
                    $sheet->setCellValueByColumnAndRow($column, $row, $value);
                    $column++;
                }
                $row++;
            }

            $title = date("YmdHis");
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="' . $title . '.xlsx"');
            header('Cache-Control: max-age=0');
            // If you're serving to IE 9, then the following may be needed
            header('Cache-Control: max-age=1');

            // If you're serving to IE over SSL, then the following may be needed
            header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
            header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
            header('Pragma: public'); // HTTP/1.0


            $objWriter = IOFactory::createWriter($spreadsheet, 'Xlsx');
            $objWriter->save('php://output');
            return;
        }
    }

    public function detailRegisterData()
    {
        $matchItemModel = new MatchItems();
        $list = $matchItemModel->alias('m')
            ->field('m.school_id,m.user_id,m.match_id,m.match_type,d.match_details,d.match_items_id')
            ->join('match_items_details d','m.id = d.match_items_id','left')
            ->where(['m.school_id' => 6])
            ->select();
        foreach ($list as $v) {
            $info = json_decode($v['match_details'],true);
            $name = isset($info['reg']['project_name']) ? $info['reg']['project_name'] : false;
            if(!$name) {
             	continue;	
               
            }
            $register = $matchItemModel->where(['id' => $v['match_items_id']])->find();
            if($register) {
                $register->project_name = $name;
                if(!$register->save()) {
			continue;
                    echo '失败:'.$v['match_items_id'];
                    break;
                }
            }
        }
    }

    /**
     * 下载文件到本地
     */
    public function downloadFile()
    {
        $url = $this->ypDomain. request()->param('url');
        $filename = request()->param('file_name','下载文件');
        if($url) {
            downloadFile($url,$filename);
        }
    }
}