<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 采集淘宝店铺数据
 * @author     Jade Xia
 * @copyright  Copyright (c) 2010-2011 天夏网络
 */
 
class Get_top_data extends Controller
{
	function Get_top_data()
	{
		parent::Controller();
		$this->load->database();
		$this->load->library('TopClient','','top');
	}
	
	//采集数据视图页面
	function index()
	{
		check_is_login();
		$this->load->view(TPL_FOLDER.'get_top_data');
	}
	
	//获取数据采集摘要
	function get_summary()
	{
		header("Content-Type:application/json");
		$md5key = $this->input->post('md5key');
		if($md5key != md5($this->config->item('md5_encode_key'))) die('{}');
		$nick = unescape($this->input->post('nick'));
		if( ! $nick) die('{}');
		$this->load->library('top/SellercatsListGetRequest','','req');
		$this->req->setFields("cid");
		$this->req->setNick($nick);
		$resp = $this->top->execute($this->req);
		$json = '{';
		if($resp) $json .= 'cat_total:'.count($resp['seller_cats']['seller_cat']).',';
		else $json .= 'cat_total:0,';
		$this->load->library('top/ItemsGetRequest','','req');
		$this->req->setFields("num_iid");
		$this->req->setNicks($nick);
		$resp = $this->top->execute($this->req);
		if($resp) $json .= 'goods_total:'.$resp['total_results'];
		else $json .= 'goods_total:0';
		$json .= '}';
		echo $json;
	}
	
	//ajax 控制台
	function ajax_make()
	{
		$md5key = $this->input->post('md5key');
		if($md5key != md5($this->config->item('md5_encode_key'))) die('0');
		$mitem=$this->input->post('mitem');
		$nick=unescape($this->input->post('nick'));
		$page_no=$this->input->post('page_no');
		if(!$mitem) die('0');
		switch($mitem)
		{
			case 'get_cat':
				$this->_get_seller_cats($nick);
				break;
			case 'get_goods':
				$this->_get_goods_items($nick,$page_no);
				break;	
		}
	}
	
	//获取店铺内卖家自定义类目
	function _get_seller_cats($nick)
	{
		if(! $nick) die('0');
		$this->load->library('top/SellercatsListGetRequest','','req');
		$this->req->setFields("type,cid,parent_cid,name,pic_url,sort_order");
		$this->req->setNick($nick);
		$resp = $this->top->execute($this->req);
		if($resp)
		{
			foreach($resp['seller_cats']['seller_cat'] as $row)
			{
				$query = $this->common_model->get_record('SELECT id FROM '.$this->db->dbprefix.'shop_product_catalog WHERE cid = ?',array($row['cid']));
				if($query)
				{
					$data = array(
						'cat_name' => $row['name']
					);
					if($row['pic_url']) $data['pic_path'] = $row['pic_url'];
					$this->db->update('shop_product_catalog',$data,array('id'=>$query->id));
				}
				else
				{
					$data = array(
						'cid' => $row['cid'],
						'cat_name' => $row['name'],
						'seqorder' => $row['sort_order'],
						'is_has_chd' => 0
					);
					if($row['pic_url']) $data['pic_path'] = $row['pic_url'];
					if($row['parent_cid'] == 0)
					{
						$data['parent_id'] = 0;
						$data['deep_id'] = 0;
					}
					else
					{
						$query = $this->common_model->get_record('SELECT id,deep_id FROM '.$this->db->dbprefix.'shop_product_catalog WHERE cid = ?',array($row['parent_cid']));
						$data['parent_id'] = $query->id;
						$data['deep_id'] = $query->deep_id + 1;
						$this->db->update('shop_product_catalog',array('is_has_chd'=>1),array('id'=>$query->id));
					}
					$this->db->insert('shop_product_catalog',$data);
					
					$query = $this->common_model->get_record('SELECT max(id) AS id FROM '.$this->db->dbprefix.'shop_product_catalog');
					$id = $query->id;
					if($row['parent_cid'] == 0)
					{
						$data = array(
							'queue' => ','.$id.','
						);
						$this->db->update('shop_product_catalog',$data,array('id'=>$id));
					}
					else
					{
						$query = $this->common_model->get_record('SELECT queue FROM '.$this->db->dbprefix.'shop_product_catalog WHERE cid = ?',array($row['parent_cid']));
						$queue = $query->queue;
						$data = array(
							'queue' => $queue.$id.','
						);
						$this->db->update('shop_product_catalog',$data,array('id'=>$id));
					}
				}
			}
			echo '1';
		}
		else
		{
			echo '0';
		}
	}
	
