<?php
 /**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
class GoodsModel extends BaseModel{
	public $msg='';
	function getInfo($params,$fields='*'){
		if(is_array($params) && $params['sku'] && $params['mega']){
			$sku = $params['sku'];
			$sql = "select goodsId from qk_goods_attr_val where `value`='$sku'";
			$goodsId = $this->db->getOne($sql);
		}else{
			$goodsId = $params;
		}
		if(!$goodsId || !settype($goodsId,"integer")){return false;}
		$fieldsArr = array();$tempArr = array();
		if($fields!='*'){
			$fieldsArr = explode(',',$fields);//print_r($fieldsArr);
			foreach($fieldsArr as $val){ if(in_array($val,array('id','name','parentId','posttime','title'))){ $tempArr[]=$val; } }
			if(in_array('promotion',$fieldsArr)){
				$tempArr = array_merge($tempArr,array('promoteEndtime','promotePrice'));
			}
			$fields=implode(',',$tempArr);
		}
		$sql = "select $fields from qk_goods where id='$goodsId'";
		$goodsInfo = $this->db->getRow($sql);
		$goodsInfo = $this->getInfoExt($goodsInfo);//print_r($goodsInfo);exit;
		return $goodsInfo;
	}
	function getInfoExt($goodsInfo,$fields='*'){
		$fieldsArr = array();$tempArr = array();
		if($fields!='*'){
			$fieldsArr = explode(',',$fields);//print_r($fieldsArr);
			foreach($fieldsArr as $val){ if(in_array($val,array('id','name','parentId','posttime','title'))){ $tempArr[]=$val; } }
			$fields=implode(',',$tempArr);
		}
		if(!$goodsInfo){ return false; }else{
			if($fields=='*' || in_array('thumb',$fieldsArr) || in_array('preview',$fieldsArr)){
				$preview = $this->db->getAll("select id,url from qk_attachment where parentId=0 and iStatus=1 and typeId=1 and goodsId='".$goodsInfo['id']."'");
				$i=0;
				foreach($preview as $item){
					$default=$this->db->getRow("select id,url from qk_attachment where parentId=0 and iStatus=1 and typeId=1 and id='".$item['id']."'");
					if($fields!='*' && !in_array('preview',$fieldsArr)){
						if($i>0){break;}
					}else{
						if(substr($preview[$i]['url'],0,7)!="http://" && substr($preview[$i]['url'],0,1)!="/"){$preview[$i]['url'] = "/".$preview[$i]['url'];}
						$smallInfo = $this->db->getRow("select url,id from qk_attachment where parentId=".$item['id']." and iStatus=1 and typeId=2 and goodsId='".$goodsInfo['id']."'");
						if(!$smallInfo){$smallInfo=$default;}
						if(substr($smallInfo['url'],0,7)!="http://" && substr($smallInfo['url'],0,1)!="/"){$smallInfo['url'] = "/".$smallInfo['url'];}
						$preview[$i]['small'] = $smallInfo;

						$largeInfo = $this->db->getRow("select url,id from qk_attachment where parentId=".$item['id']." and iStatus=1 and typeId=4 and goodsId='".$goodsInfo['id']."'");
						if(!$largeInfo){$largeInfo=$default;}
						if(substr($largeInfo['url'],0,7)!="http://" && substr($largeInfo['url'],0,1)!="/"){$largeInfo['url'] = "/".$largeInfo['url'];}
						$preview[$i]['large'] = $largeInfo;
					}
					$middleInfo = $this->db->getRow("select url,id from qk_attachment where parentId=".$item['id']." and iStatus=1 and typeId=3 and goodsId='".$goodsInfo['id']."'");
					if(!$middleInfo){$middleInfo=$default;}
					if(substr($middleInfo['url'],0,7)!="http://" && substr($middleInfo['url'],0,1)!="/"){$middleInfo['url'] = "/".$middleInfo['url'];}
					$preview[$i]['middle'] = $middleInfo;
					$preview[$i]['index']=$i;
					$i++;
				}
			//print_r($preview);exit;
				$goodsInfo['preview'] = $preview;
				if($goodsInfo['thumbId']){
					$thumb = $this->db->getRow("select * from qk_attachment where iStatus=1 and id='".$goodsInfo['thumbId']."'");
					if(substr($thumb['url'],0,7)!="http://"){$thumb['url']="/".$thumb['url'];}
				}else{
					$thumb = $preview[0]['middle'];
				}
				$goodsInfo['thumbnail'] = $thumb;
				if(count($goodsInfo['preview'])==0 && $thumb){
					$goodsInfo['preview'][0]['large'] = $thumb;
					$goodsInfo['preview'][0]['middle'] = $thumb;
					$goodsInfo['preview'][0]['small'] = $thumb;
				}
				if(count($goodsInfo['preview'])==0 && $goodsInfo['picUrl']){
					$goodsInfo['preview'][0]['large']['url'] = $goodsInfo['picUrl'];
					$goodsInfo['preview'][0]['middle']['url'] = $goodsInfo['picUrl']."_360x360.jpg";
					$goodsInfo['preview'][0]['small']['url'] = $goodsInfo['picUrl']."_220x220.jpg";
				}

				if(!$goodsInfo['thumbnail'] && $goodsInfo['picUrl']){
					if(preg_match("/paipai/",$goodsInfo['picUrl'])){
						$goodsInfo['thumbnail']['url'] = preg_replace("/\.jpg$/",".300x300.jpg",$goodsInfo['picUrl']);
					}else{
						$goodsInfo['thumbnail']['url'] = $goodsInfo['picUrl']."_320x320.jpg";
					}
				}
			}
			$goodsInfo['url'] = "/goods/view/id/".$goodsInfo['id'];
			$goodsInfo['url'] = $GLOBALS['baseUrl']."goods/".$goodsInfo['id'].".html";
			if($fields=='*' || in_array('attr',$fieldsArr)){
				$attr = $this->db->getAll("select attrId,id from qk_goods_attr_val where goodsId='".$goodsInfo['id']."' order by id asc");
				foreach($attr as $item){
					$attrId = $item['attrId'];
					$attrInfo = $this->db->getRow("select * from `qk_goods_attr` where id='$attrId'");
					$attrKey = $attrInfo['keyName'];
					$attrValId = $item['id'];
					//if($attrId==12){print_r($attrInfo);exit;}
					if($attrInfo['type']=='checkbox'){
						$goodsInfo['attr'][$attrId] = $this->db->getAll("select * from qk_goods_attr_val where goodsId='".$goodsInfo['id']."' and attrId='".$attrId."' order by id asc");
						$attrIndex = 0;
						foreach($goodsInfo['attr'][$attrId] as $ext){
							$goodsInfo['attr'][$attrId][$attrIndex]['name'] = $this->db->getOne("select value from qk_goods_attr_ext where id='".$ext['extId']."'");
							$goodsInfo['attr'][$attrId]['vals'][] = $ext['extId'];
							$attrIndex++;
						}
						//print_r($goodsInfo['attr'][$attrId]);exit;
					}else{
						$goodsInfo['attr'][$attrId] = $this->db->getRow("select * from qk_goods_attr_val where goodsId='".$goodsInfo['id']."' and id='".$attrValId."'");
					}
					$goodsInfo['attr'][$attrKey] = $this->db->getRow("select * from qk_goods_attr_val where goodsId='".$goodsInfo['id']."' and id='".$attrValId."'");
					//if($attrId==11){print_r($goodsInfo['attr'][$attrId]);exit;}
				}
				//print_r($goodsInfo['attr'][12]);exit;
				if($goodsInfo['attr']['mega']){
					$mega = $goodsInfo['attr']['mega']['value'];
					$sku = $goodsInfo['attr']['sku']['value'];
					$mega = str_replace(" ","-",$mega);
					$sku = str_replace(" ","-",$sku);
					if($mega && $sku){
					$goodsInfo['url'] = $GLOBALS['baseUrl'].$mega."-p/".$sku.".htm";
					}
				}
				if($goodsInfo['catId']){
					load_class("category");
					$catModel = new CategoryModel();
					$catInfo = $catModel->getInfo($goodsInfo['catId']);//print_r($catInfo);exit;
					if($catInfo['typeId']){
						$goodsInfo['attrs'] = $this->db->getAll("select * from qk_goods_attr where typeId='".$catInfo['typeId']."'");
						$attrIndex = 0;
						$attrs = array();
						foreach($goodsInfo['attrs'] as $attr){
							$attrs[$attrIndex]['type'] = $attr;
							$attrs[$attrIndex]['item'] = $this->db->getAll("select * from qk_goods_attr_ext where attrId='".$attr['id']."'");
							$attrIndex++;
						}
						$goodsInfo['attrs'] = $attrs;
					}
				}
			}
			//print_r($goodsInfo['attrs']);exit;
			//print_r($goodsInfo['attr'][12]);exit;
			if($fields=='*' || in_array('catName',$fieldsArr))$goodsInfo['catName'] = $this->db->getOne("select name from  qk_category where id='".$goodsInfo['catId']."'");
			if($fields=='*' || in_array('brandName',$fieldsArr)){$goodsInfo['brandName'] = $this->db->getOne("select name from  qk_brand where id='".$goodsInfo['brandId']."'");};
			if($fields=='*' || in_array('userInfo',$fieldsArr)){$goodsInfo['userInfo'] = $this->db->getRow("select * from  qk_user where id='".$goodsInfo['userId']."'");}
			if($fields=='*' || in_array('comInfo',$fieldsArr)){
				load_class("com");
				$com = new ComModel();
				$goodsInfo['comInfo'] = $com->getInfo(array("userId"=>$goodsInfo['userId']));
			}
			if($fields=='*' || in_array('threeMonthSaleNum',$fieldsArr)){
				$threeMonthSaleNum = 0;
				$threeMonth = time()-3*30*24*60*60;
				$sql = "select * from qk_order_goods where goodsId='".$goodsInfo['id']."' and posttime>$threeMonth";
				$orderGoods  = $this->db->getAll($sql);
				foreach($orderGoods as $item){
					$sql = "select iStatus from qk_order where id='".$item['orderId']."'";
					$iStatus = $this->db->getOne($sql);
					if($iStatus==20){$threeMonthSaleNum += $item['pnum'];}
				}
				$goodsInfo['threeMonthSaleNum'] = $threeMonthSaleNum;
			}
			if($fields=='*' || in_array('saleNum',$fieldsArr)){
				$saleNum=0;
				$sql = "select * from qk_order_goods where goodsId='".$goodsInfo['id']."'";
				$orderGoods  = $this->db->getAll($sql);
				foreach($orderGoods as $item){
					$sql = "select iStatus from qk_order where id='".$item['orderId']."'";
					$iStatus = $this->db->getOne($sql);
					if($iStatus==20){
						$saleNum += $item['pnum'];
					}
				}
				$goodsInfo['saleNum'] = $saleNum;
			}
			if(!$goodsInfo['title']){$goodsInfo['title'] = $goodsInfo['name'];}
			$goodsInfo['price'] = sprintf("%.2f",$goodsInfo['price']);
			if(!$goodsInfo['marketPrice'] || $goodsInfo['marketPrice'] ==0){$goodsInfo['marketPrice']=$goodsInfo['price'];}
			if($goodsInfo['promotion']){
				$goodsInfo['promotePrice'] = sprintf("%.2f",$goodsInfo['promotePrice']);
				$goodsInfo['limitTimer'] = $goodsInfo['promoteEndtime'] - time();
				if($goodsInfo['limitTimer']>0){
					$t = $goodsInfo['limitTimer'];
					$hour = $t/3600;
					$yushu = $t%3600;
					$hour = floor($hour);
					if($hour){
						if($hour>24){
							$hour=floor($hour/24).'天'.($hour%24).'小时';
						}else{
							$hour="{$hour}小时";
						}
					}else{
						$hour='';
					}
					$minute = floor($yushu/60);
					if($minute){$minute="{$minute}分钟";}else{$minute='';}
					$seconds = $yushu%60;
					if($seconds){$seconds="{$seconds}秒";}else{$seconds='';}
					$goodsInfo['limitTime'] = $hour.$minute.$seconds."";
				}else{
					$goodsInfo['promotion'] = 0;
				}
			}
			
			$goodsInfo['marketPrice'] = sprintf("%.2f",$goodsInfo['marketPrice']);
			$goodsInfo['priceNow'] = $goodsInfo['price'];
			if($goodsInfo['promotion'] && $goodsInfo['promotePrice']>0){$goodsInfo['priceNow']=$goodsInfo['promotePrice'];}
			if(!$goodsInfo['keywords']){$goodsInfo['keywords'] = $goodsInfo['title'];}
			if(!$goodsInfo['description']){$goodsInfo['description'] = $goodsInfo['title'];}
			if($goodsInfo['marketPrice']>0)$goodsInfo['discount']=sprintf("%.1f",$goodsInfo['priceNow']*10/$goodsInfo['marketPrice']);
			//print_r($goodsInfo['attr']);exit;
			if($fields=='*' || in_array('apprise',$fieldsArr)){
				$goodsInfo['appraise'] = $this->db->getAll("select * from `qk_order_goods` where point>0 and goodsId='".$goodsInfo['id']."' order by id desc limit 10");
				$i=0;
				foreach($goodsInfo['appraise'] as $item){
					$nick = $this->db->getOne("select nickname from qk_user where id='".$item['userId']."'");if(!$nick){$nick='匿名';}
					$goodsInfo['appraise'][$i]['nick']=$nick;
					$i++;
				}
				unset($i);
			}
			if($goodsInfo['content']){
				if(!function_exists("mat")){function mat($matches){return '<img src="http://static.ueidc.com/images/blank.gif" data-src="'.$matches[2].'"'.$matches[1];}}
				$contentFront = preg_replace_callback("/<img([a-zA-Z =\"]*) src=\"([0-9a-zA-Z:\/_!\.]+)\"/Usi","mat",$goodsInfo['content']);
				$goodsInfo['contentFront'] = $contentFront;
			}
			return $goodsInfo;
		}
	}
	
	function del($params){
		if(is_numeric($params)){
			$id = $params;
		}else if(is_array($params)){
			$id = isset($params['id'])?$params['id']:0;
		}
		if($id && settype($id,"integer")){
			$sql = "delete from qk_goods where id='$id'";
			if($this->db->query($sql)){
				$goodsId = $id;
				$sql = "select * from qk_attachment where goodsId='$goodsId'";
				return true;
			}else{
				return false;
			}			
		}
		return false;
	}

	function getAttr($typeId){
		if($typeId!=="" && $typeId!==null && settype($typeId,"integer")!==false){
			$sql = "select * from qk_goods_attr where typeId='$typeId'";
			$attr = $this->db->getAll($sql);
			if($attr){
				return $attr;
			}
		}
		return false;
	}

	function query($params){
		$act = $params['act'];
		$id = $params['id'];
			$name = $params['name'];
			$catId = $params['catId'];
			$brand = $params['brand'];
			$brandId = $params['brandId'];
			$price = $params['price'];
			$marketPrice = $params['marketPrice'];
			$promotion = $params['promotion'];
			$isNew = $params['isNew'];
			$isHot = $params['isHot'];
			$isLively = $params['isLively'];
			$isTuan = $params['isTuan'];
			$content = $params['content'];
			$images = $params['images'];
			$inventory = $params['inventory'];
			$attr = $params['attr'];
			$weight = $params['weight'];
			$iStatus = $params['iStatus'];
			$title = $params['title'];
			$keywords = $params['keywords'];
			$description = $params['description'];
			$thumb = $params['thumb'];
			$bn = $params['bn'];
			$remark = $params['remark'];
			$freeDelivery = $params['freeDelivery'];
			$externalLink = $params['externalLink'];
			$score = $params['score'];
			$scorePrice = $params['scorePrice'];
			$buyWay = $params['buyWay'];
			$promotePrice = $params['promotePrice'];
			$promoteName = $params['promoteName'];
			$promoteEndtime = $params['promoteEndtime'];
			$promoteStarttime = $params['promoteStarttime'];
			$clickUrl = $params['clickUrl'];
			$picUrl = $params['picUrl'];
			$iid = $params['iid'];
			$link = $params['link'];
			$linkPrice = $params['linkPrice'];
			$nick = $params['nick'];
			$shopClickUrl = $params['shopClickUrl'];
			$reason = $params['reason'];
			$sortId = $params['sortId'];
			$HandleClass = $params['HandleClass'];
			$commission = $params['commission'];
			$nsi = $params['nsi'];
			$loginName = $params['loginName'];
			if($loginName ){ if(!$userId=$this->db->getOne("select id from qk_user where loginName='$loginName'")){$this->msg = '会员不存在'; return false;} }else{ $userId=0;}
			if(!$promoteEndtime || ($promoteEndtime=strtotime($promoteEndtime))==false){$promoteEndtime=0;}
			if(!$promoteStarttime || ($promoteStarttime=strtotime($promoteStarttime))==false){$promoteStarttime=0;}
			if(!settype($score,"integer")){$score=0;}
			if(!settype($scorePrice,"integer")){$scorePrice=0;}
			if(!settype($buyWay,"integer")){$buyWay=1;}
			if(!settype($iStatus,"integer")){$iStatus=0;}
			if(!settype($inventory,"integer")){$inventory=0;}
			if(!settype($catId,"integer")){$catId=0;}
			if(!settype($brandId,"integer")){$brandId=0;}
			if(!settype($isTuan,"integer")){$isTuan=0;}
			if(!settype($price,"float")){$price=0;}
			if(!settype($freeDelivery,"integer")){$freeDelivery=0;}
			if($promotion!=1){$promotion=0;}; if($isNew!=1){$isNew=0;}; if($isHot!=1){$isHot=0;}; if($isLively!=1){$isLively=0;};
			if(!$userId){$userId = $_SESSION['adminId']; };
			if(!$userId){$userId=0; }
			if($name){
				if($act=="add"){ 
					$_SESSION['catId'] = $catId; $_SESSION['brandId'] = $brandId; 
					if($iid){
						$sql = "select id from qk_goods where iid='".$iid."'";
						$idres = $this->db->getOne($sql);
						if($idres){
							$this->msg = '商品重复';return false;
						}
					}
					$sql = "insert into qk_goods set ";
					$sqlEnd = ",posttime=UNIX_TIMESTAMP()";
				}else if($act=="edit"){
					$sql = "update qk_goods set ";
					$sqlEnd = " where id='".$id."'";
				}
				$sql .= " catId='$catId',brandId='$brandId',price='$price',promotion='$promotion',isNew='$isNew',isHot='$isHot',isLively='$isLively',inventory='$inventory'";
				$sql .= ",weight='$weight',iStatus='$iStatus',marketPrice='$marketPrice',bn='$bn',remark='$remark',freeDelivery='$freeDelivery'";
				$sql .= ",name='$name',content='$content',isTuan='$isTuan',title='$title',keywords='$keywords',description='$description'";
				$sql .= ",score='$score',scorePrice='$scorePrice',buyWay='$buyWay',promotePrice='$promotePrice',externalLink='$externalLink'";
				$sql .= ",promoteName='$promoteName',promoteEndtime='$promoteEndtime',promoteStarttime='$promoteStarttime',picUrl='$picUrl',HandleClass='$HandleClass'";
				$sql .= ",iid='$iid',link='$link',linkPrice='$linkPrice',nick='$nick',shopClickUrl='$shopClickUrl',reason='$reason',clickUrl='$clickUrl',sortId='$sortId',commission='$commission',nsi='$nsi'";
				$siteId="";if(defined('SITE_ID')){$sql .= " ,siteId='".SITE_ID."'";}
				if($userId){$sql.=",userId='$userId'";}
				$sql .= $sqlEnd;
				$this->db->query($sql);
				if($act=="add"){
					$insertId = $this->db->insert_id($this->db);
				}else{
					$insertId = $id;
				}
			}
			if($insertId){
				$goodsId = $insertId;
				if($images){
					$images = explode(",",$images);
					foreach($images as $item){
						if(settype($item,"integer")){
							$sql = "update qk_attachment set goodsId='".$goodsId."' where id='".$item."'";
							$this->db->query($sql);
						}
					}
				}
				$thumbIds = $params['thumbIds'];
				if($thumbIds && is_array($thumbIds)){
					foreach($thumbIds as $item){
						if(is_numeric($item)){
							$sql = "update qk_attachment set goodsId='".$goodsId."' where id='".$item."'";
							$this->db->query($sql);
						}
					}
				}
				//print_r($attr);
				if($attr && count($attr)>0){
					foreach($attr as $key=>$val){
							$new = false;
							$sql = "select type from qk_goods_attr where id='$key'";
							$type = $this->db->getOne($sql);
							if($type=="radio" || $type=="select"){
								$sql = "select id from qk_goods_attr_val where goodsId='$goodsId' and attrId='$key'";
								if(!$this->db->getOne($sql)){
									$new = true;
								}
								$valstr = "extId='$val'";
							}else if($type=="checkbox"){
								$sql = "delete from qk_goods_attr_val where goodsId='$goodsId' and attrId='$key'";$this->db->query($sql);
								//print_r($val);exit;
								foreach($val as $attrVal){
									$sql = "insert into qk_goods_attr_val set goodsId='$goodsId',attrId='$key',extId='$attrVal',posttime=UNIX_TIMESTAMP() ";$this->db->query($sql);
								}
								$valstr = "";
							}else{
								$valstr = "value='$val'";
							}
							if($valstr){
								if($new){
									$sql = "insert into qk_goods_attr_val set goodsId='$goodsId',attrId='$key',$valstr,posttime=UNIX_TIMESTAMP()";
								}else{
									$sql = "update qk_goods_attr_val set $valstr where goodsId='$goodsId' and attrId='$key'";
								}
								$this->db->query($sql);
							}
					}
					//exit;
				}

				if($thumb){
					$sql = "insert into qk_attachment set url='$thumb',goodsId='$goodsId',posttime=UNIX_TIMESTAMP()";
					$this->db->query($sql);
				}
				return true;
			}else{
				return false;
			}
	}

	function add($params){
			return $this->query($params);
	}

	function edit($params){
			return $this->query($params);
	}

	function ajaxColEdit($param){
				$col = $param['col'];
				$val = $param['val'];
				$id = $param['id'];
				if($col && settype($id,"integer")){
					$sql = "update qk_goods set `$col`='$val' where id='".$id."'";
					if($this->db->query($sql)){
						return true;
					}
				}else{
					$this->msg = '参数不完整';
				}
		return false;
	}

	function getPrice($id){
		$sql = "select price from qk_goods where id='$id'";
		$price = $this->db->getOne($sql);
		return $price;
	}

	function export($params){
		global $siteConf;
		header("Content-Type:application/octet-stream");
		Header("Accept-Ranges:bytes");
		header("Content-Disposition:attachment;filename=GoodsList.txt");
		header("Expires:0");
		header("Cache-Control:must-revalidate,post-check=0,pre-check=0"); 
		header("Pragma:public");
		$wheres = "";
		$key = $params["key"];
		if($key){
			$wheres = " and name like '%$key%'";
		}
		$sql = "select id from qk_goods where 1=1 $wheres order by id desc";
		$res = $this->db->getAll($sql);
		echo "Name\tWeight\tPrice\tMega Tittle\tSKU\tImage Link\tItem Link\tcatId\r\n";
		foreach($res as $item){
			$goodsInfo = $this->getInfo($item['id']);
			$thumb = str_replace("//","/",$siteConf['siteUrl'].$goodsInfo['thumbnail']['url']);
			$thumb = str_replace("http:/","http://",$thumb);
			$url = str_replace("//","/",$siteConf['siteUrl'].$goodsInfo['url']);
			$url = str_replace("http:/","http://",$url);
			echo $goodsInfo['name']."\t".$goodsInfo['weight']."\t".$goodsInfo['price']."\t".$goodsInfo['attr']['mega']['value']."\t".$goodsInfo['attr']['sku']['value']."\t".$thumb."\t".$url."\t".$goodsInfo['catId']."\r\n";
		}
		exit();
	}

	function import($params){
			 if(isset($_FILES['goodsfile'])){
				$uploaddir = ROOT_PATH."data/attachment/";
				$uploadfile = $uploaddir.$_FILES['goodsfile']['name'];
				
				if(move_uploaded_file($_FILES['goodsfile']['tmp_name'],$uploadfile)){
					$fp = fopen($uploadfile,"r");
					while(!feof($fp)){
						$tmp = fgets($fp,8192);
						$goodsInfo = explode("\t",$tmp);
						$goods['name'] = $goodsInfo[0];
						$goods['weight'] = $goodsInfo[1];
						$goods['price'] = $goodsInfo[2];
						$goods['mega'] = $goodsInfo[3];
						$goods['sku'] = $goodsInfo[4];
						$goods['thumb'] = $goodsInfo[5];
						$goods['catId'] = $goodsInfo[7];

						$this->add($goods);

					}
					fclose($fp);

				}else{
					return false;
				}
			 }
	}

	function clear(){
		$sql = "truncate qk_goods";
		if($this->db->query($sql)){
			return true;
		}
		return false;
	}

	function check($param){
		if(!is_array($param)||!($iid=$param['iid'])||!($id=$param['id'])){
			if($iid){
				$sql = "select id from qk_goods where iid='$iid'";
				return $this->db->getOne($sql);
			}
		}
		return false;
	}

	function clearGoods(){
		$sql = "delete from qk_goods";
		if($this->db->query($sql)){
			$this->msg='删除商品数据成功';return true;
		}else{
			$this->msg='删除商品数据失败';return false;
		}
	}
	function clearCategory(){
		$sql = "delete from qk_category";
		if($this->db->query($sql)){
			$this->msg='删除商品分类成功';return true;
		}else{
			$this->msg='删除商品分类失败';return false;
		}
	}
	function clearBrand(){
		$sql = "delete from qk_brand";
		if($this->db->query($sql)){
			$this->msg='删除商品品牌成功';return true;
		}else{
			$this->msg='删除商品品牌失败';return false;
		}
	}
	function clearAttr(){
		$sql = "delete from qk_goods_attr";
		if($this->db->query($sql)){
			$this->db->query("delete from qk_goods_attr_ext");$this->db->query("delete from qk_goods_attr_val");
			$this->msg='删除商品属性成功';return true;
		}else{
			$this->msg='删除商品属性失败';return false;
		}
	}
	function clearType(){
		$sql = "delete from qk_goods_type";
		if($this->db->query($sql)){
			$this->msg='删除商品类型成功';return true;
		}else{
			$this->msg='删除商品类型失败';return false;
		}
	}
	
}