<?php
namespace App\Models\results;

use CodeIgniter\Model;

class M416Model 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(*) AS cnt FROM (
                SELECT 
                    a.usr_id
                FROM indi_distance_daily a
                INNER JOIN users b ON a.usr_id = b.usr_id INNER JOIN departments c ON b.dept_sq = c.dept_sq
                WHERE a.`date` BETWEEN '{$data['sdate']}' AND '{$data['edate']}' ";

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

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

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


        $sql .= "GROUP BY a.usr_id, a.usr_nm ) AS t";

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

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


    public function getResultList($data)
    {
        $lastDay = (int) date('t', strtotime($data['edate']));

        $sql = "SELECT 
	                a.usr_id, a.usr_nm
	                , SUM(a.shoot_cnt) shoot_cnt ";

        if ($lastDay > 0) {
            for ($i = 1; $i <= $lastDay; $i++) {
                $day2 = sprintf('%02d', $i);
                $sql .= ", SUM(CASE WHEN DAY(a.date) = {$i} THEN distance ELSE 0 END) AS d{$day2}";
            }
        }

        $sql .= ", ROUND(SUM(a.distance),1) tot_distance ";

        $sql .= "
                FROM indi_distance_daily a
                INNER JOIN users b ON a.usr_id = b.usr_id INNER JOIN departments c ON b.dept_sq = c.dept_sq
                WHERE 
                    a.`date` BETWEEN '{$data['sdate']}' AND '{$data['edate']}' ";

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

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

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

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


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


        return $query->getResultArray();
    }
}