<?php
/**
 * 订单管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author zhaoh <1070309178@qq.com>
 */

namespace Mobile\Controller;
use Think\Controller;
class OrderInfoController extends BaseController {
    public function _initialize(){
    parent::_initialize ();
        if(!MID){
            cookie('forward',U('Mobile/'.CONTROLLER_NAME.'/'.ACTION_NAME));
            $this->redirect('Mobile/Login/index');
        }
        if($this->m_info['is_tmp']==1&&!$this->m_info['mobile']){
            session('format_url',U('Mobile/'.CONTROLLER_NAME.'/'.ACTION_NAME));
            $this->redirect('Mobile/Login/perfect');
        }elseif($this->m_info['is_tmp']==1&&$this->m_info['mobile']){
            $this->error('您已经领取过会员卡了！');exit;
        }
        $this->assign('uid',MID);
        $this->assign('tplseo_title','我的订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','我的订单');
    }
	//全部订单
    public function index($is_ajax = 0){
        $postdata=I('param.');
        if($postdata['no_ship']){
            $type=3;
            $this->assign('tplseo_title','待发货订单'.'_'.C('WEB_SITE_TITLE'));
            $this->assign('page_title','待发货订单');
        }
        $order_list=D('Common/OrderInfo')->all_order($postdata,$type,'',10,'',15);
        //print_r($order_list);
        if ($is_ajax == 1) return $order_list;
        $this->assign($order_list);
        $this->assign($postdata);
        $list_html=$this->fetch('index_list');
        $this->assign('list_html',$list_html);
        $this->display();
    }
	//退款订单
    public function tui_kuan($is_ajax = 0){
        $postdata=I('param.');
        if($postdata['no_ship']){$type=3;}else{$type = 9;}
        $order_list=D('Common/OrderInfo')->all_order($postdata,$type,'',10,'',15);
        if ($is_ajax == 1) return $order_list;
        $this->assign($order_list);
        $this->assign($postdata);
        $list_html=$this->fetch('index_list');
        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','退款/退货'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','退款/退货');
        $this->display();
    }
    public function get_index_list(){
        $return['status'] = 0;
        $data = $this->index(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('index_list');
        $return['data'] = $list_html;
		$return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
    public function takeout_index_list(){
        $return['status'] = 0;
        $data = $this->index(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('takeout_index_list');
        $return['data'] = $list_html;
        $return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
	//待付款订单
    public function no_pay($is_ajax = 0){
        $postdata=I('param.');
        $order_list=D('Common/OrderInfo')->all_order($postdata,2,'',10);
    // print_r($order_list);exit;
        if ($is_ajax == 1) return $order_list;

        $this->assign($order_list);

        $list_html=$this->fetch('index_list');

        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','待付款订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','待付款订单');
	    $this->display();
    }
    public function get_no_pay_list(){
        $return['status'] = 0;
        $data = $this->no_pay(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('index_list');
        $return['data'] = $list_html;
		$return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
    //外卖待付款订单
    public function takeout_no_pay($is_ajax = 0){
        $postdata=I('param.');
        $order_list=D('Common/OrderInfo')->all_order($postdata,2,'',10,true,14);
    // print_r($order_list);exit;
        if ($is_ajax == 1) return $order_list;

        $this->assign($order_list);

        $list_html=$this->fetch('takeout_index_list');

        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','待付款订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','待付款订单');
	    $this->display();
    }
    //待成团订单
    public function no_group($is_ajax = 0){
        $postdata=I('param.');
        $order_list=D('Common/OrderInfo')->all_order($postdata,6,'',10,'',6);
        if ($is_ajax == 1) return $order_list;
        $this->assign($order_list);
        $list_html=$this->fetch('group_list');
        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','待成团订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','待成团订单');
        $this->assign('pt',1);
        $this->display();
    }
    public function get_no_group_list(){
        $return['status'] = 0;
        $data = $this->no_group(1);
        if (empty($data['list'])) $this->ajaxReturn($return);
        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('group_list');
        $return['data'] = $list_html;
		$return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
    //已成团订单
    public function group($is_ajax = 0){
        $postdata=I('param.');
        $order_list=D('Common/OrderInfo')->all_order($postdata,'group','',10,'',6);
        if ($is_ajax == 1) return $order_list;
        $this->assign($order_list);
        $list_html=$this->fetch('group_list');
        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','已成团订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','已成团订单');
        $this->assign('pt',1);
        $this->display();
    }
	public function get_group_list(){
        $return['status'] = 0;
        $data = $this->group(1);
        if (empty($data['list'])) $this->ajaxReturn($return);
        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('group_list');
        $return['data'] = $list_html;
		$return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
    // 外卖未支付订单列表
    public function takeout_no_pay_list(){
        $return['status'] = 0;
        $data = $this->no_pay(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('takeout_index_list');
        $return['data'] = $list_html;
		$return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
    //待收货订单
    public function yes_ship($is_ajax = 0){
        $postdata=I('param.');
        $order_list=D('Common/OrderInfo')->all_order($postdata,4,'',10);
        if ($is_ajax == 1) return $order_list;
        $this->assign($order_list);
        $list_html=$this->fetch('index_list');
        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','待收货订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','待收货订单');
        $this->display();
    }
    //外卖待配送订单
    public function takeout_no_ship($is_ajax = 0){
        $postdata=I('param.');
        $order_list=D('Common/OrderInfo')->all_order($postdata,8,'',10,true,14);
        if ($is_ajax == 1) return $order_list;
        $this->assign($order_list);
        $list_html=$this->fetch('takeout_index_list');
        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','待配送订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','待配送订单');
        $this->display();
    }
    //待收货订单
    public function takeout_yes_ship($is_ajax = 0){
        $postdata=I('param.');
        $order_list=D('Common/OrderInfo')->all_order($postdata,4,'',10,true,14);
        if ($is_ajax == 1) return $order_list;
        $this->assign($order_list);
        $list_html=$this->fetch('takeout_index_list');
        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','待收货订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','待收货订单');
        $this->display();
    }
    //服务订单
    public function service_order($is_ajax = 0){
        $postdata=I('param.');
        $order_list=D('Common/OrderInfo')->all_order($postdata,7,'',10,'',7);
        if ($is_ajax == 1) return $order_list;
        $this->assign($order_list);
        $list_html=$this->fetch('index_list');
        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','服务订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','服务订单');
        $this->display();
    }
    public function get_service_order_list(){
        $return['status'] = 0;
        $data = $this->service_order(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('index_list');
        $return['data'] = $list_html;
		$return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
    public function get_yes_ship_list(){
        $return['status'] = 0;
        $data = $this->yes_ship(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('index_list');
        $return['data'] = $list_html;
		$return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
    // 更多外卖待收货订单
    public function takeout_yes_ship_list(){
        $return['status'] = 0;
        $data = $this->yes_ship(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('takeout_index_list');
        $return['data'] = $list_html;
		$return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
    // 更多外卖待配送订单
    public function takeout_no_ship_list(){
        $return['status'] = 0;
        $data = $this->takeout_no_ship(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('takeout_index_list');
        $return['data'] = $list_html;
		$return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
    //待评价订单
    public function no_comment($is_ajax = 0){
        $postdata=I('param.');
        $order_list=D('Common/OrderInfo')->all_order($postdata,5,'',10);
        if ($is_ajax == 1) return $order_list;
        $this->assign($order_list);
        $list_html=$this->fetch('index_list');
        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','待评价订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','待评价订单');
        $this->display();
    }
	//外卖订单
    public function takeout($is_ajax = 0){
        $postdata=I('param.');
        $order_list=D('Common/OrderInfo')->all_order($postdata,'','',10,true,14);
        if ($is_ajax == 1) return $order_list;

        $this->assign($order_list);
        $list_html=$this->fetch('takeout_index_list');
        $this->assign('list_html',$list_html);
        $this->assign('tplseo_title','外卖订单'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','外卖订单');
        $this->display();
    }
    public function get_no_comment_list(){
        $return['status'] = 0;
        $data = $this->no_comment(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('index_list');
        $return['data'] = $list_html;
		$return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
    //订单详情
    public function order_info(){
        $id=I('param.id');
        $order_info=M('OrderInfo')->where('id='.$id)->field(true)->find();
        if($order_info['is_delete']==1)$this->redirect('OrderInfo/index');
        $order_goods=M('OrderGoods')->where('order_id='.$id)->field(true)->select();
        foreach($order_goods as $k=>$v){
              $attr=unserialize($v['goods_attr']);
              $order_goods[$k]['img']=idtoname($v['goods_id'],'Goods','img');
              $order_goods[$k]['attr_name']=$attr['name'];
        }unset($k,$v);
		
        if($order_info['type'] == 11){
              $service_type = idtoname($order_info['id'],'MemberPackage','service_type','order_id',false);
              if($service_type == 1){
                    // $map['id'] = idtoname($order_info['id'],'MemberPackage','goods_id','order_id',false);
                    // $shop_id = M('Goods')->where($map)->getField('shop_id');
                    // if(!$shop_id){$shop_id = getShopid();}
                    $shop_id= $order_info['shop_id'];
                    $shop_address = idtoname($shop_id,'Shops','address','id',false);
                    $shopname = idtoname($shop_id,'Shops','name','id',false);
                    $shopmobile = idtoname($shop_id,'Shops','phone','id',false);

                    $html .= '<dd><h3>'.$shopname.'</h3><p><span class="f_r"><a href="tel:'.$shopmobile.'" class="hc-iconfont color_ff2547"></a></span>'.$shop_address.'</p></dd>';
                    $this->assign('html',$html);
              }
              $this->assign('service_type',$service_type);
              $order_info['service_num'] = intval(idtoname($order_info['id'],'MemberPackage','service_num','order_id',false));
              $order_info['service_sn'] = idtoname($order_info['id'],'MemberPackage','service_sn','order_id',false);
        }
        if($order_info['type'] == 12){
              $where['package_id'] = $order_goods[0]['goods_id'];
              $where['order_id'] = $id;
              $order_goods = M('MemberPackage')->where($where)->select();
              if(!empty($order_goods)){
                    foreach($order_goods as $k=>$v){
                          $order_goods[$k]['goods_price'] = idtoname($v['goods_id'],'Goods','shop_price','id',false);
                          $order_goods[$k]['goods_name'] = idtoname($v['goods_id'],'Goods','goods_name','id',false);
                          $order_goods[$k]['goods_number'] = $v['service_num'];
                          $order_goods[$k]['wei_number'] = $v['service_num'] - $v['use_num'];
                          $order_goods[$k]['goods_type'] = 12;
                          $order_info['continued'] = $v['continued'];
                          $order_info['endtime'] = $v['endtime'];
                          $order_info['goods_id'] = $v['package_id'];
                    }
              }

        }
        if($order_info['type'] == 3){
            $gift_map=array();
            $gift_map['order_id']=$order_info['id'];
            $order_info['sn_number']=M("WinGift")->where($gift_map)->getField('sn_number');
        }
        $map=array();
        $map['order_id']=$order_info['id'];
        $take_location_id=M("ShipOrder")->where($map)->getField('take_location_id');
        $order_info['take_location_id']=$take_location_id;
        $map_pl['order_id']=array('like','%,'.$id.',%');
        $use_score_money=M('PayLog')->where($map_pl)->getfield('use_score_money');
        $this->assign('use_score_money',$use_score_money);
        $this->assign($order_info);
        $this->assign('goods_info',$order_goods);
        $this->assign('tplseo_title','订单详情'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','订单详情');
        $this->display();
    }
     //外卖订单详情
    public function order_wm_infor(){
        $id=I('param.id');
        $notshow=I('param.notshow');
        $this->assign('notshow',$notshow);
        $order_info=M('OrderInfo')->where('id='.$id)->field(true)->find();
        $p_where['order_id']=array('like','%,'.$id.',%');
        if($order_info['type']!=3) {
            $order_info['used_score'] = M('PayLog')->where($p_where)->getField('use_score_money');
        }
        if($order_info['is_delete']==1)$this->redirect('OrderInfo/index');
        $order_goods=M('OrderGoods')->where('order_id='.$id)->field(true)->select();
        foreach($order_goods as $k=>$v){
            $attr=unserialize($v['goods_attr']);
            // $order_goods[$k]['img']=idtoname($v['goods_id'],'Goods','img');
            $order_goods[$k]['attr_name']=$attr['name'];
            $order_goods[$k]['goods_number']=intval($v['goods_number']);
            $order_info['ordergoods_id']=$v['id'].',';//获取该订单所有的order_goods的id
        }unset($k,$v);
        // if($order_info['promot_id']){
        //     $act_ids = explode(',', $order_info['promot_id']);
        //     $act_ids=array_filter($act_ids);
        //     $order_info['promote'] = D('Common/Takeout')->getpromote($act_ids,$id);
        // }
        $s_money = $order_info['discount']-$order_info['bonus_money']-$order_info['member_discount']-$order_info['custom_discount']-$order_info['goods_custom_discount']-$order_info['used_score_money']-$order_info['first_money'];
        
        if($order_info['send_type']==1){
            $address=idtoname($order_info['shop_id'],'Shops','address');
            $this->assign('shop_address',$address);
        }
		
        if($order_info['type'] == 11){
            $map = array();
            $map['uid'] = MID;
            $map['order_id'] = $order_info['id'];
            $service_info = M('MemberPackage')->where($map)->field('service_sn,status')->find();
            $this->assign('service_info',$service_info);
            if($order_goods[0]['spec_attr']){
            $order_goods[0]['spec_attr'] = unserialize($order_goods[0]['spec_attr']);
            $order_goods[0]['spec_attr'] = $order_goods[0]['spec_attr']['key'];
            }
        }
        $DIS_TRIBUTE=C("DIS_TRIBUTE");
        if($DIS_TRIBUTE == 1 && $order_info['distribute']){
            $reqStatus = D('Common/Distribute')->details_order($order_info);
            $this->assign('reqstatus',$reqStatus);
        }
        $this->assign('DIS_TRIBUTE',$DIS_TRIBUTE);
        $map_u['id']=$order_info['uid'];
        $user_info=M("Member")->where($map_u)->field('paypassword')->find();
        //判断是否有支付密码
        if(empty($user_info['paypassword'])){
            $this->assign('userpaypass',1);
        }
        $s_map = array();$s_map['id'] = $order_info['shop_id'];
        $shop_info = M('Shops')->where($s_map)->field('phone,thumb,name')->find();
        $this->assign('shop_info',$shop_info);
        $this->assign($order_info);
        if($order_info['pay_status']==3 && $order_info['shipping_status']==3 && $order_info['send_type']!=1){
            $is_tui=1;
        }else{
            $is_tui=0;
        }
        //发票信息
        if($order_info['invoice_info']){
            $invoice_info=unserialize($order_info['invoice_info']);
            $this->assign('invoice_info',$invoice_info);
        }
        $this->assign('is_tui',$is_tui);

        $this->assign('s_money',$s_money);
        $this->assign('goods_info',$order_goods);
        $this->assign('tplseo_title','订单详情'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','订单详情');
        $this->display();
    }
    /**
    * 删除订单
    */
    public function del_order(){
        $postdata['order_id']=I('order_id');
        $postdata['admin_id']=MID;//操作日志用
        $postdata['type']='Member';//操作日志用
        $return=D('Common/OrderInfo')->del_order($postdata);
        $this->ajaxReturn($return);
    }
	/**
     * 取消订单
     */
    public function cancel_order(){
        $postdata['order_id']=I('order_id');
        $postdata['admin_id']=MID;//操作日志用
        $postdata['type']='Member';//操作日志用
        $return=D('Common/OrderInfo')->cancel_order($postdata);
        $this->ajaxReturn($return);
    }
    /**
    * 确认收货
    */
    public function confirm_order(){
        $postdata['order_id']=I('order_id');
        $postdata['admin_id']=MID;//操作日志用
        $postdata['type']='Member';//操作日志用
        $result=D('Common/OrderInfo')->confirm_order($postdata);
        if($result['status'])$this->manage($postdata['order_id']);
        $this->ajaxReturn($result);
    }
    /**
    *更新第三方配送
    */
    public function manage($id){
        $return=array('status'=>'0','msg'=>'');
        $order_info=M('OrderInfo')->where('id='.$id)->field(true)->find();
        $apitype=$order_info['distribute'];
        if(empty($apitype))return $return;
        if($apitype=='Dada' || $apitype=='Meituan')$order_id=$order_sn=$order_info['order_sn'];
        if($apitype=='Local'){
            $map=array();
            $map['order_sn']=$order_info['order_sn'];
            $order_sn=M('LocalOrder')->where($map)->getField('origin_id');
            $order_id=$order_info['order_sn'];
        }
        $dis = new \Common\Vendor\Distribute($apitype);
        $input=array(
            'origin_id'=>$order_sn,
            'client_id'=>$order_id,
            'order_code'=>$order_id,
            'order_sn'=>$order_id,
            'shipping_status'=>$order_info['shipping_status'],
            'finish_time'=>NOW_TIME,
        );
        $result= $dis->notify_order($input);
        if($result['status']==1){
            $return['status']=1;
        }
        return $return;
    }
    /**
    * 退款审核页面
    */
    public function refund_content(){
        $order_id=I("order_id");
        $order_goods=I("order_goods");
        $score=I("score");
        $this->assign("order_id",$order_id);
        $this->assign("order_goods",$order_goods);
        $this->assign("score",$score);
        if($score=='th'){
            $this->assign('types','退货');
            $this->assign('tplseo_title','退货审核'.'_'.C('WEB_SITE_TITLE'));
            $this->assign('page_title','退货审核');
        }elseif($score=='tk'){
            $this->assign('types','退款');
            $this->assign('tplseo_title','退款审核'.'_'.C('WEB_SITE_TITLE'));
            $this->assign('page_title','退款审核');
        }
        $this->display();
    }
    /**
    * 退货页面
    */
    public function tui_huo(){
        $order_id=I("order_id");
        $og_id=I("order_goods");
        $this->assign("order_id",$order_id);
        $map=array();
        $map['id']=$og_id;
        $order_goods = M('OrderGoods')->where($map)->field('id,goods_number,goods_price')->find();
        $this->assign('money',($order_goods['goods_number']*$order_goods['goods_price']));
        $this->assign($order_goods);
        $this->assign('types','退货');
        $this->assign('tplseo_title','退货审核'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','退货审核');

        $this->display();
    }
    /**
     * 退货审核操作
     */
    public function check_tui(){
        $data=I("post.");
        $return=D('Common/OrderInfo')->check_tui($data);
        $this->ajaxReturn($return);
    }
    /**
     * 退款审核操作
     */
    public function sub_refund(){
        $data=I("post.");
        $order_info=$refund_data=array();
        $order_info[$data['data']['order_id']]=$data['data']['order_goods'];
        $refund_data['order_info']=$order_info;
        $refund_data['content']=$data['content'];
        $refund_data['type']=$data['type'];
        $return=D('Common/OrderInfo')->check_refund_money($refund_data);
        $this->ajaxReturn($return);
    }
    /**
    * 退款
    */
    public function refund_money(){
        $data=I("post.");
        $return=D('Common/OrderInfo')->refund_audit($data);
        $this->ajaxReturn($return);
    }
    /**
    * 退货
    */
    public function refund_goods(){
        $data=I("post.");
		
        $return=D('Common/OrderInfo')->apply_return($data);
        $this->ajaxReturn($return);
    }
    /**
    * 评论页面
    */
    public function comment(){
        $order_id=I('param.order_id');
        $status=M('OrderInfo')->where('id='.$order_id)->getField('is_comment');
        if($status)$this->error('订单已评论',U('OrderInfo/no_comment'));

        $field='og.id,og.order_id,og.goods_id,og.goods_name,og.goods_number,og.goods_price,og.goods_attr,g.img,g.dp_count,g.avg_score,g.avg_point';
        $goods_info=M('OrderGoods')->alias('og')->where('order_id='.$order_id)->field($field)->join('LEFT JOIN __GOODS__ g on og.goods_id=g.id')->select();
        foreach($goods_info as $k=>$v){
              $goods_attr=unserialize($v['goods_attr']);
              $goods_info[$k]['goods_attr']=$goods_attr['name'];
              $goods_info[$k]['avg_point']=$v['avg_point']*100;
        }
        $this->assign('order_id',$order_id);
        $this->assign('list',$goods_info);
        $this->assign('tplseo_title','评论'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','评论');
        $this->display();
    }
    /**
    * 添加评论
    */
    public function add_comment(){
        $postdata=I('param.');
        $return=D('Common/OrderInfo')->add_comment($postdata);
        if($return['status']==1)$return['url']=U('OrderInfo/no_comment');
        $this->ajaxReturn($return);
    }
    
    /*
     * 免单概率
     */
    public function is_miandan(){
        // $zhongjianglv = 0.25;
        $param = I('param.');
        $map = $return = array();
        $map['order_id'] = $param['order_id'];
        $map['status'] = 2;
        $goods_ids = M('OrderGoods') ->field('goods_id,goods_price') -> where($map) ->select();
        foreach ($goods_ids as $v){
            $ids[] = $v['goods_id'];
            $goods_price[$v['goods_id']] = $v['goods_price'];
        }
        $map = array();
        $map['id'] = array('in',$ids);
        $goods_miandan = M('Goods') -> where($map) ->field('id,is_miandan,goods_name,miandan_gailv,miandan_type') ->select();
        $confirm_time = idtoname($param['order_id'],'OrderInfo','confirm_time');
        $count_money = 0;
        foreach ($goods_miandan as $v){
            if(!empty($v['is_miandan'])){
                $num = $befor = $goods_num = $now = 0;
                $map = array();
                $map['g.goods_id'] = $v['id'];
                $map['i.pay_time'] = array('lt',$confirm_time);
                $map['i.shipping_status'] = 4;
                $sum = M('OrderGoods') -> alias('g') -> join('__ORDER_INFO__ i on g.order_id=i.id') -> where($map) -> sum('g.goods_number');
                $where = array();
                $where['order_id'] = $param['order_id'];
                $where['goods_id'] = $v['id'];
                $goods_num = M('OrderGoods') -> where($where) ->sum('goods_number');
                if($v['miandan_type'] == 1){
                    $num = $sum+$goods_num;
                    $befor = floor($sum*$v['miandan_gailv']/100);
                    $now = floor($num*$v['miandan_gailv']/100);
                    if(($now - $befor) > 0){
                        $arr[] = $v['goods_name'].'_____商品免单个数_____'.($now - $befor).'____退款金额__'.($now - $befor)*$goods_price[$v['id']];
                        $return['miandan_num'][$v['id']] = ($now - $befor);
                        $return['miandan_money'][$v['id']] = ($now - $befor)*$goods_price[$v['id']];
                        $return['goods_name'][$v['id']] = $v['goods_name'];
                        $count_money += ($now - $befor)*$goods_price[$v['id']];
                        $profit = M('OrderGoods') -> where('goods_id=%d and order_id=%d',$v['id'],$param['order_id']) -> getField('profit');
                        $data = array();
                        $data = array('miandan_num'=>$now - $befor,'profit'=>$profit - ($now - $befor)*$goods_price[$v['id']],'befor_profit'=>$profit);
                        M('OrderGoods') -> where('goods_id=%d and order_id=%d',$v['id'],$param['order_id']) -> setField($data);
                    } else {
                        $arr[] =  $v['goods_name'].'———本次没有免单';
                        $return['msg'][$v['id']] = '本次没有免单';
                        $return['goods_name'][$v['id']] = $v['goods_name'];
                    }
                }else if($v['miandan_type'] == 2){
                    $num = $v['miandan_gailv']/10;
                    $befor = floor(10-$num);
                    $now = $v['miandan_gailv']%10;
                    $befor_f = ($sum%10);
                    $befor_sum = $befor_f + 1;
                    $count = $befor_f + $goods_num;
                    $miandan_count = 0;
                    for($i=$befor_sum;$i<=$count;$i++){
                        if(($i%10)>$befor || empty($i%10)){
                            if(!empty($now) && ($befor+1) == ($i%10)){
                                $rand = mt_rand(1,100);
                                if($rand <= $now*10) $miandan_count++;
                            }else $miandan_count++;
                        }
                    }
                    if(empty($miandan_count)){
                        $arr[] =  $v['goods_name'].'———本次没有免单';
                        $return['msg'][$v['id']] = '本次没有免单';
                        $return['goods_name'][$v['id']] = $v['goods_name'];
                    }else{
                        $arr[] = $v['goods_name'].'_____商品免单个数_____'.$miandan_count.'____退款金额__'.$miandan_count*$goods_price[$v['id']];
                        $return['miandan_num'][$v['id']] = $miandan_count;
                        $return['miandan_money'][$v['id']] = $miandan_count*$goods_price[$v['id']];
                        $return['goods_name'][$v['id']] = $v['goods_name'];
                        $count_money += $miandan_count*$goods_price[$v['id']];
//                        M('OrderGoods') -> where('goods_id=%d and order_id=%d',$v['id'],$param['order_id']) -> setField('miandan_num',$now - $befor);
                        $profit = M('OrderGoods') -> where('goods_id=%d and order_id=%d',$v['id'],$param['order_id']) -> getField('profit');
                        $data = array();
                        $data = array('miandan_num'=>$miandan_count,'profit'=>$profit - $miandan_count*$goods_price[$v['id']],'befor_profit'=>$profit);
                        M('OrderGoods') -> where('goods_id=%d and order_id=%d',$v['id'],$param['order_id']) -> setField($data);
                    }
                    
                    
                }
            }
        }
        if(!empty($return['miandan_num'])){
            $order_amount = idtoname($param['order_id'],'OrderInfo','order_amount');
            $data = array();
            $data = array('is_miandan'=>1,'befor_order_amount'=>$order_amount);
            M('OrderInfo') -> where('id=%d',$param['order_id']) ->setField($data);
            $return['order_id'] = $param['order_id'];
        }
//        $this ->assign($this->member_info);
//        $this ->assign($return);
//        $this ->assign('count',count($return['miandan_num']));
//        $html = $this ->fetch('miandan');
        
        $this ->ajaxReturn($return);
    }
    
    public function miandan(){
        $order_id = I('order_id');
        $order = M('OrderInfo') -> where('id=%d',$order_id) ->getField('is_miandan');
        if(!empty($order)){
            $money = 0;
            $goods = $goods_ids = M('OrderGoods') ->field(true) -> where('order_id=%d',$order_id) ->select();
            $img = $this ->member_info['header_img'];
            if(empty($img)){
                $img =ROOT_PATH."/Application/Mobile/View/default/common/images/user-header.png";
            }
            $image = new \Common\Vendor\Image(); 
            $image->open($img);
            $img = "/Public/images/user-header".MID.".png";
            $image->thumb(50, 50)->save(ROOT_PATH.$img);
            $this->assign($this ->member_info);
            $this ->assign('img',$img);
            foreach ($goods as $k => $v){
                if(!empty($v['miandan_num'])){
                    $arr[$k] = $v;
                    $arr[$k]['miandan_money'] = $v['miandan_num'] * $v['goods_price'];
                    $money += $arr[$k]['miandan_money'];
                    $arr[$k]['goods_img'] = idtoname($v['goods_id'],'Goods','img');
                }
            }
        }
        $this->assign('order_id',$order_id);
        $this ->assign('money',$money);
        $this ->assign('count',count($arr));
        $this ->assign('goods',$arr);
        $this ->display();
    }
    
    public function miandan_tuikuan(){
        $order_id = I('order_id');
        $money = I('money');
        $return = array('status'=>0);
//        $is_miandan = M('OrderInfo') -> where('id=%d',$order_id) ->getField('is_miandan');
        $order = idtoname($order_id,'OrderInfo','order_sn,is_miandan,order_amount','id',false);
        if($order['is_miandan'] == 2)
            $this ->ajaxReturn ($return);
        
        account_change(MID,$money,1,8,'参与商品随机免单，享受免单返现'.$order['order_sn']);
        $data = array('is_miandan'=>2,'order_amount'=>$order['order_amount']-$money);
        M('OrderInfo') -> where('id=%d',$order_id) ->setField($data);
        $return = array('status'=>1,'url'=>U('Mobile/OrderInfo/index'));
        $this ->ajaxReturn($return);
    }
}
?>