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

class m_member_group extends CI_Model{

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

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


	#---------------------------------------------------------------------------
    # PURPOSE :	회원그룹 - list
    # CREATE  :	2018-02-20
    #---------------------------------------------------------------------------
	function member_group_list($req_data){
        # request
        $member_type    = $req_data['member_type'];
        $offset         = $req_data['offset'];
        $limit          = $req_data['limit'];

        $sql1 = "select * from member_group where is_del = 'N' and member_type = '".$member_type."' order by idx asc limit ".$offset.", ". $limit;
        $res1 = $this->db->query($sql1);
        $row1 = $res1->result_array();

        return $row1;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	회원그룹 - all list
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_group_all_list($req_data){
        # request
        $member_type    = $req_data['member_type'];

        $sql1 = "select * from member_group where is_del = 'N' and member_group_status = '1'  and member_type = '".$member_type."' order by idx asc";
        $res1 = $this->db->query($sql1);
        $row1 = $res1->result_array();

        return $row1;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	회원그룹 - insert
    # CREATE  :	2018-02-20
    #---------------------------------------------------------------------------
	function member_group_insert($req_data){
	    # request
        $member_type            = $req_data['member_type'];
        $member_group_name      = $req_data['member_group_name'];

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

        $insert_data = array('member_type' => $member_type,
                             'member_group_name' => $member_group_name,
                             'reg_id' => $user_id,
                             'reg_date' => $now_date,
                             'update_id' => $user_id,
                             'update_date' => $now_date
        );
        $row = $this->db->insert('member_group', $insert_data);
        $last_idx = $this->db->insert_id();

        if(!$row) {
            return 0;
        } else {
            return $last_idx;
        }

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	회원그룹 - update
    # CREATE  :	2018-02-20
    #---------------------------------------------------------------------------
	function member_group_update($req_data){
	    # request
        $idx                    = $req_data['idx'];
        $member_group_name      = $req_data['member_group_name'];

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

        $update_data = array('member_group_name' => $member_group_name,
                             'update_id' => $user_id,
                             'update_date' => $now_date
        );
        $this->db->where('idx', $idx);
        $row = $this->db->update('member_group', $update_data);

        if(!$row) {
            return '9999';
        } else {
            return '0000';
        }

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	회원그룹 - member_group.member_group_status update
    # CREATE  :	2018-02-20
    #---------------------------------------------------------------------------
	function member_group_status_update($req_data){
        $user_id    = $this->session->userdata('Adm_Id');
        $now_date   = date("Y-m-d H:i:s");

        $arr_update_data = array();
        foreach($req_data as $key=>$val) {
            $arr_update_data[$key]['idx']                   = $val;
            $arr_update_data[$key]['member_group_status']   = '2';
            $arr_update_data[$key]['update_id']             = $user_id;
            $arr_update_data[$key]['update_date']           = $now_date;
        }
        $this->db->update_batch('member_group', $arr_update_data, 'idx');

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	회원그룹 - member_group.is_del update
    # CREATE  :	2018-02-20
    #---------------------------------------------------------------------------
	function member_group_is_del_update($req_data){
        $user_id    = $this->session->userdata('Adm_Id');
        $now_date   = date("Y-m-d H:i:s");

        $arr_update_data = array();
        foreach($req_data as $key=>$val) {
            $arr_update_data[$key]['idx']           = $val;
            $arr_update_data[$key]['is_del']        = 'Y';
            $arr_update_data[$key]['update_id']     = $user_id;
            $arr_update_data[$key]['update_date']   = $now_date;
        }
        $this->db->update_batch('member_group', $arr_update_data, 'idx');

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	회원그룹 - total count
    # CREATE  :	2018-02-20
    #---------------------------------------------------------------------------
	function member_group_total_count($req_data){
        # request
        $member_type    = $req_data['member_type'];

        $this->db->where('member_type', $member_type);
        $this->db->where('is_del', 'N');
        $this->db->from('member_group');
        $row1 = $this->db->count_all_results();

        return $row1;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	member_group join member_group_menu_auth select
    # CREATE  :	2018-02-20
    #---------------------------------------------------------------------------
	function member_group_join_member_group_menu_auth_select($member_group_idx){
	    
        $sql1 = "select a.*, b.member_group_name 
                   from member_group_menu_auth as a 
                   join member_group as b on (a.member_group_idx = b.idx)
                  where a.member_group_idx = '".$member_group_idx."' order by a.idx asc";
        $res1 = $this->db->query($sql1);
        $row1 = $res1->result_array();

        return $row1;

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	member_group join member_group_menu_auth select
    # CREATE  :	2018-02-20
    #---------------------------------------------------------------------------
	function member_group_join_member_group_menu_auth_guest_select($req_data){
	    # request
        $member_type    = $req_data['member_type'];

        $sql1 = "select a.*, b.member_group_name 
                   from member_group_menu_auth as a 
                   join member_group as b on (a.member_group_idx = b.idx)
                  where b.member_type = ".$member_type." order by a.idx asc";
        $res1 = $this->db->query($sql1);
        $row1 = $res1->result_array();

        return $row1;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	member_group_menu_auth insert
    # CREATE  :	2018-02-20
    #---------------------------------------------------------------------------
	function member_group_menu_auth_insert($req_data){
	    # request
        $member_group_idx       = $req_data['member_group_idx'];

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

        $insert_data = array();
        foreach($req_data['menu_list'] as $key=>$val) {
            $insert_data[$key]['member_group_idx']  = $member_group_idx;
            $insert_data[$key]['menu_code']         = $val['menu_code'];
            $insert_data[$key]['menu_name']         = $val['menu_name'];
            $insert_data[$key]['menu_auth_status']  = $val['menu_auth_status'];
            $insert_data[$key]['reg_id']            = $user_id;
            $insert_data[$key]['reg_date']          = $now_date;
            $insert_data[$key]['update_id']         = $user_id;
            $insert_data[$key]['update_date']       = $now_date;

        }

        $this->db->insert_batch('member_group_menu_auth', $insert_data);
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	회원그룹 - 메뉴권한 - member_group_menu_auth.menu_auth_status update (member_group_idx)
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_group_menu_auth_menu_auth_status_reset($req_data){
        # request
        $member_group_idx       = $req_data;

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

        $update_data = array('menu_auth_status' => '1',
                             'update_id'        => $user_id,
                             'update_date'      => $now_date
        );

        $this->db->where('member_group_idx', $member_group_idx);
        $row = $this->db->update('member_group_menu_auth', $update_data);

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	회원그룹 - 메뉴권한 - member_group_menu_auth.menu_auth_status update
    # CREATE  :	2018-02-21
    #---------------------------------------------------------------------------
	function member_group_menu_auth_menu_auth_status_update($req_data){

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

        $arr_update_data = array();
        foreach($req_data as $key=>$val) {
            $arr_update_data[$key]['idx']                   = $val;
            $arr_update_data[$key]['menu_auth_status']      = '2';
            $arr_update_data[$key]['update_id']             = $user_id;
            $arr_update_data[$key]['update_date']           = $now_date;
        }


        $result = $this->db->update_batch('member_group_menu_auth', $arr_update_data, 'idx');

        return $result;
    }







}
?>
