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

class m_partner extends CI_Model{

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

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



	#---------------------------------------------------------------------------
    # PURPOSE :	partner where member_group_idx
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function partner_list_where_member_group_idx($req_data){
	    # request
        $member_group_idx   = $req_data['member_group_idx'];
        $search_write_id = '';
        if(isset($req_data['search_write_id']) == true) {
            $search_write_id    = $req_data['search_write_id'];
        }
        if($search_write_id!='')    $this->db->where('partner_id', $search_write_id);

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

        return $row;
    }
    
    #---------------------------------------------------------------------------
    # PURPOSE :	partner where member_group_idx -- life_channel_reg NEW
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function partner_list_no_channel_new(){
	    # request
        $search_write_id = '';
        if(isset($req_data['search_write_id']) == true) {
            $search_write_id    = $req_data['search_write_id'];
        }
        if($search_write_id!='')    $this->db->where('partner_id', $search_write_id);
        
        $this->db->where('category_idx', '0');
        $this->db->where('is_del', 'N');
        $this->db->from('partner');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }
    
    #---------------------------------------------------------------------------
    # PURPOSE :	partner where member_group_idx
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function partner_where_channel_idx_list_new($req_data){
	    # request
        $category_idx   = $req_data;

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

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	partner where member_group_idx
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function partner_my_channel_idx_list($req_data){
	    # request
        $channel_idx   = $req_data;

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

        return $row;
    }


	#---------------------------------------------------------------------------
    # PURPOSE :	partner - list
    # CREATE  :	2018-03-14
    #---------------------------------------------------------------------------
	function partner_list($req_data){
        # request
        $offset                 = $req_data['offset'];
        $limit                  = $req_data['limit'];
        $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];

        $sort_column = '';
        if(isset($req_data['sort_column'])==true)   $sort_column            = $req_data['sort_column'];

        $member_group_idx       = $req_data['member_group_idx'];

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

        if($member_group_idx == '6') {


            $search_category_code1  = $req_data['search_category_code1'];
            $search_category_code2  = $req_data['search_category_code2'];

            if($search_category_code1!='') $this->db->where('a.category_idx', $search_category_code1);
            if($search_category_code2!='') $this->db->where('a.channel_idx', $search_category_code2);

            $this->db->select('a.*, b.member_group_name, c.category_name, d.channel_name, (select count(*) from life where is_del="N" and partner_idx = a.idx) as article_count');
            $this->db->where('a.member_group_idx', $member_group_idx);
            $this->db->where('a.is_del', 'N');
            $this->db->from('partner as a');
            $this->db->join('member_group as b', 'a.member_group_idx = b.idx');
            $this->db->join('category as c', 'a.category_idx = c.idx');
            $this->db->join('channel as d', 'a.channel_idx = d.idx');
            $query = $this->db->get_compiled_select();

            $this->db->from('('.$query.') as f');
            if($sort_column!='')    $this->db->order_by('f.'.$sort_column,  'DESC');
            $this->db->order_by('f.display_sort desc, f.reg_date desc');
            $this->db->limit($limit, $offset);


        } else {
            $this->db->select('a.*, b.member_group_name, (select count(*) from `column` where is_del="N" and partner_idx = a.idx) as article_count');
            $this->db->where('a.member_group_idx', $member_group_idx);
            $this->db->where('a.is_del', 'N');
            $this->db->from('partner as a');
            $this->db->join('member_group as b', 'a.member_group_idx = b.idx');
            $query = $this->db->get_compiled_select();

            $this->db->from('('.$query.') as f');
            if($sort_column!='')    $this->db->order_by('f.'.$sort_column,  'DESC');
            $this->db->limit($limit, $offset);

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

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	partner - list용 - total count
    # CREATE  :	2018-03-14
    #---------------------------------------------------------------------------
	function partner_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $member_group_idx       = $req_data['member_group_idx'];

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

        if($member_group_idx == '6') {
            $search_category_code1  = $req_data['search_category_code1'];
            $search_category_code2  = $req_data['search_category_code2'];

            if($search_category_code1!='') $this->db->where('category_idx', $search_category_code1);
            if($search_category_code2!='') $this->db->where('channel_idx', $search_category_code2);

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

        return $row;
    }




    #---------------------------------------------------------------------------
    # PURPOSE :	partner - insert
    # CREATE  :	2018-03-14
    #---------------------------------------------------------------------------
	function partner_insert($req_data){

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

        $req_data['partner_pwd']= MD5($req_data['partner_pwd']);
        $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('partner', $req_data);


        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	partner - update
    # CREATE  :	2018-03-14
    #---------------------------------------------------------------------------
	function partner_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('partner', $req_data);

        return $row;
        
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	partner where partner_id count
    # CREATE  :	2018-03-14
    #---------------------------------------------------------------------------
	function partner_where_partner_id_count($req_data){
	    # request
        $partner_id          = $req_data;

        $this->db->where('partner_id', $partner_id);
        $this->db->from('partner');
        $row = $this->db->count_all_results();

        return $row;
    }





    #---------------------------------------------------------------------------
    # PURPOSE :	partner - select
    # CREATE  :	2018-03-14
    #---------------------------------------------------------------------------
	function partner_select($req_data){
	    # request
	    $idx            = $req_data;

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

        return $row;
    }




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

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	partner - select in idx
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function partner_select_in_idx($req_data){
	    # request
	    $arr_idx    = $req_data;

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

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	partner - select
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function partner_where_partner_id_select($req_data){
	    # request
	    $partner_id       = $req_data;

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

        return $row;
    }





}
?>
