generated from panliang/owl-admin-starter
88 lines
2.4 KiB
PHP
88 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\ModelFilters\PartyCateFilter;
|
|
use App\Models\CateRank;
|
|
use App\Models\PartyCate;
|
|
use App\Models\PartyUser;
|
|
use App\Models\UserRank;
|
|
use App\Models\UserScore;
|
|
use Slowlyo\OwlAdmin\Models\AdminPermission;
|
|
use Slowlyo\OwlAdmin\Services\AdminPermissionService;
|
|
|
|
class PartyCateService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['master', 'plan'];
|
|
|
|
protected string $modelName = PartyCate::class;
|
|
|
|
protected string $modelFilterName = PartyCateFilter::class;
|
|
|
|
public function store($data): bool
|
|
{
|
|
$data = $this->resloveData($data);
|
|
|
|
$validate = $this->validate($data);
|
|
if ($validate !== true) {
|
|
$this->setError($validate);
|
|
return false;
|
|
}
|
|
|
|
$info = $this->modelName::create($data);
|
|
|
|
// 创建权限
|
|
$parent = AdminPermission::firstOrCreate(['slug' => 'party_cate'], ['name' => '党支部']);
|
|
AdminPermissionService::make()->store([
|
|
'slug' => 'party_cate_' . $info->id,
|
|
'name' => $info->name,
|
|
'parent_id' => $parent->id,
|
|
'order' => 1,
|
|
'http_method' => $info->id,
|
|
]);
|
|
|
|
return true;
|
|
}
|
|
|
|
public function preDelete(array $ids)
|
|
{
|
|
// 删除党员记录
|
|
PartyUser::whereIn('cate_id', $ids)->delete();
|
|
// 删除审核记录
|
|
UserScore::whereIn('cate_id', $ids)->delete();
|
|
// 删除支部排名记录
|
|
CateRank::whereIn('cate_id', $ids)->delete();
|
|
// 删除党员排名记录
|
|
UserRank::whereIn('cate_id', $ids)->delete();
|
|
// 删除权限
|
|
AdminPermission::whereIn('slug', array_map(fn ($v) => 'party_cate_' . $v, $ids))->delete();
|
|
return true;
|
|
}
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
if (!$model) {
|
|
$list = UserScore::getTypeList();
|
|
$scores = [];
|
|
foreach ($list as $item) {
|
|
$scores[$item->key] = 0;
|
|
}
|
|
$data['scores'] = $scores;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public function incrementScore(PartyCate $info, $type, $score)
|
|
{
|
|
$scores = $info->scores;
|
|
if (isset($scores[$type])) {
|
|
$scores[$type] += $score;
|
|
}
|
|
$info->update([
|
|
'score' => $info->score + $score,
|
|
'current_score' => $info->current_score + $score,
|
|
'scores' => $scores,
|
|
]);
|
|
}
|
|
}
|