<?php
/**
  * @method 宜配网地址库表基本操作
  * @author 卢
  * @copyright 2017.6.21
  */
class B2cAddr extends B2cBase
{
	  /**
	  * @method 修改默认地址
	  * @param  $id 需要修改的地址id,
	  * @param $Uid用户ID
	  * @author 卢
	  * @copyright 2017.6.9
	  * @return  bool
	  */
	 function setUserDefaultAddr($id,$Uid)
	 {
	 	if(LibFc::Int($id)&& LibFc::Int($Uid))
	 	{
	 		$condition=array('add_default');
	 		$table='us_address';
		 	$where=sprintf(' and add_uid=%d',$Uid);
		 	$where2=sprintf(' and add_uid=%d and add_id=%d',$Uid,$id);
		 	foreach ($condition as $k => $v)
		 	{
			 	$Option=array($v=>0);
			 	$this->B2CDb->Begin();
			 	$this->set($table,$Option,$where);
			 	$Option[$v]=1;
			 	$res=$this->set($table,$Option,$where2);
			 	$this->B2CDb->End();
		 	}
		 	return $res;
	 	}
	 	return LibFc::ReturnData(false, $table.'查询提供的参数不正确');
	 }
 	 /**
	  * @method 获取地址列表
	  * @param  $uid 需要查询的用户id
	  * @author 卢
	  * @copyright 2017.6.8
	  * @return  array(2) { [0]=>array(1) { ['add_id']=>30 } [1]=>array(1) { ['add_id']=>31} }
	  */
	 function getMyListUser($uid)
	 {
	 	if(LibFc::Int($uid))
	 	{
	 		$condition='add_uid';
	 		$table='us_address';
	 		$value='add_pct_ids';
		 	$YpwAddr = new YpwAddr();
		 	$data=$this->get($table,['*'],' and '.$condition.'='.$uid.' order by add_default desc');
		 	foreach ($data['data'] as $k => $v) 
		 	{
		 		$result=$YpwAddr->getAddrByIds($v[$value]);
		 		$data['data'][$k]['pathname']=implode('', $result['data']);
		 	}
		 	return LibFc::ReturnData(true, $data['data']);	 		
	 	}
	 	return LibFc::ReturnData(fasle ,$table.'查询提供的参数不正确');
	 }
	 /**
	  * @method 校验两次提交信息是否一样
	  * @param  $data 提交的数据
	  * @author 卢
	  * @copyright 2017.6.9
	  * @return  bool(数据不一样返回true)
	  */
	 function checkUserUniqueAddr($data)
	 {	
	 	$table='us_address';
		$FiledArr =['add_name', 'add_pct_ids','add_address','add_tel','add_mobile','add_uid'];
		$value='add_id';
		$Val = [];
		foreach($FiledArr as $V)
		{
			if(isset($data[$V]))
			{
				if($data[$V]!==null&&$data[$V]!==''){
					$Val[] = sprIntf("%s='%s'",  $V, LibFc::Escape($data[$V]));
				}else{
					$Val[] = sprIntf("%s is null",  $V);
				}
			}
		}
 		$where =sprIntf(" and %s", implode(' and ', $Val));
 		$re=$this->get($table,["$value"],$where);
 		if(empty($re['data']))
 		{
 			return LibFc::ReturnData(true,true);
 		}else
 		{
 			return LibFc::ReturnData(true,false);
 		}
	 }
	  /**
	  * @method 获取卖家地址列表
	  * @param  $shopid 需要查询的用户店铺id
	  * @author 卢
	  * @copyright 2017.6.12
	  * @return  array(2) { [0]=>array(1) { ['add_id']=>30 } [1]=>array(1) { ['add_id']=>31} }
	  */
	 function getMyListSeller($shopid)
	 {
	 	if(LibFc::Int($shopid))
	 	{
	 		$condition='addr_shop_id';
	 		$table='sh_address';
	 		$value='addr_pct_ids';
		 	$YpwAddr = new YpwAddr();
		 	$data=$this->get($table,['*'],' and '.$condition.'='.$shopid);
		 	foreach ($data['data'] as $k => $v) 
		 	{
		 		$result=$YpwAddr->getAddrByIds($v[$value]);
		 		$data['data'][$k]['pathname']=implode('', $result['data']);
		 	}
		 	return LibFc::ReturnData(true, $data['data']);	 		
	 	}
	 	return LibFc::ReturnData(fasle ,$table.'查询提供的参数不正确');
	 }
 	  /**
	  * @method 修改卖家默认地址
	  * @param  $id 需要修改的地址id,
	  * @param $shopid 用户店铺ID 
	  * @param $condition 默认地址字段
	  * @author 卢
	  * @copyright 2017.6.12
	  * @return  bool
	  */
	 function setSellerDefaultAddr($id,$shopid,$condition)
	 {
	 	if(LibFc::Int($id)&& LibFc::Int($shopid))
	 	{
	 		$table='sh_address';
		 	$where=sprintf(' and addr_shop_id=%d',$shopid);
		 	$where2=sprintf(' and addr_shop_id=%d and addr_id=%d',$shopid,$id);
		 	foreach ($condition as $k => $v)
		 	{
			 	$Option=array($v=>0);
			 	$this->B2CDb->Begin();
			 	$this->set($table,$Option,$where);
			 	$Option[$v]=1;
			 	$res=$this->set($table,$Option,$where2);
			 	$this->B2CDb->End();
		 	}
		 	return $res;
	 	}
	 	return LibFc::ReturnData(false, $table.'查询提供的参数不正确');
	 }
	 /**
	  * @method 校验卖家两次提交信息是否一样
	  * @param  $data 提交的数据
	  * @author 卢
	  * @copyright 2017.6.12
	  * @return  bool(数据不一样返回true)
	  */
	 function checkSellerUniqueAddr($data)
	 {	
	 	$table='sh_address';
		$FiledArr =['addr_name', 'addr_pct_ids','addr_address','addr_tel','addr_mobile','addr_shop_id'];
		$value='addr_id';
		$Val = [];
		foreach($FiledArr as $V)
		{
			if(isset($data[$V]))
			{
				if($data[$V]!==null&&$data[$V]!==''){
					$Val[] = sprIntf("%s='%s'",  $V, LibFc::Escape($data[$V]));
				}else{
					$Val[] = sprIntf("%s is null",  $V);
				}
			}
		}
 		$where =sprIntf(" and %s", implode(' and ', $Val));
 		$re=$this->get($table,["$value"],$where);
 		if(empty($re['data']))
 		{
 			return LibFc::ReturnData(true,true);
 		}else
 		{
 			return LibFc::ReturnData(true,false);
 		}
	 }
}