Files
projectmanager/app/plan/controller/Index.php
T
2025-06-25 10:53:11 +08:00

255 lines
9.2 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2023-2024 美天智能科技
* @link http://www.meteteme.com
* @author 李志强
*/
declare(strict_types=1);
namespace app\plan\controller;
use app\base\BaseController;
use app\model\Plan as PlanList;
use app\model\StatusMapping as StatusMappingList;
use app\model\ProjectCategory as ProjectCategoryList;
use app\plan\validate\PlanCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
class Index extends BaseController
{
public function index()
{
if (request()->isAjax()) {
$param = get_params();
$rows = empty($param['limit']) ? get_md_contentconfig('app.page_size') : $param['limit'];
$list = PlanList::withoutField('')
->where('delete_time', null)
->order('id desc')
->paginate($rows, false, ['query' => $param])
->each(function ($item, $key) {
$bi = Db::name('Plan')->where(['id' => $item->id])->find();
$item->name = $bi['name'];
$item->cate = Db::name('project_category')->where('id', $bi['cate'])->value('name');
$item->important = $bi['important'];
$item->status = Db::name('status_mapping')->where('id', $bi['status'])->value('description');
$item->status_id = $bi['status'];
$item->price = $bi['price'];
$item->responsible = $bi['responsible'];
$item->year = $bi['year'];
$item->start_time = $bi['start_time'];
$item->end_time = $bi['end_time'];
$item->schedule = $bi['schedule'];
$item->remark = $bi['remark'];
$item->create_time = $bi['create_time'];
$item->update_time = $bi['update_time'];
$item->delete_time = $bi['delete_time'];
})
->filter(function ($item) {
return $item->delete_time === null;
});
return table_assign(0, '', $list);
} else {
return view();
}
}
//添加
public function add()
{
$param = get_params();
if (isset($param['start_time']) && isset($param['end_time'])) {
$param['start_time'] = strtotime($param['start_time']);
$param['end_time'] = strtotime($param['end_time']);
}
if (request()->isPost()) {
if (!empty($param['id']) && $param['id'] > 0) {
$plan = (new PlanList())->detail($param['id']);
try {
validate(PlanCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$param['update_time'] = time();
$res = PlanList::where('id', $param['id'])->strict(false)->field(true)->update($param);
if ($res) {
add_log('edit', $param['id'], $param, $plan);
}
return to_assign();
} else {
try {
validate(PlanCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$exist = PlanList::where('name', $param['name'])
->where('id', '<>', $param['id'])->find();
if ($exist) {
return to_assign(1, '该信息已存在');
}
$param['create_time'] = date('Y-m-d H:i:s', time());
$param['admin_id'] = $this->uid;
$sid = PlanList::strict(false)->field(true)->insertGetId($param);
if ($sid) {
add_log('add', $sid, $param);
$log_data = array(
'module' => 'plans',
// 'id' => $sid,
'new_content' => $param['name'],
'field' => 'new',
'action' => 'add',
'admin_id' => $this->uid,
'create_time' => time(),
);
Db::name('Log')->strict(false)->field(true)->insert($log_data);
}
return to_assign(2, '添加成功!');
}
} else {
$id = isset($param['id']) ? $param['id'] : 0;
if ($id > 0) {
$detail = (new PlanList())->detail($id);
if (empty($detail)) {
return to_assign(1, '1.信息不存在');
}
View::assign('detail', $detail);
}
View::assign('id', $id);
return view();
}
}
//查看
public function view()
{
$param = get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$detail = (new PlanList())->detail($id);
// print_r($id);
if (empty($detail)) {
return to_assign(1, '2.信息不存在');
} else {
View::assign('detail', $detail);
// print_r($detail);
return view();
}
}
//删除
public function delete()
{
$request = request();
if ($request->isDelete() || $request->isAjax()) {
$id = get_params("id");
$detail = Db::name('Plan')->where('id', $id)->find();
if (Db::name('Plan')->where('id', $id)->update(['delete_time' => date('Y-m-d H:i:s')]) !== false) {
$log_data = array(
'module' => 'plan',
'field' => 'delete',
'action' => 'delete',
'admin_id' => $this->uid,
'old_content' => '',
'new_content' => $detail['name'],
'create_time' => time(),
);
Db::name('Log')->strict(false)->field(true)->insert($log_data);
return to_assign(0, "删除成功");
} else {
return to_assign(0, "删除失败");
}
} else {
return to_assign(1, "错误的请求");
}
}
//批量删除
public function batch_delete()
{
$request = request();
if ($request->isDelete() || $request->isAjax()) {
$ids = get_params("ids");
foreach ($ids as $id) {
$detail = Db::name('Plan')->where('id', $id)->find();
if (Db::name('Plan')->where('id', $id)->update(['delete_time' => date('Y-m-d H:i:s')]) !== false) {
$log_data = array(
'module' => 'plan',
'field' => 'delete',
'action' => 'delete',
'admin_id' => $this->uid,
'old_content' => '',
'new_content' => $detail['name'],
'create_time' => time(),
);
Db::name('Log')->strict(false)->field(true)->insert($log_data);
}
}
return to_assign(0, "批量删除成功");
} else {
return to_assign(1, "错误的请求");
}
}
// 获取计划年份
public function getplanyear()
{
$years = PlanList::distinct(true)->column('year');
return json(["data" => $years]);
}
//获取项目类型
public function getprojectcate()
{
$pageNumber = $_GET['page'] ?? 1; // 获取当前页码,默认为第1页
$pageSize = 10; // 每页显示的数据条数
$totalRecords = ProjectCategoryList::where('delete_time', null)->count(); // 获取总记录数
$totalPages = ceil($totalRecords / $pageSize); // 计算总页数
$offset = ($pageNumber - 1) * $pageSize; // 计算偏移量
$cates = ProjectCategoryList::where('delete_time', null)->limit($offset, $pageSize)->order('id', 'desc')->column('name', 'id');
$formattedCates = [];
foreach ($cates as $id => $cate) {
$formattedCates[] = ["id" => $id, "cate" => $cate];
}
return json(["code" => 0, "count" => $totalRecords, "data" => $formattedCates, "pages" => $totalPages]);
}
// 根据前端筛选条件筛选数据并返回结果
public function getsearch()
{
$cate = input('cate');
$year = input('year');
$important = input('important');
$query = PlanList::where(function ($query) use ($cate, $year, $important) {
if (!empty ($cate)) {
$query->where('cate', $cate);
}
if (!empty ($year)) {
$query->where('year', $year);
}
if (!empty ($important)) {
$query->where('important', $important);
}
})->find();
if ($query) {
return json(["code" => 0, "data" => $query]);
} else {
return json(["code" => 0, "message" => "未找到匹配的数据"]);
}
}
}