<?php
namespace App\Models\results;

use CodeIgniter\Model;

class M417Model extends Model
{
    public function getDepart()
    {
        $sql = "SELECT * from departments " .
            "WHERE pdept_sq = 3 " .
            "ORDER BY dept_nm";

        $query = $this->db->query($sql);

        return $query->getResultArray();
    }

    public function getTotalCount($data)
    {
        $sql = "SELECT
                    COUNT(*) AS cnt
                        FROM
                        users a
                        left join (
                        SELECT
                            CASE
                            a.insert_user WHEN 0 THEN 1
                            ELSE a.insert_user
                            END insert_user,
                            COUNT(*) total_cnt ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '10' THEN a.vr_sq ELSE NULL END ) receipt_cnt ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '10' THEN a.vr_sq ELSE NULL END ) notassign_cnt ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '10' THEN NULL ELSE a.vr_sq END ) assign_cnt ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '19' THEN a.vr_sq ELSE NULL END ) cancel_cnt ,
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '30' THEN a.vr_sq ELSE NULL END ) phone_checking_cnt ,
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'D' AND a.stat_cd = '30' THEN a.vr_sq ELSE NULL END ) paper_checking_cnt ,  
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '35' THEN a.vr_sq ELSE NULL END ) phone_complete_cnt ,  -- 전화확인 완료
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'D' AND a.stat_cd = '35' THEN a.vr_sq ELSE NULL END ) paper_complete_cnt ,
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '38' THEN a.vr_sq ELSE NULL END ) phone_all_cnt , -- 전화확인 전체
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '39' THEN a.vr_sq ELSE NULL END ) phone_fail_cnt ,  -- 전화확인 실패
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'D' AND a.stat_cd = '39' THEN a.vr_sq ELSE NULL END ) paper_fail_cnt ,
                            COUNT( DISTINCT case when (b.certRegister IS NOT NULL AND b.stat_cd = '60' AND a.stat_cd = '76') then a.vr_sq ELSE NULL END ) cert_same_reg_open ,
                            COUNT( DISTINCT case when (b.certRegister IS NOT NULL AND a.stat_cd = '77') then a.vr_sq ELSE NULL END ) cert_reg_fail_open, 
                            COUNT( DISTINCT case when (b.referenceFileUrl IS NOT NULL AND b.stat_cd = '60' AND a.stat_cd = '76') then a.vr_sq ELSE NULL END ) reference_same_reg_open ,
                            COUNT( DISTINCT case when (b.referenceFileUrl IS NOT NULL AND a.stat_cd = '77') then a.vr_sq ELSE NULL END ) reference_reg_fail_open ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '40' THEN a.vr_sq ELSE NULL END ) reg_checking_cnt ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '45' THEN a.vr_sq ELSE NULL END ) reg_complete_cnt ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '70' THEN a.vr_sq ELSE NULL END ) reg_none ,	-- 등기부 등본 없음
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '86' THEN a.vr_sq ELSE NULL END ) real_top_RS ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '88' THEN a.vr_sq ELSE NULL END ) real_top_RF ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '87' THEN a.vr_sq ELSE NULL END ) real_top_GS ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '89' THEN a.vr_sq ELSE NULL END ) real_top_GF ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '49' THEN a.vr_sq ELSE NULL END ) reg_fail_cnt ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '60' THEN a.vr_sq ELSE NULL END ) complete_cnt ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '60' THEN a.vr_sq ELSE NULL END ) complete_cnt1,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '69' THEN a.vr_sq ELSE NULL END ) fail_cnt ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '69' THEN a.vr_sq ELSE NULL END ) fail_cnt1 ,
                            COUNT( CASE WHEN (a.stat_cd = '85') THEN a.vr_sq ELSE NULL END ) reg_fail_tempOpen ,
                            COUNT( CASE WHEN (a.stat_cd = '77') THEN a.vr_sq ELSE NULL END ) reg_fail_Open ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '60' AND a.stat_cd = '80' THEN a.vr_sq ELSE NULL END ) same_reg_tempOpen ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '60' AND a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) same_reg_open,
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '0' and a.stat_cd = '77' THEN a.vr_sq ELSE NULL END ) as referin_s0,  -- 첨부파일 불일치 개인 
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '1' and a.stat_cd = '77' THEN a.vr_sq ELSE NULL END ) as referin_s1,  -- 첨부파일 불일치 위임장
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '2' and a.stat_cd = '77' THEN a.vr_sq ELSE NULL END ) as referin_s2,  -- 첨부파일 불일치 법인
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '3' and a.stat_cd = '77' THEN a.vr_sq ELSE NULL END ) as referin_s3,  -- 첨부파일 불일치 외국인
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '0' and b.stat_cd = '60' AND a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_n0, -- 첨부파일 일치 개인
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '1' and b.stat_cd = '60' AND a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_n1, -- 첨부파일 일치 위임장
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '2' and b.stat_cd = '60' AND a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_n2, -- 첨부파일 일치 법인
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '3' and b.stat_cd = '60' AND a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_n3, -- 첨부파일 일치 외국인
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '0' and a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_nn0,
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '1' and a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_nn1,
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '2' and a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_nn2,
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '3' and a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_nn3
                        FROM
                            v2_chg_stat_stats a
                        INNER JOIN v2_vrfc_req b ON b.vr_sq = a.vr_sq and b.vrfc_type = 'N'
                        LEFT JOIN v2_article_info_etc c1 on a.vr_sq = c1.vr_sq
                
                        WHERE 1 = 1 and a.insert_tm between CONCAT('{$data['sdate']}'  , ':00') AND CONCAT('{$data['edate']}'  , ':59') GROUP BY CASE a.insert_user WHEN 0 THEN 1 ELSE a.insert_user END ) b on b.insert_user = a.usr_sq 
            LEFT JOIN ( 
                            select 
                            f1.insert_user, 
                            count(distinct f1.vr_sq) mobile_upload 
                            from v2_files f1 
                            INNER JOIN v2_vrfc_req b ON b.vr_sq = f1.vr_sq and b.vrfc_type = 'N' 
                            where 
                                f1.insert_tm BETWEEN CONCAT('{$data['sdate']}'  , ':00') AND CONCAT('{$data['edate']}'  , ':59') 
                                and f1.file_type = '2' and f1.use_yn = 'Y' group by f1.insert_user 
                                ) c on c.insert_user = a.usr_sq 
            WHERE (b.insert_user is not null or c.insert_user is not null) ";

        if (!empty($data['dept_sq'])) {
            $sql .= "AND a.dept_sq = {$data['dept_sq']} ";
        }

        $query = $this->db->query($sql);

        return $query->getRow()->cnt;
    }

    public function getResultList($data)
    {
        $sql = "SELECT
                    a.usr_sq,
                    a.usr_id,
                    a.usr_nm,
                    IFNULL(c.mobile_upload, 0)mobile_upload ,
                        b.*
                        FROM
                        users a
                        left join (
                        SELECT
                            CASE
                            a.insert_user WHEN 0 THEN 1
                            ELSE a.insert_user
                            END insert_user,
                            COUNT(*) total_cnt ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '10' THEN a.vr_sq ELSE NULL END ) receipt_cnt ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '10' THEN a.vr_sq ELSE NULL END ) notassign_cnt ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '10' THEN NULL ELSE a.vr_sq END ) assign_cnt ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '19' THEN a.vr_sq ELSE NULL END ) cancel_cnt ,
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '30' THEN a.vr_sq ELSE NULL END ) phone_checking_cnt ,
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'D' AND a.stat_cd = '30' THEN a.vr_sq ELSE NULL END ) paper_checking_cnt ,  
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '35' THEN a.vr_sq ELSE NULL END ) phone_complete_cnt ,  -- 전화확인 완료
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'D' AND a.stat_cd = '35' THEN a.vr_sq ELSE NULL END ) paper_complete_cnt ,
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '38' THEN a.vr_sq ELSE NULL END ) phone_all_cnt , -- 전화확인 전체
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '39' THEN a.vr_sq ELSE NULL END ) phone_fail_cnt ,  -- 전화확인 실패
                            COUNT( DISTINCT CASE WHEN b.vrfc_type = 'D' AND a.stat_cd = '39' THEN a.vr_sq ELSE NULL END ) paper_fail_cnt ,
                            COUNT( DISTINCT case when (b.certRegister IS NOT NULL AND b.stat_cd = '60' AND a.stat_cd = '76') then a.vr_sq ELSE NULL END ) cert_same_reg_open ,
                            COUNT( DISTINCT case when (b.certRegister IS NOT NULL AND a.stat_cd = '77') then a.vr_sq ELSE NULL END ) cert_reg_fail_open, 
                            COUNT( DISTINCT case when (b.referenceFileUrl IS NOT NULL AND b.stat_cd = '60' AND a.stat_cd = '76') then a.vr_sq ELSE NULL END ) reference_same_reg_open ,
                            COUNT( DISTINCT case when (b.referenceFileUrl IS NOT NULL AND a.stat_cd = '77') then a.vr_sq ELSE NULL END ) reference_reg_fail_open ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '40' THEN a.vr_sq ELSE NULL END ) reg_checking_cnt ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '45' THEN a.vr_sq ELSE NULL END ) reg_complete_cnt ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '70' THEN a.vr_sq ELSE NULL END ) reg_none ,	-- 등기부 등본 없음
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '86' THEN a.vr_sq ELSE NULL END ) real_top_RS ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '88' THEN a.vr_sq ELSE NULL END ) real_top_RF ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '87' THEN a.vr_sq ELSE NULL END ) real_top_GS ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '89' THEN a.vr_sq ELSE NULL END ) real_top_GF ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '49' THEN a.vr_sq ELSE NULL END ) reg_fail_cnt ,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '60' THEN a.vr_sq ELSE NULL END ) complete_cnt ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '60' THEN a.vr_sq ELSE NULL END ) complete_cnt1,
                            COUNT( DISTINCT CASE WHEN a.stat_cd = '69' THEN a.vr_sq ELSE NULL END ) fail_cnt ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '69' THEN a.vr_sq ELSE NULL END ) fail_cnt1 ,
                            COUNT( CASE WHEN (a.stat_cd = '85') THEN a.vr_sq ELSE NULL END ) reg_fail_tempOpen ,
                            COUNT( CASE WHEN (a.stat_cd = '77') THEN a.vr_sq ELSE NULL END ) reg_fail_Open ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '60' AND a.stat_cd = '80' THEN a.vr_sq ELSE NULL END ) same_reg_tempOpen ,
                            COUNT( DISTINCT CASE WHEN b.stat_cd = '60' AND a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) same_reg_open,
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '0' and a.stat_cd = '77' THEN a.vr_sq ELSE NULL END ) as referin_s0,  -- 첨부파일 불일치 개인 
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '1' and a.stat_cd = '77' THEN a.vr_sq ELSE NULL END ) as referin_s1,  -- 첨부파일 불일치 위임장
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '2' and a.stat_cd = '77' THEN a.vr_sq ELSE NULL END ) as referin_s2,  -- 첨부파일 불일치 법인
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '3' and a.stat_cd = '77' THEN a.vr_sq ELSE NULL END ) as referin_s3,  -- 첨부파일 불일치 외국인
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '0' and b.stat_cd = '60' AND a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_n0, -- 첨부파일 일치 개인
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '1' and b.stat_cd = '60' AND a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_n1, -- 첨부파일 일치 위임장
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '2' and b.stat_cd = '60' AND a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_n2, -- 첨부파일 일치 법인
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '3' and b.stat_cd = '60' AND a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_n3, -- 첨부파일 일치 외국인
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '0' and a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_nn0,
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '1' and a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_nn1,
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '2' and a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_nn2,
                            COUNT( DISTINCT CASE WHEN ( ( if ( b.referenceFileUrl = '[]' , NULL , b.referenceFileUrl) ) IS NOT NULL ) and c1.ownerTypeCode = '3' and a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) as referin_nn3
                        FROM
                            v2_chg_stat_stats a
                        INNER JOIN v2_vrfc_req b ON b.vr_sq = a.vr_sq and b.vrfc_type = 'N'
                        LEFT JOIN v2_article_info_etc c1 on a.vr_sq = c1.vr_sq
                
                        WHERE 1 = 1 and a.insert_tm between CONCAT('{$data['sdate']}'  , ':00') AND CONCAT('{$data['edate']}'  , ':59') GROUP BY CASE a.insert_user WHEN 0 THEN 1 ELSE a.insert_user END ) b on b.insert_user = a.usr_sq 
            LEFT JOIN ( 
                            select 
                            f1.insert_user, 
                            count(distinct f1.vr_sq) mobile_upload 
                            from v2_files f1 
                            INNER JOIN v2_vrfc_req b ON b.vr_sq = f1.vr_sq and b.vrfc_type = 'N' 
                            where 
                                f1.insert_tm BETWEEN CONCAT('{$data['sdate']}'  , ':00') AND CONCAT('{$data['edate']}'  , ':59') 
                                and f1.file_type = '2' and f1.use_yn = 'Y' group by f1.insert_user 
                                ) c on c.insert_user = a.usr_sq 
            WHERE (b.insert_user is not null or c.insert_user is not null) ";

        if (!empty($data['dept_sq'])) {
            $sql .= "AND a.dept_sq = {$data['dept_sq']} ";
        }

        $query = $this->db->query($sql);

        return $query->getResultArray();
    }

}