This commit is contained in:
2022-05-04 15:41:02 +08:00
commit c76a1850a1
766 changed files with 201246 additions and 0 deletions

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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();
}
}
}

View 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;
}
}