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
  • CMS-Message
  • app
  • Http
  • function.php
  • 李洋's avatar
    手动发消息接口联调 · cfe21d14
    李洋 committed 7 years ago
    cfe21d14
function.php 25.8 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
<?php
/**
 * Created by PhpStorm.
 * User: yemxing
 * Date: 2017/10/25
 * Time: 16:51
 */

use Illuminate\Support\Facades\DB;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Redis;


//解析SPU和SKU的ID
function  resolve($id){
    $type=substr($id,0,1);
    $dbcode=substr($id,-2,1);
    $tablecode=substr($id,-1);
    if($type==1){
        $data['db']='sku_'.$dbcode;
        $data['table']='lie_sku_'.$tablecode;
    }
    if($type==2){
        $data['db']='spu';
        $data['table']='lie_spu_'.$tablecode;
    }
    return $data;
}

//构造SPU和SKU的ID
function  structure($type='spu'){
    if($type=='sku'){
        $id='1';
        $db=rand(0,9).rand(0,9);
    }else{
        $id='2';
        $db='0'.rand(0,9);
    }
    $id .=time().random(6,true).$db;

    return $id;
}


//将所有分类写进redis
function generate_class($class_id){
    $Redis= RedisDB::connection();
    $data=$Redis->hget('class',$class_id);
    if(!$data){
        $info= DB::connection('spu')->table('lie_classify')->select('class_id','class_name')->get();
        $data=false;
        foreach ($info as $k=>$v){
            $datas[$v->class_id]=$v->class_name;
            if($v->class_id==$class_id){
                $data=$v->class_name;
            }
        }
        $Redis->hmset('class',$datas);
    }
    return $data;
}


//将所有品牌写进redis
function generate_brand($brand_id){
    $Redis= RedisDB::connection();
    $data=$Redis->hget('brand',$brand_id);
    if(!$data){
        $info= DB::connection('spu')->table('lie_brand')->select('brand_id','brand_name')->get();

        $data=false;
        foreach ($info as $k=>$v){
            $datas[$v->brand_id]=$v->brand_name;

            if($v->brand_id==$brand_id){
                $data=$v->brand_name;
            }
        }
        $Redis->hmset('brand',$datas);
    }
    return $data;
}

//将所有供应商写进redis
function generate_supplier($supplier_id=0){
    $Redis= RedisDB::connection();
    $data=$Redis->hget('supplier',$supplier_id);

    if(!$data || empty($supplier_id)){
        $info= DB::connection('spu')->table('lie_supplier')->select('supplier_id','supplier_name')->get();

        $data=false;
        foreach ($info as $k=>$v){
            $datas[$v->supplier_id]=$v->supplier_name;

            if($v->supplier_id==$supplier_id){
                $data=$v->supplier_name;
            }
        }
        $Redis->hmset('supplier',$datas);
    }
    if(empty($data)){

    }
    return $data;
}


