<?
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 :	기타섹션 - list
    # CREATE  :	2018-03-27
    #---------------------------------------------------------------------------
	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;
    }

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

        if($search_type!='' && $search_keyword!=''){
            $this->db->like('c.'.$search_type, $search_keyword);
        }
        if($search_category!='')        $this->db->where('a.category_idx', $search_category);

        $this->db->select('count(a.idx)');
        $this->db->where('a.etc_section_type', $search_etc_section_type);
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->where('c.is_del', 'N');
        $this->db->from('life_etc_section as a');
        $this->db->join('category as b', 'a.category_idx = b.idx');
        $this->db->from('channel as c', 'a.channel_idx=c.idx');
        $row = $this->db->count_all_results();

        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();
        $row = $res->result_array();

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	life_etc_section insert
    # CREATE  :	2018-03-26
    #---------------------------------------------------------------------------
	function life_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;
        $this->db->insert('life_etc_section', $req_data);
        $row = $this->db->insert_id();

        return $row;


    }

    #---------------------------------------------------------------------------
    # PURPOSE :	life_etc_section_detail insert
    # CREATE  :	2018-03-26
    #---------------------------------------------------------------------------
	function life_etc_section_detail_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;
        $this->db->insert('life_etc_section_detail', $req_data);
        $row = $this->db->insert_id();

        return $row;

    }


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

        $arr_update_data1 = array();
        $arr_update_data2 = array();
        foreach($req_data as $key=>$val) {
            $arr_update_data1[$key]['idx']           = $val;
            $arr_update_data1[$key]['is_del']        = 'Y';
            $arr_update_data1[$key]['update_id']     = $user_id;
            $arr_update_data1[$key]['update_date']   = $now_date;

            $arr_update_data2[$key]['life_etc_section_idx'] = $val;
            $arr_update_data2[$key]['is_del']        = 'Y';
            $arr_update_data2[$key]['update_id']     = $user_id;
            $arr_update_data2[$key]['update_date']   = $now_date;

        }
        $this->db->update_batch('life_etc_section', $arr_update_data1, 'idx');
        $this->db->update_batch('life_etc_section_detail', $arr_update_data2, 'life_etc_section_idx');

    }




    #---------------------------------------------------------------------------
    # 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 :	select
    # CREATE  :	2018-03-28
    #---------------------------------------------------------------------------
	function life_etc_section_where_rank_select($req_data){
	    # request
	    $rank               = $req_data['rank'];
	    $etc_section_type   = $req_data['etc_section_type'];

        $this->db->where('rank', $rank);
        $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 :	select
    # CREATE  :	2018-03-28
    #---------------------------------------------------------------------------
	function life_etc_section_detail_where_rank_select($req_data){
	    # request
	    $rank       = $req_data['rank'];
	    $life_etc_section_idx   = $req_data['life_etc_section_idx'];

        $this->db->where('rank', $rank);
        $this->db->where('life_etc_section_idx', $life_etc_section_idx);
        $this->db->where('is_del', 'N');
        $this->db->from('life_etc_section_detail');
        $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');
        $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');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }




    #---------------------------------------------------------------------------
    # PURPOSE :	is_del update
    # CREATE  :	2018-03-27
    #---------------------------------------------------------------------------
	function life_etc_section_detail_is_del_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_del' => 'Y',
                             'update_id'        => $user_id,
                             'update_date'      => $now_date
        );
        $this->db->where('idx', $idx);
        $row = $this->db->update('life_etc_section_detail', $update_data);

        return $row;
    }




    #---------------------------------------------------------------------------
    # PURPOSE :	update
    # CREATE  :	2018-03-27
    #---------------------------------------------------------------------------
	function life_etc_section_detail_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('life_etc_section_detail', $req_data);


        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	update
    # CREATE  :	2018-03-27
    #---------------------------------------------------------------------------
	function life_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('life_etc_section', $req_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	update
    # CREATE  :	2018-03-27
    #---------------------------------------------------------------------------
	function life_etc_section_is_del_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();
        $update_data['update_id']   = $user_id;
        $update_data['update_date'] = $now_date;
        $update_data['is_del']      = 'Y';

        $this->db->where('idx', $idx);
        $row = $this->db->update('life_etc_section', $update_data);

        return $row;

    }


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

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

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

        $this->db->where('life_etc_section_idx', $life_etc_section_idx);
        $row = $this->db->update('life_etc_section_detail', $update_data);

        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.*, 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;
    }

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

        if($search_type!='' && $search_keyword!=''){
            $this->db->like('d.'.$search_type, $search_keyword);
        }
        if($search_category!='')        $this->db->where('a.category_idx', $search_category);

        $this->db->select('count(a.idx)');
        $this->db->where('a.etc_section_type', $search_etc_section_type);
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->where('c.is_del', 'N');
        $this->db->where('c.is_del', 'N');
        $this->db->from('life_etc_section as a');
        $this->db->join('category as b', 'a.category_idx = b.idx');
        $this->db->join('channel as c', 'a.channel_idx=c.idx');
        $this->db->join('partner as d', 'a.partner_idx=d.idx');
        $row = $this->db->count_all_results();
        return $row;
    }




}
?>
