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

class m_main_category extends CI_Model{

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

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


	#---------------------------------------------------------------------------
    # PURPOSE :	main 카테고리 - list
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function main_category_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_category_code1  = $req_data['search_category_code1'];
        $search_category_code2  = $req_data['search_category_code2'];

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


        if($search_category_code2!='') {
            $this->db->where('category_code', $search_category_code2);
            $this->db->where('p_category_code', $search_category_code1);
        }


        $this->db->where('is_del', 'N');
        $this->db->from('main_category');
        $this->db->order_by('p_category_code asc, depth asc, category_code asc');
        $this->db->limit($limit, $offset);
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	main 카테고리 - list용 - total count
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function main_category_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_category_code1  = $req_data['search_category_code1'];


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

        $this->db->where('is_del', 'N');
        $this->db->from('main_category');
        $row = $this->db->count_all_results();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	main 카테고리 - insert
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function main_category_insert($req_data){
	    # request
	    $category_name          = $req_data['category_name'];
        $category_img           = $req_data['category_img'];
        $depth                  = $req_data['depth'];
        $p_category_code        = $req_data['p_category_code'];

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


        $this->db->select('max(category_code) + 1 as category_code');
	    $this->db->where('depth', $depth);
        if($p_category_code!='')    $this->db->where('p_category_code', $p_category_code);
        $this->db->from('main_category');
        $res1 = $this->db->get();
        $row1 = $res1->result_array();

        $category_code = $row1[0]['category_code'];
        if($depth=='1') {
            if($category_code == '')    $category_code = '10';
            $p_category_code = $category_code;
        } else {
            if($category_code == '')    $category_code = '01';
        }
        $category_code = sprintf('%02d',$category_code);

        $insert_data['category_code']   = $category_code;
        $insert_data['category_name']   = $category_name;
        $insert_data['category_img']    = $category_img;
        $insert_data['depth']           = $depth;
        $insert_data['p_category_code'] = $p_category_code;
        $insert_data['reg_id']          = $user_id;
        $insert_data['reg_date']        = $now_date;
        $insert_data['update_id']       = $user_id;
        $insert_data['update_date']     = $now_date;

        $row = $this->db->insert('main_category', $insert_data);

        return $row;

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	main 카테고리  - update
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function main_category_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('main_category', $req_data);

        return $row;

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	main 카테고리 - all list
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function main_category_all_list($req_data){
	    # request
	    $depth    = $req_data;

        if($depth!='')  $this->db->where('depth', $depth);
        
        $this->db->where('is_del', 'N');
        $this->db->from('main_category');
        $this->db->order_by('category_code asc');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	main 카테고리  select in idx
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function main_category_in_p_category_code($req_data){
	    # request
	    $p_category_codes    = $req_data;

        $this->db->where_in('p_category_code', $p_category_codes);
        $this->db->where('is_del', 'N');
        $this->db->from('main_category');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE : main 카테고리 is_del where p_category_code update
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function main_category_is_del_where_p_category_code_update($req_data){

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

        $update_data['is_del']      = 'Y';
        $update_data['update_id']   = $user_id;
        $update_data['update_date'] = $now_date;


        $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]['p_category_code']  = $val;
            $arr_update_data[$key]['is_del']            = 'Y';
            $arr_update_data[$key]['update_id']         = $user_id;
            $arr_update_data[$key]['update_date']       = $now_date;
        }
        $row = $this->db->update_batch('main_category', $arr_update_data, 'p_category_code');
        return $row;

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	main 카테고리 - select
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function main_category_select($req_data){
	    # request
        $p_category_code    = $req_data['p_category_code'];
        $idx                = $req_data['idx'];

        if($p_category_code!='')    $this->db->where('p_category_code', $p_category_code);
        if($idx!='')                $this->db->where('idx', $idx);

        $this->db->where('is_del', 'N');
        $this->db->from('main_category');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE : main 카테고리 is_del update -  사용안함.나중에도 안쓰면 지우기
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function main_category_is_del_update($req_data){
	    # request
	    $idx            = $req_data['idx'];
        $p_category_code= $req_data['p_category_code'];

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

        $update_data['is_del']      = 'Y';
        $update_data['update_id']   = $user_id;
        $update_data['update_date'] = $now_date;

        if($idx!='')                $this->db->where('idx', $idx);
        if($p_category_code!='')    $this->db->where('p_category_code', $p_category_code);
        $row = $this->db->update('main_category', $update_data);

        return $row;


    }


    #---------------------------------------------------------------------------
    # PURPOSE :	main 카테고리  category_code count
    # CREATE  :	2018-03-02
    #---------------------------------------------------------------------------
	function main_category_code_count($req_data){
	    # request
	    $depth                  = $req_data['depth'];
        $category_code          = $req_data['category_code'];

        if($depth == '1') {
            $this->db->where('category_code1', $category_code);
        } else {
            $this->db->where('category_code2', $category_code);
        }
        $this->db->where('is_del', 'N');
        $this->db->from('main_category_company');
        $row = $this->db->count_all_results();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	main 카테고리 업체 - list
    # CREATE  :	2018-03-03
    #---------------------------------------------------------------------------
	function main_category_company_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_category_code1  = $req_data['search_category_code1'];
        $search_category_code2  = $req_data['search_category_code2'];

        if($search_type!='' && $search_keyword!=''){
            $this->db->like($search_type, $search_keyword);
        }
        if($search_category_code1!='') {
            $this->db->where('a.category_code1', $search_category_code1);
            if($search_category_code2!='')  $this->db->where('a.category_code2', $search_category_code2);

        }


        $this->db->select('a.*, b.category_name as category_name1, c.category_name as category_name2');
        $this->db->where('a.is_del', 'N');
        $this->db->where('b.depth', '1');
        $this->db->where('c.depth', '2');
        $this->db->where('c.p_category_code = b.category_code');
        $this->db->from('main_category_company as a');
        $this->db->join('main_category as b', 'a.category_code1=b.category_code');
        $this->db->join('main_category as c', 'a.category_code2=c.category_code');
        $this->db->order_by('a.idx desc');
        $this->db->limit($limit, $offset);

        //echo $this->db->get_compiled_select();

        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE :	main 카테고리 업체- list용 - total count
    # CREATE  :	2018-03-03
    #---------------------------------------------------------------------------
	function main_category_company_list_total_count($req_data){
	    # request
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];
        $search_category_code1  = $req_data['search_category_code1'];
        $search_category_code2  = $req_data['search_category_code2'];

        if($search_type!='' && $search_keyword!=''){
            $this->db->like($search_type, $search_keyword);
        }
        if($search_category_code1!='') {
            $this->db->where('a.category_code1', $search_category_code1);
            if($search_category_code2!='')  $this->db->where('a.category_code2', $search_category_code2);

        }

        $this->db->where('a.is_del', 'N');
        $this->db->where('b.depth', '1');
        $this->db->where('c.depth', '2');
        $this->db->where('c.p_category_code = b.category_code');
        $this->db->from('main_category_company as a');
        $this->db->join('main_category as b', 'a.category_code1=b.category_code');
        $this->db->join('main_category as c', 'a.category_code2=c.category_code');
        $row = $this->db->count_all_results();

        return $row;
    }





    #---------------------------------------------------------------------------
    # PURPOSE :	main 카테고리 업체 - insert
    # CREATE  :	2018-03-03
    #---------------------------------------------------------------------------
	function main_category_company_insert($req_data){

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

        $req_data['reg_id']     = $user_id;
        $req_data['reg_date']   = $now_date;
        $req_data['update_id']  = $user_id;
        $req_data['update_date']= $now_date;
        $row = $this->db->insert('main_category_company', $req_data);

        return $row;

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	main 카테고리 업체 - select
    # CREATE  :	2018-03-03
    #---------------------------------------------------------------------------
	function main_category_company_select($req_data){
	    # request
	    $idx            = $req_data;

        $this->db->where('idx', $idx);
        $this->db->where('is_del', 'N');
        $this->db->from('main_category_company');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	main 카테고리 업체 - update
    # CREATE  :	2018-03-03
    #---------------------------------------------------------------------------
	function main_category_company_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('main_category_company', $req_data);

        return $row;

    }

    #---------------------------------------------------------------------------
    # PURPOSE : main 카테고리 업체 is_del update
    # CREATE  : 2018-03-03
    #---------------------------------------------------------------------------
	function main_category_company_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('main_category_company', $arr_update_data, 'idx');

    }


    #---------------------------------------------------------------------------
    # PURPOSE : main 카테고리 업체 - select in idx
    # CREATE  :	2018-03-03
    #---------------------------------------------------------------------------
	function main_category_company_select_in_idx($req_data){
	    # request
	    $arr_idx    = $req_data;

        $this->db->where_in('idx', $arr_idx);
        $this->db->where('is_del', 'N');
        $this->db->from('main_category_company');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }


}
?>
