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

class m_life_channel extends CI_Model{

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

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

	#---------------------------------------------------------------------------
    # PURPOSE :	채널에디터 - list
    # CREATE  :	2018-03-13
    #---------------------------------------------------------------------------
	function channel_editor_list($req_data){
        # request
        $idx                 = $req_data;

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

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	채널 select
    # CREATE  :	2018-03-13
    #---------------------------------------------------------------------------
	function channel_select($req_data){
        # request
        $idx                 = $req_data;

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

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	채널 - list
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function channel_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_category        = $req_data['search_category'];


        $sort_column = 'reg_date';
        if(isset($req_data['sort_column'])==true)   $sort_column = $req_data['sort_column'];
        if($search_type!='' && $search_keyword!=''){
            $this->db->like('channel.'.$search_type, $search_keyword);
        }
        if($search_category!='')  $this->db->where('channel.category_idx', $search_category);


        $this->db->select('channel.*, category.category_name, (select count(*) from partner where channel_idx=channel.idx and is_del="N") as editor_count, (select count(*) from life where channel_idx=channel.idx and is_del="N") as article_count, (select sum(hit_count) from life where channel_idx=channel.idx and is_del="N") as hit_count');
        $this->db->where('channel.is_del', 'N');
        $this->db->from('channel');
        $this->db->join('category', 'category.idx = channel.category_idx');
        $query = $this->db->get_compiled_select();

        $this->db->from('('.$query.') as b');
        $this->db->order_by('b.'.$sort_column,  'DESC');
        $this->db->limit($limit, $offset);
        $res = $this->db->get();
        $row = $res->result_array();
        
        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	채널 - list용 - total count
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function channel_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('channel');
        $row = $this->db->count_all_results();

        return $row;
    }




    #---------------------------------------------------------------------------
    # PURPOSE :	채널아이콘 - list
    # CREATE  :	2018-03-13
    #---------------------------------------------------------------------------
	function channel_icon_list(){
        $this->db->from('channel_icon');
        $this->db->order_by('idx', 'desc');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	채널아이콘 - insert
    # CREATE  :	2018-03-13
    #---------------------------------------------------------------------------
	function channel_icon_insert($req_data){
	    # request
        $channel_icon  = $req_data;

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

        $insert_data = array('channel_icon' => $channel_icon,
                             'reg_id' => $user_id,
                             'reg_date' => $now_date,
        );
        $row = $this->db->insert('channel_icon', $insert_data);

        return $row;

    }




    #---------------------------------------------------------------------------
    # PURPOSE :	채널 - insert
    # CREATE  :	2018-03-13
    #---------------------------------------------------------------------------
	function channel_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('channel', $req_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	channel is_del update
    # CREATE  : 2018-03-13
    #---------------------------------------------------------------------------
	function channel_is_del_update($req_data){
	    # request
	    $channel_idx            = $req_data;

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


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

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


        return $row;


    }


    #---------------------------------------------------------------------------
    # PURPOSE :	채널 - update
    # CREATE  :	2018-03-13
    #---------------------------------------------------------------------------
	function channel_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('channel', $req_data);



        return $row;

    }
    
    #---------------------------------------------------------------------------
    # PURPOSE :	채널 - update
    # CREATE  :	2018-03-13
    #---------------------------------------------------------------------------
	function channel_partner_update($req_data, $req_data2){
	    # request
        
        if($req_data2['partner_idx'])
            $idxArr = explode(",", $req_data2['partner_idx']);
        else 
            $idxArr = array();
            

        $user_id        = $this->session->userdata('Adm_Id');
        $now_date       = date("Y-m-d H:i:s");
        
        $update_data['update_id']  = $user_id;
        $update_data['update_date']= $now_date;
        $update_data['member_group_idx'] = 6;
        $update_data['category_idx'] = $req_data['category_idx'];
        $update_data['channel_idx'] = $req_data['idx'];
        
        
        $this->db->select('idx');
        $this->db->where('channel_idx', $req_data['idx']);
        $this->db->where('is_del', 'N');
        if(sizeof($idxArr) > 0) {
            $this->db->where_not_in('idx', $idxArr);
        }
        $this->db->from('partner');
        $res = $this->db->get();
        $row = $res->result_array();
        
        $idxExceptArr = array();
        
        for($i = 0; $i < sizeof($row); $i++) {
            $idxExceptArr[$i] = $row[$i]['idx'];
        }
        
        $except_data['channel_idx'] = 0;
        $except_data['category_idx'] = 0;
        $except_data['member_group_idx'] = 7;
        
        if(sizeof($idxExceptArr) > 0) {
            $this->db->where_in('idx', $idxExceptArr);
            $row = $this->db->update('partner', $except_data);
        }
        
        unset($this->db);     
        
        if(sizeof($idxArr) > 0) {                        
            $this->db->where_in('idx', $idxArr);
            $row = $this->db->update('partner', $update_data);
        }
        
        return $row;

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	채널 - all list
    # CREATE  :	2018-03-14
    #---------------------------------------------------------------------------
	function channel_all_list(){
        $this->db->where('is_del', 'N');
        $this->db->from('channel');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }









}
?>
