<?php
if (!defined('IN_59MIAO'))
{
	exit('Access Defined');
}
class SellercateModel extends Model {
	private $_api='';
	public function __construct() {
		parent::__construct();
		$this->_fields = array('id','cid','name','count','state','sort');
		$this->_tables = array(DB_FREFIX.'seller_cate');
		$this->_check = new SellercateCheck();
		list(
				$this->_R['id'],			
		) = $this->getRequest()->getParam(array(
				isset($_GET['id']) ? $_GET['id'] : null				
		));
		$this->_api=new ApiModel();
	}
	//批量增加b2c商家信息
	public function addb2ccate(){
		error_reporting(E_ALL);		
		$allcate=$this->_api->GetShopCats();		
		$state=1;
		$sort=10;	
		foreach ($allcate as $value){			
			$cid=$value['cid'];
			$name=$value['name'];			
			$count=$value['count'];			
			$_updateData=array('cid'=>$cid,'name'=>$name,'count'=>$count,'state'=>$state,'sort'=>$sort);
			$_where = array("cid=$cid");	
			$data='';
			$data=parent::isOne($_where);					
			if($data==1){
				parent::update($_where, $_updateData);				
			}
			else{				
				parent::add($_updateData);				
			}
		}
		return true;
	}	
	public function findAll() {
		return parent::select(array('id','cid','name','count','state','sort'), 
											array('limit'=>$this->_limit, 'order'=>'sort ASC'));
	}	
	//获取所有分类不分页
	public function findAllNoLimit(){
		$_where =array("state=1");
		return parent::select(array('id','cid','name','count','state','sort'), 
											array('where'=>$_where,'order'=>'sort ASC'));
	}	
	public function findOne() {
		$_where = array("id='{$this->_R['id']}'");
		if (!$this->_check->oneCheck($this, $_where)) $this->_check->error();
		return parent::select(array('id','cid','name','count','state','sort'),
											array('where'=>$_where, 'limit'=>'1'));
	}	
	public function total(){
		return parent::total();
	}
	
	public function add() {		
		if (!$this->_check->addCheck($this)) $this->_check->error();
		$_addData = $this->getRequest()->add($this->_fields);
		if(!is_numeric($_addData['sort'])){
			$_addData['sort']=10;
		}		
		return parent::add($_addData);
	}	
	public function update() {
		$_where = array("id='{$this->_R['id']}'");
		if (!$this->_check->oneCheck($this, $_where)) $this->_check->error();
		if (!$this->_check->updateCheck($this)) $this->_check->error();
		$_updateData = $this->getRequest()->update($this->_fields);
		if(!is_numeric($_updateData['sort'])){
			$_addData['sort']=10;
		}	
		return parent::update($_where, $_updateData);
	}	
	public function delete() {
		$_where = array("id='{$this->_R['id']}'");
		return parent::delete($_where);
	}	
	public function sort() {
		foreach ($_POST['sort'] as $_key=>$_value) {
			if (!is_numeric($_value)) continue;
			$_setField = array('sort'=>$_value);
			$_where = array("id='$_key'");
			parent::update($_where, $_setField);
		}
		return true;
	}
}
?>