generated from panliang/owl-admin-starter
106 lines
3.0 KiB
PHP
106 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Enums\CheckStatus;
|
|
use App\Exceptions\BaseException;
|
|
use App\ModelFilters\UserScoreFilter;
|
|
use App\Models\PartyUser;
|
|
use App\Models\UserScore;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Slowlyo\OwlAdmin\Admin;
|
|
|
|
class UserScoreService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['user', 'type', 'cate', 'checkUser'];
|
|
|
|
protected string $modelName = UserScore::class;
|
|
|
|
protected string $modelFilterName = UserScoreFilter::class;
|
|
|
|
public function listQuery()
|
|
{
|
|
$filter = $this->getModelFilter();
|
|
|
|
$query = $this->query();
|
|
if ($this->withRelationships) {
|
|
$query->with($this->withRelationships);
|
|
}
|
|
|
|
if ($filter) {
|
|
$query->filter(request()->input(), $filter);
|
|
}
|
|
|
|
return $query->sort();
|
|
}
|
|
|
|
public function check(UserScore $info, $options = [])
|
|
{
|
|
if ($info->check_status == CheckStatus::Success) {
|
|
throw new BaseException('已经审核过了');
|
|
}
|
|
$status = data_get($options, 'check_status');
|
|
$attributes = [
|
|
'check_status' => $status,
|
|
'check_remarks' => data_get($options, 'check_remarks'),
|
|
'check_user_id' => data_get($options, 'check_user_id', Admin::user()->id),
|
|
'check_at' => now(),
|
|
];
|
|
if ($status == CheckStatus::Success->value) {
|
|
$attributes['score'] = data_get($options, 'score', 0);
|
|
if ($attributes['score'] <= 0) {
|
|
throw new BaseException('得分必填');
|
|
}
|
|
|
|
PartyUserService::make()->incrementScore($info->user, $info->type?->key, $attributes['score']);
|
|
}
|
|
|
|
$info->update($attributes);
|
|
}
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
if (!$model) {
|
|
$data['cate_id'] = PartyUser::where('id', $data['user_id'])->value('cate_id');
|
|
}
|
|
$images = data_get($data, 'images');
|
|
if ($images && !is_array($images)) {
|
|
$data['images'] = explode(',', $images);
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
$createRules = [
|
|
'type_id' => 'required',
|
|
'cate_id' => 'required',
|
|
'user_id' => 'required',
|
|
];
|
|
$updateRules = [
|
|
];
|
|
$validator = Validator::make($data, $model ? $updateRules : $createRules, [
|
|
'type_id.required' => __('user_score.type_id') . ' 必填',
|
|
'cate_id.required' => __('user_score.cate_id') . ' 必填',
|
|
'user_id.required' => __('user_score.user_id') . ' 必填',
|
|
]);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
/**
|
|
* 统计时间段内党员得分
|
|
*
|
|
* @param $start
|
|
* @param $end
|
|
* @return void
|
|
*/
|
|
public static function userTotal($start, $end)
|
|
{
|
|
$list = UserScore::where('check_status', CheckStatus::Success)->whereBetween('check_at', [$start, $end])->get();
|
|
}
|
|
}
|