Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

朱继来 / 后台订单管理

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
BlameHistoryPermalink
Switch branch/tag
  • Order
  • app
  • Http
  • Controllers
  • AddOrderController.php
  • 朱继来's avatar
    1. 去掉客服折叠效果; · a9a4f72a
    2. 调整新增商品采购员;
    朱继来 committed 5 years ago
    a9a4f72a
AddOrderController.php 32.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Cookie;
use App\Http\Page;
use App\Http\Controllers\PermController;
use App\Http\Error;
use App\Model\UserMainModel;
use App\Model\OrderModel;
use App\Model\CrmModel;
use App\Model\CmsModel;
use Excel;

Class AddOrderController extends Controller
{
	// 页面用户、菜单信息
    public function getPageInfo(Request $request)
    {
        $uri = '/' . $request->path();

        $username = $request->user->email;
        $useremail= $request->user->email;

        // 菜单
        $menuconfig = DB::table('config')->where('config_title', '订单系统')->first();
        $menus = [];
        if ($menuconfig && !($menus = json_decode($menuconfig->config_data)))
            $menus = [];

        $perm = new PermController;

        // 用户角色
        $role = $perm->getUserRole($request);

        // 获取权限菜单
        if ($role != 1) {
            $menus = $perm->getPermMenu($menus, $request->user->userId);   
        }

        $data = [
            'header'    => $request->user->header,
            'uri'       => $uri,
            'username'  => $username,
            'useremail' => $useremail,
            'menus'     => $menus,
            'role'      => $role,
        ];

        return $data;
    }

    // 新增联营订单
    public function addOrder(Request $request)
    {
        $info = $this->getPageInfo($request);

        $info['title'] = '新增联营订单';
        $info['sale_id'] = $request->user->userId;

        $crm_user_id = $request->input('user_id', 0);

        $info['crm_account'] = '';

        if ($crm_user_id) {
            $UserMainModel = new UserMainModel();
            $user_info = $UserMainModel->find($crm_user_id);
            $info['crm_account'] = $user_info['mobile'] ? $user_info['mobile'] : $user_info['email'];
        }

        return view('addOrder', $info);
    }

    // 新增自营线上订单
    public function addOnline(Request $request)
    {
        $info = $this->getPageInfo($request);

        $info['title'] = '新增自营线上订单';
        $info['sale_id'] = $request->user->userId;

        $crm_user_id = $request->input('user_id', 0);

        $info['crm_account'] = '';

        if ($crm_user_id) {
            $UserMainModel = new UserMainModel();
            $user_info = $UserMainModel->find($crm_user_id);
            $info['crm_account'] = $user_info['mobile'] ? $user_info['mobile'] : $user_info['email'];
        }

        return view('addOnline', $info);
    }

    // 新增自营其他业务订单
    public function addOtherOrder(Request $request)
    {
        $info = $this->getPageInfo($request);

        $info['title'] = '新增自营其他业务订单';
        $info['sale_id'] = $request->user->userId;

        return view('addOtherOrder', $info);
    }

    // 获取用户信息
    public function getUserInfo(Request $request)
    {
        if ($request->isMethod('post')) {
            $mobile      = $request->input('mobile');
            // $user_id     = $request->input('user_id');
            $goods_type  = $request->input('goods_type'); // 类型,1-联营,2-自营
            $operator_id = $request->user->userId;

            $UserMainModel = new UserMainModel;
          
            // if(!empty($user_id) && strlen($request->input('user_id'))<11 ){           
            // if(!empty($user_id)){           
            //      $user = $UserMainModel->where('user_id', $user_id)->select('user_id', 'email','mobile', 'is_test')->orderBy('create_time', 'desc')->first();
            // }else{              
                if (preg_match('/@/', $mobile)) {
                    $user = $UserMainModel->where('email', $mobile)->select('user_id', 'email', 'is_test')->orderBy('create_time', 'desc')->first();
                } else {
                    $user = $UserMainModel->where('mobile', $mobile)->select('user_id', 'mobile', 'is_test')->orderBy('create_time', 'desc')->first();
                }
            // }

            if (empty($user)) return ['errcode' => -1, 'errmsg' => '未找用户信息'];
   
            // $perm = new PermController;   
            // $role = $perm->getUserRole($request); // 用户角色

            // if ($goods_type == 1 || ($goods_type == 2 && !in_array($role, [1, 10, 11]))) { // 自营管理员、主管、主管助理可以帮客服下单
            //     $last_sale_id = $this->findLastSaleId($user->user_id);

            //     if ($last_sale_id && $operator_id != $last_sale_id) return ['errcode' => -1, 'errmsg' => '暂不能跟进该客户,请联系主管重新指派'];
            // }

            if (strpos($_SERVER['HTTP_HOST'], 'sz') === false) { // 非测试环境下执行
                // 若用户已分配,则判断当前登录客服是否能给用户下单,若没分配,按之前流程,订单生成时分配给当前登录客服
                $CrmModel = new CrmModel();
                $sale_id = $CrmModel->getSaleId($user->user_id);

                if ($sale_id && $sale_id != $operator_id) {
                    $CmsModel = new CmsModel();
                    $sale_name = $CmsModel->getUserName($sale_id);
                    return ['errcode' => -1, 'errmsg' => '该客户由 '.$sale_name.' 跟进,请联系主管重新指派'];
                }
            }     
            
            $address = DB::connection('order')->table('lie_user_address')->where('user_id', $user->user_id)->get(); // 收货地址  
            $invoice = DB::connection('order')->table('lie_taxinfo')->where('user_id', $user->user_id)->get(); // 发票信息
            
            $info['user']    = $user;
            $info['address'] = $address;
            $info['invoice'] = $invoice;               

            return ['errcode' => 0, 'errmsg' => '', 'data' => $info];
        }
    }

    // 获取用户最近的订单跟进业务员
    // public function findLastSaleId($user_id)
    // {
    //     $OrderModel = new OrderModel;

    //     $map['user_id'] = $user_id;
    //     $map['is_type'] = 0; // 过滤尽调数据
    //     $map[] = ['sale_id', '>', 0];

    //     $order = $OrderModel->where($map)->select('sale_id')->orderBy('order_id', 'desc')->first();

    //     return $order ? $order['sale_id'] : 0;
    // }

    // 选择收货地址
    public function selectAddr(Request $request)
    {
        if ($request->isMethod('post')) {
            $address_id = $request->input('address_id');

            $address = DB::connection('order')->table('lie_user_address')->where('address_id', $address_id)->first();

            $address->province_val = $this->getAddress($address->province);
            $address->city_val = $this->getAddress($address->city);
            $address->district_val = $this->getAddress($address->district);

            return ['errcode' => 0, 'errmsg' => '', 'data' => $address];
        }
    }

    // 选择发票信息
    public function selectInv(Request $request)
    {
        if ($request->isMethod('post')) {
            $tax_id = $request->input('tax_id');

            $invoice = DB::connection('order')->table('lie_taxinfo')->where('tax_id', $tax_id)->first();

            $invoice->province_val = $invoice->consignee_province ? $this->getAddress($invoice->consignee_province) : '';
            $invoice->city_val = $invoice->consignee_city ? $this->getAddress($invoice->consignee_city) : '';
            $invoice->district_val = $invoice->consignee_district ? $this->getAddress($invoice->consignee_district) : '';

            return ['errcode' => 0, 'errmsg' => '', 'data' => $invoice];
        }
    }

    // 根据发票类型选择发票信息
    public function selectInvType(Request $request)
    {
        if ($request->isMethod('post')) {
            $user_id = $request->input('user_id');
            $inv_type = $request->input('inv_type');

            if ($inv_type == 3) {
                $invoice = DB::connection('order')->table('lie_taxinfo')->where(['user_id' => $user_id, 'inv_type' => $inv_type])->get();
            } else {
                $invoice = DB::connection('order')->table('lie_taxinfo')->where('user_id', $user_id)->whereIn('inv_type', [2, 4])->get();
            }
            
            return ['errcode' => 0, 'errmsg' => '', 'data' => $invoice];
        }
    }

    // 获取可用优惠券
    public function getCoupon(Request $request)
    {
        if ($request->isMethod('post')) {        
            $data['uid'] = $request->input('uid');
            $data['order_goods_type'] = $request->input('goods_type');
            $data['cart_ids'] = $request->input('cart_ids');        

            $data['k1'] = time();
            $data['k2'] = md5(md5($data['k1']).'fh6y5t4rr351d2c3bryi');

            $url = Config('website.api_domain').'ucoupon/bestusebyordersystem';

            $response = json_decode(curlApi($url, $data), true); 

            if ($response['err_code'] == 0) {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg'], 'data'=>$response['data']];
            } else {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg']];
            }
        }
    }

    // 查询省市区
    public function getAddress($id)
    {
        $addr = DB::connection('order')->table('lie_region')->where('region_id', $id)->select('region_name')->first();

        return $addr ? $addr->region_name : '未匹配到ID:'.$id;
    }

	// 新增自营线下订单
    public function addOffline(Request $request) 
    {
        $info = $this->getPageInfo($request);

        $info['title'] = '新增自营线下订单';

        // 默认内部账号ID
        $internal = DB::connection('order')->table('lie_user_main')->where('mobile', Config('website.internal-account'))->select('user_id')->first();
        $info['internal_uid'] = $internal->user_id;

        // 默认内部账号收货地址
        $address = DB::connection('order')->table('lie_user_address')->where(['user_id' => $info['internal_uid'], 'is_default' => 1])->first();
        
        $address->province_val = $this->getAddress($address->province);
        $address->city_val     = $this->getAddress($address->city);
        $address->district_val = $this->getAddress($address->district); 
             
        $info['address'] = $address;
        $sale_id         = $request->user->userId;
        $info['sale_id'] = $request->user->userId;

        // 当前登录用户名称
        $userInfo = DB::table('user_info')->where('userId', $sale_id)->select('name')->first(); 
        $info['name'] = $userInfo->name;

        // 当前登录用户内部绑定手机
        $intracode = DB::table('lie_intracode')->where('admin_id', $sale_id)->select('user_id')->first();

        if (!empty($intracode)) {
            $user = DB::connection('order')->table('lie_user_main')->where('user_id', $intracode->user_id)->select('mobile')->first();

            $info['mobile'] = $user ? $user->mobile : '';
        } else {
            $info['mobile'] = '';
        }

        return view('addOffline', $info);
    }  

    // 获取SKU信息
    public function getSku(Request $request)
    {
        if ($request->isMethod('post')) {
            $url = Config('website.search-skuid');
            $data['id'] = $request->input('sku_id');

            $data['k1'] = time();
            $data['k2'] = md5(md5($data['k1']).'fh6y5t4rr351d2c3bryi');

            $response = json_decode(curlApi($url, $data, 'POST'), true); 

            if (!empty($response['data'])) {
                return ['errcode' => 0, 'errmsg' => '', 'data' => $response['data']];
            } else {
                return ['errcode' => -1, 'errmsg' => '未找SKU信息'];
            }
        }
    }

    // 添加到购物车
    public function addCart(Request $request)
    {
        if ($request->isMethod('post')) {        
            $data['uid'] = $request->input('uid');
            $data['id'] = $request->input('id');
            $data['num'] = $request->input('num');
            $data['buy'] = $request->input('buy');
            $data['delivery_place'] = $request->input('delivery_place');
            $data['type'] = 2; // 后台添加标记
            $data['pf'] = 4;

            $data['k1'] = time();
            $data['k2'] = md5(md5($data['k1']).'fh6y5t4rr351d2c3bryi');

            $url = Config('website.api_domain').'cart/addByOrderSystem';

            $response = json_decode(curlApi($url, $data), true); 
        
            if ($response['err_code'] == 0) {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg'], 'data'=>$response['data']];
            } else {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg']];
            }
        }
    }

    // 切换销售类型
    public function switchSaleType(Request $request)
    {
        if ($request->isMethod('post')) {        
            $data['sale_type'] = $request->input('sale_type');
            $data['cart_ids'] = $request->input('cart_ids');

            $data['k1'] = time();
            $data['k2'] = md5(md5($data['k1']).'fh6y5t4rr351d2c3bryi');

            $url = Config('website.api_domain').'cart/switchSaleType';

            $response = json_decode(curlApi($url, $data), true);  

            return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg']];
        }
    }

    // 获取购物车列表
    public function cartList(Request $request)
    {
        if ($request->isMethod('post')) {        
            $data['uid'] = $request->input('uid');
            $data['type'] = $request->input('type');
            $data['delivery_place'] = $request->input('delivery_place');

            $data['k1'] = time();
            $data['k2'] = md5(md5($data['k1']).'fh6y5t4rr351d2c3bryi');

            $url = Config('website.api_domain').'cart/listsByOrderSystem';

            $response = json_decode(curlApi($url, $data), true); 
         
            if ($response['err_code'] == 0) {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg'], 'data'=>$response['data']];
            } else {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg']];
            }
        }
    }

    // 修改购物车商品数量
    public function changeNum(Request $request)
    {
        if ($request->isMethod('post')) {        
            $data['uid'] = $request->input('uid');
            $data['num'] = $request->input('num');
            $data['cart_id'] = $request->input('cart_id');
            $type = $request->input('type'); // 3-批量添加的商品
            $goods_type = $request->input('goods_type');

            $data['k1'] = time();
            $data['k2'] = md5(md5($data['k1']).'fh6y5t4rr351d2c3bryi');

            if ($type == 3 && $goods_type == 1) { // 联营批量添加
                $url = Config('website.api_domain').'cart/changeNumForJoint';
            } else {
                $url = Config('website.api_domain').'cart/changeNumByOrderSystem';
            }    

            $response = json_decode(curlApi($url, $data), true); 
            
            if ($response['err_code'] == 0) {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg'], 'data'=>$response['data']];
            } else {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg']];
            }            
        }
    }

    // 删除购物车商品
    public function deleteGoods(Request $request)
    {
        if ($request->isMethod('post')) {        
            $data['uid'] = $request->input('uid');
            $data['cart_id'] = $request->input('cart_id');

            $data['k1'] = time();
            $data['k2'] = md5(md5($data['k1']).'fh6y5t4rr351d2c3bryi');

            $url = Config('website.api_domain').'cart/deleteByOrderSystem';

            $response = json_decode(curlApi($url, $data), true);  
            
            if ($response['err_code'] == 0) {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg'], 'data'=>$response['data']];
            } else {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg']];
            }            
        }
    }

    // 确认订单信息
    public function confirm(Request $request) 
    {
        if ($request->isMethod('post')) {  
            $is_online = $request->input('is_online');
            $data['uid'] = $request->input('uid', 0);
            $data['address_id'] = $request->input('address_id', 0);
            $data['cart_id'] = $request->input('cart_ids', '');
            $data['user_coupon_id'] = $request->input('user_coupon_id', 0);
            if ($is_online == 0) {
                $data['type'] = 3; // 自营线下
            } else if ($is_online == 2) {
                $data['type'] = 4; // 自营其他业务
                $data['business_type'] = $request->input('business_type', 0); // 业务类型
            } else {
                $data['type'] = $request->input('goods_type');
            }
            
            $data['k1'] = time();
            $data['k2'] = md5(md5($data['k1']).'fh6y5t4rr351d2c3bryi');

            $url = Config('website.api_domain').'cart/confirm';

            $response = json_decode(curlApi($url, $data), true); 
          
            if ($response['err_code'] == 0) {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg'], 'data'=>$response['data']];
            } else {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg']];
            }            
        }
    }

    // 检查期货库存
    public function checkStock(Request $request)
    {
        if ($request->isMethod('post')) {  
            $cart_info = json_decode($request->input('cart_info', ''), true);
            $sale_type = $request->input('sale_type', 1);

            $data['k1'] = time();
            $data['k2'] = md5(md5($data['k1']).'fh6y5t4rr351d2c3bryi');

            $url = Config('website.api_domain').'goods/detail';

            $warning_arr = array();

            foreach ($cart_info as $k=>$v) {
                $data['id'] = $v['gid'];

                $res = curlApi($url, $data);

                if (!$res) return ['errcode'=>1, 'errmsg'=>'未访问到API接口,请重新尝试'];

                $response = json_decode($res, true); 

                if ($sale_type == 1) {
                    // 现货订单--判断下单数量是否大于库存,若大于则弹出提示信息
                    if ($v['num'] > $response['data']['goods_number']) {
                        $temp['goods_name'] = $response['data']['goods_name']; // 型号
                        $temp['stock'] = $response['data']['goods_number']; // 可用库存
                        $warning_arr[] = $temp;
                        $errmsg = '库存不足,建议设置为期货类型';
                    } 
                } else {
                    // 期货订单--判断下单数量是否小于库存,若小于则弹出提示信息
                    if ($v['num'] < $response['data']['goods_number']) {
                        $temp['goods_name'] = $response['data']['goods_name']; // 型号
                        $temp['stock'] = $response['data']['goods_number']; // 可用库存
                        $warning_arr[] = $temp;
                        $errmsg = '您提交的数量有实际库存,建议直接购买现货';
                    } 
                }
            }

            if (!empty($warning_arr)) {
                return ['errcode' => -1, 'errmsg' => $errmsg, 'data'=>json_encode($warning_arr)];
            }
            
            return ['errcode' => 0, 'errmsg' => ''];           
        }
    }

    // 提交订单
    public function create(Request $request)
    {
        if ($request->isMethod('post')) {        
            $data['uid']        = $request->input('uid');
            $data['sale_id']    = $request->input('sale_id');
            $data['address_id'] = $request->input('address_id');
            $data['cart_id']    = implode(',', $request->input('cart_ids'));
            $data['type']       = $request->input('type');
            $data['remark']     = $request->input('remark');

            if ($data['type'] == 3) { // 自营线下订单
                $data['address_name']   = $request->input('address_name');
                $data['address_mobile'] = $request->input('address_mobile');
            } else { // 联营、自营线上订单
                $data['role']           = $request->input('role', '');
                $data['tax_id']         = $request->input('tax_id');
                $data['user_coupon_id'] = $request->input('user_coupon_id');

                if ($data['type'] == 4) {
                    $data['business_type'] = $request->input('business_type');
                }
            }

            $data['sale_type'] = $request->input('sale_type', ''); // 自营线上选择销售类型

            $data['k1'] = time();
            $data['k2'] = md5(md5($data['k1']).'fh6y5t4rr351d2c3bryi');

            $url = Config('website.api_domain').'order/create';

            $response = json_decode(curlApi($url, $data), true); 
           
            if ($response['err_code'] == 0) {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg'], 'data'=>['order_id' => $response['data'], 'type' => $data['type']]];
            } else {
                return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg']];
            }            
        }
    }

    // 提交订单成功页面
    public function success(Request $request)
    {
        $info = $this->getPageInfo($request);

        $order_id = $request->input('order_id');
        $type = $request->input('type');

        switch ($type) {
            case 1: $title = '新增联营订单'; break;
            case 2: $title = '新增自营线上订单'; break;
            case 3: $title = '新增自营线下订单'; break;
            case 4: $title = '新增自营其他业务订单'; break;
        }

        $info['title'] = $title;
        $info['paths'] = [["title" => $title, "href" => '#']];
        $info['type'] = $type;

        $order = DB::connection('order')->table('lie_order')->where('order_id', $order_id)->first();

        if ($type != 3) {
            $user = DB::connection('order')->table('lie_user_main')->where('user_id', $order->user_id)->select('mobile')->first();
            $info['user_info'] = $user;
        }

        $info['order_info'] = $order;  

        return view('success', $info);
    }

    // 自营线下订单审核
    public function selfCheck(Request $request)
    {
        if ($request->isMethod('post')) {        
            $order_id = $request->input('order_id');
            $status = $request->input('status');

            // 审核通过推到WMS
            if ($status == 4) {
                $data['order_id'] = $order_id;
                $data['k1'] = time();
                $data['k2'] = md5(md5($data['k1']).'fh6y5t4rr351d2c3bryi');

                $url = Config('website.api_domain').'order/wmsOrder';

                $response = json_decode(curlApi($url, $data), true); 

                if ($response['err_code'] == 0) {
                    $orderInfo = DB::connection('order')->table('lie_order')->where('order_id', $order_id)->first();

                    DB::beginTransaction();
                    try {
                        // 插入到order_price
                        $price['order_id'] = $order_id;
                        $price['order_sn'] = $orderInfo->order_sn;
                        $price['price_type'] = -1;
                        $price['price'] = '-'.$orderInfo->order_amount;
                        $price['create_time'] = time();

                        $priceAdd = DB::connection('order')->table('lie_order_price')->insert($price);

                        // 添加付款记录
                        $pay['user_id'] = $orderInfo->user_id; 
                        $pay['order_id'] = $order_id; 
                        $pay['order_sn'] = $orderInfo->order_sn; 
                        $pay['pay_id'] = 0; // 支付方式ID
                        $pay['pay_name'] = '交通银行'; // 支付方式名
                        $pay['pay_type'] = 1;
                        $pay['pay_amount'] = $orderInfo->order_amount;
                        $pay['is_paid'] = 1;
                        $pay['create_time'] = time();
                        $pay['pay_time'] = time();

                        $payLog = DB::connection('order')->table('lie_pay_log')->insert($pay);

                        // 待发货状态
                        $update = DB::connection('order')->table('lie_order')->where('order_id', $order_id)->update(['status' => $status, 'pay_time' => time(), 'wms_syn' => 1]);

                        if ($update && $priceAdd && $payLog) {
                            DB::commit();
                        }
                    } catch (Exception $e) {
                        DB::rollBack();
                    }   
                } else {
                    return ['errcode' => $response['err_code'], 'errmsg' => $response['err_msg']];
                }

                $event = '自营线下订单审核通过';
            } else {
                $data['order_id'] = $request->input('order_id');
                $data['operator_id'] = $request->user->userId;
                $data['type'] = 3;

                $url = Config('website.api_domain').'order/cancel';

                $data['k1']  = time();
                $data['k2']  = md5(md5($data['k1']).'fh6y5t4rr351d2c3bryi');

                $temp = json_decode(curlApi($url, $data, "POST"), true);

                $event = '自营线下订单审核不通过';

                return array('errcode'=>$temp['err_code'],'errmsg'=>$temp['err_msg']);
            }  
            
            // 操作记录
            $log['order_id'] = $order_id;
            $log['operator_id'] = $request->user->userId;
            $log['operator_type'] = 2;
            $log['event'] = $event;
            $log['ip'] = get_client_ip();
            $log['create_time'] = time();
            $actionLog = DB::connection('order')->table('lie_order_action_log')->insert($log);

            if (!$actionLog){
                errorLog(Error::E_ADD_FAILED, '添加操作记录失败');
                return ['errcode'=>Error::E_ADD_FAILED, 'errmsg'=>'添加操作记录失败'];
            } 

            if (!empty($update)) {
                return ['errcode' => 0, 'errmsg' => '操作成功'];
            } else {
                return ['errcode' => -1, 'errmsg' => '操作失败'];
            }            
        }
    }

    // 商品批量导入
    public function batchGoodsImport(Request $request)
    {
        $file = $_FILES['file']; // $request->file('file')
        $filePath = $file['tmp_name']; // 临时路径

        // 获取导入内容
        $excel = [];
        Excel::load($filePath, function($reader) use(&$excel){
            $data = $reader->getSheet(0);
            $excel = $data->toArray();
        }, 'GBK');
  
        if (empty($excel)) return ['errcode' => 1, 'errmsg' => '未获取到模板内容,请检查模板内容数据格式'];

        $type = $request->input('type'); // 1.联营 2.自营

        if ($type == 1) {
            $map = Config('params.joint_map');
            $type_val = '联营';
        } else {
            $map = Config('params.self_map');
            $type_val = '自营';
        }

        if (count($map) != count($excel[0])) {
            errorLog(Error::E_IMPORT_TEMP_ERR, '导入模板错误,需导入'.$type_val.'模板');
            return ['errcode' => 1, 'errmsg' => '导入模板错误,需导入'.$type_val.'模板'];
        }

        $excel = $this->handleData($excel, $map); // 处理数据

        // 验证excel内容
        $valid = $this->excelValid($excel); 

        if ($valid['errcode'] != 0) {
            errorLog(Error::E_IMPORT_VALID_FAILED, $valid['errmsg']);
            return $valid;
        }

        array_shift($excel); // 删除第一行

        // 推送到API
        $data['data']           = $excel;
        $data['operator_id']    = $request->user->userId;
        $data['type']           = 3; // 后台批量添加标记
        $data['uid']            = $request->input('user_id'); 
        $data['delivery_place'] = $request->input('delivery_place'); 

        if ($type == 1) { // 联营
            $url = Config('website.api_domain').'cart/addBatchByOrderSystem';
        } else {
            $url = Config('website.api_domain').'cart/addBatch';
        }
        
        $data['k1']  = time();
        $data['k2']  = md5(md5($data['k1']).'fh6y5t4rr351d2c3bryi');

        $temp = json_decode(curlApi($url, $data, "POST"), true);

        return ['errcode' => $temp['err_code'], 'errmsg' => $temp['err_msg']];
    }

    // 验证导入excel
    public function excelValid($excel)
    {
        $count = count($excel);

        if ($count == 1) return ['errcode' => 1, 'errmsg' => '未填写物料信息'];

        // 获取excel菜单栏必填项
        $required = array_filter($excel[0], function($v) {
            return strpos($v, '*') === false ? false : true;
        });

        $required_keys = array_keys($required); // 必填项keys   

        $err = ['errcode' => 0, 'errmsg' => '验证成功']; // 提示信息
        array_walk($excel, function($val, $key) use($required, $required_keys, &$err) {
            // 跳过第一条
            if ($key != 0) {
                foreach ($val as $k=>$v) {
                    if (in_array($k, $required_keys)) {
                        if (empty($v)) { // 若必填项值为空,返回提示信息
                            $err = ['errcode' => 1, 'errmsg' => $required[$k].'列,第'.$key.'行不能为空']; 
                            break;
                        }
                    }

                    // 自营ID、采购数量和单价为数字类型
                    if ($k == 'id' || $k == 'num' || $k == 'goods_price') {
                        if (!preg_match('/\d/', $v)) {
                            $err = ['errcode' => 2, 'errmsg' => $required[$k].'列,第'.$key.'行格式错误']; 
                            break;
                        }        
                    }
                } 
            }
        });
        
        return $err;
    }

    // 处理导入内容---赋上字段
    public function handleData($excel, $map)
    {
        $goods_info = array_map(function($val) use($map) {
            foreach ($val as $k=>$v) {
                $tmp[$map[$k]] = $v;
            }

            return $tmp;
        }, $excel);

        return $goods_info;
    }

    // 添加商品到订单
    public function addGoods(Request $request)
    {
        if ($request->isMethod('post')) {
            $add_type = $request->input('add_type', ''); // 新增类型

            if ($add_type) {
                $data['add_goods_name']    = $request->input('add_goods_name', '');
                $data['add_brand_name']    = $request->input('add_brand_name', '');
                $data['add_supplier_name'] = $request->input('add_supplier_name', '');
                $data['add_goods_number']  = $request->input('add_goods_number', 0);
                $data['add_goods_price']   = $request->input('add_goods_price', 0);
                $data['add_delivery_time'] = $request->input('add_delivery_time', '');
                $data['add_buyer_id']      = $request->input('add_buyer_id', 0);
                $data['add_batch']         = $request->input('add_batch', '');
                $data['add_remarks']       = $request->input('add_remarks', '');
            } else {
                $data['goods_id']    = $request->input('goods_id');
                $data['goods_num']   = $request->input('goods_num');
            }
            
            $data['order_id']    = $request->input('order_id');
            $data['add_type']    = $add_type;
            $data['operator_id'] = $request->user->userId;

            $url = Config('website.order_api_domain').'/order/addgoods';

            $response = json_decode(curlApi($url, $data, 'post'), true); 

            return ['errcode' => $response['errcode'], 'errmsg' => $response['errmsg']];
        }
    }

}