<?php


namespace app\models\tree;


use app\models\user\User;
use app\models\user\UserBill;
use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class Tree extends BaseModel
{
    use ModelTrait;

    protected $name = 'tree';

    /**
     * 排点
     * @param int $uid
     * @param int $spread_uid
     * @return array|bool|int[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getTreePoint()
    {
        return self::max('tree_num') + 1;
    }

    /**
     * 进场
     * @param int $uid
     * @param int $spread_uid
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function insertTree(int $uid): bool
    {
        $point = self::getTreePoint();
        if (!$point) return self::setErrorInfo(self::getErrorInfo());
        self::beginTrans();
        try {
            $res = self::create([
                'uid' => $uid,
                'tree_num' => $point,
                'add_time' => time(),
            ]);
            $res = $res && self::sendToUper($point);
            self::checkTrans($res);
            if ($res) {
                return true;
            } else {
                return self::setErrorInfo('加入失败');
            }
        } catch (\Exception $e) {
            self::rollbackTrans();
            return self::setErrorInfo($e->getMessage());
        }
    }

    /**
     * 发奖
     * @param int $point
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function sendToUper(int $point): bool
    {
        $get = 132;
        $info = self::where('tree_num', $point - 1)->find();
        $layer = 0;
        $up_point = $point - 1;
        $res = true;
        while ($info && $layer < 12) {
            $balance = User::where('uid', $info['uid'])->value('brokerage_price');
            $res = $res && UserBill::income('市场分红(佣金)', $info['uid'], 'now_money', 'brokerage', bcmul($get, 0.5, 2), $point, bcadd($balance, bcmul($get, 0.5, 2), 2), '下级用户参与分红，获得分红' . bcmul($get, 0.5, 2));
            $res = $res && User::where('uid', $info['uid'])->inc('brokerage_price', bcmul($get, 0.5, 2))->update();
            $balance2 = User::where('uid', $info['uid'])->value('integral');
            $res = $res && UserBill::income('市场分红(消费券)', $info['uid'], 'integral', 'brokerage_integral', bcsub($get, bcmul($get, 0.5, 2), 2), $point, bcadd($balance2, bcsub($get, bcmul($get, 0.5, 2), 2), 2), '下级用户参与分红，获得分红' . bcsub($get, bcmul($get, 0.5, 2), 2));
            $res = $res && User::where('uid', $info['uid'])->inc('integral', bcsub($get, bcmul($get, 0.5, 2), 2))->update();
            $info->get += $get;
            $res = $res && $info->save();
            $up_point = $up_point - 1;
            $info = self::where('tree_num', $up_point)->find();
            $layer++;
        }
        return $res;
    }
}