<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
 * 컨텐츠관련 스케쥴
 */
class cron01 extends CI_Controller {
    #---------------------------------------------------------------------------
    # PURPOSE :	언론사 등록기사 업데이트
    # CREATE  :	2018-03-22
    #---------------------------------------------------------------------------
    function press_article_count_update() {

        if (!is_dir('./logs/cron01/'.date("Ym")))   mkdir('./logs/cron01/'.date("Ym"), 0777, TRUE);

        # file helper
        $this->load->helper('file');
        $contents = '['.date("Y-m-d H:i:s").'] press_article_count_update start'.chr(10);

        # model - 뉴스 count
        $this->load->model('admin/news/m_news');
        $result1    = $this->m_news->news_group_by_press_idx();

        # model - press update
        $this->load->model('admin/etc_section/m_press');
        $result2    = $this->m_press->press_article_count_update($result1);

        $contents.= $result2.'개 업데이트 완료'.chr(10);
        $contents.= '['.date("Y-m-d H:i:s").'] press_article_count_update end'.chr(10);
        $contents.= '-------------------------------------------------------------------------------------'.chr(10);
        write_file('./logs/cron01/'.date("Ym").'/'.date("Ymd").'.php', $contents, 'a+');



	}

	#---------------------------------------------------------------------------
    # PURPOSE :	예약글 노출 - 테마도 같이 해줄까?
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function reservation_article_display_update() {

	    if (!is_dir('./logs/cron01/'.date("Ym")))   mkdir('./logs/cron01/'.date("Ym"), 0777, TRUE);
	    # file helper
        $this->load->helper('file');
        $contents = '['.date("Y-m-d H:i:s").'] reservation_article_display_update start'.chr(10);


        $this->load->model('admin/news/m_news');
	    $this->load->model('admin/life/m_life');
        $this->load->model('admin/column/m_column');
        $arr = array('1', '2', '3');
        foreach ($arr as $content_type) {
            if($content_type == '1') {
                $result1    = $this->m_news->news_reservation_article_list();
                if(count($result1) > 0)     $this->m_news->news_is_display_update_batch($result1);
            } else if($content_type == '2') {
                $result1    = $this->m_column->column_reservation_article_list();
                if(count($result1) > 0)     $this->m_column->column_is_display_update_batch($result1);
            } else if($content_type == '3') {
                $result1    = $this->m_life->life_reservation_article_list();
                if(count($result1) > 0)     $this->m_life->life_is_display_update_batch($result1);
            }

            foreach ($result1 as $val) {
                $req_data = array();
                $req_data['content_type']   = $content_type;
                $req_data['content_idx']    = $val['idx'];
                $req_data['search_type']    = 'is_display';
                $req_data['search_key']     = 'N';

                // 테마
                $this->load->library('comm_admin');
                $this->comm_admin->fn_theme_change_is_display($req_data);

                // 기타테마
                $this->load->library('comm_admin');
                $this->comm_admin->fn_etc_section_change_is_display($req_data);

            }


            $contents.= $content_type.'-----------------------------------------------------------------------------------'.chr(10);

            ob_flush();
            flush();
            sleep(1);

        }

	    $contents.= '['.date("Y-m-d H:i:s").'] reservation_article_display_update end'.chr(10);
        $contents.= '-------------------------------------------------------------------------------------'.chr(10);
        write_file('./logs/cron01/'.date("Ym").'/'.date("Ymd").'.php', $contents, 'a+');
    }



