<?php
namespace app\models\store;
use app\models\user\User;
use app\models\user\UserBill;
use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;
use think\Exception;

class StoreGp extends BaseModel
{
    use ModelTrait;

    /**
     * 创建公排
     * @param $order
     */
    public static function createGp($order)
    {
        try {
            $no = intval(self::value('max(no)') ?: 0) + 1;
            list($p_no, $no_path,$gp_id) = self::getpath($no);
            $order_id = $order['id'];
            $add_time = time();
            $price = $order['total_price'];
            $uid = $order['uid'];
            self::create(compact('no', 'uid', 'order_id', 'add_time', 'price', 'p_no', 'no_path'));
            self::where('no', $p_no)->inc('cts', 1)->update();
            self::where('no', $gp_id)->inc('team_cts', 1)->update();
            $user = User::where('uid',$order['uid'])->find();
            User::where('uid',$order['uid'])->inc('cycle_time',strtotime("+31 day "));
            if($user['spread_uid']>0)
            {
                User::where('uid',$user['spread_uid'])->inc('cycle_time',strtotime("+31 day "));
            }
            $p_no_cts =  self::where('no', $gp_id)->value('team_cts');
            if ($p_no_cts % 3 == 0) {
                $info = self::where('no', $gp_id)->find();
                $sp_userInfo = User::find($info['uid']);
                if($sp_userInfo['cycle_time']>time()) {
                    switch ($p_no_cts) {
                        case 3:
                            $sp_brokeragePrice = $order['out_amount'] - 3000;
                            $balance = bcadd($sp_userInfo['brokerage_price'], $sp_brokeragePrice, 2);
                            $mark = "公排号:" . $no . '满3个拥金' . $order['out_amount'] . "元,订单号:" . $order['order_id'];
                            $res = UserBill::income('满3个拥金', $sp_userInfo['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
                            $sp_brokeragePrice1 = 3000;
                            $gp_balance = bcadd($sp_userInfo['gp_now_money'], $sp_brokeragePrice1, 2);
                            $mark = "公排号:" . $no . '满3个加报单余额' . $order['out_amount'] . "元,订单号:" . $order['order_id'];
                            $res = UserBill::income('满3个报单余额', $sp_userInfo['uid'], 'now_money', 'brokerage', $sp_brokeragePrice1, $order['id'], $balance, $mark, 1);
                            User::where('uid', $sp_userInfo['uid'])->inc('brokerage_price', $sp_brokeragePrice)->inc('gp_now_money', $sp_brokeragePrice1)->update();
                            break;
                        case 6:
                        case 9:
                            $sp_brokeragePrice = $order['out_amount'];
                            $balance = bcadd($sp_userInfo['brokerage_price'], $sp_brokeragePrice, 2);
                            $mark = "公排号:" . $no . '满' . $p_no_cts . '个拥金' . $order['out_amount'] . "元,订单号:" . $order['order_id'];
                            $res = UserBill::income('满' . $p_no_cts . '个拥金', $sp_userInfo['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
                            User::where('uid', $sp_userInfo['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
                            break;
                        case 12:
                            $sp_brokeragePrice = $order['out_amount'] - 1000;
                            $balance = bcadd($sp_userInfo['brokerage_price'], $sp_brokeragePrice, 2);
                            $mark = "公排号:" . $no . '满' . $p_no_cts . '个拥金' . $order['out_amount'] . "元,订单号:" . $order['order_id'];
                            $res = UserBill::income('满' . $p_no_cts . '个拥金', $sp_userInfo['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
                            User::where('uid', $sp_userInfo['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
                            self::where('no', $gp_id)->update(['status' => 1]);
                            break;
                    }
                }

            }
            if ($p_no_cts % 12 == 0 && sizeof($no_path) > 1) {
                $path = array_reverse($no_path);
                $n = 0;
                foreach ($path as $v) {
                    $n++;
                    if ($n <= 1) continue;
                    $sp_brokeragePrice = $order['team'];
                    $user = user::find($v);
                    if($user['cycle_time']>time()) {
                        $balance = bcadd($user['brokerage_price'], $sp_brokeragePrice, 2);
                        $mark = "公排号:" . $no . '团队奖' . floatval($sp_brokeragePrice) . "元,订单号:" . $order['order_id'];
                        $res1 = UserBill::income('团队奖', $user['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
                        User::where('uid', $user['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
                    }
                }
            }
            return true;
        }catch (Exception $e)
        {
            return false;
        }

    }

    /**
     * 获取上级及路径
     * @param $no
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private static function getpath($no)
    {
        $no_path = [];$p_no=0;
        if($no==1) {
            $no_path[] =1;
            $gp_id = 1;
        }
        else
        {
            $gp_id = self::where('team_cts','<',12)->value('no');
            $min = self::where('cts','<',12)->order("id asc")->find();
            $p_no = $min['no'];
            $no_path = explode(",",$min['no_path']);
            $no_path[] = $no;
        }
        return compact('p_no','no_path','gp_id');

    }
}