AdminCommonController.php 38.4 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
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\portal\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;
use ZipArchive;
use app\portal\model\InspectModel;
//甲方巡检列表
class AdminCommonController extends AdminBaseController
{
    //甲方服务列表
    const serviceAList = [
        ['id' => 4, 'service_name' => '报修'],
        ['id' => 3, 'service_name' => '改造'],
        ['id' => 5, 'service_name' => '培训'],
        ['id' => 6, 'service_name' => '演习'],
    ];
    //乙方服务列表
    const serviceBList = [
        ['id' => 2, 'service_name' => '月检'],
        ['id' => 4, 'service_name' => '报修'],
        ['id' => 3, 'service_name' => '改造'],
    ];

    //所有服务列表
    const serviceList = [
        ['id' => 2, 'service_name' => '月检'],
        ['id' => 4, 'service_name' => '报修'],
        ['id' => 3, 'service_name' => '改造'],
        ['id' => 5, 'service_name' => '培训'],
        ['id' => 6, 'service_name' => '演习'],
    ];

    //根据项目id,甲方或者乙方查询正常巡检天数
    public function normalTime($where){
        $time = $this->inspectDays($where);
        //查询巡检点总数
        $count = $this->pointCount($where['project_id']);
        //正常状态
        $where['status'] = 0;
        $day = 0;
        foreach($time as $value){
            $days = $this->inspect($where,$value);
            if($days == $count){
                $day++;
            }
        }
        return $day;
    }

    //根据项目id,甲方或者乙方查询故障巡检天数
    public function breakTime($where){
        $time = $this->inspectDays($where);
        //查询巡检点总数
        $count = $this->pointCount($where['project_id']);
        //正常状态
        $where['status'] = 0;
        $day = 0;
        foreach($time as $value){
            $days = $this->inspect($where,$value);
            if($days < $count){
                $day++;
            }
        }
        return $day;
    }

    //根据项目id,查询巡检天数
    public function inspectDays($where){
        $res = Db::name('inspect')
            ->where($where)
            ->field('id,create_time')
            ->group("DATE_FORMAT(FROM_UNIXTIME(create_time),'%Y-%m-%d')")
            ->select()
            ->toArray();
        $time = array_column($res,'create_time');
        return $time;
    }

    //根据项目id,甲方或者乙方查巡检总数,来判断正常,故障
    public function inspect($where,$time){
        $time1 = date('Y-m-d',$time);
        $time2 = date('Y-m-d',$time+86400);
        $count = Db::name('inspect')
            ->where($where)
            ->whereTime('create_time',[$time1,$time2])
            ->count();
        return $count;
    }

    //根据项目id查询巡检点个数
    public function pointCount($project_id){
        $count = Db::name('point')
            ->where('p_id',$project_id)
            ->count();
        return $count;
    }

    //查询所有的项目,以及对应的乙方公司
    public function getCompanyBList(){
        $res = Db::name('project')
            ->alias('p')
            ->join('company c','p.b_cid = c.id')
            ->field('p.id,p.name project_name,c.company_name,c.id company_id,p.create_time')
            ->order('p.id desc')
            ->paginate(10);
        return $res;
    }

    //计算运行天数
    public function runTime($time){
        $currentTime = time();
        $cnt=$currentTime-$time;
        $days = floor($cnt/(3600*24));//算出天数
        return $days+1;
    }

    //按照条件查询次数
    public function getCount($table,$where){
        $count = Db::name($table)
            ->where($where)
            ->count();
        return $count;
    }

    //根据uid查询用户正常考勤日期
    public function getNormalWork($uid){
        //聚合相同日期
        $res = Db::name('work')
            ->where(['uid'=>$uid])
            ->field('id,uid,create_time')
            ->group("DATE_FORMAT(FROM_UNIXTIME(create_time),'%Y-%m-%d')")
            ->select()
            ->toArray();
        $normal = 0;
        $not_normal = 0;
        $arr = [];
        foreach($res as $value){
            $count = $this->getWorkCount($value['uid'],$value['create_time']);
            if($count == 2){
                //正常
                $arr['normal'] = ++$normal;
            }else{
                //缺卡
                $arr['not_normal'] = ++$not_normal;
            }
        }
        return $arr;
    }

