<?php
namespace App\Controllers\V2;

use CodeIgniter\Model;

class V2StDailyModel extends Model
{
  /**
   * 일자별 통계데이터에 값 집어 넣기...
   * 반환값 없음.
   * @param date $sd_date 날짜 값이 없을경우 현재날짜로 입력됨.
   * @param string $cpid CPID
   * @param string $gbn_cd 코드값 (category='STATISTICS_DAILY3')
   * @param int $cnt 더할 숫자..
   * @param string $cnt_type 숫자를 더할것인지.. 아니변 변경할것인지.
   */
  public function set_v2_st_daily($st_date, $cpid, $gbn_cd, $cnt, $cnt_type = 'add')
  {
    /**
     * insert into v2_st_daily (sd_date, cpid, gbn_cd, cnt) values ($sd_date, $cpid, $gbn_cd, $cnt) 
     * on duplicate key update cnt = values(cnt);			// $cnt_type='change'
     * on duplicate key update cnt = cnt + $cnt;	// $cnt_type='add'
     */
    if (empty($cnt))
      $cnt = '0';

    $sql_dup = "";
    switch (strtolower($cnt_type)) {
      case 'add':
        $sql_dup = "on duplicate key update cnt = cnt + " . $cnt;
        break;

      case 'change':
        $sql_dup = "on duplicate key update cnt = values(cnt)";
        break;
    }
    if (empty($st_date)) {
      $sql = "insert into v2_st_daily (st_date, cpid, gbn_cd, cnt) values (now(), ?, ?, ?)" . $sql_dup;
      $data = array(
        $cpid,
        $gbn_cd,
        $cnt
      );
    } else {
      $sql = "insert into v2_st_daily (st_date, cpid, gbn_cd, cnt) values (?, ?, ?, ?)" . $sql_dup;
      $data = array(
        $st_date,
        $cpid,
        $gbn_cd,
        $cnt
      );
    }
    $this->db->query($sql, $data);

    $return['error_number'] = $this->db->_error_number();
    $return['error_message'] = $this->db->_error_message();

    return $return;
  }

  /**
   * 지정한 매물의 요청번호를 이용하여 요청내역을 가져온다.
   */
  public function get_v2_vrfc_req($vr_sq)
  {
    $columns = "vr_sq,atcl_no,step,cpid,cp_atcl_id,trade_type,realtor_nm,realtor_tel_no,seller_tel_no,vrfc_type,rgbk_confirm,req_type,rdate,stat_cd,try_cnt,insert_user,insert_tm,memo,contact_fail_cnt,sync_yn,reg_try_cnt";
    $builder = $this->db->table('v2_vrfc_req');
    $builder->select($columns, false);
    $builder->where('vr_sq', $vr_sq);
    $row = $builder->get()->getRowArray();
    return $row;
  }


}