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

class m_bbs_faq extends CI_Model{

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

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

    #---------------------------------------------------------------------------
    # PURPOSE :	faq - list
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function bbs_faq_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_category1       = $req_data['search_category1'];
        $search_category2       = $req_data['search_category2'];

        if($search_category1!='') {
            $this->db->where('a.faq_category_code1', $search_category1);
        }
        if($search_category2!='') {
            $this->db->where('a.faq_category_code2', $search_category2);
        }
        if($search_type!='' && $search_keyword!=''){
            $this->db->like($search_type, $search_keyword);
        }

        $this->db->select('a.*, b.category_name as category_name1, c.category_name as category_name2');
        $this->db->where('b.is_del', 'N');
        $this->db->where('c.is_del', 'N');
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.depth', '1');
        $this->db->where('c.depth', '2');
        $this->db->where('c.p_category_code = a.faq_category_code1');
        $this->db->from('bbs_faq as a');
        $this->db->join('bbs_faq_category as b', 'a.faq_category_code1 = b.category_code');
        $this->db->join('bbs_faq_category as c', 'a.faq_category_code2 = c.category_code');
        $this->db->order_by('a.idx desc');
        $this->db->limit($limit, $offset);
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	faq - list - total count
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function bbs_faq_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];

        $search_category1       = $req_data['search_category1'];
        $search_category2       = $req_data['search_category2'];

        if($search_category1!='') {
            $this->db->where('faq_category_code1', $search_category1);
        }
        if($search_category2!='') {
            $this->db->where('faq_category_code2', $search_category2);
        }

        if($search_type!='' && $search_keyword!=''){
            $this->db->like($search_type, $search_keyword);
        }

        $this->db->where('is_del', 'N');
        $this->db->from('bbs_faq');
        $row = $this->db->count_all_results();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	공지 - select
    # CREATE  :	2018-02-27
    #---------------------------------------------------------------------------
	function bbs_faq_select($req_data){
	    # request
	    $idx            = $req_data;

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

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	faq  - insert
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function bbs_faq_insert($req_data){
        $user_id        = $this->session->userdata('Adm_Id');
        $user_name      = $this->session->userdata('Adm_Name');
        $now_date       = date("Y-m-d H:i:s");

        $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;
        $row = $this->db->insert('bbs_faq', $req_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	faq - category_code count
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function bbs_faq_category_code_count($req_data){
	    # request
	    $depth                  = $req_data['depth'];
        $category_code          = $req_data['category_code'];


        if($depth == '1') {
            $this->db->where('faq_category_code1', $category_code);
        } else {
            $this->db->where('faq_category_code2', $category_code);
        }
        $this->db->where('is_del', 'N');
        $this->db->from('bbs_faq');
        $row = $this->db->count_all_results();

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	faq is_del update
    # CREATE  :	2018-02-27
    #---------------------------------------------------------------------------
	function bbs_faq_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_faq', $arr_update_data, 'idx');

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	faq 카테고리 - list
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function faq_category_list($req_data){
        # request
        $offset                 = $req_data['offset'];
        $limit                  = $req_data['limit'];

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

        if($search_type!='' && $search_keyword!=''){
            $this->db->like($search_type, $search_keyword);
        }
        $this->db->where('is_del', 'N');
        $this->db->from('bbs_faq_category');
        $this->db->order_by('p_category_code asc, depth asc, category_code asc');
        $this->db->limit($limit, $offset);
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	faq 카테고리 list용 - total count
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function faq_category_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];

        if($search_type!='' && $search_keyword!=''){
            $this->db->like($search_type, $search_keyword);
        }

        $this->db->where('is_del', 'N');
        $this->db->from('bbs_faq_category');
        $row = $this->db->count_all_results();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	faq 카테고리 - all list
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function faq_category_all_list(){
        $this->db->where('is_del', 'N');
        $this->db->from('bbs_faq_category');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	faq 카테고리  - insert
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function bbs_faq_category_insert($req_data){
	    # request
	    $category_name          = $req_data['category_name'];
        $depth                  = $req_data['depth'];
        $p_category_code        = $req_data['p_category_code'];

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


        $this->db->select('max(category_code) + 1 as category_code');
	    $this->db->where('depth', $depth);
        if($p_category_code!='')    $this->db->where('p_category_code', $p_category_code);
        $this->db->from('bbs_faq_category');
        $res1 = $this->db->get();
        $row1 = $res1->result_array();

        $category_code = $row1[0]['category_code'];
        if($depth=='1') {
            if($category_code == '')    $category_code = '10';
            $p_category_code = $category_code;
        } else {
            if($category_code == '')    $category_code = '01';
        }

        $category_code = sprintf('%02d',$category_code);


        $insert_data['category_code']  = $category_code;
        $insert_data['category_name']  = $category_name;
        $insert_data['depth']          = $depth;
        $insert_data['p_category_code']= $p_category_code;
        $insert_data['reg_id']         = $user_id;
        $insert_data['reg_date']       = $now_date;
        $insert_data['update_id']      = $user_id;
        $insert_data['update_date']    = $now_date;

        $row = $this->db->insert('bbs_faq_category', $insert_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	faq 카테고리 - update
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function bbs_faq_category_update($req_data){
        # request
	    $idx            = $req_data['idx'];
        unset($req_data['idx']);
        
        $user_id        = $this->session->userdata('Adm_Id');
        $now_date       = date("Y-m-d H:i:s");

        $req_data['update_id']   = $user_id;
        $req_data['update_date'] = $now_date;

        $this->db->where('idx', $idx);
        $row = $this->db->update('bbs_faq_category', $req_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	faq 카테고리 is_del update
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function bbs_faq_category_is_del_update($req_data){
	    # request
	    $idx            = $req_data['idx'];
        $p_category_code= $req_data['p_category_code'];

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


        $update_data['is_del']      = 'Y';
        $update_data['update_id']   = $user_id;
        $update_data['update_date'] = $now_date;

        if($idx!='') {
            $this->db->where('idx', $idx);
        }
        if($p_category_code!='') {
            $this->db->where('p_category_code', $p_category_code);
        }
        $row = $this->db->update('bbs_faq_category', $update_data);


    }


    #---------------------------------------------------------------------------
    # PURPOSE :	update
    # CREATE  :	2018-02-27
    #---------------------------------------------------------------------------
	function bbs_faq_update($req_data){
        # request
	    $idx            = $req_data['idx'];
        unset($req_data['idx']);

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

        $req_data['update_id']   = $user_id;
        $req_data['update_date'] = $now_date;

        $this->db->where('idx', $idx);
        $row = $this->db->update('bbs_faq', $req_data);

        return $row;

    }







}
?>