    //根据uid获取是否含有上班,下班
    public function getWorkCount($uid,$create_time){
        $time = date('Y-m-d',$create_time);
        $time1 = date('Y-m-d',$create_time+86400);
        //查询所有记录
        $count = Db::name('work')
            ->where(['uid'=>$uid])
            ->whereTime('create_time',[$time,$time1])
            ->count();
        return $count;
    }

    //查询甲方人员id
    public function getPartA(){
        $staff = Db::name('company')
            ->where(['pid'=>['neq',0]])
            ->field('id,company_name,u_s_id,u_l_id,u_ls_id,is_children,company_name_head')
            ->select()
            ->toArray();
        return $staff;
    }

    //查询乙方员工id
    public function getPartB(){
        $staff = Db::name('company')
            ->where('pid',0)
            ->field('id,company_name,u_s_id,u_l_id,u_ls_id')
            ->select()
            ->toArray();
        return $staff;
    }

    //查找甲方员工所在的项目组
    public function getProjectA($company_id,$uid){
        $res = Db::name('project')
            ->where(['a_cid'=>$company_id])
            ->field('id,a_sid,name project_name,create_time')
            ->select()
            ->toArray();
        $arr = [];
        $common = new AdminCommonController();
        foreach($res as $value){
            $a_sid = explode(',',trim($value['a_sid'],','));
            if(in_array($uid,$a_sid)){
                $arr['project_name'] = $value['project_name'];
                $arr['run_time'] = $common->runTime($value['create_time']);
            }
        }
        return $arr;
    }

    //查找乙方员工所在的项目组
    public function getProjectB($company_id,$uid){
        $res = Db::name('project')
            ->where(['b_cid'=>$company_id])
            ->field('id,b_sid,name project_name,create_time')
            ->select()
            ->toArray();
        $arr = [];
        $common = new AdminCommonController();
        foreach($res as $value){
            $a_sid = explode(',',trim($value['b_sid'],','));
            if(in_array($uid,$a_sid)){
                $arr['project_name'] = $value['project_name'];
                $arr['run_time'] = $common->runTime($value['create_time']);
            }
        }
        return $arr;
    }

    //根据服务查询完成情况
    public function getServiceCount($table,$where){
        $count = Db::name($table)
            ->where($where)
            ->count();
        return $count;
    }

