generated from liutk/owl-admin-base
106 lines
3.3 KiB
PHP
106 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use App\Models\ActivityGame;
|
|
use App\Models\Filters\ActivityGameFilter;
|
|
use App\Traits\UploadTrait;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use DB;
|
|
|
|
/**
|
|
* @method ActivityGame getModel()
|
|
* @method ActivityGame|\Illuminate\Database\Query\Builder query()
|
|
*/
|
|
class ActivityGameService extends BaseService
|
|
{
|
|
use UploadTrait;
|
|
|
|
protected string $modelName = ActivityGame::class;
|
|
|
|
protected string $modelFilterName = ActivityGameFilter::class;
|
|
|
|
protected bool $modelSortAble = true;
|
|
|
|
public function store($data): bool
|
|
{
|
|
$columns = $this->getTableColumns();
|
|
$model = $this->getModel();
|
|
|
|
$data['home_logo'] = $this->saveImage('home_logo', 'activity_games/home_logo')[0] ?? Storage::disk('public')->url('images/default-gamelogo.png');
|
|
$data['away_logo'] = $this->saveImage('away_logo', 'activity_games/away_logo')[0] ?? Storage::disk('public')->url('images/default-gamelogo.png');
|
|
|
|
foreach ($data as $k => $v) {
|
|
if (!in_array($k, $columns)) {
|
|
continue;
|
|
}
|
|
|
|
$model->setAttribute($k, $v);
|
|
}
|
|
|
|
return $model->save();
|
|
}
|
|
|
|
public function update($primaryKey, $data): bool
|
|
{
|
|
$columns = $this->getTableColumns();
|
|
$model = $this->query()->whereKey($primaryKey)->first();
|
|
|
|
if(array_key_exists('home_logo', $data)){
|
|
$data['home_logo'] = $this->saveImage('home_logo', 'activity_games/home_logo')[0] ?? Storage::disk('public')->url('images/default-gamelogo.png');
|
|
}
|
|
if(array_key_exists('away_logo', $data)){
|
|
$data['away_logo'] = $this->saveImage('away_logo', 'activity_games/away_logo')[0] ?? Storage::disk('public')->url('images/default-gamelogo.png');
|
|
}
|
|
|
|
foreach ($data as $k => $v) {
|
|
if (!in_array($k, $columns)) {
|
|
continue;
|
|
}
|
|
|
|
$model->setAttribute($k, $v);
|
|
}
|
|
|
|
return $model->save();
|
|
}
|
|
|
|
|
|
public function delete(string $ids): mixed
|
|
{
|
|
$ids = explode(',', $ids);
|
|
//判断活动未发布,才可删除
|
|
if($this->query()->whereIn('id', $ids)->where('state', '<>', 0)->exists()){
|
|
$this->setError('已发布竞猜不能删除');
|
|
}
|
|
return $this->query()->whereIn('id', $ids)->delete();
|
|
}
|
|
|
|
public function finishGame(ActivityGame $activityGame, $score)
|
|
{
|
|
try{
|
|
DB::beginTransaction();
|
|
//更新状态,结果
|
|
$activityGame->update([
|
|
'state' => 2,
|
|
'score' => $score
|
|
]);
|
|
$activityGame->logs()->where('score', $score)->update(['is_right'=>1]);
|
|
$activityGame->activity->logs()->whereIn('user_id',
|
|
$activityGame->logs()->where('score', $score)->get()->pluck('user_id')->toArray()
|
|
)
|
|
->incrementEach([
|
|
'mark'=> $activityGame->mark,
|
|
'right_times'=> 1
|
|
]);
|
|
|
|
DB::commit();
|
|
}catch(Throwable $th){
|
|
DB::rollBack();
|
|
report($th);
|
|
return $this->setError('系统错误,请刷新后重试');
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |