<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
 * 컨텐츠관련 스케쥴
 */
class cron_test extends CI_Controller {
    #---------------------------------------------------------------------------
    # PURPOSE :	실시간 조회 순위 등록
    # CREATE  :	2019-03-26
    #---------------------------------------------------------------------------
    // function maptoon_view_rank_update() {
    function index() {

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


        # model -create rank array
        $this->load->database();
        $num=0;
        $_time=1;
        $result;
        while($num==0){
            $result=$this->db->query("SELECT * FROM mt_ep_hit_log WHERE reg_date>=\"".date("Y-m-d H:i:s",strtotime ("-$_time hours"))."\" Order by ep_idx");
            $num=$result->num_rows();
            $_time++;
        }

        $row=$result->result_array();
        
        $rank_list=array();
        $y=0;
        $ep_idx=0;
        foreach($row as $val){
            if($ep_idx!=$val['ep_idx']){
                $ep_idx=$val['ep_idx'];
                $y=0;
            }
            array_push($rank_list,$ep_idx);
        }
        $rank_list=array_count_values($rank_list);
        arsort($rank_list); //ep_idx view count sort

        $ep_idx_rank_list=array_keys($rank_list); // only ep_idx array;

        $insert_date=array();
        $rank=1;//순위
        foreach($ep_idx_rank_list as $val){
            array_push($insert_date,array('ep_idx'=>$val,'rank'=>$rank));
            $rank++;
        }

        if(count($ep_idx_rank_list)<10){
            $result=$this->db->query("SELECT * FROM mt_episodes ORDER BY reg_date DESC limit 10");
            $row = $result->result_array();
            foreach($row as $val){
                $is_same=false;
                foreach($ep_idx_rank_list as $val2){
                    if($val['idx']==$val2){
                        $is_same=true;
                    }
                }
                if(!$is_same){ array_push($insert_date,array('ep_idx'=>$val['idx'],'rank'=>$rank)); $rank++;}
                if(count($insert_date)==10){break;}
            }
        };


        # insert_data;
        $this->db->insert_batch("mt_ep_view_rank",$insert_date);
        
        
/*
        # 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+');
*/
	}
}

?>