where('ad_id',$ad_id)->find();
            $ad_info['start_time'] = date('Y-m-d H:i:s',$ad_info['start_time']);
            $ad_info['end_time'] = date('Y-m-d H:i:s',$ad_info['end_time']);
        }
        if($act == 'add')
           $ad_info['pid'] = $this->request->param('pid');
        $position = D('ad_position')->where('1=1')->select();
        $this->assign('info',$ad_info);
        $this->assign('act',$act);
        $this->assign('position',$position);
        $this->assign('erpid',getERPId());
        $this->assign('oldurl', $oldurl);
        $qclurl=QCLOUD_IMGURL;
        $this->assign('qclurl',$qclurl);
        return $this->fetch('',getAdmStoId());
    }
    public function adList(){
        delFile(RUNTIME_PATH.'Html'); // 先清除缓存, 否则不好预览
        $Ad =  M('ad');
        $pid = I('pid',0);
        $pagenum=20;//每页显示多少条
        if ((int)I('pagenum/s')>0)
        {
            $pagenum=I('pagenum/s');
        }
        $where['store_id'] = getAdmStoId();
        if($pid){
            $where['pid'] = $pid;
        	$this->assign('pid',I('pid'));
        }
        $keywords = I('keywords/s',false,'trim');
        if($keywords){
            $where['ad_name'] = array('like','%'.$keywords.'%');
        }
        $count = $Ad->where($where)->count();// 查询满足要求的总记录数
        $Page = $pager = new Page($count,$pagenum);// 实例化分页类 传入总记录数和每页显示的记录数
        $res = $Ad->where($where)->order('pid asc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $list = array();
        if($res){
        	$media = array('图片','文字','flash');
        	foreach ($res as $val){
        		$val['media_type'] = $media[$val['media_type']];
        		$list[] = $val;
        	}
        }
        $ad_position_list = M('AdPosition')->getField("position_id,position_name,is_open");
        $this->assign('ad_position_list',$ad_position_list);//广告位
        $show = $Page->show();// 分页显示输出
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$pager);
        $this->assign('pagenum',$pagenum);
        $this->assign('oldurl',urlencode(curPageURL()));
        return $this->fetch('',getAdmStoId());
    }
    public function position(){
        $act = I('get.act','add');
        $position_id = I('get.position_id/d');
        $info = array();
        if($position_id){
            $info = D('ad_position')->where('position_id',$position_id)->find();
        }
        $this->assign('info',$info);
        $this->assign('act',$act);
        return $this->fetch('',getAdmStoId());
    }
    public function positionList(){
        $Position =  M('ad_position');
        $count = $Position->where('1=1')->count();// 查询满足要求的总记录数
        $Page = $pager = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $list = $Position->order('position_id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$pager);
        return $this->fetch('',getAdmStoId());
    }
    public function adHandle(){
    	$data = I('post.');
    	$data['start_time'] = strtotime($data['begin']);
    	$data['end_time'] = strtotime($data['end']);
        $oldurl = I('oldurl/s');
        if($oldurl){
            $oldurl = urldecode($oldurl);
            $oldurl = urldecode($oldurl);
        }
        unset($data['oldurl']);
    	if($data['act'] == 'add'){
//            upload_ylp_log('增加广告');
            $data["store_id"]=getAdmStoId();
    		$r = D('ad')->add($data);
    	}
    	if($data['act'] == 'edit'){
//            upload_ylp_log('编辑广告');
            $data["store_id"]=getAdmStoId();
            unset($data['act']);
            unset($data['begin']);
            unset($data['end']);
            $q=D('ad')->where('ad_id', $data['ad_id'])->where($data)->find();
            if($q){
                if($oldurl){
                    $this->success('操作成功',$oldurl);
                }
                else {
                    $this->success("操作成功1", U('Admin/Ad/adList'));
                }
                exit;
            }
    		$r = D('ad')->where('ad_id', $data['ad_id'])->save($data);
    	}
    	if($data['act'] == 'del'){
//            upload_ylp_log('删除广告');
            $d=D('ad')->where('ad_id', $data['del_id'])->find();
            if($d){
                $url=$d['ad_code'];
                if(!empty($url)){
                    mdelFile(ROOT_PATH.$url);
                    vendor('qcloudcos.myqcloudcos');
                    $delres = Myqcloudcos::delFile('wxd', $url);
                }
               $r = D('ad')->where('ad_id', $data['del_id'])->delete();
                if($r) exit(json_encode(1));
            }else{
                $this->error("未找到该广告",$referurl);
                exit;
            }
    	}
    	/*--清除缓存--*/
        ClearALLCache();
        delFile(TEMP_PATH."/".getAdmStoId());
    	$referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Ad/adList');
        // 不管是添加还是修改广告 都清除一下缓存
        delFile(RUNTIME_PATH.'Html'); // 先清除缓存, 否则不好预览
    	if($r){
    		$this->success("操作成功",U('Admin/Ad/adList'));
    	}else{
    		$this->error("操作失败",$referurl);
    	}
    }
    public function positionHandle(){
        $data = I('post.');
        if($data['act'] == 'add'){
//            upload_ylp_log('增加广告位');
            $r = M('ad_position')->add($data);
        }
        if($data['act'] == 'edit'){
//            upload_ylp_log('编辑广告位');
        	$r = M('ad_position')->where('position_id',$data['position_id'])->save($data);
        }
        if($data['act'] == 'del'){
//            upload_ylp_log('删除广告位');
        	if(M('ad')->where('pid',$data['position_id'])->count()>0){
        		$this->error("此广告位下还有广告,请先清除",U('Admin/Ad/positionList'));
        	}else{
        		$r = M('ad_position')->where('position_id', $data['position_id'])->delete();
        		if($r) exit(json_encode(1));
        	}
        }
        $referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Ad/positionList');
        if($r){
        	$this->success("操作成功",$referurl);
        }else{
        	$this->error("操作失败",$referurl);
        }
    }
    public function changeAdField(){
//        upload_ylp_log('保存广告');
        $field = $this->request->request('field');
    	$data[$field] = I('get.value');
    	$data['ad_id'] = I('get.ad_id');
    	M('ad')->save($data); // 根据条件保存修改的数据
    }
	//商城首页广告
	public function shop_index(){
		//在页面渲染,无法直接使用,必须赋值给标签才能使用(json)
        $getmintime=date('Y-m-d 00:00:00',time());
        $getmaxtime=date('Y-m-d 23:59:59',time());
        $this->assign('getmaxtime',$getmaxtime);
        $this->assign('getmintime',$getmintime);
		$this->assign('erpid',getERPId());
//		upload_ylp_log('商城首页广告');
		return $this->fetch('',getAdmStoId());
	}
	//商城首页广告
	public function shop_user(){
		//在页面渲染,无法直接使用,必须赋值给标签才能使用(json)
        $getmintime=date('Y-m-d 00:00:00',time());
        $getmaxtime=date('Y-m-d 23:59:59',time());
        $this->assign('getmaxtime',$getmaxtime);
        $this->assign('getmintime',$getmintime);
		$this->assign('erpid',getERPId());
//		upload_ylp_log('个人中心广告');
		return $this->fetch('',getAdmStoId());
	}
    //积分兑换广告
    public function shop_integral(){
        $getmintime=date('Y-m-d 00:00:00',time());
        $getmaxtime=date('Y-m-d 23:59:59',time());
        $this->assign('getmaxtime',$getmaxtime);
        $this->assign('getmintime',$getmintime);
        $this->assign('erpid',getERPId());
        return $this->fetch('',getAdmStoId());
    }
    //客显屏幕广告
    public function erp_kxpm(){
        $getmintime=date('Y-m-d 00:00:00',time());
        $getmaxtime=date('Y-m-d 23:59:59',time());
        $this->assign('getmaxtime',$getmaxtime);
        $this->assign('getmintime',$getmintime);
        $this->assign('erpid',getERPId());
        return $this->fetch('',getAdmStoId());
    }
    public function search_strore()
    {
        $pickup_listid=I('pickup_listid');
        $where='';
        if ($pickup_listid){
            $newstr = substr($pickup_listid,0,strlen($pickup_listid)-1);
            $where="(pickup_id not in($newstr)) and";
        }
        $pagenum = 5;//每页显示多少条
        $getAdmStoId =getAdmStoId();
        $where = "$where store_id=" . $getAdmStoId;
        $kword = urldecode(urldecode(I('keywords/s')));
        //分页
        $intro = I('intro');
        if (!empty($intro)) {
            $where = "$where and " . I('intro') . " = 1";
            $this->assign('intro', $intro);
        }
        //搜索
        if (!empty($kword)) {
            $this->assign('keywords', $kword);
            $where = "$where and (pickup_no like '%" . $kword . "%' or pickup_name like '%" . $kword . "%')";
        }
        $count = M('pick_up')->where($where)->count();
        $Page = new Page($count, $pagenum);
        $stroreList = M('pick_up')->where($where)->order('pickup_id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $show = $Page->show();//分页显示输出
        $this->assign('page', $show);//赋值分页输出
        $this->assign('stroreList', $stroreList);
        $this->assign('pickup_listid', $pickup_listid);
        $this->assign('pager', $Page);//赋值分页输出
        $tpl = I('get.tpl', 'search_strore');
        return $this->fetch($tpl, getAdmStoId());
    }
    // 优惠促销增加,编辑
    public function prom_goods_info()
    {
        $prom_id = I('id','0');
        $store_id = getAdmStoId();
        $time = time();
        $info['start_time'] = date('Y-m-d H:i:s');
        $info['end_time'] = date('Y-m-d H:i:s', time() + 3600 * 60 * 24);
        $coupon = M('coupon')->where(['store_id' => $store_id, 'send_end_time' => ['gt', $time], 'type' => 5])->select();
        $gift = M('gift')->where(['store_id' => $store_id, 'is_end' => 0])->select();
        $libao=M('libao_form')->where(['store_id' => $store_id,'isdel'=>0,
            'startime'=>['lt', $time],'endtime' => ['gt', $time]])->select();
        //全场活动的开始时间
        $now = time();
        $wh = " store_id=" . $store_id . " and is_end=0 and prom_type=3 ".
            " and e_time>" . $now . " and good_object=0";
        $wh.=" and act_id<>".$prom_id;
        $fu = M("activitylist")->where($wh)->field('id,goods_listid,s_time,e_time')->find();
        if ($fu) {
            $this->assign('quan_s_time', date('Y-m-d H:i:s', $fu['s_time']));
            $this->assign('quan_e_time', date('Y-m-d H:i:s', $fu['e_time']));
        }
        //其他所有活动的结束时间,开始时间
        $wh1 = " store_id=" . $store_id . " and is_end=0".
            " and end_time>" . $now;
        $wh1.=" and id<>".$prom_id;
        $max=M("prom_goods")->where($wh1)->max('end_time');
        $min=M("prom_goods")->where($wh1)->min('start_time');
        if($max)
            $this->assign('zhiding_e_time', date('Y-m-d H:i:s',$max));
        if($min)
            $this->assign('zhiding_s_time', date('Y-m-d H:i:s',$min));
        //是否有全场的,包括指定商品不参与
        if(empty($prom_id)) {
            if ($fu) {
                if($fu['s_time']<$now) {
                    $this->assign('isfull', 1);
                    if (empty($fu['goods_listid'])) $this->assign('isall', 1);
                }
                else{
                    $this->assign('goods_listid', $fu['goods_listid']);
                }
            }
        }
        if ($prom_id > 0) {
            //是否有全场的,包括指定商品不参与
            if ($fu) {
                if($fu['s_time']<$now) {
                    $this->assign('isfull', 1);
                    if (empty($fu['goods_listid'])) $this->assign('isall', 1);
                }
                else{
                    $this->assign('goods_listid', $fu['goods_listid']);
                }
            }
            $is_full = 'false';
            $info = M('prom_goods')->where(['store_id' => $store_id, 'id' => $prom_id])->find();
            $info['start_time'] = date('Y-m-d H:i:s', $info['start_time']);
            $info['end_time'] = date('Y-m-d H:i:s', $info['end_time']);
            $list = M('prom_goods_list')->where('prom_id', $prom_id)->select();
            foreach ($list as $k => $v) {
                $list[$k]['preferential_type'] = json_decode($v['preferential_type'], true);
            }
            $this->assign('list', $list);
            // $prom_goods = M('goods')->where("prom_id=$prom_id and prom_type=3")->select();
            $activitylist = M('activitylist')->where("act_id=$prom_id and prom_type=3")->field('goods_listid')->find();
            if ($activitylist) {
                $prom_goods = M('goods')->where("goods_id", array('in', '0' . $activitylist['goods_listid'] . '0'))->order('goods_id DESC')->select();
            }
            $this->assign('prom_goods', $prom_goods);
        }
        $tpl = I('tpl', '');
        $this->assign([
            'coupon' => $coupon,
            'info' => $info,
            'gift' => $gift,
            'tpl' => $tpl,
            'libao' => $libao,
        ]);
        return $this->fetch('', getAdmStoId());
    }
	//商城首页广告数据
    public  function getgg()
    {
        $getstoid=getAdmStoId();
        $gettypid=I('typeid');
        $resgg=M('ad')->where(array('store_id'=>$getstoid,'pid'=>$gettypid))->order('orderby desc')->select();
        if ($resgg)
        {
            foreach ($resgg as $k=>$v){
                $pickup_listid=$resgg[$k]['pickup_listid'];
                if($pickup_listid != null){
                    $newstr = substr($pickup_listid,0,strlen($pickup_listid)-1);
                    $where="(pickup_id IN($newstr))";
                    $pickupList=M('pick_up')->where($where)->select();
                    if($pickupList){
                        $resgg[$k]['pick_list']=$pickupList;
                    }
                }
            }
            return json(['code'=>0,'data'=>json_encode($resgg)]);
        }
        else
        {
            return json(['code'=>1,'msg'=>'没有记录']);
        }
    }
	//商城首页广告添加和修改
	public function addEditAd(){
		$ad_id=I('ad_id');
		$typeid=I('typeid');
		$enable=I('ad_enabled');
		if($enable == null){
			$enable=1;
		}
		$data = I('post.');
		$data['store_id']=getAdmStoId();
    	$data['start_time'] = strtotime($data['start_time']);
    	$data['end_time'] = strtotime($data['end_time']);
		$data['pid'] = $typeid;
		$data['enabled']=$enable;
//		echo "alert('"+$data['pid']+"')";
		if(empty($ad_id)){
			//新增
			$r = D('ad')->add($data);
		}else{
			//修改
            upload_ylp_log('A11商城首页编辑/确认提交');
			$r = D('ad')->where('ad_id', $ad_id)->save($data);
		}
		/*--清除缓存--*/
        ClearALLCache();
        delFile(TEMP_PATH."/".getAdmStoId());
		if($r){
    		$this->success("操作成功",U('Admin/Ad/shop_index'));
    	}else{
    		$this->error("操作失败");
    	}
	}
	//商城个人中心添加和修改广告
	public function addEditAd_user(){
		$ad_id=I('ad_id');
		$typeid=I('typeid');
		$enable=I('ad_enabled');
		$data = I('post.');
		$data['store_id']=getAdmStoId();
    	$data['start_time'] = strtotime($data['start_time']);
    	$data['end_time'] = strtotime($data['end_time']);
		$data['pid'] = $typeid;
		$data['enabled']=$enable;
//		var_dump($data['enabled']);
//		die();
//		echo "alert('"+$data['pid']+"')";
		if(empty($ad_id)){
			//新增
			$r = D('ad')->add($data);
		}else{
			//修改
            upload_ylp_log('A12个人中心页编辑/确认提交');
			$r = D('ad')->where(array('ad_id'=>$ad_id))->save($data);
		}
		/*--清除缓存--*/
        ClearALLCache();
        delFile(TEMP_PATH."/".getAdmStoId());
		if($r){
    		$this->success("操作成功",U('Admin/Ad/shop_user'));
    	}else{
    		$this->error("操作失败");
    	}
	}
	//删除所有广告
	public function delAd(){
//	    upload_ylp_log('删除所有广告');
//		$typeid=$_GET['typeid'];
		$typeid=I('typeid');
		$d=D('ad')->where(array('pid'=>$typeid,'store_id'=>getAdmStoId()))->find();
            if($d){
                $url=$d['ad_code'];
                if(!empty($url)){
                    mdelFile(ROOT_PATH.$url);
                    vendor('qcloudcos.myqcloudcos');
                    $delres = Myqcloudcos::delFile('wxd', $url);
                }
				$r=D('ad')->where(array('pid'=>$typeid,'store_id'=>getAdmStoId()))->delete();
				/*--清除缓存--*/
        		ClearALLCache();
        		delFile(TEMP_PATH."/".getAdmStoId());
                if($r){
                	$this->success("操作成功",U('Admin/Ad/shop_index'));
                }
            }else{
                exit;
            }
	}
//删除轮播
	public function delAdid(){
//	    upload_ylp_log('删除广告轮播');
			$ad_id=$_GET['id'];
			$d=D('ad')->where(array('ad_id'=>$ad_id,'store_id'=>getAdmStoId()))->find();
	            if($d){
	                $url=$d['ad_code'];
	                if(!empty($url)){
	                    vendor('qcloudcos.myqcloudcos');
	                    $urllist=explode(',',$url);
                        $url_len = count($urllist);
                        for ($i = 0; $i < $url_len; $i++) {
                            mdelFile(ROOT_PATH . $urllist[$i]);
                            $delres = Myqcloudcos::delFile('wxd', $urllist[$i]);
                        }
	                }
					$r=D('ad')->where(array('ad_id'=>$ad_id,'store_id'=>getAdmStoId()))->delete();
					/*--清除缓存--*/
	        		ClearALLCache();
	        		delFile(TEMP_PATH."/".getAdmStoId());
	                if($r){
	              		$this->success("操作成功",U('Admin/Ad/shop_index'));
						exit;
	                }
	            }else{
	                exit;
	            }
		}
		//删除单个广告
    public function delId_sigle()
    {
        vendor('qcloudcos.myqcloudcos');
        $ad_id = $_GET['id'];
        $imgurl = $_GET['imgurl'];
        $getadcode = $_GET['ad_code'];
        if ($ad_id) {
            $d = D('ad')->where(array('ad_id' => $ad_id, 'store_id' => getAdmStoId()))->find();
            if ($d) {
                $url = $d['ad_code'];
            }
        } else {
            $url = $getadcode;
        }
        //
        if ($url) {
            $urllist = explode(',', $url);
            $url_len = count($urllist);
            $newurl = "";
            for ($i = 0; $i < $url_len; $i++) {
                if ($urllist[$i] == $imgurl) {
                    if ($imgurl) {
                        mdelFile(ROOT_PATH . $urllist[$i]);
                        $delres = Myqcloudcos::delFile('wxd', $urllist[$i]);
                    }
                } else {
                    if ($urllist[$i]) {
                        if ($newurl) {
                            $newurl .= "," . $urllist[$i];
                        } else {
                            $newurl = $urllist[$i];
                        }
                    }
                }
            }
            if ($ad_id) {
                $r = D('ad')->where(array('ad_id' => $ad_id, 'store_id' => getAdmStoId()))->save(array('ad_code' => $newurl));
                /*--清除缓存--*/
                ClearALLCache();
                delFile(TEMP_PATH . "/" . getAdmStoId());
                if ($r) {
                    return json(array('code' => 1, 'msg' => '删除成功!', 'urllist' => $newurl));
                }
            }
            else
            {
                return json(array('code' => 1, 'msg' => '删除成功!', 'urllist' => $newurl));
            }
        }
        //
    }
    //积分兑换添加和修改广告
    public function addEditAd_integral(){
        $ad_id=I('ad_id');
        $typeid=I('typeid');
        $enable=I('ad_enabled');
        $data = I('post.');
        $data['store_id']=getAdmStoId();
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        $data['pid'] = $typeid;
        $data['enabled']=$enable;
        if(empty($ad_id)){
            //新增
            $r = D('ad')->add($data);
        }else{
            //修改
            upload_ylp_log('A12积分兑换页编辑/确认提交');
            $r = D('ad')->where(array('ad_id'=>$ad_id))->save($data);
        }
        /*--清除缓存--*/
        ClearALLCache();
        delFile(TEMP_PATH."/".getAdmStoId());
        if($r){
            $this->success("操作成功",U('Admin/Ad/shop_integral'));
        }else{
            $this->error("操作失败");
        }
    }
    //客显屏幕广告添加和修改广告
    public function addEditAd_erpkxpm(){
        $ad_id=I('ad_id');
        $typeid=I('typeid');
        $enable=I('ad_enabled');
        $data = I('post.');
        $intype=$data['intype'];
        if($intype==0){
            $data['pickup_listid']='';
        }
        $data['store_id']=getAdmStoId();
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        $data['pid'] = $typeid;
        $data['enabled']=$enable;
        if(empty($ad_id)){
            //新增
            $r = D('ad')->add($data);
        }else{
            //修改
            upload_ylp_log('A12客显屏幕页编辑/确认提交');
            $r = D('ad')->where(array('ad_id'=>$ad_id))->save($data);
        }
        /*--清除缓存--*/
        ClearALLCache();
        delFile(TEMP_PATH."/".getAdmStoId());
        if($r){
            $this->success("操作成功",U('Admin/Ad/erp_kxpm'));
        }else{
            $this->error("操作失败");
        }
    }
}