<?php
/**
 * 自动取消拼团订单
 * @Copyright (C) 2018 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Common\Vendor\Cron;
class CancelPintuanOrder{	
	/**
     * 判断是否过期自动取消拼团订单
     */
    public function index($config_info){
		if(!empty($config_info)){
			$map=array();
			// 新加start
			$map['o.type'] = 6;
			$map['p.is_pt'] = 1;
			$map['o.pay_status'] = 3;
			$map['o.pay_time'] = array('gt',0);
			// end
			$map['o.shipping_status'] = 6;
			$map['o.status'] = 2;
			$map['o.pt_id'] = array('gt',0);
			$map['_string'] = "(o.confirm_time + p.group_time_limit * 3600) < ".NOW_TIME." OR p.end_time < ".NOW_TIME;
			$count=M('OrderInfo')->alias('o')->join('__PROMOT_ACT__ p on o.pt_id=p.id')->where($map)->count();
			
            // echo M('OrderInfo')->_sql();exit;
			if($count){
				$order_list=M('OrderInfo')->alias('o')->join('__PROMOT_ACT__ p on o.pt_id=p.id')->where($map)->field('o.confirm_time,p.group_time_limit,o.pt_id,o.id,p.end_time')->select();
				if(!empty($order_list)){
					foreach($order_list as $k=>$v){
						$begin_time=$v['confirm_time'];
						$group_time_limit=$v['group_time_limit'];
						$p_end=$v['end_time'];
						$end_time = $begin_time + ($group_time_limit * 3600);
						if($end_time > $p_end){$end_time = $p_end;}
						if($end_time < NOW_TIME){
							$data=array();
							$data['order_id']=$v['id'];
							$data['type']='admin';
							D('Common/OrderInfo')->cancel_order($data);
						}
					}
				}
			}
			//更新计划任务
			updatetask($config_info);        
		}else{return '';}
	}
}
?>