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

class m_member extends CI_Model{

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

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

	#---------------------------------------------------------------------------
    # PURPOSE :	일반회원 - list
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_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_member_group    = $req_data['search_member_group'];
        $search_member_gender   = $req_data['search_member_gender'];
        $search_member_age      = $req_data['search_member_age'];
        $search_member_new      = $req_data['search_member_new'];

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

        if($search_member_group!=''){
            $where_query.= 'and a.member_group_idx="'.$search_member_group.'"';
        }

        if($search_member_gender!=''){
            $where_query.= 'and a.member_gender="'.$search_member_gender.'"';
        }

        if($search_member_age!=''){
            $where_query.= 'and a.member_age="'.$search_member_age.'"';
        }

        if($search_member_new!=''){
            $now_date   = date("Y-m-d").' 00:00:00';
            $where_query.= 'and a.reg_Date >= "'.$now_date.'"';
        }

        $sql = "select a.*, b.member_group_name
                  from member as a 
                  join member_group as b on (a.member_group_idx = b.idx)
                 where a.member_status = '1' ".$where_query." order by a.idx desc limit ".$offset.", ". $limit;
        $res = $this->db->query($sql);
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	회원 - list용 - total count
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_list_total_count($req_data){
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_member_group    = $req_data['search_member_group'];
        $search_member_gender   = $req_data['search_member_gender'];
        $search_member_age      = $req_data['search_member_age'];
        $search_member_new      = $req_data['search_member_new'];


        if($search_type!='' && $search_keyword!=''){
            $this->db->like($search_type, $search_keyword);
        }

        if($search_member_group!='') {
            $this->db->where('member_group_idx', $search_member_group);
        }

        if($search_member_gender!=''){
            $this->db->where('member_gender', $search_member_gender);
        }

        if($search_member_age!=''){
            $this->db->where('member_age', $search_member_age);
        }

        if($search_member_new!=''){
            $now_date   = date("Y-m-d").' 00:00:00';
	        $this->db->where('reg_date >=', $now_date);
        }

        $this->db->where('member_status', '1');
        $this->db->from('member');
        $row = $this->db->count_all_results();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	회원 - total count
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_total_count(){
        $this->db->where('member_status', '1');
        $this->db->from('member');
        $row = $this->db->count_all_results();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	회원 - 성별 count
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_group_by_member_gender(){
	    $this->db->select('member_gender, count(member_gender) as cnt');
	    $this->db->where('member_status', '1');
	    $this->db->group_by("member_gender");
        $this->db->from('member');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	회원 - 그룹 count
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_group_by_member_group(){
	    $this->db->select('member_group_idx, count(member_group_idx) as cnt');
	    $this->db->where('member_status', '1');
	    $this->db->group_by("member_group_idx");
        $this->db->from('member');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	회원 - 신규회원
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_count_member_new(){
	    $now_date   = date("Y-m-d").' 00:00:00';
	    $this->db->where('reg_date >=', $now_date);
        $this->db->where('member_status', '1');
        $this->db->from('member');
        $row = $this->db->count_all_results();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	회원 - 회원 나이 대
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_group_by_member_age(){
	    $this->db->select('member_age, count(member_age) as cnt');
	    $this->db->where('member_status', '1');
        $this->db->group_by("member_age");
        $this->db->from('member');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	일반회원 - select
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_select($req_data){
	    # request
	    $member_idx = $req_data;

	    $this->db->where('idx', $member_idx);
        $this->db->from('member');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	일반회원 - update
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_update($req_data){
	    # request
        $idx                = $req_data['idx'];
        $member_email       = $req_data['member_email'];
        $member_birth       = $req_data['member_birth'];
        $member_age         = $req_data['member_age'];
        $member_calendar    = $req_data['member_calendar'];
        $member_gender      = $req_data['member_gender'];
        $member_group_idx   = $req_data['member_group_idx'];
        $member_church      = $req_data['member_church'];
        $member_dong        = $req_data['member_dong'];
        $member_pastor      = $req_data['member_pastor'];
        $is_agree_sms       = $req_data['is_agree_sms'];
        $is_agree_mail      = $req_data['is_agree_mail'];
        $is_agree_info_open = $req_data['is_agree_info_open'];
        $member_img         = $req_data['member_img'];

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


        $update_data['member_email']      = $member_email;
        $update_data['member_birth']      = $member_birth;
        $update_data['member_age']        = $member_age;
        $update_data['member_calendar']   = $member_calendar;
        $update_data['member_gender']     = $member_gender;
        $update_data['member_group_idx']  = $member_group_idx;
        $update_data['member_church']     = $member_church;
        $update_data['member_dong']       = $member_dong;
        $update_data['member_pastor']     = $member_pastor;
        $update_data['is_agree_sms']      = $is_agree_sms;
        $update_data['is_agree_mail']     = $is_agree_mail;
        $update_data['is_agree_info_open']= $is_agree_info_open;
        $update_data['member_img']        = $member_img;
        $update_data['update_id']         = $user_id;
        $update_data['update_date']       = $now_date;

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

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	일반회원 - member_status.update
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_member_status_update($req_data){
	    # request
        $idx                = $req_data;

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

        $update_data = array('member_status' => '2',
                             'member_group_idx'=>'5',
                             'update_id' => $user_id,
                             'update_date' => $now_date
        );
        $this->db->where('idx', $idx);
        $row = $this->db->update('member', $update_data);

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	일반회원 - select
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_where_member_id_select($member_id){


	    $this->db->where('member_id', $member_id);
        $this->db->from('member');
        $res = $this->db->get();
        $row = $res->row_array();

        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	일반회원 - where_member_id
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_where_member_id_update($req_data){
        # request
	    $member_id            = $req_data['member_id'];
        unset($req_data['member_id']);

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

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

        $this->db->where('member_id', $member_id);
        $row = $this->db->update('member', $req_data);

        return $row;

    }

    

}
?>
