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
Normal viewHistoryPermalink
Switch branch/tag
  • CMS-Message
  • app
  • Http
  • Controllers
  • FootStoneApiController.php
FootStoneApiController.php 30 KB
李洋's avatar
Initial commit
60b5dc25
 
李洋 committed 7 years ago
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 843 844 845 846 847 848 849 850 851 852 853 854
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
use RedisDB;

class FootStoneApiController extends Controller
{
    public function Entrance(Request $request, $id ){
        //统一入口
        $this->$id($request, $id);
    }

    private function Export($errcode=0,$errmsg='成功',$data=''){
        echo json_encode(['errcode'=>$errcode,'errmsg'=>$errmsg,'data'=>$data]);
        exit();
    }


    //获取猎芯品牌
    private function liexinbrand($request,$id,$limit=20){
        $map=strtoupper($request->input('brand_name'));
        $result= DB::connection('spu')->table('lie_brand')->where('status','=',1);

        if($map){
            $result=$result->where('brand_name','like','%'.$map.'%');
        }
        $content=$result->select('brand_name','brand_id')->paginate($limit);
        if(!$content){
            $this->Export(11005,'没有查找到猎芯标准制造商');
        }
        if($map){
            foreach ($content as $k=>$v){
                $content[$k]->brand_name= str_ireplace($map,'<span style="color: red">'.$map.'</span>',$v->brand_name);
            }
        }


        $this->Export(0,'成功',$content);
    }

    //获取供应商品牌
    private function supplier_brand($request,$id){
        $map=strtoupper($request->input('supplier_brand_name'));
        $supplier_name=$request->input('supplier_name');

        $DB= DB::connection('spu')->table('lie_brand_mapping')->where('status','=',1);
        if($map){
            $DB=$DB->where('supplier_brand_name','like','%'.$map.'%');
        }
        if($supplier_name){
            $DB=$DB->where('supplier_name','=',$supplier_name);
        }
        $DB=$DB->where('status','=',1)->where('liexin_brand_id','=',0);

        $data='';
        $content=$DB->select('supplier_brand_name','id')->limit(1000)->get();
        if(count($content)>0){
            foreach ($content as $k=>$v){
                $v=(array)$v;
                $field=Getzimu($v['supplier_brand_name']);
                if(($field || json_encode($field)==='null')){
                    $data[$field][$k]=$v;
                }else{
                    $data[strtoupper(substr( $v['supplier_brand_name'], 0, 1))][$k]=$v;
                }
                if($map){
                    $data[strtoupper(substr( $v['supplier_brand_name'], 0, 1))][$k]['supplier_brand_name']= str_ireplace($map,'<span style="color: red">'.$map.'</span>',$data[strtoupper(substr( $v['supplier_brand_name'], 0, 1))][$k]['supplier_brand_name']);
                }
            }
        }
        $content=null;
        $request=null;
        if($data){
            ksort($data);
        }
        $this->Export(0,'成功',$data);
    }

    //品牌映射关联
    private function binding_mapping($request){
        $id=$request->input('id');
        $brand_id=$request->input('brand_id');
        if(empty($id) || !is_array($id) ){
            $this->Export(11001,'请选择需映射制造商','');
        }
        if(empty($brand_id)){
            $this->Export(11002,'请选择猎芯标准制造商','');
        }


        $data= DB::connection('spu')->table('lie_brand_mapping')->where('status','=',1)->where('liexin_brand_id','=',0)->lists('supplier_brand_name', 'id');
        foreach ($id as $k=>$v){
            if(!$data[$v]){
                $this->Export(11003,"该制造商{$data[$v]}暂时不能映射",'');
            }
        }

        $errinfo='';
        foreach ($id as $k=>$v){
            $add=DB::connection('spu')->table('lie_brand_mapping')->where('id', $v)->update(['liexin_brand_id' => $brand_id]);
            if(!$add){
                $errinfo .=','.$data[$v];
            }
        }
        if($errinfo){
            $this->Export(11004,"制造商{$errinfo}映射失败",'');
        }
        $this->Export();
    }

