init
This commit is contained in:
37
app/Admin/Actions/CleanData.php
Normal file
37
app/Admin/Actions/CleanData.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Actions;
|
||||
|
||||
use App\Models\User;
|
||||
use Encore\Admin\Actions\Action;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CleanData extends Action
|
||||
{
|
||||
|
||||
protected $selector = '.clean';
|
||||
|
||||
public function handle(Request $request)
|
||||
{
|
||||
// 清理用户签到
|
||||
User::where('sign', 1)->update(['sign' => 0]);
|
||||
|
||||
DB::table('item_logs')->truncate();
|
||||
|
||||
return $this->response()->success('清理成功')->refresh();
|
||||
}
|
||||
|
||||
function dialog()
|
||||
{
|
||||
return $this->confirm('确认要清理数据么');
|
||||
}
|
||||
|
||||
public function html()
|
||||
{
|
||||
return <<<HTML
|
||||
<a class="btn btn-sm btn-danger clean"><i class="fa fa-trash"></i> 清理数据</a>
|
||||
HTML;
|
||||
}
|
||||
|
||||
}
|
||||
127
app/Admin/Actions/DestroyData.php
Normal file
127
app/Admin/Actions/DestroyData.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Actions;
|
||||
|
||||
use App\Models\User;
|
||||
use Encore\Admin\Actions\RowAction;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use PhpOffice\PhpWord\Exception\CopyFileException;
|
||||
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
|
||||
use PhpOffice\PhpWord\TemplateProcessor;
|
||||
|
||||
class DestroyData extends RowAction
|
||||
{
|
||||
|
||||
public $name = '数据导出';
|
||||
|
||||
public function dialog()
|
||||
{
|
||||
$this->confirm('确定要销毁数据么,一旦操作不可恢复?');
|
||||
}
|
||||
|
||||
public function handle(Model $model)
|
||||
{
|
||||
try {
|
||||
if ($model->archives) {
|
||||
// return $this->response()->error('数据销毁失败')->refresh();
|
||||
}
|
||||
// 先做存档
|
||||
$archives = [];
|
||||
|
||||
foreach ($model->items as $key => $item) {
|
||||
$archives[$key] = [
|
||||
'item_id' => $item->id,
|
||||
'item_name' => $item->name,
|
||||
'total' => User::where('type', 0)->count(),
|
||||
'sign' => User::where('type', 0)->where('sign', 1)->count(),
|
||||
'tickets' => $item->logs()->sum('result'),
|
||||
];
|
||||
}
|
||||
$model->archives = $archives;
|
||||
$model->save();
|
||||
|
||||
if ($model->type == 'diff') {
|
||||
$download = $this->exportDiff($model);
|
||||
} else {
|
||||
$download = $this->exportEqual($model);
|
||||
}
|
||||
|
||||
// $model->logs()->delete();
|
||||
|
||||
return $this->response()->success('数据销毁完成')->refresh()->download($download);
|
||||
} catch (\RuntimeException $exception) {
|
||||
return $this->response()->error('数据销毁失败'.$exception->getMessage())->refresh();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 等额数据导出
|
||||
*
|
||||
* @Date : 2022/5/4 15:39
|
||||
* @Author : <Jason.C>
|
||||
* @param $vote
|
||||
* @return string
|
||||
* @throws CopyFileException
|
||||
* @throws CreateTemporaryFileException
|
||||
*/
|
||||
public function exportEqual($vote)
|
||||
{
|
||||
$templateProcessor = new TemplateProcessor(storage_path('app/public/DENG_FEN.docx'));
|
||||
|
||||
$templateProcessor->setValue('TOTAL1', $vote->logs()->distinct('user_id')->count());
|
||||
$templateProcessor->setValue('TITLE', $vote->title);
|
||||
|
||||
$i = 0;
|
||||
$tpl = '';
|
||||
foreach ($vote->items as $item) {
|
||||
$i++;
|
||||
$tpl .= $i.'. '.$item->name.' '.$item->logs()->sum('result')." 票 <w:br />";
|
||||
}
|
||||
|
||||
$templateProcessor->setValue('LOOPS', $tpl);
|
||||
$save = 'download_'.uniqid().'.docx';
|
||||
$templateProcessor->saveAs(storage_path('app/public/'.$save));
|
||||
|
||||
return '/storage/'.$save;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 差额数据倒出
|
||||
*
|
||||
* @Author: <C.Jason>
|
||||
* @Date : 2020/5/7 4:34 下午
|
||||
* @param $vote
|
||||
* @return string
|
||||
* @throws CopyFileException
|
||||
* @throws CreateTemporaryFileException
|
||||
*/
|
||||
protected function exportDiff($vote): string
|
||||
{
|
||||
$templateProcessor = new TemplateProcessor(storage_path('app/public/CHA_E.docx'));
|
||||
|
||||
$templateProcessor->setValue('TOTAL', $vote->logs()->distinct('user_id')->count());
|
||||
$templateProcessor->setValue('TITLE', $vote->title);
|
||||
$templateProcessor->setValue('DATE', date('Y年m月d日', time()));
|
||||
|
||||
$data = $vote->items;
|
||||
|
||||
$data = $data->sortByDesc(function ($item) {
|
||||
return $item->logs()->sum('result');
|
||||
});
|
||||
|
||||
$i = 0;
|
||||
$tpl = '';
|
||||
foreach ($data as $item) {
|
||||
$i++;
|
||||
$tpl .= $i.'. '.$item->name.' '.$item->logs()->sum('result')." 票<w:br />";
|
||||
}
|
||||
|
||||
$templateProcessor->setValue('TPL', $tpl);
|
||||
|
||||
$save = 'download_'.uniqid().'.docx';
|
||||
$templateProcessor->saveAs(storage_path('app/public/'.$save));
|
||||
|
||||
return '/storage/'.$save;
|
||||
}
|
||||
|
||||
}
|
||||
49
app/Admin/Actions/ExportCha.php
Normal file
49
app/Admin/Actions/ExportCha.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Actions;
|
||||
|
||||
use App\Models\Vote;
|
||||
use Encore\Admin\Actions\Action;
|
||||
use Illuminate\Http\Request;
|
||||
use PhpOffice\PhpWord\TemplateProcessor;
|
||||
|
||||
class ExportCha extends Action
|
||||
{
|
||||
|
||||
public $name = '差额倒出';
|
||||
|
||||
protected $vote;
|
||||
|
||||
protected $selector = '.export-cha';
|
||||
|
||||
public function handle(Request $request)
|
||||
{
|
||||
$templateProcessor = new TemplateProcessor(storage_path('app/public/CHA_E.docx'));
|
||||
|
||||
$vote = Vote::find(2);
|
||||
$templateProcessor->setValue('TOTAL', $vote->logs()->distinct('user_id')->count());
|
||||
$templateProcessor->setValue('TITLE', $vote->title);
|
||||
|
||||
$i = 0;
|
||||
$tpl = '';
|
||||
foreach ($vote->items as $item) {
|
||||
$i++;
|
||||
$tpl .= $i . '. ' . $item->name . ' ' . $item->logs()->sum('result') . " 票\r";
|
||||
}
|
||||
|
||||
$templateProcessor->setValue('TPL', $tpl);
|
||||
|
||||
$save = 'chae_1.docx';
|
||||
$templateProcessor->saveAs(storage_path('app/public/' . $save));
|
||||
|
||||
return $this->response()->success('倒出成功')->download('/storage/' . $save);
|
||||
}
|
||||
|
||||
public function html()
|
||||
{
|
||||
return <<<HTML
|
||||
<a class="btn btn-sm btn-warning export-cha"><i class="fa fa-upload"></i> 差额数据倒出</a>
|
||||
HTML;
|
||||
}
|
||||
|
||||
}
|
||||
48
app/Admin/Actions/ExportDeng.php
Normal file
48
app/Admin/Actions/ExportDeng.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Actions;
|
||||
|
||||
use App\Models\Vote;
|
||||
use Encore\Admin\Actions\Action;
|
||||
use Illuminate\Http\Request;
|
||||
use PhpOffice\PhpWord\TemplateProcessor;
|
||||
|
||||
class ExportDeng extends Action
|
||||
{
|
||||
|
||||
public $name = '批量导入';
|
||||
|
||||
protected $vote;
|
||||
|
||||
protected $selector = '.export-deng';
|
||||
|
||||
public function handle(Request $request)
|
||||
{
|
||||
$templateProcessor = new TemplateProcessor(storage_path('app/public/DENG_E.docx'));
|
||||
|
||||
$vote1 = Vote::find(1);
|
||||
$templateProcessor->setValue('TOTAL1', $vote1->logs()->distinct('user_id')->count());
|
||||
|
||||
foreach ($vote1->items as $item) {
|
||||
$templateProcessor->setValue('ITEM_' . $item->id, $item->logs()->sum('result'));
|
||||
}
|
||||
$vote3 = Vote::find(3);
|
||||
$templateProcessor->setValue('TOTAL3', $vote1->logs()->distinct('user_id')->count());
|
||||
foreach ($vote3->items as $item) {
|
||||
$templateProcessor->setValue('ITEM_' . $item->id, $item->logs()->sum('result'));
|
||||
}
|
||||
|
||||
$save = 'denge_1.docx';
|
||||
$templateProcessor->saveAs(storage_path('app/public/' . $save));
|
||||
|
||||
return $this->response()->success('倒出成功')->download('/storage/' . $save);
|
||||
}
|
||||
|
||||
public function html()
|
||||
{
|
||||
return <<<HTML
|
||||
<a class="btn btn-sm btn-info export-deng"><i class="fa fa-upload"></i> 等额数据倒出</a>
|
||||
HTML;
|
||||
}
|
||||
|
||||
}
|
||||
42
app/Admin/Actions/Replicate.php
Normal file
42
app/Admin/Actions/Replicate.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Actions;
|
||||
|
||||
use Encore\Admin\Actions\RowAction;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class Replicate extends RowAction
|
||||
{
|
||||
|
||||
public $name = '复制活动';
|
||||
|
||||
public function dialog()
|
||||
{
|
||||
$this->confirm('确定复制活动链接么?');
|
||||
}
|
||||
|
||||
public function handle(Model $model)
|
||||
{
|
||||
try {
|
||||
DB::transaction(function () use ($model) {
|
||||
$new = $model->replicate();
|
||||
$new->status = 0;
|
||||
$new->save();
|
||||
foreach ($model->items as $item) {
|
||||
$new->items()->create([
|
||||
'name' => $item->name,
|
||||
'cover' => $item->cover,
|
||||
'desc' => $item->desc,
|
||||
'desc2' => $item->desc2,
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
return $this->response()->success('活动链接复制完成')->refresh();
|
||||
} catch (\RuntimeException $exception) {
|
||||
return $this->response()->error('活动链接复制出错了')->refresh();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
79
app/Admin/Actions/UserImport.php
Normal file
79
app/Admin/Actions/UserImport.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Actions;
|
||||
|
||||
use App\Models\User;
|
||||
use Encore\Admin\Actions\Action;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class UserImport extends Action
|
||||
{
|
||||
|
||||
public $name = '批量导入';
|
||||
|
||||
protected $vote;
|
||||
|
||||
protected $selector = '.import-post';
|
||||
|
||||
public function handle(Request $request)
|
||||
{
|
||||
$file = $request->file('file');
|
||||
// if ($file->extension() != 'csv') {
|
||||
// return $this->response()->error('必须使用csv文件' . $file->extension());
|
||||
// }
|
||||
$handle = fopen($file, 'r');
|
||||
if (!$handle) {
|
||||
exit('读取文件失败');
|
||||
}
|
||||
$failed = 0;
|
||||
$success = 0;
|
||||
while (($data = fgetcsv($handle)) !== false) {
|
||||
if (empty($data[0])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strlen($data[0]) != 11) {
|
||||
$failed++;
|
||||
continue;
|
||||
}
|
||||
if ($this->preg_is_utf8($data[1])) {
|
||||
$name = $data[1];
|
||||
} else {
|
||||
$name = iconv('gbk', 'utf-8', $data[1]);
|
||||
}
|
||||
try {
|
||||
User::create([
|
||||
'parent_id' => $request->parent_id ?: 1,
|
||||
'mobile' => $data[0],
|
||||
'name' => $name,
|
||||
]);
|
||||
$success++;
|
||||
} catch (\Exception $exception) {
|
||||
$failed++;
|
||||
}
|
||||
}
|
||||
fclose($handle);
|
||||
|
||||
return $this->response()->success('批量处理成功' . $success . '条,失败' . $failed . '条')->refresh();
|
||||
}
|
||||
|
||||
function preg_is_utf8($string)
|
||||
{
|
||||
return preg_match('/^.*$/u', $string) > 0;//preg_match('/^./u', $string)
|
||||
}
|
||||
|
||||
public function form()
|
||||
{
|
||||
$parents = User::select()->where('type', 1)->pluck('name', 'id');
|
||||
$this->select('parent_id')->options($parents);
|
||||
$this->file('file', '请选择文件')->help('CSV - 手机号-姓名');
|
||||
}
|
||||
|
||||
public function html()
|
||||
{
|
||||
return <<<HTML
|
||||
<a class="btn btn-sm btn-info import-post"><i class="fa fa-upload"></i> 批量导入</a>
|
||||
HTML;
|
||||
}
|
||||
|
||||
}
|
||||
41
app/Admin/Controllers/ArticleController.php
Normal file
41
app/Admin/Controllers/ArticleController.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Controllers;
|
||||
|
||||
use App\Models\Article;
|
||||
use App\Models\Category;
|
||||
use Encore\Admin\Controllers\AdminController;
|
||||
use Encore\Admin\Form;
|
||||
use Encore\Admin\Grid;
|
||||
|
||||
class ArticleController extends AdminController
|
||||
{
|
||||
|
||||
protected $title = '内容管理';
|
||||
|
||||
public function grid()
|
||||
{
|
||||
$grid = new Grid(new Article);
|
||||
$grid->column('id', '#ID#');
|
||||
$grid->column('title', '文章标题');
|
||||
$grid->column('sort', '序号');
|
||||
$grid->column('created_at', '创建时间');
|
||||
|
||||
return $grid;
|
||||
}
|
||||
|
||||
public function form()
|
||||
{
|
||||
$form = new Form(new Article);
|
||||
|
||||
$form->text('title', '文章标题');
|
||||
$form->number('category_id', '分类ID')
|
||||
->help('审阅文件固定写 2 ');
|
||||
$form->textarea('desc');
|
||||
$form->ueditor('content', '文章内容')->rules('required', ['required' => '详情不能为空']);
|
||||
$form->number('sort', '序号')->default(0)->rules('required', ['required' => '序号必须填写'])->help('倒序优先');
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
}
|
||||
10
app/Admin/Controllers/AuthController.php
Normal file
10
app/Admin/Controllers/AuthController.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Controllers;
|
||||
|
||||
use Encore\Admin\Controllers\AuthController as BaseAuthController;
|
||||
|
||||
class AuthController extends BaseAuthController
|
||||
{
|
||||
|
||||
}
|
||||
21
app/Admin/Controllers/HomeController.php
Normal file
21
app/Admin/Controllers/HomeController.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Encore\Admin\Controllers\Dashboard;
|
||||
use Encore\Admin\Layout\Column;
|
||||
use Encore\Admin\Layout\Content;
|
||||
use Encore\Admin\Layout\Row;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
|
||||
public function index(Content $content)
|
||||
{
|
||||
return $content
|
||||
->title('Dashboard')
|
||||
->description('Description...');
|
||||
}
|
||||
|
||||
}
|
||||
82
app/Admin/Controllers/ItemController.php
Normal file
82
app/Admin/Controllers/ItemController.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Item;
|
||||
use App\Models\Vote;
|
||||
use Encore\Admin\Form;
|
||||
use Encore\Admin\Grid;
|
||||
use Encore\Admin\Layout\Content;
|
||||
|
||||
class ItemController extends Controller
|
||||
{
|
||||
|
||||
public function index(Content $content, Vote $vote)
|
||||
{
|
||||
return $content
|
||||
->header($vote->title)
|
||||
->description('投票列表')
|
||||
->body($this->grid($vote));
|
||||
}
|
||||
|
||||
protected function grid($vote)
|
||||
{
|
||||
$grid = new Grid(new Item);
|
||||
$grid->model()->where('vote_id', $vote->id);
|
||||
$grid->column('cover')->image('', 80);
|
||||
$grid->column('id', '#ID#');
|
||||
$grid->column('name');
|
||||
$grid->column('sort');
|
||||
$grid->column('得票数')->display(function () {
|
||||
return $this->logs()->where('result', 1)->count();
|
||||
});
|
||||
$grid->column('created_at', '创建时间')->date('Y-m-d H:i:s');
|
||||
|
||||
return $grid;
|
||||
}
|
||||
|
||||
function create(Content $content, Vote $vote)
|
||||
{
|
||||
return $content
|
||||
->header($vote->title)
|
||||
->description('新增条目')
|
||||
->body($this->form($vote));
|
||||
}
|
||||
|
||||
function form($vote)
|
||||
{
|
||||
$form = new Form(new Item);
|
||||
|
||||
$form->hidden('vote_id')->value($vote->id);
|
||||
$form->text('name', '姓名');
|
||||
$form->number('sort', '排序')
|
||||
->default(1)
|
||||
->help('正序排序');
|
||||
$form->textarea('desc', '竞选岗位');
|
||||
$form->textarea('desc2', '服务队');
|
||||
$form->image('cover', '照片')
|
||||
->uniqueName();
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
function store(Vote $vote)
|
||||
{
|
||||
return $this->form($vote)->store();
|
||||
}
|
||||
|
||||
function edit(Content $content, Vote $vote, $id)
|
||||
{
|
||||
return $content
|
||||
->header($vote->title)
|
||||
->description('新增条目')
|
||||
->body($this->form($vote)->edit($id));
|
||||
}
|
||||
|
||||
function update(Vote $vote, $id)
|
||||
{
|
||||
return $this->form($vote)->update($id);
|
||||
}
|
||||
|
||||
}
|
||||
60
app/Admin/Controllers/UserController.php
Normal file
60
app/Admin/Controllers/UserController.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Controllers;
|
||||
|
||||
use App\Admin\Actions\CleanData;
|
||||
use App\Admin\Actions\UserImport;
|
||||
use App\Models\User;
|
||||
use Encore\Admin\Controllers\AdminController;
|
||||
use Encore\Admin\Form;
|
||||
use Encore\Admin\Grid;
|
||||
|
||||
class UserController extends AdminController
|
||||
{
|
||||
|
||||
protected $title = '用户管理';
|
||||
|
||||
public function grid(): Grid
|
||||
{
|
||||
$grid = new Grid(new User);
|
||||
$grid->tools(function (Grid\Tools $tools) {
|
||||
$tools->append(new UserImport);
|
||||
$tools->append(new CleanData);
|
||||
});
|
||||
// $grid->disableCreateButton();
|
||||
// $grid->disableActions();
|
||||
$grid->column('归属干事')->display(function () {
|
||||
return $this->parent->name ?? '';
|
||||
});
|
||||
$grid->column('mobile', '手机号');
|
||||
$grid->column('name', '姓名');
|
||||
$grid->column('sign', '签到')->bool();
|
||||
$grid->column('type', '角色')->using([
|
||||
0 => '投票人',
|
||||
1 => '干事',
|
||||
2 => '总干事',
|
||||
]);
|
||||
$grid->column('created_at', '创建时间')->date('Y-m-d H:i:s');;
|
||||
|
||||
return $grid;
|
||||
}
|
||||
|
||||
protected function form(): Form
|
||||
{
|
||||
$form = new Form(new User);
|
||||
|
||||
$form->text('name', '姓名');
|
||||
$form->text('mobile', '手机号');
|
||||
$form->select('parent_id','归属干事')->options(
|
||||
User::where('type', 1)->pluck('name', 'id')
|
||||
)->default(0);
|
||||
$form->select('type')->options([
|
||||
0 => '投票人',
|
||||
1 => '干事',
|
||||
2 => '总干事',
|
||||
])->help('只有干事,才可以呗归属,投票人必须要有归属,干事不可以有归属。');
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
}
|
||||
135
app/Admin/Controllers/VoteController.php
Normal file
135
app/Admin/Controllers/VoteController.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Controllers;
|
||||
|
||||
use App\Admin\Actions\DestroyData;
|
||||
use App\Admin\Actions\ExportCha;
|
||||
use App\Admin\Actions\ExportDeng;
|
||||
use App\Admin\Actions\Replicate;
|
||||
use App\Models\Item;
|
||||
use App\Models\User;
|
||||
use App\Models\Vote;
|
||||
use Encore\Admin\Controllers\AdminController;
|
||||
use Encore\Admin\Form;
|
||||
use Encore\Admin\Grid;
|
||||
use Encore\Admin\Widgets\Table;
|
||||
|
||||
class VoteController extends AdminController
|
||||
{
|
||||
|
||||
protected $title = '投票管理';
|
||||
|
||||
public function grid()
|
||||
{
|
||||
$grid = new Grid(new Vote());
|
||||
// $grid->tools(function (Grid\Tools $tools) {
|
||||
// $tools->append(new ExportDeng());
|
||||
// $tools->append(new ExportCha());
|
||||
// });
|
||||
$grid->actions(function ($actions) {
|
||||
$actions->add(new Replicate);
|
||||
$actions->add(new DestroyData);
|
||||
});
|
||||
$grid->column('id', '#ID#');
|
||||
$grid->column('title', '投票名称');
|
||||
$grid->column('type', '类型')->using(['diff' => '差额投票', 'equal' => '等额投票']);
|
||||
$grid->column('status', '状态')->switch();
|
||||
$grid->column('数量')->display(function () {
|
||||
return '<a href="'.admin_url('votes/'.$this->id.'/items').'">'.$this->items()->count().'</a>';
|
||||
});
|
||||
$grid->column('参与/签到/总人数')->display(function () {
|
||||
return $this->logs()->distinct('user_id')->count()
|
||||
.'/'.User::where('type', 0)->where('sign', 1)->count()
|
||||
.'/'.User::where('type', 0)->count();
|
||||
});
|
||||
$grid->column('created_at', '创建时间')->date('Y-m-d H:i:s');
|
||||
|
||||
return $grid;
|
||||
}
|
||||
|
||||
public function form()
|
||||
{
|
||||
$form = new Form(new Vote);
|
||||
|
||||
$form->text('title', '投票名称')->required();
|
||||
$form->select('type', '类型')->options([
|
||||
'equal' => '等额',
|
||||
'diff' => '差额',
|
||||
]);
|
||||
$form->number('max')->default(0)
|
||||
->help('差额选票,最大可选人数');
|
||||
|
||||
$form->switch('status', '状态');
|
||||
|
||||
$form->ueditor('rules', '投票规则');
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
public function detail($id)
|
||||
{
|
||||
$data = Item::where('vote_id', $id)->get();
|
||||
|
||||
$data = $data->sortByDesc(function ($item, $key) {
|
||||
return $item->logs()->sum('result');
|
||||
});
|
||||
|
||||
// table 2
|
||||
$headers = ['序号', '姓名', '得票', '投票用户', '签到用户', '通过率'];
|
||||
$rows = [];
|
||||
$i = 1;
|
||||
foreach ($data as $item) {
|
||||
$cent = $item->logs()->sum('result') / User::where('type', 0)->where('sign', 1)->count();
|
||||
|
||||
$color = $cent > 0.5 ? 'green' : 'red';
|
||||
|
||||
$cent = '<span style="color:'.$color.'">'.number_format($cent * 100, 2).'%</span>';
|
||||
$rows[] = [
|
||||
$i++,
|
||||
$item->name,
|
||||
$item->logs()->sum('result'),
|
||||
$item->vote->logs()->distinct('user_id')->count(),
|
||||
User::where('type', 0)->where('sign', 1)->count(),
|
||||
$cent,
|
||||
];
|
||||
}
|
||||
|
||||
return new Table($headers, $rows);
|
||||
|
||||
$grid = new Grid(new Item);
|
||||
|
||||
// $grid->model()->with([
|
||||
// 'logs' => function ($query) {
|
||||
// $query->orderBy(\DB::raw('SUM(`result`)'), 'desc');
|
||||
// },
|
||||
// ]);
|
||||
// $grid->model()->orderByRaw('updated_at - created_at DESC')
|
||||
|
||||
$grid->paginate(100);
|
||||
$grid->disableActions();
|
||||
$grid->disableCreateButton();
|
||||
$grid->model()->where('vote_id', $id);
|
||||
|
||||
$grid->column('name', '姓名');
|
||||
$grid->column('票数')->display(function () {
|
||||
return $this->logs()->sum('result');
|
||||
});
|
||||
$grid->column('结果')->display(function () {
|
||||
$cent = $this->logs()->sum('result') / User::where('type', 0)->where('sign', 1)->count();
|
||||
|
||||
$color = $cent > 0.51 ? 'green' : 'red';
|
||||
|
||||
return '<span style="color:'.$color.'">'.number_format($cent * 100, 2).'%</span>';
|
||||
});
|
||||
|
||||
$grid->column('签到用户')->display(function () {
|
||||
return User::where('type', 0)->where('sign', 1)->count();
|
||||
});
|
||||
// $grid->column('用户总数')->display(function () {
|
||||
// return User::where('type', 0)->count();
|
||||
// });
|
||||
|
||||
return $grid;
|
||||
}
|
||||
|
||||
}
|
||||
29
app/Admin/bootstrap.php
Normal file
29
app/Admin/bootstrap.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
use Encore\Admin\Form;
|
||||
use Encore\Admin\Grid;
|
||||
|
||||
Form::forget(['map', 'editor']);
|
||||
|
||||
Form::init(function (Form $form) {
|
||||
$form->disableEditingCheck();
|
||||
$form->disableCreatingCheck();
|
||||
$form->disableViewCheck();
|
||||
$form->tools(function (Form\Tools $tools) {
|
||||
$tools->disableDelete();
|
||||
$tools->disableView();
|
||||
$tools->disableList();
|
||||
});
|
||||
});
|
||||
|
||||
Grid::init(function (Grid $grid) {
|
||||
$grid->disableExport();
|
||||
$grid->actions(function (Grid\Displayers\Actions $actions) {
|
||||
$actions->disableView();
|
||||
});
|
||||
$grid->disableBatchActions();
|
||||
$grid->filter(function ($filter) {
|
||||
$filter->disableIdFilter();
|
||||
});
|
||||
// $grid->expandFilter();
|
||||
});
|
||||
19
app/Admin/routes.php
Normal file
19
app/Admin/routes.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
use Encore\Admin\Facades\Admin;
|
||||
use Illuminate\Routing\Router;
|
||||
|
||||
Admin::routes();
|
||||
|
||||
Route::group([
|
||||
'prefix' => config('admin.route.prefix'),
|
||||
'namespace' => config('admin.route.namespace'),
|
||||
'middleware' => config('admin.route.middleware'),
|
||||
], function (Router $router) {
|
||||
$router->get('/', 'HomeController@index')->name('admin.home');
|
||||
$router->resource('votes/{vote}/items', 'ItemController');
|
||||
$router->resource('votes', 'VoteController');
|
||||
$router->resource('users', 'UserController');
|
||||
$router->resource('articles', 'ArticleController');
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user