<?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($data)
    {
        $order = $data['data'];
        @file_put_contents("job.txt",json_encode($order));
        try {
            $no = intval(self::value('max(no)') ?: 0) + 1;
            $rs = self::getpath($no);
            list($p_no, $no_path,$gp_id,$send,$gpinfo) = [$rs['p_no'],$rs['no_path'],$rs['gp_id'],$rs['send'],$rs['info']];
            @file_put_contents("job.txt","\r\n".json_encode($rs),8);
            $order_id = $order['id'];
            $add_time = time();
            $price = $order['total_price'];
            $uid = $order['uid'];
            $status = 0;
            self::create(compact('no', 'uid', 'order_id', 'add_time', 'price', 'p_no', 'no_path','status'));
            if($no>1) {
                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();
            $spuser = User::where('uid',$user['spread_uid'])->find();
            User::where('uid',$order['uid'])->update(['cycle_time'=>strtotime("+31 day")]);
            User::where('uid',$spuser['uid'])->update(['cycle_time'=>strtotime("+31 day")]);
            if($send==1)
            {
                $p_user = User::find($gpinfo['uid']);
                $sp_brokeragePrice1 = 3000;
                $gp_balance = bcadd($p_user['gp_now_money'], $sp_brokeragePrice1, 2);
                $mark = "公排号:" . $no . "满3个加报单余额3000元,订单号:" . $order['order_id'];
                $res = UserBill::income('满3个报单余额', $p_user['uid'], 'now_money', 'brokerage', $sp_brokeragePrice1, $order['id'], $gp_balance, $mark, 1);
                User::where('uid', $p_user['uid'])->inc('gp_now_money', $sp_brokeragePrice1)->update();
                self::where('no', $p_no)->update(['send'=>1]);
            }
            $p_no_cts =  self::where('no', $gp_id)->value('team_cts');
            if ($p_no_cts>0 && $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个拥金3000元,订单号:" . $order['order_id'];
                            $res = UserBill::income('满3个拥金', $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 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;
                    }
                }
            }
            $no_path = explode(",",$no_path);
            if (sizeof($no_path) > 3) {
                $path = array_reverse($no_path);
                $n = 0;
                foreach ($path as $v) {
                    $n++;
                    if ($n <=3) continue;
                    if($n>10) break;
                    $sp_brokeragePrice = $order['team'];
                    $user = user::find(self::where('no', $v)->value('uid'));
                    $spuser = user::find($user['spread_uid']);
                    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();
                    }
                    if($spuser && $user['spread_uid']>0 && $spuser['cycle_time']>time())
                    {
                        $sp_brokeragePrice = $order['recommend'];
                        $balance = bcadd($spuser['brokerage_price'], $sp_brokeragePrice, 2);
                        $mark = "公排号:" . $no . '推荐奖' . $order['recommend'] . "元,订单号:" . $order['order_id'];
                        $res = UserBill::income('推荐奖', $spuser['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
                        User::where('uid', $spuser['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
                    }
                }
            }
            return true;
        }catch (Exception $e)
        {
            echo $e->getFile().'--'.$e->getLine().'--'.$e->getMessage();
            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;$send = 0;$info = null;
        if($no==1) {
            $no_path[] =1;
            $gp_id = 1;
        }
        else
        {
            $gp_id = self::where('team_cts','<',12)->value('no');
            $min = self::where('cts','<',3)->order("id asc")->find();
            $p_no = $min['no'];
            $no_path = explode(",",$min['no_path']);
            $no_path[] = $no;
            if($min['cts']==2)
            {
                $send = 1;
            }
            $info = $min;
        }
        $no_path = join(",",$no_path);
        return compact('p_no','no_path','gp_id','send','info');

    }
}