//处理SPU信息,并生成redis数据(单条)
function Handle_spu($info){
    $info=(array)$info;
    $Redis= RedisDB::connection();
    $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']);
    if(!isset($info['class_name'])){
        $info['class_name']=generate_class($info['class_id']);
    }

    $info['brand_name']=$Redis->hget('brand',$info['brand_id']);
    if(!isset($info['brand_name'])){
        $info['brand_name']=generate_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']);
    $Redis->hset('spu',$info['spu_id'],json_encode($info));
    return $info;
}

//处理sku数据并生成redis
function Handle_sku($info){
    $info=(array)$info;
    $Redis= RedisDB::connection();

//    $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->get('footstone_class_'.$info['class_id']);
//    if(!isset($info['class_name'])){
//        $info['class_name']=generate_class($info['class_id']);
//    }

    $spu=json_decode($Redis->hget('spu',$info['spu_id']),true);
    if(!$spu){
        $dbinfo = resolve($info['spu_id']);

        $spu = DB::connection($dbinfo['db'])->table($dbinfo['table'])->where('spu_id', $info['spu_id'])->first();

        if ($spu){
            $spu = Handle_spu($spu);
        }
    }
    $info['brand_name']=$spu['brand_name'];
    $info['pdf']=$spu['pdf'];
    $info['sku_img']=$spu['images_l'];
    if(empty($info['goods_name'])){
        $info['goods_name']=$spu['spu_name'];
    }
    $spu=null;//注销

    $info['supplier_name']=generate_supplier($info['supplier_id']);
    if(!is_array($info['ladder_price'])){
        $info['ladder_price']=json_decode($info['ladder_price'],true);
    }
    switch ($info['goods_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']='未知';
    }

    switch ($info['goods_type']){
        case 0:
            $info['type_name']='自营';
            break;
        case 1:
            $info['type_name']='联营';
            break;
        case 2:
            $info['type_name']='专卖';
            break;
        default :
            $info['type_name']='未知';
    }

    $info['create_time_s']=date('Y-m-d H:i',$info['create_time']);
    $Redis->hset('sku',$info['goods_id'],json_encode($info));
    return $info;
}


//生成分页
function generate_page($request,$total,$offset=20,$map='',$path='') {

    $perPage = $offset;
    if ($request->has('p')) {
        $current_page = $request->input('p');
        $current_page = $current_page <= 0 ? 1 :$current_page;
    } else {
        $current_page = 1;
    }

    $item = array_slice([], ($current_page-1)*$perPage, $perPage); //注释1
    $total = $total;

    $paginator =new LengthAwarePaginator($item, $total, $perPage, $current_page, [
        'pageName' => 'p',
        'path' => !empty($path)?'':Paginator::resolveCurrentPath(),  //注释2
    ]);

    $paginator=$paginator->appends($map);

    return $paginator;
}

//无限极分类
function tree_class($list,$pid=0){
    $data=[];
    foreach ($list as $k=>$v){
        if($v->parent_id==$pid){
            $data[$v->class_id]=$v;
            $return=tree_class($list,$v->class_id);
            if($return){
                $data[$v->class_id]->lower=$return;
            }
        }
    }
    return $data;
}

//无限极分类(映射)
function tree_class_su($list,$pid=0){
    $data=[];
    foreach ($list as $k=>$v){
        if($v->parent_id==$pid){
            $data[$v->cm_id]=$v;
            $return=tree_class_su($list,$v->cm_id);
            if($return){
                $data[$v->cm_id]->lower=$return;
            }
        }
    }
    $data=array_merge($data);
    return $data;
}

function curl($url, $params = false, $ispost = 0, $https = 0)
{
    $httpInfo = array();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if ($https) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
    }
    if ($ispost) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {
        if ($params) {
            if (is_array($params)) {
                $params = http_build_query($params);
            }
            curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
        } else {
            curl_setopt($ch, CURLOPT_URL, $url);
        }
    }

    $response = curl_exec($ch);

    if ($response === FALSE) {
//        echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    return $response;
}

/**
 * 统一密码加密
 * @param  [type] $pwd [description]
 * @return [type]      [description]
 */
function pwdhash($pwd, $salt)
{
    return md5(md5($pwd).$salt);
}

//Csrf接口验证
function Csrf($request){
//    $Redis =RedisDB::connection('api');
//
//    $key = 'api_csrf_'.Cookie::get('PHPSESSID');
//
//    $csrf = $Redis->get($key);
//
//    if (empty($csrf) || !$request->cookie('Yo4teW_csrf')) {
//
//        $csrf = md5(md5(rand().'csrf'));
//
//        $Redis->set($key, $csrf);//基础秘钥
//        $Redis->expire($key, 86400);//基础秘钥
//
//        setcookie('Yo4teW_csrf', $csrf,0,'/',Config::get('website.domain'));
//    }
}


/**
 * 生成随机数
 *
 * @param $length   int      随机数长度
 * @param $numeric  boolean  是否为纯数字
 * @return string  随机数
 */
function random($length, $numeric = 0) {
    $seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
    $seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
    if($numeric) {
        $hash = '';
    } else {
        $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
        $length--;
    }
    $max = strlen($seed) - 1;
    for($i = 0; $i < $length; $i++) {
        $hash .= $seed{mt_rand(0, $max)};
    }
    return $hash;
}


//判断SPU是否唯一
function only_judge_spu($brand_id,$spu_name){
    $mongo=new \MongoClient(env('MONGO_HOST', ''));
    $mongodb=$mongo->ichunt->spu;
    $result=$mongodb->findOne(array('brand_id'=>(int)$brand_id,'spu_name'=>$spu_name));
    if(!$result){
        return false;
    }else{
        return $result['spu_id'];
    }
}

//新增和修改mongo spu判断唯一数据
function save_mongo_spu($brand_id,$spu_name,$spu_id,$type=''){
    if(empty($brand_id) || empty($spu_name) || empty($spu_id)){
        return false;
    }
    $mongo=new \MongoClient(env('MONGO_HOST', ''));
    $mongodb=$mongo->ichunt->spu;
    if(empty($type)) {
        $result = $mongodb->findOne(['spu_id' => (int)$spu_id]);
    }else{
        $result=null;
    }
    $data['spu_id']=(int)$spu_id;
    $data['spu_name']=$spu_name;
    $data['brand_id']=(int)$brand_id;
    if($result){
        $map['spu_id']=(int)$spu_id;
        $save=$mongodb->update($map,$data);
    }else{
        $save=$mongodb->insert($data);
    }
    if($save){
        return true;
    }else{
        return false;
    }
}

//判断SKU是否唯一
function only_judge_sku($spu_id,$supplier_id,$moq){

    $mongo=new \MongoClient(env('MONGO_HOST', ''));
    $mongodb=$mongo->ichunt->sku;
    $result=$mongodb->findOne(['spu_id'=>(int)$spu_id,'supplier_id'=>(int)$supplier_id,'moq'=>(int)$moq]);
    if(!$result){
        return false;
    }else{
        return $result['goods_id'];
    }
}

//新增和修改mongo sku判断唯一数据
function save_mongo_sku($spu_id,$supplier_id,$moq,$sku_id,$type=''){
    if(empty($spu_id) || empty($supplier_id) || empty($moq) || empty($sku_id)){
        return false;
    }
    $mongo=new \MongoClient(env('MONGO_HOST', ''));
    $mongodb=$mongo->ichunt->sku;
    if(empty($type)){
        $result=$mongodb->findOne(['sku_id'=>(int)$sku_id]);
    }else{
        $result=null;
    }

    $data['spu_id']=(int)$spu_id;
    $data['supplier_id']=(int)$supplier_id;
    $data['moq']=(int)$moq;
    $data['goods_id']=(int)$sku_id;
    if($result){
        $map['goods_id']=(int)$sku_id;
        $save=$mongodb->update($map,$data);
    }else{
        $save=$mongodb->insert($data);
    }
    if($save){
        return true;
    }else{
        return false;
    }
}


//获取中文的首字母
function Getzimu($str)
{
    $str= iconv("UTF-8","gb2312", $str);//如果程序是gbk的,此行就要注释掉
    if (preg_match("/^[\x7f-\xff]/", $str))
    {
        $fchar=ord($str{0});
        if($fchar>=ord("A") and $fchar<=ord("z") )return strtoupper($str{0});
        $a = $str;
        $val=ord($a{0})*256+ord($a{1})-65536;
        if($val>=-20319 and $val<=-20284)return "A";
        if($val>=-20283 and $val<=-19776)return "B";
        if($val>=-19775 and $val<=-19219)return "C";
        if($val>=-19218 and $val<=-18711)return "D";
        if($val>=-18710 and $val<=-18527)return "E";
        if($val>=-18526 and $val<=-18240)return "F";
        if($val>=-18239 and $val<=-17923)return "G";
        if($val>=-17922 and $val<=-17418)return "H";
        if($val>=-17417 and $val<=-16475)return "J";
        if($val>=-16474 and $val<=-16213)return "K";
        if($val>=-16212 and $val<=-15641)return "L";
        if($val>=-15640 and $val<=-15166)return "M";
        if($val>=-15165 and $val<=-14923)return "N";
        if($val>=-14922 and $val<=-14915)return "O";
        if($val>=-14914 and $val<=-14631)return "P";
        if($val>=-14630 and $val<=-14150)return "Q";
        if($val>=-14149 and $val<=-14091)return "R";
        if($val>=-14090 and $val<=-13319)return "S";
        if($val>=-13318 and $val<=-12839)return "T";
        if($val>=-12838 and $val<=-12557)return "W";
        if($val>=-12556 and $val<=-11848)return "X";
        if($val>=-11847 and $val<=-11056)return "Y";
        if($val>=-11055 and $val<=-10247)return "Z";
    }
    else
    {
        return false;
    }
}

//生成分类归类信息redis
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);
        }
    }
}


