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

class m_column extends CI_Model{

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

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

	#---------------------------------------------------------------------------
    # PURPOSE :	column - list
    # CREATE  :	2018-03-06
    #---------------------------------------------------------------------------
	function column_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            = $req_data['sort_column'];
        $search_write_id        = $req_data['search_write_id'];

        $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('reg_date >=', $today_date);
        }

        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 if($search_type == 'write_id') {
                $this->db->where($search_type, $search_keyword);
            } else {
                $this->db->like($search_type, $search_keyword);
            }
        }
        if($search_write_id!='')    $this->db->where('write_id', $search_write_id);
        $this->db->where('is_del', 'N');
        $this->db->from('column');
        $this->db->limit($limit, $offset);
        $this->db->order_by($sort_column,  'desc');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	column list - total count
    # CREATE  : 2018-03-06
    #---------------------------------------------------------------------------
	function column_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_write_id        = $req_data['search_write_id'];

        $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('reg_date >=', $today_date);
        }

        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 if($search_type == 'write_id') {
                $this->db->where($search_type, $search_keyword);
            } else {
                $this->db->like($search_type, $search_keyword);
            }
        }
        if($search_write_id!='')    $this->db->where('write_id', $search_write_id);
        $this->db->where('is_del', 'N');
        $this->db->from('column');
        $row = $this->db->count_all_results();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	column - list
    # CREATE  :	2018-03-06
    #---------------------------------------------------------------------------
	function column_is_display_group_by_list($req_data){
        # request
        $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_write_id        = $req_data['search_write_id'];

        $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('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 if($search_type == 'write_id') {
                $this->db->where($search_type, $search_keyword);
            } else {
                $this->db->like($search_type, $search_keyword);
            }
        }
        if($search_write_id!='')    $this->db->where('write_id', $search_write_id);
        $this->db->select('is_display, count(*) as cnt');
        $this->db->where('is_del', 'N');
        $this->db->from('column');
        $this->db->group_by('is_display');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }





    #---------------------------------------------------------------------------
    # PURPOSE :	new count
    # CREATE  :	2018-06-11
    #---------------------------------------------------------------------------
	function column_new_count($req_data){
	    # request
        $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_write_id        = $req_data['search_write_id'];

        $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 if($search_type == 'write_id') {
                $this->db->where($search_type, $search_keyword);
            } else {
                $this->db->like($search_type, $search_keyword);
            }
        }

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

        if($search_write_id!='')    $this->db->where('write_id', $search_write_id);
        $this->db->where('is_del', 'N');
        $this->db->where('reg_date >=', $now_date);
        $this->db->from('column');
        $row = $this->db->count_all_results();

        return $row;

    }




    #---------------------------------------------------------------------------
    # PURPOSE :	column - list limit 100
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function column_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('column');
        $this->db->order_by($sort_column,  $sort_type);
        $this->db->limit(100, 0);
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	column  - insert
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function column_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('column', $req_data);
        $row = $this->db->insert_id();

        return $row;


    }


    #---------------------------------------------------------------------------
    # PURPOSE :	column - update
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function column_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('column', $req_data);

        return $row;

    }

    
    #---------------------------------------------------------------------------
    # PURPOSE :	is_display update
    # CREATE  : 2018-03-08
    #---------------------------------------------------------------------------
	function column_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('column', $update_data);

        return $row;
    }








	#---------------------------------------------------------------------------
    # PURPOSE :	theme column - list
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function theme_join_column_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_theme_type      = $req_data['search_theme_type'];
        $content_type           = $req_data['content_type'];
        $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('b.is_display', 'Y');
        } else if($search_click2=='2') {
            $this->db->like('b.is_display', 'N');
        }

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

        $this->db->select('a.idx, a.rank, a.title, a.theme_img as main_img, a.theme_type, a.content_idx, b.column_name, a.reg_date, b.is_display, b.display_date, b.hit_count, b.like_count, b.comment_count, b.share_count');
        $this->db->where('a.content_type', $content_type);
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->from('theme as a');
        $this->db->join('column as b', 'a.content_idx = b.idx');
        $this->db->limit($limit, $offset);
        $this->db->order_by($sort_column,  'desc');
        $res = $this->db->get();
        $row = $res->result_array();



        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	theme column - list - total count
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function theme_join_column_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_theme_type      = $req_data['search_theme_type'];
        $content_type           = $req_data['content_type'];


        $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('b.is_display', 'Y');
        } else if($search_click2=='2') {
            $this->db->like('b.is_display', 'N');
        }


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

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

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	theme column - list
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function theme_join_column_is_display_group_by_list($req_data){
        # request
        $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_theme_type      = $req_data['search_theme_type'];
        $content_type           = $req_data['content_type'];

        $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!=''){
            $this->db->like('b.'.$search_type, $search_keyword);
        }
        if($search_theme_type!='')  $this->db->where('a.theme_type', $search_theme_type);

        $this->db->select('b.is_display, count(*) as cnt');
        $this->db->where('a.content_type', $content_type);
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->from('theme as a');
        $this->db->join('column as b', 'a.content_idx = b.idx');
        $this->db->group_by('b.is_display');
        $res = $this->db->get();
        $row = $res->result_array();



        return $row;
    }




    #---------------------------------------------------------------------------
    # PURPOSE :	new count
    # CREATE  :	2018-06-11
    #---------------------------------------------------------------------------
	function theme_join_column_new_count($req_data){

	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_theme_type      = $req_data['search_theme_type'];
        $content_type           = $req_data['content_type'];


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


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


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

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


        $this->db->where('a.content_type', $content_type);
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->where('a.reg_date >=', $now_date);
        $this->db->from('theme as a');
        $this->db->join('column as b', 'a.content_idx = b.idx');
        $row = $this->db->count_all_results();

        return $row;


    }






    #---------------------------------------------------------------------------
    # PURPOSE : select
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function column_select($req_data){
	    # request
	    $idx            = $req_data;

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

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	테마 type list - select
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function theme_join_column_type_list($req_data){
	    # request
        $content_type       = $req_data['content_type'];
        $theme_type         = $req_data['theme_type'];

        $this->db->select('a.*, b.partner_idx, b.column_name');
        $this->db->where('a.content_type', $content_type);
        $this->db->where('a.theme_type', $theme_type);
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->where('a.is_display', 'Y');
        $this->db->from('theme as a');
        $this->db->join('column as b', 'a.content_idx = b.idx');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }




    #---------------------------------------------------------------------------
    # PURPOSE :	theme column - select
    # CREATE  :	2018-03-07
    #---------------------------------------------------------------------------
	function theme_join_column_select($req_data){
	    # request
        $idx       = $req_data;

        $this->db->select('a.*, b.column_name');
        $this->db->where('a.idx', $idx);
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->from('theme as a');
        $this->db->join('column as b', 'a.content_idx = b.idx');
        $res = $this->db->get();
        $row = $res->result_array();


        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	관련기사 - list
    # CREATE  :	2018-03-11
    #---------------------------------------------------------------------------
	function relation_article_join_column_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('column 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 column_tag_select($req_data){
	    # request
        $content_idx        = $req_data['content_idx'];
        $tag                = $req_data['tag'];

        $sql = "select * from `column` 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 :	is_del where partner_idx update
    # CREATE  : 2018-03-14
    #---------------------------------------------------------------------------
	function column_is_del_where_partner_idx_update($req_data){
	    # request
	    $partner_idx    = $req_data;
        $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('partner_idx', $partner_idx);
        $row = $this->db->update('column', $update_data);

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	예약글 따른 리스트  - 스케쥴에서 사용중
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function column_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('is_display', 'N');
        $this->db->where('is_del', 'N');
        $this->db->from('column');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	is_display update batch
    # CREATE  : 2018-03-30
    #---------------------------------------------------------------------------
	function column_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('column', $arr_update_data, 'idx');

        return $row;

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	video_url update
    # CREATE  : 2018-05-11
    #---------------------------------------------------------------------------
	function column_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('column', $update_data);

        return $row;


    }

    #---------------------------------------------------------------------------
    # PURPOSE : 스케쥴 인기순 초기화
    # CREATE  : 2018-05-11
    #---------------------------------------------------------------------------
	function column_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('column', $update_data);

        return $row;

    }


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

	    $this->db->select('idx, hit_count, 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('column');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }










}
?>