    /*
     * 添加文件路径到压缩包中
     * param files:可以为二维数组
     * array=>[
            [
                0 => string 'upload/export/2019-06-29.xls'
                1 => string 'upload/export/2019-06-30.xls'
            ],
            [
                0 => string 'upload/export/2019-06-29.xls'
                1 => string 'upload/export/2019-06-30.xls'
            ]
     * ]
     * param files:也可一维数组
     * array=>[
        0 => string 'upload/export/2019-06-29.xls'
        1 => string 'upload/export/2019-06-30.xls'
     * ]
     * param zipName:压缩包路径名称(upload/zipName.zip)
    */
    public function zip(array $files,$zipName,$arr_time,$flag='',$xlsName=''){

        $zip = new ZipArchive();//使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
        /*
         * 参考链接https://blog.csdn.net/weixin_34354945/article/details/91330692
         * 通过ZipArchive的对象处理zip文件
         * $zip->open这个方法如果对zip文件对象操作成功,$zip->open这个方法会返回TRUE
         * $zip->open这个方法第一个参数表示处理的zip文件名。
         * 这里重点说下第二个参数,它表示处理模式
         * ZipArchive::OVERWRITE 总是以一个新的压缩包开始,此模式下如果已经存在则会被覆盖。
         * ZIPARCHIVE::CREATE 如果不存在则创建一个zip压缩包,若存在系统就会往原来的zip文件里添加内容。
         *
         * 这里不得不说一个大坑。
         * 我的应用场景是需要每次都是创建一个新的压缩包,如果之前存在,则直接覆盖,不要追加
         * so,根据官方文档和参考其他代码,$zip->open的第二个参数我应该用 ZipArchive::OVERWRITE
         * 问题来了,当这个压缩包不存在的时候,会报错:ZipArchive::addFile(): Invalid or uninitialized Zip object
         * 也就是说,通过我的测试发现,ZipArchive::OVERWRITE 不会新建,只有当前存在这个压缩包的时候,它才有效
         * 所以我的解决方案是 $zip->open($zipName, \ZIPARCHIVE::OVERWRITE | \ZIPARCHIVE::CREATE)
         *
         * 以上总结基于我当前的运行环境来说
         * */
        if ($zip->open($zipName, \ZIPARCHIVE::OVERWRITE | \ZIPARCHIVE::CREATE)!==TRUE) {
            exit('无法打开文件,或者文件创建失败');
        }
        if($flag != ''){
            //二维数组
            if($flag == 'month'){
                //月
                foreach($files as $key=>$val){
                    foreach ($val as $k => $v) {
                        if(file_exists($v)){
                            $zip->addFromString($xlsName.'/'.$key.'/'.$arr_time[$key][$k], file_get_contents($v));//第一个参数:文件路径(含文件夹),第二个参数文件名称
                        }
                    }
                }
            }else{
                //年
                foreach($files as $key=>$val){
                    foreach($val as $key1=>$val1){
                        foreach ($val1 as $key2 => $val2) {
                            if(file_exists($val2)){
                                $zip->addFromString($xlsName.'/'.$key.'年/'.$key1.'/'.$arr_time[$key][$key1][$key2], file_get_contents($val2));
                            }
                        }
                    }
                }
            }
        }else{
            //一维数组
            foreach($files as $key=>$val){
                //addFile函数首个参数如果带有路径,则压缩的文件里包含的是带有路径的文件压缩
                //若不希望带有路径,则需要该函数的第二个参数
                if(file_exists($val)){
                    $zip->addFile($val, $arr_time[$key]);//含中文
                    //$zip->addFile($val, basename($val));//不含中文,第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下
                }
            }
        }
        $zip->close();//关闭

        $sys = $this->getOperateSys();
        if($sys == 'Linux'){
            chmod($zipName, 0777);//适用于linux
        }

//        if(!file_exists($zipName)){
//            exit("无法找到文件"); //即使创建,仍有可能失败
//        }

        //如果不要下载,下面这段删掉即可,如需返回压缩包下载链接,只需 return $zipName;
        if($sys == 'Windows'){
            //中文导出需要转码
            $zipName=iconv('UTF-8','GB2312',$zipName);
        }

        //设置脚本的最大执行时间,设置为0则无时间限制
        set_time_limit(0);
        ini_set("memory_limit","128M");
        //下载zip
        header("Cache-Control: public");//任何情况下都可以得到资源参考:https://blog.csdn.net/u012375924/article/details/82806617
        header("Content-Description: File Transfer");
        header('Content-disposition: attachment; filename='.basename($zipName)); //文件名
        header("Content-Type: application/zip"); //zip格式的
        header("Content-Transfer-Encoding: Binary"); //告诉浏览器,这是二进制文件
        header('Content-Length: '. filesize($zipName)); //告诉浏览器,文件大小
        ob_clean();//清楚缓存
        flush();//刷新缓冲区的内容,输出
        readfile($zipName);
//        unlink($zipName); //删除压缩包临时文件
        $this->delDirAndFile(ROOT_PATH . 'public/upload/export/',1);
        exit;
    }

    //删除目录及目录下文件
    public function delDirAndFile($dirName,$delDir=''){
        if ($handle = opendir($dirName)) {
            while (false !== ($item = readdir($handle))) {
                if ($item != "." && $item != "..") {
                    if (is_dir( $dirName.'/'.$item) ) {
                        $this->delDirAndFile( $dirName.'/'.$item);
                    }else{
                        unlink($dirName.'/'.$item);//删除目录下文件
                    }
                }
            }
            closedir($handle);
            if(empty($delDir)){
                rmdir($dirName);//删除目录
            }
        }
    }

    //转化编码值UFT-8
    public function iconv_to_utf8($keyword, $to='UTF-8'){
        $encode = mb_detect_encoding($keyword, array('ASCII','UTF-8','GBK','GB2312'));
        if($encode != $to){
            $keyword = iconv($encode, $to, $keyword);
        }
        return $keyword;
    }