//调用接口获取spu
function curl_spu($map){
    $url=env('ES_SKU_URL', '');
    $return=curl($url,$map,1);
    $data=json_decode($return,true);
}

//实时推送接口
function Push_update($sku_id,$type='sku'){
    $url=env('ES_COUNT_URL').$type;
    $data['ids']=$sku_id;
    $result=curl($url,$data,1);
    $result = json_decode($result,true);
    if($result['error_code']==0){
        return true;
    }else{
        return false;
    }
}

//处理mouser数据
function Handle_Mouser($info){

    if(!is_array($info)){
        exit();
    }
    foreach ($info as $k=>$v){
        if(empty($v['goods_name']) || empty($v['brand_name']) || empty($v['moq']) || (empty($v['stock']) && $v['stock']!=0)){
            unset($info[$k]);
            $data['must'][]=$v['goods_sn'];
        }else{
            $brand=DB::connection('spu')
                ->table('lie_brand_mapping')
                ->where('supplier_brand_name',trim($v['brand_name']))
                ->where('supplier_name','mouser')
                ->select('id','liexin_brand_id')
                ->first();
            $brand=(array)$brand;
            if(!$brand){
                $save['supplier_name']='mouser';
                $save['supplier_brand_name']=$v['brand_name'];
                $save['status']=1;
                $save['create_time']=time();
                $result=DB::connection('spu')->table('lie_classify_mapping')->insert($save);
                if(!$result){
                    $data['brand_fail'][]=$v['brand_name'];
                }else{
                    $data['brand_success']=$v['brand_name'];
                }
            }else{
                if(!empty($brand['liexin_brand_id']) && $brand['liexin_brand_id']>0){
                    $exist=only_judge_spu($brand['liexin_brand_id'],$v['goods_name']);
                    $sku_exist='';
                    if(!$exist){
                        $save_spu=[];
                        $save_spu['spu_id']     = (int)structure('spu');//构造SPUID
                        $save_spu['brand_id']   = $brand['liexin_brand_id'];
                        $save_spu['spu_name']   = $v['goods_name'];
                        $save_spu['status']     = 1;
                        $save_spu['encap']      = isset($v['encap'])?$v['encap']:'';
                        $save_spu['images_l']   = isset($v['images'])?$v['images']:'';
                        $save_spu['images_s']   = isset($v['images'])?$v['images']:'';
                        $save_spu['pdf']        = isset($v['pdf'])?$v['pdf']:'';
                        $save_spu['spu_brief']  = isset($v['spu_brief'])?$v['spu_brief']:'';
                        $save_spu['has_rohs']   = isset($v['has_rohs'])?$v['has_rohs']:'';
                        $save_spu['attrs']      = isset($v['attrs'])?$v['attrs']:'';
                        $save_spu['spu_detail'] = isset($v['detail'])?$v['detail']:'';
                        $save_spu['create_time']= time();
                        $save_spu['sale_time']= time();
                        $save_spu['update_time']= time();
                        $save_spu['bussiness_area'] = isset($v['bussiness_area'])?$v['bussiness_area']:'';
                        $dbinfo=resolve($save_spu['spu_id']);
                        $set=DB::connection($dbinfo['db'])->table($dbinfo['table'])->insert($save_spu);
                        if(!$set){
                            $data['spu_fail'][]=$v['goods_sn'];
                        }else{
                            Handle_spu($save_spu);
                            save_mongo_spu($brand['liexin_brand_id'],$v['goods_name'], $save_spu['spu_id'],1);
                            $exist=$save_spu['spu_id'];
                        }
                    }else{
                        $sku_exist=only_judge_sku($exist,14,$v['moq']);
                    }
                    if(!$sku_exist){
                        $save_sku=[];
                        $save_sku['goods_id']=(int)structure('sku');
                        $save_sku['spu_id']=$exist;
                        $save_sku['goods_name']=$v['goods_name'];
                        $save_sku['goods_type']=1;
                        $save_sku['supplier_id']=14;
                        $save_sku['goods_status']=1;
                        $save_sku['batch_sn']=isset($v['batch_sn'])?$v['batch_sn']:'';
                        $save_sku['moq']=isset($v['moq'])?$v['moq']:0;
                        $save_sku['mpq']=isset($v['mpq'])?$v['mpq']:0;
                        $save_sku['stock']=isset($v['stock'])?$v['stock']:0;
                        $save_sku['hk_delivery_time']=isset($v['hk_delivery_time'])?$v['hk_delivery_time']:'';
                        $save_sku['cn_delivery_time']=isset($v['cn_delivery_time'])?$v['cn_delivery_time']:'';
                        $save_sku['goods_details']=isset($v['goods_details'])?$v['goods_details']:'';
                        $save_sku['ladder_price']=isset($v['ladder_price'])?$v['ladder_price']:'';
                        $save_sku['goods_images']=isset($v['images'])?$v['images']:'';
                        $save_sku['goods_sn']=isset($v['goods_sn'])?$v['goods_sn']:'';
                        $save_sku['create_time']= time();
                        $save_sku['sale_time']= time();
                        $save_sku['update_time']= time();
                        $save_sku['goods_sn']= $v['goods_sn'];
                        $dbinfo=resolve($save_sku['goods_id']);
                        $set_sku=DB::connection($dbinfo['db'])->table($dbinfo['table'])->insert($save_sku);
                        if(!$set_sku){
                            $data['sku_fail'][]=$v['goods_sn'];
                        }else{
                            Handle_sku($save_sku);
                            save_mongo_sku($exist,$save_sku['supplier_id'], $save_sku['moq'],$save_sku['goods_id'],1);
                            $return[$save_sku['goods_id']]=$v['goods_sn'];
                        }
                    }else{
                        unset($info[$k]);
                    }

                }
            }
        }
    }
    if(is_array($info) && !empty($info)){
        $data['mouser']=insertMonAndMysql($info);
    }

    $data['sku']= isset($return)?$return:'';
    $data['errinfo']=isset($data)?$data:'';
    return $data;
}

