// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\services\system\config; use qiniu\basic\BaseServices; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; use think\exception\ValidateException; use think\facade\Log; class SystemUserLevelTaskServices extends BaseServices { protected $tasks = [ 'expend' => ['name' => 'expend', 'title' => '消费满额', 'info' => '消费满{%num1%}', 'unit1' => '元'], 'achievement' => ['name' => 'achievement', 'title' => '业绩达标', 'info' => '业绩满{%num1%}', 'unit1' => '元'], 'spread_num' => ['name' => 'spread_num', 'title' => '推荐会员', 'info' => '邀请等级{%num1%}及以上的用户{%num2%}', 'unit1' => '级', 'unit2' => '人'], ]; public function taskBase() { $tasks = $this->tasks; foreach ($tasks as $v) { $units = []; for ($i = 1; ; $i++) { $unit = 'unit' . $i; if (isset($v[$unit])) { $units[] = ['label' => $v[$unit], 'replace' => "{%num" . $i . "%}", 'key' => $i, 'num_name' => 'num' . $i]; } else { break; } } $tasks[$v['name']]['units'] = $units; } return $tasks; } /** * @param array $task * @return array */ public function checkTask(array $task = []): array { foreach ($task as &$v) { if (!$this->tasks[$v['name'] ?? '']) throw new ValidateException('任务不存在'); if (!isset($v['units'])) { throw new ValidateException('请设置任务数量'); } foreach ($v['units'] as $task_value) { if (($task_value['value'] ?? 0) < 0) throw new ValidateException('任务数量不能小于0'); if (!isset($task_value['replace'])) throw new ValidateException('任务参数异常'); $v['info'] = str_replace($task_value['replace'], ($task_value['value'] ?? 0) . ($task_value['label'] ?? ''), ($v['info'] ?? $this->tasks[$v['name']]['info'])); } $v['must'] = $v['must'] ?? 0; } return $task; } /** * @param $id * @param $uid * @param int $start_time * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function checkLevelTask($id, $uid, int $start_time = 0) { /** @var SystemUserLevelServices $services */ $services = app()->make(SystemUserLevelServices::class); $level = $services->getLevel($id); $task = $level['task']; if (!count($task)) return false; $res = false; $must = []; $chose = []; foreach ($task as &$v) { if (!method_exists($this, $v['name'])) { Log::error('等级任务' . $v['name'] . '未完成'); continue; } if (!isset($v['units'])) { Log::error('任务数量未设置'); continue; } $nums = []; foreach ($v['units'] as $task_value) { if (($task_value['value'] ?? 0) < 0) throw new ValidateException('任务数量不能小于0'); $nums['num' . (string)$task_value['key']] = $task_value['value'] ?? 0; } if (!$this->{'task_' . $v['name']}($uid, $start_time, $nums)) { if ($v['must']) $must[] = ['name' => $v['name'], 'status' => 0]; else $chose[] = ['name' => $v['name'], 'status' => 0]; } else { if ($v['must']) $must[] = ['name' => $v['name'], 'status' => 1]; else $chose[] = ['name' => $v['name'], 'status' => 1]; } } if ((count($must) == array_sum(array_column($must, 'status'))) && (array_sum(array_column($chose, 'status')) >= 1)) $res = true; return $res; } //Todo 任务方法 /** * 消费金额 * @param $uid * @param $start_time * @param array $num * @return bool */ private function task_expend($uid, $start_time, array $num): bool { return true; } /** * 业绩 * @param $uid * @param $start_time * @param array $num * @return bool */ private function task_achievement($uid, $start_time, array $num): bool { return true; } /** * 推广人数 * @param $uid * @param $start_time * @param array $num * @return bool */ private function task_spread_num($uid, $start_time, array $num): bool { return true; } }