<?php
namespace App\Models\article;

use CodeIgniter\Model;

class DelChgAptModel extends Model
{

    public function getTotalCount($data)
    {
        $sql = "SELECT COUNT(*) AS cnt FROM apt_receipt WHERE 1=1 ";

        if (!empty($data['hscpNo'])) {
            $sql .= "AND hscp_no LIKE CONCAT('%', '{$data['hscpNo']}', '%')";
        }

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

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

    public function getAptLists($start, $end, $data)
    {
        $sql = "SELECT rcpt_no, hscp_no, rcpt_hscp_nm FROM apt_receipt WHERE 1=1 ";

        if (!empty($data['hscpNo'])) {
            $sql .= "AND hscp_no LIKE CONCAT('%', '{$data['hscpNo']}', '%')";
        }

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

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

        return $query->getResultArray();
    }


    public function chkExistAptHscp($data)
    {
        $sql = "SELECT COUNT(*) AS cnt FROM apt_receipt WHERE 1=1 ";

        $sql .= "AND hscp_no = '{$data['hscpNo']}' ";
        $sql .= "AND rcpt_no != {$data['rcptNo']} ";

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

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

    // 단지코드 정보 변경
    public function saveAptHscp($data)
    {
        $this->db->transStart();

        $sql = "UPDATE apt_receipt SET ";
        $sql .= "hscp_no = {$data['hscpNo']} ";
        $sql .= "WHERE rcpt_no = {$data['rcptNo']} ";

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

        $sql = "UPDATE apt_result SET ";
        $sql .= "hscp_no = {$data['hscpNo']} ";
        $sql .= "WHERE rcpt_no = {$data['rcptNo']} ";

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

        $this->db->transComplete();

        // 성공
        return [
            'success' => true,
        ];
    }

    // 아파트 정보 삭제
    public function deleteAptHscp($data)
    {
        $this->db->transStart();

        $sql = "DELETE FROM apt_history WHERE rcpt_no = {$data['rcptNo']} ";
        if ($this->db->query($sql) === false) {
            return [
                'success' => false,
                'msg' => '저장실패',
            ];
        }

        $sql = "DELETE FROM apt_category WHERE rcpt_no = {$data['rcptNo']} ";
        if ($this->db->query($sql) === false) {
            return [
                'success' => false,
                'msg' => '저장실패',
            ];
        }

        $sql = "DELETE FROM apt_photo WHERE rcpt_no = {$data['rcptNo']} ";
        if ($this->db->query($sql) === false) {
            return [
                'success' => false,
                'msg' => '저장실패',
            ];
        }

        $sql = "DELETE FROM apt_result WHERE rcpt_no = {$data['rcptNo']} ";
        if ($this->db->query($sql) === false) {
            return [
                'success' => false,
                'msg' => '저장실패',
            ];
        }

        $sql = "DELETE FROM apt_receipt WHERE rcpt_no = {$data['rcptNo']} ";
        if ($this->db->query($sql) === false) {
            return [
                'success' => false,
                'msg' => '저장실패',
            ];
        }

        $this->db->transComplete();

        // 성공
        return [
            'success' => true,
        ];
    }

}