    //新增品牌
    private function addbrand($request){
       $data=$request->input();
        if(empty($data['brand_name'])){
            $this->Export(11010,'请输入制造商名称','brand_name');
        }
        if(empty($data['brand_logo'])){
//            $this->Export(11011,'制造商图标必须上传','brand_name');
        }

        $input_model = [];
        $input_model['brand_id']=$data['brand_id'];
        $input_model['brand_name']=$data['brand_name'];
        $input_model['brand_logo']=$data['brand_logo'];
        $input_model['brand_desc']=$data['brand_desc'];
        $input_model['status']=$data['status'];
        $input_model['main_product']=$data['main_product'];
        $input_model['brand_brief']=$data['brand_brief'];
        if(isset($data['brand_area']))
        {
            $input_model['brand_area']=$data['brand_area'];
        }

        $db=DB::connection('spu')->table('lie_brand');
        if(!empty($input_model['brand_id'])){
            $resurt=$db->where('brand_id',$input_model['brand_id'])->update($input_model);

            if(!$resurt) {
                $this->Export(11011, '修改制造商失败', 'sql');
            }

        }else{
            $find=DB::connection('spu')->table('lie_brand')->where('brand_name',$input_model['brand_name'])->first();
            if($find){
                $this->Export(11012, '当前品牌名字已经存在', 'brand_name');
            }
            $input_model['create_time']=time();
            $resurt=$db->insert($input_model);

            if(!$resurt) {
                $this->Export(11013, '新增制造商失败', 'sql');
            }
        }

        $this->Export(0,empty($input_model['brand_id'])? '新增成功':'修改成功');
    }

    //获取类
    private function Obtain_class($request){
        $map=$request->input('class_id')? $request->input('class_id'):0;
        $list= DB::connection('spu')->table('lie_classify')->where('status',1)->where('parent_id',$map)->get();
        $this->Export(0, '', $list);
    }

    //批量下架SKU
    private function off_shelf($request){
        $map=$request->input('spu_id');
        if(empty($map) || !is_array($map)){
            $this->Export(14021,'请选择要操作的SPU','0');
        }

        $status=$request->input('status');

        $Redis=RedisDB::connection();

        switch ($status){
            case 'offshelf':
                $updata['status']=3;
                $errinfo='成功下架';
                break;
            case 'passed':
                $errinfo='成功上架';
                $updata['status']=1;
                break;
            case 'negative':
                $updata['status']=2;
                $errinfo='审核不通过';
                break;
        }

        $i=0;
        foreach ($map as $k=>$v){
            $dbinfo=resolve($v);
            $list= DB::connection('spu')->table($dbinfo['table'])->where('spu_id',$v)->update($updata);
            if($list){
                $info=json_decode($Redis->hget('spu',$v),true);
                $info['status']=$updata['status'];
                Handle_spu($info);
                $i++;
            }
        }
        $this->Export(0,$errinfo.$i.'件SPU',$i);
    }

    //生成分类归类信息redis
    private function generate_class_redis(){
        $Redis = RedisDb::connection();
        $list=DB::connection('spu')->table('lie_classify')->get();
        $class=tree_class($list);

        foreach ($class as $k1=>$v1){

            $class[$k1]=(array)$class[$k1];

            foreach ($class[$k1]['lower'] as $k2=>$v2){
                $data=[];
                $class[$k1]['lower'][$k2]=(array)$class[$k1]['lower'][$k2];
                foreach ($class[$k1]['lower'][$k2]['lower'] as $k3=>$v3){

                    $class[$k1]['lower'][$k2]['lower'][$k3]=(array)$class[$k1]['lower'][$k2]['lower'][$k3];

                    $value=$v1->class_id.','.$v2->class_id;
                    $data[$v3->class_id]=$value;
                }
                $Redis->hmset('class_ing',$data);
            }
        }

        $list=$Redis->hget('class_ing_'.'*');
        dump($list);
    }

