<?php
/** 第三方订单
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author lss <lss@qq.com>
 */

namespace Pad\Model;
use Think\Model;
class ThreeOrderModel extends Model{
    /**
     * 获取订单列表
     * @param $postdata
     * @return array
     */
    public function order_list($postdata){
        $return=array('status'=>0,'msg'=>'');
        $map=array();
        // $map['type']=9;
        $map['shop_id']=$postdata['shop_id'];
        $map['ordersn']=array('neq','');
        if($postdata['code']==1 || $postdata['code_name']=='Jddj')$map['code']='Jddj';
        if($postdata['code']==2 || $postdata['code_name']=='ele')$map['code']='ele';
        if($postdata['order_sn'])$map['ordersn']=$postdata['order_sn'];
        if($postdata['order_status'])$map['order_status']=$postdata['order_status'];
        $order_list=page_list('ThreeOrder',$map,'id desc');   
        // $return['sql']=M('ThreeOrder')->getLastSql();  
        if(empty($order_list['list'])){$return['status']=2;$return['msg']='暂无数据'; return $return;}
        $return['status']=1;
        $return['data']=$order_list;
        return $return;
    }
    public function order_info($postdata){
        $return=array('status'=>0,'msg'=>'');
        $map=array();
        $map['id']=$postdata['order_id'];
        $order_info['infos']=M('ThreeOrder')->where($map)->field(true)->find();
        if(empty($order_info['infos'])){$return['msg']='订单信息错误'; return $return;}             
        $return['status']=1;
        $return['data']=$order_info;
        return $return;
    }
    /*
    *获取取消原因
    */
    public function get_reason($apitype='ele'){
        $three = new \Common\Vendor\Threeorder($apitype);
        $reason=$three->getreasons();
        foreach ($reason as $key => $value) {
            $order_reason[]['reason']=$value;
        }
        return $order_reason;
    }
    //操作按钮goods_status 0未做任何处理，1已加过库存2已减过库存
    public function three_manage($postdata=array()){
        $return=array('status'=>0,'info'=>'');
        $data['delivery_status']=2;
        $order_id=$postdata['order_id'];
        $manage_way=$postdata['manage_way'];//点击的是哪个操作按钮
        $mobile='';
        $orderss=idtoname($order_id,'ThreeOrder','*');   
        if($orderss['order_status']<3 && empty($postdata['reault']) && empty($postdata['cancel']) && $manage_way!='cancel'){
            $order_info['goodsinfo']=unserialize($orderss['goods_info']);
            foreach ($order_info['goodsinfo'] as $key => $value) {
                if(empty($postdata['act_good_ids'][$key])){$return['info']='有商品没有匹配商品，请匹配商品！';return $return;break;}
                $order_info['goodsinfo'][$key]['attr_key']=$postdata['attr_key'][$key];
                $order_info['goodsinfo'][$key]['goodsid']=$postdata['act_good_ids'][$key];
                if(empty($postdata['attr_key'][$key]))$postdata['attr_key'][$key]=$postdata['act_good_ids'][$key];
                $order_info['goodsinfo'][$key]['act_good_ids']=$postdata['attr_key'][$key];
                $val=$postdata['attr_key'][$key];

                if(strpos($val,"-")){
                    $new_arr = $arr = array();
                    $new_arr = explode('-',$val);
                    $goods_attr_sn = $new_arr[1];
                    $goods_id = $new_arr[0];
                }else{
                    $goods_id = $val;
                    $goods_attr_sn=0;
                }

                $goods_number=D('Common/Cart')->shopgoodsstock($goods_id,$postdata['shop_id'],$goods_attr_sn);
                if($goods_number['goods_number']<=0 || $goods_number['goods_number']<$value['quantity']){
                    $return['info']='"'.idtoname($goods_id,'Goods','goods_name').'"库存不足';
                    return $return;break;
                }              
            }        
            if(empty($postdata['shop_id'])){
                $return['info']='请选择门店';return $return;
            }

            if($postdata['shop_id'])$data['shop_id']=$postdata['shop_id'];
            $data['goods_info']=serialize($order_info['goodsinfo']);
        }            
                     
        $data['update_time']=NOW_TIME;
        if($postdata['confirm'] || $manage_way=='confirm'){$log='接单';$data['order_status']='2';$status=1;$step=1;}//接单和拒单
        if($postdata['cancel'] || $manage_way=='cancel'){$log='拒单';$data['order_status']='5';$data['order_canceltime']=NOW_TIME;$status=0;$step=1;$data['order_closetime']=NOW_TIME;$data['order_endtime']=NOW_TIME;}//拒单
        if($postdata['ship'] || $manage_way=='ship'){
            $log='发货';
            $data['order_deliverytime']=NOW_TIME;
            $data['order_status']='3';
            $data['type']=1;
            $step=2;
            $status=2;
            $data['delivery_mark']='商家已送出';

        }//发货口出库存
        if($postdata['return'] || $manage_way=='return'){$log='退货';$data['order_status']='5';$status=1;$step=8;$data['order_canceltime']=NOW_TIME;$data['order_endtime']=NOW_TIME;}//加库存
        if($postdata['no_return'] || $manage_way=='no_return'){$log='不退货';$data['order_status']='8';$status=0;if($orderss['code']=='jddj'){$step=8;}else if($orderss['code']=='ele'){$step=6;}}//不退
        if($postdata['myself'] || $manage_way=='myself'){
            $log='自配';
            $data['order_status']='8';
            $data['type']=0;
            $data['order_deliverytime']=NOW_TIME;
            $data['delivery_status']=1;
            $data['delivery_mark']='商家已送出';
            $step=3;
            if($postdata['status']==1 && $orderss['code']=='ele'){
                $status=0;
            }else{
                $status=2;
            }
        }
        if($postdata['no_confirm'] || $manage_way=='no_confirm'){$log='取消';$data['order_status']='5';$status=0;$step=1;if($postdata['reault']){$data['delivery_mark']=$postdata['reault'];}$data['order_closetime']=NOW_TIME;$data['order_canceltime']=NOW_TIME;$data['order_endtime']=NOW_TIME;}//取消订单，加库存
        if($postdata['yes_confirm'] || $manage_way=='yes_confirm'){$log='收货';$data['order_endtime']=NOW_TIME;$data['delivery_status']=1;$data['order_status']='4';$step=5;}//确认收货
        if($log){
            $apitype=$orderss['code'];$orderid=$orderss['ordersn'];
            $three = new \Common\Vendor\Threeorder($apitype);
            $appid=C('appid');           
            $shopid=$orderss['seller_id'];
            if($shopid)$three->setConfig(intval($shopid));   
            
            if($step==1){$request=array('ordersn'=>$orderid,'operator'=>$postdata['operator'],'status'=>$status,'reason'=>$postdata['reault']);$result=$three->orderconfirm($request);}
            $mobile=$orderss['delivery_shop'];
            if($step==2){            
                $request=array('ordersn'=>$orderid,'operator'=>$postdata['operator'],'status'=>$status,'mobile'=>$mobile);
                $result=$three->orderdelivery($request);
            }//发货 
          
            if($step==3){
                if(empty($status) && $orderss['code']=='ele' && empty($orderss['delivery_shop'])){
                    $shopid_arr=array('shopId'=>intval($shopid));
                    $shop_info=$three->getshop_info($shopid_arr);
                    $mobile=$shop_info['result']['mobile'];
                    $data['delivery_mobile']=$mobile;
                    $data['delivery_shop']=$shop_info['result']['name'];
                    $data['delivery_name']=$shop_info['result']['name'];
                }
                $request=array('ordersn'=>$orderid,'operator'=>$postdata['operator'],'mobile'=>$mobile);$result=$three->orderdeliverytoself($request);
            }//三方转自送
            if($step==5){$request=array('ordersn'=>$orderid,'operator'=>$postdata['operator'],'mobile'=>'');$result=$three->orderend($request);}//自送确认送达
            if($step==6){$request=array('ordersn'=>$orderid,'status'=>$status,'reason'=>$postdata['content']);$data['delivery_mark']=$postdata['content'];$result=$three->orderrefund($request);}//退单
            if($step==8){$request=array('ordersn'=>$orderid,'operator'=>$postdata['operator'],'status'=>$status,'remark'=>$postdata['content']);$result=$three->orderCancelConfirm($request);}//审核取消订单
            // if($step==9){$request=array('ordersn'=>$orderid,'operator'=>'操作人','code'=>'12233444');$result=$three->checkcode($request);echo '<pre>';print_r($result);}//自提码核销
            
            if($result['status']==1 && empty($result['info']))$result['info']='操作成功';
            if($result['status']==1 && empty($result['info']) &&$step==5)$result['info']='收货成功';
            if($result['status']!=1 && empty($result['info']))$result['info']='操作失败';
            if($result['status']==1){
                if(empty($data['delivery_mark']))$data['delivery_mark']=$result['info'];
                if($data)M('ThreeOrder')->where('id='.$order_id)->save($data);   
                $res=$this->change_goods_num($order_id);
                // $result['info']=$res['msg'];
                $return['status']=1;
            }
            $return['info']=$result['info'];
        }else{
            $return['info']='参数不正确!';
        }
        return $return;
        
    }
    public function change_goods_num($order_id){
        $map = $res=array();
        $map['id'] = $order_id;
        $order_info=M('ThreeOrder')->where($map)->field(true)->find();  
        if($order_info['order_status']==3){$res=D('Common/OrderInfo')->three_order_stock($order_info['id'],1);if($res['status']==1)$data['order_deliverytime']=NOW_TIME;}//扣库存
        if(($order_info['order_status']==5 || $order_info['order_status']==7) && $order_info['order_deliverytime']>0){$res==D('Common/OrderInfo')->three_order_stock($order_info['id']);if($res['status']==1){$data['order_deliverytime']=0;$data['is_reduce']=0;}}
        if($res)$data['delivery_mark']=$res['msg'];
        if($data)M('ThreeOrder')->where('id='.$order_id)->save($data);
        return $res;
    }
}
?>