lihai-oa/app/api/controller/OaApprove.php

513 lines
16 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
/**
* @copyright Copyright (c) 2021 勾股工作室
* @license https://opensource.org/licenses/GPL-3.0
* @link https://www.gougucms.com
*/
declare (strict_types = 1);
namespace app\api\controller;
use app\api\ApiController;
use app\api\middleware\Auth;
use app\note\model\Note as NoteList;
use app\home\model\AdminLog;
use app\user\validate\AdminCheck;
use think\exception\ValidateException;
use think\facade\Db;
class OaApprove extends ApiController
{
protected $middleware = [
Auth::class => ['except' => []]
];
//获取审批应用
public function app()
{
$uid = JWT_UID;
$userInfo = Db::name('Admin')->where(['id' => $uid])->field(['id', 'username', 'name', 'email', 'mobile', 'sex', 'nickname', 'thumb', 'did', 'position_id', 'desc', 'entry_time'])->find();
$department = $userInfo['did'];
$map1 = [];
$map2 = [];
$map1[] = ['status', '=', 1];
$map1[] = ['department_ids', '=', ''];
$map2[] = ['status', '=', 1];
$map2[] = ['', 'exp', Db::raw("FIND_IN_SET('{$department}',department_ids)")];
$list = Db::name('FlowType')->whereOr([$map1,$map2])->fetchSql(false)->select()->toArray();
$this->apiSuccess('获取成功', $list);
}
//获取审批应用字段
public function app_field()
{
$this->uid = JWT_UID;
$loginAdmin = Db::name('Admin')->where(['id' => $this->uid])->find();
$this->did = $loginAdmin['did'];
$param = get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$type = isset($param['type']) ? $param['type'] : 0;
if($id > 0){
$detail = Db::name('Approve')->where('id',$id)->find();
$detail['start_time_a'] = date('Y-m-d',$detail['start_time']);
$detail['start_time_b'] = date('H:i',$detail['start_time']);
$detail['end_time_a'] = date('Y-m-d',$detail['end_time']);
$detail['end_time_b'] = date('H:i',$detail['end_time']);
$detail['detail_time'] = date('Y-m-d',$detail['detail_time']);
$detail['days'] = floor($detail['duration']*10/80);
$detail['hours'] = (($detail['duration']*10)%80)/10;
$type = $detail['type'];
if($detail['file_ids'] !=''){
$fileArray = Db::name('File')->where('id','in',$detail['file_ids'])->select();
$detail['fileArray'] = $fileArray;
}
}
// * 动态表单展示
$flows = get_cate_department_flows($type, $this->did);
$flowMap = [];
foreach ($flows as $item) {
$flowMap[$item['id']] = $item['name'];
}
//获取审批字段
$field = [
'detail_type' => [
'title' => '请假类型',
'type' => 'select',
'item' => [
1 => '事假',
2 => '年假',
3 => '调休假',
4 => '病假',
5 => '婚假',
6 => '丧假',
7 => '产假',
8 => '陪产假',
9 => '其他',
],
],
'start_time' => [
'title' => '开始时间',
'type' => 'input',
'item' => (object)[]
],
'end_time' => [
'title' => '结束时间',
'type' => 'input',
'item' => (object)[]
],
'duration' => [
'title' => '请假工时',
'type' => 'input',
'item' => (object)[]
],
'content' => [
'title' => '请假事由',
'type' => 'textarea',
'item' => (object)[]
],
'file_ids' => [
'title' => '附件',
'type' => 'input',
'item' => (object)[]
],
'flow_id' => [
'title' => '审批流程',
'type' => 'select',
'item' => $flowMap
],
'check_admin_ids' => [
'title' => '审核人ID',
'type' => 'select',
'item' => (object)[]
],
'check_admin_name' => [
'title' => '审核人',
'type' => 'select',
'item' => (object)[]
],
'copy_uids' => [
'title' => '抄送人ID',
'type' => 'select',
'item' => (object)[]
],
'copy_names' => [
'title' => '抄送人',
'type' => 'select',
'item' => (object)[]
],
];
$moban = Db::name('FlowType')->where('id',$type)->value('name');
$this->apiSuccess('获取成功', [
'type' => $type,
'moban' => $moban,
'field' => $field
]);
}
//添加新增/编辑审批
public function add()
{
$this->checkAuth();
$this->uid = JWT_UID;
$loginAdmin = Db::name('Admin')->where(['id' => $this->uid])->find();
$this->did = $loginAdmin['did'];
$param = get_params();
if (empty($param['flow_id'])) {
$this->apiError("审批类型id不能为空");
}
if (isset($param['detail_time'])) {
$param['detail_time'] = strtotime($param['detail_time']);
}
if (isset($param['start_time'])) {
$param['start_time'] = strtotime($param['start_time']);
}
if (isset($param['end_time'])) {
$param['end_time'] = strtotime($param['end_time']);
if ($param['end_time'] < $param['start_time']) {
$this->apiError('时间选择有误');
}
}
if (isset($param['start_time_a'])) {
$param['start_time'] = strtotime($param['start_time_a'] . '' . $param['start_time_b']);
}
if (isset($param['end_time_a'])) {
$param['end_time'] = strtotime($param['end_time_a'] . '' . $param['end_time_b']);
if ($param['end_time'] <= $param['start_time']) {
$this->apiError('结束时间需要大于开始时间');
}
}
$flow_list = Db::name('Flow')->where('id',$param['flow_id'])->value('flow_list');
$flow = unserialize($flow_list);
if (!empty($param['id']) && $param['id'] > 0) {
$param['update_time'] = time();
$param['check_status'] = 0;
$param['check_step_sort'] = 0;
try {
//删除原来的审核流程和审核记录
Db::name('FlowStep')->where(['action_id'=>$param['id'],'type'=>1,'delete_time'=>0])->update(['delete_time'=>time()]);
Db::name('FlowRecord')->where(['action_id'=>$param['id'],'type'=>1,'delete_time'=>0])->update(['delete_time'=>time()]);
if (!isset($param['check_admin_ids'])) {
if($flow[0]['flow_type'] == 1){
//部门负责人
$leader = get_department_leader($this->uid);
if($leader == 0){
$this->apiError('审批流程设置有问题当前部门负责人还未设置请联系HR或者管理员');
}
else{
$param['check_admin_ids'] = $leader;
}
}
else if($flow[0]['flow_type'] == 2){
//上级部门负责人
$leader = get_department_leader($this->uid,1);
if($leader == 0){
$this->apiError('审批流程设置有问题上级部门负责人还未设置请联系HR或者管理员');
}
else{
$param['check_admin_ids'] = $leader;
}
}
else{
$param['check_admin_ids'] = $flow[0]['flow_uids'];
}
Db::name('Approve')->strict(false)->field(true)->update($param);
foreach ($flow as $key => &$value){
$value['action_id'] = $param['id'];
$value['sort'] = $key;
$value['create_time'] = time();
}
$res = Db::name('FlowStep')->strict(false)->field(true)->insertAll($flow);
}
else{
Db::name('Approve')->strict(false)->field(true)->update($param);
$flow_step = array(
'action_id' => $param['id'],
'flow_uids' => $param['check_admin_ids'],
'create_time' => time()
);
$res = Db::name('FlowStep')->strict(false)->field(true)->insertGetId($flow_step);
}
//添加提交申请记录
$checkData=array(
'action_id' => $param['id'],
'check_user_id' => $this->uid,
'content' => '重新提交申请',
'check_time' => time(),
'create_time' => time()
);
$record_id = Db::name('FlowRecord')->strict(false)->field(true)->insertGetId($checkData);
add_log('edit', $param['id'], $param);
//发送消息通知
$msg=[
'from_uid'=>$this->uid,
'title' => Db::name('FlowType')->where('id',$param['type'])->value('title'),
'action_id'=>$param['id']
];
$users = $param['check_admin_ids'];
sendMessage($users,21,$msg);
} catch (\Exception $e) {
$this->apiError($e->getError());
}
} else {
$param['admin_id'] = $this->uid;
$param['department_id'] = $this->did;
$param['create_time'] = time();
try {
if (!isset($param['check_admin_ids'])) {
if($flow[0]['flow_type'] == 1){
//部门负责人
$leader = get_department_leader($this->uid);
if($leader == 0){
$this->apiError('当前部门负责人还未设置请联系HR或者管理员');
}
else{
$param['check_admin_ids'] = $leader;
}
}
else if($flow[0]['flow_type'] == 2){
//上级部门负责人
$leader = get_department_leader($this->uid, 1);
if($leader == 0){
$this->apiError('上级部门负责人还未设置请联系HR或者管理员');
}
else{
$param['check_admin_ids'] = $leader;
}
}
else{
$param['check_admin_ids'] = $flow[0]['flow_uids'];
}
$aid = Db::name('Approve')->strict(false)->field(true)->insertGetId($param);
foreach ($flow as $key => &$value){
$value['action_id'] = $aid;
$value['sort'] = $key;
$value['create_time'] = time();
}
$res = Db::name('FlowStep')->strict(false)->field(true)->insertAll($flow);
}
else{
$aid = Db::name('Approve')->strict(false)->field(true)->insertGetId($param);
$flow_step = array(
'action_id' => $aid,
'flow_uids' => $param['check_admin_ids'],
'create_time' => time()
);
$step_id = Db::name('FlowStep')->strict(false)->field(true)->insertGetId($flow_step);
}
//添加提交申请记录
$checkData=array(
'action_id' => $aid,
'check_user_id' => $this->uid,
'content' => '提交申请',
'check_time' => time(),
'create_time' => time()
);
$record_id = Db::name('FlowRecord')->strict(false)->field(true)->insertGetId($checkData);
add_log('add', $aid, $param);
//给审核人发送消息通知
$msg=[
'from_uid'=>$this->uid,
'title' => Db::name('FlowType')->where('id',$param['type'])->value('title'),
'action_id'=>$aid
];
$users = $param['check_admin_ids'];
sendMessage($users, 21, $msg);
} catch (\Exception $e) {
$this->apiError($e->getError());
}
}
$this->apiSuccess('操作成功');
}
//发起审批记录
public function index()
{
$this->checkAuth();
$this->uid = JWT_UID;
$param = get_params();
$where = [];
if (isset($param['status'])) {
if($param['status'] == 1){
$where[] = ['check_status','<',2];
}
if($param['status'] == 2){
$where[] = ['check_status','=',2];
}
if($param['status'] == 3){
$where[] = ['check_status','>',2];
}
}
$where[] = ['admin_id','=',$this->uid];
$rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
$list = Db::name('Approve')
->field('f.*,a.name,d.title as department_name,t.title as flow_type')
->alias('f')
->join('Admin a', 'a.id = f.admin_id', 'left')
->join('Department d', 'd.id = f.department_id', 'left')
->join('FlowType t', 't.id = f.type', 'left')
->where($where)
->order('f.id desc')
->paginate(['list_rows' => $rows, 'query' => $param])
->each(function($item, $key){
$item['create_time'] = date('Y-m-d H:i', $item['create_time']);
$item['check_user'] = '-';
if($item['check_status']<2 && !empty($item['check_admin_ids'])){
$check_user = Db::name('Admin')->where('id','in',$item['check_admin_ids'])->column('name');
$item['check_user'] = implode(',',$check_user);
}
return $item;
});
$this->apiSuccess('获取成功', $list);
}
//我处理的审批记录
public function list()
{
$this->checkAuth();
$this->uid = JWT_UID;
$param = get_params();
$status = isset($param['status'])?$param['status']:0;
$user_id = $this->uid;
//查询条件
$map1 = [];
$map2 = [];
$map1[] = ['', 'exp', Db::raw("FIND_IN_SET('{$user_id}',f.check_admin_ids)")];
$map2[] = ['', 'exp', Db::raw("FIND_IN_SET('{$user_id}',f.flow_admin_ids)")];
if (!empty($param['type'])) {
$map1[] = ['f.type', '=', $param['type']];
$map2[] = ['f.type', '=', $param['type']];
}
if (!empty($param['uid'])) {
$map1[] = ['f.admin_id', '=', $param['uid']];
$map2[] = ['f.admin_id', '=', $param['uid']];
}
//按时间检索
if (!empty($param['apply_time'])) {
$apply_time =explode('~', $param['apply_time']);
$map1[] = ['f.create_time', 'between', [strtotime(urldecode($apply_time[0])),strtotime(urldecode($apply_time[1]))]];
$map2[] = ['f.create_time', 'between', [strtotime(urldecode($apply_time[0])),strtotime(urldecode($apply_time[1]))]];
}
$rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
if($status == 0){
$list = Db::name('Approve')
->field('f.*,a.name,d.title as department_name,t.title as flow_type')
->alias('f')
->join('Admin a', 'a.id = f.admin_id', 'left')
->join('Department d', 'd.id = f.department_id', 'left')
->join('FlowType t', 't.id = f.type', 'left')
->whereOr([$map1,$map2])
->order('f.id desc')
->group('f.id')
->paginate(['list_rows' => $rows, 'query' => $param])
->each(function($item, $key){
$item['create_time'] = date('Y-m-d H:i', $item['create_time']);
$item['check_user'] = '-';
if($item['check_status']<2 && !empty($item['check_admin_ids'])){
$check_user = Db::name('Admin')->where('id','in',$item['check_admin_ids'])->column('name');
$item['check_user'] = implode(',',$check_user);
}
return $item;
});
}
if($status == 1){
$list = Db::name('Approve')
->field('f.*,a.name,d.title as department_name,t.title as flow_type')
->alias('f')
->join('Admin a', 'a.id = f.admin_id', 'left')
->join('Department d', 'd.id = f.department_id', 'left')
->join('FlowType t', 't.id = f.type', 'left')
->where($map1)
->order('f.id desc')
->group('f.id')
->paginate(['list_rows' => $rows, 'query' => $param])
->each(function($item, $key){
$item['create_time'] = date('Y-m-d H:i', $item['create_time']);
$item['check_user'] = '-';
if($item['check_status']<2 && !empty($item['check_admin_ids'])){
$check_user = Db::name('Admin')->where('id','in',$item['check_admin_ids'])->column('name');
$item['check_user'] = implode(',',$check_user);
}
return $item;
});
}
if($status == 2){
$list = Db::name('Approve')
->field('f.*,a.name,d.title as department_name,t.title as flow_type')
->alias('f')
->join('Admin a', 'a.id = f.admin_id', 'left')
->join('Department d', 'd.id = f.department_id', 'left')
->join('FlowType t', 't.id = f.type', 'left')
->where($map2)
->order('f.id desc')
->group('f.id')
->paginate(['list_rows' => $rows, 'query' => $param])
->each(function($item, $key){
$item['create_time'] = date('Y-m-d H:i', $item['create_time']);
$item['check_user'] = '-';
if($item['check_status']<2 && !empty($item['check_admin_ids'])){
$check_user = Db::name('Admin')->where('id','in',$item['check_admin_ids'])->column('name');
$item['check_user'] = implode(',',$check_user);
}
return $item;
});
}
$this->apiSuccess('获取成功', $list);
}
//抄送给我的审批记录
public function copy()
{
$this->checkAuth();
$this->uid = JWT_UID;
$param = get_params();
$user_id = $this->uid;
//查询条件
$map = [];
$map[] = ['f.check_status', '=', 2];
$map[] = ['', 'exp', Db::raw("FIND_IN_SET('{$user_id}',f.copy_uids)")];
if (!empty($param['type'])) {
$map[] = ['f.type', '=', $param['type']];
}
if (!empty($param['uid'])) {
$map[] = ['f.admin_id', '=', $param['uid']];
}
//按时间检索
if (!empty($param['apply_time'])) {
$apply_time =explode('~', $param['apply_time']);
$map[] = ['f.create_time', 'between', [strtotime(urldecode($apply_time[0])),strtotime(urldecode($apply_time[1]))]];
}
$rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
$list = Db::name('Approve')
->field('f.*,a.name,d.title as department_name,t.title as flow_type')
->alias('f')
->join('Admin a', 'a.id = f.admin_id', 'left')
->join('Department d', 'd.id = f.department_id', 'left')
->join('FlowType t', 't.id = f.type', 'left')
->where($map)
->order('f.id desc')
->group('f.id')
->paginate(['list_rows' => $rows, 'query' => $param])
->each(function($item, $key){
$item['create_time'] = date('Y-m-d H:i', $item['create_time']);
$item['check_user'] = '-';
if($item['check_status']<2 && !empty($item['check_admin_ids'])){
$check_user = Db::name('Admin')->where('id','in',$item['check_admin_ids'])->column('name');
$item['check_user'] = implode(',',$check_user);
}
return $item;
});
$this->apiSuccess('获取成功', $list);
}
}