    //新增/编辑分类接口
    private function addclassify($request){
        $data=$request->input();
        if(empty($data['class_name'])){
            $this->Export(12010,'请输入分类名称','class_name');
        }

        //通过模型取出需要传递的数据,避免_url的出现
        $input_model=[];
        $input_model['class_id']=$data['class_id'];
        $input_model['class_name']=$data['class_name'];
        $input_model['parent_id']=$data['parent_id'];
        $input_model['status']=$data['status'];
        $input_model['class_icon']=$data['class_icon'];

        $db=DB::connection('spu')->table('lie_classify');
        if(!empty($input_model['class_id'])){
            $resurt=$db->where('class_id',$input_model['class_id'])->update($input_model);
            if(!$resurt) {
                $this->Export(12011, '编辑分类失败', 'sql');
            }
            //清除class和tree_class缓存
            $this->redisRemove('class');
            $this->redisRemove('tree_class');
        }else{
            //同名&&同父级则重复
            $isDuplicated = $this->isDuplicatedInSameLevel($input_model['class_name'],$input_model['parent_id']);
            if($isDuplicated){
                $this->Export(12012, '当前分类名字已经存在', 'class_name');
            }

            $input_model['create_time']=time();
            $result=$db->insert($input_model);

            if(!$result) {
                $this->Export(12013, '新增分类失败', 'sql');
            }
        }
        $this->Export(0,empty($input_model['class_id'])? '新增成功':'修改成功');
    }

    //新增/编辑供应商接口
    private function addsupplier($request)
    {
        $data=$request->input();
        if(empty($data['supplier_name'])){
            $this->Export(15010,'请填写供应商');
        }
        if(empty($data['code']))
        {
            $this->Export(15011,'请选择供应商负责人');
        }
        $db=DB::connection('spu')->table('lie_supplier');

        if(!empty($data['supplier_id'])){
            $model_update = [];
            $model_update['type_id']=$data['type_id'];
            $model_update['supplier_name']=$data['supplier_name'];
            $model_update['code']=$data['code'];
            $model_update['status']=$data['status'];
            $resurt=$db->where('supplier_id',$data['supplier_id'])->update($model_update);
            if(!$resurt) {
                $this->Export(15012, '修改供应商失败', 'sql');
            }
            //清除class和tree_class缓存
            $this->redisRemove('supplier');
        }else{
            $find=DB::connection('spu')->table('lie_supplier')->where('supplier_name',$data['supplier_name'])->select('supplier_id')->first();
            if($find){
                $this->Export(15013, '当前供应商名字已经存在', 'supplier_name');
            }
            $model_insert = [];
            $model_insert['type_id']=$data['type_id'];
            $model_insert['supplier_name']=$data['supplier_name'];
            $model_insert['code']=$data['code'];
            $model_insert['status']=$data['status'];
            $model_insert['create_time']=time();
            $resurt=$db->insert($model_insert);

            if(!$resurt) {
                $this->Export(15014, '新增供应商失败', 'sql');
            }
        }

        $this->Export(0,empty($data['supplier_id'])? '新增成功':'修改成功');
    }

    //点击变更供应商状态接口
    private function operation_supplier_status($request)
    {
        $data = $request->input();
        $db=DB::connection('spu')->table('lie_supplier');
        $supplier = $db->where('supplier_id',$data['supplier_id'])->first();
        $supplier->status = $data['status'];
        $supplier = (array)$supplier;
        $result = $db->where('supplier_id',$data['supplier_id'])->update($supplier);
        if(!$result)
        {
            $this->Export(15015, '修改供应商状态失败');
        }
        $this->Export(0,"修改成功");
    }

