更新项目管理接口

This commit is contained in:
yaooo 2023-10-31 10:12:16 +08:00
parent 09f6d58bed
commit 3dab0af11d
2 changed files with 109 additions and 104 deletions

View File

@ -197,88 +197,62 @@ class ProjectIndex extends ApiController
//编辑 //编辑
public function edit() public function edit()
{ {
$this->checkAuth();
$this->uid = JWT_UID;
$param = get_params(); $param = get_params();
$id = isset($param['id']) ? $param['id'] : 0; $id = isset($param['id']) ? $param['id'] : 0;
$detail = (new ProjectList())->detail($id); $detail = (new ProjectList())->detail($id);
if (request()->isPost()) { try {
if ($this->uid == $detail['admin_id'] || $this->uid == $detail['director_uid']) { validate(ProjectCheck::class)->scene('edit')->check($param);
if (isset($param['start_time'])) { } catch (ValidateException $e) {
$param['start_time'] = strtotime(urldecode($param['start_time'])); $this->apiError($e->getError());
if ($param['start_time'] >= $detail['end_time']) {
return to_assign(1, '开始时间不能大于计划结束时间');
}
}
if (isset($param['end_time'])) {
$param['end_time'] = strtotime(urldecode($param['end_time']));
if ($param['end_time'] <= $detail['start_time']) {
return to_assign(1, '计划结束时间不能小于开始时间');
}
}
try {
validate(ProjectCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$param['update_time'] = time();
$res = ProjectList::where('id', $param['id'])->strict(false)->field(true)->update($param);
if ($res) {
if(isset($param['director_uid'])){
$project_user=array(
'uid'=>$param['director_uid'],
'admin_id'=>$this->uid,
'project_id'=>$param['id'],
'create_time'=>time(),
'delete_time'=>0,
);
$has = Db::name('ProjectUser')->where(array('uid'=>$param['director_uid'],'project_id'=>$param['id']))->find();
if(empty($has)){
Db::name('ProjectUser')->strict(false)->field(true)->insert($project_user);
}
else{
Db::name('ProjectUser')->where(array('id'=>$has['id']))->strict(false)->field(true)->update($project_user);
}
}
add_log('edit', $param['id'], $param);
add_project_log($this->uid,'project',$param, $detail);
}
return to_assign();
} else {
return to_assign(1, '只有创建人或者负责人才有权限编辑');
}
} }
else{ if ($this->uid == $detail['admin_id'] || $this->uid == $detail['director_uid']) {
if (empty($detail)) { if (isset($param['start_time'])) {
return to_assign(1, '项目不存在'); $param['start_time'] = strtotime(urldecode($param['start_time']));
} else { if ($param['start_time'] >= $detail['end_time']) {
//项目阶段 return to_assign(1, '开始时间不能大于计划结束时间');
$step_array = Db::name('Step')
->field('s.*,a.name as check_name')
->alias('s')
->join('Admin a', 'a.id = s.flow_uid', 'LEFT')
->order('s.sort asc')
->where(array('s.action_id' => $id, 's.type' => 2, 's.delete_time' => 0))
->select()->toArray();
foreach ($step_array as $kk => &$vv) {
$vv['start_time'] = date('Y-m-d', $vv['start_time']);
$vv['end_time'] = date('Y-m-d', $vv['end_time']);
$flow_names = Db::name('Admin')->where([['id','in',$vv['flow_ids']]])->column('name');
$vv['flow_names'] = implode(',',$flow_names);
} }
View::assign('step_array', $step_array);
View::assign('detail', $detail);
View::assign('id', $id);
return view();
} }
if (isset($param['end_time'])) {
$param['end_time'] = strtotime(urldecode($param['end_time']));
if ($param['end_time'] <= $detail['start_time']) {
return to_assign(1, '计划结束时间不能小于开始时间');
}
}
$param['update_time'] = time();
$res = ProjectList::where('id', $param['id'])->strict(false)->field(true)->update($param);
if ($res) {
if(isset($param['director_uid'])){
$project_user=array(
'uid'=>$param['director_uid'],
'admin_id'=>$this->uid,
'project_id'=>$param['id'],
'create_time'=>time(),
'delete_time'=>0,
);
$has = Db::name('ProjectUser')->where(array('uid'=>$param['director_uid'],'project_id'=>$param['id']))->find();
if(empty($has)){
Db::name('ProjectUser')->strict(false)->field(true)->insert($project_user);
}
else{
Db::name('ProjectUser')->where(array('id'=>$has['id']))->strict(false)->field(true)->update($project_user);
}
}
add_log('edit', $param['id'], $param);
add_project_log($this->uid,'project',$param, $detail);
}
$this->apiSuccess('操作成功');
} else {
$this->apiError('只有创建人或者负责人才有权限修改项目');
} }
} }
//查看 //查看
public function view() public function view()
{ {
$this->checkAuth();
$param = get_params(); $param = get_params();
$id = isset($param['id']) ? $param['id'] : 0; $id = isset($param['id']) ? $param['id'] : 0;
$detail = (new ProjectList())->detail($id); $detail = (new ProjectList())->detail($id);
@ -381,34 +355,38 @@ class ProjectIndex extends ApiController
//删除 //删除
public function delete() public function delete()
{ {
if (request()->isDelete()) { $this->checkAuth();
$id = get_params("id"); $this->uid = JWT_UID;
$count_task = Db::name('ProjectTask')->where([['project_id', '=', $id], ['delete_time', '=', 0]])->count(); $id = get_params("id");
if ($count_task > 0) { $project = Db::name('project')->where([
return to_assign(1, "该项目下有关联的任务,无法删除"); 'id' => $id
} ])->findOrEmpty();
$detail = Db::name('Project')->where('id', $id)->find(); if (empty($project)) {
if ($detail['admin_id'] != $this->uid) { $this->apiError('项目不存在');
return to_assign(1, "你不是该项目的创建人,无权限删除");
}
if (Db::name('Project')->where('id', $id)->update(['delete_time' => time()]) !== false) {
$log_data = array(
'module' => 'project',
'field' => 'delete',
'action' => 'delete',
'project_id' => $detail['id'],
'admin_id' => $this->uid,
'old_content' => '',
'new_content' => $detail['name'],
'create_time' => time(),
);
Db::name('ProjectLog')->strict(false)->field(true)->insert($log_data);
return to_assign(0, "删除成功");
} else {
return to_assign(0, "删除失败");
}
} else {
return to_assign(1, "错误的请求");
} }
$count_task = Db::name('ProjectTask')->where([['project_id', '=', $id], ['delete_time', '=', 0]])->count();
if ($count_task > 0) {
$this->apiError('该项目下有关联的任务,无法删除');
}
$detail = Db::name('Project')->where('id', $id)->find();
if ($detail['admin_id'] != $this->uid) {
$this->apiError('你不是该项目的创建人,无权限删除');
}
if (Db::name('Project')->where('id', $id)->update(['delete_time' => time()]) !== false) {
$log_data = array(
'module' => 'project',
'field' => 'delete',
'action' => 'delete',
'project_id' => $detail['id'],
'admin_id' => $this->uid,
'old_content' => '',
'new_content' => $detail['name'],
'create_time' => time(),
);
Db::name('ProjectLog')->strict(false)->field(true)->insert($log_data);
$this->apiSuccess('删除成功');
} else {
$this->apiError('删除失败');
}
} }
} }

View File

@ -1,18 +1,27 @@
<?php <?php
namespace app\project\validate; namespace app\project\validate;
use think\Validate; use think\Validate;
use think\facade\Db;
class ProjectCheck extends Validate class ProjectCheck extends Validate
{ {
protected $rule = [ protected $rule = [
'name' => 'require|unique:project', 'name' => 'require|unique:project',
'code' => 'alphaNum|length:5,10|unique:project', 'start_time' => 'require',
'id' => 'require' 'end_time' => 'require',
'director_uid' => 'require',
'content' => 'require',
'code' => 'alphaNum|length:5,10|unique:project',
'id' => 'require|checkProject'
]; ];
protected $message = [ protected $message = [
'name.require' => '项目名称不能为空', 'name.require' => '项目名称不能为空',
'name.unique' => '同样的项目名称已经存在', 'name.unique' => '项目已存在',
'start_time.require' => '项目开始时间不能为空',
'end_time.require' => '项目结束时间不能为空',
'director_uid.require' => '项目负责人不能为空',
'content.require' => '项目简介不能为空',
'code.alphaNum' => '项目代码只能为5至10为字母和数字', 'code.alphaNum' => '项目代码只能为5至10为字母和数字',
'code.length' => '项目代码只能为5至10为字母和数字', 'code.length' => '项目代码只能为5至10为字母和数字',
'code.unique' => '同样的项目代码已经存在', 'code.unique' => '同样的项目代码已经存在',
@ -20,7 +29,25 @@ class ProjectCheck extends Validate
]; ];
protected $scene = [ protected $scene = [
'add' => ['name','code'], 'add' => ['name','start_time','end_time','director_uid','content','code'],
'edit' => ['id'] 'edit' => ['id', 'name','start_time','end_time','director_uid','content','code']
]; ];
public function checkProject($id, $rule, $data)
{
$project = Db::name('project')->where([
'id' => $id
])->findOrEmpty();
if (empty($project)) {
return '项目不存在';
}
$oProject = Db::name('project')->where([
'name' => $data['name']
])->findOrEmpty();
if (!empty($oProject) && ($oProject['id'] != $id)) {
return '项目已存在';
}
return true;
}
} }