	#---------------------------------------------------------------------------
    # PURPOSE :	랭킹관리 노출 - 뉴스 라이프
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function article_rank_update() {

	    if (!is_dir('./logs/cron01/'.date("Ym")))   mkdir('./logs/cron01/'.date("Ym"), 0777, TRUE);

        # file helper
        $this->load->helper('file');
        $contents = '['.date("Y-m-d H:i:s").'] article_rank_update start'.chr(10);



	    $this->load->model('admin/comm/m_ranking');
	    $this->load->model('admin/news/m_news');
	    $this->load->model('admin/life/m_life');

        $this->m_ranking->rank_all_copy_ranking_log(1);  // 로그로 이동
        $this->m_ranking->rank_all_copy_ranking_log(3);  // 로그로 이동


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


        $this->m_ranking->rank_where_ranking_date_delete($ranking_date);


        # 뉴스
        $arr_orderby = array('hit_count', 'comment_count', 'review_count1');
        $z=1;
        foreach ($arr_orderby as $val0) {
            $req_data = array();
            $req_data['order_by'] = $val0;
            $result1    = $this->m_news->news_rank_list($req_data);
            if(count($result1) > 0) {
                $i=1;
                foreach ($result1 as $val) {
                    $org_file = $val['main_img'];
                    $filename = '';
                    if($org_file!='') {
                        # image copy
                        $upload_path = './files/ranking/'.date("Y")."/".date("m");
                        if (!is_dir($upload_path)) {
                            mkdir($upload_path, 0777, TRUE);
                        }
                        $arr_new_file =  explode('/', $org_file);
                        copy('.' . $org_file, $upload_path . '/ranking_type'.$z.$i. '_' . $arr_new_file[5]);
                        $filename = '/files/ranking/'.date("Y").'/'.date("m").'/ranking_type'.$z.$i. '_' . $arr_new_file[5];
                    }

                    $req_data = array();
                    $req_data['ranking_date']   = $ranking_date;
                    $req_data['ranking_type']   = $z;
                    $req_data['content_type']   = '1';
                    $req_data['rank']           = $i;
                    $req_data['title']          = $val['title'];
                    $req_data['main_img']       = $filename;
                    $req_data['content_idx']    = $val['idx'];
                    $req_data['reg_id']         = 'system';
                    $req_data['reg_date']       = $now_date;
                    $req_data['update_id']      = 'system';
                    $req_data['update_date']    = $now_date;

                    $this->m_ranking->ranking_all_insert($req_data);
                    $i++;
                }
            }

            $z++;

        }

        # 라이프
        $arr_orderby = array('hit_count', 'comment_count', 'like_count');
        $z=1;
        foreach ($arr_orderby as $val0) {
            $req_data = array();
            $req_data['order_by'] = $val0;
            $result1    = $this->m_life->life_rank_list($req_data);
            if(count($result1) > 0) {
                $i=1;
                foreach ($result1 as $val) {
                    $org_file = $val['main_img'];
                    $filename = '';
                    if($org_file!='') {
                        # image copy
                        $upload_path = './files/ranking/'.date("Y")."/".date("m");
                        if (!is_dir($upload_path)) {
                            mkdir($upload_path, 0777, TRUE);
                        }
                        $arr_new_file =  explode('/', $org_file);
                        copy('.' . $org_file, $upload_path . '/ranking_type'.$z.$i. '_' . $arr_new_file[5]);
                        $filename = '/files/ranking/'.date("Y").'/'.date("m").'/ranking_type'.$z.$i. '_' . $arr_new_file[5];
                    }


                    $req_data = array();
                    $req_data['ranking_date']   = $ranking_date;
                    $req_data['ranking_type']   = $z;
                    $req_data['content_type']   = '3';
                    $req_data['rank']           = $i;
                    $req_data['head_title']     = $val['head_title'];
                    $req_data['title']          = $val['title'];
                    $req_data['main_img']       = $filename;
                    $req_data['content_idx']    = $val['idx'];
                    $req_data['reg_id']         = 'system';
                    $req_data['reg_date']       = $now_date;
                    $req_data['update_id']      = 'system';
                    $req_data['update_date']    = $now_date;

                    $this->m_ranking->ranking_all_insert($req_data);
                    $i++;
                }
            }

            $z++;

        }

        $delete_date = date("Y-m-d", strtotime("-1 day"));
        $this->m_ranking->rank_where_org_ranking_date_delete($delete_date);


        $contents.= '['.date("Y-m-d H:i:s").'] article_rank_update end'.chr(10);
        $contents.= '-------------------------------------------------------------------------------------'.chr(10);
        write_file('./logs/cron01/'.date("Ym").'/'.date("Ymd").'.php', $contents, 'a+');


	}