    //新增/编辑SPU接口
    private function addspu($request){

        $data=$request->input();
        if(empty($data['class_id3'])){
            $this->Export(13010,'请选择分类名称','class_id3');
        }
        if(empty($data['class_id1']) || empty($data['class_id2']))
        {
            $this->Export(13011,'提交分类名称有误');
        }
        if(empty($data['spu_name'])){
            $this->Export(13012,'请填写SPU型号','spu_name');
        }
        if(empty($data['brand_id'])){
            $this->Export(13013,'请选择制造商','brand_id');
        }

        //通过模型取出需要传递的数据,避免_url的出现
        $input_model=[];
        $input_model['spu_id']=$data['spu_id'];
        $input_model['class_id1']=$data['class_id1'];
        $input_model['class_id2']=$data['class_id2'];
        $input_model['class_id3']=$data['class_id3'];
        $input_model['brand_id']=$data['brand_id'];
        $input_model['spu_name']=$data['spu_name'];
        $input_model['status']=$data['status'];
        $input_model['images_l']=$data['images_l'];
        $input_model['images_s']=$data['images_s'];
        $input_model['encap']=$data['encap'];
        $input_model['pdf']=$data['pdf'];
        $input_model['spu_brief']=$data['spu_brief'];
        $input_model['has_rohs']=$data['has_rohs'];
        $input_model['attrs']=$data['attrs'];
        $input_model['spu_detail']=$data['spu_detail'];
        $input_model['bussiness_area']=$data['bussiness_area'];

        $collert=$input_model['spu_id'];
        if($collert)//编辑
        {
            $dt = resolve($collert);
            $input_model['update_time']=time();
            $result = DB::connection($dt['db'])->table($dt['table'])->where('spu_id',$input_model['spu_id'])->update($input_model);
            if(!$result) {
                $this->Export(13014, '编辑SPU失败', 'sql');
            }
            //编辑成功,写入Redis
            $info = DB::connection($dt['db'])->table($dt['table'])->where('spu_id',$input_model['spu_id'])->first();
            Handle_spu($info);
            //写入mongo
            $result_mongo = save_mongo_spu($info->brand_id,$info->spu_name,$info->spu_id);
//            if(false == $result_mongo)
//            {
//                $this->Export(13017, '编辑时写入mongo失败', 'sql');
//            }
        }else//新增
        {
            //在同个品牌下不存在spu才能新增
            if(!only_judge_spu($input_model['brand_id'],$input_model['spu_name']))
            {

                //添加到哪个库
                $spu_id = structure();
                $dbinfo = resolve($spu_id);
                $input_model['create_time']=time();
                $input_model['update_time']=time();
                $input_model['sale_time']=time();
                $input_model['spu_id']=$spu_id;
                $result= DB::connection($dbinfo['db'])->table($dbinfo['table'])->insert($input_model);
                if(!$result)
                {
                    $this->Export(13015, '新增SPU失败', 'sql');
                }
                //新增spu成功,写入Redis
                $info = DB::connection($dbinfo['db'])->table($dbinfo['table'])->where('spu_id',$input_model['spu_id'])->first();
                Handle_spu($info);
                //写入mongo
                save_mongo_spu($info->brand_id,$info->spu_name,$info->spu_id);
            }else
            {
                $this->Export(13016, '新增SPU失败,该品牌中已存在同名SPU', 'sql');
            }
        }
        //推送通知ES
        Push_update($input_model['spu_id'],$type='spu');
        $this->Export(0,$collert? '编辑成功':'新增成功');
    }