    //根据项目id获取项目名称,甲方公司名称,logo,乙方公司名称,logo
    public function getCompanyName($project_id,$report_name){
        //获取甲方id,乙方id
        $arr = [];
        $project = $this->getProject(['id'=>$project_id],'id,a_cid,b_cid,name');
        $host = new InspectModel();
        $host = $host::host.'/';
        if($project){
            //甲方公司
            $a_company = $this->getCompany(['id'=>$project['a_cid']],'id,company_name,logo');
            $arr['a_company']['company_name'] = !empty($a_company['company_name'])?$a_company['company_name']:'';
            $arr['a_company']['logo'] = !empty($a_company['logo'])?$host.$a_company['logo']:'';
            //乙方公司
            $b_company = $this->getCompany(['id'=>$project['b_cid']],'id,company_name,logo');
            $arr['b_company']['company_name'] = !empty($b_company['company_name'])?$b_company['company_name']:'';
            $arr['b_company']['logo'] = !empty($b_company['logo'])?$host.$b_company['logo']:'';
            //报表标题
            $arr['title'] = $project['name'].$report_name.'报表';
        }
        return $arr;
    }

    //获取项目相关信息
    public function getProject($where,$field){
        $res = Db::name('project')
            ->where($where)
            ->field($field)
            ->order('id desc')
            ->find();
        return $res;
    }

    //获取企业相关信息
    public function getCompany($where,$field){
        $res = Db::name('company')
            ->where($where)
            ->field($field)
            ->order('id desc')
            ->find();
        return $res;
    }

    //根据项目id获取乙方项目组人员
    public function getBUserByProject($project_id,$field,$flag = ''){
        $users = Db::name('project')
            ->where('id',$project_id)
            ->field($field)
            ->find();
        $b_sid = explode(',',trim($users['b_sid'],','));
        $user_login = Db::name('user')
            ->whereIn('id',$b_sid)
            ->where('user_status',1)
            ->field('id,user_login')
            ->select()
            ->toArray();
        if(!empty($flag)){
            $users = trim(implode(array_column($user_login,'user_login'),'、'),'、');
        }else{
            $users = trim(implode(array_column($user_login,'user_login'),'/'),'/');
        }
        return $users;
    }

    //根据项目id获取甲方项目组人员
    public function getAUserByProject($project_id,$field){
        $users = Db::name('project')
            ->where('id',$project_id)
            ->field($field)
            ->find();
        $a_sid = explode(',',trim($users['a_sid'],','));
        $user_login = Db::name('user')
            ->whereIn('id',$a_sid)
            ->where('user_status',1)
            ->field('id,user_login')
            ->select()
            ->toArray();
        $group_name = trim(implode(array_column($user_login,'user_login'),'、'),'、');
        return $group_name;
    }

    //查询巡检正常或故障
    public function insStatus($where,$time,$time1){
        //查询总巡检点个数
        $point_count = Db::name('point')
            ->where(['p_id'=>$where['project_id']])
            ->count();
        //查询每天的正常巡检点个数
        $inspect_count = Db::name('inspect')
            ->where($where)
            ->whereTime('create_time', [$time,$time1])
            ->count();
        if($inspect_count < $point_count){
            $status = '故障';
        }else{
            $status = '正常';
        }
        return $status;
    }

    //获取巡检点相关的信息
    public function getPoint($where,$time,$time1){
        $point = Db::name('point')
            ->where('p_id',$where['project_id'])
            ->select()
            ->toArray();

        //查找以巡检的数据
        $inspect = Db::name('inspect')
            ->where($where)
            ->whereTime('create_time', [$time,$time1])
            ->field('id,point_id,images')
            ->select()
            ->toArray();
        //巡检名称,巡检人员,巡检图片
        foreach ($point as &$value1){
            $value1['images'] = [];
            foreach ($inspect as $ins_value){
                if($value1['id'] == $ins_value['point_id']){
                    $value1['images'] = $this->exportUrl($ins_value['images']);
                }
            }
        }
        return $point;
    }

    //相对路径转绝对路径
    public function exportUrl($images){
        if(!empty($images)){
            $images = unserialize($images);
            if(is_array($images)){
                $host = new InspectModel();
                $host = $host::host;
                foreach($images as &$value){
                    $value['image_url'] = $host.$value['image_url'];
                }
            }
        }
        return $images;
    }

    //获取导出巡检列表
    public function inspectList($party,$param){
        $res_ins = Db::name('inspect')
            ->alias('i')
            ->join('project p','i.project_id = p.id','LEFT')
            ->where(['i.party'=>$party])
            ->where(function (Query $query) use ($param){
                if (!empty($param['name'])) {
                    $keyword = $param['name'];
                    $query->where('p.name', 'like', "%$keyword%");
                }
                $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
                $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time'])+86400;
                if (!empty($startTime)) {
                    $query->where('i.create_time', '>=', $startTime);
                }
                if (!empty($endTime)) {
                    $query->where('i.create_time', '<=', $endTime);
                }

            })
            ->field('i.id,i.create_time,i.project_id,p.name project_name')
            ->group("DATE_FORMAT(FROM_UNIXTIME(i.create_time),'%Y-%m-%d')")
            ->select()->toArray();
        return $res_ins;
    }