function insertMonAndMysql($data){
    if(empty($data)){
        return false;
    }
    $res_data = array();
    $model = DB::connection('mouser');
    foreach ($data as $k=>$v){
        $find=$model->table('lie_goods')->where('goods_sn',$v['goods_sn'])->select('goods_sn')->first();
        if($find){
            $data['existence'][]=$v['goods_sn'];
            break;
        }
        $arr = array();
        $cat_res = $model->table('lie_category')->select('cat_id')->where('cat_name',$v['cat'])->first();
        $b_id = 0;
        $brand_res = $model->table('lie_brand')->select('brand_id')->where('brand_name',$v['brand_name'])->first();
        if(empty($brand_res)){
            $letter = substr($v['brand_name'], 0,1);
            $b_arr = array();
            $b_arr['brand_name'] = $v['brand_name'];
            $b_arr['first_letter'] = $letter;
            $b_res = $model->table('lie_brand')->insert($b_arr);
            $b_id = $b_res;
        }else{
            $brand_res=(array)$brand_res;
            $b_id = $brand_res['brand_id'];
        }
        if($cat_res){
            $cat_res=(array)$cat_res;
            $arr['cat_id'] = $cat_res['cat_id'];
            $arr['goods_sn'] = $v['goods_sn'];
            $arr['goods_name'] = $v['goods_name'];
            $arr['brand_id'] = $b_id;
            $arr['provider_name'] = $v['brand_name'];
            $arr['pdf_url'] = $v['pdf'];
            $arr['goods_number'] = $v['stock'];
            $arr['min_buynum'] = $v['moq'];
            $arr['goods_brief'] = $v['spu_brief'];
            $arr['goods_thumb'] = $v['images'];
            $arr['goods_img'] = $v['images'];
            $arr['add_time'] = time();
            $arr['last_update'] = time();
            $arr['site_url'] = $v['url'];
            $arr['increment'] = $v['mpq'];
            $arr['MOQ'] = $v['moq'];
        }else{
            $arr['goods_name_style'] = $v['cat'];
            $arr['goods_sn'] = $v['goods_sn'];
            $arr['goods_name'] = $v['goods_name'];
            $arr['brand_id'] = $b_id;
            $arr['provider_name'] = $v['brand_name'];
            $arr['pdf_url'] = $v['pdf'];
            $arr['goods_number'] = $v['stock'];
            $arr['min_buynum'] = $v['moq'];
            $arr['goods_brief'] = $v['spu_brief'];
            $arr['goods_thumb'] = $v['images'];
            $arr['goods_img'] = $v['images'];
            $arr['add_time'] = time();
            $arr['last_update'] = time();
            $arr['site_url'] = $v['url'];
            $arr['increment'] = $v['mpq'];
            $arr['MOQ'] = $v['moq'];
        }
        $goods_id = $model->table('lie_goods')->insertGetId($arr);
        $goods_id = ceil($goods_id);
        if($goods_id){
            $data[$k]['goods_id'] = $goods_id;
            $arr_price = array();
            //获取价格梯度
            $sqlNum = substr($goods_id,-1);//根据goods_id 最后一位确定价格梯度的数据库编号
            $price_arr = array();
            $price_arr['goods_id'] = $goods_id;
            $sku_price=json_decode($v['ladder_price'],true);
            foreach ($sku_price as $kpr=>$vpr){
                $sku_price[$kpr]['price']=$vpr['price_us'];
                unset($sku_price[$kpr]['price_us']);
                unset($sku_price[$kpr]['price_cn']);
            }
            $price_arr['price'] = json_encode($sku_price);
            $table = 'lie_goods_price_'.$sqlNum;
            //$sql = 'insert into lie_goods_price_'.$sqlNum.' values('.$goods_id.',"'.json_encode($v['hhs_price']).'")';
            $r = $model->table($table)->insert($price_arr);
            $M=new \MongoClient(env('MONGO_HOST', ''));
            $M=$M->ichunt->mouser;
            $arr = array();
            $arr['goods_id'] = $goods_id;
            $arr['goods_sn'] = $v['goods_sn'];
            $arr['goods_name'] = $v['goods_name'];
            $arr['brand_name'] = $v['brand_name'];
            $arr['dt'] = array();
            $arr['desc'] = $v['spu_brief'];
            $arr['docurl'] = $v['pdf'];
            $arr['pn'] = 'mouser';
            $start = $v['moq'] ? intval($v['moq']) : 0;
            $stock = $v['stock'] ? intval($v['stock']) : 0;
            $arr['stock'] = array($start,$stock);
            $price_temp = array();
            if(!empty($v['ladder_price'])){
                $v['ladder_price']=json_decode($v['ladder_price'],true);
                foreach($v['ladder_price'] as $kp=>$vp){
                    $price_temp[$kp][0] = $vp['purchases'];
                    $price_temp[$kp][1] = $vp['price_us'];
                }
            }
            $arr['tiered'] = $price_temp;
            $arr['increment'] = $v['mpq']? intval($v['mpq']) : 0;
            $arr['time'] = time();
            $arr['url'] = $v['url'];
            $arr['isapi'] = 1;
            $res = $M->insert($arr);
            unset($data[$k]['ladder_price']);
            $res_data[] = $data[$k]['goods_sn'];
        }else{
            return false;
        }
    }
    return $res_data;
}