	//获取商品
	function _get_goods_items($nick,$page_no = 1)
	{
		if(! $nick) die('0');
		$this->load->library('top/ItemsGetRequest','','req');
		$this->load->library('top/ItemsListGetRequest','','ireq');
		$this->req->setFields("num_iid");
		if($page_no > 1) $this->req->setPageNo($page_no);
		$this->req->setNicks($nick);
		$resp = $this->top->execute($this->req);
		if($resp)
		{
			$num_iids = array_chunk($resp['items']['item'],20);
			$num_str = '';
			foreach($num_iids as $row)
			{
				$num_arr = array();
				foreach($row as $v)
				{
					$num_arr[] = $v['num_iid'];
				}
				if($num_arr) 
				{
					$num_str = implode(',',$num_arr);
					$this->ireq->setFields("num_iid,title,price,type,desc,created,cid,seller_cids,props,input_pids,input_str,pic_url,num,item_img");
					$this->ireq->setNumIids($num_str);
					$iresp = $this->top->execute($this->ireq);
					if($iresp)
					{
						foreach($iresp['items']['item'] as $v)
						{
							$query = $this->common_model->get_record('SELECT id FROM '.$this->db->dbprefix.'shop_product WHERE num_iid = ?',array($v['num_iid']));
							if($query)
							{
								$data = array(
									'title' => $v['title'],
									'cid' => $v['cid'],
									'props' => $v['props'],
									'kucun' => $v['num'],
									'content' => $v['desc'],
									'market_price' => $v['price'],
									'small_pic_path' => $v['pic_url'],
									'big_pic_path' => $v['pic_url']
								);
								$this->db->update('shop_product',$data,array('id'=>$query->id));
							}
							else
							{
								$data = array(
									'num_iid' => $v['num_iid'],
									'title' => $v['title'],
									'cid' => $v['cid'],
									'props' => $v['props'],
									'kucun' => $v['num'],
									'keyword' => $v['title'],
									'description' => $v['title'],
									'content' => $v['desc'],
									'market_price' => $v['price'],
									'small_pic_path' => $v['pic_url'],
									'big_pic_path' => $v['pic_url'],
									'create_date' => time()
								);
								if(isset($v['seller_cids'])) 
								{
									$catalog_id = trim($v['seller_cids'],',');
									$query = $this->common_model->get_records('SELECT queue FROM '.$this->db->dbprefix.'shop_product_catalog WHERE cid IN ('.$catalog_id.')');
									$qarr = array();
									foreach($query as $qrow)
									{
										$qarr[] = trim($qrow->queue,',');
									}
									if($qarr)
									{
										$catalog_id = ','.implode(',',array_unique(explode(',',implode(',',$qarr)))).',';
										$data['catalog_id'] = $catalog_id;
									}
								}
								$this->db->insert('shop_product',$data);
								if(isset($v['item_imgs']['item_img']) && count($v['item_imgs']['item_img']) > 1)
								{
									$query = $this->common_model->get_record('SELECT MAX(id) AS id FROM '.$this->db->dbprefix.'shop_product');
									$i = 1;
									foreach($v['item_imgs']['item_img'] as $iv)
									{
										if($i > 1)
										{
											$data = array(
												'product_id' => $query->id,
												'pic_path' => $iv['url']
											);
											$this->db->insert('shop_product_image',$data);
										}
										$i++;
									}
								}
							}
						}
					}
				}
				sleep(1);
			}
			echo '1';
		}
		else
		{
			echo '0';
		}
	}
	