    /**
     *  API返回信息格式函数 ;0失败,1成功
     * @param string $code
     * @param string $message
     * @param array $data
     */
    public function apiResponse($code = '', $msg = '',$data = array()){
        header('Access-Control-Allow-Origin: *');
        header('Content-Type:application/json; charset=utf-8');
        $result = array(
            'code'=>$code,
            'msg'=>$msg,
            'data'=>$data,
        );
        die(json_encode($result,JSON_UNESCAPED_UNICODE));
    }

    //判断当前操作系统
    public function getOperateSys(){
        $os_name = php_uname('s');
        //判断
        if(strpos($os_name,"Linux")!==false){
            $os_str="Linux";
        }else if(strpos($os_name,"Windows")!==false){
            $os_str="Windows";
        }else{
            $os_str='';
        }
        return $os_str;
    }

    //获取导出(月检,培训,演习)列表
    public function exportCheckList($table,$param,$field){
        $res_ins = Db::name($table)
            ->alias('t')
            ->join('project p','t.project_id = p.id','LEFT')
            ->where(function (Query $query) use ($param,$table){
                if (!empty($param['name'])) {
                    $keyword = $param['name'];
                    $query->where('p.name', 'like', "%$keyword%");
                }
                $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
                $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time'])+86400;
                if (!empty($startTime)) {
                    if($table == 'check'){
                        $query->where('t.ins_m_time', '>=', $startTime);
                    }else if($table == 'train'){
                        $query->where('t.train_time', '>=', $startTime);
                    }else{
                        $query->where('t.exercise_time', '>=', $startTime);
                    }
                }
                if (!empty($endTime)) {
                    if($table == 'check'){
                        $query->where('t.ins_m_time', '<=', $endTime);
                    }else if($table == 'train'){
                        $query->where('t.train_time', '<=', $endTime);
                    }else{
                        $query->where('t.exercise_time', '<=', $endTime);
                    }
                }

            })
            ->field($field)
            ->select()->toArray();
        return $res_ins;
    }

    //获取导出(报修改造)列表
    public function exportRepairList($table,$param,$party,$field){
        $res_ins = Db::name($table)
            ->alias('r')
            ->join('project p','r.project_id = p.id','LEFT')
            ->where(['r.party'=>$party])
            ->where(function (Query $query) use ($param){
                if (!empty($param['name'])) {
                    $keyword = $param['name'];
                    $query->where('p.name', 'like', "%$keyword%");
                }
                $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
                $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time'])+86400;
                if (!empty($startTime)) {
                    $query->where('r.create_time', '>=', $startTime);
                }
                if (!empty($endTime)) {
                    $query->where('r.create_time', '<=', $endTime);
                }

            })
            ->field($field)
            ->select()->toArray();
        return $res_ins;
    }

