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

class cron_ep_view_ranking extends CI_Controller {
    #---------------------------------------------------------------------------
    # PURPOSE :	실시간 맵툰 에피소드 조회 순위 등록
    # CREATE  :	2019-03-26
    # 실행주기 : 1시간
    #---------------------------------------------------------------------------
    function maptoon_view_rank_update() {
        if (!is_dir('./logs/cron_mt_ep_ranking/'.date("Ym")))   mkdir('./logs/cron_mt_ep_ranking/'.date("Ym"), 0777, TRUE); //log file 생성
        $contents = 'log description';
        # file helper
        $this->load->helper('file');
        $contents = '['.date("Y-m-d H:i:s").'] mt_ep_ranking_work 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;}
            }
        };

        print_r($row);

        # insert_data;
        $this->db->insert_batch("mt_ep_view_rank",$insert_date);

        $contents.= date("Y-m-d H:i:s",strtotime ("-$_time hours")).' 업데이트 완료'.chr(10);
        $contents.= '['.date("Y-m-d H:i:s").'] episode veiw count ranking end'.chr(10);
        $contents.= '-------------------------------------------------------------------------------------'.chr(10);
        write_file('./logs/cron_mt_ep_ranking/'.date("Ym").'/'.date("Ymd").'.php', $contents, 'a+');

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	예약 조회 후 업데이트
    # CREATE  :	2019-04-09
    # 실행주기 : 매분
    #---------------------------------------------------------------------------
    function epsiode_reservation(){
        if (!is_dir('./logs/cron_maptoon/'.date("Ym")))   mkdir('./logs/cron_maptoon/'.date("Ym"), 0777, TRUE); //log file 생성
        $contents = 'log description';
        # file helper
        $this->load->helper('file');
        $contents = '['.date("Y-m-d H:i:s").'] reservation_article_display_update start'.chr(10);

        $this->load->model('maptoon/m_maptoon');
        $result = $this->m_maptoon->get_epsiodes_reservation_list();
        if(count($result)>0){
            $num =$this->m_maptoon->episodes_is_display_update_batch($result);
        }
        
        $num>0?$contents.= $content_type.$num.'개 업데이트-----------------------------------------------------------------------------'.chr(10):$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/cron_maptoon/'.date("Ym").'/'.date("Ymd").'.php', $contents, 'a+');

    }
}

?>