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

class m_life extends CI_Model{

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

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

	#---------------------------------------------------------------------------
    # PURPOSE :	카테고리 - list
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function category_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 = '';
        if(isset($req_data['sort_column'])==true)   $sort_column            = $req_data['sort_column'];


        if($search_type!='' && $search_keyword!=''){
            $this->db->like($search_type, $search_keyword);
        }

        $this->db->select('*, (select count(*) from channel where category_idx=category.idx and is_del="N") as channel_count, (select count(*) from partner where category_idx=category.idx) as editor_count, (select count(*) from life where category_idx=category.idx and is_del="N") as article_count, (select sum(hit_count) from life where category_idx=category.idx and is_del="N") as hit_count');
        $this->db->where('is_del', 'N');
        $this->db->from('category');
        $query = $this->db->get_compiled_select();

        $this->db->from('('.$query.') as f');
        if($sort_column!='')    $this->db->order_by('f.'.$sort_column,  'DESC');
        $this->db->limit($limit, $offset);
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	카테고리 - list용 - total count
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function category_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];

        if($search_type!='' && $search_keyword!=''){
            $this->db->like($search_type, $search_keyword);
        }

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

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	카테고리 - insert
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function category_insert($req_data){
	    # request
        $category_name  = $req_data['category_name'];
        $display_sort   = $req_data['display_sort'];

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

        $insert_data = array('category_name' => $category_name,
                             'display_sort' => $display_sort,
                             'reg_id' => $user_id,
                             'reg_date' => $now_date,
                             'update_id' => $user_id,
                             'update_date' => $now_date
        );
        $row = $this->db->insert('category', $insert_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	카테고리 - category.is_del update
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function category_is_del_update($req_data){
        # request
        $category_idx       = $req_data;

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

        $update_data = array('is_del' => 'Y',
                             'update_id'        => $user_id,
                             'update_date'      => $now_date
        );
        $this->db->where('idx', $category_idx);
        $row = $this->db->update('category', $update_data);

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	life is_del where channel_idx update
    # CREATE  : 2018-03-02
    #---------------------------------------------------------------------------
	function life_is_del_where_channel_idx_update($req_data){
	    # request
	    $channel_idx            = $req_data;

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

        $this->db->where('channel_idx', $channel_idx);
        $row = $this->db->update('life', $update_data);



        return $row;


    }



    #---------------------------------------------------------------------------
    # PURPOSE :	is_del where partner_idx update
    # CREATE  : 2018-03-14
    #---------------------------------------------------------------------------
	function life_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('life', $update_data);

        return $row;
    }



	#---------------------------------------------------------------------------
    # PURPOSE :	life - list
    # CREATE  :	2018-03-21
    #---------------------------------------------------------------------------
	function life_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_life_type       = $req_data['search_life_type'];
        $search_category_code1  = $req_data['search_category_code1'];
        $search_category_code2  = $req_data['search_category_code2'];
        $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('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 if($search_type == 'write_id') {
                $this->db->where($search_type, $search_keyword);
            } else {
                $this->db->like($search_type, $search_keyword);
            }
        }
        if($search_life_type!='')       $this->db->where('a.life_type', $search_life_type);
        if($search_category_code1!='')  $this->db->where('a.category_idx', $search_category_code1);
        if($search_category_code2!='')  $this->db->where('a.channel_idx', $search_category_code2);
        if($search_write_id!='')        $this->db->where('a.write_id', $search_write_id);

        $this->db->select('a.*, b.category_name as category_name1, c.channel_name as category_name2');
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->where('c.is_del', 'N');
        $this->db->from('life as a');
        $this->db->join('category as b', 'a.category_idx = b.idx');
        $this->db->join('channel as c', 'a.channel_idx = c.idx');
        $this->db->limit($limit, $offset);
        $this->db->order_by($sort_column,  'desc');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	life - list
    # CREATE  :	2018-03-21
    #---------------------------------------------------------------------------
	function life_is_display_group_by_list($req_data){
        # request
        $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_life_type       = $req_data['search_life_type'];
        $search_category_code1  = $req_data['search_category_code1'];
        $search_category_code2  = $req_data['search_category_code2'];
        $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('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 if($search_type == 'write_id') {
                $this->db->where($search_type, $search_keyword);
            } else {
                $this->db->like($search_type, $search_keyword);
            }
        }
        if($search_life_type!='')       $this->db->where('a.life_type', $search_life_type);
        if($search_category_code1!='')  $this->db->where('a.category_idx', $search_category_code1);
        if($search_category_code2!='')  $this->db->where('a.channel_idx', $search_category_code2);
        if($search_write_id!='')        $this->db->where('a.write_id', $search_write_id);

        $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('c.is_del', 'N');
        $this->db->from('life as a');
        $this->db->join('category as b', 'a.category_idx = b.idx');
        $this->db->join('channel as c', 'a.channel_idx = c.idx');
        $this->db->group_by('a.is_display');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }






    #---------------------------------------------------------------------------
    # PURPOSE :	life - list - total count
    # CREATE  :	2018-03-21
    #---------------------------------------------------------------------------
	function life_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_life_type       = $req_data['search_life_type'];
        $search_category_code1  = $req_data['search_category_code1'];
        $search_category_code2  = $req_data['search_category_code2'];
        $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('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_life_type!='')       $this->db->where('a.life_type', $search_life_type);
        if($search_category_code1!='')  $this->db->where('a.category_idx', $search_category_code1);
        if($search_category_code2!='')  $this->db->where('a.channel_idx', $search_category_code2);
        if($search_write_id!='')        $this->db->where('a.write_id', $search_write_id);
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->where('c.is_del', 'N');
        $this->db->from('life as a');
        $this->db->join('category as b', 'a.category_idx = b.idx');
        $this->db->join('channel as c', 'a.channel_idx = c.idx');
        $row = $this->db->count_all_results();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	life - new count
    # CREATE  :	2018-03-21
    #---------------------------------------------------------------------------
	function life_new_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_life_type       = $req_data['search_life_type'];
        $search_category_code1  = $req_data['search_category_code1'];
        $search_category_code2  = $req_data['search_category_code2'];
        $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('a.is_display', 'Y');
        } else if($search_click2=='2') {
            $this->db->like('a.is_display', 'N');
        }

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

        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_life_type!='')       $this->db->where('a.life_type', $search_life_type);
        if($search_category_code1!='')  $this->db->where('a.category_idx', $search_category_code1);
        if($search_category_code2!='')  $this->db->where('a.channel_idx', $search_category_code2);
        if($search_write_id!='')        $this->db->where('a.write_id', $search_write_id);

        $this->db->where('a.is_del', 'N');
        $this->db->where('b.is_del', 'N');
        $this->db->where('c.is_del', 'N');
        $this->db->where('a.reg_date >=', $now_date);
        $this->db->from('life as a');
        $this->db->join('category as b', 'a.category_idx = b.idx');
        $this->db->join('channel as c', 'a.channel_idx = c.idx');
        $row = $this->db->count_all_results();

        return $row;
    }


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

        return $row;


    }

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

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

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	update
    # CREATE  :	2018-03-21
    #---------------------------------------------------------------------------
	function life_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('life', $req_data);

        return $row;

    }

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

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	is_del update
    # CREATE  : 2018-03-21
    #---------------------------------------------------------------------------
	function life_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('life', $arr_update_data, 'idx');

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	select in idx
    # CREATE  :	2018-03-21
    #---------------------------------------------------------------------------
	function life_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('life');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }



	#---------------------------------------------------------------------------
    # PURPOSE :	theme life - list
    # CREATE  :	2018-03-21
    #---------------------------------------------------------------------------
	function theme_join_life_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'];
        $search_category_code1  = $req_data['search_category_code1'];
        $search_category_code2  = $req_data['search_category_code2'];
        $search_life_type       = $req_data['search_life_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($search_type, $search_keyword);
        }

        if($search_theme_type!='')          $this->db->where('a.theme_type', $search_theme_type);
        if($search_category_code1!='')      $this->db->where('b.category_idx', $search_category_code1);
        if($search_category_code2!='')      $this->db->where('b.channel_idx', $search_category_code2);
        if($search_life_type!='')           $this->db->where('b.life_type', $search_life_type);
        $this->db->select('a.idx, a.rank, a.title, a.theme_img as main_img, a.content_idx, a.theme_type, c.category_name as category_name1, d.channel_name as category_name2, b.editor_name, b.life_type, a.reg_date, a.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->where('c.is_del', 'N');
        $this->db->where('d.is_del', 'N');
        $this->db->from('theme as a');
        $this->db->join('life as b', 'a.content_idx = b.idx');
        $this->db->join('category as c', 'b.category_idx = c.idx');
        $this->db->join('channel as d', 'b.channel_idx = d.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 life - list
    # CREATE  :	2018-03-21
    #---------------------------------------------------------------------------
	function theme_join_life_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'];
        $search_category_code1  = $req_data['search_category_code1'];
        $search_category_code2  = $req_data['search_category_code2'];
        $search_life_type       = $req_data['search_life_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($search_type, $search_keyword);
        }

        if($search_theme_type!='')          $this->db->where('a.theme_type', $search_theme_type);
        if($search_category_code1!='')      $this->db->where('b.category_idx', $search_category_code1);
        if($search_category_code2!='')      $this->db->where('b.channel_idx', $search_category_code2);
        if($search_life_type!='')           $this->db->where('b.life_type', $search_life_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->where('c.is_del', 'N');
        $this->db->where('d.is_del', 'N');
        $this->db->from('theme as a');
        $this->db->join('life as b', 'a.content_idx = b.idx');
        $this->db->join('category as c', 'b.category_idx = c.idx');
        $this->db->join('channel as d', 'b.channel_idx = d.idx');
        $this->db->group_by('b.is_display');
        $res = $this->db->get();
        $row = $res->result_array();


        return $row;
    }






    #---------------------------------------------------------------------------
    # PURPOSE :	theme life - list - total count
    # CREATE  :	2018-03-06
    #---------------------------------------------------------------------------
	function theme_join_life_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'];
        $search_category_code1  = $req_data['search_category_code1'];
        $search_category_code2  = $req_data['search_category_code2'];
        $search_life_type       = $req_data['search_life_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($search_type, $search_keyword);
        }
        if($search_theme_type!='')          $this->db->where('a.theme_type', $search_theme_type);
        if($search_category_code1!='')      $this->db->where('b.category_idx', $search_category_code1);
        if($search_category_code2!='')      $this->db->where('b.channel_idx', $search_category_code2);
        if($search_life_type!='')           $this->db->where('b.life_type', $search_life_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->where('c.is_del', 'N');
        $this->db->where('d.is_del', 'N');
        $this->db->from('theme as a');
        $this->db->join('life as b', 'a.content_idx = b.idx');
        $this->db->join('category as c', 'b.category_idx = c.idx');
        $this->db->join('channel as d', 'b.channel_idx = d.idx');
        $row = $this->db->count_all_results();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE : theme life - new count
    # CREATE  :	2018-03-05
    #---------------------------------------------------------------------------
	function theme_join_life_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'];
        $search_category_code1  = $req_data['search_category_code1'];
        $search_category_code2  = $req_data['search_category_code2'];
        $search_life_type       = $req_data['search_life_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');
        }


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

        if($search_type!='' && $search_keyword!=''){
            $this->db->like($search_type, $search_keyword);
        }
        if($search_theme_type!='')          $this->db->where('a.theme_type', $search_theme_type);
        if($search_category_code1!='')      $this->db->where('b.category_idx', $search_category_code1);
        if($search_category_code2!='')      $this->db->where('b.channel_idx', $search_category_code2);
        if($search_life_type!='')           $this->db->where('b.life_type', $search_life_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->where('c.is_del', 'N');
        $this->db->where('d.is_del', 'N');
        $this->db->from('theme as a');
        $this->db->join('life as b', 'a.content_idx = b.idx');
        $this->db->join('category as c', 'b.category_idx = c.idx');
        $this->db->join('channel as d', 'b.channel_idx = d.idx');
        $this->db->where('a.reg_date >=', $now_date);
        $row = $this->db->count_all_results();

        return $row;
    }




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

        $this->db->select('a.*, b.hit_count, b.like_count, b.editor_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('life as b', 'a.content_idx = b.idx');
        $res = $this->db->get();
        $row = $res->result_array();


        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	life - list limit 100
    # CREATE  :	2018-03-21
    #---------------------------------------------------------------------------
	function life_list_limit100($req_data){
        # request
        $article_keyword    = $req_data['article_keyword'];
        $sort_column        = $req_data['sort_column'];
        $sort_type          = $req_data['sort_type'];
        $category1          = $req_data['category1'];
        $category2          = $req_data['category2'];
        $category3          = $req_data['category3'];


        if($category1!='')  $this->db->where('category_idx', $category1);
        if($category2!='')  $this->db->where('channel_idx', $category2);
        if($category3!='')  $this->db->where('partner_idx', $category3);

        $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('life');
        $this->db->order_by($sort_column,  $sort_type);
        $this->db->limit(100, 0);
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


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

        $this->db->select('a.*, b.editor_name, b.like_count, b.hit_count');
        $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('b.is_display', 'Y');
        $this->db->from('theme as a');
        $this->db->join('life as b', 'a.content_idx = b.idx');
        $res = $this->db->get();
        $row = $res->result_array();


        return $row;
    }





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

        return $row;
    }


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

        return $row;

    }


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

        return $row;
        

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 리스트 - 스케쥴에서 사용중
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function life_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('life');
        $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 : 스케쥴에서 사용중
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function life_where_channel_idx_sum_count($req_data){
        # request
        $start_date     = $req_data['start_date'];
        $channel_idx    = $req_data['channel_idx'];

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

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE : 스케쥴에서 사용중
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function life_where_partner_idx_sum_count($req_data){
        # request
        $start_date     = $req_data['start_date'];
        $partner_idx    = $req_data['partner_idx'];

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

        return $row;
    }







    #---------------------------------------------------------------------------
    # PURPOSE : 에디터별 북마크
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function life_join_bookmark_editor_count($req_data){
	    # request
        $content_type   = $req_data['content_type'];
        $start_date     = $req_data['start_date'];
        $partner_idx    = $req_data['partner_idx'];

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

        return $row;
    }


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

        return $row;

    }




    #---------------------------------------------------------------------------
    # PURPOSE :	스케쥴에서 사용중
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function life_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('life');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }






}
?>