    //导出月检报表详情
    public function checkExportData($res){
        $arr = [];
        //甲乙方logo,名称,报表名称
        $company = $this->getCompanyName($res['project_id'],'月检');
        if($company){
            $arr['a_company'] = $company['a_company'];
            $arr['b_company'] = $company['b_company'];
            $arr['title'] = $company['title'];
        }

        //月检人员(乙方项目组)
        $arr['group_name'] = $this->getBUserByProject($res['project_id'],'id,b_sid',1);

        //月检时间
        $arr['ins_m_time'] = date('Y/m/d',$res['ins_m_time']);
        $arr['time'] = date('Y-m-d',$res['ins_m_time']).'('.$res['id'].')';
        //月检地点
        $arr['address'] = $res['address'];

        //月检备注
        $arr['remark'] = $res['remark'];

        //月检(乙方发起人)
        $user = $this->getUserById(['id'=>$res['uid']]);
        if($user){
            $arr['user_login'] = $user['user_login'];
        }else{
            $arr['user_login'] = '';
        }

        //甲方领导确认人
        $leader_user = $this->getUserById(['id'=>$res['a_leader']]);
        if($leader_user){
            $arr['a_leader_user'] = $leader_user['user_login'];
        }else{
            $arr['a_leader_user'] = '';
        }

        //乙方确认完成人
        $finish_user = $this->getUserById(['id'=>$res['finish_uid']]);
        if($finish_user){
            $arr['confirm_user'] = $finish_user['user_login'];
        }else{
            $arr['confirm_user'] = '';
        }

        //月检工期及验收完成时间
        if($res['finish_time']){
            $arr['finish_time'] = date('Y/m/d',$res['finish_time']);
            //培训
            $arr['check_period'] = $this->getPeriod($res['finish_time'],$res['ins_m_time']);
        }else{
            //验收完成时间
            $arr['finish_time'] = '';
            //月检工期
            $arr['check_period'] = '';
        }

        //月检图片
        if($res['images']){
            $images = $this->exportUrl($res['images']);
            $arr1 = $this->getDayImages($images);
            $arr['check'][0]['check_name'] = '月检图片';
            $arr['check'][0]['images'] = $arr1;
        }else{
            $arr['check'][0]['check_name'] = '月检图片';
            $arr['check'][0]['images'] = [];
        }

        //确认图片
        if($res['confirm_images']){
            $images = $this->exportUrl($res['confirm_images']);
            $arr1 = $this->getDayImages($images);
            $arr['check'][1]['check_name'] = '确认完成图片';
            $arr['check'][1]['images'] = $arr1;
        }else{
            $arr['check'][1]['check_name'] = '确认完成图片';
            $arr['check'][1]['images'] = [];
        }
        return $arr;
    }

    //获取导出检测列表
    public function testList($table,$param,$field){
        $res_ins = Db::name($table)
            ->alias('t')
            ->join('project p','t.project_id = p.id','LEFT')
            ->where(function (Query $query) use ($param){
                if (!empty($param['name'])) {
                    $keyword = $param['name'];
                    $query->where('p.name', 'like', "%$keyword%");
                }
                $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
                $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time'])+86400;
                if (!empty($startTime)) {
                    $query->where('t.test_time', '>=', $startTime);
                }
                if (!empty($endTime)) {
                    $query->where('t.test_time', '<=', $endTime);
                }

            })
            ->field($field)
            ->select()->toArray();
        return $res_ins;
    }

    //获取年检点相关的信息
    public function getSpot($where){
        $spot = Db::name('spot')
            ->alias('s')
            ->join('years y','s.id = y.spot_id','LEFT')
            ->where($where)
            ->field('s.spot_name,y.images')
            ->select()
            ->toArray();
        foreach($spot as &$value){
            $images = $this->exportUrl($value['images']);
            if($images){
                $value['images'] = $images;
            }else{
                $value['images'] = [];
            }
        }
        return $spot;
    }

    //获取年检报表详情
    public function yearsExportData($res){
        $arr = [];
        $company = $this->getCompanyName($res['project_id'],'年检');
        if($company){
            $arr['a_company'] = $company['a_company'];
            $arr['b_company'] = $company['b_company'];
            $arr['title'] = $company['title'];
        }
        //年检检测
        $where = ['s.project_id'=>$res['project_id'],'s.test_id'=>$res['id']];
        $arr['years'] = $this->getSpot($where);

        //年检状态
        $status = $this->getYearStatus(['test_id'=>$res['id'],'project_id'=>$res['project_id']]);
        $arr['years_status'] = $status;
        //检测时间
        $arr['test_time'] = date('Y/m/d',$res['test_time']);
        $arr['time'] = date('Y-m-d',$res['test_time']).'('.$res['id'].')';
        //乙方项目组
        $arr['group_name'] = $this->getBUserByProject($res['project_id'],'id,b_sid',1);
        return $arr;
    }

    //获取年检状态
    public function getYearStatus($where){
        //查询总巡检点个数
        $spot_count = Db::name('spot')
            ->where($where)
            ->count();
        //查询检测点个数
        $years_count = Db::name('years')
            ->where($where)
            ->count();
        if($years_count < $spot_count){
            $status = '故障';
        }else{
            $status = '正常';
        }
        return $status;
    }

