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

class m_weather extends CI_Model{

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

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

	#---------------------------------------------------------------------------
    # PURPOSE :	영적기상도 - list
    # CREATE  :	2018-03-05
    #---------------------------------------------------------------------------
	function weather_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_display_status  = $req_data['search_display_status'];

        $where_query = "";
        if($search_type!='' && $search_keyword!=''){
            $where_query.=" and AA.".$search_type." like '%".$search_keyword."%'";
        }
        if($search_display_status!='')       $where_query.=" and AA.display_status = '".$search_display_status."'";
        $sql = "select * 
                  from (SELECT *, (select GROUP_CONCAT(DISTINCT b.title ORDER BY b.title DESC SEPARATOR '||||') from relation_article as a join news as b on (a.relation_article_idx=b.idx) where b.is_del='N' and a.content_type='5' and a.content_idx=weather.idx) as news_title, (select relation_article_idx from relation_article as a join news as b on (a.relation_article_idx=b.idx) where b.is_del='N' and a.content_type='5' and a.content_idx=weather.idx limit 1) as news_idx  
                          FROM weather WHERE is_del = 'N'
                  ) as AA  where 1 ".$where_query." order by AA.reg_date desc limit $offset, $limit ";

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


        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	영적기상도 - list - total count
    # CREATE  :	2018-03-05
    #---------------------------------------------------------------------------
	function weather_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_display_status  = $req_data['search_display_status'];


        $where_query = "";
        if($search_type!='' && $search_keyword!=''){
            $where_query.=" and AA.".$search_type." like '%".$search_keyword."%'";
        }
        if($search_display_status!='')       $where_query.=" and AA.display_status = '".$search_display_status."'";
        $sql = "select * 
                  from (SELECT *, (select GROUP_CONCAT(DISTINCT b.title ORDER BY b.title DESC SEPARATOR '||||') from relation_article as a join news as b on (a.relation_article_idx=b.idx) where b.is_del='N' and a.content_type='5' and a.content_idx=weather.idx) as news_title 
                          FROM weather WHERE is_del = 'N'
                  ) as AA  where 1 ".$where_query;

        $res = $this->db->query($sql);
        $row = $res->num_rows();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	영적기상도 - 상태별 count
    # CREATE  :	2018-03-26
    #---------------------------------------------------------------------------
	function weather_list_display_status($req_data){
        # request
        $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];

        $where_query = "";
        if($search_type!='' && $search_keyword!=''){
            $where_query.=" and AA.".$search_type." like '%".$search_keyword."%'";
        }

        $sql = "select display_status, count(*) as cnt
                  from (SELECT *, (select GROUP_CONCAT(DISTINCT b.title ORDER BY b.title DESC SEPARATOR '||||') from relation_article as a join news as b on (a.relation_article_idx=b.idx) where b.is_del='N' and a.content_type='5' and a.content_idx=weather.idx) as news_title 
                          FROM weather WHERE is_del = 'N'
                  ) as AA  where 1 ".$where_query." group by display_status";

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


        return $row;
    }





    #---------------------------------------------------------------------------
    # PURPOSE :	display_status update
    # CREATE  : 2018-03-26
    #---------------------------------------------------------------------------
	function weather_display_status_update($req_data){
	    # request
	    $idx            = $req_data['idx'];
	    $display_status = $req_data['display_status'];

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

        if($display_status=='1')    $display_date = $now_date;

        $update_data['update_id']       = $user_id;
        $update_data['update_date']     = $now_date;
        if($display_status=='1')     $update_data['display_date']    = $display_date;
        $update_data['display_status']  = $display_status;

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

        return $row;

    }


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

    }





    #---------------------------------------------------------------------------
    # PURPOSE :	날씨아이콘 - list
    # CREATE  :	2018-03-26
    #---------------------------------------------------------------------------
	function weather_icon_list(){
        $this->db->from('weather_icon');
        $this->db->order_by('idx', 'desc');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	날씨아이콘 - insert
    # CREATE  :	2018-03-26
    #---------------------------------------------------------------------------
	function weather_icon_insert($req_data){
	    # request
        $weather_icon  = $req_data;

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

        $insert_data = array('weather_icon' => $weather_icon,
                             'reg_id' => $user_id,
                             'reg_date' => $now_date,
        );
        $row = $this->db->insert('weather_icon', $insert_data);

        return $row;

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	영적기상도  - insert
    # CREATE  :	2018-03-26
    #---------------------------------------------------------------------------
	function weather_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('weather', $req_data);
        $row = $this->db->insert_id();

        return $row;


    }


    #---------------------------------------------------------------------------
    # PURPOSE :	영적기상도 - select
    # CREATE  :	2018-03-26
    #---------------------------------------------------------------------------
	function weather_select($req_data){
	    # request
	    $idx            = $req_data;

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

        return $row;
    }




    #---------------------------------------------------------------------------
    # PURPOSE :	영적기상도 - update
    # CREATE  :	2018-03-26
    #---------------------------------------------------------------------------
	function weather_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('weather', $req_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	영적기상도 - 랭크 count - 예약, 미사용제외
    # CREATE  :	2018-03-26
    #---------------------------------------------------------------------------
	function weather_rank_select($req_data){
	    # request
	    $rank            = $req_data['rank'];

        $this->db->where('rank', $rank);
        $this->db->where('display_status', '1');
        $this->db->where('is_del', 'N');
        $this->db->from('weather');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	영적기상도 - rank list  - 예약, 미사용제외
    # CREATE  :	2018-03-26
    #---------------------------------------------------------------------------
	function weather_rank_list(){

        $this->db->where('display_status', '1');
        $this->db->where('is_del', 'N');
        $this->db->from('weather');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


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

        $this->db->select('a.rank, c.idx, c.title');
        $this->db->where('a.display_status', '1');
        $this->db->where('a.is_del', 'N');
        $this->db->where('c.is_del', 'N');
        $this->db->from('weather as a');
        $this->db->join('relation_article as b', 'a.idx=b.content_idx and b.content_type='.$content_type);
        $this->db->join('news as c', 'b.relation_article_idx=c.idx');
        $this->db->order_by('a.reg_date',  'desc');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	예약글 업데이트
    # CREATE  : 2018-03-05
    #---------------------------------------------------------------------------
	function weather_reservation_update(){
        $user_id    = $this->session->userdata('Adm_Id');
        $now_date   = date("Y-m-d H:i:s");

        $display_now_date   = date("Y-m-d");

        $req_data['display_status'] = '1';
        $req_data['update_id']      = $user_id;
        $req_data['update_date']    = $now_date;

        $this->db->where('is_del', 'N');
        $this->db->where('display_status', '0');
        $this->db->where('display_date<=', $display_now_date);
        $row = $this->db->update('weather', $req_data);

        return $row;

    }










}
?>
