<?php
/** pad
 * 第三方订单
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author liushanshan
 */
namespace Pad\Controller;
use Common\Controller\HanchaoController;
class ThreeOrderController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
    }
    /**
     * 订单状态列表
     */
    public function get_order_status(){
        $return['status']=1;
        $return['data']['order_status']=array('1'=>'待接单','2'=>'待取货','3'=>'配送中','4'=>'已完成','5'=>'已取消','7'=>'已过期');
        $this->ajaxReturn($return);
    }
    /**
     * 获取订单列表
     */
    public function get_order_list(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');

        $auth_code='threeorder_list';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) $this->ajaxReturn($ret);
        $data=D('Pad/ThreeOrder')->order_list($postdata);
        if($data['status']==0){
            $return['msg']=$data['msg'];
            $this->ajaxReturn($return);
        }
        if($data['status']==2)$this->ajaxReturn($data);
        foreach($data['data']['list'] as $k => $v){
            $tag='';
            if($v['code']=='jddj')$tag='京东到家';
            else if($v['code']=='ele')$tag='饿了么';
            $list[$k]=$v;
            $list[$k]['id']=$v['id'];
            $list[$k]['order_sn']=$v['ordersn'];
            $list[$k]['addtime']=time_format($v['addtime']);
            $list[$k]['updatetime']=time_format($v['updatetime']);
            $list[$k]['order_endtime']=time_format($v['order_endtime']);
            $list[$k]['order_deliverytime']=time_format($v['order_deliverytime']);
            $list[$k]['order_deliverytime']=time_format($v['order_deliverytime']);
            $list[$k]['order_canceltime']=time_format($v['order_canceltime']);
            $list[$k]['creattime']=time_format($v['creattime']);

            $list[$k]['tag']=$tag;
        }
        $return=array();
        $return['status']=1;
        $return['data']['list']=$list;
        $map=array();
        $map['order_status']=array('elt',2);
        $map['shop_id']=$postdata['shop_id'];
        $count=M('ThreeOrder')->where($map)->count();
        $return['count']=$count;
        $this->ajaxReturn($return);
    }
    /**
     * 获取订单内容
     */
    public function get_order_info(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('param.');
        $data=D('Pad/ThreeOrder')->order_info($postdata);
        if($data['status']==0){
            $return['msg']=$data['msg'];
            $this->ajaxReturn($return);
        }
        $data['data']['infos']['order_status_name']=$this->three_order_status($data['data']['infos']['order_status']);
        $data['data']['infos']['addtime']=time_format($data['data']['infos']['addtime']);
        if($data['data']['infos']['order_deliverytime'])$data['data']['infos']['order_deliverytime']=time_format($data['data']['infos']['order_deliverytime']);
        if($data['data']['infos']['order_closetime'])$data['data']['infos']['order_closetime']=time_format($data['data']['infos']['order_closetime']);
        if($data['data']['infos']['order_canceltime'])$data['data']['infos']['order_canceltime']=time_format($data['data']['infos']['order_canceltime']);
        if($data['data']['infos']['order_endtime'])$data['data']['infos']['order_endtime']=time_format($data['data']['infos']['order_endtime']);
        if($data['data']['infos']['creattime'])$data['data']['infos']['creattime']=time_format($data['data']['infos']['creattime']);
        if($data['data']['infos']['type']==0){
            $data['data']['infos']['type_name']='自配';
        }else if($data['data']['infos']['type']==1){
            $data['data']['infos']['type_name']='众包';
        }else if($data['data']['infos']['type']==9){
            $data['data']['infos']['type_name']='未知';
        }
        $return['status']=1;
        $goods=unserialize($data['data']['infos']['goods_info']);
        $all_num=0;$goods_list=array();
        foreach ($goods as $key => $value) {
            $value=$value;
            $all_num+=$value['count'];
            if($value['act_good_ids']){
                $val=$value['act_good_ids'];
                $value['act_good_ids']=$val;

                if(strpos($val,"-")){
                    $new_arr = $arr = array();
                    $new_arr = explode('-',$val);
                    $arr['attr_key'] = $new_arr[1];
                    $arr['goods_id'] = $new_arr[0];
                    $value['goods_id'] = $new_arr[0];
                    $value['goodsname'] = idtoname($new_arr[0],'Goods','goods_name','id',false);
                    $value['attr']='('.attr_key2name($arr['attr_key'],$arr['goods_id']).')';
                    $value['goods_sn']=idtoname($arr['attr_key'],'GoodsAttrStock','goods_attr_sn','attr_key',false);
                }else{
                    $value['goods_id'] = $val;
                    $value['goodsname'] = idtoname($val,'Goods','goods_name','id',false);
                    $value['goods_sn']=idtoname($val,'Goods','goods_sn','id',false);
                }
            }else{
                $whereor=array();
                $whereor['goods_name|goods_sn'] = array(array('like','%'.$value['goods_name'].'%'),array('like','%'.$value['goodssn'].'%'), '_multi' => true);                
                $goods=M('Goods')->where($whereor)->field('id,goods_name,goods_sn')->find();
                if($goods){
                    $value['goods_id'] = $goods['id'];
                    $value['goodsname'] = $goods['goods_name'];
                    $value['goods_sn']=$goods['goods_sn'];
                }else{
                    $value['goodsname'] = '请匹配该商品';
                }
            }
            $goods_list[$value['goodssn']]=$value;
        }
        $return['data']['goods_list']=$goods_list;
        $data['data']['infos']['all_num']=$all_num;
        $return['data']['infos']=$data['data']['infos'];
        $order_reason=D('Pad/ThreeOrder')->get_reason('ele');
        $return['data']['reason_list']=$order_reason;
        $map=array();
        $map['order_status']=array('elt',2);
        $map['shop_id']=$postdata['shop_id'];
        $count=M('ThreeOrder')->where($map)->count();
        $return['count']=$count;
        $this->ajaxReturn($return);
    }
    /**
     * 订单操作按钮列表
     */
    public function order_manage(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        switch($postdata['manage_way']){
            case 'confirm':
                $auth_code='threeorder_confirm';
                break;
            case 'no_confirm':
                $auth_code='threeorder_no_confirm';
                break;
            case 'yes_confirm':
                $auth_code='threeorder_yes_confirm';
                break;
            case 'ship':
                $auth_code='threeorder_ship';
                break;
            case 'return':
                $auth_code='threeorder_return';
                break;
            case 'no_return':
                $auth_code='threeorder_no_return';
                break;
            case 'cancel':
                $auth_code='threeorder_cancel';
                break;
            case 'myself':
                $auth_code='threeorder_myself';
                break;
        }
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) $this->ajaxReturn($ret);
        $postdata['operator']=idtoname($postdata['shop_id'],'Shops','name');
        $i=0;$infos=array();
        $postdata['infos']=$this->StrToparamArray($postdata['infos']);
        foreach($postdata['infos'] as $k => $v){
            $postdata['act_good_ids'][$i]=$v['act_good_ids'];
            $postdata['attr_key'][$i]=$v['attr_key'];
            $i++;
        }unset($postdata['infos']);

        $result=D('Pad/ThreeOrder')->three_manage($postdata);
        if($result['status']==0){$return['msg']=$result['info']; $this->ajaxReturn($return);}
        $return['status']=1;
        $return['msg']=$result['info'];

        $map=array();
        $map['order_status']=array('elt',2);
        $map['shop_id']=$postdata['shop_id'];
        $count=M('ThreeOrder')->where($map)->count();
        $return['count']=$count;

        $this->ajaxReturn($return);
    }
    /**
     * 订单打印
     */
    public function get_print_info(){
        $return=array('status'=>3,'msg'=>'');
        $sid = I('sid');
        $token = I('token');
        $postdata=I('post.');
        $result= D('Pad/Cashier')->order_print_info($postdata);
        if($result['status']==0) {$return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $this->ajaxReturn($result);
    }
    public function three_order_status($code=1){
        $order_status=array('1'=>'待接单','2'=>'待取货','3'=>'配送中','4'=>'已完成','5'=>'已取消','6'=>'申请取消','7'=>'已过期','8'=>'配送中','1000'=>'系统故障订单发布失败');
        return $order_status[$code];
    }
    /**
     * 格式化App和pad传过来的商品数据
     * @param string $str
     * @return array
     */
    public function StrToparamArray($str=''){
        $str=html_entity_decode($str);
        if(is_string($str)&&!empty($str)){
            $data = array();
            $arr = explode('^',$str);
            foreach ($arr as $val) {
                if(empty($val))continue;
                $d = array();
                $d = explode('/=/',$val);
                if(is_string($d[1])){
                    $tobj = array();
                    $tarr = explode('@',$d[1]);
                    foreach ($tarr as $v) {
                        if(empty($v))continue;
                        $dt = explode('___',$v);
                        $tobj[$dt[0]] = $dt[1];
                    }
                    $data[$d[0]] = $tobj;
                }else{
                    $data[$d[0]] = $d[1];
                }
            }
            return $data;
        }
        return array();
    }
    /**
    *获取第三方设置详情
    */
    public function quickedit(){
        $return['status']=0;
        $postdata=I('post.');
        $auth_code='cashierset_threeorder';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) $this->ajaxReturn($ret);
        $shop_id=I('shop_id');
        $name = $postdata['code'];
        if (empty($name)) {$return['msg']=L('PARAM_ERR');$this->ajaxReturn($return);}
        $three = new \Common\Vendor\Threeorder($name);
        $appid=C('appid');
        $config=$authurl='';
        $configs=$three->orderconfig('base');    
        if(!empty($configs)){
            $time=0;
            foreach ($configs as $k => $v) {
                if($appid==$v){
                    $venderId=$k;
                    $three->setConfig($venderId);
                    $configg=$three->orderconfig($venderId);
                    if($configg['shop_id']==$shop_id && $configg['update_time']>$time){
                        $time=$configg['update_time'];
                        $config=$configg;
                        $authurl=$three->get_auth_url($venderId);
                    }
                    
                }
            }
        } 
        $return=array('status'=>1,'shop_id'=>$shop_id,'name'=>$name,'config'=>$config,'authurl'=>$authurl,'configg'=>$configg);
        $this->ajaxReturn($return);
        
    }
    /**
    *保存第三方设置
    */
    public function save_quickedit(){
        $param=array();
        $return['status']=3;
        $postdata=I('post.');
        $auth_code='cashierset_threeorder';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($ret['status']!=1) $this->ajaxReturn($ret);
        $shop_id=I('shop_id');
        if(!$shop_id){$return['msg']='参数错误';$this->ajaxReturn($return);}         
        $name = $postdata['code'];
        if (empty($name)) {$return['msg']=L('PARAM_ERR');$this->ajaxReturn($return);}
        if($name=='ele'){
            if(empty($postdata['ele_venderId'])){$return['msg']='请输入店铺ID';$this->ajaxReturn($return);}
            $param['venderId']=$postdata['ele_venderId'];
        }else if($name=='jddj'){
            if(empty($postdata['jddj_venderId'])){$return['msg']='请输入店铺ID';$this->ajaxReturn($return);}
            if(empty($postdata['jddj_appkey'])){$return['msg']='请输入App Key';$this->ajaxReturn($return);}
            if(empty($postdata['jddj_appsecret'])){$return['msg']='请输入App Secret';$this->ajaxReturn($return);}
            $param['venderId']=$postdata['jddj_venderId'];
            $param['appkey']=$postdata['jddj_appkey'];
            $param['appsecret']=$postdata['jddj_appsecret'];
        }
        $three = new \Common\Vendor\Threeorder($name);
        $appid=C('appid');
        $base=array($param['venderId']=>$appid);
        $three->orderconfig('base',$base);
        $param['siteid']=$appid;
        $param['shop_id']=$shop_id;
        $param['update_time']=NOW_TIME;
        $result=$three->orderconfig($param['venderId'],$param);
        if($result!==false){
            $return['status']=1;
            $return['msg']=L('EDIT_OK');
        }else{
            $return['msg']=L('EDIT_ERROR');
        }
        $this->ajaxReturn($return);
    }
}