    //导出培训演习报表详情
    public function trainExportData($res,$table){
        $arr = [];
        //甲乙方logo,名称,报表名称
        if($table == 'train'){
            $company = $this->getCompanyName($res['project_id'],'培训');
        }else{
            $company = $this->getCompanyName($res['project_id'],'演习');
        }
        if($company){
            $arr['a_company'] = $company['a_company'];
            $arr['b_company'] = $company['b_company'];
            $arr['title'] = $company['title'];
        }
        //培训演习单号
        $arr['number'] = $res['number'];

        //培训演习人员(乙方项目组)
        $arr['group_name'] = $this->getBUserByProject($res['project_id'],'id,b_sid',1);

        //培训演习时间
        if($table == 'train'){
            $arr['train_time'] = date('Y/m/d',$res['train_time']);
            $arr['time'] = date('Y-m-d',$res['train_time']).'('.$res['id'].')';
        }else{
            $arr['exercise_time'] = date('Y/m/d',$res['exercise_time']);
            $arr['time'] = date('Y-m-d',$res['exercise_time']).'('.$res['id'].')';
        }

        //培训演习(甲方发起人)
        $user = $this->getUserById(['id'=>$res['uid']]);
        if($user){
            $arr['user_login'] = $user['user_login'];
        }else{
            $arr['user_login'] = '';
        }

        //乙方领导确认人
        $leader_user = $this->getUserById(['id'=>$res['b_leader']]);
        if($leader_user){
            $arr['b_leader_user'] = $leader_user['user_login'];
        }else{
            $arr['b_leader_user'] = '';
        }

        //甲方确认完成人
        $finish_user = $this->getUserById(['id'=>$res['confirm_uid']]);
        if($finish_user){
            $arr['confirm_user'] = $finish_user['user_login'];
        }else{
            $arr['confirm_user'] = '';
        }

        //培训演习工期及验收完成时间
        if($res['finish_time']){
            $arr['finish_time'] = date('Y/m/d',$res['finish_time']);
            if($table == 'train'){
                $arr['check_period'] = $this->getPeriod($res['finish_time'],$res['train_time']);
            }else{
                $arr['check_period'] = $this->getPeriod($res['finish_time'],$res['exercise_time']);
            }

        }else{
            //验收完成时间
            $arr['finish_time'] = '';
            //工期
            $arr['check_period'] = '';
        }

        //甲乙方图片
        if($res['images']){
            $images = $this->exportUrl($res['images']);
            $arr1 = $this->getDayImages($images);
            $arr['train'][0]['check_name'] = '甲乙方完善图片';
            $arr['train'][0]['images'] = $arr1;
        }else{
            $arr['train'][0]['check_name'] = '甲乙方完善图片';
            $arr['train'][0]['images'] = [];
        }

        //确认图片
        if($res['confirm_images']){
            $images = $this->exportUrl($res['confirm_images']);
            $arr1 = $this->getDayImages($images);
            $arr['train'][1]['check_name'] = '确认完成图片';
            $arr['train'][1]['images'] = $arr1;
        }else{
            $arr['train'][1]['check_name'] = '确认完成图片';
            $arr['train'][1]['images'] = [];
        }
        return $arr;
    }

