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

namespace app\api\model;

use think\Db;
use think\Model;


class Order extends Model
{
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
        'status_text'
    ];

    public function getCreatetimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public function getStatusList($lang)
    {
        if ($lang == 'ch') {
            return ['-1' => '已取消', '1' => '待支付', '2' => '待发货', '3' => '待收货', '4' => '待评价', '5' => '已完成', '6' => '待成团', '7' => '退款中', '8' => '已退款', '10' => '待审核'];
        } else {
            return ['-1' => 'canceled', '1' => 'unpaid', '2' => 'shipments', '3' => 'receiving', '4' => 'evaluated', '5' => 'done', '6' => 'together', '7' => 'refund', '8' => 'refunded', '10' => 'check'];
        }
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList($data['lang']);
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function selectPageData($where, $page, $limit)
    {
        $total = $this
            ->where($where)
            ->count();

        $orderList = $this
            ->where($where)
            ->field('id order_id,order_num,createtime,pay_total,status,type,team_group_id')
            ->order('createtime desc')
            ->page($page, $limit)
            ->select();

        return ['total' => $total, 'list' => $orderList];
    }

    public function getInfo($where)
    {
        $data = $this->alias('o')
            ->join('fa_order_address oa', 'oa.order_id=o.id')
            ->where($where)
            ->field('o.createtime,o.order_num,o.status,o.postage_date,o.postage_time,o.pay_type,o.pay_total,o.goods_total,o.present_id,o.postage_total,o.discount_price,o.score,o.team_group_status,o.ticket_price,o.ticketcode_id,o.ticketcode_price,oa.name,oa.mobile,oa.address,oa.ch_province_name province_name,oa.ch_city_name city_name,oa.ch_county_name county_name,o.shipper_code,o.logistic_code,o.team_group_id')
            ->find();
        return $data;
    }

    /**
     * 余额支付
     * @param $order_id 订单id
     * @param $user_id 用户id
     * @param $money 支付金额
     * @return boolean
     */
    public function balancePay($order_id, $user_id, $money, $goodsList)
    {
        $userScoreLogModel = new \app\api\model\UserScoreLog();
        Db::startTrans();
        try {
            $oldmoney = Db::name('user')->where('id', $user_id)->value('money');
            $integral_total = 0; //购买商品所需积分
            //获取订单
            $order = $this->where('id', $order_id)->field('id,pay_total,postage_total,ticket_price,ticketcode_price,type,team_group_id,team_group_status')->find();
            if ($order['type'] == 3) {
                //团购商品状态,成团后改为待审核
                $status = 6; //待成团
            } else {
                //其他商品 为待审核
                $status = 10; //待审核
            }
            //减去用户余额
            Db::name('user')->where('id', $user_id)->setDec('money', $money);
            foreach ($goodsList as $key => $goods) {
                $integral_total += $goods['score']; //累计积分总数
                if ($goods['type'] == 3) $goods['type'] = '1';
                Db::name('depot_goods')->where(['type' => $goods['type'], 'goods_id' => $goods['goods_id'], 'depot_id' => $goods['depot_id']])->setDec('stock_num', $goods['number']);
                //判断商品类型减去库存 加销量
                if ($goods['type'] == 2) {
                    Db::name('integral_goods')->where('id', $goods['goods_id'])->setInc('sales_actual', $goods['number']);
                } else {
                    Db::name('goods')->where('id', $goods['goods_id'])->setInc('sales_actual', $goods['number']);
                }
            }

            //有积分商品减积分
            if ($integral_total != 0) {
                $userScoreLogModel->setDecScore($user_id, $integral_total);
            }
            //修改订单状态
            $this->where('id', $order_id)->update(['status' => $status, 'paytime' => time()]);

            //余额支付变动记录
            $data = [
                'user_id' => $user_id,
                'type' => 3, //购物消费
                'money' => $money,
                'before' => $oldmoney,
                'after' => $oldmoney - $money,
                'createtime' => time(),
            ];
            Db::name('user_money_log')->insert($data);

            if ($order['type'] == 3 && !empty($order['team_group_id'])) {
                //参团
                $this->joinTeam($order, $user_id);
            } elseif ($order['type'] == 3) {
                //开团
                $this->createTeam($order, $user_id);
            }
            Db::commit();
            return true;

        } catch (Exception $e) {
            Db::rollback();
            return false;
        }
    }

    /**
     * 微信支付回调
     * @param $order_id 订单id
     * @param $user_id 用户id
     * @param $money 支付金额
     * @return boolean
     */
    public function notify($order_id, $user_id, $money, $goodsList)
    {
        $userScoreLogModel = new \app\api\model\UserScoreLog();
        Db::startTrans();
        try {
            $integral_total = 0; //购买商品所需积分
            //获取订单
            $order = $this->where('id', $order_id)->field('id,pay_total,postage_total,ticket_price,ticketcode_price,type,team_group_id,team_group_status')->find();
            if ($order['type'] == 3) {
                //团购商品状态,成团后改为待审核
                $status = 6; //待成团
            } else {
                //其他商品 为待审核
                $status = 10; //待审核
            }
            foreach ($goodsList as $key => $goods) {
                $integral_total += $goods['score']; //累计积分总数
                if ($goods['type'] == 3) $goods['type'] = '1';
                //减库存 改状态
                Db::name('depot_goods')->where(['type' => $goods['type'], 'goods_id' => $goods['goods_id'], 'depot_id' => $goods['depot_id']])->setDec('stock_num', $goods['number']);
                //判断商品类型减去库存 加销量
                if ($goods['type'] == 2) {
                    Db::name('integral_goods')->where('id', $goods['goods_id'])->setInc('sales_actual', $goods['number']);
                } else {
                    Db::name('goods')->where('id', $goods['goods_id'])->setInc('sales_actual', $goods['number']);
                }

            }

            //有积分商品减积分
            if ($integral_total != 0) {
                $userScoreLogModel->setDecScore($user_id, $integral_total);
            }
            //修改订单状态
            $this->where('id', $order_id)->update(['status' => $status, 'paytime' => time()]);
            //积分变动表记录
            $oldscore = Db::name('user')->where('id', $user_id)->value('score');
            $scoredata = [
                'user_id' => $user_id,
                'type' => 1,  //购物返积分
                'score' => intval($money),
                'before' => $oldscore,
                'after' => $oldscore + intval($money),
                'createtime' => time(),
            ];
            Db::name('user_score_log')->insert($scoredata);
            Db::name('user')->where('id', $user_id)->setInc('score', intval($money));

            if ($order['type'] == 3 && !empty($order['team_group_id'])) {
                //参团
                $this->joinTeam($order, $user_id);
            } elseif ($order['type'] == 3) {
                //开团
                $this->createTeam($order, $user_id);
            }
            Db::commit();
            return true;

        } catch (Exception $e) {
            Db::rollback();
            return false;
        }
    }

    /**
     * 开团
     * @param $order
     * @param $user_id
     * @return bool
     */
    public function createTeam($order, $user_id)
    {
        $goods_id = Db::name('order_info')->where(['order_id' => $order['id']])->value('goods_id');
        $goods_info = Db::name('goods')->where(['id' => $goods_id])->field('group_vip_rebate,group_staff_rebate,group_one_agency_rebate,group_two_agency_rebate,group_three_agency_rebate,group_num,grouptime')->find();
        $user_info = Db::name('user')->where(['id' => $user_id])->field('id,avatar,nickname,pid')->find();
        //拼团表
        $teamData = [
            'goods_id' => $goods_id,
            'user_id' => $user_id,
            'user_avatar' => $user_info['avatar'],
            'user_nickname' => $user_info['nickname'],
            'grouptime' => strtotime('+' . $goods_info['grouptime'] . 'hour'),
            'group_num' => $goods_info['group_num'],
            'status' => 1,
            'createtime' => time(),
            'updatetime' => time(),
        ];
        $teamId = Db::name('team_group')->insertGetId($teamData);
        //给订单添加拼团id
        $this->where('id', $order['id'])->update(['team_group_id' => $teamId]);
        //拼团详情表
        $teamInfoData = [
            'team_group_id' => $teamId,
            'order_id' => $order['id'],
            'user_id' => $user_id,
            'user_avatar' => $user_info['avatar'],
            'user_nickname' => $user_info['nickname'],
            'is_head' => 1,
            'createtime' => time(),
            'updatetime' => time(),
        ];

        Db::name('team_group_info')->insertGetId($teamInfoData);
    }

    /**
     * 参团
     * @param $order
     * @param $user_id
     * @return bool
     */
    public function joinTeam($order, $user_id)
    {
        $user_info = Db::name('user')->where(['id' => $user_id])->field('id,avatar,nickname,pid')->find();

        //添加拼团详情
        $teamInfoData = [
            'team_group_id' => $order['team_group_id'],
            'order_id' => $order['id'],
            'user_id' => $user_id,
            'user_avatar' => $user_info['avatar'],
            'user_nickname' => $user_info['nickname'],
            'is_head' => 2,
            'createtime' => time(),
            'updatetime' => time(),
        ];
        Db::name('team_group_info')->insertGetId($teamInfoData);

        $teamInfo = Db::name('team_group')->where('id', $order['team_group_id'])->field('group_num,grouptime')->find();
        $count = Db::name('team_group_info')->where('team_group_id', $order['team_group_id'])->count();
        //判断是否拼团成功
        if ($teamInfo['group_num'] == $count) {
            //拼团成功
            $ids = Db::name('team_group_info')->where(['team_group_id' => $order['team_group_id']])->column('user_id');
            Db::name('team_group')->where('id', $order['team_group_id'])->update(['status' => 2]);
            //改为待审核
            $this->where(['team_group_id' => $order['team_group_id'], 'user_id' => ['in', $ids]])->update(['status' => 10]);
        }
        return true;
    }

    /**
     * 取消订单
     * @param $order
     * @return bool
     */
    public function orderCancel($order)
    {
        $userTicketModel = new UserTicket();
        $ticketcodeModel = new Ticketcode();

        Db::startTrans();
        try {
            $this->where('id', $order['id'])->update(['status' => -1]);
            if (!empty($order['user_ticket_id'])) {
                $userTicketModel->where('id', $order['user_ticket_id'])->update(['status' => '1', 'updatetime' => time()]);
            }
            if (!empty($order['ticketcode_id'])) {
                $ticketcodeModel->where('id', $order['ticketcode_id'])->update(['user_id' => '', 'updatetime' => time()]);
            }
            Db::commit();
            return true;

        } catch (Exception $e) {
            Db::rollback();
            return false;
        }
    }

    /**
     * 订单退款
     * @param $orderNo
     * @param $refundNo
     * @return boolean
     */
    public function orderRefund($orderNo, $refundNo, $refund_note = null ,$refundFee = null)
    {
        $orderRefundModel = new OrderRefund();
        $orderInfo = $this->where('order_num', $orderNo)->find();
        Db::startTrans();
        try {
            $total = $refundFee?$refundFee:$orderInfo['pay_total'];
            $refundData = [
                'user_id' => $orderInfo['user_id'],
                'total' => $total,
                'order_num' => $orderNo,
                'refund_num' => $refundNo,
                'type' => '2', //微信退款
            ];
            $data = ['status' => 8];
            if ($refund_note) $data['refund_note'] = $refund_note;
            $orderId = $this->where('order_num', $orderNo)->value('id');
            $res1 = $this->where('order_num', $orderNo)->update($data);
            $res2 = $orderRefundModel->save($refundData);

            $orderInfoModel = new \app\admin\model\OrderInfo();
            $goodsList = $orderInfoModel->where('order_id', $orderId)->field('goods_id,depot_id,number,ch_goods_name,type,score')->select();
            $orderInfoModel->where('order_id', $orderId)->update(['status' => '3']);

            foreach ($goodsList as $key => $goods) {
                Db::name('depot_goods')->where(['type' => $goods['type'], 'goods_id' => $goods['goods_id'], 'depot_id' => $goods['depot_id']])->setInc('stock_num', $goods['number']);
                //判断商品类型减销量 加库存
                if ($goods['type'] == 2) {
                    Db::name('integral_goods')->where('id', $goods['goods_id'])->setDec('sales_actual', $goods['number']);
                } else {
                    Db::name('goods')->where('id', $goods['goods_id'])->setDec('sales_actual', $goods['number']);
                }
            }
            //待评价或已完成,退款扣积分,有返利扣返利
            if ($orderInfo['tmp_status'] == 4 || $orderInfo['tmp_status'] == 5){
                //扣积分
                $oldscore = Db::name('user')->where('id', $orderInfo['user_id'])->value('score');
                $scoredata = [
                    'user_id' => $orderInfo['user_id'],
                    'type' => 3,  //积分扣除
                    'score' => intval($total),
                    'before' => $oldscore,
                    'after' => $oldscore - intval($total),
                    'createtime' => time(),
                ];
                Db::name('user_score_log')->insert($scoredata);
                Db::name('user')->where('id', $orderInfo['user_id'])->setDec('score', intval($total));
                //扣返利
                $user_pid = Db::name('user')->where('id', $orderInfo['user_id'])->value('pid');
                //判断是否有上级进行返利
                if ($user_pid && $orderInfo['type'] != 3) {

                    $parentInfo = Db::name('user')->where('id', $user_pid)->find();
                    //普通用户无返利
                    if ($parentInfo['type'] != 1) {
                        $rebateMoney = get_rebate($total, $parentInfo['type']);
                        $rebatedata = [
                            'user_id' => $user_pid,
                            'type' => 7, //扣除返利
                            'money' => $rebateMoney,
                            'before' => $parentInfo['money'],
                            'after' => $parentInfo['money'] - $rebateMoney,
                            'createtime' => time(),
                        ];
                        Db::name('user_money_log')->insert($rebatedata);
                        Db::name('user')->where('id', $user_pid)->setDec('money', $rebateMoney);
                    }
                } elseif ($user_pid && $orderInfo['type'] == 3) {
                    //团购返利
                    $parentInfo = Db::name('user')->where('id', $user_pid)->find();
                    $goods_id = Db::name('team_group')->where(['id' => $orderInfo['team_group_id']])->value('goods_id');
                    $goods_info = Db::name('goods')->where(['id' => $goods_id])->field('group_vip_rebate,group_staff_rebate,group_one_agency_rebate,group_two_agency_rebate,group_three_agency_rebate,group_price')->find();
                    if (!empty($parentInfo) && $parentInfo['type'] != 1) {
                        //普通用户无返利
                        if ($parentInfo['type'] == 2 && $goods_info['group_vip_rebate']) {
                            //会员返利
                            $rebateMoney = $total * $goods_info['group_vip_rebate'];
                        } elseif ($parentInfo['type'] == 3 && $goods_info['group_staff_rebate']) {
                            //员工返利
                            $rebateMoney = $total * $goods_info['group_staff_rebate'];
                        } elseif ($parentInfo['type'] == 4 && $goods_info['group_one_agency_rebate']) {
                            //一级代理返利
                            $rebateMoney = $total * $goods_info['group_one_agency_rebate'];
                        } elseif ($parentInfo['type'] == 5 && $goods_info['group_two_agency_rebate']) {
                            //二级代理返利
                            $rebateMoney = $total * $goods_info['group_two_agency_rebate'];
                        } elseif ($parentInfo['type'] == 6 && $goods_info['group_three_agency_rebate']) {
                            //三级代理返利
                            $rebateMoney = $total * $goods_info['group_three_agency_rebate'];
                        }
                        if (!empty($rebateMoney)) {
                            $rebatedata = [
                                'user_id' => $user_pid,
                                'type' => 7, //返利扣除
                                'money' => $rebateMoney,
                                'before' => $parentInfo['money'],
                                'after' => $parentInfo['money'] - $rebateMoney,
                                'createtime' => time(),
                            ];
                            Db::name('user_money_log')->insert($rebatedata);
                            Db::name('user')->where('id', $parentInfo['id'])->setDec('money', $rebateMoney);
                        }
                    }
                }
            }

            if ($res1 && $res2) {
                Db::commit();
                return true;
            } else {
                return false;
            }
        } catch (Exception $e) {
            Db::rollback();
            return false;
        }
    }

    /**
     * 订单退款单商品
     * @param $orderNo
     * @param $refundNo
     * @return boolean
     */
    public function orderRefundGoods($orderNo, $refundNo, $refund_note = null, $order_info_id, $refundFee)
    {
        $orderRefundModel = new OrderRefund();
        $orderInfoModel = new OrderInfo();
        $orderInfo = $this->where('order_num', $orderNo)->find();
        Db::startTrans();
        try {
            $refundData = [
                'user_id' => $orderInfo['user_id'],
                'total' => $refundFee,
                'order_num' => $orderNo,
                'refund_num' => $refundNo,
                'type' => '2', //微信退款
            ];
            $data = ['status' => 3];
            if ($refund_note) $data['refund_note'] = $refund_note;
            $res1 = $orderInfoModel->where('id', $order_info_id)->update($data);
            $res2 = $orderRefundModel->save($refundData);

            $goods = $orderInfoModel->where('id', $order_info_id)->field('goods_id,depot_id,number,ch_goods_name,type,score')->find();
            Db::name('depot_goods')->where(['type' => $goods['type'], 'goods_id' => $goods['goods_id'], 'depot_id' => $goods['depot_id']])->setInc('stock_num', $goods['number']);
            //判断商品类型减销量 加库存
            if ($goods['type'] == 2) {
                Db::name('integral_goods')->where('id', $goods['goods_id'])->setDec('sales_actual', $goods['number']);
            } else {
                Db::name('goods')->where('id', $goods['goods_id'])->setDec('sales_actual', $goods['number']);
            }

            //待评价或已完成,退款扣积分,有返利扣返利
            if ($orderInfo['tmp_status'] == 4 || $orderInfo['tmp_status'] == 5){
                //扣积分
                $oldscore = Db::name('user')->where('id', $orderInfo['user_id'])->value('score');
                $scoredata = [
                    'user_id' => $orderInfo['user_id'],
                    'type' => 3,  //积分扣除
                    'score' => intval($refundFee),
                    'before' => $oldscore,
                    'after' => $oldscore - intval($refundFee),
                    'createtime' => time(),
                ];
                Db::name('user_score_log')->insert($scoredata);
                Db::name('user')->where('id', $orderInfo['user_id'])->setDec('score', intval($refundFee));
                //扣返利
                $user_pid = Db::name('user')->where('id', $orderInfo['user_id'])->value('pid');
                //判断是否有上级进行返利
                if ($user_pid && $orderInfo['type'] != 3) {

                    $parentInfo = Db::name('user')->where('id', $user_pid)->find();
                    //普通用户无返利
                    if ($parentInfo['type'] != 1) {
                        $rebateMoney = get_rebate($refundFee, $parentInfo['type']);
                        $rebatedata = [
                            'user_id' => $user_pid,
                            'type' => 7, //扣除返利
                            'money' => $rebateMoney,
                            'before' => $parentInfo['money'],
                            'after' => $parentInfo['money'] - $rebateMoney,
                            'createtime' => time(),
                        ];
                        Db::name('user_money_log')->insert($rebatedata);
                        Db::name('user')->where('id', $user_pid)->setDec('money', $rebateMoney);
                    }
                } elseif ($user_pid && $orderInfo['type'] == 3) {
                    //团购返利
                    $parentInfo = Db::name('user')->where('id', $user_pid)->find();
                    $goods_id = Db::name('team_group')->where(['id' => $orderInfo['team_group_id']])->value('goods_id');
                    $goods_info = Db::name('goods')->where(['id' => $goods_id])->field('group_vip_rebate,group_staff_rebate,group_one_agency_rebate,group_two_agency_rebate,group_three_agency_rebate,group_price')->find();
                    if (!empty($parentInfo) && $parentInfo['type'] != 1) {
                        //普通用户无返利
                        if ($parentInfo['type'] == 2 && $goods_info['group_vip_rebate']) {
                            //会员返利
                            $rebateMoney = $refundFee * $goods_info['group_vip_rebate'];
                        } elseif ($parentInfo['type'] == 3 && $goods_info['group_staff_rebate']) {
                            //员工返利
                            $rebateMoney = $refundFee * $goods_info['group_staff_rebate'];
                        } elseif ($parentInfo['type'] == 4 && $goods_info['group_one_agency_rebate']) {
                            //一级代理返利
                            $rebateMoney = $refundFee * $goods_info['group_one_agency_rebate'];
                        } elseif ($parentInfo['type'] == 5 && $goods_info['group_two_agency_rebate']) {
                            //二级代理返利
                            $rebateMoney = $refundFee * $goods_info['group_two_agency_rebate'];
                        } elseif ($parentInfo['type'] == 6 && $goods_info['group_three_agency_rebate']) {
                            //三级代理返利
                            $rebateMoney = $refundFee * $goods_info['group_three_agency_rebate'];
                        }
                        if (!empty($rebateMoney)) {
                            $rebatedata = [
                                'user_id' => $user_pid,
                                'type' => 7, //返利扣除
                                'money' => $rebateMoney,
                                'before' => $parentInfo['money'],
                                'after' => $parentInfo['money'] - $rebateMoney,
                                'createtime' => time(),
                            ];
                            Db::name('user_money_log')->insert($rebatedata);
                            Db::name('user')->where('id', $parentInfo['id'])->setDec('money', $rebateMoney);
                        }
                    }
                }
            }

            if ($res1 && $res2) {
                Db::commit();
                return true;
            } else {
                return false;
            }
        } catch (Exception $e) {
            Db::rollback();
            return false;
        }
    }

    public function findData($where)
    {
        return $this->where($where)->find();
    }

    public function receiving($order_id)
    {
        $order = $this->where('id', $order_id)->find();
        Db::startTrans();
        try {
            //修改订单状态
            $res1 = $this->where(['id' => $order_id])->update(['status' => 4]);

            //积分变动表记录
            $oldscore = Db::name('user')->where('id', $order['user_id'])->value('score');
            $scoredata = [
                'user_id' => $order['user_id'],
                'type' => 1,  //购物返积分
                'score' => intval($order['pay_total']),
                'before' => $oldscore,
                'after' => $oldscore + intval($order['pay_total']),
                'createtime' => time(),
            ];
            Db::name('user_score_log')->insert($scoredata);
            Db::name('user')->where('id', $order['user_id'])->setInc('score', intval($order['pay_total']));


            $user_pid = Db::name('user')->where('id', $order['user_id'])->value('pid');
            //判断是否有上级进行返利
            if ($user_pid && $order['type'] != 3) {
                if (!empty($order['ticket_price'])) $order['pay_total'] += $order['ticket_price'];
                if (!empty($order['ticketcode_price'])) $order['pay_total'] += $order['ticketcode_price'];
                if (!empty($order['postage_total'])) $order['pay_total'] -= $order['postage_total'];
                $parentInfo = Db::name('user')->where('id', $user_pid)->find();
                //普通用户无返利
                if ($parentInfo['type'] != 1) {
                    $rebateMoney = get_rebate($order['pay_total'], $parentInfo['type']);
                    $rebatedata = [
                        'user_id' => $user_pid,
                        'type' => 4, //佣金收入(返利)
                        'money' => $rebateMoney,
                        'before' => $parentInfo['money'],
                        'after' => $parentInfo['money'] + $rebateMoney,
                        'createtime' => time(),
                    ];
                    Db::name('user_money_log')->insert($rebatedata);
                    Db::name('user')->where('id', $user_pid)->setInc('money', $rebateMoney);
                }
            } elseif ($user_pid && $order['type'] == 3) {
                //团购返利
                $parentInfo = Db::name('user')->where('id', $user_pid)->find();
                $goods_id = Db::name('team_group')->where(['id' => $order['team_group_id']])->value('goods_id');
                $goods_info = Db::name('goods')->where(['id' => $goods_id])->field('group_vip_rebate,group_staff_rebate,group_one_agency_rebate,group_two_agency_rebate,group_three_agency_rebate,group_price')->find();
                if (!empty($parentInfo) && $parentInfo['type'] != 1) {
                    if (!empty($order['postage_total'])) $order['pay_total'] -= $order['postage_total'];
                    //普通用户无返利
                    if ($parentInfo['type'] == 2 && $goods_info['group_vip_rebate']) {
                        //会员返利
                        $rebateMoney = $order['pay_total'] * $goods_info['group_vip_rebate'];
                    } elseif ($parentInfo['type'] == 3 && $goods_info['group_staff_rebate']) {
                        //员工返利
                        $rebateMoney = $order['pay_total'] * $goods_info['group_staff_rebate'];
                    } elseif ($parentInfo['type'] == 4 && $goods_info['group_one_agency_rebate']) {
                        //一级代理返利
                        $rebateMoney = $order['pay_total'] * $goods_info['group_one_agency_rebate'];
                    } elseif ($parentInfo['type'] == 5 && $goods_info['group_two_agency_rebate']) {
                        //二级代理返利
                        $rebateMoney = $order['pay_total'] * $goods_info['group_two_agency_rebate'];
                    } elseif ($parentInfo['type'] == 6 && $goods_info['group_three_agency_rebate']) {
                        //三级代理返利
                        $rebateMoney = $order['pay_total'] * $goods_info['group_three_agency_rebate'];
                    }
                    if (!empty($rebateMoney)) {
                        $rebatedata = [
                            'user_id' => $user_pid,
                            'type' => 4, //佣金收入(返利)
                            'money' => $rebateMoney,
                            'before' => $parentInfo['money'],
                            'after' => $parentInfo['money'] + $rebateMoney,
                            'createtime' => time(),
                        ];
                        Db::name('user_money_log')->insert($rebatedata);
                        Db::name('user')->where('id', $parentInfo['id'])->setInc('money', $rebateMoney);
                    }
                }
            }
            if ($res1) {
                Db::commit();
                return true;
            } else {
                return false;
            }
        } catch (Exception $e) {
            Db::rollback();
            return false;
        }
    }

    /**
     * 余额支付
     * @param $order_id 订单id
     * @param $user_id 用户id
     * @param $money 支付金额
     * @return boolean
     */
//    public function balancePay($order_id, $user_id, $money, $goodsList)
//    {
//        $goodsModel = new \app\api\model\Goods();
//        $userScoreLogModel = new \app\api\model\UserScoreLog();
//        $orderDetailModel = new \app\api\model\OrderDetail();
//        $orderInfoModel = new \app\api\model\OrderInfo();
//        Db::startTrans();
//        try {
//            $areaId = Db::name('order_address')->where('order_id', $order_id)->value('province_id');
//            $oldmoney = Db::name('user')->where('id', $user_id)->value('money');
//            $integral_total = 0; //购买商品所需积分
//            //获取订单
//            $order = $this->where('id', $order_id)->field('id,pay_total,postage_total,ticket_price,ticketcode_price,type,team_group_id,team_group_status')->find();
//
//            //减去用户余额
//            Db::name('user')->where('id', $user_id)->setDec('money', $money);
//            foreach ($goodsList as $key => $goods) {
//                $integral_total += $goods['score']; //累计积分总数
//                if ($goods['type'] == 3) $goods['type'] = '1';
//                //根据仓库进行拆单
//                $data[] = $goodsModel->getDepot($goods['goods_id'], $areaId, $goods['number'], $goods['type']);
//            }
//            //获取以仓库分好的商品
//            $goodsDepotList = setArr($data);
//            $orderInfoData = [];
//            $oldOrderInfoIds = []; //获取创建订单时 order_info_id
//            foreach ($goodsDepotList as $k => $goodsDepot) {
//                $detailArr = reset($goodsDepot);
//                $detailData = [
//                    'order_id' => $order_id,
//                    'user_id' => $user_id,
//                    'pay_type' => 2, //余额支付
//                    'depot_id' => $detailArr['depots_id'],
//                    'status' => 2,  //代发货
//                    'createtime' => time(),
//                    'updatetime' => time(),
//                ];
//                //添加订单子表
//                $detailId = $orderDetailModel->insertGetId($detailData);
//                foreach ($goodsDepot as $key => $goodsDepotInfo) {
//                    $where = [
//                        'order_id' => $order_id,
//                        'user_id' => $user_id,
//                        'goods_id' => $goodsDepotInfo['goods_id'],
//                        'type' => $order['type'] == 3 ? 3 : $goodsDepotInfo['type'],
//                    ];
//                    $orderInfo = $orderInfoModel->where($where)->find();
//                    if (!in_array($orderInfo['id'], $oldOrderInfoIds)) $oldOrderInfoIds[] = $orderInfo['id'];
//                    $orderInfoData[] = [
//                        'pay_type' => $orderInfo['pay_type'], //余额支付
//                        'order_id' => $order_id,
//                        'order_detail_id' => $detailId,
//                        'user_id' => $user_id,
//                        'goods_id' => $goodsDepotInfo['goods_id'],
//                        'type' => $goodsDepotInfo['type'],
//                        'ch_goods_name' => $orderInfo['ch_goods_name'],
//                        'en_goods_name' => $orderInfo['en_goods_name'],
//                        'goods_image' => strstr($orderInfo['goods_image'],'/uploads'),
//                        'number' => $goodsDepotInfo['number'],
//                        'goods_total' => round($orderInfo['goods_total'] / $orderInfo['number'] * $goodsDepotInfo['number'], 2),
//                        'pay_total' => $orderInfo['pay_total'],
//                        'postage_total' => $orderInfo['postage_total'],
//                        'score' => $orderInfo['score'] / $orderInfo['number'] * $goodsDepotInfo['number'],
//                    ];
//
//                    //减仓库库存
//                    Db::name('depot')->where('id', $goodsDepotInfo['depots_id'])->setDec('stock_num', $goodsDepotInfo['number']);
//                    //判断商品类型减去库存 加销量
//                    if ($goodsDepotInfo['type'] == 2) {
//                        Db::name('integral_goods')->where('id', $goodsDepotInfo['goods_id'])->setDec('stock_num', $goodsDepotInfo['number']);
//                        Db::name('integral_goods')->where('id', $goodsDepotInfo['goods_id'])->setInc('sales_actual', $goodsDepotInfo['number']);
//                    } else {
//                        Db::name('goods')->where('id', $goodsDepotInfo['goods_id'])->setDec('stock_num', $goodsDepotInfo['number']);
//                        Db::name('goods')->where('id', $goodsDepotInfo['goods_id'])->setInc('sales_actual', $goodsDepotInfo['number']);
//                    }
//                }
//            }
//            //添加订单详情
//            $orderInfoModel->saveAll($orderInfoData);
//            //删除之前订单详情
//            $orderInfoModel->where(['id' => ['in', $oldOrderInfoIds]])->delete();
//
//            //有积分商品减积分
//            if ($integral_total != 0) {
//                $userScoreLogModel->setDecScore($user_id, $integral_total);
//            }
//            //将订单状态修改为已支付
//            $this->where('id', $order_id)->update(['status' => 2, 'paytime' => time()]);
//            //积分变动表记录
//            $oldscore = Db::name('user')->where('id', $user_id)->value('score');
//            $scoredata = [
//                'user_id' => $user_id,
//                'type' => 1,  //购物返积分
//                'score' => intval($money),
//                'before' => $oldscore,
//                'after' => $oldscore + intval($money),
//                'createtime' => time(),
//            ];
//            Db::name('user_score_log')->insert($scoredata);
//            Db::name('user')->where('id', $user_id)->setInc('score', intval($money));
//
//            //余额支付变动记录
//            $data = [
//                'user_id' => $user_id,
//                'type' => 3, //购物消费
//                'money' => $money,
//                'before' => $oldmoney,
//                'after' => $oldmoney - $money,
//                'createtime' => time(),
//            ];
//            Db::name('user_money_log')->insert($data);
//
//
//            $user_pid = Db::name('user')->where('id', $user_id)->value('pid');
//
//            //判断是否有上级进行返利  团购订单不在这里返利
//            if ($user_pid && $order['type'] != 3) {
//                if (!empty($order['ticket_price'])) $order['pay_total'] += $order['ticket_price'];
//                if (!empty($order['ticketcode_price'])) $order['pay_total'] += $order['ticketcode_price'];
//                if (!empty($order['postage_total'])) $order['pay_total'] -= $order['postage_total'];
//                $parentInfo = Db::name('user')->where('id', $user_pid)->find();
//                //普通用户无返利
//                if ($parentInfo['type'] != 1) {
//                    $rebateMoney = get_rebate($order['pay_total'], $parentInfo['type']);
//                    $rebatedata = [
//                        'user_id' => $user_pid,
//                        'type' => 4, //佣金收入(返利)
//                        'money' => $rebateMoney,
//                        'before' => $parentInfo['money'],
//                        'after' => $parentInfo['money'] + $rebateMoney,
//                        'createtime' => time(),
//                    ];
//                    Db::name('user_money_log')->insert($rebatedata);
//                    Db::name('user')->where('id', $user_pid)->setInc('money', $rebateMoney);
//                }
//            }
//            if ($order['type'] == 3 && !empty($order['team_group_id'])) {
//                //参团
//                $this->joinTeam($order, $user_id);
//            } elseif ($order['type'] == 3) {
//                //开团
//                $this->createTeam($order, $user_id);
//            }
//            Db::commit();
//            return true;
//
//        } catch (Exception $e) {
//            Db::rollback();
//            return false;
//        }
//    }
}