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

class m_life_etc_section extends CI_Model{

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

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


    #---------------------------------------------------------------------------
    # PURPOSE :	join detail join category join channel
    # CREATE  :	2018-04-30
    #---------------------------------------------------------------------------
	function life_etc_section_join_detail_join_category_join_channel_list($etc_section_type){

        $this->db->select('a.*, b.main_img, b.head_title, b.title, b.content_idx, b.rank, d.channel_name, e.editor_name');
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->where('d.is_del', 'N');
        $this->db->where('e.is_del', 'N');
        $this->db->where('e.is_display', 'Y');
        $this->db->where('etc_section_type', $etc_section_type);
        $this->db->from('life_etc_section as a');
        $this->db->join('life_etc_section_detail as b', 'a.idx = b.life_etc_section_idx');
        $this->db->join('channel as d', 'd.idx = a.channel_idx');
        $this->db->join('life as e', 'e.idx = a.content_idx');
        $this->db->order_by('a.rank',  'asc');
        $this->db->order_by('b.rank',  'asc');
        $res = $this->db->get();
        $row = $res->result_array();


        return $row;

    }





    #---------------------------------------------------------------------------
    # PURPOSE :	join channel
    # CREATE  :	2018-04-30
    #---------------------------------------------------------------------------
	function life_etc_section_join_channel_list($etc_section_type){

        $this->db->select('a.*, d.channel_name, d.channel_icon, (select count(*) from life where channel_idx=a.channel_idx and is_del = "N" and is_display = "Y") as channel_article_count,  (select count(*) from delivery where content_type = "3" and main_idx = a.channel_idx and is_del = "N") as delivery_count');
        $this->db->where('a.is_del', 'N');
        $this->db->where('d.is_del', 'N');
        if(is_array($etc_section_type))
        	$this->db->where_in('etc_section_type', $etc_section_type);
        else
			$this->db->where('etc_section_type', $etc_section_type);
        $this->db->from('life_etc_section as a');
        $this->db->join('channel as d', 'd.idx = a.channel_idx');
        $this->db->order_by('a.rank',  'asc');
        $res = $this->db->get();
        $row = $res->result_array();


        return $row;

    }

   #---------------------------------------------------------------------------
    # PURPOSE :	detail join life
    # CREATE  :	2018-04-30
    #---------------------------------------------------------------------------
	function life_etc_section_detail_join_life_list($etc_section_type){

        $this->db->select('a.*, b.partner_idx, b.editor_name');
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->where('b.is_display', 'Y');
        $this->db->where('a.etc_section_type', $etc_section_type);
        $this->db->from('life_etc_section_detail as a');
        $this->db->join('life as b', 'b.idx = a.content_idx');
        $this->db->order_by('a.rank',  'asc');
        $res = $this->db->get();
        $row = $res->result_array();


        return $row;

    }











	#---------------------------------------------------------------------------
	# PURPOSE :	select
	# CREATE  :	2018-03-27
	#---------------------------------------------------------------------------
	function life_etc_section_select($req_data){
		# request
		$idx            = $req_data;

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

		return $row;
	}




	#---------------------------------------------------------------------------
	# PURPOSE :	list
	# CREATE  :	2018-03-27
	#---------------------------------------------------------------------------
	function life_etc_section_detail_list($req_data){
		# request
		$life_etc_section_idx            = $req_data;

		$this->db->select('a.*, b.hit_count, b.editor_name, b.partner_idx, b.video_url, b.video_playtime');
		$this->db->where('a.life_etc_section_idx', $life_etc_section_idx);
		$this->db->where('a.is_del', 'N');
		$this->db->where('b.is_del', 'N');
		$this->db->from('life_etc_section_detail as a');
		$this->db->join('life as b', 'a.content_idx=b.idx');
		$this->db->order_by('rank', 'asc');
		$res = $this->db->get();
		$row = $res->result_array();

//		echo $this->db->last_query();
		return $row;
	}



	#---------------------------------------------------------------------------
	# PURPOSE :	type 별 list
	# CREATE  :	2018-03-27
	#---------------------------------------------------------------------------
	function life_etc_section_type_list($req_data){
		# request
		$etc_section_type         = $req_data['etc_section_type'];

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

		return $row;
	}


	#---------------------------------------------------------------------------
	# PURPOSE :	type 별 list
	# CREATE  :	2018-03-27
	#---------------------------------------------------------------------------
	function life_etc_section_detail_type_list($req_data){

		# request
		$etc_section_type         = $req_data['etc_section_type'];

		$this->db->select('a.*, b.hit_count, b.editor_name');
		$this->db->where('a.etc_section_type', $etc_section_type);
		$this->db->where('a.is_del', 'N');
		$this->db->where('b.is_del', 'N');
		$this->db->from('life_etc_section_detail as a');
		$this->db->from('life as b', 'a.content_idx=b.idx');
		$res = $this->db->get();
//		echo $this->db->last_query();
		$row = $res->result_array();

		return $row;
	}

#---------------------------------------------------------------------------
	# PURPOSE :	기타섹션 - list
	# CREATE  :	2018-03-27
	#---------------------------------------------------------------------------
	function life_editor_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_category        = $req_data['search_category'];
		$sort_column            = $req_data['sort_column'];


		$where_query = "";
		if($search_type!='' && $search_keyword!=''){
			$where_query.=" and AA.".$search_type." like '%".$search_keyword."%'";
		}
		if($search_category!='')        $where_query.=" and AA.category_idx = '".$search_category."'";

		$sql = "select * 
                  from (select a.*, d.partner_intro,b.category_name, c.channel_name, d.partner_img, d.partner_name, (select count(*) from life where partner_idx=a.partner_idx and is_del='N') as article_count, (select sum(hit_count) from life where partner_idx=a.partner_idx and is_del='N') as hit_count 
                          from life_etc_section as a 
                          join category as b on (a.category_idx = b.idx)
                          join channel as c on (a.channel_idx = c.idx)
                          join partner as d on (a.partner_idx = d.idx)
                         where a.is_del='N'
                           and b.is_del='N'
                           and c.is_del='N'
                           and d.is_del='N'
                           and a.etc_section_type = '".$search_etc_section_type."') as AA
                   where 1".$where_query." order by AA.".$sort_column." desc limit $offset, $limit ";


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


		return $row;
	}
	function life_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_category        = $req_data['search_category'];
		$sort_column            = $req_data['sort_column'];


		$where_query = "";
		if($search_type!='' && $search_keyword!=''){
			$where_query.=" and AA.".$search_type." like '%".$search_keyword."%'";
		}
		if($search_category!='')        $where_query.=" and AA.category_idx = '".$search_category."'";

		$sql = "select * 
                  from (select a.*, b.category_name, c.channel_name, c.channel_icon, (select count(*) from partner where channel_idx=a.channel_idx) as editor_count, (select count(*) from life where channel_idx=a.channel_idx and is_del='N') as article_count, (select sum(hit_count) from life where channel_idx=a.channel_idx and is_del='N') as hit_count 
                          from life_etc_section as a 
                          join category as b on (a.category_idx = b.idx)
                          join channel as c on (a.channel_idx = c.idx)
                         where a.is_del='N'
                           and b.is_del='N'
                           and c.is_del='N'
                           and a.etc_section_type = '".$search_etc_section_type."') as AA
                   where 1".$where_query." order by AA.".$sort_column." desc limit $offset, $limit ";



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


		return $row;
	}


}
?>


