<?php
namespace App\Models\results;

use CodeIgniter\Model;

class M409Model extends Model
{

    public function getCodeList($data)
    {
        $builder = $this->db->table('codes')
            ->select('category, cd, cd_nm, use_yn')
            ->whereIn('category', $data)
            ->where('use_yn', 'Y')
            ->orderBy('category', 'ASC')
            ->orderBy('view_odr', 'ASC');

        // if ($useYn !== null && $useYn !== '') {
        //     $builder->where('use_yn', $useYn);
        // }

        $rows = $builder->get()->getResultArray();

        $codes = [];
        foreach ($rows as $row) {
            $codes[$row['category']][] = [
                'cd' => $row['cd'],
                'cd_nm' => $row['cd_nm'],
            ];
        }

        return $codes;
    }
    public function getTotalCount($data)
    {
        $sql = "SELECT COUNT(*) AS cnt
                FROM (
                    SELECT DATE_FORMAT(b.insert_tm, '%Y-%m-%d') AS ref_date
                    FROM v2_vrfc_req a
                    INNER JOIN v2_chg_stat b
                        ON b.vr_sq = a.vr_sq
                    AND b.insert_tm >= '{$data['sdate']} 00:00:00'
                    AND b.insert_tm <=  '{$data['edate']} 23:59:59'
                    LEFT JOIN v2_confirm c
                        ON c.vr_sq = a.vr_sq AND c.vrfc_type = a.vrfc_type
                    LEFT JOIN v2_check_list d1
                        ON d1.vr_sq = a.vr_sq AND d1.vrfc_type = a.vrfc_type AND d1.type = CONCAT(d1.vrfc_type,'11')
                    LEFT JOIN v2_check_list d2
                        ON d2.vr_sq = a.vr_sq AND d2.vrfc_type = a.vrfc_type AND d2.type = CONCAT(d2.vrfc_type,'12')
                    LEFT JOIN v2_check_list d3
                        ON d3.vr_sq = a.vr_sq AND d3.vrfc_type = a.vrfc_type AND d3.type = CONCAT(d3.vrfc_type,'13')
                    LEFT JOIN v2_check_list d4
                        ON d4.vr_sq = a.vr_sq AND d4.vrfc_type = a.vrfc_type AND d4.type = CONCAT(d4.vrfc_type,'14')
                    LEFT JOIN v2_check_list e1
                        ON e1.vr_sq = a.vr_sq AND e1.vrfc_type = a.vrfc_type AND e1.type = '21'
                    LEFT JOIN v2_check_list e2
                        ON e2.vr_sq = a.vr_sq AND e2.vrfc_type = a.vrfc_type AND e2.type = '22'
                    WHERE 1=1 ";

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

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

        $sql .= "GROUP BY DATE_FORMAT(b.insert_tm, '%Y-%m-%d')
                ) AS t ";


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

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

    public function getResultList($data)
    {
        $sql = "SELECT 
                    DATE_FORMAT(b.insert_tm, '%Y-%m-%d') ref_date
                    , COUNT( DISTINCT CASE b.stat_cd WHEN '10' THEN a.vr_sq ELSE NULL END ) rcpt_cnt
                    , COUNT( DISTINCT CASE a.stat_cd WHEN '10' THEN a.vr_sq ELSE NULL END ) notassign_cnt
                    , COUNT( DISTINCT CASE a.stat_cd WHEN '10' THEN NULL ELSE a.vr_sq END ) assign_cnt
                    , COUNT( DISTINCT CASE WHEN a.vrfc_type = 'D' AND b.stat_cd = '35' THEN a.vr_sq ELSE NULL END) paper_cnt
                    , COUNT( DISTINCT CASE WHEN a.vrfc_type = 'T' AND b.stat_cd = '35' THEN a.vr_sq ELSE NULL END) phone_cnt
                    , COUNT( DISTINCT CASE b.stat_cd WHEN '19' THEN a.vr_sq ELSE NULL END) cancel_cnt
                    , COUNT( DISTINCT CASE ifnull(d1.code, '10000') WHEN '10000' THEN NULL ELSE a.vr_sq END) priv_cnt
                    , COUNT( DISTINCT CASE ifnull(d2.code, '10000') WHEN '10000' THEN NULL ELSE a.vr_sq END) info_cnt
                    , COUNT( DISTINCT CASE ifnull(d3.code, '10000') WHEN '10000' THEN NULL ELSE a.vr_sq END) price_cnt
                    , COUNT( DISTINCT CASE ifnull(d4.code, '10000') WHEN '10000' THEN NULL ELSE a.vr_sq END) owner_cnt
                    , COUNT( DISTINCT CASE b.stat_cd WHEN '39' THEN a.vr_sq ELSE NULL END) fail_cnt
                    , COUNT( DISTINCT CASE WHEN a.try_cnt > 0 THEN a.vr_sq ELSE NULL END) retry_cnt
                    , COUNT( DISTINCT CASE ifnull(e1.code, '10000') WHEN '10000' THEN NULL ELSE a.vr_sq END) reg_info_cnt
                    , COUNT( DISTINCT CASE ifnull(e2.code, '10000') WHEN '10000' THEN NULL ELSE a.vr_sq END) reg_owner_cnt
                    , COUNT( DISTINCT CASE b.stat_cd WHEN '60' THEN a.vr_sq ELSE NULL END) complete_cnt
                    , COUNT( DISTINCT CASE WHEN a.rgbk_confirm = '1' AND b.stat_cd = '60' THEN a.vr_sq ELSE NULL END) rgbk_cnt
                FROM v2_vrfc_req a
                INNER JOIN v2_chg_stat b ON b.vr_sq = a.vr_sq  AND  b.insert_tm BETWEEN CONCAT('{$data['sdate']} 00:00:00') AND CONCAT('{$data['edate']} 23:59:59')
                LEFT JOIN v2_confirm c ON c.vr_sq = a.vr_sq AND c.vrfc_type = a.vrfc_type
                LEFT JOIN v2_check_list d1 ON d1.vr_sq = a.vr_sq AND d1.vrfc_type = a.vrfc_type AND d1.type=CONCAT(d1.vrfc_type,'11')
                LEFT JOIN v2_check_list d2 ON d2.vr_sq = a.vr_sq AND d2.vrfc_type = a.vrfc_type AND d2.type=CONCAT(d2.vrfc_type,'12')
                LEFT JOIN v2_check_list d3 ON d3.vr_sq = a.vr_sq AND d3.vrfc_type = a.vrfc_type AND d3.type=CONCAT(d3.vrfc_type,'13')
                LEFT JOIN v2_check_list d4 ON d4.vr_sq = a.vr_sq AND d4.vrfc_type = a.vrfc_type AND d4.type=CONCAT(d4.vrfc_type,'14')
                LEFT JOIN v2_check_list e1 ON e1.vr_sq = a.vr_sq AND e1.vrfc_type = a.vrfc_type AND e1.type='21'
                LEFT JOIN v2_check_list e2 ON e2.vr_sq = a.vr_sq AND e2.vrfc_type = a.vrfc_type AND e2.type='22'
                WHERE 1=1 ";

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

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

        $sql .= "GROUP BY DATE_FORMAT(b.insert_tm, '%Y-%m-%d') ";

        // $sql .= "LIMIT {$start}, {$end} ";

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

        return $query->getResultArray();
    }

    // 엑셀 다운로드
    public function getExcelList($data)
    {
        $sql = "SELECT 
                    DATE_FORMAT(b.insert_tm, '%Y-%m-%d') ref_date
                    , COUNT( DISTINCT CASE b.stat_cd WHEN '10' THEN a.vr_sq ELSE NULL END ) rcpt_cnt
                    , COUNT( DISTINCT CASE a.stat_cd WHEN '10' THEN a.vr_sq ELSE NULL END ) notassign_cnt
                    , COUNT( DISTINCT CASE a.stat_cd WHEN '10' THEN NULL ELSE a.vr_sq END ) assign_cnt
                    , COUNT( DISTINCT CASE WHEN a.vrfc_type = 'D' AND b.stat_cd = '35' THEN a.vr_sq ELSE NULL END) paper_cnt
                    , COUNT( DISTINCT CASE WHEN a.vrfc_type = 'T' AND b.stat_cd = '35' THEN a.vr_sq ELSE NULL END) phone_cnt
                    , COUNT( DISTINCT CASE b.stat_cd WHEN '19' THEN a.vr_sq ELSE NULL END) cancel_cnt
                    , COUNT( DISTINCT CASE ifnull(d1.code, '10000') WHEN '10000' THEN NULL ELSE a.vr_sq END) priv_cnt
                    , COUNT( DISTINCT CASE ifnull(d2.code, '10000') WHEN '10000' THEN NULL ELSE a.vr_sq END) info_cnt
                    , COUNT( DISTINCT CASE ifnull(d3.code, '10000') WHEN '10000' THEN NULL ELSE a.vr_sq END) price_cnt
                    , COUNT( DISTINCT CASE ifnull(d4.code, '10000') WHEN '10000' THEN NULL ELSE a.vr_sq END) owner_cnt
                    , COUNT( DISTINCT CASE b.stat_cd WHEN '39' THEN a.vr_sq ELSE NULL END) fail_cnt
                    , COUNT( DISTINCT CASE WHEN a.try_cnt > 0 THEN a.vr_sq ELSE NULL END) retry_cnt
                    , COUNT( DISTINCT CASE ifnull(e1.code, '10000') WHEN '10000' THEN NULL ELSE a.vr_sq END) reg_info_cnt
                    , COUNT( DISTINCT CASE ifnull(e2.code, '10000') WHEN '10000' THEN NULL ELSE a.vr_sq END) reg_owner_cnt
                    , COUNT( DISTINCT CASE b.stat_cd WHEN '60' THEN a.vr_sq ELSE NULL END) complete_cnt
                    , COUNT( DISTINCT CASE WHEN a.rgbk_confirm = '1' AND b.stat_cd = '60' THEN a.vr_sq ELSE NULL END) rgbk_cnt
                FROM v2_vrfc_req a
                INNER JOIN v2_chg_stat b ON b.vr_sq = a.vr_sq  AND  b.insert_tm BETWEEN CONCAT('{$data['sdate']} 00:00:00') AND CONCAT('{$data['edate']} 23:59:59')
                LEFT JOIN v2_confirm c ON c.vr_sq = a.vr_sq AND c.vrfc_type = a.vrfc_type
                LEFT JOIN v2_check_list d1 ON d1.vr_sq = a.vr_sq AND d1.vrfc_type = a.vrfc_type AND d1.type=CONCAT(d1.vrfc_type,'11')
                LEFT JOIN v2_check_list d2 ON d2.vr_sq = a.vr_sq AND d2.vrfc_type = a.vrfc_type AND d2.type=CONCAT(d2.vrfc_type,'12')
                LEFT JOIN v2_check_list d3 ON d3.vr_sq = a.vr_sq AND d3.vrfc_type = a.vrfc_type AND d3.type=CONCAT(d3.vrfc_type,'13')
                LEFT JOIN v2_check_list d4 ON d4.vr_sq = a.vr_sq AND d4.vrfc_type = a.vrfc_type AND d4.type=CONCAT(d4.vrfc_type,'14')
                LEFT JOIN v2_check_list e1 ON e1.vr_sq = a.vr_sq AND e1.vrfc_type = a.vrfc_type AND e1.type='21'
                LEFT JOIN v2_check_list e2 ON e2.vr_sq = a.vr_sq AND e2.vrfc_type = a.vrfc_type AND e2.type='22'
                WHERE 1=1 ";

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

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

        $sql .= "GROUP BY DATE_FORMAT(b.insert_tm, '%Y-%m-%d') ";

        // $sql .= "LIMIT {$start}, {$end} ";

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

        return $query->getResultArray();
    }
}