<?php
namespace App\Models\v2;

use CodeIgniter\Model;

class M706Model extends Model
{
    // 지역 목록 조회
    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 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 getUserList()
    {
        $sql = "SELECT 
                    a.usr_sq, a.usr_id, a.usr_nm, a.dept_sq
                FROM users a
                WHERE 
                        a.usr_level IN ('3','4','40','5','50','6','60','61','62','7','8','70')
                        AND a.use_yn = 'Y'
                        AND EXISTS (
                        SELECT 'x' FROM departments a1 INNER JOIN departments a2 ON a2.lft BETWEEN a1.lft AND a1.rgt AND a2.use_yn = 'Y' 
                        WHERE 1=1 AND a2.dept_sq = a.dept_sq AND a1.use_yn = 'Y'
                )
                ORDER BY a.usr_level DESC, a.usr_nm ASC ";

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

        return $query->getResultArray();
    }

    public function getTotalCount($data)
    {
        $sql = "SELECT 
                    COUNT(*) AS cnt
                FROM v2_article_info a 
                JOIN v2_vrfc_req b ON a.vr_sq = b.vr_sq AND b.try_cnt >= 1 
                JOIN v2_modify_info c ON a.vr_sq = c.vr_sq 
                JOIN v2_article_info_etc c1 ON c1.vr_sq = a.vr_sq 
                LEFT JOIN region_codes f ON a.address_code = f.region_cd 
                LEFT JOIN v2_chg_stat d ON a.vr_sq = d.vr_sq AND d.stat_cd = '35' 
                LEFT JOIN v2_chg_stat e ON a.vr_sq = e.vr_sq AND e.stat_cd = '60' 
                LEFT JOIN codes g ON b.stat_cd = g.cd AND g.category = 'STEP_VERIFICATION' 
                LEFT JOIN codes h ON b.vrfc_type = h.cd AND h.category = 'VRFCREQ_WAY' 
                LEFT JOIN users i ON a.charger = i.usr_id 
                LEFT JOIN users j ON a.reg_charger = j.usr_id
                LEFT JOIN v2_chg_stat d2 ON d2.vr_sq = a.vr_sq AND d2.stat_cd = 39

                WHERE
                    1=1
                    AND b.insert_tm >= DATE_ADD(CURDATE(), INTERVAL -2 MONTH) ";


        // 매물번호
        if (!empty($data['atcl_no'])) {
            $sql .= "AND a.atcl_no = '{$data['atcl_no']}' ";
        } else {
            // 현재상태
            if (!empty($data['stat_cd'])) {
                $sql .= "AND b.stat_cd = '{$data['stat_cd']}' ";
            }

            // 중개소
            if (!empty($data['realtor_nm'])) {
                $sql .= "AND a.realtor_nm = '{$data['realtor_nm']}' ";
            }

            // 접수기간
            if (!empty($data['receipt_sdate'])) {
                $sql .= "AND b.insert_tm >= '{$data['receipt_sdate']} 00:00:00' ";
            }

            if (!empty($data['receipt_edate'])) {
                $sql .= "AND b.insert_tm <= '{$data['receipt_edate']} 23:59:59' ";
            }

            // 완료기간
            if (!empty($data['complete_sdate'])) {
                $sql .= "AND e.insert_tm >= '{$data['complete_sdate']} 00:00:00' ";
            }

            if (!empty($data['complete_edate'])) {
                $sql .= "AND e.insert_tm <= '{$data['complete_edate']} 23:59:59' ";
            }

            // 검증방식
            if (!empty($data['vrfcreq_way'])) {
                $sql .= "AND b.vrfc_type = '{$data['vrfcreq_way']}' ";
            }


            // 매체사
            if (!empty($data['rcpt_cpid'])) {
                $sql .= "AND a.cpid = '{$data['rcpt_cpid']}' ";
            }

            // 지역구분
            if (!empty($data['srcDong'])) {
                $sql .= "AND a.address_code = '{$data['srcDong']}' ";
            } else {
                if (!empty($data['srcGugun'])) {
                    $str_gugun = substr($data['srcGugun'], '0', '2');
                    if ($str_gugun == '36') { //세종시는 군구가 없고 바로 동이라서 예외
                        $sql .= "AND a.address_code = '{$data['srcGugun']}' ";
                    } else {
                        $gugunPrefix = substr($data['srcGugun'], '0', '5');
                        $sql .= "AND a.address_code LIKE '{$gugunPrefix}%' ";
                    }
                } else {
                    if (!empty($data['srcSido'])) {
                        $sidoPrefix = substr($data['srcSido'], '0', '2');
                        $sql .= "AND a.address_code LIKE '{$sidoPrefix}%' ";
                    }
                }
            }

            // 담당자
            if (!empty($data['damdang'])) {

                switch ($data['charger_gbn']) {
                    case "1":
                        $sql .= "a.charger = '{$data['damdang']}' ";
                        break;
                    case "2":
                        $sql .= "a.reg_charger = '{$data['damdang']}' ";
                        break;
                }

            } else {
                // 배정여부
                if ($data['assign_yn'] !== "A") {
                    switch ($data['charger_gbn'] . $data['assign_yn']) {
                        case "1Y":	// 전화/서류 담당자
                            $sql .= "a.charger != '' ";
                            break;
                        case "1N":	// 전화/서류 담당자                            
                            $sql .= "a.charger = '' ";
                            break;
                        case "2Y":	// 등기부등본 담당자
                            $sql .= "a.reg_charger != '' ";
                            break;
                        case "2N":	// 등기부등본 담당자
                            $sql .= "a.reg_charger IS NULL ";
                            break;
                    }
                }
            }

            // 본부
            if (!empty($data['bonbu'])) {
                if ($data['charger_gbn'] === "1") {
                    $sql .= "AND a.dept1_sq = '{$data['bonbu']}' ";
                } else {
                    $sql .= "AND a.reg_dept1_sq = '{$data['bonbu']}' ";
                }
            }

            // 팀
            if (!empty($data['team'])) {
                if ($data['charger_gbn'] === "1") {
                    $sql .= "AND a.dept2_sq = '{$data['team']}' ";
                } else {
                    $sql .= "AND a.reg_dept2_sq = '{$data['team']}' ";
                }
            }

        }

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

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

    public function getResultList($start, $end, $data)
    {
        $sql = "SELECT 
                    a.vr_sq,
					a.atcl_no,
					a.cpid,
					a.cp_atcl_id,
					a.rlet_type_cd,
					a.address1,
					a.sise,
					a.rdate,
					a.seller_tel_no,
					a.seller_nm,
					a.realtor_nm,
					a.realtor_tel_no,
					a.charger,
					b.insert_tm,
					b.stat_cd,
					c.bild_nm,
					b.vrfc_type,
					c.rm_no,
					c.floor,
					c.address_code,
					c.address2,
                    c1.address2a,
                    c1.address2b,
					c.address3,
					c.trade_type,
					c.deal_amt,
					c.wrrnt_amt,
					c.lease_amt,
					c.isale_amt,
					c.prem_amt,
					c.sply_spc,
					c.excls_spc,
					c.tot_spc,
					c.grnd_spc,
					c.bldg_spc,
					c.hscp_no,
					c.ptp_no,
					d.insert_tm as update_res_tm,
					e.insert_tm as result_tm,
					f.region_nm,
					g.cd_nm as pre_stat,
					h.cd_nm as vrfc_type,
					i.usr_nm,
					j.usr_nm as reg_charger,
					d2.insert_tm as stat_39_tm
                FROM v2_article_info a 
                JOIN v2_vrfc_req b ON a.vr_sq = b.vr_sq AND b.try_cnt >= 1 
                JOIN v2_modify_info c ON a.vr_sq = c.vr_sq 
                JOIN v2_article_info_etc c1 ON c1.vr_sq = a.vr_sq 
                LEFT JOIN region_codes f ON a.address_code = f.region_cd 
                LEFT JOIN v2_chg_stat d ON a.vr_sq = d.vr_sq AND d.stat_cd = '35' 
                LEFT JOIN v2_chg_stat e ON a.vr_sq = e.vr_sq AND e.stat_cd = '60' 
                LEFT JOIN codes g ON b.stat_cd = g.cd AND g.category = 'STEP_VERIFICATION' 
                LEFT JOIN codes h ON b.vrfc_type = h.cd AND h.category = 'VRFCREQ_WAY' 
                LEFT JOIN users i ON a.charger = i.usr_id 
                LEFT JOIN users j ON a.reg_charger = j.usr_id
                LEFT JOIN v2_chg_stat d2 ON d2.vr_sq = a.vr_sq AND d2.stat_cd = 39

                WHERE
                    1=1
                    AND b.insert_tm >= DATE_ADD(CURDATE(), INTERVAL -2 MONTH) ";


        // 매물번호
        if (!empty($data['atcl_no'])) {
            $sql .= "AND a.atcl_no = '{$data['atcl_no']}' ";
        } else {
            // 현재상태
            if (!empty($data['stat_cd'])) {
                $sql .= "AND b.stat_cd = '{$data['stat_cd']}' ";
            }

            // 중개소
            if (!empty($data['realtor_nm'])) {
                $sql .= "AND a.realtor_nm = '{$data['realtor_nm']}' ";
            }

            // 접수기간
            if (!empty($data['receipt_sdate'])) {
                $sql .= "AND b.insert_tm >= '{$data['receipt_sdate']} 00:00:00' ";
            }

            if (!empty($data['receipt_edate'])) {
                $sql .= "AND b.insert_tm <= '{$data['receipt_edate']} 23:59:59' ";
            }

            // 완료기간
            if (!empty($data['complete_sdate'])) {
                $sql .= "AND e.insert_tm >= '{$data['complete_sdate']} 00:00:00' ";
            }

            if (!empty($data['complete_edate'])) {
                $sql .= "AND e.insert_tm <= '{$data['complete_edate']} 23:59:59' ";
            }

            // 검증방식
            if (!empty($data['vrfcreq_way'])) {
                $sql .= "AND b.vrfc_type = '{$data['vrfcreq_way']}' ";
            }


            // 매체사
            if (!empty($data['rcpt_cpid'])) {
                $sql .= "AND a.cpid = '{$data['rcpt_cpid']}' ";
            }

            // 지역구분
            if (!empty($data['srcDong'])) {
                $sql .= "AND a.address_code = '{$data['srcDong']}' ";
            } else {
                if (!empty($data['srcGugun'])) {
                    $str_gugun = substr($data['srcGugun'], '0', '2');
                    if ($str_gugun == '36') { //세종시는 군구가 없고 바로 동이라서 예외
                        $sql .= "AND a.address_code = '{$data['srcGugun']}' ";
                    } else {
                        $gugunPrefix = substr($data['srcGugun'], '0', '5');
                        $sql .= "AND a.address_code LIKE '{$gugunPrefix}%' ";
                    }
                } else {
                    if (!empty($data['srcSido'])) {
                        $sidoPrefix = substr($data['srcSido'], '0', '2');
                        $sql .= "AND a.address_code LIKE '{$sidoPrefix}%' ";
                    }
                }
            }

            // 담당자
            if (!empty($data['damdang'])) {

                switch ($data['charger_gbn']) {
                    case "1":
                        $sql .= "a.charger = '{$data['damdang']}' ";
                        break;
                    case "2":
                        $sql .= "a.reg_charger = '{$data['damdang']}' ";
                        break;
                }

            } else {
                // 배정여부
                if ($data['assign_yn'] !== "A") {
                    switch ($data['charger_gbn'] . $data['assign_yn']) {
                        case "1Y":	// 전화/서류 담당자
                            $sql .= "a.charger != '' ";
                            break;
                        case "1N":	// 전화/서류 담당자                            
                            $sql .= "a.charger = '' ";
                            break;
                        case "2Y":	// 등기부등본 담당자
                            $sql .= "a.reg_charger != '' ";
                            break;
                        case "2N":	// 등기부등본 담당자
                            $sql .= "a.reg_charger IS NULL ";
                            break;
                    }
                }
            }

            // 본부
            if (!empty($data['bonbu'])) {
                if ($data['charger_gbn'] === "1") {
                    $sql .= "AND a.dept1_sq = '{$data['bonbu']}' ";
                } else {
                    $sql .= "AND a.reg_dept1_sq = '{$data['bonbu']}' ";
                }
            }

            // 팀
            if (!empty($data['team'])) {
                if ($data['charger_gbn'] === "1") {
                    $sql .= "AND a.dept2_sq = '{$data['team']}' ";
                } else {
                    $sql .= "AND a.reg_dept2_sq = '{$data['team']}' ";
                }
            }



        }

        $sql .= "ORDER BY b.vr_sq DESC , b.insert_tm DESC ";

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

        // print ($sql);

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

        return $query->getResultArray();
    }


    // 엑셀다운로드
    public function getExcelList($data)
    {
        $sql = "SELECT 
                    a.atcl_no AS '매물번호',
					g.cd_nm AS '진행상태',
					b.insert_tm AS '접수시간',
					h.cd_nm AS '검증방식',
					CONCAT(f.region_nm, ' ', c.rm_no) AS '주소',
					CONCAT(c.address2, ' ', c.address3) AS '상세주소',
					a.cpid AS '매체사',
					a.realtor_nm AS '중개소',					
					IFNULL(d.insert_tm, d2.insert_tm) AS '서류/전화 확인완료시간',
					i.usr_nm AS '담당자',
					a.reg_charger AS '등기부등본 담당자',
					e.insert_tm AS '검증완료 일시'
                FROM v2_article_info a 
                JOIN v2_vrfc_req b ON a.vr_sq = b.vr_sq AND b.try_cnt >= 1 
                JOIN v2_modify_info c ON a.vr_sq = c.vr_sq 
                JOIN v2_article_info_etc c1 ON c1.vr_sq = a.vr_sq 
                LEFT JOIN region_codes f ON a.address_code = f.region_cd 
                LEFT JOIN v2_chg_stat d ON a.vr_sq = d.vr_sq AND d.stat_cd = '35' 
                LEFT JOIN v2_chg_stat e ON a.vr_sq = e.vr_sq AND e.stat_cd = '60' 
                LEFT JOIN codes g ON b.stat_cd = g.cd AND g.category = 'STEP_VERIFICATION' 
                LEFT JOIN codes h ON b.vrfc_type = h.cd AND h.category = 'VRFCREQ_WAY' 
                LEFT JOIN users i ON a.charger = i.usr_id 
                LEFT JOIN users j ON a.reg_charger = j.usr_id
                LEFT JOIN v2_chg_stat d2 ON d2.vr_sq = a.vr_sq AND d2.stat_cd = 39

                WHERE
                    1=1
                    AND b.insert_tm >= DATE_ADD(CURDATE(), INTERVAL -2 MONTH) ";


        // 매물번호
        if (!empty($data['atcl_no'])) {
            $sql .= "AND a.atcl_no = '{$data['atcl_no']}' ";
        } else {
            // 현재상태
            if (!empty($data['stat_cd'])) {
                $sql .= "AND b.stat_cd = '{$data['stat_cd']}' ";
            }

            // 중개소
            if (!empty($data['realtor_nm'])) {
                $sql .= "AND a.realtor_nm = '{$data['realtor_nm']}' ";
            }

            // 접수기간
            if (!empty($data['receipt_sdate'])) {
                $sql .= "AND b.insert_tm >= '{$data['receipt_sdate']} 00:00:00' ";
            }

            if (!empty($data['receipt_edate'])) {
                $sql .= "AND b.insert_tm <= '{$data['receipt_edate']} 23:59:59' ";
            }

            // 완료기간
            if (!empty($data['complete_sdate'])) {
                $sql .= "AND e.insert_tm >= '{$data['complete_sdate']} 00:00:00' ";
            }

            if (!empty($data['complete_edate'])) {
                $sql .= "AND e.insert_tm <= '{$data['complete_edate']} 23:59:59' ";
            }

            // 검증방식
            if (!empty($data['vrfcreq_way'])) {
                $sql .= "AND b.vrfc_type = '{$data['vrfcreq_way']}' ";
            }


            // 매체사
            if (!empty($data['rcpt_cpid'])) {
                $sql .= "AND a.cpid = '{$data['rcpt_cpid']}' ";
            }

            // 지역구분
            if (!empty($data['srcDong'])) {
                $sql .= "AND a.address_code = '{$data['srcDong']}' ";
            } else {
                if (!empty($data['srcGugun'])) {
                    $str_gugun = substr($data['srcGugun'], '0', '2');
                    if ($str_gugun == '36') { //세종시는 군구가 없고 바로 동이라서 예외
                        $sql .= "AND a.address_code = '{$data['srcGugun']}' ";
                    } else {
                        $gugunPrefix = substr($data['srcGugun'], '0', '5');
                        $sql .= "AND a.address_code LIKE '{$gugunPrefix}%' ";
                    }
                } else {
                    if (!empty($data['srcSido'])) {
                        $sidoPrefix = substr($data['srcSido'], '0', '2');
                        $sql .= "AND a.address_code LIKE '{$sidoPrefix}%' ";
                    }
                }
            }

            // 담당자
            if (!empty($data['damdang'])) {

                switch ($data['charger_gbn']) {
                    case "1":
                        $sql .= "a.charger = '{$data['damdang']}' ";
                        break;
                    case "2":
                        $sql .= "a.reg_charger = '{$data['damdang']}' ";
                        break;
                }

            } else {
                // 배정여부
                if ($data['assign_yn'] !== "A") {
                    switch ($data['charger_gbn'] . $data['assign_yn']) {
                        case "1Y":	// 전화/서류 담당자
                            $sql .= "a.charger != '' ";
                            break;
                        case "1N":	// 전화/서류 담당자                            
                            $sql .= "a.charger = '' ";
                            break;
                        case "2Y":	// 등기부등본 담당자
                            $sql .= "a.reg_charger != '' ";
                            break;
                        case "2N":	// 등기부등본 담당자
                            $sql .= "a.reg_charger IS NULL ";
                            break;
                    }
                }
            }

            // 본부
            if (!empty($data['bonbu'])) {
                if ($data['charger_gbn'] === "1") {
                    $sql .= "AND a.dept1_sq = '{$data['bonbu']}' ";
                } else {
                    $sql .= "AND a.reg_dept1_sq = '{$data['bonbu']}' ";
                }
            }

            // 팀
            if (!empty($data['team'])) {
                if ($data['charger_gbn'] === "1") {
                    $sql .= "AND a.dept2_sq = '{$data['team']}' ";
                } else {
                    $sql .= "AND a.reg_dept2_sq = '{$data['team']}' ";
                }
            }



        }

        $sql .= "ORDER BY b.vr_sq DESC , b.insert_tm DESC ";

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

        return $query->getResultArray();
    }

    public function getRecordInfo($vr_sq, $file_type)
    {
        $sql = "SELECT seq, vr_sq, use_yn, file_type, view_odr, file_path, file_name, file_ext, file_size, img_width, img_height, meta_data, insert_user, insert_tm , cloud_upload_yn " .
            " FROM v2_files" .
            " WHERE vr_sq = ?" .
            " AND use_yn = 'Y'" .
            " AND file_type = ?" .
            " ORDER BY seq DESC";

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

        return $query->getRowArray();
    }

    // 상세정보
    public function getDetail($vr_sq)
    {
        $sql = "SELECT
                    a.vr_sq,
					a.dong_ho_chk,
					a.hscplqry_lv,
					b.tel_fail_cause,
					a.atcl_no,
					a.cpid,
					b.stat_cd,
					a.cp_atcl_id,
					a.rlet_type_cd,
					a.address1,
					a.sise,
					a.rdate,
					a.seller_tel_no,
					b.try_cnt,
					a.seller_nm,
					a.realtor_nm,
					a.realtor_tel_no,
					a.charger,
					b.insert_tm,
					a.reg_charger,
					i2.usr_nm as reg_charger_nm,
					c.bild_nm,
					b.vrfc_type as vrfc_type_cd,
					c.rm_no,
					c.floor,
					c.floor2,
					c.address_code,
					c.address2,
                    c1.address2a,
                    c1.address2b,
					c.address3,
                    c.address4,
					c.trade_type as trade_type_cd,
					c.deal_amt,
					c.wrrnt_amt,
					c.lease_amt,
					c.isale_amt,
					c.prem_amt,
					c.sply_spc,
					c.excls_spc,
					c.tot_spc,
					c.grnd_spc,
					c.bldg_spc,
					c.hscp_no,
					c.ptp_no,
					d.insert_tm as update_res_tm,
					e.insert_tm as result_tm,
					f.region_nm,
					g.cd_nm as pre_stat,
					g.cd as pre_stat_cd,
					h.cd_nm as vrfc_type,
					i.usr_nm,
					j.cd_nm as trade_type,
					c.hscp_nm, 
					c.ptp_nm,
					l.success,
					k.cd_nm as atcl_nm,
					m.code as result_d11,
					m.comment,
					n.code as fax_conf_yn_2,
					o.code as fax_conf_yn_3,
					p.code as fax_conf_yn_4,
					n.comment as fax_conf_yn_info_2,
					o.comment as fax_conf_yn_info_3,
					p.comment as fax_conf_yn_info_4,
					v.success AS tel_suc, 
					r.code AS tel_agree,
					s.code AS tel_conf_yn_2, 
					t.code AS tel_conf_yn_3, 
					u.code AS tel_conf_yn_4,
					s.comment AS tel_conf_yn_info_2, 
					t.comment AS tel_conf_yn_info_3,
					u.comment AS tel_conf_yn_info_4,
					w.success AS reg_conf_yn_1,
					x.code AS reg_conf_yn_2,
					y.code AS reg_conf_yn_3,
					x.comment AS reg_conf_yn_info_2,
					y.comment AS reg_conf_yn_info_3,
					b.rgbk_confirm,
				
					a.confirm_doc_img_url,
					a.cert_register,
				
					a.cert_register_save_yn,
					a.confirm_doc_img_url_save_yn
                FROM v2_article_info a
                JOIN v2_vrfc_req b ON a.vr_sq = b.vr_sq
                JOIN v2_modify_info c ON a.vr_sq = c.vr_sq
                JOIN v2_article_info_etc c1 ON c1.vr_sq = a.vr_sq
                LEFT JOIN region_codes f ON a.address_code = f.region_cd
                LEFT JOIN v2_chg_stat d ON a.vr_sq = d.vr_sq AND d.stat_cd = 35
                LEFT JOIN v2_chg_stat e ON a.vr_sq = e.vr_sq AND e.stat_cd = 60
                LEFT JOIN codes g ON b.stat_cd = g.cd AND g.category = 'STEP_VERIFICATION'
                LEFT JOIN codes h ON b.vrfc_type = h.cd AND h.category = 'VRFCREQ_WAY'
                LEFT JOIN codes j ON c.trade_type = j.cd AND j.category = 'TRADE_TYPE'
                LEFT JOIN codes k ON a.rlet_type_cd = k.cd AND k.category = 'ARTICLE_TYPE'
                LEFT JOIN v2_confirm l ON a.vr_sq = l.vr_sq AND l.vrfc_type = 'D'
                LEFT JOIN v2_check_list m ON a.vr_sq = m.vr_sq AND m.type = 'D11'
                LEFT JOIN v2_check_list n ON a.vr_sq = n.vr_sq AND n.type = 'D12'
                LEFT JOIN v2_check_list o ON a.vr_sq = o.vr_sq AND o.type = 'D13'
                LEFT JOIN v2_check_list p ON a.vr_sq = p.vr_sq AND p.type = 'D14'
                LEFT JOIN v2_confirm v ON a.vr_sq = v.vr_sq AND v.vrfc_type = 'T'
                LEFT JOIN v2_check_list r ON a.vr_sq = r.vr_sq AND r.type = 'T11'
                LEFT JOIN v2_check_list s ON a.vr_sq = s.vr_sq AND s.type = 'T12'
                LEFT JOIN v2_check_list t ON a.vr_sq = t.vr_sq AND t.type = 'T13'
                LEFT JOIN v2_check_list u ON a.vr_sq = u.vr_sq AND u.type = 'T14'
                LEFT JOIN v2_confirm w ON a.vr_sq = w.vr_sq AND w.vrfc_type = 'R'
                LEFT JOIN v2_check_list x ON a.vr_sq = x.vr_sq AND x.type = '21'
                LEFT JOIN v2_check_list y ON a.vr_sq = y.vr_sq AND y.type = '22'
                LEFT JOIN users i ON a.charger = i.usr_id
                LEFT JOIN users i2 ON a.reg_charger = i2.usr_id
                WHERE a.vr_sq = " . $vr_sq;


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

        return $query->getRowArray();
    }

    // 메모확인
    public function getMemo($vr_sq)
    {
        $sql = "SELECT memo FROM v2_vrfc_req where vr_sq = ?";

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

        return $query->getRowArray();
    }

    // 변경이력 조회
    public function getHistory($id)
    {
        $sql = "SELECT
                    a.seq,
                    a.vr_sq,
                    a.stat_cd,
                    a.chg_type,
                    a.insert_id,
                    a.insert_tm,
                    a.memo,
                    b.cd_nm as stat_cd_nm,
                    c.cd_nm as chg_type
                FROM
                    v2_chg_history a
                LEFT JOIN codes b ON a.stat_cd = b.cd AND b.category = 'STEP_VERIFICATION'							
                LEFT JOIN codes c ON a.chg_type = c.cd AND b.category = 'CHANGED_TYPE'
                WHERE
                    a.vr_sq = ?
                ORDER BY a.seq DESC ";

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

        return $query->getResultArray();
    }

}