    #---------------------------------------------------------------------------
    # PURPOSE :	실시간검색어
    # CREATE  :	2018-03-27
    #---------------------------------------------------------------------------
    function realtime_keyword_update() {

        if (!is_dir('./logs/cron01/'.date("Ym")))   mkdir('./logs/cron01/'.date("Ym"), 0777, TRUE);

        # file helper
        $this->load->helper('file');
        $contents = '['.date("Y-m-d H:i:s").'] realtime_keyword_update start'.chr(10);


        # model - realtime_keyword_setting 가져오기
        $idx = 1;

	    # model
        $this->load->model('admin/setting/m_realtime_keyword');
        $result1    = $this->m_realtime_keyword->realtime_keyword_setting_select($idx);
        $base_hour = $result1[0]['base_hour'];
        $now_date = date("Y-m-d H:i:s");

        $result2    = $this->m_realtime_keyword->realtime_keyword_max_reg_date();
        if(isset($result2[0]['reg_date'])==true){
            $date1 = strtotime($now_date);
            $date2 = strtotime($result2[0]['reg_date']);
            $diff_hour = floor(($date1 - $date2) / 3600);
        } else {
            $diff_hour = $base_hour;
            $date1 = $now_date;
            $date2 = date("Y-m-d H:i:s", strtotime ("-".$base_hour." hours"));
        }

        if($base_hour <= $diff_hour) {

            $this->m_realtime_keyword->realtime_keyword_move_log();

            $req_data = array();
            $req_data['start_date'] = $date2;
            $req_data['end_date']   = $date1;

            $this->load->model('admin/comm/m_search_keyword_log');
            $result3    = $this->m_search_keyword_log->search_keyword_log_group_by_keyword($req_data);

            $i=1;
            foreach ($result3 as $val) {
                $arr_keyword    = $this->m_realtime_keyword->realtime_keyword_where_keyword_select($val['keyword']);
                $rank = 0;
                if(count($arr_keyword)>0) {
                    $rank = $arr_keyword['rank'];
                }

                $req_data = array();
                $req_data['keyword']            = $val['keyword'];
                $req_data['search_count']       = $val['cnt'];
                $req_data['reg_id']             = 'system';
                $req_data['reg_date']           = $now_date;
                $req_data['update_id']          = 'system';
                $req_data['update_date']        = $now_date;
                $req_data['last_rank']          = $rank;
                $req_data['rank']               = $i;

                $this->m_realtime_keyword->realtime_keyword_insert($req_data);

                $i++;

            }

        }

        $this->m_realtime_keyword->realtime_keyword_where_reg_date_delete($date2);


        $contents.= '['.date("Y-m-d H:i:s").'] realtime_keyword_update end'.chr(10);
        $contents.= '-------------------------------------------------------------------------------------'.chr(10);
        write_file('./logs/cron01/'.date("Ym").'/'.date("Ymd").'.php', $contents, 'a+');





	}


	#---------------------------------------------------------------------------
    # PURPOSE :	오늘의 말씀, 영적기상도 노출
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function reservation_etc_display_update() {
	    if (!is_dir('./logs/cron01/'.date("Ym")))   mkdir('./logs/cron01/'.date("Ym"), 0777, TRUE);

        # file helper
        $this->load->helper('file');
        $contents = '['.date("Y-m-d H:i:s").'] reservation_etc_display_update start'.chr(10);



	    # 예약글 가져오기 - display = 'N' && display_date <= now()
	    $this->load->model('admin/etc_section/m_today_word');
        $this->m_today_word->today_word_reservation_update();

        # 예약글 가져오기 - display = 'N' && display_date <= now()
	    $this->load->model('admin/etc_section/m_weather');
        $this->m_weather->weather_reservation_update();

        $contents.= '['.date("Y-m-d H:i:s").'] reservation_etc_display_update end'.chr(10);
        $contents.= '-------------------------------------------------------------------------------------'.chr(10);
        write_file('./logs/cron01/'.date("Ym").'/'.date("Ymd").'.php', $contents, 'a+');



	}