    //新增/编辑SKU接口
    private function addsku($request){
        $data=$request->input();
        if(empty($data['supplier_id']))
        {
            $this->Export(14011,'请选择供应商','supplier_id');
        }
        if(empty($data['goods_name']))
        {
            $this->Export(14012,'请选择SKU型号','goods_name');
        }
        if(empty($data['stock']))
        {
            $this->Export(14013,'请填写库存量','stock');
        }
        if(empty($data['moq']))
        {
            $this->Export(14014,'请填写起订量','moq');
        }
        if(empty($data['mpq']))
        {
            $this->Export(14015,'请填写标准包装量','mpq');
        }
        if(empty($data['hk_delivery_time']))
        {
            $this->Export(14016,'请填写香港货期','hk_delivery_time');
        }
        if(empty($data['cn_delivery_time']))
        {
            $this->Export(14017,'请填写大陆货期','cn_delivery_time');
        }
        //通过模型取出需要传递的数据,避免_url的出现
        $input_model=[];
        $input_model['goods_id']=$data['goods_id'];
        $input_model['spu_id']=$data['spu_id'];
        $input_model['goods_name']=$data['goods_name'];
        $input_model['supplier_id']=$data['supplier_id'];
        //新增的时候根据supplier_id查商品类型  编辑的时候带过来
        if(isset($data['goods_type']))
        {
            $input_model['goods_type']=$data['goods_type'];
        }else
        {
            $info = DB::connection('spu')->table('lie_supplier')->where('supplier_id',$data['supplier_id'])->first();
            $input_model['goods_type'] = $info->type_id;
        }
        $input_model['goods_status']=$data['goods_status'];
        if(isset($data['encoded']))
        {
            $input_model['encoded']=$data['encoded'];
        }
        $input_model['batch_sn']=$data['batch_sn'];
        $input_model['moq']=$data['moq'];
        $input_model['mpq']=$data['mpq'];
        $input_model['stock']=$data['stock'];
        $input_model['hk_delivery_time']=$data['hk_delivery_time'];
        $input_model['cn_delivery_time']=$data['cn_delivery_time'];
        $input_model['goods_details']=$data['goods_details'];
        $input_model['ladder_price']=$data['ladder_price'];
        $input_model['single_price']=$data['single_price'];
        $input_model['remark']=$data['remark'];
        $input_model['goods_images']=$data['goods_images'];

        $collert=$input_model['goods_id'];
        if($collert)//编辑
        {
            $dt = resolve($collert);
            $input_model['update_time']=time();
            $result = DB::connection($dt['db'])->table($dt['table'])->where('goods_id',$input_model['goods_id'])->update($input_model);
            if(!$result) {
                $this->Export(14018, '编辑SKU失败', 'sql');
            }else
            {
                //编辑成功,写入Redis
                $info = DB::connection($dt['db'])->table($dt['table'])->where('goods_id',$input_model['goods_id'])->first();
                Handle_sku($info);

                //写到mongo
                save_mongo_sku($info->spu_id,$info->supplier_id,$info->moq,$info->goods_id);
            }
        }else//新增
        {
            if(!only_judge_sku($input_model['spu_id'],$input_model['supplier_id'],$input_model['moq']))
            {
                //添加到哪个库
                $sku_id = structure('sku');
                $dbinfo = resolve($sku_id);
                $input_model['create_time']=time();
                $input_model['update_time']=time();
                $input_model['goods_id']=$sku_id;
                $result= DB::connection($dbinfo['db'])->table($dbinfo['table'])->insert($input_model);
                if(!$result)
                {
                    $this->Export(14019, '新增SKU失败', 'sql');
                }else
                {
                    //新增成功,写入Redis
                    $info = DB::connection($dbinfo['db'])->table($dbinfo['table'])->where('goods_id',$input_model['goods_id'])->first();
                    Handle_sku($info);

                    //写到mongo
                    save_mongo_sku($info->spu_id,$info->supplier_id,$info->moq,$info->goods_id);
                }
            }else
            {
                $this->Export(14020, '新增SKU失败,该品牌中已存在同名SKU', 'sql');
            }
        }
        $push_result = Push_update($input_model['goods_id'],$type='sku');
        $this->Export(0,$collert? '编辑成功':'新增成功');
    }

    //分类映射
    private function classifymapping($request){

        $map=$request->input('cm_id');
        $name=$request->input('supplier_name');
        $db=DB::connection('spu')->table('lie_classify_mapping')->where('liexin_class_id',0);
        if($name){
            $db=$db->where('supplier_name',$name);
        }
        $list=$db->where('parent_id',$map)->select('cm_id','cm_name','parent_id')->get();

        foreach ($list as $k=>$v){
            $result='';
            $result=DB::connection('spu')->table('lie_classify_mapping')
                ->where('liexin_class_id',0)
                ->where('parent_id',$v->cm_id)
                ->select('cm_id')
                ->first();
            if($result){
                $list[$k]->lower='1';
            }else{
                if($map==0){
                    unset($list[$k]);
                }
            }
        }
        $this->Export(0,'成功',$list);
    }

