<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\api\controller\order;

use app\api\controller\Base;
use think\Db;
use app\common\model\bbfxshop\FenxiaoLevel;
use app\common\model\bbfxshop\User;
use app\common\model\bbfxshop\Agent;
use app\common\model\bbfxshop\CloudWarehouse;
use app\common\model\bbfxshop\Goods;
use app\common\model\bbfxshop\Weixin;
use app\common\model\QRcode;
use app\common\model\bbfxshop\Shipping;


class Order extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Order;
    }

    
    public function refundDetail(){
        $params = $this->param();
        
        $user = $this->user;
        $order_id = intval($params['order_id']);
        
        $order = Db::name("bbfx_order")->where(['id'=>$order_id,'is_pay'=>1,'user_id'=>$user['id']])->find();
        
        if(empty($order)){
            __error("订单不存在");
        }
        
        $order_refund = Db::name("bbfx_order_refund")->where(['order_id'=>$order['id']])->find();
        if(empty($order_refund)){
            __error("未查询到退款信息");
        }
        
        $order_goods = Db::name("bbfx_order_goods")->where(["uniacid"=>UNIACID,"order_id"=>$order['id']])->select();
        foreach($order_goods as $k => &$v){
            $goods_info = iunserializer($v['goods_info']);
            $goods_info['thumb'] = cdnurl($goods_info['thumb']);
            $v['goods'] = $goods_info;
        }
        unset($v);
        
        if($order_refund['createtime'] > strtotime(date("Y-01-01 00:00:00"))){
            $createtime = date("m/d H:i",$order_refund['createtime']);
        }
        else{
            $createtime = date("Y/m/d H:i",$order_refund['createtime']);
        }
        
        $steps = array(
            array('title'=>'提交申请','time'=>$createtime),
            array('title'=>'平台审核','time'=>''),
            array('title'=>'退款成功','time'=>''),
        );
        $order_refund['step'] = 0;
        if($order_refund['status'] > 0){
            if($order_refund['refundtime'] > strtotime(date("Y-01-01 00:00:00"))){
                $refundtime = date("m/d H:i",$order_refund['refundtime']);
            }
            else{
                $refundtime = date("Y/m/d H:i",$order_refund['refundtime']);
            }
            $steps[1]['time'] = $refundtime;
            $steps[2]['time'] = $refundtime;
            
            $order_refund['step'] = 2;
        }
        if($order_refund['status'] == 1){
            $steps[2]['title'] = '退款成功';
        }
        else if($order_refund['status'] == 2){
            $steps[2]['title'] = '退款失败';
        }
        $order_refund['createtime'] = date("Y-m-d H:i:s",$order_refund['createtime']);
        $order_refund['images'] = iunserializer($order_refund['images']);
        foreach ($order_refund['images'] as $key => &$val){
            $val = cdnurl($val);
        }
        unset($val);
        __success("操作成功",['order'=>$order,'steps'=>$steps,'order_goods'=>$order_goods,'refund'=>$order_refund]);
    }
    
    public function submitRefund(){
        global $_W;
        $params = $this->param();
        
        $user = $this->user;
        $order_id = intval($params['order_id']);
        $money = floatval($params['money']);
        $order = Db::name("bbfx_order")->where(['id'=>$order_id,'user_id'=>$user['id']])->find();
        if(empty($order)){
            __error("订单不存在");
        }
        
        if($order['is_pay'] != 1){
            __error("订单未支付");
        }
        
        if($order['refund_status'] != -1){
            __error("已提交申请");
        }
        
        if($order['is_asales_end'] == 1){
            __error("订单已过售后期");
        }
        
        if($order['order_source'] == 2 || $order['order_source'] == 4 || $order['order_source'] == 5 || $order['order_source'] == 6){
            __error("该类型的订单不可退款");
        }

        if($order['gift_status'] == 1){
            __error("该订单礼品已被领取不可退款");
        }
        
        $orderset = $_W['base']['orderset'];
        if($orderset['is_refund'] != 1 || empty($orderset['allow_refund_status']) || !in_array($order['status'],$orderset['allow_refund_status'])){
            __error("当前订单状态不允许退款");
        }

        if($order['pintuan_miandan_status'] == 1){
            __error("拼团免单订单不支持重复退款");
        }
        
        if($money > $order['price']){
            __error("申请金额不得超出订单实付金额");
        }
        
        if($money <= 0){
            $money = $order['price'];
        }
        
        $order_no = date('YmdHis').random(8,true);
        
        $images = $params['images'];
        foreach ($images as $k => &$v){
            $v = replaceSiteroot($v);
        }
        unset($v);
        
        $data = array(
            "uniacid"=>UNIACID,
            "user_id"=>$user['id'],
            "order_no"=>$order_no,
            "money"=>$money,
            "order_id"=>$order_id,
            "content"=>trim($params['content']),
            "mobile"=>trim($params['mobile']),
            "images"=>iserializer($images),
            "createtime"=>time()
        );
        
        if(empty($data['content'])){
            __error("请填写退款原因");
        }
        if(empty($data['mobile'])){
            __error("请填写联系方式");
        }
        
        $res = Db::name("bbfx_order_refund")->insert($data);
        
        if($res){
            
            Db::name("bbfx_order")->where(['id'=>$order['id']])->update(['refund_status'=>0]);
            
            __success("提交成功，请等待审核！");
        }
        else{
            __error("提交申请失败！");
        }
    }
    
    public function close(){
        $params = $this->param();
        
        $id = intval($params['id']);
        $user = $this->user;
        $order = Db::name("bbfx_order")->where(['id'=>$id,'user_id'=>$user['id']])->find();
        if(empty($order)){
            __error("订单不存在");
        }
        
        if($order['is_pay'] == 1){
            __error("订单已支付");
        }
        
        if($order['status'] == -1){
            __error("订单已取消");
        }
        
        $res = $this->model->closeOrder($id);
        
        if($res['code'] == 1){
            __success("操作成功");
        }
        else{
            __error($res['message']);
        }
    }
    
    // 物流查询
    public function queryExpressDelivery(){
        $params = $this->param();
        
        $id = intval($params['id']);
        $user = $this->user;
        $order = Db::name("bbfx_order")->where(['id'=>$id,'user_id'=>$user['id']])->find();
        if(empty($order)){
            __error("订单不存在");
        }
        $express_no = $this->param("express_no/s");
        
        if($order['order_type'] == 2 && $order['delivery_method'] != 1){
            __error("不是快递订单");
        }
        if($order['status'] <= 1){
            __error("订单未发货");
        }
        
        $express = iunserializer($order['express']);
        $new_express_no = $express_no;
        if(!strstr($new_express_no, ':')){
            foreach ($express as $key => $val){
                if($val['express_no'] == $express_no && $val['express_name'] == '中通快递'){
                    $new_express_no = $express_no.":".mb_substr($order['delivery_mobile'], -4);
                }
            }
            unset($val);
        }
        
        $result = \app\common\model\bbfxshop\Delivery::queryExpressDelivery($new_express_no);
        if($result['code'] == 1){
            $res = $result['data'];
            foreach ($express as $key => &$val){
                if($val['express_no'] == $express_no){
                    $val['express_data'] = $res;
                }
            }
            unset($val);
            
            Db::name("bbfx_order")->where(['id'=>$order['id']])->update(['express'=>iserializer($express)]);
        }
        
        __result($result);
    }
    
    public function deliveryGoods(){
        $params = $this->param();
        
        $id = intval($params['id']);
        $user = $this->user;
        $order = Db::name("bbfx_order")->where(['id'=>$id,'user_id'=>$user['id']])->find();
        if(empty($order)){
            __error("订单不存在");
        }
        
        if($order['status'] != 2){
            __error("订单错误");
        }
        
        $res = $this->model->takeDelivery($order['id']);
        
        if(!empty($res) && $res['code'] == 1){
            __success("操作成功");
        }
        else{
            __error($res['message']);
        }
    }

    public function checkStatus(){
        $id = $this->param("id/d");
        $order = Db::name("bbfx_order")->where(['id'=>$id,'user_id'=>$this->user['id']])->find();
        if(empty($order)){
            __error("订单不存在");
        }
        $result = Shipping::setSendStatus($id);
        __result($result);
    }
    
    public function getHexiaoQrcode(){
        $id = $this->param("id/d");
        $platform = $this->param("platform/s");
        $order = Db::name("bbfx_order")->where(['id'=>$id,'user_id'=>$this->user['id']])->find();
        if(empty($order)){
            __error("订单不存在");
        }
        
        if($platform == 'wxapp'){
            
            if($order['mid'] > 0){
                $result = Weixin::getUnlimitedQRCode("merchant/verification/index?{$order['hexiao_code']}", "hexiao_wxacode_{$order['uniacid']}_".$order['id'].".png");
            }
            else{
                $result = Weixin::getUnlimitedQRCode("pagesS/order/detail?{$order['hexiao_code']}", "hexiao_wxacode_{$order['uniacid']}_".$order['id'].".png");
            }
            
            if($result['code'] != 1){
                __result($result);
            }
            $qrcode = cdnurl($result['data']['url']); 
            Db::name("bbfx_order")->where("id",$order['id'])->update(['hexiao_wxacode'=>$result['data']['url']]);
        }
        else{
            if($order['mid'] > 0){
                $url = request()->domain()."/mobile/#/merchant/verification/index?code={$order['hexiao_code']}&i=".$order['uniacid'];
            }
            else{
                $url = request()->domain()."/mobile/#/pagesS/order/detail?code={$order['hexiao_code']}&i=".$order['uniacid'];
            }
            $result = QRcode::create($url, "hexiao_qrcode_{$order['uniacid']}_".$order['id']);
            if($result['code'] != 1){
                __result($result);
            }
            $qrcode = cdnurl($result['data']['url']); 
            Db::name("bbfx_order")->where("id",$order['id'])->update(['hexiao_qrcode'=>$result['data']['url']]);
        }
        
        __success("操作成功",$qrcode."?".time());
    }
    
    public function list(){
        $params = $this->param();
        $platform = $params['platform'];
        $user = $this->user;
        $limit = 10;
        $page = max(1,intval($params['page']));
        
        $status = intval($params['status']);
        $where = ['uniacid'=>UNIACID,'user_id'=>$user['id'],'order_source'=>['<>',8]];
        if($status != -1){
            if($status == 4){ 
                $where['status'] = ['>',0];
                $where['refund_status'] = ['>=',0]; 
            }
            else{
                $where['status'] = $status;
            }
        }
        $list = Db::name("bbfx_order")->where($where)->order('id desc')->limit(($page-1)*$limit,$limit)->select();
        if(!empty($list)){
            foreach($list as $key => &$val){

                $yuyue = Db::name("bbfx_order_yuyue")->where(['uniacid'=>UNIACID,'order_id'=>$val['id'],'status'=>0])->order("id asc")->find();
                if(!empty($yuyue)){
                    $yuyue['createtime'] = date("Y-m-d H:i:s",$yuyue['createtime']);
                    $yuyue['yy_time'] = date("Y-m-d",$yuyue['yy_stime'])." ".date('H:i',$yuyue['yy_stime']).' - '.date('H:i',$yuyue['yy_etime']);
                    $val['yuyue'] = $yuyue;
                }
                
                $val['paytypeText'] = $this->model->getOrderPaytype($val);
                $val['statusText'] = $this->model->getOrderStatus($val);

                if(!empty($val['hexiao_store'])){
                    $val['hexiao_store'] = iunserializer($val['hexiao_store']);
                    $val['hexiao_store']['logo'] = cdnurl($val['hexiao_store']['logo']);
                }
                
                $val['is_shipping'] = 0;
                $val['transaction_id'] = '';
                $val['is_shouhuo'] = 0;
                if(($val['platform'] == 'wxapp' || $val['platform'] == 'WXAPP') && $val['third_party'] == 0 && $val['paytype'] == 1 && $val['order_type'] == 2 && $val['is_hexiao'] != 1){
                    $pay_order = Db::name("bbfx_pay_order")->where(['uniacid'=>UNIACID,'order_no'=>$val['order_no'],'order_type'=>1])->find();
                    if(!empty($pay_order)){
                        $val['is_shipping'] = $pay_order['is_shipping'];
                        $val['transaction_id'] = $pay_order['transaction_id'];

                        if($val['is_shipping'] == 1){
                            $result = Shipping::queryStatus($val['id']);
                            if($result['code'] == 1 && in_array($result['data']['order_state'],[3,4,6])){
                                $val['is_shouhuo'] = 1;
                            }
                        }
                    }
                }
                
                $order_goods = Db::name("bbfx_order_goods")->where(["uniacid"=>UNIACID,"order_id"=>$val['id']])->select();
                
                $val['is_virtual_fahuo'] = 0;
                $is_comment = 1;
                foreach($order_goods as $k => &$v){
                    $goods_info = iunserializer($v['goods_info']);
                    $goods_info['thumb'] = cdnurl($goods_info['thumb']);
                    $plugin = iunserializer($goods_info['plugin']);
                    
                    unset($goods_info['plugin']);
                    
                    if($val['order_type'] == 4 && $val['is_hexiao'] != 1 && $val['is_pay'] == 1 && !empty($plugin['virtual_goods']['fahuo_content'])){
                        $val['is_virtual_fahuo'] = 1;
                        $v['virtual_fahuo'] = $plugin['virtual_goods']['fahuo_content'];
                    }
                     
                    $v['goods'] = $goods_info;
                    if($goods_info['type'] == 1 && $val['status'] >= 1){
                        $serial_number = Db::name("bbfx_serial_number")->where(['uniacid'=>UNIACID,'pid'=>$v['id']])->select();
                        if(!empty($serial_number)){
                            foreach($serial_number as $kk => &$vv){
                                $vv['val'] = trim($vv['val']);
                            }
                            unset($vv);
                            $v['serial_number'] = $serial_number;
                        }
                        else{
                            $v['serial_number'] = [];
                        }
                    }
                    unset($v['goods_info']);
                    $comment = Db::name("bbfx_goods_comment")->where(['uniacid'=>UNIACID,'order_id'=>$val['id'],'goods_id'=>$v['goods_id']])->find();
                    if(empty($comment)){
                        $is_comment = 0;
                        $v['is_comment'] = 0;
                    }
                    else{
                        $v['is_comment'] = 1;
                    }

                    if($val['gift_status'] > -1){
                        if($platform == 'wxapp'){
                            $result = Weixin::getUnlimitedQRCode("plugin/gift/index?{$val['order_no']}", "gift_wxacode_".$val['order_no'].".png",1);
                        }
                        else{
                            $url = request()->domain()."/mobile/#/plugin/gift/index?order_no={$val['order_no']}&i=".UNIACID;
                            $result = QRcode::create($url, "gift_qrcode_".$val['order_no']);
                        }
                        $val['gift_qrcode'] = "";
                        if($result['code'] == 1){
                            $val['gift_qrcode'] = cdnurl($result['data']['url']);
                        }
                    }
                }
                unset($v);
                $val['is_comment'] = $is_comment;
                $val['order_goods'] = $order_goods;
                $val['pay_time'] = date("Y-m-d H:i:s",$val['pay_time']);
                $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
                $val['express'] = iunserializer($val['express']);
                $val['hexiao_wxacode'] = cdnurl($val['hexiao_wxacode']);
                $val['hexiao_qrcode'] = cdnurl($val['hexiao_qrcode']);
                unset($val['goods_info']);
            }
            unset($val);
        }

        __success("操作成功",$list);
    }
    
    public function detail(){
        $id = $this->param('id/d');
        $platform = $this->param('platform/s');
        
        
        $where = ['uniacid'=>UNIACID,'id'=>$id,'user_id'=>$this->user['id']];
        
        $order = Db::name("bbfx_order")->where($where)->find();
        if(empty($order)){
            __error("订单不存在");
        }
        
        if(($order['order_type'] == 2 || $order['order_type'] == 4) && $order['is_hexiao'] == 1 && $order['status'] == 3){
            $hexiao_user = Db::name("bbfx_user")->where("id",$order['hexiao_uid'])->field("id,nickname,avatar,level")->find();
            if(!empty($hexiao_user)){
                $hexiao_user['avatar'] = cdnurl($hexiao_user['avatar']);
                $order['hexiao_user'] = $hexiao_user;
            }
            
        }
        $order['hexiao_store'] = iunserializer($order['hexiao_store']);
        
        if($order['order_type'] == 2){
            $order['express'] = iunserializer($order['express']);
        }

        if($order['paytype'] == 6){
            $order['pay_voucher'] = cdnurl(Db::name("bbfx_pay_order")->where(['uniacid'=>UNIACID,'order_no'=>$order['order_no']])->order("id desc")->value("pay_voucher"));
        }
        
        $goods_info = iunserializer($order['goods_info']);
        foreach ($goods_info as $key => &$val){
            $val['thumb'] = cdnurl($val['thumb']);
            unset($val['plugin']);
        }
        unset($val);
        $order['goods_info'] = $goods_info;
        $order['createtime'] = date("Y-m-d H:i:s",$order['createtime']);
        $order['pay_time'] = date("Y-m-d H:i:s",$order['pay_time']);
        $order['verify_time'] = date("Y-m-d H:i:s",$order['hexiao_time']);
        $order['hexiao_time'] = date("Y-m-d H:i:s",$order['hexiao_time']);
        
        $order_goods = Db::name("bbfx_order_goods")->where(["uniacid"=>UNIACID,"order_id"=>$order['id']])->select();
        $order['is_virtual_fahuo'] = 0;
        $og_ids = [];
        $is_comment = 1;
        $goods_ids = [];
        foreach($order_goods as $k => &$v){
            $og_ids[] = $v['id'];
            $goods_ids[] = $v['goods_id'];
            $goods_info = iunserializer($v['goods_info']);
            $goods_info['thumb'] = cdnurl($goods_info['thumb']);
            $plugin = iunserializer($goods_info['plugin']);
                    
            unset($goods_info['plugin']);
            
            if($order['order_type'] == 4 && $order['is_hexiao'] != 1 && $order['is_pay'] == 1 && !empty($plugin['virtual_goods']['fahuo_content'])){
                $order['is_virtual_fahuo'] = 1;
                $v['virtual_fahuo'] = $plugin['virtual_goods']['fahuo_content'];
            }
            
            $v['goods'] = $goods_info;
            if($goods_info['type'] == 1 && $order['status'] >= 1){
                $serial_number = Db::name("bbfx_serial_number")->where(['uniacid'=>UNIACID,'pid'=>$v['id']])->select();
                if(!empty($serial_number)){
                    foreach($serial_number as $kk => &$vv){
                        $vv['val'] = trim($vv['val']);
                    }
                    unset($vv);
                    $v['serial_number'] = $serial_number;
                }
                else{
                    $v['serial_number'] = [];
                }
            }
            unset($v['goods_info']);
            $comment = Db::name("bbfx_goods_comment")->where(['uniacid'=>UNIACID,'order_id'=>$order['id'],'goods_id'=>$v['goods_id']])->find();
            if(empty($comment)){
                $is_comment = 0;
                $v['is_comment'] = 0;
            }
            else{
                $v['is_comment'] = 1;
            }
        }
        unset($v);
        $order['order_goods'] = $order_goods;
        $order['is_comment'] = $is_comment;

        $hexiao_store_list = [];
        if($order['is_hexiao'] == 1 && $order['store_id'] == 0){
            $hexiao_store_list = $this->model->getOrderHexiaoStoreList($goods_ids);
        }
        $order['hexiao_store_list'] = $hexiao_store_list;
                
        $order['paytypeText'] = $this->model->getOrderPaytype($order);
        
        $order['statusText'] = $this->model->getOrderStatus($order);
        $order['hexiao_qrcode'] = cdnurl($order['hexiao_qrcode']);
        $order['hexiao_wxacode'] = cdnurl($order['hexiao_wxacode']);
        
        
        $order['give_balance'] = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'item_id'=>['in',$og_ids],'user_id'=>$order['user_id'],'type'=>1,'item_type'=>24])->sum("money");
        $order['give_score'] = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'item_id'=>['in',$og_ids],'user_id'=>$order['user_id'],'type'=>2,'item_type'=>24])->sum("money");
        $order['give_balance'] = round($order['give_balance'],2);
        $order['give_score'] = round($order['give_score'],2);
        
        $order['coupon_list'] = Db::name("bbfx_user_coupon")->where(['uniacid'=>UNIACID,'item_id'=>['in',$og_ids],'receive_type'=>3,'user_id'=>$order['user_id']])->select();
        $order['hx_list'] = $this->model->getHexiaoCode($order,$platform);

        $yuyue = Db::name("bbfx_order_yuyue")->where(['uniacid'=>UNIACID,'order_id'=>$order['id'],'status'=>0])->order("id asc")->find();
        if(!empty($yuyue)){
            $yuyue['createtime'] = date("Y-m-d H:i:s",$yuyue['createtime']);
            $yuyue['yy_time'] = date("Y-m-d",$yuyue['yy_stime'])." ".date('H:i',$yuyue['yy_stime']).' - '.date('H:i',$yuyue['yy_etime']);
            $order['yuyue'] = $yuyue;
        }

        __success("操作成功",$order);
    }
    
    public function express(){
        $params = $this->param();
        
        $id = intval($params['id']);
        $current = intval($params['current']);
        
        $order = Db::name("bbfx_order")->where(['order_type'=>2,'uniacid'=>UNIACID,'user_id'=>$this->user['id'],'id'=>$id,'status'=>['>',0]])->find();
        if(empty($order)){
            __error("订单信息错误");
        }
        
        $result = Weixin::getAccessToken();
        if($result['code'] != 1){
            __result($result);
        }
        $access_token = $result['data'];
        
        $openid = $params['openid'];
        
        $order_goods = Db::name("bbfx_order_goods")->where(["uniacid"=>UNIACID,"order_id"=>$order['id']])->select();
         
        $goods_info = ['detail_list'=>[]];       
        foreach($order_goods as $key => $val){
            $goods = iunserializer($val['goods_info']);
            $goods['thumb'] = cdnurl($goods['thumb']);
            
            $goods_info['detail_list'][] = array(
                "goods_name"=>$goods['title'],
                "goods_img_url"=>$goods['thumb']
            );
        }
        
        $express = iunserializer($order['express']);
        
        if(empty($express[$current]) || empty($express[$current]['express_no'])){
            __error("物流信息错误");
        }
        
        $express_no = $express[$current]['express_no'];
        
        
        $data = array(
            "openid"=>$openid,
            "waybill_id"=>$express_no,
            "receiver_phone"=>$order['delivery_mobile'],
            "goods_info"=>$goods_info
        );
        
        $result = ihttp_request("https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/trace_waybill?access_token={$access_token}",$data);
        $result = json_decode($result['content'],true);
        if($result['errcode'] == 0){
            $express[$current]['waybill_token'] = $result['waybill_token'];
            
            Db::name("bbfx_order")->where(['id'=>$order['id']])->update(['express'=>iserializer($express)]);
            
            __success("操作成功",$result['waybill_token']);
        }
        __error($result['errmsg']);
    }
    
    public function payComplete(){
        $params = $this->param();
        
        $openid = $_W['openid'];
        $paytype = intval($params['paytype']);
        $user = $this->user;
        $order_no = $params['order_no'];
        if($paytype == 2){
            $order = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("订单不存在");
            }
            if($order['is_pay'] == 1){
                __error("订单已支付");
            }
            
            if($user['balance'] < $order['price']){
                __error("余额不足");
            }
            
            $res = User::setUserMoney($user['id'],-$order['price'],1,1,"商品订单支付",$order);
            if(empty($res)){
                __error("余额不足");
            }
            
            $this->model->goodsPayResult($order_no,2);
        }
        else{
            __success("支付类型错误");
        }
        __success("支付成功");
    }
    
    public function pay(){
        $params = $this->param();
        
        $openid = $_W['openid'];
        
        $order_no = $params['order_no'];
        
        $user = $this->user;
        
        $order = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'order_no'=>$order_no])->field(['id','order_no','price','is_pay','status','order_type','paytype'])->find();
        
        if(empty($order)){
            __error("订单不存在");
        }
        
        if($order['is_pay'] == 1){
            __error("订单已支付");
        }
        
        __success("操作成功",$order);
    }
    
    public function confirm(){
        $params = $this->param();
        $user = $this->user;
        $params['user_id'] = $user['id'];
        $params['user'] = $user;
        $params['create_type'] = 0;
        $result = \app\common\model\bbfxshop\OrderCreate::confirm($params);

        __result($result);
    }
    
    public function check(){
        global $_W;
        $params = $this->param();
        $goods_id = intval($params['goods_id']);
        
        $goods = Db::name("bbfx_goods")->where(['id'=>$goods_id,'status'=>1])->find();
        if(empty($goods)){
            __error("商品不存在");
        }
        
        $user = $this->user;
        if($user['is_distributor'] != 1 || $user['distributor_status'] != 1){
            $user['level'] = -1;
        }
        $levels = iunserializer($goods['levels']);
        $set = $_W['base'];
        if(!empty($levels) && !in_array($user['level'],$levels)){
            if(!empty($set['buylevel_tips'])){
                __error($set['buylevel_tips']);
            }
            else{
                __error("商品【{$goods['title']}】当前等级不能购买");
            }
        }
        
        __success("操作成功");
    }
    
    public function submit(){
        $params = $this->param();
        $user = $this->user;
        $params['user_id'] = $user['id'];
        $params['user'] = $user;
        $params['create_type'] = 0;
        $result = \app\common\model\bbfxshop\OrderCreate::create($params);

        __result($result);
    }

    public function getOrderHexiaoStoreList(){
        $order_id = $this->param("order_id/d");
        $order = Db::name("bbfx_order")->where(['id'=>$order_id,'user_id'=>$this->user['id'],'is_pay'=>1])->find();
        if(empty($order)){
            __error("订单不存在");
        }
        $goods = iunserializer($order['goods_info']);
        foreach($goods as $key => $val){
            $goods_ids[] = $val['id'];
        }
        $list = $this->model->getOrderHexiaoStoreList($goods_ids);

        __success("操作成功",['list'=>$list]);
    }
    
    public function submitYuyue(){
        $params = $this->param(['order_id/d','yy_date/s','yy_stime/s','yy_etime/s','store_id/d']);

        $yy_date = $params['yy_date'];
        $yy_stime = strtotime($yy_date." ".$params['yy_stime']);
        $yy_etime = strtotime($yy_date." ".$params['yy_etime']);

        if($yy_stime < time() || $yy_etime < time()){
            __error("请选择大于当前时间的预约时间段");
        }

        $order = Db::name("bbfx_order")->where(['id'=>$params['order_id'],'user_id'=>$this->user['id'],'is_pay'=>1])->find();
        if(empty($order)){
            __error("订单不存在");
        }
        if($order['yuyue_status'] != 1){
            __error("订单不是预约订单，不能预约");
        }
        if($order['status'] == 3){
            __error("订单已完成，不能预约");
        }
        if(in_array($order['refund_status'],[0,1])){
            __error("订单已申请退款，不能预约");
        }

        $log = Db::name("bbfx_order_yuyue")->where(['uniacid'=>UNIACID,'order_id'=>$params['order_id'],'status'=>0])->find();
        if(!empty($log)){
            __error("已预约");
        }

        $data = [
            "uniacid"=>UNIACID,
            "order_id"=>$order['id'],
            'user_id'=>$this->user['id'],
            'store_id'=>$order['store_id'],
            "yy_stime"=>$yy_stime,
            "yy_etime"=>$yy_etime,
            "createtime"=>time()
        ];

        if($order['store_id'] == 0){
            $store_id = $params['store_id'];
            
            $goods = iunserializer($order['goods_info']);
            foreach($goods as $key => $val){
                $goods_ids[] = $val['id'];
            }
            $store_list = $this->model->getOrderHexiaoStoreList($goods_ids);
            if(empty($store_list)){
                __error("该订单不存在预约门店，请联系管理员");
            }
            $sids = array_column($store_list,"id");
            $store_list = array_skey($store_list,"id");
            if(!in_array($store_id,$sids)){
                return __json(0,"该订单不能在该门店预约，请重新选择门店预约");
            }
            $store = $store_list[$store_id];
            if(empty($store)){
                __error("预约门店信息错误");
            }
            $data['store_id'] = $store['id'];
        }

        try {

            Db::name("bbfx_order_yuyue")->insertGetId($data);

            if($order['store_id'] == 0){
                $odata = ['store_id'=>$store['id'],'hexiao_store'=>iserializer($store)];
                Db::name("bbfx_order")->where(['id'=>$order['id']])->update($odata);
            }

        } catch (\Exception $e) {
            __error($e->getMessage());
        }

        __success("操作成功");
    }
}