DistributLogic.php 33.9 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
<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: IT宇宙人
 * Date: 2015-09-09
 * 
 * TPshop 公共逻辑类  将放到Application\Common\Logic\   由于很多模块公用 将不在放到某个单独模下面
 */

namespace app\common\logic;

use think\Model;
//use think\Page;

/**
 * 分销逻辑层
 * Class CatsLogic
 * @package Home\Logic
 */
class DistributLogic //extends Model
{
     public function hello(){
        echo 'function hello(){'; 
     }
     
     /**
      * 生成分销记录
      */
     public function rebate_log($order,$stoid,$odr_garr=null)
     {
         $jsondata=[];
         $disgoods=[];
         $user = M('users')->where("user_id", $order['user_id'])->find();
         $pattern = tpCache('distribut.pattern',$stoid);         // 分销模式
         $first_rate = tpCache('distribut.first_rate',$stoid);   // 一级比例
         $second_rate = tpCache('distribut.second_rate',$stoid); // 二级比例
         $third_rate = tpCache('distribut.third_rate',$stoid);   // 三级比例

         $first_money = 0; // 一级赚到的钱
         $second_money = 0; // 二级赚到的钱
         $thirdmoney = 0; // 三级赚到的钱
         $allmoney= 0;
         //获取所有商品
         $order_goods=$odr_garr;
         if(empty($order_goods))
             $order_goods = M('order_goods')->where("order_id", $order['order_id'])->select(); // 订单所有商品
         /*---积累分成---*/
         $commission = 0;
         /*--循环判断商品的条件--*/
         foreach($order_goods as $k => $v)
         {
                $tmp_commission = 0;
                $goods = M('goods')->where("goods_id", $v['goods_id'])->find(); // 单个商品的佣金
                //获取活动的佣金,如果活动未设置佣金,则佣金0
                if($v['prom_type']==1 || $v['prom_type']==2  || $v['prom_type']==4){
                    switch ($v['prom_type']){
                        case 1:
                            $tb='flash_sale';
                            break;
                        case 2:
                            $tb='group_buy';
                            break;
                        case 4:
                            $tb='integral_buy';
                            break;
                    }
                    $rrss=M($tb)->where('id',$v['prom_id'])->find();
                    if($pattern==0) {
                        if (empty($rrss['commission'])) { $tmp_commission = 0;
                        } else {
                            $tmp_commission = $rrss['commission'];
                        }
                    }else if($pattern==1) {
                        $first_money+=$rrss['fir_rate']* $v['goods_num'];;
                        $second_money+=$rrss['sec_rate']* $v['goods_num'];;
                        $thirdmoney+=$rrss['thi_rate']* $v['goods_num'];;
                    }

                    if($tmp_commission!=0) $tmp_commission = $tmp_commission * $v['goods_num'];
                    $jsondata[]=['goods_id'=>$v['goods_id'],
                        'commission'=>$tmp_commission,
                        'fir'=>$rrss['fir_rate'],
                        'sec'=>$rrss['sec_rate'],
                        'thi'=>$rrss['thi_rate'],
                        'fir_r'=>$first_rate,
                        'sec_r'=>$second_rate,
                        'thi_r'=>$third_rate,
                        'g_num'=>$v['goods_num'],
                    ];
                }else{
                    //直接获取商品的佣金
                    if($pattern==0) {
                        $tmp_commission = $goods['commission'];
                    }
                    else if($pattern==1){
                        $first_money+=$goods['fir_rate']* $v['goods_num'];
                        $second_money+=$goods['sec_rate']* $v['goods_num'];
                        $thirdmoney+=$goods['thi_rate']* $v['goods_num'];
                    }
                    if($tmp_commission!=0) $tmp_commission = $tmp_commission * $v['goods_num'];

                    $jsondata[]=['goods_id'=>$v['goods_id'],
                        'commission'=>$tmp_commission,
                        'fir'=>$goods['fir_rate'],
                        'sec'=>$goods['sec_rate'],
                        'thi'=>$goods['thi_rate'],
                        'fir_r'=>$first_rate,
                        'sec_r'=>$second_rate,
                        'thi_r'=>$third_rate,
                        'g_num'=>$v['goods_num'],
                    ];
                }
                $disgoods[]=$v['goods_id'];
                $commission += $tmp_commission; // 所有商品的累积佣金
        }

        mlog("订单:".$order['order_id'].$order['order_sn']
             ."分销金额".$commission.'-'.$first_money.'-'.$second_money.'-'.$thirdmoney,
             "rebate_log/".$stoid);

        if( $pattern==0) {
            // 如果这笔订单没有分销金额
            if($commission == 0) return false;
            $first_money = $commission * ($first_rate / 100); // 一级赚到的钱
            $second_money = $commission * ($second_rate / 100); // 二级赚到的钱
            $thirdmoney = $commission * ($third_rate / 100); // 三级赚到的钱
        }

        $allmoney= $commission;
         mlog("订单:".$order['order_id'].$order['order_sn']."一级赚到的钱".$first_money."二级赚到的钱".$second_money."三级赚到的钱".$thirdmoney,"rebate_log/".$stoid);
         //  微信消息推送
         $erpid=tpCache('shop_info.ERPId',$stoid);
         $reb1=tpCache("distribut.first_rate",$stoid);
         $reb2=tpCache("distribut.second_rate",$stoid);
         $reb3=tpCache("distribut.third_rate",$stoid);
         $remark="交易成功后您将获得奖励";

         //是否是分销商
         if ( $user['is_distribut'] == 1){
             /*--没有一级分享分和二级分享人--*/
             if(empty($user['first_leader']) && empty($user['second_leader'])){
                 if(($first_money + $second_money + $thirdmoney)>0) {
                     $data = array(
                         'user_id' => $user['user_id'],
                         'buy_user_id' => $user['user_id'],
                         'nickname' => $user['nickname'],
                         'order_sn' => $order['order_sn'],
                         'order_id' => $order['order_id'],
                         'goods_price' => $order['goods_price'],
                         'money' => $first_money + $second_money + $thirdmoney,
                         'level' => 1,
                         'create_time' => time(),
                         'store_id' => $stoid,
                         'first_rate' => ($first_rate + $second_rate + $third_rate),
                         'second_rate' => 0,
                         'third_rate' => 0,
                         'commission_json' => json_encode($jsondata),
                         'dis_moshi' => $pattern,
                         'user_type' => 1,
                         'pickup_id'=>$order['pickup_id'],
                     );

                     if ($pattern == 1) {
                         $data['first_rate'] = $first_money + $second_money + $thirdmoney;
                     }

                     M('rebate_log')->add($data);
                     // 微信推送消息
                     if ($user['oauth'] == 'weixin') {
                         $title = "您的一级会员刚下了一笔订单";
                         $this->post_msg($stoid,$erpid,$user['user_id'],$title,$order['order_sn'],$order['total_amount'],($first_money + $second_money + $thirdmoney),date('Y年m月d日 H:i'),$remark);
                     }
                     M('order_goods')->where('order_id', $order['order_id'])
                         ->where('goods_id', 'in', $disgoods)->save(['is_distri' => 1]);
                 }
             }
             /*--有一级分享分人,没有二级分享人--*/
             if(!empty($user['first_leader']) && empty($user['second_leader'])){
                 if($first_money) {
                     $data = array(
                         'user_id' => $user['user_id'],
                         'buy_user_id' => $user['user_id'],
                         'nickname' => $user['nickname'],
                         'order_sn' => $order['order_sn'],
                         'order_id' => $order['order_id'],
                         'goods_price' => $order['goods_price'],
                         'money' => $first_money,
                         'level' => 1,
                         'create_time' => time(),
                         'store_id' => $stoid,
                         'first_rate' => $first_rate,
                         'second_rate' => ($second_rate + $third_rate),
                         'third_rate' => 0,
                         'commission_json' => json_encode($jsondata),
                         'dis_moshi' => $pattern,
                         'user_type' => 1,
                         'pickup_id'=>$order['pickup_id'],
                     );

                     if ($pattern == 1) {
                         $data['first_rate'] = $first_money;
                         $data['second_rate'] = $second_money + $thirdmoney;
                     }

                     M('rebate_log')->add($data);
                     // 微信推送消息
                     if ($user['oauth'] == 'weixin') {
                         $title = "您的一级会员刚下了一笔订单";
                         $this->post_msg($stoid,$erpid,$user['user_id'],$title,$order['order_sn'],$order['total_amount'],$first_money,date('Y年m月d日 H:i'),$remark);
                     }
                 }
                 if(($second_money + $thirdmoney)>0) {
                     $fuid2 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['first_leader'])->find();
                     if($fuid2 && $fuid2['is_distribut']==1) {
                         $data0 = array(
                             'user_id' => $user['first_leader'],
                             'buy_user_id' => $user['user_id'],
                             'nickname' => $user['nickname'],
                             'order_sn' => $order['order_sn'],
                             'order_id' => $order['order_id'],
                             'goods_price' => $order['goods_price'],
                             'money' => ($second_money + $thirdmoney),
                             'level' => 2,
                             'create_time' => time(),
                             'store_id' => $stoid,
                             'first_rate' => $first_rate,
                             'second_rate' => ($second_rate + $third_rate),
                             'third_rate' => 0,
                             'commission_json' => json_encode($jsondata),
                             'dis_moshi' => $pattern,
                             'user_type' => 1,
                             'pickup_id' => $order['pickup_id'],
                         );
                         if ($pattern == 1) {
                             $data0['first_rate'] = $first_money;
                             $data0['second_rate'] = $second_money + $thirdmoney;
                         }
                         M('rebate_log')->add($data0);
                         // 微信推送消息
                         if ($user['oauth'] == 'weixin') {
                             $title = "您的二级会员刚下了一笔订单";
                             $this->post_msg($stoid, $erpid, $fuid2['user_id'], $title, $order['order_sn'], $order['total_amount'], ($second_money + $thirdmoney), date('Y年m月d日 H:i'), $remark);
                         }
                     }
                 }
                 if(($first_money + $second_money + $thirdmoney)>0)
                    M('order_goods')->where('order_id',$order['order_id'])
                       ->where('goods_id','in',$disgoods)->save(['is_distri'=>1]);

             }
             /*--有一级分享分人和二级分享人--*/
             if(!empty($user['first_leader']) && !empty($user['second_leader'])){
                 if($first_money>0) {
                     $data = array(
                         'user_id' => $user['user_id'],
                         'buy_user_id' => $user['user_id'],
                         'nickname' => $user['nickname'],
                         'order_sn' => $order['order_sn'],
                         'order_id' => $order['order_id'],
                         'goods_price' => $order['goods_price'],
                         'money' => $first_money,
                         'level' => 1,
                         'create_time' => time(),
                         'store_id' => $stoid,
                         'first_rate' => $first_rate,
                         'second_rate' => $second_rate,
                         'third_rate' => $third_rate,
                         'commission_json' => json_encode($jsondata),
                         'dis_moshi' => $pattern,
                         'user_type' => 1,
                         'pickup_id'=>$order['pickup_id'],
                     );

                     if ($pattern == 1) {
                         $data['first_rate'] = $first_money;
                         $data['second_rate'] = $second_money;
                         $data['third_rate'] = $thirdmoney;
                     }

                     M('rebate_log')->add($data);
                     // 微信推送消息
                     if ($user['oauth'] == 'weixin') {
                         $title = "您的一级会员刚下了一笔订单";
                         $this->post_msg($stoid,$erpid,$user['user_id'],$title,$order['order_sn'],$order['total_amount'],$first_money,date('Y年m月d日 H:i'),$remark);
                     }
                 }
                 if($second_money>0){
                     $fuid2 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['first_leader'])->find();
                     if($fuid2 && $fuid2['is_distribut']==1) {
                         $data0 = array(
                             'user_id' => $user['first_leader'],
                             'buy_user_id' => $user['user_id'],
                             'nickname' => $user['nickname'],
                             'order_sn' => $order['order_sn'],
                             'order_id' => $order['order_id'],
                             'goods_price' => $order['goods_price'],
                             'money' => $second_money,
                             'level' => 2,
                             'create_time' => time(),
                             'store_id' => $stoid,
                             'first_rate' => $first_rate,
                             'second_rate' => $second_rate,
                             'third_rate' => $third_rate,
                             'commission_json' => json_encode($jsondata),
                             'dis_moshi' => $pattern,
                             'user_type' => 1,
                             'pickup_id' => $order['pickup_id'],
                         );

                         if ($pattern == 1) {
                             $data0['first_rate'] = $first_money;
                             $data0['second_rate'] = $second_money;
                             $data0['third_rate'] = $thirdmoney;
                         }

                         M('rebate_log')->add($data0);
                         // 微信推送消息
                         if ($user['oauth'] == 'weixin') {
                             $title = "您的二级会员刚下了一笔订单";
                             $this->post_msg($stoid, $erpid, $fuid2['user_id'], $title, $order['order_sn'], $order['total_amount'], $second_money, date('Y年m月d日 H:i'), $remark);
                         }
                     }
                 }
                 if($thirdmoney>0) {
                     $fuid3 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['second_leader'])->find();
                     if($fuid3 && $fuid3['is_distribut']==1) {
                         $data1 = array(
                             'user_id' => $user['second_leader'],
                             'buy_user_id' => $user['user_id'],
                             'nickname' => $user['nickname'],
                             'order_sn' => $order['order_sn'],
                             'order_id' => $order['order_id'],
                             'goods_price' => $order['goods_price'],
                             'money' => $thirdmoney,
                             'level' => 3,
                             'create_time' => time(),
                             'store_id' => $stoid,
                             'first_rate' => $first_rate,
                             'second_rate' => $second_rate,
                             'third_rate' => $third_rate,
                             'commission_json' => json_encode($jsondata),
                             'dis_moshi' => $pattern,
                             'user_type' => 1,
                             'pickup_id' => $order['pickup_id'],
                         );
                         if ($pattern == 1) {
                             $data1['first_rate'] = $first_money;
                             $data1['second_rate'] = $second_money;
                             $data1['third_rate'] = $thirdmoney;
                         }

                         M('rebate_log')->add($data1);
                         // 微信推送消息
                         if ($user['oauth'] == 'weixin') {
                             $title = "您的三级会员刚下了一笔订单";
                             $this->post_msg($stoid, $erpid, $fuid3['user_id'], $title, $order['order_sn'], $order['total_amount'], $thirdmoney, date('Y年m月d日 H:i'), $remark);
                         }
                     }
                 }
                 if(($first_money + $second_money + $thirdmoney)>0)
                     M('order_goods')->where('order_id',$order['order_id'])
                         ->where('goods_id','in',$disgoods)->save(['is_distri'=>1]);
             }
         }else{
             /*--有一级分享分人,没有二级分享人,没有三级分享人--*/
             if(!empty($user['first_leader']) && empty($user['second_leader']) && empty($user['third_leader'])){
                 if(($first_money+$second_money+$thirdmoney) <= 0) return false;
                 $fuid0 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['first_leader'])->find();
                 if($fuid0 && $fuid0['is_distribut']==1) {
                     $data = array(
                         'user_id' => $user['first_leader'],
                         'buy_user_id' => $user['user_id'],
                         'nickname' => $user['nickname'],
                         'order_sn' => $order['order_sn'],
                         'order_id' => $order['order_id'],
                         'goods_price' => $order['goods_price'],
                         'money' => ($first_money + $second_money + $thirdmoney),
                         'level' => 1,
                         'create_time' => time(),
                         'store_id' => $stoid,
                         'first_rate' => ($first_rate + $second_rate + $third_rate),
                         'second_rate' => 0,
                         'third_rate' => 0,
                         'commission_json' => json_encode($jsondata),
                         'dis_moshi' => $pattern,
                         'pickup_id' => $order['pickup_id'],
                     );

                     if ($pattern == 1) {
                         $data1['first_rate'] = ($first_money + $second_money + $thirdmoney);
                     }

                     M('rebate_log')->add($data);
                     // 微信推送消息
                     if ($user['oauth'] == 'weixin') {
                         $title = "您推荐的好友刚下了一笔订单";
                         $this->post_msg($stoid, $erpid, $fuid0['user_id'], $title, $order['order_sn'], $order['total_amount'], ($first_money + $second_money + $thirdmoney), date('Y年m月d日 H:i'), $remark);
                     }
                     M('order_goods')->where('order_id', $order['order_id'])
                         ->where('goods_id', 'in', $disgoods)->save(['is_distri' => 1]);
                 }
             }
             /*--有一级分享分人和二级分享人--*/
             if(!empty($user['first_leader']) && !empty($user['second_leader']) && empty($user['third_leader']) ){

                 if($first_money>0) {
                     $fuid0 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['first_leader'])->find();
                     if($fuid0 && $fuid0['is_distribut']==1) {
                         $data = array(
                             'user_id' => $user['first_leader'],
                             'buy_user_id' => $user['user_id'],
                             'nickname' => $user['nickname'],
                             'order_sn' => $order['order_sn'],
                             'order_id' => $order['order_id'],
                             'goods_price' => $order['goods_price'],
                             'money' => $first_money,
                             'level' => 1,
                             'create_time' => time(),
                             'store_id' => $stoid,
                             'first_rate' => $first_rate,
                             'second_rate' => ($second_rate + $third_rate),
                             'third_rate' => 0,
                             'commission_json' => json_encode($jsondata),
                             'dis_moshi' => $pattern,
                             'pickup_id' => $order['pickup_id'],
                         );
                         if ($pattern == 1) {
                             $data['first_rate'] = ($first_money);
                             $data['first_rate'] = ($second_money + $thirdmoney);
                         }
                         M('rebate_log')->add($data);
                         // 微信推送消息
                         if ($user['oauth'] == 'weixin') {
                             $title = "您推荐的好友刚下了一笔订单";
                             $this->post_msg($stoid, $erpid, $fuid0['user_id'], $title, $order['order_sn'], $order['total_amount'], $first_money, date('Y年m月d日 H:i'), $remark);
                         }
                     }
                 }

                 mlog("第二级金额".($second_money+$thirdmoney),"rebate_log/".$stoid);
                 if(($second_money+$thirdmoney)>0) {
                     $fuid1 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['second_leader'])->find();

                     mlog("第二级会员".(json_encode($fuid1)),"rebate_log/".$stoid);
                     if($fuid1 && $fuid1['is_distribut']==1) {
                         $data0 = array(
                             'user_id' => $user['second_leader'],
                             'buy_user_id' => $user['user_id'],
                             'nickname' => $user['nickname'],
                             'order_sn' => $order['order_sn'],
                             'order_id' => $order['order_id'],
                             'goods_price' => $order['goods_price'],
                             'money' => ($second_money + $thirdmoney),
                             'level' => 2,
                             'create_time' => time(),
                             'store_id' => $stoid,
                             'first_rate' => $first_rate,
                             'second_rate' => ($second_rate + $third_rate),
                             'third_rate' => 0,
                             'commission_json' => json_encode($jsondata),
                             'dis_moshi' => $pattern,
                             'pickup_id' => $order['pickup_id'],
                         );
                         if ($pattern == 1) {
                             $data0['first_rate'] = ($first_money);
                             $data0['first_rate'] = ($second_money + $thirdmoney);
                         }
                         M('rebate_log')->add($data0);
                         // 微信推送消息
                         if ($user['oauth'] == 'weixin') {
                             $title = "您推荐的二级好友刚下了一笔订单";
                             $this->post_msg($stoid, $erpid, $fuid1['user_id'], $title, $order['order_sn'], $order['total_amount'], ($second_money + $thirdmoney), date('Y年m月d日 H:i'), $remark);
                         }
                     }
                 }
                 if(($first_money + $second_money + $thirdmoney)>0)
                     M('order_goods')->where('order_id',$order['order_id'])
                         ->where('goods_id','in',$disgoods)->save(['is_distri'=>1]);

             }

             /*--有一级分享分人和二级分享人,三级分享人--*/
             if(!empty($user['first_leader']) && !empty($user['second_leader']) && !empty($user['third_leader']) ){
                 if($first_money>0) {
                     $fuid0 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['first_leader'])->find();
                     if($fuid0 && $fuid0['is_distribut']==1) {
                         $data = array(
                             'user_id' => $user['first_leader'],
                             'buy_user_id' => $user['user_id'],
                             'nickname' => $user['nickname'],
                             'order_sn' => $order['order_sn'],
                             'order_id' => $order['order_id'],
                             'goods_price' => $order['goods_price'],
                             'money' => $first_money,
                             'level' => 1,
                             'create_time' => time(),
                             'store_id' => $stoid,
                             'first_rate' => $first_rate,
                             'second_rate' => $second_rate,
                             'third_rate' => $third_rate,
                             'commission_json' => json_encode($jsondata),
                             'dis_moshi' => $pattern,
                             'pickup_id' => $order['pickup_id'],
                         );
                         if ($pattern == 1) {
                             $data['first_rate'] = $first_money;
                             $data['second_rate'] = $second_money;
                             $data['third_rate'] = $thirdmoney;
                         }
                         M('rebate_log')->add($data);
                         // 微信推送消息
                         if ($user['oauth'] == 'weixin') {
                             $title = "您推荐的好友刚下了一笔订单";
                             $this->post_msg($stoid, $erpid, $fuid0['user_id'], $title, $order['order_sn'], $order['total_amount'], $first_money, date('Y年m月d日 H:i'), $remark);
                         }
                     }
                 }
                 if($second_money>0) {
                     $fuid1 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['second_leader'])->find();
                     if($fuid1 && $fuid1['is_distribut']==1) {
                         $data0 = array(
                             'user_id' => $user['second_leader'],
                             'buy_user_id' => $user['user_id'],
                             'nickname' => $user['nickname'],
                             'order_sn' => $order['order_sn'],
                             'order_id' => $order['order_id'],
                             'goods_price' => $order['goods_price'],
                             'money' => $second_money,
                             'level' => 2,
                             'create_time' => time(),
                             'store_id' => $stoid,
                             'first_rate' => $first_rate,
                             'second_rate' => $second_rate,
                             'third_rate' => $third_rate,
                             'commission_json' => json_encode($jsondata),
                             'dis_moshi' => $pattern,
                             'pickup_id' => $order['pickup_id'],
                         );
                         if ($pattern == 1) {
                             $data0['first_rate'] = $first_money;
                             $data0['second_rate'] = $second_money;
                             $data0['third_rate'] = $thirdmoney;
                         }
                         M('rebate_log')->add($data0);
                         // 微信推送消息
                         if ($user['oauth'] == 'weixin') {
                             $title = "您推荐的二级好友刚下了一笔订单";
                             $this->post_msg($stoid, $erpid, $fuid1['user_id'], $title, $order['order_sn'], $order['total_amount'], $second_money, date('Y年m月d日 H:i'), $remark);
                         }
                     }
                 }
                 if($thirdmoney>0) {
                     $fuid2 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['third_leader'])->find();
                     if($fuid2 && $fuid2['is_distribut']==1) {
                         $data1 = array(
                             'user_id' => $user['third_leader'],
                             'buy_user_id' => $user['user_id'],
                             'nickname' => $user['nickname'],
                             'order_sn' => $order['order_sn'],
                             'order_id' => $order['order_id'],
                             'goods_price' => $order['goods_price'],
                             'money' => $thirdmoney,
                             'level' => 3,
                             'create_time' => time(),
                             'store_id' => $stoid,
                             'first_rate' => $first_rate,
                             'second_rate' => $second_rate,
                             'third_rate' => $third_rate,
                             'commission_json' => json_encode($jsondata),
                             'dis_moshi' => $pattern,
                             'pickup_id' => $order['pickup_id'],
                         );
                         if ($pattern == 1) {
                             $data1['first_rate'] = $first_money;
                             $data1['second_rate'] = $second_money;
                             $data1['third_rate'] = $thirdmoney;
                         }
                         M('rebate_log')->add($data1);
                         // 微信推送消息
                         if ($user['oauth'] == 'weixin') {
                             $title = "您推荐的三级好友刚下了一笔订单";
                             $this->post_msg($stoid, $erpid, $fuid2['user_id'], $title, $order['order_sn'], $order['total_amount'], $thirdmoney, date('Y年m月d日 H:i'), $remark);
                         }
                     }
                 }
                 if(($first_money + $second_money + $thirdmoney)>0)
                     M('order_goods')->where('order_id',$order['order_id'])
                         ->where('goods_id','in',$disgoods)->save(['is_distri'=>1]);
             }
         }
         M('order')->where("order_id", $order['order_id'])->save(array("is_distribut"=>1));  //修改订单为已经分成
     }
     

     /**
      * 自动分成 符合条件的 分成记录
      */
     function auto_confirm($stoid){
        // mlog("执行分成","autoconfirm");
         $switch = tpCache('distribut.switch',$stoid);
         if($switch == 0)
             return false;
         $today_time = time();
         $distribut_date = tpCache('distribut.date',$stoid);
         $distribut_time = $distribut_date * (60 * 60 * 24); // 计算天数 时间戳
         $rebate_log_arr = M('rebate_log')->where(" store_id=".$stoid." and status = 2 and ($today_time - confirm) >  $distribut_time")->select();
         foreach ($rebate_log_arr as $key => $val)
         {
             accountLog($val['user_id'], $val['money'], 0,"订单:{$val['order_sn']}分佣",
                 $val['money'],$stoid,null,$val['order_sn'],1);

             $val['status'] = 3;
             $val['store_id']=$stoid;
             $val['confirm_time'] = $today_time;
             $val['remark'] = $val['remark']."{$distribut_date}天,程序自动分成.";
             M("rebate_log")->where("id", $val['id'])->save($val);
         }
     }

    //推送消息
    public function post_msg($stoid,$erpid,$user_id,$title,$order_sn,$order_money,$fc_num,$time,$remark){
        $colorlist = urlencode("#FF0000|#173177|#173177|#173177|#173177|#FF0000|#FF0000|#ca003a");
        //$url = curHostURL() . "/home/api/poswxcode?typeid=1021&Acc=" . $erpid . "&ErpVipid=" . $user_id . "&title=" . $title . "&key1=" . $order_sn . "&key2=" . number_format($order_money,2) . "元&key3=" . number_format($fc_num,2) . "元&key4=" . $time . "&remark=" . $remark.'&colorlist='. $colorlist;//. "&backurl=" . curHostURL() . "/Mobile/User/order_list/stoid/" . $stoid;
        //$resp=httpRequest($url, "GET");

        $data['typeid']='1021';//微信消息类型 1001至1010
        $data['title']=$title;
        $data['remark']=$remark;
        $data['key1']=$order_sn;
        $data['key2']=number_format($order_money,2) . "元";
        $data['key3']=number_format($fc_num,2) . "元";
        $data['key4']=$time;
        $data['colorlist']=$colorlist;
        $data['backurl']=curHostURL() . "/Mobile/User/order_list/stoid/" . $stoid;

        $data['Acc']=$erpid;
        $data['user_id']=$user_id;
        $api  = new \app\home\controller\Api();
        //$resp=httpRequest($url, "GET");
        $resp=$api->poswxcode2($data);
        return $resp;

    }

}