<?php
namespace app\task\behavior\user;
use think\Cache;
use app\task\model\User as UserModel;
use app\task\model\user\Grade as GradeModel;
class Grade
{
private $model;
public function run($model)
{
if (!$model instanceof GradeModel) {
return new GradeModel and false;
}
$this->model = $model;
if (!$model::$wxapp_id) {
return false;
}
$cacheKey = "__task_space__[user/Grade]__{$model::$wxapp_id}";
if (!Cache::has($cacheKey)) {
$this->setUserGrade();
Cache::set($cacheKey, time(), 60 * 10);
}
return true;
}
private function setUserGrade()
{
$UserModel = new UserModel;
$list = GradeModel::getUsableList(null, ['weight' => 'desc']);
if ($list->isEmpty()) {
return false;
}
$data = [];
foreach ($list as $grade) {
$userList = $UserModel->getUpgradeUserList($grade, array_keys($data));
foreach ($userList as $user) {
if (!isset($data[$user['user_id']])) {
$data[$user['user_id']] = [
'user_id' => $user['user_id'],
'old_grade_id' => $user['grade_id'],
'new_grade_id' => $grade['grade_id'],
];
}
}
}
return $UserModel->setBatchGrade($data);
}
}