264 lines
9.4 KiB
PHP
264 lines
9.4 KiB
PHP
<?php
|
|
namespace app\adminapi\logic\oa;
|
|
|
|
use app\common\model\auth\Admin;
|
|
use app\common\model\oa\Flow;
|
|
use app\common\model\oa\FlowApprove;
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\model\oa\FlowRecord;
|
|
use app\common\model\oa\FlowStep;
|
|
use think\facade\Db;
|
|
|
|
|
|
/**
|
|
* 日常审批逻辑
|
|
* Class FlowApproveLogic
|
|
* @package app\adminapi\logic\oa
|
|
*/
|
|
class FlowApproveLogic extends BaseLogic
|
|
{
|
|
/**
|
|
* @notes 获取日常审批详情
|
|
* @param $params
|
|
* @return array
|
|
* @author likeadmin
|
|
* @date 2024/02/01 11:26
|
|
*/
|
|
public static function detail($params): array
|
|
{
|
|
$data = FlowApprove::field('id,title,flow_id,content_id,create_user,check_status')->findOrEmpty($params['id']);
|
|
if($data->isEmpty()) return [];
|
|
//获取创建人信息
|
|
$create_user = Admin::field('name')->where('id',$data['create_user'])->findOrEmpty();
|
|
//获取审批流程信息
|
|
$flow = Flow::field('name,copy_uids')->where('id',$data['flow_id'])-> findOrEmpty();
|
|
//获取抄送人信息
|
|
$copy = Admin::where('id','in',$flow['copy_uids'])->column('name');
|
|
//当前审核人
|
|
$current_check_user = '';
|
|
$current_check_user_ids = '';
|
|
//获取审核步骤
|
|
$step = FlowStep::field('flow_step,flow_user,is_active')->where('approve_id',$data['id'])->order('sort asc')->select()->each(function($item)use($create_user,&$current_check_user,&$current_check_user_ids){
|
|
$item['flow_step_text'] = $item->flow_step_text;
|
|
if($item['flow_step'] == 0){
|
|
$item['flow_step_text'] = $create_user['name'].'创建';
|
|
$item['flow_user_name'] = '';
|
|
}else{
|
|
$item['flow_step_text'] = $item->flow_step_text;
|
|
$flow_user = Admin::where('id','in',$item['flow_user'])->column('name');
|
|
$item['flow_user_name'] = implode(',',$flow_user);
|
|
}
|
|
if($item['is_active'] == 1){
|
|
$current_check_user = $item['flow_user_name'];
|
|
$current_check_user_ids = explode(',',$item['flow_user']);
|
|
}
|
|
})->toArray();
|
|
//获取审批记录
|
|
$record = FlowRecord::field('title,content,check_time,status')->where('approve_id',$data['id'])->select()->each(function($item){
|
|
$item['status_text'] = $item->status_text;
|
|
})->toArray();
|
|
$data['flow_name'] = $flow['name'];
|
|
$data['check_status_text'] = $data->check_status_text;
|
|
$data['current_check_user'] = $current_check_user;
|
|
$data['current_check_user_ids'] = $current_check_user_ids;
|
|
$data['create_user_name'] = $create_user['name'];
|
|
$data['copy_user'] = implode(',',$copy);
|
|
$data['step'] = $step;
|
|
$data['record'] = $record;
|
|
unset($data['flow_id']);
|
|
return $data->toArray();
|
|
}
|
|
|
|
//撤销申请
|
|
public static function revoke(array $params,$admin_id): bool
|
|
{
|
|
$approve_data = FlowApprove::where('id',$params['id'])->findOrEmpty();
|
|
if($approve_data->isEmpty()){
|
|
self::setError('审批信息不存在');
|
|
return false;
|
|
}
|
|
if($approve_data['check_status'] != 0){
|
|
self::setError('审批信息状态不是待审核状态,不可撤回');
|
|
return false;
|
|
}
|
|
if($approve_data['create_user'] != $admin_id){
|
|
self::setError('当前登录用户不是该审批信息的创建用户');
|
|
return false;
|
|
}
|
|
$createUser = Admin::field('name')->where('id',$approve_data['create_user'])->findOrEmpty();
|
|
Db::startTrans();
|
|
try {
|
|
$revoke_time = time();
|
|
FlowApprove::where('id',$params['id'])->update([
|
|
'check_status' => 4,
|
|
'update_time' => $revoke_time,
|
|
]);
|
|
//添加审批数据记录
|
|
FlowRecord::create([
|
|
'approve_id' => $params['id'],
|
|
'step_id' => 0,
|
|
'check_user_id' => 0,
|
|
'check_time' => $revoke_time,
|
|
'status' => 3,
|
|
'title' => $createUser['name'].'撤销了此申请',
|
|
'content' => $params['revoke_reason'],
|
|
'is_invalid' => 0
|
|
]);
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//审核申请
|
|
public static function check($params,$admin_id): bool{
|
|
$str = $params['check_status'] == 1 ? '通过' : '拒绝';
|
|
$check_user = Admin::field('name')->where('id',$admin_id)->findOrEmpty();
|
|
$approve_data = FlowApprove::where('id',$params['id'])->findOrEmpty();
|
|
if($approve_data->isEmpty()){
|
|
self::setError('当前审批信息不存在');
|
|
return false;
|
|
}
|
|
if($approve_data['check_status'] == 2 || $approve_data['check_status'] == 3){
|
|
self::setError('当前审批信息已审核');
|
|
return false;
|
|
}
|
|
if($approve_data['check_status'] == 4){
|
|
self::setError('当前审批信息已撤销');
|
|
return false;
|
|
}
|
|
//获取当前审批信息的审批步骤
|
|
$current_step = FlowStep::where('approve_id',$params['id'])->where('is_active',1)->findOrEmpty();
|
|
//获取下一步步骤
|
|
$next_step = FlowStep::where('approve_id',$params['id'])->where('sort',$current_step['sort']+1)->findOrEmpty();
|
|
$current_check_user = explode(',',$current_step['flow_user']);
|
|
//获取当前审批信息的步骤总数
|
|
$step_count = FlowStep::where('approve_id',$params['id'])->count();
|
|
if(!in_array($admin_id,$current_check_user)){
|
|
self::setError('当前登录用户不是此审批信息的审核人');
|
|
return false;
|
|
}
|
|
//判断当前用户是否审核过该条记录
|
|
$check_record = FlowRecord::where('approve_id',$params['id'])->where('step_id',$current_step['id'])->where('check_user_id',$admin_id)->where('is_invalid',0)->findOrEmpty();
|
|
if(!$check_record->isEmpty()){
|
|
self::setError('你已经审核过该条信息,无需重复审核');
|
|
return false;
|
|
}
|
|
//审批时间
|
|
$check_time = time();
|
|
//获取审批流程信息
|
|
$flow_data = Flow::where('id',$approve_data['flow_id'])->findOrEmpty();
|
|
Db::startTrans();
|
|
try {
|
|
if($flow_data['check_type'] == 1){
|
|
//判断当前审批步骤类型
|
|
switch($current_step['flow_step']){
|
|
case 1://当前部门负责人审批
|
|
if($next_step->isEmpty()){
|
|
FlowApprove::where('id',$params['id'])->update([
|
|
'check_status' => $params['check_status'] == 1 ? 2 : 3,
|
|
'update_time' => $check_time,
|
|
]);
|
|
}else{
|
|
FlowApprove::where('id',$params['id'])->update([
|
|
'check_status' => $params['check_status'] == 1 ? 1 : 3,
|
|
'update_time' => $check_time,
|
|
]);
|
|
//设置下一步
|
|
if($params['check_status'] == 1){
|
|
FlowStep::where('id',$current_step['id'])->update(['is_active'=>0]);
|
|
FlowStep::where('id',$next_step['id'])->update(['is_active'=>1]);
|
|
}
|
|
}
|
|
break;
|
|
case 2: //指定人或签
|
|
//判断当前审核状态
|
|
if($params['check_status'] == 1){
|
|
$approve_check_status = $next_step->isEmpty() ? 2 : 1;
|
|
if(!$next_step->isEmpty()){
|
|
FlowStep::where('id',$current_step['id'])->update(['is_active'=>0]);
|
|
FlowStep::where('id',$next_step['id'])->update(['is_active'=>1]);
|
|
}
|
|
}else{
|
|
//获取当前记录的审核记录数
|
|
$check_record_count = FlowRecord::where('approve_id',$params['id'])->where('step_id',$current_step['id'])->where('is_invalid',0)->where('status','<>',0)->count();
|
|
//判断当前记录除当前用户外,是否都审批了
|
|
if(count($current_check_user)-1 == $check_record_count){
|
|
$approve_check_status = 3;
|
|
if(!$next_step->isEmpty()){
|
|
FlowStep::where('id',$current_step['id'])->update(['is_active'=>0]);
|
|
FlowStep::where('id',$next_step['id'])->update(['is_active'=>1]);
|
|
}
|
|
}else{
|
|
$approve_check_status = 1;
|
|
}
|
|
}
|
|
FlowApprove::where('id',$params['id'])->update([
|
|
'check_status' => $approve_check_status,
|
|
'update_time' => $check_time,
|
|
]);
|
|
break;
|
|
case 3: //指定人会签
|
|
if($params['check_status'] == 2){
|
|
$approve_check_status = 3;
|
|
}else{
|
|
//获取当前记录的审核记录数
|
|
$check_record_count = FlowRecord::where('approve_id',$params['id'])->where('step_id',$current_step['id'])->where('is_invalid',0)->where('status','<>',0)->count();
|
|
//判断当前记录除当前用户外,是否都审批了,
|
|
if(count($current_check_user)-1 == $check_record_count){
|
|
$approve_check_status = 2;
|
|
if(!$next_step->isEmpty()){
|
|
FlowStep::where('id',$current_step['id'])->update(['is_active'=>0]);
|
|
FlowStep::where('id',$next_step['id'])->update(['is_active'=>1]);
|
|
}
|
|
}else{
|
|
$approve_check_status = 1;
|
|
}
|
|
}
|
|
FlowApprove::where('id',$params['id'])->update([
|
|
'check_status' => $approve_check_status,
|
|
'update_time' => $check_time,
|
|
]);
|
|
break;
|
|
}
|
|
}else{
|
|
if($next_step->isEmpty()){
|
|
FlowApprove::where('id',$params['id'])->update([
|
|
'check_status' => $params['check_status'] == 1 ? 2 : 3,
|
|
'update_time' => $check_time,
|
|
]);
|
|
}else{
|
|
FlowApprove::where('id',$params['id'])->update([
|
|
'check_status' => $params['check_status'] == 1 ? 1 : 3,
|
|
'update_time' => $check_time,
|
|
]);
|
|
//设置下一步
|
|
if($params['check_status'] == 1){
|
|
FlowStep::where('id',$current_step['id'])->update(['is_active'=>0]);
|
|
FlowStep::where('id',$next_step['id'])->update(['is_active'=>1]);
|
|
}
|
|
}
|
|
}
|
|
//添加审批数据记录
|
|
FlowRecord::create([
|
|
'approve_id' => $params['id'],
|
|
'step_id' => $current_step['id'],
|
|
'check_user_id' => $admin_id,
|
|
'check_time' => $check_time,
|
|
'status' => $params['check_status'],
|
|
'title' => $check_user['name'].$str.'了此申请',
|
|
'content' => $params['check_reason'],
|
|
'is_invalid' => 0
|
|
]);
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
} |