    #---------------------------------------------------------------------------
    # PURPOSE :	인기수 업데이트 - 컨텐츠
    # CREATE  :	2018-03-27
    #---------------------------------------------------------------------------
    function article_popular_count_update() {

        if (!is_dir('./logs/cron01/'.date("Ym")))   mkdir('./logs/cron01/'.date("Ym"), 0777, TRUE);

        # file helper
        $this->load->helper('file');
        $contents = '['.date("Y-m-d H:i:s").'] article_popular_count_update start'.chr(10);


        $this->load->model('admin/news/m_news');
        $this->load->model('admin/life/m_life');
        $this->load->model('admin/column/m_column');

        $start_date     = date("Y-m-d H:i:s", strtotime ("-30 day"));
        $delete_date    = date("Y-m-d", strtotime ("-30 day"));

        $this->m_news->news_popular_count_where_display_date_update($delete_date);
        $this->m_life->life_popular_count_where_display_date_update($delete_date);
        $this->m_column->column_popular_count_where_display_date_update($delete_date);


        $arr_news    = $this->m_news->news_where_display_date_list($start_date);
        foreach ($arr_news as $val) {
            $hit_count = 0; $like_count = 0; $comment_count = 0;
            if(isset($val['hit_count'])==true)         $hit_count      = (int)$val['hit_count'];
            if(isset($val['like_count'])==true)        $like_count     = (int)$val['like_count'];
            if(isset($val['comment_count'])==true)     $comment_count  = (int)$val['comment_count'];

            $hit_count      = $hit_count * 3;
            $like_count     = $like_count * 200;
            $comment_count  = $comment_count * 400;

            $popular_count = $hit_count + $like_count + $comment_count;

            # 인기수 업데이트
            $req_data = array();
            $req_data['idx']            = $val['idx'];
            $req_data['popular_count']  = $popular_count;
            $this->m_news->news_update($req_data);

        }


        $arr_column    = $this->m_column->column_where_display_date_list($start_date);
        foreach ($arr_column as $val) {
            $hit_count = 0; $like_count = 0; $comment_count = 0;
            if(isset($val['hit_count'])==true)         $hit_count      = (int)$val['hit_count'];
            if(isset($val['like_count'])==true)        $like_count     = (int)$val['like_count'];
            if(isset($val['comment_count'])==true)     $comment_count  = (int)$val['comment_count'];

            $hit_count      = $hit_count * 3;
            $like_count     = $like_count * 200;
            $comment_count  = $comment_count * 400;

            $popular_count = $hit_count + $like_count + $comment_count;

            # 인기수 업데이트
            $req_data = array();
            $req_data['idx']            = $val['idx'];
            $req_data['popular_count']  = $popular_count;
            $this->m_column->column_update($req_data);

        }


        $arr_life    = $this->m_life->life_where_display_date_list($start_date);
        foreach ($arr_life as $val) {
            $hit_count = 0; $like_count = 0; $comment_count = 0;
            if(isset($val['hit_count'])==true)         $hit_count      = (int)$val['hit_count'];
            if(isset($val['like_count'])==true)        $like_count     = (int)$val['like_count'];
            if(isset($val['comment_count'])==true)     $comment_count  = (int)$val['comment_count'];

            $hit_count      = $hit_count * 3;
            $like_count     = $like_count * 200;
            $comment_count  = $comment_count * 400;

            $popular_count = $hit_count + $like_count + $comment_count;

            # 인기수 업데이트
            $req_data = array();
            $req_data['idx']            = $val['idx'];
            $req_data['popular_count']  = $popular_count;
            $this->m_life->life_update($req_data);

        }

        $contents.= '['.date("Y-m-d H:i:s").'] article_popular_count_update end'.chr(10);
        $contents.= '-------------------------------------------------------------------------------------'.chr(10);
        write_file('./logs/cron01/'.date("Ym").'/'.date("Ymd").'.php', $contents, 'a+');

	}


