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

class m_etc_section extends CI_Model{

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

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

	#---------------------------------------------------------------------------
    # PURPOSE :	기타섹션 - list
    # CREATE  :	2018-03-05
    #---------------------------------------------------------------------------
	function etc_section_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_etc_section_type= $req_data['search_etc_section_type'];
        $search_content_type    = $req_data['search_content_type'];
        $sort_column            = $req_data['sort_column'];


        $where_query = "";
        if($search_type!='' && $search_keyword!=''){
            $where_query.=" and ".$search_type." like '%".$search_keyword."%'";
        }
        if($search_etc_section_type!='')    $where_query.=" and etc_section_type = '".$search_etc_section_type."'";
        if($search_content_type!='')        $where_query.=" and content_type = '".$search_content_type."'";


        $sql = "select * from  ((select a.*, b.display_date, b.write_id, b.write_name, b.hit_count, b.like_count, b.comment_count, b.share_count, b.popular_count
                                   from etc_section as a
                                   join news as b on (a.content_idx = b.idx)
                                  where a.is_del='N' and a.content_type='1'
                                    and b.is_del = 'N') union all
                                
                                (select c.*, d.display_date, d.write_id, d.write_name, d.hit_count, d.like_count, d.comment_count, d.share_count, d.popular_count
                                   from etc_section as c
                                   join `column` as d on (c.content_idx = d.idx)
                                  where c.is_del='N' and c.content_type='2'
                                    and d.is_del = 'N')
                                union all
                                
                                (select e.*, f.display_date, f.write_id, f.write_name, f.hit_count, f.like_count, f.comment_count, f.share_count, f.popular_count
                                   from etc_section as e
                                   join life as f on (e.content_idx = f.idx)
                                  where e.is_del='N' and e.content_type='3'
                                    and f.is_del = 'N')) a where 1".$where_query." order by ".$sort_column." desc limit $offset, $limit ";


        $res = $this->db->query($sql);
        $row = $res->result_array();


        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	기타섹션 - list
    # CREATE  :	2018-03-05
    #---------------------------------------------------------------------------
	function etc_section_join_vw_content_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_etc_section_type= $req_data['search_etc_section_type'];
        $search_content_type    = $req_data['search_content_type'];
        $sort_column            = $req_data['sort_column'];
        $search_is_display      = $req_data['search_is_display'];


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

        if($search_etc_section_type!='')    $this->db->where('a.etc_section_type', $search_etc_section_type);
        if($search_content_type!='')        $this->db->where('a.content_type', $search_content_type);
        if($search_is_display!='')          $this->db->where('b.is_display', $search_is_display);

        $this->db->select('a.*, b.display_date, b.search_name, b.hit_count, b.like_count, b.comment_count, b.is_display');
        $this->db->where('a.is_del', 'N');
        $this->db->from('etc_section as a');
        $this->db->join('vw_content as b', 'a.content_idx=b.idx and a.content_type=b.content_type');
        $this->db->limit($limit, $offset);
        $this->db->order_by($sort_column,  'desc');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }




    #---------------------------------------------------------------------------
    # PURPOSE :	기타섹션 - list
    # CREATE  :	2018-03-05
    #---------------------------------------------------------------------------
	function etc_section_display_group_by_list($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_etc_section_type= $req_data['search_etc_section_type'];
        $search_content_type    = $req_data['search_content_type'];

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

        if($search_etc_section_type!='')    $this->db->where('a.etc_section_type', $search_etc_section_type);
        if($search_content_type!='')        $this->db->where('a.content_type', $search_content_type);


        $this->db->select('b.is_display, count(*) as cnt');
        $this->db->where('a.is_del', 'N');
        $this->db->from('etc_section as a');
        $this->db->join('vw_content as b', 'a.content_idx=b.idx and a.content_type=b.content_type');
        $this->db->group_by('b.is_display');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }




    #---------------------------------------------------------------------------
    # PURPOSE :	기타섹션 - list - total count
    # CREATE  :	2018-03-05
    #---------------------------------------------------------------------------
	function etc_section_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_etc_section_type= $req_data['search_etc_section_type'];
        $search_content_type    = $req_data['search_content_type'];
        $search_is_display      = $req_data['search_is_display'];

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

        if($search_etc_section_type!='')    $this->db->where('a.etc_section_type', $search_etc_section_type);
        if($search_content_type!='')        $this->db->where('a.content_type', $search_content_type);
        if($search_is_display!='')          $this->db->where('b.is_display', $search_is_display);


        $this->db->where('a.is_del', 'N');
        $this->db->from('etc_section as a');
        $this->db->join('vw_content as b', 'a.content_idx=b.idx and a.content_type=b.content_type');
        $res = $this->db->get();
        $row = $this->db->count_all_results();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	etc_section 카드뉴스 송출중 내용가져오기
    # CREATE  :	2018-03-05
    #---------------------------------------------------------------------------
	function etc_section_card_news_display_select(){
        $now_date       = date("Y-m-d H:i:s");

        $this->db->where('etc_section_type', '4');
	    $this->db->where('display_date >=', $now_date);
        $this->db->where('is_del', 'N');
        $this->db->order_by('display_date',  'asc');
        $this->db->from('etc_section');
        $this->db->limit(1);

        $res = $this->db->get();
        $row = $res->result_array();

        return $row;





    }

    #---------------------------------------------------------------------------
    # PURPOSE :	기타섹션 - rank count
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function etc_section_rank_count($req_data){
	    # request
	    $etc_section_type   = $req_data['etc_section_type'];
        $rank               = $req_data['rank'];

        $this->db->where('etc_section_type', $etc_section_type);
        $this->db->where('rank', $rank);
        $this->db->where('is_del', 'N');
        $this->db->where('is_display', 'Y');
        $this->db->from('etc_section');
        $row = $this->db->count_all_results();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	기타섹션 is_del update
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function etc_section_rank_is_del_update($req_data){
	    # request
	    $etc_section_type   = $req_data['etc_section_type'];
        $rank               = $req_data['rank'];

        $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('etc_section_type', $etc_section_type);
        $this->db->where('rank', $rank);
        $this->db->where('is_del', 'N');
        $this->db->where('is_display', 'Y');
        $row = $this->db->update('etc_section', $update_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	기타섹션 insert
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function etc_section_insert($req_data){
        $user_id    = $this->session->userdata('Adm_Id');
        $now_date   = date("Y-m-d H:i:s");

        $req_data['reg_id']     = $user_id;
        $req_data['reg_date']   = $now_date;
        $req_data['update_id']  = $user_id;
        $req_data['update_date']= $now_date;
        $row = $this->db->insert('etc_section', $req_data);

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	기타섹션 content_idx - select
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function etc_section_content_idx_select($req_data){
	    # request
        $content_type       = $req_data['content_type'];
        $content_idx        = $req_data['content_idx'];

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

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	기타섹션 content_idx - list
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function etc_section_content_idx_list($req_data){
	    # request
        $content_type       = $req_data['content_type'];
        $content_idx        = $req_data['content_idx'];

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

        if($search_type!='' && $search_key!='') $this->db->where($search_type, $search_key);

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

        return $row;
    }




    #---------------------------------------------------------------------------
    # PURPOSE :	기타섹션 is_display update where content_idx
    # CREATE  :	2018-03-21
    #---------------------------------------------------------------------------
	function etc_section_is_display_where_content_idx_update($req_data){
	    # request
        $content_idx    = $req_data['content_idx'];
        $content_type   = $req_data['content_type'];

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

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

        $this->db->where('content_type', $content_type);
        $this->db->where('content_idx', $content_idx);
        $this->db->where('is_display', 'N');
        $this->db->where('is_del', 'N');
        
        $row = $this->db->update('etc_section', $update_data);

        return $row;

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	기타섹션 content_idx
    # CREATE  :	2018-03-22
    #---------------------------------------------------------------------------
	function etc_section_rank_content_idx_select($req_data){
	    # request
        $etc_section_type   = $req_data['etc_section_type'];
        $content_type       = $req_data['content_type'];
        $content_idx        = $req_data['content_idx'];
        $rank               = $req_data['rank'];

        $this->db->select('idx');
        $this->db->where('etc_section_type', $etc_section_type);
        $this->db->where('rank', $rank);
        $this->db->where('content_type', $content_type);
        $this->db->where('content_idx', $content_idx);
        $this->db->where('is_del', 'N');
        $this->db->from('etc_section');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE : 기타섹션 - update
    # CREATE  :	2018-03-22
    #---------------------------------------------------------------------------
	function etc_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('etc_section', $req_data);

        return $row;

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	is_del update
    # CREATE  :	2018-03-23
    #---------------------------------------------------------------------------
	function etc_section_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('etc_section', $arr_update_data, 'idx');

    }



	#---------------------------------------------------------------------------
    # PURPOSE :	type 별 list - 예약제외
    # CREATE  :	2018-03-23
    #---------------------------------------------------------------------------
	function etc_section_type_list($req_data){
        # request
        $etc_section_type         = $req_data['etc_section_type'];

        $sql = "select * from  ((select a.*, b.display_date, b.write_id, b.write_name, b.hit_count, b.like_count, b.comment_count, b.share_count, b.popular_count
                                   from etc_section as a
                                   join news as b on (a.content_idx = b.idx)
                                  where a.is_del='N' and a.content_type='1'
                                    and b.is_del = 'N'
                                    and a.is_display = 'Y'
                                    and a.etc_section_type = '".$etc_section_type."') 
                          union all
                                
                                (select c.*, d.display_date, d.write_id, d.write_name, d.hit_count, d.like_count, d.comment_count, d.share_count, d.popular_count
                                   from etc_section as c
                                   join `column` as d on (c.content_idx = d.idx)
                                  where c.is_del='N' and c.content_type='2'
                                    and d.is_del = 'N'
                                    and c.is_display = 'Y'
                                    and c.etc_section_type = '".$etc_section_type."')
                          union all
                                
                                (select e.*, f.display_date, f.write_id, f.write_name, f.hit_count, f.like_count, f.comment_count, f.share_count, f.popular_count
                                   from etc_section as e
                                   join life as f on (e.content_idx = f.idx)
                                  where e.is_del='N' and e.content_type='3'
                                    and f.is_del = 'N'
                                    and e.is_display = 'Y'
                                    and e.etc_section_type = '".$etc_section_type."')) a ";


        $res = $this->db->query($sql);
        $row = $res->result_array();


        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	기타섹션 + 컨텐츠
    # CREATE  :	2018-03-23
    #---------------------------------------------------------------------------
	function etc_section_join_content_select($req_data){
        # request
        $idx       = $req_data;

        $this->db->select('a.*');
        $this->db->where('a.is_del', 'N');
        $this->db->where('a.idx', $idx);
        $this->db->from('etc_section as a');
        $this->db->join('vw_content as b', 'a.content_type=b.content_type and a.content_idx=b.idx');
        $res = $this->db->get();
        $row = $res->result_array();


        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	예약글 따른 리스트  - 스케쥴에서 사용중
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function today_word_reservation_article_list(){

        $now_date                   = date("Y-m-d H:i:s");

        $this->db->select('idx');
        $this->db->where('display_date <=', $now_date);
        $this->db->where('is_display', 'N');
        $this->db->where('is_del', 'N');
        $this->db->from('today_word');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	is_display update batch
    # CREATE  : 2018-03-30
    #---------------------------------------------------------------------------
	function today_word_is_display_update_batch($req_data){

        $user_id    = 'system';
        $now_date   = date("Y-m-d");

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

        return $row;

    }

    #---------------------------------------------------------------------------
    # PURPOSE : is_display update batch - 스케쥴에서 사용중
    # CREATE  : 2018-03-30
    #---------------------------------------------------------------------------
	function etc_section_is_display_update_batch($req_data){

        $user_id    = 'system';
        $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['idx'];
            $arr_update_data[$key]['is_display']    = 'Y';
            $arr_update_data[$key]['update_id']     = $user_id;
            $arr_update_data[$key]['update_date']   = $now_date;
        }
        $row = $this->db->update_batch('etc_section', $arr_update_data, 'idx');

        return $row;

    }

}
?>
