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

class m_news_section extends CI_Model{

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

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

    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스섹션 - list
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function news_section_list($req_data){
        # request
        $offset                 = $req_data['offset'];
        $limit                  = $req_data['limit'];
        $cateogory_code1        = $req_data['cateogory_code'];
        $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];

        if($cateogory_code1!=''){
            $this->db->where('p_category_code', $cateogory_code1);
            $this->db->where('depth', '1');
        }

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

        $this->db->where('is_del', 'N');
        $this->db->from('news_section');
        $this->db->order_by('p_category_code asc, depth asc, display_sort asc, category_code asc');
        $this->db->limit($limit, $offset);
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스섹션 - list - total count
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function news_section_list_total_count($req_data){
	    # request
        $cateogory_code1        = $req_data['cateogory_code'];
        $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];

        if($cateogory_code1!=''){
            $this->db->where('p_category_code', $cateogory_code1);
            $this->db->where('depth', '1');
        }

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


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

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	총 글  count
    # CREATE  :	2018-04-13
    #---------------------------------------------------------------------------
	function news_section_article_count($req_data){
	    # request
        $cateogory_code1        = $req_data['cateogory_code'];
        $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];

        $this->db->select('sum(total_count) as article_count');
        if($cateogory_code1!=''){
            $this->db->where('p_category_code', $cateogory_code1);
            $this->db->where('depth', '1');
        }
        if($search_type!='' && $search_keyword!='') $this->db->like($search_type, $search_keyword);
        $this->db->where('is_del', 'N');
        $this->db->from('news_section');
        $res = $this->db->get();
        $row = $res->row_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	오늘의 글 count
    # CREATE  :	2018-04-13
    #---------------------------------------------------------------------------
	function news_section_today_article_count($req_data){
	    # request
        $cateogory_code1        = $req_data['cateogory_code'];
        $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];

        $this->db->select('sum(today_count) as today_article_count');
        if($cateogory_code1!=''){
            $this->db->where('p_category_code', $cateogory_code1);
            $this->db->where('depth', '1');
        }
        if($search_type!='' && $search_keyword!='') $this->db->like($search_type, $search_keyword);
        $this->db->where('is_del', 'N');
        $this->db->from('news_section');
        $res = $this->db->get();
        $row = $res->row_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스섹션 - all list
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function news_section_all_list(){

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


        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	group by depth
    # CREATE  :	2018-04-13
    #---------------------------------------------------------------------------
	function news_section_group_by_depth_count(){

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

        return $row;

    }








    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스섹션  - insert
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function news_section_insert($req_data){
	    # request
	    $category_name          = $req_data['category_name'];
        $depth                  = $req_data['depth'];
        $p_category_code        = $req_data['p_category_code'];
        $display_sort           = $req_data['display_sort'];

        $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('news_section');
        $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 = $p_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['display_sort']   = $display_sort;
        $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('news_section', $insert_data);

        return $row;

    }






    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스섹션 - update
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function news_section_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('news_section', $req_data);

        return $row;

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스섹션 is_del update for category_code
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function news_section_is_del_where_category_code_update($req_data){
	    # request
        $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;

        $this->db->where('category_code', $p_category_code);
        $row = $this->db->update('news_section', $update_data);

        return $row;

    }


     #---------------------------------------------------------------------------
    # PURPOSE :	뉴스섹션 is_del update for p_category_code
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function news_section_is_del_where_p_category_code_update($req_data){
	    # request
        $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;

        $this->db->like('p_category_code', $p_category_code, 'after');
        $row = $this->db->update('news_section', $update_data);

        return $row;

    }










}
?>
