<?php

Doo::loadCore('db/DooModel');

class district extends DooModel {

    public $id;
    public $name;
    public $level;
   	public $usetype;
    public $upid;
    public $displayorder;
    public $area;
    
    public $_table = 'CLD_district';
    public $_primarykey = 'id';
    public $_fields = array('id', 'name', 'level', 'usetype', 'upid', 'displayorder', 'area');

    public function getDistrictInId($idString=''){
    	if (empty($idString)) return array();
    	$list=$this->find(array('where' => 'id in (' . $idString.')', 'asArray' => true));
    	Doo::loadClass ( 'XDeode' );
    	$XDeode = new XDeode ( 5 );
    	foreach ( $list as $key => $value ) {
    		$list [$key] ['idKey'] = $XDeode->encode ( $value ['id'] );
    	}
    	return $list;
    }

	public function get_all() {
        $archive_list = $this->find(array( 'asc' => 'id',  'asArray' => true));
        return $archive_list;
    }
    
	public function getbyid($id) {
        $archive_list = $this->find(array('select' => 'id,name,level,upid,area', 'where' => 'id=' . $id, 'asArray' => true));
        
        return $archive_list;
    }
    
	public function getbyidlist($id) {
        $archive_list = $this->find(array('select' => 'id,name,level,upid,area', 'where' => 'id in ( ' . $id.' )', 'asArray' => true));
        
        return $archive_list;
    }
    
    public function get_lvByStaffid($lv,$sid){
    	$sql = "select * from CLD_district_staff as a left join " . $this->_table . " as b on (a.did=b.id and a.sid=".$sid.")  where level= ".$lv ." group by b.id" ;
		
    	$query = Doo::db ()->query ( $sql );
		
		$result = $query->fetchAll ();
		
		return $result;
    }
    
	public function districtByStaffid($did,$sid){
    	$sql = "select * from CLD_district_staff as a left join " . $this->_table . " as b on (a.did=b.id and a.sid=".$sid.")  where upid= ".$did  ;
		
    	$query = Doo::db ()->query ( $sql );
		
		$result = $query->fetchAll ();
		
		return $result;
    }
    
	public function get_lv($lv) {
        $archive_list = $this->find(array( 'asc' => 'id','where'=>'level='.$lv,  'asArray' => true));
        
        Doo::loadClass ( 'XDeode' );
        $XDeode = new XDeode ( 5 );
        
        foreach ($archive_list as $key=>$value){
        	$archive_list[$key] ['didKey'] = $XDeode->encode ( $value ['id'] );
        }
        
        return $archive_list;
    }
    
	public function get_lvByid($lv,$id) {
        $archive_list = $this->find(array( 'asc' => 'id','where'=>' upid='.$id,  'asArray' => true));
        return $archive_list;
    }
    
}

?>