126 lines
3.9 KiB
PHP
126 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Forms;
|
|
|
|
use Dcat\Admin\Widgets\Form;
|
|
use App\Models\{Guess, GuessOption, UserGuess};
|
|
use Dcat\Admin\Traits\LazyWidget;
|
|
use Dcat\Admin\Contracts\LazyRenderable;
|
|
|
|
/**
|
|
* 弹窗设置竞猜答案
|
|
*/
|
|
class GuessAnswer extends Form implements LazyRenderable
|
|
{
|
|
use LazyWidget;
|
|
|
|
/**
|
|
* Handle the form request.
|
|
*
|
|
* @param array $input
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle(array $input)
|
|
{
|
|
$id = $this->payload['id'] ?? null;
|
|
$info = Guess::findOrFail($id);
|
|
if ($info->prize_state === Guess::PRIZE_EXIST) {
|
|
return $this->response()->error('已经公布答案');
|
|
}
|
|
|
|
// 正确答案id
|
|
$answers = $input['option'];
|
|
sort($answers);
|
|
if (count($answers) != $info->num) {
|
|
return $this->response()->error('答案应该有'.$info->num.'个');
|
|
}
|
|
// 关闭答题活动
|
|
$info->update([
|
|
'prize_state' => Guess::PRIZE_EXIST
|
|
]);
|
|
|
|
// 修改选项结果
|
|
|
|
$info->options()->update([
|
|
'correct_state' => GuessOption::UN_CORRECT
|
|
]);
|
|
$info->options()->whereIn('id', $answers)->update([
|
|
'correct_state' => GuessOption::CORRECT
|
|
]);
|
|
|
|
// 回答正确的记录
|
|
$right_ids = [];
|
|
// 遍历所有答题记录
|
|
foreach($info->users()->with(['options'])->get() as $item) {
|
|
$options_id = $item->options->pluck('id')->all();
|
|
sort($options_id);
|
|
if ($options_id == $answers) {
|
|
array_push($right_ids, $item->id);
|
|
}
|
|
}
|
|
|
|
// 开奖
|
|
$prize_id = [];
|
|
|
|
$prize_num = data_get($input, 'prize_num', 1);
|
|
if ($prize_num > 0) {
|
|
# code...
|
|
if ($prize_num > count($right_ids)) {
|
|
$prize_num = count($right_ids);
|
|
$prize_id = $right_ids;
|
|
} else if($prize_num == 1 && count($right_ids) >= 1) {
|
|
$prize_id_key = array_rand($right_ids, $prize_num);
|
|
$prize_id[] = $right_ids[$prize_id_key];
|
|
} else if ($prize_num > 1 && count($right_ids) >= 2) {
|
|
$prize_id_key = array_rand($right_ids, $prize_num);
|
|
foreach ($prize_id_key as $key => $value) {
|
|
$prize_id[] = $right_ids[$value];
|
|
}
|
|
}
|
|
}
|
|
|
|
$info->users()->update([
|
|
'correct_state' => UserGuess::UN_CORRECT,
|
|
'prize_state' => UserGuess::PRIZE_NO
|
|
]);
|
|
if (count($right_ids) > 0) {
|
|
$info->users()->whereIn('id', $right_ids)->update([
|
|
'correct_state' => UserGuess::CORRECT
|
|
]);
|
|
}
|
|
if (count($prize_id) > 0) {
|
|
$info->users()->whereIn('id', $prize_id)->update([
|
|
'prize_state' => UserGuess::PRIZE_YES
|
|
]);
|
|
}
|
|
if ($input['tel'] && $input['tel'] != '') {
|
|
# code...
|
|
$tel_array = explode(',', $input['tel']);
|
|
if (count($tel_array) > 0) {
|
|
$info->users()->whereHas('user', function ($query) use($tel_array) {
|
|
$query->whereIn('phone', $tel_array);
|
|
})->update([
|
|
'prize_state' => UserGuess::PRIZE_YES
|
|
]);
|
|
}
|
|
}
|
|
return $this->response()->success('操作成功')->refresh();
|
|
}
|
|
|
|
/**
|
|
* Build a form here.
|
|
*/
|
|
public function form()
|
|
{
|
|
$id = $this->payload['id'] ?? null;
|
|
$info = Guess::findOrFail($id);
|
|
$options = $info->options()->pluck('option_name', 'id');
|
|
|
|
$this->disableResetButton();
|
|
$this->checkbox('option', '选项')->help('请勾选正确的选项')->options($options)->canCheckAll();
|
|
$this->number('prize_num', '多少个奖励')->default(0)->min(0)->help('多少个奖励');
|
|
$this->text('tel', '电话号码')->help('用户电话号码');
|
|
}
|
|
}
|