<?php
namespace app\plan\controller;
use think\Controller;
use think\Db;
class Index extends Base{

   
   //定时关闭超时未支付订单
     public function index(){  
      
       $perfix=config("database.prefix");
	   $now=time();
	   $sql="UPDATE ".$perfix."order SET status=5 WHERE `expire_time` < ".$now;				
        $arr=Db::query($sql);
		if(count($arr)>0){
			   $res["code"]=1;
				$res["from"]="bymall";
				header('Content-Type:application/json; charset=utf-8');
				print_r( json_encode($res,JSON_UNESCAPED_UNICODE));
			
		}
      else{    $res["code"]=0;
				$res["from"]="ifmall";
				header('Content-Type:application/json; charset=utf-8');
				print_r( json_encode($res,JSON_UNESCAPED_UNICODE));
		}
		
      }

    //定时关闭收货超时未确认订单
    public function order(){  
      
       $perfix=config("database.prefix");
	   $now=time()-1296000;//15天自动确认签收订单
	   $sql="UPDATE ".$perfix."order SET status=3 WHERE `send_time` < ".$now."and status=2";				
       $arr=Db::query($sql);
	   
	  
	   
		//关闭过期未使用优惠券
		 $now=time();
	     $sql="UPDATE ".$perfix."user_coupon SET status=2 WHERE `end_time` < ".$now;				
        $arr=Db::query($sql);
		
		
		if(count($arr)>0){
			   $res["code"]=1;
		       $res["from"]="yer";
			   header('Content-Type:application/json; charset=utf-8');
			    print_r( json_encode($res,JSON_UNESCAPED_UNICODE));
			
		}
      else{   
			  $res["code"]=0;
				$res["from"]="yer";
				header('Content-Type:application/json; charset=utf-8');
				print_r( json_encode($res,JSON_UNESCAPED_UNICODE));
		}
	}
    //定时导出未发货订单
   public function out(){  
		 
			$xlsName  = "order";      
			$xlsCell  = array(
				array('id','编号'),
				array('order_sn','订单编号'),
				array('total_money','总金额'),
				array('saleslist','订单内容'),
				array('username','联系人'),
				array('mobile','联系方式'), 
				array('address','地址'), 
				array('status','订单状态'),
				array('score_money','积分抵消金额'),
				array('express','快递名称'), 
				array('express_code','快递单号'),
				array('update_time','支付完成时间'),
				array('send_time','发货时间'), 
				array('confirm_time','确认时间'), 
				array('uid','用户uid')
			);  
		  
		    $map[]=["status",'<',1];
			$map=isset($map)?$map:'';
			$xlsModel = db($xlsName);
			$xlsData  = $xlsModel->where($map)->select(); 
			foreach($xlsData as &$val){
				  //$val["update_time"]=isset($val["update_time"])?date("Y-m-d H:i:s",$val["update_time"]):"";
				  $val["send_time"]=isset($val["send_time"])?date("Y-m-d H:i:s",$val["send_time"]):""; 
				  $val["confirm_time"]=isset($val["confirm_time"])?date("Y-m-d H:i:s",$val["confirm_time"]):"";
				  $val["status"]=isset($val["status"])?$this->parse($val["status"]):"";
				  $val["paytype"]=isset($val["paytype"])?$this->parsePaytype($val["paytype"]):"";
				 
			}      
			if(!$xlsData){               
				 $this->error('无数据');
			}
		    outExcel($xlsName,$xlsCell,$xlsData); 
		
	}
		/**
     * 解析状态
     */
	 public function parse($status){   
	       switch ($status)
				{
				case 0:
				  return "待支付";
				  break;
				  
				case 1:
				  return "待发货";
				  break;
				case 2:
				  return "已发货";
				  break;
				case 3:
				  return "已确认";
				case 4:
				  return "已完成";
				  break;
				case 5:
				  return "超时取消";
				case 6:
				  return "已取消";
				  break;
				default:
				  return  "未支付";
				}
	}
	/**
     * 解析支付方式
     */
	 public function parsePaytype($paytype){   
	       switch ($paytype)
				{
				case 1:
				  return "余额支付";
				  break;
				case 2:
				  return "微信支付";
				  break;
                 case 3:
				  return "支付宝wap";
				  break;
				case 4:
				  return "支付宝";
				  break;
				 case 5:
				  return "货到付款";
				  break;
				default:
				  return  "未支付";
				}
	}
	 //定时自动发货先发货后导出订单，
    public function auto(){  
      
       $perfix=config("database.prefix");
	   $now=time();
	   $sql="UPDATE ".$perfix."order SET status=5 WHERE `expire_time` < ".$now;				
       $arr=Db::query($sql);
	   if(is_countable($arr)&&count($arr)>0){
			dump($arr);
			
	   }
       else{
		   dump($sql);
	   }
		
	}
	


	
}