	#---------------------------------------------------------------------------
    # PURPOSE :	인기수 업데이트 - 채널, 에디터
    # CREATE  :	2018-03-27
    #---------------------------------------------------------------------------
    function life_popular_count_update() {

        if (!is_dir('./logs/cron01/'.date("Ym")))   mkdir('./logs/cron01/'.date("Ym"), 0777, TRUE);

        # file helper
        $this->load->helper('file');
        $contents = '['.date("Y-m-d H:i:s").'] life_popular_count_update start'.chr(10);

        $start_date = date("Y-m-d H:i:s", strtotime ("-30 day"));

        # 채널
        $this->load->model('admin/comm/m_delivery');
        $this->load->model('admin/life/m_life');
        $this->load->model('admin/life/m_life_channel');
        $this->load->model('admin/comm/m_delivery');
        $arr_channel    = $this->m_life_channel->channel_all_list();
        foreach ($arr_channel as $val) {
            # 배달수
            $req_data = array();
            $req_data['start_date']     = $start_date;
            $req_data['content_type']   = '3';
            $req_data['main_idx']       = $val['idx'];
            $delivery_count    = $this->m_delivery->delivery_where_content_type_content_idx_count($req_data);
            $delivery_count = $delivery_count * 700;

            # 조회수, 좋아요, 댓글
            $req_data = array();
            $req_data['start_date']     = $start_date;
            $req_data['channel_idx']    = $val['idx'];
            $arr_sum_count  = $this->m_life->life_where_channel_idx_sum_count($req_data);

            $hit_count = 0; $like_count = 0; $comment_count = 0;
            if(isset($arr_sum_count['hit_count'])==true)        $hit_count      = (int)$arr_sum_count['hit_count'];
            if(isset($arr_sum_count['like_count'])==true)       $like_count     = (int)$arr_sum_count['like_count'];
            if(isset($arr_sum_count['comment_count'])==true)    $comment_count  = (int)$arr_sum_count['comment_count'];

            $hit_count      = $hit_count * 3;
            $like_count     = $like_count * 200;
            $comment_count  = $comment_count * 400;

            $popular_count = $delivery_count + $hit_count + $like_count + $comment_count;

            # 인기수 업데이트
            $req_data = array();
            $req_data['idx']            = $val['idx'];
            $req_data['popular_count']  = $popular_count;
            $this->m_life_channel->channel_update($req_data);

        }

        # 에디터
        # model
        $this->load->model('admin/member/m_partner');
        $req_data = array();
        $req_data['member_group_idx']   = '6';
        $arr_editor    = $this->m_partner->partner_list_where_member_group_idx($req_data);
        foreach ($arr_editor as $val) {

            # 북마크
            $req_data = array();
            $req_data['content_type']   = '3';
            $req_data['start_date']     = $start_date;
            $req_data['partner_idx']    = $val['idx'];
            $bookmark_count    = $this->m_life->life_join_bookmark_editor_count($req_data);
            $bookmark_count = $bookmark_count * 700;

            # 조회수, 좋아요, 댓글
            $req_data = array();
            $req_data['start_date']     = $start_date;
            $req_data['partner_idx']    = $val['idx'];
            $arr_sum_count  = $this->m_life->life_where_partner_idx_sum_count($req_data);

            $hit_count = 0; $like_count = 0; $comment_count = 0;
            if(isset($arr_sum_count['hit_count'])==true)        $hit_count      = (int)$arr_sum_count['hit_count'];
            if(isset($arr_sum_count['like_count'])==true)       $like_count     = (int)$arr_sum_count['like_count'];
            if(isset($arr_sum_count['comment_count'])==true)    $comment_count  = (int)$arr_sum_count['comment_count'];


            $popular_count = $bookmark_count + $hit_count + $like_count + $comment_count;

            # 인기수 업데이트
            $req_data = array();
            $req_data['idx']            = $val['idx'];
            $req_data['popular_count']  = $popular_count;
            $this->m_partner->partner_update($req_data);

        }

        $contents.= '['.date("Y-m-d H:i:s").'] life_popular_count_update end'.chr(10);
        $contents.= '-------------------------------------------------------------------------------------'.chr(10);
        write_file('./logs/cron01/'.date("Ym").'/'.date("Ymd").'.php', $contents, 'a+');

	}

}

?>