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

class m_delivery extends CI_Model{

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

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


	#---------------------------------------------------------------------------
    # PURPOSE :	select
    # CREATE  :	2018-04-28
    #---------------------------------------------------------------------------
	function delivery_where_member_id_main_idx_select($req_data){
        # request
        $content_type       = $req_data['content_type'];
        $member_id          = $req_data['member_id'];
        $main_idx           = $req_data['main_idx'];


        $this->db->where('content_type', $content_type);
        $this->db->where('member_id', $member_id);
        $this->db->where('main_idx', $main_idx);
        $this->db->where('is_del', 'N');
        $this->db->from('delivery');
        $res = $this->db->get();
        $row = $res->row_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	update
    # CREATE  :	2018-04-28
    #---------------------------------------------------------------------------
	function delivery_update($req_data){
        # request
	    $idx            = $req_data['idx'];
        unset($req_data['idx']);

        $this->db->where('idx', $idx);
        $row = $this->db->update('delivery', $req_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	insert
    # CREATE  :	2018-04-28
    #---------------------------------------------------------------------------
	function delivery_insert($req_data){

        $row = $this->db->insert('delivery', $req_data);
        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	count
    # CREATE  :	2018-04-28
    #---------------------------------------------------------------------------
	function delivery_where_main_idx_count($req_data){
        # request
        $content_type       = $req_data['content_type'];
        $main_idx           = $req_data['main_idx'];


        $this->db->where('content_type', $content_type);
        $this->db->where('main_idx', $main_idx);
        $this->db->where('is_del', 'N');
        $this->db->from('delivery');
        $row = $this->db->count_all_results();

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	list
    # CREATE  :	2018-04-28
    #---------------------------------------------------------------------------
	function delivery_join_channel_list($req_data){
        # request
        $offset                 = $req_data['offset'];
        $limit                  = $req_data['limit'];
        $content_type           = $req_data['content_type'];
        $member_id              = $req_data['member_id'];

        $this->db->select('a.*, b.channel_icon, b.channel_name, (select count(*) from life where is_del="N" and is_display="Y" and channel_idx=a.main_idx) as article_count, (select count(*) from delivery where is_del="N" and content_type=a.content_type and main_idx=a.main_idx) as delivery_count');
        $this->db->where('a.content_type', $content_type);
        $this->db->where('a.member_id', $member_id);
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->from('delivery as a');
        $this->db->join('channel as b', 'a.main_idx=b.idx');
        $this->db->limit($limit, $offset);
        $this->db->order_by('a.reg_date',  'desc');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	list
    # CREATE  :	2018-04-28
    #---------------------------------------------------------------------------
	function delivery_join_channel_list_total_count($req_data){
        # request
        $content_type           = $req_data['content_type'];
        $member_id              = $req_data['member_id'];

        $this->db->where('a.content_type', $content_type);
        $this->db->where('a.member_id', $member_id);
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->from('delivery as a');
        $this->db->join('channel as b', 'a.main_idx=b.idx');
        $row = $this->db->count_all_results();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	list
    # CREATE  :	2018-04-28
    #---------------------------------------------------------------------------
	function delivery_member_id_idx_count($req_data){
        # request
        $idx            = $req_data['idx'];
        $member_id      = $req_data['member_id'];

        $this->db->where('idx', $idx);
        $this->db->where('member_id', $member_id);
        $this->db->where('is_del', 'N');;
        $this->db->from('delivery');
        $row = $this->db->count_all_results();

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	is_del update
    # CREATE  :	2018-04-28
    #---------------------------------------------------------------------------
	function delivery_is_del_update($req_data){
        # request
        $idx            = $req_data['idx'];
        unset($req_data['idx']);

        $this->db->where('idx', $idx);
        $row = $this->db->update('delivery', $req_data);

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	list
    # CREATE  :	2018-05-08
    #---------------------------------------------------------------------------
	function delivery_join_partner_list($req_data){
        # request
        $offset                 = $req_data['offset'];
        $limit                  = $req_data['limit'];
        $content_type           = $req_data['content_type'];
        $member_id              = $req_data['member_id'];

        $this->db->select('a.*, b.partner_img, b.partner_name, (select count(*) from `column` where is_del="N" and is_display="Y" and partner_idx=a.main_idx) as article_count, (select count(*) from delivery where is_del="N" and content_type=a.content_type and main_idx=a.main_idx) as delivery_count');
        $this->db->where('a.content_type', $content_type);
        $this->db->where('a.member_id', $member_id);
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->from('delivery as a');
        $this->db->join('partner as b', 'a.main_idx=b.idx');
        $this->db->limit($limit, $offset);
        $this->db->order_by('a.reg_date',  'desc');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	list
    # CREATE  :	2018-05-08
    #---------------------------------------------------------------------------
	function delivery_join_partner_list_total_count($req_data){
        # request
        $content_type           = $req_data['content_type'];
        $member_id              = $req_data['member_id'];

        $this->db->where('a.content_type', $content_type);
        $this->db->where('a.member_id', $member_id);
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->from('delivery as a');
        $this->db->join('partner as b', 'a.main_idx=b.idx');
        $row = $this->db->count_all_results();

        return $row;
    }




    #---------------------------------------------------------------------------
    # PURPOSE :	list
    # CREATE  :	2018-05-11
    #---------------------------------------------------------------------------
	function delivery_join_tag_list($req_data){
        # request
        $offset                 = $req_data['offset'];
        $limit                  = $req_data['limit'];
        $content_type           = $req_data['content_type'];
        $member_id              = $req_data['member_id'];

        $this->db->select('max(a.idx) as idx, a.main_idx, max(a.content_type) as content_type, max(a.reg_date) as reg_date');
        $this->db->where('a.content_type', $content_type);
        $this->db->where('a.member_id', $member_id);
        $this->db->where('a.is_del', 'N');
        $this->db->where('c.is_del', 'N');
        $this->db->where('c.is_display', 'Y');
        $this->db->where('b.content_type', '3');
        $this->db->from('delivery as a');
        $this->db->join('tag as b', 'a.main_idx=b.tag');
        $this->db->join('life as c', 'b.content_idx=c.idx');
        $this->db->group_by('b.tag');
        $query = $this->db->get_compiled_select();



        $this->db->select('d.*, (select count(*) from delivery where is_del="N" and content_type=d.content_type and main_idx=d.main_idx) as delivery_count');
        $this->db->from('('.$query.') as d');
        $this->db->limit($limit, $offset);
        $this->db->order_by('d.reg_date',  'desc');
        $res = $this->db->get();
        $row = $res->result_array();


        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	list
    # CREATE  :	2018-05-11
    #---------------------------------------------------------------------------
	function delivery_join_tag_list_total_count($req_data){
        # request
        $content_type           = $req_data['content_type'];
        $member_id              = $req_data['member_id'];

        $this->db->select('a.main_idx');
        $this->db->where('a.content_type', $content_type);
        $this->db->where('a.member_id', $member_id);
        $this->db->where('a.is_del', 'N');
        $this->db->where('c.is_del', 'N');
        $this->db->where('c.is_display', 'Y');
        $this->db->where('b.content_type', '3');
        $this->db->from('delivery as a');
        $this->db->join('tag as b', 'a.main_idx=b.tag');
        $this->db->join('life as c', 'b.content_idx=c.idx');
        $this->db->group_by('b.tag');
        $row = $this->db->count_all_results();

        return $row;
    }

    


}
?>
