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

class m_news extends CI_Model{

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

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

	#---------------------------------------------------------------------------
    # PURPOSE :	news - list
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function news_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_news_type       = $req_data['search_news_type'];
        $search_category_code   = $req_data['search_category_code'];
        $sort_column            = $req_data['sort_column'];

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

        if($search_click1=='1') {
            $today_date             = date("Y-m-d").' 00:00:00';
            $this->db->where('a.reg_date >=', $today_date);
        }

        if($search_click2=='1') {
            $this->db->like('a.is_display', 'Y');
        } else if($search_click2=='2') {
            $this->db->like('a.is_display', 'N');
        }

        if($search_type!='' && $search_keyword!=''){
            if($search_type == 'tag') {
                $this->db->where("match(tag) against('".$search_keyword."'  in boolean mode)");
            } else {
                $this->db->like($search_type, $search_keyword);
            }
        }
        if($search_news_type!='')       $this->db->where('a.news_type', $search_news_type);
        if($search_category_code!='')   $this->db->like('a.category_code', $search_category_code, 'after');

        $this->db->select('a.*, b.category_name as category_name1');
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->where('b.depth', '1');
        $this->db->from('news as a');
        $this->db->join('news_section as b', 'SUBSTRING(a.category_code, 1, 2) = b.category_code');
        $this->db->limit($limit, $offset);
        //$this->db->order_by($sort_column,  'desc');
        $this->db->order_by('a.idx desc');
        $res = $this->db->get();
        $row = $res->result_array();
        if($_SERVER['REMOTE_ADDR']=='24.44.89.197') {
            //echo $this->db->last_query();    
        }
        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	news - list - total count
    # CREATE  :	2018-02-27
    #---------------------------------------------------------------------------
	function news_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_news_type       = $req_data['search_news_type'];
        $search_category_code   = $req_data['search_category_code'];

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

        if($search_click1=='1') {
            $today_date             = date("Y-m-d").' 00:00:00';
            $this->db->where('a.reg_date >=', $today_date);
        }

        if($search_click2=='1') {
            $this->db->like('a.is_display', 'Y');
        } else if($search_click2=='2') {
            $this->db->like('a.is_display', 'N');
        }

        if($search_type!='' && $search_keyword!=''){
            if($search_type == 'tag') {
                $this->db->where("match(tag) against('".$search_keyword."'  in boolean mode)");
            } else {
                $this->db->like($search_type, $search_keyword);
            }
        }
        if($search_news_type!='')       $this->db->where('a.news_type', $search_news_type);
        if($search_category_code!='')   $this->db->like('a.category_code', $search_category_code, 'after');