    //导出报修改造报表详情
    public function repairExportData($res,$table,$party){
        $arr = [];
        //甲乙方logo,名称,报表名称
        if($table == 'repair'){
            if($party == 0){
                $company = $this->getCompanyName($res['project_id'],'(甲方)报修');
            }else{
                $company = $this->getCompanyName($res['project_id'],'(乙方)报修');
            }
        }else{
            if($party == 0){
                $company = $this->getCompanyName($res['project_id'],'(甲方)改造');
            }else{
                $company = $this->getCompanyName($res['project_id'],'(乙方)改造');
            }
        }
        if($company){
            $arr['a_company'] = $company['a_company'];
            $arr['b_company'] = $company['b_company'];
            $arr['title'] = $company['title'];
        }
        //培训报修改造单号
        $arr['number'] = $res['number'];

        //报修改造人员(乙方项目组)
        $arr['group_name'] = $this->getBUserByProject($res['project_id'],'id,b_sid',1);

        //报修改造时间
        $arr['create_time'] = date('Y/m/d',$res['create_time']);
        $arr['time'] = date('Y-m-d',$res['create_time']).'('.$res['id'].')';

        //报修改造(甲方/乙方发起人)
        $user = $this->getUserById(['id'=>$res['uid']]);
        if($user){
            $arr['user_login'] = $user['user_login'];
        }else{
            $arr['user_login'] = '';
        }

        //报修改造确认人(甲方发起,乙方确认,乙方发起,甲方确认)
        $h_user = $this->getUserById(['id'=>$res['h_uid']]);
        if($h_user){
            $arr['h_user'] = $h_user['user_login'];
        }else{
            $arr['h_user'] = '';
        }

        //甲方确认完成人
        $finish_user = $this->getUserById(['id'=>$res['confirm_uid']]);
        if($finish_user){
            $arr['confirm_user'] = $finish_user['user_login'];
        }else{
            $arr['confirm_user'] = '';
        }

        //报修改造工期及验收完成时间
        if($res['finish_time']){
            $arr['finish_time'] = date('Y/m/d',$res['finish_time']);
            $arr['check_period'] = $this->getPeriod($res['finish_time'],$res['create_time']);

        }else{
            //验收完成时间
            $arr['finish_time'] = '';
            //工期
            $arr['check_period'] = '';
        }

        //甲乙方图片凭证
        if($res['images']){
            $images = $this->exportUrl($res['images']);
            $arr1 = $this->getDayImages($images);
            if($party == 0){
                $arr['repair'][0]['check_name'] = '甲方发起图片凭证';
            }else{
                $arr['repair'][0]['check_name'] = '乙方发起图片凭证';
            }
            $arr['repair'][0]['images'] = $arr1;
        }else{
            if($party == 0){
                $arr['repair'][0]['check_name'] = '甲方发起图片凭证';
            }else{
                $arr['repair'][0]['check_name'] = '乙方发起图片凭证';
            }
            $arr['repair'][0]['images'] = [];
        }

        //甲乙方完善图片
        if($table == 'repair'){
            if($res['repair_images']){
                $images = $this->exportUrl($res['repair_images']);
                $arr1 = $this->getDayImages($images);
                $arr['repair'][1]['check_name'] = '甲乙方完善图片';
                $arr['repair'][1]['images'] = $arr1;
            }else{
                $arr['repair'][1]['check_name'] = '甲乙方完善图片';
                $arr['repair'][1]['images'] = [];
            }
        }else{
            if($res['reform_images']){
                $images = $this->exportUrl($res['reform_images']);
                $arr1 = $this->getDayImages($images);
                $arr['repair'][1]['check_name'] = '甲乙方完善图片';
                $arr['repair'][1]['images'] = $arr1;
            }else{
                $arr['repair'][1]['check_name'] = '甲乙方完善图片';
                $arr['repair'][1]['images'] = [];
            }
        }

        //确认图片
        if($res['confirm_images']){
            $images = $this->exportUrl($res['confirm_images']);
            $arr1 = $this->getDayImages($images);
            $arr['repair'][2]['check_name'] = '确认完成图片';
            $arr['repair'][2]['images'] = $arr1;
        }else{
            $arr['repair'][2]['check_name'] = '确认完成图片';
            $arr['repair'][2]['images'] = [];
        }
        return $arr;
    }

    //根据uid查询用户
    public function getUserById($where){
        $res = Db::name('user')
            ->where($where)
            ->field('id,user_login,identity')
            ->find();
        return $res;
    }

    //获取项目完成工期
    public function getPeriod($finishTime,$time){
        $cnt = $finishTime-strtotime(date('Y-m-d',$time));
        $days = floor($cnt/(3600*24))+1;//算出天数
        return $days;
    }

    //按照日期进行排序
    public function getDayImages($images){
        $arr1 = [];
        foreach($images  as &$value){
            $value['file_time'] = date('Y/m/d',$value['file_time']);
        }
        //去重后的日期
        $res = array_values(array_unique(array_column($images,'file_time')));//array_values键名从0开始
        foreach($res as $key1=>$value1){
            $k = 0;
            foreach($images as $value2){
                if($value1 == $value2['file_time']){
                    $k+=0;
                    $arr1[$key1]['day'] = $value2['file_time'];
                    $arr1[$key1]['image'][$k]['image_url'] = $value2['image_url'];
                    $k++;
                }
            }
        }
        return $arr1;
    }

    //获取日期时间戳
    public function getDate($time){
        $dateStr = date('Y-m-d',$time);
        //获取0点的时间戳
        $time1 = strtotime($dateStr);
        //获取24点的时间戳
        $time2 = strtotime($dateStr) + 86400;
        $arr = [$time1,$time2];
        return $arr;
    }

}