<?php
namespace App\Models\results;

use CodeIgniter\Model;

class AssignModel extends Model
{

    // 소속본부조회
    public function getBonbuList()
    {
        $sql = "SELECT dept_sq, pdept_sq, dept_nm, dept_desc, dept_head, use_yn, depth, insert_tm, insert_usr, update_tm, update_usr, lft, rgt" .
            "  FROM departments" .
            " WHERE depth = 1" .
            "   AND use_yn = 'Y'" .
            " ORDER BY lft";

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


        return $query->getResultArray();
    }

    // 소속팀 조회
    public function getTeamList()
    {
        $sql = "SELECT dept_sq, pdept_sq, dept_nm" .
            "  FROM departments" .
            " WHERE depth = 2" .
            "   AND use_yn = 'Y'" .
            " ORDER BY dept_nm";

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


        return $query->getResultArray();
    }

    // 지역 목록 조회
    public function getAreaList($sido = '', $gugun = '')
    {

        if (!empty($gugun)) {
            $gugun = substr($gugun, '0', '5');

            $sql = "SELECT a.region_cd, TRIM(REPLACE(a.region_nm, b.region_nm, '')) region_nm" .
                " FROM region_codes a" .
                " LEFT JOIN region_codes b ON b.region_cd = CONCAT(SUBSTR(a.region_cd,1,5),'00000')" .
                " WHERE a.region_cd LIKE concat(?, '%')" .
                " AND a.region_cd NOT LIKE '%00000'" .
                " AND a.region_cd LIKE '%00'" .
                " AND a.use_yn = 'Y'" .
                " ORDER BY a.region_nm ASC";

            $query = $this->db->query($sql, [$gugun]);

        } else if (!empty($sido)) {
            $chk_sido = substr($sido, '0', '2');

            if ($chk_sido === '36') {
                $sido = substr($sido, '0', '4');
                $sql = "SELECT a.region_cd, TRIM(REPLACE(a.region_nm, b.region_nm, '')) region_nm " .
                    "FROM region_codes a " .
                    "LEFT JOIN region_codes b ON b.region_cd = CONCAT(SUBSTR(a.region_cd,1,4),'000000') " .
                    "WHERE a.region_cd LIKE concat(?, '%') " .
                    "AND a.region_cd NOT LIKE '%000000' " .
                    "AND a.region_cd LIKE '%00' " .
                    "AND a.use_yn = 'Y' " .
                    "AND EXISTS (SELECT 'x' FROM region_codes c WHERE c.region_cd LIKE CONCAT(SUBSTR(a.region_cd,1,5),'%') AND c.region_cd > CONCAT(SUBSTR(a.region_cd,1,5),'00000')) " .
                    "ORDER BY a.region_nm ASC";
            } else {
                $sido = substr($sido, '0', '2');
                $sql = "SELECT a.region_cd, TRIM(REPLACE(a.region_nm, b.region_nm, '')) region_nm" .
                    " FROM region_codes a" .
                    " LEFT JOIN region_codes b ON b.region_cd = CONCAT(SUBSTR(a.region_cd,1,2),'00000000')" .
                    " WHERE a.region_cd LIKE concat(?, '%')" .
                    " AND a.region_cd NOT LIKE '%00000000'" .
                    " AND a.region_cd LIKE '%00000'" .
                    " AND a.use_yn = 'Y'" .
                    " AND EXISTS (SELECT 'x' FROM region_codes c WHERE c.region_cd LIKE CONCAT(SUBSTR(a.region_cd,1,5),'%') AND c.region_cd > CONCAT(SUBSTR(a.region_cd,1,5),'00000'))" .
                    " ORDER BY a.region_nm ASC";
            }

            $query = $this->db->query($sql, [$sido]);
        } else {
            $sql = "SELECT a.region_cd, a.region_nm " .
                "FROM region_codes a " .
                "WHERE (a.region_cd LIKE '%00000000' " .
                "AND a.use_yn = 'Y') " .
                "OR region_cd = 3611000000;";

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


        return $query->getResultArray();
    }

    public function getTotalCount($data)
    {
        $sql = "SELECT	
                    COUNT(DISTINCT b.usr_sq) AS cnt
                FROM result a
                INNER JOIN users b ON b.usr_sq = a.usr_sq
                INNER JOIN receipt d ON d.rcpt_sq = a.rcpt_sq
                INNER JOIN departments c ON c.dept_sq = a.dept_sq

                WHERE 1=1 ";

        if (!empty($data['bonbu'])) {
            $sql .= "AND c.pdept_sq = {$data['bonbu']} ";
        }

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

        if (!empty($data['schDateGb'])) {
            if ($data['schDateGb'] === "1") {

                if (!empty($data['sdate'])) {
                    $sql .= "AND a.rsrv_date >= CONCAT('{$data['sdate']} 00:00:00') ";
                }

                if (!empty($data['edate'])) {
                    $sql .= "AND a.rsrv_date <= CONCAT('{$data['edate']} 23:59:59') ";
                }

            } else if ($data['schDateGb'] === "2") {
                if (!empty($data['sdate'])) {
                    $sql .= "AND d.rcpt_tm >= CONCAT('{$data['sdate']} 00:00:00') ";
                }

                if (!empty($data['edate'])) {
                    $sql .= "AND d.rcpt_tm <= CONCAT('{$data['edate']} 23:59:59') ";
                }
            }
        }

        if (!empty($data['srchTxt'])) {
            if ($data['srchType'] === "1") {
                $sql .= "AND b.usr_id like CONCAT('%', '{$data['srchTxt']}', '%' ) ";
            } else if ($data['srchType'] === "2") {
                $sql .= "AND b.usr_nm like CONCAT('%', '{$data['srchTxt']}', '%' ) ";
            } else {
                $sql .= "AND (
                            b.usr_id like CONCAT('%', '{$data['srchTxt']}', '%' ) 
                            OR b.usr_nm like CONCAT('%', '{$data['srchTxt']}', '%' )
                            ) ";
            }
        }

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

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

    public function getUserList($start, $end, $data)
    {
        $sql = "SELECT	
                    sub.*,
                    COUNT(*) OVER() as total_count
                FROM (
                    SELECT	
                        b.usr_nm, b.usr_id, b.usr_sq
                        , SUM(CASE WHEN a.rsrv_tm_hour IN ('00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24') THEN 1 ELSE 0 END) TODAY
                        , SUM(CASE WHEN a.rsrv_tm_ap = 'AM' AND a.rsrv_tm_hour = '09' THEN 1 ELSE 0 END) AM09
                        , SUM(CASE WHEN a.rsrv_tm_ap = 'AM' AND a.rsrv_tm_hour = '10' THEN 1 ELSE 0 END) AM10
                        , SUM(CASE WHEN a.rsrv_tm_ap = 'AM' AND a.rsrv_tm_hour = '11' THEN 1 ELSE 0 END) AM11
                        , SUM(CASE WHEN a.rsrv_tm_ap = 'AM' AND a.rsrv_tm_hour = '12' THEN 1 ELSE 0 END) AM12
                        , SUM(CASE WHEN a.rsrv_tm_ap = 'AM' AND a.rsrv_tm_hour IN ('00','01','02','03','04','05','06','07','08') THEN 1 ELSE 0 END) AMETC
                        , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('01', '13') THEN 1 ELSE 0 END) PM01
                        , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('02', '14') THEN 1 ELSE 0 END) PM02
                        , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('03', '15') THEN 1 ELSE 0 END) PM03
                        , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('04', '16') THEN 1 ELSE 0 END) PM04
                        , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('05', '17') THEN 1 ELSE 0 END) PM05
                        , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('06', '18') THEN 1 ELSE 0 END) PM06
                        , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('07', '19') THEN 1 ELSE 0 END) PM07
                        , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('00','08','09','10','11','12','20','21','22','23','24') THEN 1 ELSE 0 END) PMETC
                    FROM result a
                    INNER JOIN users b ON b.usr_sq = a.usr_sq
                    INNER JOIN receipt d ON d.rcpt_sq = a.rcpt_sq
                    INNER JOIN departments c ON c.dept_sq = a.dept_sq

                    WHERE 1=1 ";

        if (!empty($data['bonbu'])) {
            $sql .= "AND c.pdept_sq = {$data['bonbu']} ";
        }

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

        if (!empty($data['schDateGb'])) {
            if ($data['schDateGb'] === "1") {

                if (!empty($data['sdate'])) {
                    $sql .= "AND a.rsrv_date >= CONCAT('{$data['sdate']} 00:00:00') ";
                }

                if (!empty($data['edate'])) {
                    $sql .= "AND a.rsrv_date <= CONCAT('{$data['edate']} 23:59:59') ";
                }

            } else if ($data['schDateGb'] === "2") {
                if (!empty($data['sdate'])) {
                    $sql .= "AND d.rcpt_tm >= CONCAT('{$data['sdate']} 00:00:00') ";
                }

                if (!empty($data['edate'])) {
                    $sql .= "AND d.rcpt_tm <= CONCAT('{$data['edate']} 23:59:59') ";
                }
            }
        }

        if (!empty($data['srchTxt'])) {
            if ($data['srchType'] === "1") {
                $sql .= "AND b.usr_id like CONCAT('%', '{$data['srchTxt']}', '%' ) ";
            } else if ($data['srchType'] === "2") {
                $sql .= "AND b.usr_nm like CONCAT('%', '{$data['srchTxt']}', '%' ) ";
            } else {
                $sql .= "AND (
                            b.usr_id like CONCAT('%', '{$data['srchTxt']}', '%' ) 
                            OR b.usr_nm like CONCAT('%', '{$data['srchTxt']}', '%' )
                            ) ";
            }
        }

        $sql .= "GROUP BY b.usr_id, b.usr_nm
                ) sub
                LIMIT {$start}, {$end}";

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

        return $query->getResultArray();
    }

    // 엑셀 다운로드
    public function getExcelUserList($data)
    {
        $sql = "SELECT	
                    b.usr_id
                    , b.usr_nm 
                    , SUM(CASE WHEN a.rsrv_tm_hour IN ('00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24') THEN 1 ELSE 0 END) TODAY
                    , SUM(CASE WHEN a.rsrv_tm_ap = 'AM' AND a.rsrv_tm_hour = '09' THEN 1 ELSE 0 END) AM09
                    , SUM(CASE WHEN a.rsrv_tm_ap = 'AM' AND a.rsrv_tm_hour = '10' THEN 1 ELSE 0 END) AM10
                    , SUM(CASE WHEN a.rsrv_tm_ap = 'AM' AND a.rsrv_tm_hour = '11' THEN 1 ELSE 0 END) AM11
                    , SUM(CASE WHEN a.rsrv_tm_ap = 'AM' AND a.rsrv_tm_hour = '12' THEN 1 ELSE 0 END) AM12
                    , SUM(CASE WHEN a.rsrv_tm_ap = 'AM' AND a.rsrv_tm_hour IN ('00','01','02','03','04','05','06','07','08') THEN 1 ELSE 0 END) AMETC
                    , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('01', '13') THEN 1 ELSE 0 END) PM01
                    , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('02', '14') THEN 1 ELSE 0 END) PM02
                    , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('03', '15') THEN 1 ELSE 0 END) PM03
                    , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('04', '16') THEN 1 ELSE 0 END) PM04
                    , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('05', '17') THEN 1 ELSE 0 END) PM05
                    , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('06', '18') THEN 1 ELSE 0 END) PM06
                    , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('07', '19') THEN 1 ELSE 0 END) PM07
                    , SUM(CASE WHEN a.rsrv_tm_ap = 'PM' AND a.rsrv_tm_hour IN ('00','08','09','10','11','12','20','21','22','23','24') THEN 1 ELSE 0 END) PMETC
                FROM result a
                INNER JOIN users b ON b.usr_sq = a.usr_sq
                INNER JOIN receipt d ON d.rcpt_sq = a.rcpt_sq
                INNER JOIN departments c ON c.dept_sq = a.dept_sq

                WHERE 1=1 ";

        if (!empty($data['bonbu'])) {
            $sql .= "AND c.pdept_sq = {$data['bonbu']} ";
        }

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

        if (!empty($data['schDateGb'])) {
            if ($data['schDateGb'] === "1") {

                if (!empty($data['sdate'])) {
                    $sql .= "AND a.rsrv_date >= CONCAT('{$data['sdate']} 00:00:00') ";
                }

                if (!empty($data['edate'])) {
                    $sql .= "AND a.rsrv_date <= CONCAT('{$data['edate']} 23:59:59') ";
                }

            } else if ($data['schDateGb'] === "2") {
                if (!empty($data['sdate'])) {
                    $sql .= "AND d.rcpt_tm >= CONCAT('{$data['sdate']} 00:00:00') ";
                }

                if (!empty($data['edate'])) {
                    $sql .= "AND d.rcpt_tm <= CONCAT('{$data['edate']} 23:59:59') ";
                }
            }
        }

        if (!empty($data['srchTxt'])) {
            if ($data['srchType'] === "1") {
                $sql .= "AND usr_id like CONCAT('%', '{$data['srchTxt']}', '%' ) ";
            } else if ($data['srchType'] === "2") {
                $sql .= "AND usr_nm like CONCAT('%', '{$data['srchTxt']}', '%' ) ";
            } else {
                $sql .= "AND (
                            usr_id like CONCAT('%', '{$data['srchTxt']}', '%' ) 
                            OR usr_nm like CONCAT('%', '{$data['srchTxt']}', '%' )
                            ) ";
            }
        }

        $sql .= "GROUP BY b.usr_id, b.usr_nm ";

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

        return $query->getResultArray();
    }

}