<?php
namespace App\Models\webfax;

use CodeIgnier\Model;

class FaxModel extends Model
{
  /**
   * FAX 내용중 하나만 가져오기
   */
  public function selectFax($faxSq)
  {
    $sql = "SELECT mid, caller_no, recv_time, fax_sq, file_path, file_name, thumbnail, img_width, img_height, img_size" .
      " FROM fax_imgs" .
      " WHERE fax_sq = ?";
    $data = array($faxSq);
    $query = $this->db->query($sql, $data);
    return $query->getRowArray();
  }

  public function selectFaxListNotExistsThumb()
  {
    helper('cron');

    if (!isset($_SERVER["HOSTNAME"])) {
      $_SERVER["HOSTNAME"] = get_linux_hostname();
    }
    if (empty($_SERVER["HOSTNAME"])) {
      $_SERVER["HOSTNAME"] = "unknown";
    }

    $_SERVER["HOSTNAME"] = "unknown";
    $data = [$_SERVER["HOSTNAME"]];

    $sql = "UPDATE uds_tiff a" .
      " SET a.etc1 = ?" .
      " WHERE NOT EXISTS (SELECT 'x' FROM fax_imgs a1 WHERE a1.mid = a.mid)" .
      " AND a.recv_time >= DATE_ADD(NOW(), INTERVAL -7 DAY)" .
      " AND a.etc1 = ''" .
      " limit 100";
    $this->db->query($sql, $data);

    $sql = "SELECT a.mid,a.eid,a.pages,a.caller_no,a.callee_no,a.file_name,REPLACE(a.save_path, a.file_name,'') save_path,a.file_size,a.recv_time,a.save_time" .
      " FROM uds_tiff a" .
      " WHERE NOT EXISTS (SELECT 'x' FROM fax_imgs a1 WHERE a1.mid = a.mid)" .
      " AND a.recv_time >= DATE_ADD(NOW(), INTERVAL -7 DAY)" .
      " AND a.etc1 = ?";
    $query = $this->db->query($sql, $data);

    return $query->getResultArray();
  }

  /**
   * tiff 이미지 jpg로 변환 및 썸네일 생성
   */
  public function insertFaxImgs($mid, $file_name, $file_path, $thumbnail, $img_width, $img_height, $img_size, $qrcode = '', $caller_no, $callee_no, $tiff_file_name, $tiff_save_path, $tiff_file_size, $recv_time, $save_time, $receiver)
  {
    $sql = "INSERT INTO fax_imgs" .
      " (MID, file_name, file_path, thumbnail, img_width, img_height, img_size, qrcode, caller_no, callee_no, tiff_file_name, tiff_save_path, tiff_file_size, recv_time, save_time, receiver)" .
      " VALUES" .
      " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    $data = [$mid, $file_name, $file_path, $thumbnail, $img_width, $img_height, $img_size, $qrcode, $caller_no, $callee_no, $tiff_file_name, $tiff_save_path, $tiff_file_size, $recv_time, $save_time, $receiver];
    $this->db->query($sql, $data);
  }

  public function insertFaxImgs2($mid, $file_name, $file_path, $thumbnail, $img_width, $img_height, $img_size, $qrcode = '', $caller_no, $callee_no, $tiff_file_name, $tiff_save_path, $tiff_file_size, $recv_time, $save_time, $receiver)
  {
    $sql = "INSERT INTO fax_imgs" .
      " (MID, file_name, file_path, thumbnail, img_width, img_height, img_size, qrcode, caller_no, callee_no, tiff_file_name, tiff_save_path, tiff_file_size, recv_time, save_time, receiver)" .
      " VALUES" .
      " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    $data = [$mid, $file_name, $file_path, $thumbnail, $img_width, $img_height, $img_size, $qrcode, $caller_no, $callee_no, $tiff_file_name, $tiff_save_path, $tiff_file_size, $recv_time, $save_time, $receiver];
    $this->db->query($sql, $data);
  }

