<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of User_model
 *
 * @author zmq163
 */
class User_model extends CI_Model {

    //put your code here
    private $mdb;

    public function __construct() {
        parent::__construct();
        $this->mdb = $this->load->database('mdb', TRUE);
    }
/**
 * 
 * 
 * 
 * 
 * 
 */
public function getUserDetailRightModel($_app_id=NULL,$_level=NULL,$_super_user=NULL){
    if($_app_id!=NULL and $_level!=NULL and $_super_user!=NULL){
        $this->mdb->select('*');
        $this->mdb->from('master_user_details');
        $this->mdb->join('master_login_user','master_login_user.user_login_id=master_user_details.user_login_id','INNER');
        
        $this->mdb->join('master_area_create_by_super','master_login_user.create_region_id=master_area_create_by_super.region_code','INNER');
        $this->mdb->where(array('master_login_user.user_activation_status'=>1,'master_user_details.app_id'=>$_app_id,'master_user_details.super_user_mapped_region_code'=>$_super_user,'master_user_details.user_level'=>$_level));
        return $this->getData($this->mdb->get());
    }  else {
        return FALSE;    
    }
}

/**
     * 
     * 
     * 
     * 
     */
    public function updateLoginStatusModel($_auto_id = NULL, $_region_id = NULL,$_status=NULL,$_app_id=NULL) {
        if ($_auto_id != NULL and $_region_id != NULL and $_status!=NULL) {
            $this->mdb->trans_begin();
            $this->loginTable($_auto_id,array('user_activation_status'=>$_status));
            if($_status){
                $this->updateUserHistoryModel(array('activation_date'=>NULL,'current_status'=>0,'app_id'=>$_app_id,'region_code'=>$_region_id), array('current_status'=>1,'activation_date'=>date('Y-m-d H:i:s')));
                $this->mappingAreaUserTable($_region_id,array('activate_date'=>  date('Y-m-d')));
            }  else {
               $this->mappingAreaUserTable($_region_id,array('activate_date'=>NULL)); 
            }
            
            if ($this->mdb->trans_status() === FALSE) {
                $this->mdb->trans_rollback();
                return FALSE;
            } else {
                $this->mdb->trans_commit();
                return TRUE;
            }
        } else {
            return FALSE;
        }
    }
    /**
     * 
     * 
     * 
     * 
     */
    public function updateUserHistoryModel($_conditio_array=NULL ,$_update_data=NULL){
        if($_conditio_array!=NULL and $_update_data!=NULL){
            $this->mdb->where($_conditio_array);
            $this->mdb->update('user_activation_history',$_update_data);
        }  else {
            return FALSE;    
        }
        
    }

