<?php
class IndexAction extends CommonAction
{
    public function index()
    {
        //开始域名判断
        $currentdoMain=$this->_server('HTTP_HOST');
        //简单的判断，复杂的以后再修改框架，单页面系统才能实现
        $currentdoMain = strtolower($currentdoMain);
        $currentdoMain = str_replace('www.', '', $currentdoMain);
        $currentdoMain = str_replace('flowerworld.com.cn', 'flowerworld.cn', $currentdoMain);
        $mainControl = array('flowerworld.cn','flowerworld.com.cn','flowerworld');
        if (in_array($currentdoMain, $mainControl))
        {                
            $domainUrl='http://'.$this->_server('HTTP_HOST').'/index/init/';
        }else{
            if($currentdoMain == 'flowerworld.org.cn'){
                R('Empty/wuye');die();
                //$domainUrl='http://www.'.$currentdoMain.'/empty/wuye/';
            }else{
                $member = M('Domaininfo')->where("lower(domainName)='{$currentdoMain}'")->getField('member');
                if($member)
                {
                    $domainUrl='http://www.'.$currentdoMain.'/com-'.$member.'/index.html';
                }else{
                    $domainUrl = M('Domainurl')->where("lower(domainName)='".$currentdoMain."'")->getField('url');
                    if(!$domainUrl)
                    {
                        $domainUrl='http://'.$this->_server('HTTP_HOST').'/index/init/';
                    }
                }

            }
        }
        $this->goDomainURL($domainUrl);
    }

    function goDomainURL($url)
    {
        $ch = curl_init();
        $timeout = 10;
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);

