You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
12 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\Admin;
use App\Exports\CommonExport;
use App\Helpers\ResponseCode;
use App\Models\CourseKeep;
use App\Models\User;
use Illuminate\Support\Carbon;
use Maatwebsite\Excel\Facades\Excel;
use Rap2hpoutre\FastExcel\FastExcel;
class CourseKeepController extends BaseController
{
/**
* 构造函数
*/
public function __construct()
{
parent::__construct(new CourseKeep());
}
/**
* @OA\Get(
* path="/api/admin/course-keeps/index",
* tags={"考勤管理"},
* summary="列表",
* description="",
* @OA\Parameter(name="is_export", in="query", @OA\Schema(type="string"), required=false, description="是否导出0否1是"),
* @OA\Parameter(name="export_fields", in="query", @OA\Schema(type="string"), required=false, description="需要导出的字段数组"),
* @OA\Parameter(name="filter", in="query", @OA\Schema(type="string"), required=false, description="查询条件。数组"),
* @OA\Parameter(name="show_relation", in="query", @OA\Schema(type="string"), required=false, description="需要输出的关联关系数组包括teachercourseSettingscoursePeriods"),
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
* @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), required=false, description="名字"),
* @OA\Parameter(name="file_name", in="query", @OA\Schema(type="string"), required=false, description="导出文件名"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function index()
{
$all = request()->all();
$list = $this->model->whereHas('user', function ($query) use ($all) {
if (isset($all['name'])) {
$query->where('name', 'like', '%' . $all['name'] . '%');
}
})->with(underlineToHump($all['show_relation'] ?? []))->where(function ($query) use ($all) {
if (isset($all['filter']) && !empty($all['filter'])) {
foreach ($all['filter'] as $condition) {
$key = $condition['key'] ?? null;
$op = $condition['op'] ?? null;
$value = $condition['value'] ?? null;
if (!isset($key) || !isset($op) || !isset($value)) {
continue;
}
// 等于
if ($op == 'eq') {
$query->where($key, $value);
}
// 不等于
if ($op == 'neq') {
$query->where($key, '!=', $value);
}
// 大于
if ($op == 'gt') {
$query->where($key, '>', $value);
}
// 大于等于
if ($op == 'egt') {
$query->where($key, '>=', $value);
}
// 小于
if ($op == 'lt') {
$query->where($key, '<', $value);
}
// 小于等于
if ($op == 'elt') {
$query->where($key, '<=', $value);
}
// 模糊搜索
if ($op == 'like') {
$query->where($key, 'like', '%' . $value . '%');
}
// 否定模糊搜索
if ($op == 'notlike') {
$query->where($key, 'not like', '%' . $value . '%');
}
// 范围搜索
if ($op == 'range') {
list($from, $to) = explode(',', $value);
if (empty($from) || empty($to)) {
continue;
}
$query->whereBetween($key, [$from, $to]);
}
}
}
})->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc');
if (isset($all['is_export']) && !empty($all['is_export'])) {
// 导出文件名字
$list = $list->limit(5000)->get()->toArray();
return Excel::download(new CommonExport($list, $all['export_fields'] ?? ''), $all['file_name'] ?? '' . date('YmdHis') . '.xlsx');
} else {
// 输出
$list = $list->paginate($all['page_size'] ?? 20);
}
return $this->success($list);
}
/**
* @OA\Get(
* path="/api/admin/course-keeps/show",
* tags={"考勤管理"},
* summary="详情",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
* @OA\Parameter(name="show_relation", in="query", @OA\Schema(type="string"), required=false, description="需要输出的关联关系数组,填写输出指定数据"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function show()
{
return parent::show();
}
/**
* @OA\Post(
* path="/api/admin/course-keeps/save",
* tags={"考勤管理"},
* summary="更新或新增",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), required=true, description="课程ID存在则更新不存在则新增"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="访问令牌"),
* @OA\Parameter(name="course_id", in="query", @OA\Schema(type="integer"), description="课程ID"),
* @OA\Parameter(name="course_content_id", in="query", @OA\Schema(type="integer"), description="排课ID"),
* @OA\Parameter(name="user_id", in="query", @OA\Schema(type="integer"), description="用户ID"),
* @OA\Parameter(name="status", in="query", @OA\Schema(type="integer"), description="考勤状态0缺勤1正常"),
* @OA\Parameter(name="date", in="query", @OA\Schema(type="string", format="date"), description="考勤日期"),
* @OA\Parameter(name="time", in="query", @OA\Schema(type="string", format="date-time"), description="考勤时间"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function save()
{
return parent::save();
}
/**
* @OA\Get(
* path="/api/admin/course-keeps/destroy",
* tags={"考勤管理"},
* summary="删除",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function destroy()
{
return parent::destroy();
}
/**
* @OA\Post(
* path="/api/admin/course_keeps/excel-show",
* tags={"考勤管理"},
* summary="导入预览",
* description="",
* @OA\Parameter(name="data", in="query", @OA\Schema(type="string"), required=true, description="里面包含course_id"),
* @OA\Parameter(name="file", in="query", @OA\Schema(type="string"), required=true, description="文件。包含字段:学员名字,日期,考勤时间,状态"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function excelShow()
{
$file = \request()->file('file');
$data = \request('data');
//判断文件是否有效
if (!(request()->hasFile('file') && $file->isValid())) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '文件不存在或无效']);
}
//获取文件大小
$img_size = floor($file->getSize() / 1024);
if ($img_size >= 10 * 1024) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '文件必须小于10M']);
}
//过滤文件后缀
$ext = $file->getClientOriginalExtension();
if (!in_array($ext, ['xls', 'xlsx', 'csv'])) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '仅支持xls/xlsx/csv格式']);
}
$tempFile = $file->getRealPath();
$dataArray = (new FastExcel)->import($tempFile)->toArray();
if (!isset($data['course_id'])) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '课程id不存在']);
}
// 获取所有key
$keyList = array_keys($dataArray[0]);
if (!in_array('学员名字', $keyList)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '学员名字字段不存在']);
}
if (!in_array('日期', $keyList)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '日期字段不存在']);
}
// if (!in_array('考勤时间', $keyList)) {
// return $this->fail([ResponseCode::ERROR_BUSINESS, '考勤时间字段不存在']);
// }
if (!in_array('状态', $keyList)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '状态字段不存在']);
}
$list = [];
$statusList = array_flip(CourseKeep::$intToString['status']);
foreach ($dataArray as $value) {
$userId = User::where('name', $value['学员名字'])->value('id');
if (empty($userId)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, "学员名字{$value['学员名字']}不存在"]);
};
$temp = [
'user_name' => $value['学员名字'],
'user_id' => User::where('name', $value['学员名字'])->value('id'),
'date' => Carbon::parse($value['日期'])->toDateString(),
'time' => $value['考勤时间'] ?? null,
'status' => $statusList[$value['状态']]
];
$list[] = array_merge($temp, $data);
}
return $this->success($list);
}
/**
* @OA\Post(
* path="/api/admin/course-keeps/import",
* tags={"考勤管理"},
* summary="导入",
* description="",
* @OA\Parameter(name="data", in="query", @OA\Schema(type="string"), required=true, description="导入分析获取到的二维数组"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function import()
{
return parent::import();
}
}