	//获取行业类目
	function get_industry_cats()
	{
		header("Content-Type:application/json");
		$md5key = $this->input->post('md5key');
		if($md5key != md5($this->config->item('md5_encode_key'))) die('{}');
		$p_id = $this->input->post('p_id');
		if( ! $p_id) $p_id = 0;
		$this->load->library('top/ItemcatsGetRequest','','req');
		$this->req->setFields("cid,parent_cid,name,is_parent,sort_order");
		$this->req->setParentCid($p_id);
		$resp = $this->top->execute($this->req);
		if($resp)
		{
			$json = '{';
			foreach($resp['item_cats']['item_cat'] as $row)
			{
				$json .= $row['cid'].':"'.$row['name'].'",';
				$query = $this->common_model->get_record('SELECT id FROM '.$this->db->dbprefix.'shop_industry_catalog WHERE cid = ?',array($row['cid']));
				if($query)
				{
					$data = array(
						'cat_name' => $row['name']
					);
					$this->db->update('shop_industry_catalog',$data,array('id'=>$query->id));
				}
				else
				{
					$data = array(
						'cid' => $row['cid'],
						'cat_name' => $row['name'],
						'seqorder' => $row['sort_order'],
						'is_has_chd' => 0
					);
					if($row['parent_cid'] == 0)
					{
						$data['parent_id'] = 0;
						$data['deep_id'] = 0;
					}
					else
					{
						$query = $this->common_model->get_record('SELECT id,deep_id FROM '.$this->db->dbprefix.'shop_industry_catalog WHERE cid = ?',array($row['parent_cid']));
						if($query)
						{
							$data['parent_id'] = $query->id;
							$data['deep_id'] = $query->deep_id + 1;
							$this->db->update('shop_industry_catalog',array('is_has_chd'=>1),array('id'=>$query->id));
						}
						else
						{
							$data['parent_id'] = 0;
							$data['deep_id'] = 0;
						}
					}
					$this->db->insert('shop_industry_catalog',$data);
					
					$query = $this->common_model->get_record('SELECT max(id) AS id FROM '.$this->db->dbprefix.'shop_industry_catalog');
					$id = $query->id;
					if($row['parent_cid'] == 0)
					{
						$data = array(
							'queue' => ','.$id.','
						);
						$this->db->update('shop_industry_catalog',$data,array('id'=>$id));
					}
					else
					{
						$query = $this->common_model->get_record('SELECT queue FROM '.$this->db->dbprefix.'shop_industry_catalog WHERE cid = ?',array($row['parent_cid']));
						$queue = $query->queue;
						$data = array(
							'queue' => $queue.$id.','
						);
						$this->db->update('shop_industry_catalog',$data,array('id'=>$id));
					}
				}
			}
			$json = rtrim($json,',');
			$json .= '}';
		}
		else
		{
			$json = '{}';
		}
		sleep(5);
		echo $json;
	}
	
	//采集店铺类目
	function get_shop_cats()
	{
		check_is_login();
		$this->load->library('top/ShopcatsListGetRequest','','req');
		$this->req->setFields("cid,parent_cid,name,is_parent");
		$resp = $this->top->execute($this->req);
		if($resp)
		{
			foreach($resp['shop_cats']['shop_cat'] as $row)
			{
				$query = $this->common_model->get_record('SELECT id FROM '.$this->db->dbprefix.'shop_catalog WHERE cid = ?',array($row['cid']));
				if($query)
				{
					$data = array(
						'cat_name' => $row['name']
					);
					$this->db->update('shop_catalog',$data,array('id'=>$query->id));
				}
				else
				{
					$data = array(
						'cid' => $row['cid'],
						'cat_name' => $row['name'],
						'parent_id' => 0,
						'seqorder' => 0,
						'is_has_chd' => 0,
						'deep_id' => 0
					);
					$this->db->insert('shop_catalog',$data);
					
					$query = $this->common_model->get_record('SELECT max(id) AS id FROM '.$this->db->dbprefix.'shop_catalog');
					$id = $query->id;
					$data = array(
						'queue' => ','.$id.','
					);
					$this->db->update('shop_catalog',$data,array('id'=>$id));
				}
			}
		}
		echo_msg('<li>店铺类目采集完毕</li>','','yes');
	}
	
	//采集行业类目页
	function get_industry()
	{
		check_is_login();
		$this->load->view(TPL_FOLDER.'get_industry');
	}
}
?>