 <?
defined('BASEPATH') OR exit('No direct script access allowed');

class m_bbs_question extends CI_Model{

	// 생성자, 해당 모델이 생성될때 항상 실행되는 초기화작업
	function __construct(){
		parent::__construct();

        $this->load->database();
        $this->load->library('session');
	}

	#---------------------------------------------------------------------------
    # PURPOSE :	1:1문의 - list
    # CREATE  :	2018-02-27
    #---------------------------------------------------------------------------
	function bbs_question_list($req_data){
        # request
        $offset                 = $req_data['offset'];
        $limit                  = $req_data['limit'];

        $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];

        $search_click1 = ''; $search_click2 = '';
        if(isset($req_data['search_click1'])==true)      $search_click1 = $req_data['search_click1'];
        if(isset($req_data['search_click2'])==true)      $search_click2 = $req_data['search_click2'];

        if($search_click1=='1') {
            $today_date             = date("Y-m-d").' 00:00:00';
            $this->db->where('reg_date >=', $today_date);
        }

        if($search_click2=='1') {
            $this->db->like('is_reply', 'Y');
        } else if($search_click2=='2') {
            $this->db->like('is_reply', 'N');
        }

        if($search_type!='' && $search_keyword!='') $this->db->like($search_type, $search_keyword);
        $this->db->where('is_del', 'N');
        $this->db->from('bbs_question');
        $this->db->order_by('p_idx desc, s_idx asc');
        $this->db->limit($limit, $offset);
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	1:1문의 - list - total count
    # CREATE  :	2018-02-27
    #---------------------------------------------------------------------------
	function bbs_question_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];

        $search_click1 = ''; $search_click2 = '';
        if(isset($req_data['search_click1'])==true)      $search_click1 = $req_data['search_click1'];
        if(isset($req_data['search_click2'])==true)      $search_click2 = $req_data['search_click2'];

        if($search_click1=='1') {
            $today_date             = date("Y-m-d").' 00:00:00';
            $this->db->where('reg_date >=', $today_date);
        }

        if($search_click2=='1') {
            $this->db->like('is_reply', 'Y');
        } else if($search_click2=='2') {
            $this->db->like('is_reply', 'N');
        }

        if($search_type!='' && $search_keyword!=''){
            $this->db->like($search_type, $search_keyword);
        }
        $this->db->where('is_del', 'N');
        $this->db->from('bbs_question');
        $row = $this->db->count_all_results();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	1:1문의 - select
    # CREATE  :	2018-02-27
    #---------------------------------------------------------------------------
	function bbs_question_select($req_data){
	    # request
	    $idx            = $req_data;

        $this->db->where('idx', $idx);
        $this->db->where('is_del', 'N');
        $this->db->from('bbs_question');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	1:1문의 reply - insert
    # CREATE  :	2018-02-27
    #---------------------------------------------------------------------------
	function bbs_question_reply_insert($req_data){
        # request
	    $idx            = $req_data['p_idx'];

        $user_id        = $this->session->userdata('Adm_Id');
        $user_name      = $this->session->userdata('Adm_Name');
        $now_date       = date("Y-m-d H:i:s");

        $this->db->select('max(s_idx) + 1 as s_idx');
	    $this->db->where('p_idx', $idx);
        $this->db->from('bbs_question');
        $res1 = $this->db->get();
        $row1 = $res1->result_array();

        $s_idx = $row1[0]['s_idx'];

        $req_data['write_id']   = $user_id;
        $req_data['write_name'] = $user_name;
        $req_data['reg_date']   = $now_date;
        $req_data['update_id']  = $user_id;
        $req_data['update_date']= $now_date;
        $req_data['s_idx']      = $s_idx;
        $req_data['p_idx']      = $idx;
        $req_data['is_reply']   = 'Y';
        $row = $this->db->insert('bbs_question', $req_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	1:1문의 is_reply - update
    # CREATE  :	2018-02-27
    #---------------------------------------------------------------------------
	function bbs_question_is_reply_update($req_data){
        # request
	    $idx            = $req_data;

        $user_id        = $this->session->userdata('Adm_Id');
        $now_date       = date("Y-m-d H:i:s");

        $update_data = array('is_reply'         => 'Y',
                             'update_id'        => $user_id,
                             'update_date'      => $now_date
        );
        $this->db->where('idx', $idx);
        $row = $this->db->update('bbs_question', $update_data);

        return $row;

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	1:1문의 is_del update
    # CREATE  :	2018-02-27
    #---------------------------------------------------------------------------
	function bbs_question_is_del_update($req_data){
        $user_id    = $this->session->userdata('Adm_Id');
        $now_date   = date("Y-m-d H:i:s");

        $arr_update_data = array();
        foreach($req_data as $key=>$val) {
            $arr_update_data[$key]['idx']           = $val;
            $arr_update_data[$key]['is_del']        = 'Y';
            $arr_update_data[$key]['update_id']     = $user_id;
            $arr_update_data[$key]['update_date']   = $now_date;
        }
        $this->db->update_batch('bbs_question', $arr_update_data, 'idx');

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	1:1문의 신규 count
    # CREATE  :	2018-02-27
    #---------------------------------------------------------------------------
	function bbs_question_new_count($req_data){

	    $search_click2 = '';
        if(isset($req_data['search_click2'])==true)      $search_click2 = $req_data['search_click2'];

        if($search_click2=='1') {
            $this->db->like('is_reply', 'Y');
        } else if($search_click2=='2') {
            $this->db->like('is_reply', 'N');
        }

	    $now_date   = date("Y-m-d").' 00:00:00';
	    $this->db->where('reg_date >=', $now_date);
        $this->db->where('is_del', 'N');
        $this->db->where('s_idx', '1');
        $this->db->from('bbs_question');
        $row = $this->db->count_all_results();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	1:1문의 처리상태 count
    # CREATE  :	2018-02-27
    #---------------------------------------------------------------------------
	function bbs_question_group_by_is_reply($req_data){
	    $search_click1 = '';
        if(isset($req_data['search_click1'])==true)      $search_click1 = $req_data['search_click1'];

        if($search_click1=='1') {
            $today_date             = date("Y-m-d").' 00:00:00';
            $this->db->where('reg_date >=', $today_date);
        }


	    $this->db->select('is_reply, count(is_reply) as cnt');
	    $this->db->where('is_del', 'N');
        $this->db->where('s_idx', '1');
	    $this->db->group_by("is_reply");
        $this->db->from('bbs_question');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }





}
?>