        if(empty($file_contents))
        {
            Header("HTTP/1.1 303 See Other");
            Header("Location: $url");
            die();
        }else{
            echo $file_contents;
            die();
        }
    }

    public function init()
    {  		
        $nowTime=getDate(time());
        $currentDate = $nowTime["year"]."年".$nowTime["mon"]."月".$nowTime["mday"]."日";
        $this->assign("currentDate",$currentDate);     
		//苗木产品
		$ml = D('Productclass');
		$mlclass = $ml->getAssignClass('116','6',' memberLevel in(1,2,3,10,11,14) and viewCount Desc ','enterprisename');
		//种苗产品1923
		$zmclass = $ml->getAssignClass('1923','6',' memberLevel in(1,2,3,10,11,14) and viewCount Desc ','enterprisename');
		//资材产品1410
		$zcclass = $ml->getAssignClass('1410','6',' memberLevel in(1,2,3,10,11,14) and viewCount Desc ');
 
		/*园林公司
		SELECT id,enterpriseName FROM	view_allmemberenterprise WHERE	(type1 IN (382, 383, 389, 409) OR type2 IN (382, 383, 389, 409)	OR type3 IN (382, 383, 389, 409))
        ORDER BY orderLevel DESC,lastLoginDate DESC,updateDate DESC;
		*/
		$where = ' type1 IN (382, 383, 389, 409) OR type2 IN (382, 383, 389, 409)	OR type3 IN (382, 383, 389, 409) ';
		$order = 'orderLevel DESC,lastLoginDate DESC,updateDate DESC';
		$yl = M('view_allmemberenterprise')->field('id,enterpriseName')->where($where)->order($order)->limit('9')->select();
		
		//园林工程 
		/*
			select rp.id,rp.member,rp.title,rpp.pic from routemap as rp  left join routemappic as rpp on rp.id=rpp.r_id where rp.member = 12346
		*/

		foreach($yl as $k=>$v){
			$sql = 'select rp.id,rp.member,rp.title,rpp.pic from routemap as rp  left join routemappic as rpp on rp.id=rpp.r_id where rp.member="'.$v['id'].'" group by rp.id order by rp.id DESC';
			//echo $sql;exit;
			$yl[$k]['gc'] = M()->query($sql);
		}
		
		//首页轮播图版
		/*$slid = M('Recommendlink')->field('id,logo,title,webUrl,orderNum')->where('groupId=31 and orderNum > 0')->order('orderNum desc,id desc')->limit('5')->select();
		*/

		//首页轮播图版改为使用广告位置管理的内容2016-06-27 add by kfq
		$slid = M('ad')->field('id,path as logo,title,linkUrl as webUrl,orderNum')->where("name='home_lundong_1' and status = 3")->order('orderNum desc,id desc')->limit('5')->select();

		$hotsearch = hot(16);

        //推荐企业
        $tj = recommend(32);

		//昨天发布求购信息条数
		$date1 = date('Y-m-d').' 00:00:00';
		$date2 = date('Y-m-d H:i:s',strtotime($date1)-86400);
		//$askCount = S('askCnt');		
		//if(!$askCount){
			$askCount = M('view_wit')->where('TO_DAYS(NOW()) - TO_DAYS(updateDate) <= 1')->getfield("count(updateDate)");
		//	S('askCnt',$askCount,86400);
		//}
		
		//昨天发布供应信息条数
		//$sellCount = S('sellCnt');
		//if(!$sellCount){
			$sellCount = M('sell')->where('TO_DAYS(NOW()) - TO_DAYS(updateDate) <= 1')->getfield("count(updateDate)");
		//	S('sellCnt',$sellCount,86400);
		//}
		//echo '<pre>';
		//print_r($yl);
		//print_r($zmclass);
		//exit;

		$this->assign('mlclass',$mlclass);
		$this->assign('zmclass',$zmclass);
		$this->assign('zcclass',$zcclass);
		$this->assign('yl',$yl);
		$this->assign('slid',$slid);
		$this->assign('hotsearch',$hotsearch);
		$this->assign('tj',$tj);
		$this->assign('askCount',$askCount);
		$this->assign('sellCount',$sellCount);

        $this->display('init');
    }

    function getTaoHuaData()
    {
        $taohua = S('taohua1');
        if(!$taohua)
        {
            require_once($_SERVER['DOCUMENT_ROOT']."/Public/taohua/rpcClient.class.php");
            $host = "www2.tao-hua.com";
            $rpc = new rpcClient($host);
            $result = $rpc->getData();

            if($result !== false)
            {

                /*
                *  产品对象属性描述
                *    $post->image        产品图片
                *    $post->postUrl        产品完整超链地址,直接在前面加HTTP引用即可
                *    $post->productName  产品名称
                *    $post->marketPrice    产品市场价,按实际情况决定是否显示
                *    $post->price        产品价格(实际购买价)
                */

                //以下为产品数据输出处理部分，只需要在此根据实际情况编写相应的处理代码即可

                $taohua = "<div class='taohua_down_right'><div class='taohua_down_right_frame'>";
                $i=0;
                foreach($result as $key =>  $post)
                {
                    if($key < 25)
                    {
                        $post->productName = iconv("GB2312","UTF-8",$post->productName);
                        $post->productName = truncate($post->productName,7,'');

                        if($key % 5 == 0)
                        {
                            $taohua .= "</div><div class='taohua_down_right_frame'><ul><li class='tu'><a href='http://".$post->postUrl."'><img src='http://".$post->image."' width='170' height='170' /></a><span><a class='s1' href='http://".$post->postUrl."'>".$post->productName."</a><a class='s2' href='http://".$post->postUrl."'>".$post->price."元</a></span></li>";
                            $i++;
                        }else{

                            $taohua .= "<li class='name'><a href='http://".$post->postUrl."'>&middot;&nbsp;".$post->productName."</a></li><li class='p'>".$post->price."元</li>";
                        }
                    }
                }
                $taohua .= "</div></div>";
                S('taohua1',$taohua,86400);
            }else
            {

                $taohua = "<div class='taohua_down_right'>";
                /*
                for($i=0;$i<4;$i++){
                $taohua .= "<div class='taohua_down_right_frame'><ul><li class='tu'></li><li class='name'><a href='#'>·浪漫情怀</a></li><li class='p'>888元</li><li class='name'><a href='#'>·浪漫情怀</a></li><li class='p'>888元</li><li class='name'><a href='#'>·浪漫情怀</a></li><li class='p'>888元</li><li class='name'><a href='#'>·浪漫情怀</a></li><li class='p'>888元</li></ul></div>";
                }
                */
                $taohua .= "花束数据读取异常，请稍候刷新尝试，";
                $taohua .= "</div>";
            }
 			
           
        }
        $this->ajaxReturn($taohua,"ok",1);
    }

    public function updatearea()
    {
        $model = $this->_post("model");
        $field = $this->_post("field");
        $field2 = $this->_post("field2");

        if($model && $field)
        {      
            if (is_string($model) && (!empty($model))) {
                if (!$obj = D($model))
                    $this->ajaxReturn(0,"{$model}不存在！",0);
            }
            if (is_object($obj)){
                $list = $obj->getField('id,'.$field);
                if($list)
                {   
                    foreach($list as $key => $val)
                    {
                        $obj->where('id='.$key)->setField($field2,getArea($val));
                    }
                    $info = '修改'.$model.'表所有'.$field.'成功';
                }
            }
        }
        $this->assign("model",$model);
        $this->assign("field",$field);
        $this->assign("field2",$field2);
        $this->assign("info",$info);
        $this->display();
    }

    public function recommendlink($id)
    {            
        $listTitle = M('Recommendlinkgroup')->where("id = %d",array($id))->getField('title');

        $field = 'id,title,webUrl,clickNum';
        $order = 'updateDate desc';
        $where = "groupId = ".intval($id);
        $rst = $this->getPage(M('Recommendlink'),$where,$field,$order,18);

        $this->assign('infolist', $rst['list']);
        $this->assign("page", $rst['page']);
        $this->assign("totalRow", $rst['count']); 

        $this->assign("listTitle", $listTitle); 
        $this->head_title($listTitle.'列表');
        $this->display();
    }

    public function lastCount()
    {
        $rst = array();
        $where = 'TO_DAYS(NOW())-TO_DAYS(createDate)<=3';
        $rst['sell_num'] = M('Sell')->where($where)->count();
        $rst['ask_buy_num'] = M('Wanttobuy')->where($where)->count();
        $rst['enterprise_num'] = M('ViewAllmemberenterprise')->where($where)->count();
        $rst['enterprise_num2'] = M('ViewAllmemberenterprise')->where($where.' and orderLevel > 5')->count();
        $rst['product_num'] = M('Productpost')->where($where)->count();
        $rst['news_num'] = M('Info')->where($where)->count();

        $this->ajaxReturn($rst,"ok",1);
    }
}
