<?php
namespace App\Models\v2;

use CodeIgniter\Model;

class M710Model 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_vrfc_req b 
                JOIN v2_article_info a ON a.vr_sq = b.vr_sq 
                JOIN v2_modify_info c ON c.vr_sq = a.vr_sq 
                LEFT JOIN v2_article_info_etc c1 ON c1.vr_sq = a.vr_sq 
                JOIN v2_stop_api_save_info k ON k.atcl_no = a.atcl_no AND k.status IN ('stop','wait','err') 
                LEFT JOIN region_codes f ON f.region_cd = a.address_code 
                LEFT JOIN v2_chg_stat d ON d.vr_sq = a.vr_sq AND d.stat_cd = '35' 
                LEFT JOIN v2_chg_stat d45 ON d45.vr_sq = a.vr_sq AND d45.stat_cd = '45' 
                LEFT JOIN v2_chg_stat d49 ON d49.vr_sq = a.vr_sq AND d49.stat_cd = '49' 
                LEFT JOIN v2_chg_stat e ON e.vr_sq = a.vr_sq AND e.stat_cd = '60' 
                LEFT JOIN codes g ON g.cd = b.stat_cd AND g.category = 'STEP_VERIFICATION' 
                LEFT JOIN codes h ON h.cd = b.vrfc_type AND h.category = 'VRFCREQ_WAY' 
                LEFT JOIN users i ON i.usr_id = a.charger 
                LEFT JOIN users j ON j.usr_id = a.reg_charger 
                LEFT JOIN v2_chg_stat d2 ON d2.vr_sq = a.vr_sq AND d2.stat_cd = '39' 
                WHERE b.insert_tm >= DATE_ADD(CURDATE(), INTERVAL -2 MONTH) ";


        // 매물번호
        if (!empty($data['atcl_no'])) {
            $sql .= "AND a.atcl = '{$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['rlet_type_cd'])) {
                $sql .= "AND a.rlet_type_cd = '{$data['rlet_type_cd']}' ";
            }


            // 접수기간
            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 b.insert_tm >= '{$data['complete_sdate']} 00:00:00' ";
            }

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

            // 검증방식
            if (!empty($data['vrfc_type_sub'])) {
                $sql .= "AND a.vrfc_type_sub = '{$data['vrfc_type_sub']}' ";
            } else {
                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']}' ";
                }
            }

            // 매물종류
            if (!empty($data['rlet_type_cd'])) {
                $sql .= "AND a.rlet_type_cd = '{$data['rlet_type_cd']}' ";
            }

            // 참고파일
            if (!empty($data['status'])) {
                $sql .= "AND k.status = '{$data['status']}' ";
            }

        }

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

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

    public function getResultList($start, $end, $data)
    {
        $sql = "SELECT
                    a.vr_sq,
					j.usr_nm as reg_charger,
					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.rlet_type_cd,
					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,
					greatest(ifnull(d45.insert_tm, ''), ifnull(d49.insert_tm, '')) as rgbk_check_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,
					d2.insert_tm as stat_39_tm,
					a.vrfc_type_sub,
					k.status
                FROM v2_vrfc_req b 
                JOIN v2_article_info a ON a.vr_sq = b.vr_sq 
                JOIN v2_modify_info c ON c.vr_sq = a.vr_sq 
                LEFT JOIN v2_article_info_etc c1 ON c1.vr_sq = a.vr_sq 
                JOIN v2_stop_api_save_info k ON k.atcl_no = a.atcl_no AND k.status IN ('stop','wait','err') 
                LEFT JOIN region_codes f ON f.region_cd = a.address_code 
                LEFT JOIN v2_chg_stat d ON d.vr_sq = a.vr_sq AND d.stat_cd = '35' 
                LEFT JOIN v2_chg_stat d45 ON d45.vr_sq = a.vr_sq AND d45.stat_cd = '45' 
                LEFT JOIN v2_chg_stat d49 ON d49.vr_sq = a.vr_sq AND d49.stat_cd = '49' 
                LEFT JOIN v2_chg_stat e ON e.vr_sq = a.vr_sq AND e.stat_cd = '60' 
                LEFT JOIN codes g ON g.cd = b.stat_cd AND g.category = 'STEP_VERIFICATION' 
                LEFT JOIN codes h ON h.cd = b.vrfc_type AND h.category = 'VRFCREQ_WAY' 
                LEFT JOIN users i ON i.usr_id = a.charger 
                LEFT JOIN users j ON j.usr_id = a.reg_charger 
                LEFT JOIN v2_chg_stat d2 ON d2.vr_sq = a.vr_sq AND d2.stat_cd = '39' 
                WHERE b.insert_tm >= DATE_ADD(CURDATE(), INTERVAL -2 MONTH) ";


        // 매물번호
        if (!empty($data['atcl_no'])) {
            $sql .= "AND a.atcl = '{$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['rlet_type_cd'])) {
                $sql .= "AND a.rlet_type_cd = '{$data['rlet_type_cd']}' ";
            }


            // 접수기간
            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 b.insert_tm >= '{$data['complete_sdate']} 00:00:00' ";
            }

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

            // 검증방식
            if (!empty($data['vrfc_type_sub'])) {
                $sql .= "AND a.vrfc_type_sub = '{$data['vrfc_type_sub']}' ";
            } else {
                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']}' ";
                }
            }

            // 매물종류
            if (!empty($data['rlet_type_cd'])) {
                $sql .= "AND a.rlet_type_cd = '{$data['rlet_type_cd']}' ";
            }

            // 참고파일
            if (!empty($data['status'])) {
                $sql .= "AND k.status = '{$data['status']}' ";
            }

        }

        $sql .= "ORDER BY b.vr_sq DESC , b.insert_tm DESC ";
        $sql .= "LIMIT {$start}, {$end}";


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

        return $query->getResultArray();
    }

    // 엑셀다운로드
    public function getExcelList($data)
    {
        $sql = "SELECT
                    CASE 
						WHEN k.`status` = 'stop' THEN '전송전'
						WHEN k.`status` = 'wait' THEN '전송중'
						WHEN k.`status` = 'err' THEN '오류'
					END AS '전송여부',
					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 '중개소',
					i.usr_nm AS '서류/전화 담당자',
					IFNULL(d.insert_tm, d2.insert_tm) AS '서류/전화 확인완료시간',
					a.reg_charger AS '등기부등본 담당자',
					greatest(ifnull(d45.insert_tm, ''), ifnull(d49.insert_tm, '')) AS '등기부등본 확인시간',
					e.insert_tm AS '검증완료 일시'
                FROM v2_vrfc_req b 
                JOIN v2_article_info a ON a.vr_sq = b.vr_sq 
                JOIN v2_modify_info c ON c.vr_sq = a.vr_sq 
                LEFT JOIN v2_article_info_etc c1 ON c1.vr_sq = a.vr_sq 
                JOIN v2_stop_api_save_info k ON k.atcl_no = a.atcl_no AND k.status IN ('stop','wait','err') 
                LEFT JOIN region_codes f ON f.region_cd = a.address_code 
                LEFT JOIN v2_chg_stat d ON d.vr_sq = a.vr_sq AND d.stat_cd = '35' 
                LEFT JOIN v2_chg_stat d45 ON d45.vr_sq = a.vr_sq AND d45.stat_cd = '45' 
                LEFT JOIN v2_chg_stat d49 ON d49.vr_sq = a.vr_sq AND d49.stat_cd = '49' 
                LEFT JOIN v2_chg_stat e ON e.vr_sq = a.vr_sq AND e.stat_cd = '60' 
                LEFT JOIN codes g ON g.cd = b.stat_cd AND g.category = 'STEP_VERIFICATION' 
                LEFT JOIN codes h ON h.cd = b.vrfc_type AND h.category = 'VRFCREQ_WAY' 
                LEFT JOIN users i ON i.usr_id = a.charger 
                LEFT JOIN users j ON j.usr_id = a.reg_charger 
                LEFT JOIN v2_chg_stat d2 ON d2.vr_sq = a.vr_sq AND d2.stat_cd = '39' 
                WHERE b.insert_tm >= DATE_ADD(CURDATE(), INTERVAL -2 MONTH) ";


        // 매물번호
        if (!empty($data['atcl_no'])) {
            $sql .= "AND a.atcl = '{$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['rlet_type_cd'])) {
                $sql .= "AND a.rlet_type_cd = '{$data['rlet_type_cd']}' ";
            }


            // 접수기간
            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 b.insert_tm >= '{$data['complete_sdate']} 00:00:00' ";
            }

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

            // 검증방식
            if (!empty($data['vrfc_type_sub'])) {
                $sql .= "AND a.vrfc_type_sub = '{$data['vrfc_type_sub']}' ";
            } else {
                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']}' ";
                }
            }

            // 매물종류
            if (!empty($data['rlet_type_cd'])) {
                $sql .= "AND a.rlet_type_cd = '{$data['rlet_type_cd']}' ";
            }

            // 참고파일
            if (!empty($data['status'])) {
                $sql .= "AND k.status = '{$data['status']}' ";
            }

        }

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

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

        return $query->getResultArray();
    }

    // 전송요청 api 수정
    public function updateSendApi($type)
    {
        $sql = "UPDATE v2_stop_api_save_info SET status = 'wait'
                WHERE type like CONCAT(?, '%') AND status = 'stop'
                ";

        if ($this->db->query($sql, [$type]) === false) {
            return [
                'success' => false,
                'msg' => '저장 실패',
            ];
        }

        return [
            'success' => true,
        ];
    }

    public function insert_v2_stop_api_save_info($atcl_no, $vr_sq, $type, $fax_sq)
    {
        $sql = "INSERT INTO v2_stop_api_save_info(atcl_no,vr_sq,type,status,stop_dt,fax_sq)" .
            " VALUE(?,?,?,'stop',now(),?)";
        $date = [
            $atcl_no,
            $vr_sq,
            $type,
            $fax_sq
        ];
        $this->db->query($sql, $date);
    }


    public function get_send_yn($type)
    {
        $sql = "SELECT stop_yn
				FROM v2_stop_api_chg_stat
				WHERE TYPE = ?
				ORDER BY pk DESC 
				LIMIT 1";
        $date = array(
            $type
        );
        $query = $this->db->query($sql, $date);
        $return = $query->getRowArray();

        return $return;
    }
}