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

class m_blind extends CI_Model{

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

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

	#---------------------------------------------------------------------------
    # PURPOSE :	list
    # CREATE  :	2018-03-28
    #---------------------------------------------------------------------------
	function blind_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_content_type    = $req_data['search_content_type'];
        $search_spam_reason     = $req_data['search_spam_reason'];
        $search_status          = $req_data['search_status'];

        $where_query = "";
        if($search_type!='' && $search_keyword!=''){
            $where_query.=" and AA.".$search_type." like '%".$search_keyword."%'";
        }
        if($search_content_type!='')        $where_query.=" and AA.content_type = '".$search_content_type."'";
        if($search_spam_reason!='')         $where_query.=" and AA.reason_type like '%".$search_spam_reason."%'";
        if($search_status!='')              $where_query.=" and AA.blind_status = '".$search_status."'";

        $sql="select *, (select count(*) from blind where is_del='N' and blind_id=AA.blind_id and blind_status = '2') as admin_spam_count, (select count(*) from spam where comment_idx=AA.comment_idx) as spam_count
                from (select *, (select GROUP_CONCAT(DISTINCT reason_type ORDER BY reason_type DESC SEPARATOR ',') from spam where is_del='N' and comment_idx=blind.comment_idx) as reason_type
                        from blind 
                       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-28
    #---------------------------------------------------------------------------
	function blind_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_content_type    = $req_data['search_content_type'];
        $search_spam_reason     = $req_data['search_spam_reason'];
        $search_status          = $req_data['search_status'];


        $where_query = "";
        if($search_type!='' && $search_keyword!=''){
            $where_query.=" and AA.".$search_type." like '%".$search_keyword."%'";
        }
        if($search_content_type!='')        $where_query.=" and AA.content_type = '".$search_content_type."'";
        if($search_spam_reason!='')         $where_query.=" and AA.reason_type like '%".$search_spam_reason."%'";
        if($search_status!='')              $where_query.=" and AA.blind_status = '".$search_status."'";

        $sql="select * 
                from (select *, 
                            (select GROUP_CONCAT(DISTINCT reason_type ORDER BY reason_type DESC SEPARATOR ',') from spam where is_del='N' and comment_idx=blind.comment_idx) as reason_type 
                        from blind 
                       where is_del = 'N') as AA where 1 ".$where_query;
        $res = $this->db->query($sql);
        $row = $res->num_rows();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	update
    # CREATE  :	2018-03-28
    #---------------------------------------------------------------------------
	function blind_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('blind', $req_data);

        return $row;

    }


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

        $this->db->select('a.*, b.reg_date as write_date');
        $this->db->where('a.idx', $idx);
        $this->db->where('a.is_del', 'N');
        $this->db->from('blind as a');
        $this->db->join('comment as b', 'a.comment_idx=b.idx');
        $res = $this->db->get();
        $row = $res->result_array();


        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE : status group by count
    # CREATE  :	2018-03-28
    #---------------------------------------------------------------------------
	function blind_list_status_group_by_count(){

        $this->db->select('blind_status, count(blind_status) as cnt');
	    $this->db->where('is_del', 'N');
	    $this->db->group_by("blind_status");
        $this->db->from('blind');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE : comment status count
    # CREATE  :	2018-04-13
    #---------------------------------------------------------------------------
	function blind_list_join_comment_status_count($comment_status){

	    $this->db->where('a.is_del', 'N');
	    $this->db->where('b.comment_status', $comment_status);
        $this->db->from('blind as a');
        $this->db->join('comment as b', 'a.comment_idx=b.idx');
        $res = $this->db->get();
        $row = $res->num_rows();

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	랭킹 insert
    # CREATE  :	2018-03-06
    #---------------------------------------------------------------------------
	function blind_insert($req_data){

        $row = $this->db->insert('blind', $req_data);

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE : select
    # CREATE  :	2018-04-27
    #---------------------------------------------------------------------------
	function blind_select($idx){

	    # request
        $this->db->where('idx', $idx);
        $this->db->from('blind');
        $res = $this->db->get();
        $row = $res->row_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE : select
    # CREATE  :	2018-04-27
    #---------------------------------------------------------------------------
	function blind_group_by_count_list($req_data){
	    # request
	    $cnt                = $req_data['cnt'];

        $sql="select blind_id, count(*) as cnt
                from blind 
               where blind_status = '2'
                 and limit_count < '".$cnt."'
                 and is_del = 'N'
               group by blind_id having count(*) >= ".$cnt;
        $res = $this->db->query($sql);
        $row = $res->result_array();

        return $row;

    }

    #---------------------------------------------------------------------------
    # PURPOSE : update
    # CREATE  :	2018-04-27
    #---------------------------------------------------------------------------
	function blind_group_by_count_update($req_data){
	    # request
	    $member_id          = $req_data['member_id'];
	    $limit_count        = $req_data['cnt'];

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

        $update_date['update_id']   = $user_id;
        $update_date['update_date'] = $now_date;
        $update_date['limit_count'] = $limit_count;

        $this->db->where('blind_id', $member_id);
        $row = $this->db->update('blind', $update_date);

        return $row;

    }





}
?>