  public function selectFaxListTran()
  {

    if (!isset($_SERVER["HOSTNAME"])) {
      $_SERVER["HOSTNAME"] = get_linux_hostname();
    }

    $data = [$_SERVER["HOSTNAME"]];

    $sql = "SELECT TR_MSGID, TR_TITLE, TR_SENDFAXNUM, TR_RECVFAXNUM, TR_RECVTIME, TR_FILENAMELIST" .
      " FROM FC_RECV_TRAN a" .
      " WHERE NOT EXISTS (SELECT 'x' FROM fax_imgs a1 WHERE a1.mid = a.TR_MSGID AND a1.receiver != 'uds_tiff')" .
      " AND a.TR_RECVTIME >= DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -7 DAY), '%Y%m%d%h%i%s')";

    $query = $this->db->query($sql, $data);
    return $query->getResultArray();
  }


  /**
   * FAX이미지를 홍보확인서에서 선택할 경우...
   */
  public function updateImgSqOnFaxImgs($faxSq, $imgSq)
  {
    $sql = "UPDATE fax_imgs" .
      " SET img_sq = ?" .
      " WHERE fax_sq = ?";

    $data = [$imgSq, $faxSq];
    $this->db->query($sql, $data);
  }

  /**
   * 홍보확인서를 재지정했을 경우 기존의 홍보확인서는 선택되지 않음으로 ...
   */
  public function updateImgSqNullOnFaxImgs($imgSq)
  {
    $sql = "UPDATE fax_imgs" .
      " SET img_sq = null" .
      " WHERE img_sq = ?";
    $data = [$imgSq];
    $this->db->query($sql, $data);
  }

  /**
   * 비고를 지정한다.
   */
  public function updateRemark($faxSq, $remark)
  {
    $sql = "UPDATE fax_imgs" .
      " SET remark = ?" .
      " WHERE fax_sq = ?";
    $data = [$remark, $faxSq];
    $this->db->query($sql, $data);
  }

  /**
   * 홍보확인서의 주소가 업데이트 되었을 경우
   * @param bigint $vr_sq receipt 테이블의 PK (rcpt_sq) 또는 v2_vrfc_req 테이블의 PK (vr_sq)
   * @param string $address_code 법정동코드
   * @param string $address2 상세주소
   * @param string $address3 기타주소
   * @param string $hscp_no 단지번호
   * @param string $hscp_nm 단지명
   */
  public function updateAddress($vr_sq, $address_code, $address2, $address3, $hscp_no, $hscp_nm)
  {
    $data = [];
    if (!empty($address_code))
      $data['address_code'] = $address_code;
    if (!empty($address2))
      $data['address2'] = $address2;
    if (!empty($address3))
      $data['address3'] = $address3;
    // 		if (!empty($address4))		$data['address4'] = $address4;
    if (!empty($hscp_no))
      $data['hscp_no'] = $hscp_no;
    if (!empty($hscp_nm))
      $data['hscp_nm'] = $hscp_nm;

    if (!empty($data)) {
      $where = [
        'vr_sq' => $vr_sq
      ];
      $this->db->update('fax_imgs', $data, $where);
    }
  }

  public function updateAddress2($vr_sq, $address_code, $address2, $address2a, $address2b, $address3, $hscp_no, $hscp_nm)
  {
    $data = array();
    if (!empty($address_code))
      $data['address_code'] = $address_code;
    if (!empty($address2))
      $data['address2'] = $address2;
    if (!empty($address2))
      $data['address2a'] = $address2a;
    if (!empty($address2))
      $data['address2b'] = $address2b;
    if (!empty($address3))
      $data['address3'] = $address3;
    // 		if (!empty($address4))		$data['address4'] = $address4;
    if (!empty($hscp_no))
      $data['hscp_no'] = $hscp_no;
    if (!empty($hscp_nm))
      $data['hscp_nm'] = $hscp_nm;

    if (!empty($data)) {
      $where = [
        'vr_sq' => $vr_sq
      ];
      $this->db->update('fax_imgs', $data, $where);
    }
  }

  /**
   * 팩스건수 가져오기 -> 화면 최상단에 건수 보여줌.
   */
  public function getFaxCount()
  {
    $sql = "SELECT
				date_format(now(), '%H') as base_time 
				,(SELECT count(*) FROM uds_tiff where save_time >= concat(date_format(now(), '%Y-%m-%d %H'), ':00:00')) as enfax_count
				,(SELECT COUNT(*) FROM FC_RECV_TRAN where TR_RECVTIME >= concat(date_format(now(), '%Y%m%d%H'), '0000')) as lgfax_count
		";
    $query = $this->db->query($sql);

    $row = $query->getRowArray();

    return $row;
  }
}