        $this->db->select('a.*, b.category_name as category_name1');
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->where('b.depth', '1');
        $this->db->from('news as a');
        $this->db->join('news_section as b', 'SUBSTRING(a.category_code, 1, 2) = b.category_code');
        $row = $this->db->count_all_results();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	news - new count
    # CREATE  :	2018-03-05
    #---------------------------------------------------------------------------
	function news_new_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];

        $now_date   = date("Y-m-d")." 00:00:00";

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

        if($search_click2=='1') {
            $this->db->like('is_display', 'Y');
        } else if($search_click2=='2') {
            $this->db->like('is_display', 'N');
        }

        if($search_type!='' && $search_keyword!=''){
            if($search_type == 'tag') {
                $this->db->where("match(tag) against('".$search_keyword."'  in boolean mode)");
            } else {
                $this->db->like($search_type, $search_keyword);
            }
        }
        $this->db->where('is_del', 'N');
        $this->db->where('reg_date >=', $now_date);
        $this->db->from('news');
        $row = $this->db->count_all_results();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	is_display group by
    # CREATE  :	2018-02-27
    #---------------------------------------------------------------------------
	function news_is_display_group_by_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_news_type       = $req_data['search_news_type'];
        $search_category_code   = $req_data['search_category_code'];

        $search_click1 = '';
        if(isset($req_data['search_click1'])==true)      $search_click1 = $req_data['search_click1'];
        if($search_click1=='1') {
            $today_date             = date("Y-m-d").' 00:00:00';
            $this->db->where('a.reg_date >=', $today_date);
        }

        if($search_type!='' && $search_keyword!=''){
            if($search_type == 'tag') {
                $this->db->where("match(tag) against('".$search_keyword."'  in boolean mode)");
            } else {
                $this->db->like($search_type, $search_keyword);
            }
        }
        if($search_news_type!='')       $this->db->where('a.news_type', $search_news_type);
        if($search_category_code!='')   $this->db->like('a.category_code', $search_category_code, 'after');


        $this->db->select('a.is_display, count(*) as cnt');
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->where('b.depth', '1');
        $this->db->from('news as a');
        $this->db->join('news_section as b', 'SUBSTRING(a.category_code, 1, 2) = b.category_code');
        $this->db->group_by('a.is_display');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	news - section_code count
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function news_section_code_count($req_data){
	    # request
        $category_code          = $req_data['category_code'];

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

        if($display_date!='')   $this->db->where('display_date>=', $display_date);
        $this->db->like('category_code', $category_code, 'after');
        $this->db->where('is_del', 'N');
        $this->db->from('news');
        $row = $this->db->count_all_results();

        return $row;
    }

    

    #---------------------------------------------------------------------------
    # PURPOSE :	news category_code update
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function news_category_code_update($req_data){
	    # request
        $org_category_code  = $req_data['org_category_code'];
        $category_code      = $req_data['category_code'];

        $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['category_code']   = $category_code;

        $this->db->where('category_code', $org_category_code);
        $row = $this->db->update('news', $update_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스  - insert
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function news_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;
        $this->db->insert('news', $req_data);
        $row = $this->db->insert_id();

        return $row;


    }


    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스 - select
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function news_select($req_data){
	    # request
	    $idx            = $req_data;

        $this->db->where('idx', $idx);
        $this->db->where('is_del', 'N');
        $this->db->from('news');

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

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스 - update
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function news_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('news', $req_data);

        return $row;

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스 is_del update
    # CREATE  : 2018-03-02
    #---------------------------------------------------------------------------
	function news_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('news', $arr_update_data, 'idx');

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스 - select in idx
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function news_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('news');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스 is_display update
    # CREATE  : 2018-03-05
    #---------------------------------------------------------------------------
	function news_is_display_update($req_data){
	    # request
	    $idx            = $req_data['idx'];
	    $is_display     = $req_data['is_display'];

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

        if($is_display=='Y')    $display_date = $now_date;

        $update_data['update_id']       = $user_id;
        $update_data['update_date']     = $now_date;
        $update_data['display_date']    = $display_date;
        $update_data['is_display']      = $is_display;

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

        return $row;
        
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	news - list limit 100
    # CREATE  :	2018-03-05
    #---------------------------------------------------------------------------
	function news_list_limit100($req_data){
        # request
        $article_keyword    = $req_data['article_keyword'];
        $sort_column        = $req_data['sort_column'];
        $sort_type          = $req_data['sort_type'];


        $this->db->where('(title like "%'.$article_keyword.'%" or content like "%'.$article_keyword.'%" or tag like "%'.$article_keyword.'%")');
        $this->db->where('is_del', 'N');
        $this->db->where('is_display', 'Y');
        $this->db->from('news');
        $this->db->order_by($sort_column,  $sort_type);
        $this->db->limit(100, 0);
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스 hit_count update
    # CREATE  : 2018-03-07
    #---------------------------------------------------------------------------
	function news_hit_count_update($req_data){
	    # request
	    $idx            = $req_data['idx'];
	    $hit_count      = $req_data['hit_count'];

        $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['hit_count']       = $hit_count;

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

        return $row;

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스 review_count update
    # CREATE  : 2018-03-07
    #---------------------------------------------------------------------------
	function news_review_count1_update($req_data){
	    # request
	    $idx                = $req_data['idx'];
	    $review_count1      = $req_data['review_count1'];

        $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['review_count1']   = $review_count1;

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

        return $row;

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	관련기사 - list
    # CREATE  :	2018-03-11
    #---------------------------------------------------------------------------
	function relation_article_join_news_list($req_data){
        # request
        $content_idx        = $req_data['content_idx'];
        $content_type       = $req_data['content_type'];

        $this->db->select('a.*, b.title');
        $this->db->where('a.content_idx', $content_idx);
        $this->db->where('a.content_type', $content_type);
        $this->db->where('b.is_del', 'N');
        $this->db->from('relation_article as a');
        $this->db->join('news as b', 'a.relation_article_idx=b.idx');
        $this->db->order_by('a.content_idx',  'asc');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	tag - select
    # CREATE  :	2018-03-11
    #---------------------------------------------------------------------------
	function news_tag_select($req_data){
	    # request
        $content_idx        = $req_data['content_idx'];
        $tag                = $req_data['tag'];

        $sql = "select * from `news` where is_del = 'N' and idx!= ".$content_idx." and match(tag, title) against('".$tag."'  in boolean mode) order by rand() limit 5";
        $res = $this->db->query($sql);
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	group by press_idx
    # CREATE  :	2018-03-22
    #---------------------------------------------------------------------------
	function news_group_by_press_idx(){

        $this->db->select('press_idx, count(press_idx) as cnt');
	    $this->db->where('is_del', 'N');
	    $this->db->group_by("press_idx");
        $this->db->from('news');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	news 노춯여부에 따른 리스트  - 스케쥴에서 사용중
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function news_reservation_article_list(){

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

        $this->db->select('idx');
        $this->db->where('display_date <=', $now_date);
        $this->db->where('display_date !=', '0000-00-00 00:00:00');
        $this->db->where('is_display', 'N');
        $this->db->where('is_del', 'N');
        $this->db->from('news');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	뉴스 is_display update batch - 스케쥴에서 사용중
    # CREATE  : 2018-03-30
    #---------------------------------------------------------------------------
	function news_is_display_update_batch($req_data){

        $user_id    = 'system';
        $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['idx'];
            $arr_update_data[$key]['is_display']    = 'Y';
            $arr_update_data[$key]['update_id']     = $user_id;
            $arr_update_data[$key]['update_date']   = $now_date;
        }
        $row = $this->db->update_batch('news', $arr_update_data, 'idx');


        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 리스트 - 스케쥴에서 사용중
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function news_rank_list($req_data){
        # request
        $order_by        = $req_data['order_by'];

        $this->db->where('is_display', 'Y');
        $this->db->where('is_del', 'N');
        $this->db->from('news');
        $this->db->limit(50, 0);
        $this->db->order_by($order_by,  'desc');
        $this->db->order_by('display_date',  'desc');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	video_url update
    # CREATE  : 2018-05-11
    #---------------------------------------------------------------------------
	function news_video_url_update($req_data){
	    # request
        $idx            = $req_data['content_idx'];
        $video_url      = $req_data['video_url'];
        $video_playtime = $req_data['video_playtime'];

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

        $update_data = array('video_url' => $video_url,
                             'video_playtime' => $video_playtime,
                             'update_id'        => $user_id,
                             'update_date'      => $now_date
        );
        $this->db->where('idx', $idx);
        $row = $this->db->update('news', $update_data);

        return $row;


    }

    #---------------------------------------------------------------------------
    # PURPOSE : 뉴스 - 스케쥴 인기순 초기화
    # CREATE  : 2018-05-11
    #---------------------------------------------------------------------------
	function news_popular_count_where_display_date_update($delete_date){

	    $start_date = $delete_date.' 00:00:00';
	    $end_date   = $delete_date.' 23:59:59';

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

        $update_data = array('popular_count' => 0,
                             'update_id'        => $user_id,
                             'update_date'      => $now_date
        );
        $this->db->where('display_date>=', $start_date);
        $this->db->where('display_date<=', $end_date);
        $row = $this->db->update('news', $update_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	스케쥴에서 사용중
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function news_where_display_date_list($start_date){

	    $this->db->select('idx, hit_count, review_count1 as like_count, comment_count');
        $this->db->where('display_date >=', $start_date);
        $this->db->where('is_display', 'Y');
        $this->db->where('is_del', 'N');
        $this->db->from('news');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }







}
?>