    //完成分类映射
    private function complete_mapping($request){
        $su_id=$request->input('su_id');
        $liexin_class_id=$request->input('liexin_class_id');
        if(empty($su_id) || !is_array($su_id)){
            $this->Export(12051,'请选择需映射的分类');
        }
        if(!$liexin_class_id){
            $this->Export(12052,'请选择猎芯标准分类');
        }
        $map['status']=1;
        $map['liexin_class_id']=0;
        $save['liexin_class_id']=$liexin_class_id;
        $i=0;
        foreach ($su_id as $k=>$v){
            $map['cm_id']=$v;
            $db=DB::connection('spu')->table('lie_classify_mapping')->where($map);
            $result=$db->update($save);
            if($result){
               $i++;
            }
        }
        $this->Export(0,'成功关联'.$i.'个分类');
    }

    private function create_spu_mongo()
    {
        set_time_limit(0);
        $starttime = explode(' ',microtime());

        $mongo=new \MongoClient(env('MONGO_HOST', ''));
        $mongodb=$mongo->ichunt->spu;


        $count=100;
        $db=DB::connection('spu');

        $i=0;
        for($table=0;$table<10;$table++){
            for ($p=1;$p<$count+1;$p++){
                $data=[];
                $list=$db->table('lie_spu_'.$table)->select('spu_name','brand_id','spu_id')->paginate(1000,['*'],'',$p);
                foreach ($list as $k=>$v){
                    $data[]=[
                        'spu_id'=>$v->spu_id,
                        'spu_name'=>$v->spu_name,
                        'brand_id'=>$v->brand_id
                    ];
                    $i++;
                }
                $result=$mongodb->batchInsert($data);
                $count=$list->lastPage();
                echo $i.'->';
            }
        }

        //程序运行时间
        $endtime = explode(' ',microtime());
        $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);
        $thistime = round($thistime,3);
        echo "本程序执行耗时:".$thistime." 秒。";

    }

    private function create_sku_mongo()
    {
        set_time_limit(0);
        $starttime = explode(' ',microtime());

        $mongo=new \MongoClient(env('MONGO_HOST', ''));
        $mongodb=$mongo->ichunt->sku;


        $count=100;

        for ($dbcode=0;$dbcode<10;$dbcode++){

            $db=DB::connection('sku_'.$dbcode);
            $i=0;
            for($table=0;$table<10;$table++){
                for ($p=1;$p<$count+1;$p++){
                    $data=[];
                    $list=$db->table('lie_sku_'.$table)->select('goods_id','spu_id','moq','supplier_id')->paginate(10000,['*'],'',$p);
                    foreach ($list as $k=>$v){
                        $data[]=[
                            'goods_id'=>intval($v->goods_id),
                            'spu_id'  =>intval($v->spu_id),
                            'supplier_id'=>intval($v->supplier_id),
                            'moq'=>intval($v->moq)
                        ];
                        $i++;
                    }
                    $mongodb->batchInsert($data);
                    $count=$list->lastPage();
                    echo $i.'->';
                }
            }
        }


        //程序运行时间
        $endtime = explode(' ',microtime());
        $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);
        $thistime = round($thistime,3);
        echo "本程序执行耗时:".$thistime." 秒。";

    }

    private function create_spu_redis($request){

        set_time_limit(0);

        $redis=RedisDB::connection();
        $db=DB::connection('spu');
        $table=$request->input('table');
        $data=[];
        $list=$db->table('lie_spu_'.$table)
            ->select('spu_id','class_id1','class_id2','brand_id','class_id3','spu_name','status','images_l','images_s','encap','pdf','spu_brief','has_rohs','attrs','spu_detail','sale_time','create_time','update_time','remark','bussiness_area')
            ->paginate(1000);

        foreach ($list as $k=>$v){
            $info=(array)$v;
            $info['class_id']=0;

            if(!empty($info['class_id1'])){
                $info['class_id']=$info['class_id1'];
            }
            if(!empty($info['class_id2'])){
                $info['class_id']=$info['class_id2'];
            }
            if(!empty($info['class_id3'])){
                $info['class_id']=$info['class_id3'];
            }

            $info['class_name']=$redis->hget('class',$info['class_id']);


            $info['brand_name']=$redis->hget('brand',$info['brand_id']);


            switch ($info['status']){
                case 0:
                    $info['status_name']='审核中';
                    break;
                case 1:
                    $info['status_name']='上架';
                    break;
                case 2:
                    $info['status_name']='审核不通过';
                    break;
                case 3:
                    $info['status_name']='下架';
                    break;
                default :
                    $info['status_name']='未知';
            }

            $info['create_time_s']=date('Y-m-d H:i',$info['create_time']);

            $data[$v->spu_id]=json_encode($info);
        }
        $result=$redis->hmset('spu',$data);
        $objdata['count']=$list->count();
        $objdata['last_page']=$list->lastPage();

        if($result=='OK'){
            $this->Export(0,'',$objdata);
        }else{
            $this->Export(20001,'写入Redis出错');
        }
    }

    private function sku_redis(){
        $redis=Redis::connection();


        $count=100;
        $i=0;
        for ($dbcode = 0;$dbcode < 10; $dbcode++ ) {
            $db=DB::connection('sku_'.$dbcode);
            for ($table = 0; $table < 10; $table++) {
                for ($p = 1; $p < $count + 1; $p++) {
                    $data = [];
                    $list = $db->table('lie_sku_' . $table)
                        ->select('goods_id', 'spu_id', 'old_goods_id', 'goods_name', 'goods_type', 'supplier_id', 'goods_status', 'encoded', 'batch_sn', 'moq', 'mpq', 'stock', 'hk_delivery_time', 'cn_delivery_time', 'goods_details', 'ladder_price', 'single_price', 'former_price', 'sale_time', 'create_time','update_time','remark','goods_images')
                        ->paginate(1000, ['*'], '', $p);
                    foreach ($list as $k => $v) {
                        $v = (array)$v;
                        $data[$v['spu_id']] = json_encode($v);
                        $i++;
                    }
                    $result = $redis->hmset('spu', $data);
                    $count = $list->lastPage();
                    echo $i . '->';
                }
            }
        }
        echo '初始化完成';
    }

    //同名同父级则重复
    private function isDuplicatedInSameLevel($class_name,$parent_id)
    {
        $find=DB::connection('spu')->table('lie_classify')->where('class_name',$class_name)->get();
        if($find)
        {
            foreach ($find as $classify)
            {
                if($classify->parent_id == $parent_id){
                    return true;
                }
            }
        }
        return false;
    }

    //解绑供应商
    private function Unbundling($request){
        $id=$request->input('supplier_id');
        $code=$request->input('code');
        if(empty($id)){
            $this->Export(15201,'网络连接错误');
        }
        $data['code']='';
        $db=DB::connection('spu')->table('lie_supplier')->where('supplier_id',$id)->where('code',$code)->update($data);
        if(!$db){
            $this->Export(15202,'解绑失败');
        }
        $this->Export(0,'解绑成功');
    }


    //修改SKU状态
    private function operation_sku_status($request){
        $map=$request->input('goods_id');
        if(empty($map) || !is_array($map)){
            $this->Export(14021,'请选择要操作的SKU','0');
        }
        $status=$request->input('status');

        $Redis=RedisDB::connection();

        switch ($status){
            case 'offshelf':
                $updata['goods_status']=3;
                $errinfo='成功下架';
                break;
            case 'passed':
                $errinfo='成功上架';
                $updata['goods_status']=1;
                break;
            case 'negative':
                $updata['goods_status']=2;
                $errinfo='审核不通过';
                break;
        }

        $i=0;
        foreach ($map as $k=>$v){
            $dbinfo=resolve($v);
            $list= DB::connection($dbinfo['db'])->table($dbinfo['table'])->where('goods_id',$v)->update($updata);
            if($list){
                $info=json_decode($Redis->hget('sku',$v),true);
                $info['goods_status']=$updata['goods_status'];
                Handle_sku($info);
                $i++;
            }
        }
        $this->Export(0,$errinfo.$i.'件SPU',$i);
    }

    //清空指定缓存
    private function redisRemove($key)
    {
        $Redis=RedisDB::connection();
        return $Redis->del($key);
    }

    //清空已存储的所有元素
    private function redisFlush()
    {
        $Redis=RedisDB::connection();
        return $Redis->flushall();
    }
}