    /**
     * 
     * 
     * 
     */
    public function loginTable($_auto_id=NULL,$data=NULL){
        $this->mdb->where('auto_id', $_auto_id);
        $this->mdb->update('master_login_user', $data);
    }
    /**
     * 
     * 
     * 
     */
    /**
     * 
     * 
     * 
     */
    public function mappingAreaUserTable($_region_id=NULL,$data=NULL){
        $this->mdb->where('region_code', $_region_id);
        $this->mdb->update('mapping_area_to_user', $data);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getMappedUserRegionListModel($_app_id = NULL, $_level = NULL, $_region_id = NULL) {
        if ($_app_id != NULL and $_level != NULL and $_region_id != NULL) {
            $this->mdb->select('master_user_details.*,master_area_create_by_super.region_name,mapping_area_to_user.activate_date');
            $this->mdb->from('master_user_details');
            $this->mdb->join('mapping_area_to_user', 'master_user_details.auto_id=mapping_area_to_user.user_id', 'INNER');
            $this->mdb->join('master_area_create_by_super', 'master_area_create_by_super.region_code=mapping_area_to_user.region_code', 'INNER');
            $this->mdb->where(array('master_user_details.app_id' => $_app_id, 'master_user_details.user_level' => $_level, 'master_user_details.user_mapping_status' => 1, 'master_user_details.super_user_mapped_region_code' => $_region_id, 'master_user_details.delete_status' => 0));
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getMappedUserRegionListUpdateModel($_app_id = NULL, $_region_id = NULL,$_level=NULL) {
        if ($_app_id != NULL and $_region_id != NULL and $_level!=NULL) {
            $this->mdb->select('master_user_details.*,master_area_create_by_super.region_name,mapping_area_to_user.activate_date,master_login_user.auto_id,master_login_user.user_activation_status,mapping_area_to_user.region_code');
            $this->mdb->from('master_user_details');
            $this->mdb->join('mapping_area_to_user', 'master_user_details.auto_id=mapping_area_to_user.user_id', 'INNER');
            $this->mdb->join('master_area_create_by_super', 'master_area_create_by_super.region_code=mapping_area_to_user.region_code', 'INNER');
            $this->mdb->join('master_login_user', 'master_login_user.create_region_id=master_area_create_by_super.region_code', 'INNER');
            $this->mdb->where(array('master_user_details.app_id' => $_app_id, 'master_user_details.user_mapping_status' => 1, 'master_user_details.super_user_mapped_region_code' => $_region_id, 'master_user_details.delete_status' => 0, 'master_login_user.app_id' => $_app_id,'master_login_user.user_level >'=>$_level));
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }
    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getMappedUserRegionListForUnmappedModel($_app_id = NULL, $_region_id = NULL) {
        if ($_app_id != NULL and $_region_id != NULL ) {
            $this->mdb->select('*');
            $this->mdb->from('mapping_area_to_user');
            
            $this->mdb->join('master_user_details', 'master_user_details.auto_id=mapping_area_to_user.user_id', 'INNER');
            
            
            $this->mdb->join('master_area_create_by_super', 'master_area_create_by_super.region_code=mapping_area_to_user.region_code', 'INNER');
            
            $this->mdb->where(array('master_user_details.app_id' => $_app_id, 'mapping_area_to_user.mapping_status' => 1, 'master_user_details.super_user_mapped_region_code' => $_region_id, 'master_user_details.delete_status' => 0));
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }
    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function updateUserRegionUnmappedModel($_region_code = NULL, $_auto_id = NULL,$_app_id=NULL) {
        if ($_region_code != NULL and $_auto_id != NULL and $_app_id!=NULL) {
             $this->mdb->trans_begin();
             
             $this->mdb->where('region_code',$_region_code);
             $this->mdb->update('master_area_create_by_super',array('mapping_status'=>0));
            
             $this->mdb->where('auto_id',$_auto_id);
             $this->mdb->update('master_user_details',array('user_mapping_status'=>0));
             
             $this->mdb->where(array('current_status'=>1,'region_code'=>$_region_code,'user_id'=>$_auto_id,'app_id'=>$_app_id));
             $this->mdb->update('user_activation_history',array('deactivation_date'=>date('Y-m-d h:i:s'),'current_status'=>0));
             
             $this->mdb->where(array('create_region_id'=>$_region_code));
             $this->mdb->delete('master_login_user');
             
             $this->mdb->where(array('region_code'=>$_region_code,'user_id'=>$_auto_id));
             $this->mdb->delete('mapping_area_to_user');
             
             $this->mdb->where(array('region_code'=>$_region_code));
             $this->mdb->delete('mapping_area_rights_to_apps_right');
             
          
            if ($this->mdb->trans_status() === FALSE) {
                $this->mdb->trans_rollback();
                return FALSE;
            } else {
                $this->mdb->trans_commit();
                return TRUE;
            }
            
          
            $this->mdb->from('mapping_area_to_user');
            
            $this->mdb->join('master_user_details', 'master_user_details.auto_id=mapping_area_to_user.user_id', 'INNER');
            
            
            $this->mdb->join('master_area_create_by_super', 'master_area_create_by_super.region_code=mapping_area_to_user.region_code', 'INNER');
            
            $this->mdb->where(array('master_user_details.app_id' => $_app_id, 'mapping_area_to_user.mapping_status' => 1, 'master_user_details.super_user_mapped_region_code' => $_region_id, 'master_user_details.delete_status' => 0));
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getUserListModel($_condition_array = NULL) {
        if ($_condition_array != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('master_user_details');
            $this->mdb->where($_condition_array);
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function updateUserModel($_auto_id = NULL, $_data = NULL) {
        if ($_auto_id != NULL and $_data != NULL) {
            $this->mdb->where('auto_id', $_auto_id);
            $this->mdb->update('master_user_details', $_data);
            return ($this->mdb->affected_rows() != 1) ? FALSE : TRUE;
        } else {
            return FALSE;
        }
    }
/**
 * 
 * 
 * 
 * 
 * 
 */
   
public function getUserRightModel($_app_id=NULL,$_region_id=NULL) {
    if($_app_id!=NULL and $_region_id!=NULL){
        $this->mdb->select('*');
        $this->mdb->from('mapping_area_rights_to_apps_right');
        $this->mdb->where(array('app_id'=>$_app_id,'region_code'=>$_region_id,'mapping_status'=>1));
        return $this->getData($this->mdb->get());
    }  else {
        return FALSE;    
    }
    
}/**
     * 
     * 
     * 
     * 
     */
    private function getData($_query = NULL) {
        if ($_query != NULL and $_query->num_rows() > 0) {
            return $_query->result_array();
        } else {
            return FALSE;
        }
    }

}
