<?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 RegionModel
 *
 * @author zmq163
 */
class Region_model extends CI_Model {

    //put your code here
    var $mdb;

    public function __construct() {
        parent::__construct();
        $this->mdb = $this->load->database('mdb', TRUE);
    }

    public function submitUpdateUserRightModel($_app_id = NULL, $_region_code=NULL,$_right_array = NULL) {
        if ($_app_id != NULL) {
            $this->mdb->trans_begin();
            if ($_right_array != NULL) {
                $this->mdb->where('app_id', $_app_id);
                $this->mdb->where('region_code', $_region_code);
                $this->mdb->where_not_in('right_id', $_right_array);
                $this->mdb->update('mapping_area_rights_to_apps_right', array('mapping_status' => 0));
                
                $this->mdb->where('app_id', $_app_id);
                $this->mdb->where('region_code', $_region_code);
                $this->mdb->where_in('right_id', $_right_array);
                $this->mdb->update('mapping_area_rights_to_apps_right', array('mapping_status' => 1));
                
                $this->mdb->select('right_id');
                $this->mdb->from('mapping_area_rights_to_apps_right');
                $this->mdb->where(array('app_id' => $_app_id,'region_code'=>$_region_code));
                $right_id = NULL;
                foreach ($this->getData($this->mdb->get()) as $value) {
                    $right_id[] = $value['right_id'];
                }
                $insert_batch_array = NULL;
                for ($i = 0; $i < count($_right_array); $i++) {
                    if (!in_array($_right_array[$i], $right_id)) {
                        $insert_batch_array[] = array('app_id' => $_app_id, 'right_id' => $_right_array[$i], 'mapping_status' => 1,'region_code'=>$_region_code);
                    }
                }
                if ($insert_batch_array != NULL) {
                    $this->mdb->insert_batch('mapping_area_rights_to_apps_right', $insert_batch_array);
                }
            } else {
                $this->mdb->where('app_id', $_app_id);
                $this->mdb->where('region_code', $_region_code);
                $this->mdb->update('mapping_area_rights_to_apps_right', array('mapping_status' => 0));
            }


            if ($this->mdb->trans_status() === FALSE) {
                $this->mdb->trans_rollback();
                return FALSE;
            } else {
                $this->mdb->trans_commit();
                return TRUE;
            }
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getUserRightRegionCodeModel($_region_code = NULL, $_app_id = NULL) {
        if ($_region_code != NULL and $_app_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_code, 'mapping_status' => 1));
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function mappingRegionUser($region_code, $update_region_data, $user_auto_id, $update_user_data, $data_right, $data_login,$history_data) {
        $this->mdb->trans_begin();
        $this->updateRegionModel($region_code, $update_region_data);
        $this->updateUserModel($user_auto_id, $update_user_data);
        $this->mappinRegionRight($data_right);
        $this->mappingRegionUserModel(array('region_code' => $region_code, 'user_id' => $user_auto_id));
        $this->insertLoginUserModel($data_login);
        $this->userHistoryInsert($history_data);
        if ($this->mdb->trans_status() === FALSE) {
            $this->mdb->trans_rollback();
            return FALSE;
        } else {
            $this->mdb->trans_commit();
            return TRUE;
        }
    }
    /**
     * 
     * 
     * 
     * 
     */
    private function userHistoryInsert($_history_data=NULL){
        if($_history_data!=NULL){
             $this->mdb->insert('user_activation_history', $_history_data);
            return ($this->mdb->affected_rows() != 1) ? FALSE : TRUE;
        }  else {
            return FALSE;    
        }
    }

    /**
     * 
     * @param type $_login_data
     * @return boolean
     * 
     * 
     * 
     * 
     */
    public function insertLoginUserModel($_login_data = NULL) {
        if ($_login_data != NULL) {
            $this->mdb->insert('master_login_user', $_login_data);
            return ($this->mdb->affected_rows() != 1) ? FALSE : TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function mappingRegionUserModel($_mapping_data = NULL) {
        if ($_mapping_data != NULL) {
            $this->mdb->insert('mapping_area_to_user', $_mapping_data);
            return ($this->mdb->affected_rows() != 1) ? FALSE : TRUE;
        } 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 getRegionListModel($_array_condition = NULL) {
        if ($_array_condition != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('master_area_create_by_super');
            $this->mdb->where($_array_condition);
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    public function getUserRightModel($_region_id = NULL) {
        if ($_region_id != NULL) {
            $this->mdb->select('*');
            $this->mdb->from('');
            $this->mdb->join('', '', 'INNER');
            $this->mdb->where(array());
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getAdminUserRightModel($_app_id = NULL) {
        if ($_app_id != NULL) {
            $this->mdb->select('mapping_app_rights.right_id,sadmin_app_rights.right_name');
            $this->mdb->from('mapping_app_rights');
            $this->mdb->join('sadmin_app_rights', 'sadmin_app_rights.right_id=mapping_app_rights.right_id', 'INNER');
            $this->mdb->where(array('mapping_app_rights.app_id' => $_app_id, 'mapping_app_rights.mapp_right_status' => 1));
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */

    /**
     * 
     * 
     * 
     */
    public function updateRegionModel($_region_code = NULL, $_update_data_array = NULL) {
        if ($_region_code != NULL and $_update_data_array != NULL) {
            $this->mdb->where('region_code', $_region_code);
            $this->mdb->update('master_area_create_by_super', $_update_data_array);
            return ($this->mdb->affected_rows() != 1) ? FALSE : TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getRegionUpdateEditDeleteModel($_app_id = NULL, $_region_level) {
        if ($_app_id != NULL and $_region_level != NULL) {
            $this->mdb->select('master_area_create_by_super.*,application_create.app_name');
            $this->mdb->from('master_area_create_by_super');
            $this->mdb->join('application_create', 'master_area_create_by_super.app_id=application_create.app_id', 'INNER');
            $this->mdb->where(array('master_area_create_by_super.app_id' => $_app_id, 'master_area_create_by_super.region_level' => $_region_level));
            return $this->getData($this->mdb->get());
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function createNewRegionModel($_region_data = NULL) {
        if ($_region_data != NULL) {
            $this->mdb->insert('master_area_create_by_super', $_region_data);
            return ($this->mdb->affected_rows() != 1) ? FALSE : TRUE;
        }
    }

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

    /**
     * 
     * 
     * 
     * 
     */
    public function mappinRegionRight($_mapping_batch_array = NULL) {
        if ($_mapping_batch_array != NULL) {
            $this->mdb->insert_batch('mapping_area_rights_to_apps_right', $_mapping_batch_array);
            return ($this->mdb->affected_rows() != 1) ? FALSE : TRUE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    private function getData($_query = NULL) {
        if ($_query != NULL and $_query->num_rows() > 0) {
            return $_query->result_array();
        } else {
            return FALSE;
        }
    }

}
