engineering/app/adminapi/logic/oa/FlowApproveLogic.php
2024-02-04 16:29:32 +08:00

279 lines
12 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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){
$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 = $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['create_user_name'] = $create_user['name'];
$data['copy_user'] = implode(',',$copy);
$data['step'] = $step;
$data['record'] = $record;
unset($data['flow_id'],$data['content_model'],$data['content_logic']);
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['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();
$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)->findOrEmpty();
if(!$check_record->isEmpty()){
self::setError('你已经审核过该条信息,无需重复审核');
return false;
}
//审批时间
$check_time = time();
Db::startTrans();
try {
//判断当前审批步骤类型
switch($current_step['flow_step']){
case 1://当前部门负责人审批
//判断当前审核步骤是否是最后一步;
//1、如果是最后一步如果当前用户审核通过则将该记录设置为审核通过如果当前用户未审核通过则则将该记录设置为审核不通过流程结束
//2、如果不是最后一步如果当前用户审核通过则将该记录设置为审核中流程进入下一步如果当前用户未审核通过则则将该记录设置为审核不通过流程结束
if(($current_step['sort']+1) == $step_count){
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('approve_id',$params['id'])->where('sort',$current_step['sort']+1)->update(['is_active'=>1]);
}
}
break;
case 2: //指定人或签
//判断当前记录除当前用户外,是否都审批了,
//1、如果都审批了则判断其他用户是否有审批通过的
//1-1、如果其他用户都没有审批通过则判断当前用户是否审批通过通过如果当前用户审批通过则判断当前审核步骤是否是最后一步
//1-1-1-1、如果是最后一步则该记录审批通过流程结束
//1-1-1-2、如果不是最后一步则该记录设置为审批中流程设置为下一步
//1-1-2、如果当前用户审批不通过则该记录审批不通过流程结束
//1-2、如果其他用户有一位审批通过则不管当前用户是否审批通过则判断当前审核步骤是否是最后一步如果是最后一步该记录设置为审核通过如果不是最后一步该记录设置为审核中流程设置为下一步
//2、如果没审批完则不管当前用户是否审批通过也不管当前审核步骤是否是最后一步该记录都设置为审批中不设置下一步操作
//获取当前记录的审核记录数
$check_record_count = FlowRecord::where('approve_id',$params['id'])->where('step_id',$current_step['id'])->count();
//获取但前记录的已经通过的记录
$check_record_pass = FlowRecord::where('approve_id',$params['id'])->where('step_id',$current_step['id'])->where('status',1)->findOrEmpty();
//判断当前记录除当前用户外,是否都审批了,
if(count($current_check_user)-1 == $check_record_count){
//判断有没有审核通过的
if($check_record_pass->isEmpty()){//没有审核通过的
if($params['check_status'] == 1){
if(($current_step['sort']+1) == $step_count){
$approve_check_status = 2;
}else{
$approve_check_status = 1;
//设置下一步
FlowStep::where('id',$current_step['id'])->update(['is_active'=>0]);
FlowStep::where('approve_id',$params['id'])->where('sort',$current_step['sort']+1)->update(['is_active'=>1]);
}
}else{
$approve_check_status = 3;
}
}else{//有审核通过的
if(($current_step['sort']+1) == $step_count){
$approve_check_status = 2;
}else{
$approve_check_status = 1;
//设置下一步
FlowStep::where('id',$current_step['id'])->update(['is_active'=>0]);
FlowStep::where('approve_id',$params['id'])->where('sort',$current_step['sort']+1)->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: //指定人会签
//判断当前记录除当前用户外,是否都审批了,
//1、如果都审批了则判断其他用户是否有未审批通过的
//1-1、如果其他用户都审批通过则判断当前用户是否审批通过通过如果当前用户审批通过则判断当前审核步骤是否是最后一步
//1-1-1-1、如果是最后一步则该记录审批通过流程结束
//1-1-1-2、如果不是最后一步则该记录设置为审批中流程设置为下一步
//1-1-2、如果当前用户审批不通过则该记录审批不通过流程结束
//1-2、如果其他用户有一位审批不通过则不管当前用户是否审批通过该记录设置为审核不通过流程结束
//2、如果没审批完则判断当前用户是否审批通过如果当前用户审批通过则将记录设置为审批中如果当前用户审批不通过则将记录设置为审批不通过
//获取当前记录的审核记录数
$check_record_count = FlowRecord::where('approve_id',$params['id'])->where('step_id',$current_step['id'])->count();
//获取但前记录的未通过的记录
$check_record_not_pass = FlowRecord::where('approve_id',$params['id'])->where('step_id',$current_step['id'])->where('status',2)->findOrEmpty();
//判断当前记录除当前用户外,是否都审批了,
if(count($current_check_user)-1 == $check_record_count){
//判断有没有审核不通过的
if($check_record_not_pass->isEmpty()){//没有审核不通过的
if($params['check_status'] == 1){
if(($current_step['sort']+1) == $step_count){
$approve_check_status = 2;
}else{
$approve_check_status = 1;
//设置下一步
FlowStep::where('id',$current_step['id'])->update(['is_active'=>0]);
FlowStep::where('approve_id',$params['id'])->where('sort',$current_step['sort']+1)->update(['is_active'=>1]);
}
}else{
$approve_check_status = 3;
}
}else{//有审核不通过的
$approve_check_status = 3;
}
}else{
//没审批完,则判断当前用户是否审批通过,如果当前用户审批通过,则将记录设置为审批中,如果当前用户审批不通过,则将记录设置为审批不通过
$approve_check_status = $params['check_status'] == 1 ? 1 : 3;
}
FlowApprove::where('id',$params['id'])->update([
'check_status' => $approve_check_status,
'update_time' => $check_time,
]);
break;
}
//添加审批数据记录
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;
}
}
}