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

class m_ranking extends CI_Model{

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

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



    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 - rank count
    # CREATE  :	2018-03-06
    #---------------------------------------------------------------------------
	function ranking_rank_count($req_data){
	    # request
	    $ranking_type       = $req_data['ranking_type'];
        $content_type       = $req_data['content_type'];
        $rank               = $req_data['rank'];

        $this->db->where('ranking_type', $ranking_type);
        $this->db->where('content_type', $content_type);
        $this->db->where('rank', $rank);
        $this->db->where('is_del', 'N');
        $this->db->from('ranking');
        $row = $this->db->count_all_results();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 is_del update
    # CREATE  :	2018-03-06
    #---------------------------------------------------------------------------
	function ranking_rank_is_del_update($req_data){
	    # request
	    $ranking_type       = $req_data['ranking_type'];
        $content_type       = $req_data['content_type'];
        $rank               = $req_data['rank'];

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

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

        $this->db->where('ranking_type', $ranking_type);
        $this->db->where('content_type', $content_type);
        $this->db->where('rank', $rank);
        $this->db->where('is_del', 'N');
        $row = $this->db->update('ranking', $update_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 insert
    # CREATE  :	2018-03-06
    #---------------------------------------------------------------------------
	function ranking_insert($req_data){
        $user_id    = $this->session->userdata('Adm_Id');
        $now_date   = date("Y-m-d H:i:s");

        $req_data['ranking_date']   = $now_date;
        $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('ranking', $req_data);

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 content_idx - select
    # CREATE  :	2018-03-06
    #---------------------------------------------------------------------------
	function ranking_content_idx_select($req_data){
	    # request
        $content_type       = $req_data['content_type'];
        $content_idx        = $req_data['content_idx'];

        $this->db->where('content_type', $content_type);
        $this->db->where('content_idx', $content_idx);
        $this->db->where('is_del', 'N');
        $this->db->from('ranking');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 - update
    # CREATE  :	2018-03-06
    #---------------------------------------------------------------------------
	function ranking_update($req_data){
        # request
	    $ranking_type         = $req_data['ranking_type'];
        $content_type       = $req_data['content_type'];
        $rank               = $req_data['rank'];

        $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('ranking_type', $ranking_type);
        $this->db->where('content_type', $content_type);
        $this->db->where('rank', $rank);
        $this->db->where('is_del', 'N');
        $row = $this->db->update('ranking', $req_data);

        return $row;

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 - select
    # CREATE  :	2018-03-06
    #---------------------------------------------------------------------------
	function ranking_select($req_data){
	    # request
	    $idx            = $req_data;

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

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 is_del update
    # CREATE  : 2018-03-06
    #---------------------------------------------------------------------------
	function ranking_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('ranking', $arr_update_data, 'idx');

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 - select in idx
    # CREATE  :	2018-03-06
    #---------------------------------------------------------------------------
	function ranking_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('ranking');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 - update
    # CREATE  :	2018-03-22
    #---------------------------------------------------------------------------
	function ranking_where_idx_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('ranking', $req_data);

        return $row;


    }

    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 log로 copy - 스케쥴
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function rank_all_copy_ranking_log($content_type){

        $sql ="insert into ranking_log";
        if($content_type =='1'){
            $sql.=" select '', a.ranking_date, b.category_code, '', a.ranking_type, a.content_type, a.rank, a.head_title, a.title, b.press_name, b.reporter_name, a.sub_content, a.main_img, a.content_idx, b.hit_count, b.comment_count, b.share_count, b.like_count, 'system', now(), 'N'
                     from ranking as a 
                     join news as b on (a.content_idx =  b.idx) 
                    where a.content_type = '".$content_type."'";

        }else if($content_type =='3'){
             $sql.=" select '', a.ranking_date, b.category_idx, b.channel_idx, a.ranking_type, a.content_type, a.rank, a.head_title, a.title, '', '', a.sub_content, a.main_img, a.content_idx, b.hit_count, b.comment_count, b.share_count, b.like_count, 'system', now(), 'N'
                     from ranking as a 
                     join life as b on (a.content_idx =  b.idx) 
                    where a.content_type = '".$content_type."'";

        }
        $this->db->query($sql);


    }





    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 log로 copy - 스케쥴
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function rank_copy_ranking_log($req_data){
        # request
	    $rank_type            = $req_data['rank_type'];
	    $content_type         = $req_data['content_type'];

        $sql ="insert into ranking_log";
        if($content_type =='1'){
            $sql.=" select '', a.ranking_date, b.category_code, '', a.ranking_type, a.content_type, a.rank, a.head_title, a.title, b.press_name, b.reporter_name, a.sub_content, a.main_img, a.content_idx, b.hit_count, b.comment_count, b.share_count, b.like_count, 'system', now(), 'N'
                     from ranking as a 
                     join news as b on (a.content_idx =  b.idx) 
                    where a.ranking_type='".$rank_type."' and a.content_type = '".$content_type."'";

        }else if($content_type =='3'){
             $sql.=" select '', a.ranking_date, b.category_idx, b.channel_idx, a.ranking_type, a.content_type, a.rank, a.head_title, a.title, '', '', a.sub_content, a.main_img, a.content_idx, b.hit_count, b.comment_count, b.share_count, b.like_count, 'system', now(), 'N'
                     from ranking as a 
                     join life as b on (a.content_idx =  b.idx) 
                    where a.ranking_type='".$rank_type."' and a.content_type = '".$content_type."'";

        }
        $this->db->query($sql);


    }



    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 - is_del update - 스케쥴
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function rank_is_del_where_content_type_update($req_data){
        # request
	    $rank_type            = $req_data['rank_type'];
	    $content_type         = $req_data['content_type'];

        $user_id        = 'system';
        $now_date       = date("Y-m-d H:i:s");

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

        $this->db->where('is_del', 'N');
        $this->db->where('rank_type', $rank_type);
        $this->db->where('content_type', $content_type);
        $row = $this->db->update('ranking', $update_data);

        return $row;


    }


    #---------------------------------------------------------------------------
    # PURPOSE : 현재랭킹 지우기
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function rank_where_ranking_date_delete($reg_date){

        $this->db->where('ranking_date = ', $reg_date);
        $row = $this->db->delete('ranking');

        return $row;


    }



    #---------------------------------------------------------------------------
    # PURPOSE : 오래된 랭킹 지우기
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function rank_where_org_ranking_date_delete($reg_date){

        $this->db->where('ranking_date <= ', $reg_date);
        $row = $this->db->delete('ranking');

        return $row;


    }

    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 insert
    # CREATE  :	2018-03-06
    #---------------------------------------------------------------------------
	function ranking_all_insert($req_data){

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


    
    





}
?>
