<?php

namespace app\models\system;

use app\admin\model\system\SystemUserLevel;
use app\models\store\StoreCart;
use app\models\store\StoreOrder;
use app\models\user\User;
use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;

/**
 * TODO 会员等级Model
 * Class UserLevel
 * @package app\models\user
 */
class SystemGroupLevel extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'system_group_level';

    use ModelTrait;

    /**
     * 获取查询条件
     * @param $where
     * @param string $alert
     * @param null $model
     * @return SystemUserLevel|null
     */
    public static function setWhere($where, $alert = '', $model = null)
    {
        $model = $model === null ? new self() : $model;
        if ($alert) $model = $model->alias($alert);
        $alert = $alert ? $alert . '.' : '';
        $model = $model->where("{$alert}is_del", 0);
        if (isset($where['is_show']) && $where['is_show'] !== '') $model = $model->where("{$alert}is_show", $where['is_show']);
        if (isset($where['title']) && $where['title']) $model = $model->where("{$alert}name", 'LIKE', "%$where[title]%");
        return $model;
    }

    public static function getSystemList($where)
    {
        $data = self::setWhere($where)->order('id asc')->page((int)$where['page'], (int)$where['limit'])->select();
        $data = count($data) ? $data->toArray() : [];
        $count = self::setWhere($where)->count();
        return compact('data', 'count');
    }

    public static function checkLevel($uid)
    {
        $spread = User::where('uid', $uid)->find();
//        $spread = User::where('uid', $user['spread_uid'])->find();
        while ($spread) {
            //伞下全员
//            $uids = [];
//            $sp = [$spread['uid']];
//            while ($sp) {
//                $sp = User::where('spread_uid', 'in', $sp)->column('uid');
//                if (count($sp)) $uids = array_merge($uids, $sp);
//            }
//            $orders = StoreOrder::where('paid', 1)
//                ->where('status', 'in', [2, 3])
//                ->where('uid', 'in', $uids)
//                ->where('refund_status', 0)
//                ->where('is_del', 0)
//                ->where('is_system_del', 0)->select();
//            $num = 0;
//            foreach ($orders as $v) {
//                $num += StoreCart::where('id', 'in', $v['cart_id'])->where('is_suit', 1)->sum('cart_num');
//            }
            $level_info = self::where('id', $spread['group_level'])->find();
            $level = self::where('level_num', '>', $level_info['level_num'] ?? 0)->order('level_num', 'desc')->select();
            foreach ($level as $v) {
                if ($spread['group_suit_num'] >= $v['level_num']) {
                    User::where('uid', $spread['uid'])->update(['group_level' => $v['id']]);
                    SystemAwardLevel::sendBackOrderBrokerage($spread['uid'], $spread['award_level'] > $v['id'] ? $v['id'] : $spread['award_level']);
                    break;
                }
            }
            $spread = User::where('uid', $spread['spread_uid'])->find();
        }
        return true;
    }
}