224 lines
6.5 KiB
PHP
224 lines
6.5 KiB
PHP
<?php
|
||
|
||
declare (strict_types=1);
|
||
|
||
namespace app\controller\manager;
|
||
|
||
use app\model\ClockLog;
|
||
use Exception;
|
||
use app\model\Log;
|
||
use think\Collection;
|
||
use think\response\View;
|
||
use think\response\Json;
|
||
use think\db\exception\DbException;
|
||
use think\exception\ValidateException;
|
||
use think\db\exception\DataNotFoundException;
|
||
use think\db\exception\ModelNotFoundException;
|
||
|
||
class Clock extends Base
|
||
{
|
||
protected $noNeedLogin = ['index', 'add', 'edit', 'del', 'modify'];
|
||
|
||
/**
|
||
* 列表
|
||
*
|
||
* @throws Exception
|
||
*/
|
||
public function index()
|
||
{
|
||
$accountId = input('user_id/d', 0);
|
||
if ($this->request->isPost()) {
|
||
$params = input('searchParams/a');
|
||
$page = input('page/d', 1);
|
||
$size = input('size/d', 20);
|
||
|
||
$where = [];
|
||
if (!empty($params)) {
|
||
foreach ($params as $key => $param) {
|
||
$param = trim($param);
|
||
if ($key == 'keyword') {
|
||
$where[] = ['w.name|a.nickname|a.mobile|a.real_name|a2.nickname|a2.real_name', 'like', '%'.$param.'%'];
|
||
continue;
|
||
}
|
||
if ($param == '0' || !empty($param)) {
|
||
$where[] = ['cl.'.$key, '=', $param];
|
||
}
|
||
}
|
||
}
|
||
|
||
if ($accountId > 0) {
|
||
$where[] = ['cl.account_id', '=', $accountId];
|
||
}
|
||
|
||
$query = ClockLog::alias('cl')
|
||
->leftJoin('account a', 'a.id = cl.account_id')
|
||
->leftJoin('account a2', 'a2.id = cl.check_by')
|
||
->leftJoin('worksite w', 'w.id = cl.worksite_id')
|
||
->where($where);
|
||
$total = $query->count();
|
||
|
||
$res = [
|
||
'total' => $total,
|
||
'current' => $page ?: 1,
|
||
'size' => $size ?: 20,
|
||
'list' => [],
|
||
];
|
||
|
||
if ($total > 0) {
|
||
$typeText = ClockLog::typeText();
|
||
$statusText = ClockLog::statusText();
|
||
$res['list'] = $query->fieldRaw('cl.*,a.nickname,a.real_name,a.mobile,w.name as worksite_name,a2.nickname as check_nickname,a2.real_name as check_name')->page($page, $size)->order('cl.id', 'desc')->select();
|
||
$res['list']->each(function ($item) use ($typeText, $statusText) {
|
||
$item->type_text = $typeText[$item->type] ?? '其他';
|
||
$item->status_text = $statusText[$item->status] ?? '其他';
|
||
$item->account_text = $item->nickname.'-'.$item->real_name.'-'.$item->mobile;//用户信息
|
||
$item->check_text = $item->check_by ? $item->check_nickname.'-'.$item->check_name : '';//审核人信息
|
||
});
|
||
}
|
||
|
||
return $this->json(0, 'success', $res);
|
||
}
|
||
|
||
$this->data['userId'] = $accountId;
|
||
$this->data['worksiteList'] = \app\model\Worksite::list();
|
||
return $this->view();
|
||
}
|
||
|
||
/**
|
||
* 添加
|
||
*
|
||
* @return Json|View
|
||
*/
|
||
public function add()
|
||
{
|
||
if ($this->request->isPost()) {
|
||
try {
|
||
$input = input('post.');
|
||
if (!isset($input['name'])) {
|
||
return $this->json(4000, '参数错误');
|
||
}
|
||
ClockLog::create([
|
||
'name' => $input['name'] ?? '',
|
||
]);
|
||
|
||
return $this->json();
|
||
} catch (Exception $e) {
|
||
return $this->json(4001, '添加失败'.$e->getMessage());
|
||
}
|
||
}
|
||
|
||
return $this->view();
|
||
}
|
||
|
||
/**
|
||
* 编辑
|
||
*
|
||
* @return \think\response\Json|\think\response\View
|
||
*/
|
||
public function edit()
|
||
{
|
||
$id = input('id');
|
||
|
||
//通过ID查询
|
||
$item = ClockLog::where('id', (int) $id)->find();
|
||
|
||
if (empty($item)) {
|
||
return $this->json(4000, '没有相关记录!');
|
||
}
|
||
|
||
if ($this->request->isPost()) {
|
||
try {
|
||
$input = input('post.');
|
||
if (!isset($input['name'])) {
|
||
return $this->json(4000, '参数错误');
|
||
}
|
||
|
||
$item->save([
|
||
'name' => $input['name'] ?? '',
|
||
]);
|
||
return $this->json();
|
||
} catch (Exception $e) {
|
||
return $this->json(5000, $e->getMessage());
|
||
}
|
||
}
|
||
|
||
$this->data['item'] = $item;
|
||
$this->data['id'] = $id;
|
||
return $this->view();
|
||
}
|
||
|
||
/**
|
||
* 更新属性
|
||
*
|
||
* @throws ModelNotFoundException
|
||
* @throws DbException
|
||
* @throws DataNotFoundException
|
||
* @throws Exception
|
||
*/
|
||
public function modify()
|
||
{
|
||
if (!$this->request->isPost()) {
|
||
return $this->json(4000, '非法请求');
|
||
}
|
||
|
||
$item = input('post.');
|
||
$validate = $this->validateByApi($item, [
|
||
'field' => 'require',
|
||
'value' => 'require',
|
||
]);
|
||
|
||
if ($validate !== true) {
|
||
return $validate;
|
||
}
|
||
|
||
// 通过ID查询
|
||
if (!$info = ClockLog::where('id', (int) $item['id'])->find()) {
|
||
return $this->json(4001, '记录不存在');
|
||
}
|
||
|
||
$update = [$item['field'] => $item['value']];
|
||
|
||
try {
|
||
$info->save($update);
|
||
return $this->json();
|
||
} catch (ValidateException $e) {
|
||
return $this->json(4001, $e->getError());
|
||
} catch (Exception $e) {
|
||
return $this->json(5000, '修改失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除
|
||
*
|
||
* @return \think\response\Json
|
||
*/
|
||
public function del(): Json
|
||
{
|
||
if (!$this->request->isPost()) {
|
||
return $this->json(4000, '非法请求');
|
||
}
|
||
|
||
$ids = $this->request->param('ids/a', []);
|
||
if (empty($ids)) {
|
||
$ids[] = $this->request->param('id/d', 0);
|
||
$ids = array_filter($ids);
|
||
}
|
||
|
||
try {
|
||
if (count($ids)) {
|
||
//删除逻辑
|
||
if (\app\model\Account::whereIn('ClockLog', $ids)->count() > 0) {
|
||
return $this->json(4000, '所选岗位已分配给用户,请先移除后再删除');
|
||
}
|
||
ClockLog::whereIn('id', $ids)->delete();
|
||
|
||
Log::write(get_class(), 'del', '删除操作,涉及到的ID为:'.implode(',', $ids));
|
||
}
|
||
} catch (Exception $e) {
|
||
return $this->json(5000, $e->getMessage());
|
||
}
|
||
|
||
return $this->json();
|
||
}
|
||
} |