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

class m_tag extends CI_Model{

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

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

    #---------------------------------------------------------------------------
    # PURPOSE :	total_count
    # CREATE  :	2018-05-01
    #---------------------------------------------------------------------------
	function tag_join_life_list_total_count($req_data){
        # request
        $tag            = $req_data['tag'];
        $content_type   = $req_data['content_type'];

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


        return $row;
    }




	#---------------------------------------------------------------------------
    # PURPOSE : join life list
    # CREATE  :	2018-05-01
    #---------------------------------------------------------------------------
	function tag_join_life_list($req_data){
         # request
        $tag            = $req_data['tag'];
        $content_type   = $req_data['content_type'];
        $offset         = $req_data['offset'];
        $limit          = $req_data['limit'];

        $this->db->where('b.is_del', 'N');
        $this->db->where('b.is_display', 'Y');
        $this->db->where('a.tag', $tag);
        $this->db->where('a.content_type', $content_type);
        $this->db->from('tag as a');
        $this->db->join('life as b', 'a.content_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 : join tag
    # CREATE  :	2018-05-11
    #---------------------------------------------------------------------------
	function tag_join_tag_list($req_data){
        # request
        $tag            = $req_data['tag'];
        $content_type   = $req_data['content_type'];


        $this->db->select('b.tag, count(*) as cnt');
        $this->db->where('a.content_type', $content_type);
        $this->db->where('a.tag', $tag);
        $this->db->where('d.is_del', 'N');
        $this->db->where('d.is_display', 'Y');
        $this->db->from('tag as a');
        $this->db->join('tag as b', 'a.content_type=b.content_type and a.content_idx=b.content_idx');
        $this->db->join('life as d', 'b.content_idx=d.idx');
        $this->db->group_by('b.tag');
        $query = $this->db->get_compiled_select();

        $this->db->from('('.$query.') as c');
        $this->db->where('c.tag!=', $tag);
        $this->db->order_by('c.cnt',  'DESC');
        $this->db->limit(5, 0);
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE : join life
    # CREATE  :	2018-05-11
    #---------------------------------------------------------------------------
	function tag_join_life_where_tag_count($req_data){
        # request
        $tag            = $req_data['tag'];

        $this->db->where('a.content_type', '3');
        $this->db->where('a.tag', $tag);
        $this->db->where('d.is_del', 'N');
        $this->db->where('d.is_display', 'Y');
        $this->db->from('tag as a');
        $this->db->join('life as d', 'a.content_idx=d.idx');
        $row = $this->db->count_all_results();

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE : join life
    # CREATE  :	2018-05-11
    #---------------------------------------------------------------------------
	function tag_join_life_where_tag_list_limit1($req_data){
        # request
        $tag            = $req_data['tag'];

        $this->db->where('a.content_type', '3');
        $this->db->where('a.tag', $tag);
        $this->db->where('d.is_del', 'N');
        $this->db->where('d.is_display', 'Y');
        $this->db->from('tag as a');
        $this->db->join('life as d', 'a.content_idx=d.idx');
        $this->db->limit(1, 0);
        $this->db->order_by('d.display_date',  'desc');
        $res = $this->db->get();
        $row = $res->row_array();

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE : join life
    # CREATE  :	2018-05-11
    #---------------------------------------------------------------------------
	function tag_join_life_where_display_date_count($req_data){
	    # request
        $reg_date   = $req_data['now_date'];
        $tag        = $req_data['tag'];

        $this->db->where('d.display_date >=', $reg_date);
        $this->db->where('a.content_type', '3');
        $this->db->where('a.tag', $tag);
        $this->db->where('d.is_del', 'N');
        $this->db->where('d.is_display', 'Y');
        $this->db->from('tag as a');
        $this->db->join('life as d', 'a.content_idx=d.idx');
        $row = $this->db->count_all_results();



        return $row;

    }






}
?>


