123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- namespace app\common\service;
- use app\common\constant\CommonConstant;
- use app\common\model\Approve;
- use app\common\model\ApproveInfo;
- use app\common\model\User;
- use think\Db;
- use think\Exception;
- /**
- * 审批服务类
- */
- class ApproveService
- {
- /**
- * 审批待处理统计
- *
- * @param mixed $user 用户信息
- **/
- public static function get_count(User $user)
- {
- $get_module_list = CommonConstant::get_module_list();
- $modules = array_keys($get_module_list);
- $userid = $user['userid'];
- $ids = Approve::where('status', CommonConstant::STATUS_2)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->where('approve_user', $userid)
- ->column('info_id');
- $object = [];
- if ($ids) {
- $list = ApproveInfo::field('module,count(id) as number')
- ->where('id', 'in', $ids)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->where('module', 'in', $modules)
- ->where('status', CommonConstant::STATUS_2)
- ->group('module')
- ->select();
- $object = $list ? array_column($list->toArray(), null, 'module') : [];
- }
- $data = [];
- foreach ($get_module_list as $module => $value) {
- $module_text = $value;
- $number = 0;
- if (array_key_exists($module, $object)) {
- $number = $object[$module]['number'];
- }
- $params = compact('module', "module_text", 'number');
- $data[] = $params;
- }
- return $data;
- }
- /**
- * 审批列表
- *
- * @param integer $status 处理状态
- * @param integer $module 模块类型
- * @param string $start_time 申请开始时间
- * @param string $end_time 申请结束时间
- * @param string $search 搜索
- * @param integer $offset 起始位置
- * @param integer $length 查询数量
- * @param mixed $user 用户信息
- **/
- public static function get_list($status, $module, $start_time, $end_time, $search, $offset, $length, $user)
- {
- if (!in_array($status, [1, 2, 3])) {
- return [];
- }
- if ($module && !array_key_exists($module, CommonConstant::get_module_list())) {
- return [];
- }
- $userids = $search && !is_numeric($search) ? User::where('name', 'like', '%' . $search . '%')->column('userid') : [];
- $userid = $user['userid'];
- $aliasName = 'approve.';
- $jobName = 'approveInfo.';
- $field = 'id as approve_id,info_id,status,create_at';
- $joinField = 'module,user_id,order_no,apply_date,reason,type,desc,start_time,end_time';
- $field = self::getAliasField($field, $aliasName);
- $joinField = self::getAliasField($joinField, $jobName);
- $field = implode(',', array_merge($field, $joinField));
- $list = Approve::alias('approve')
- ->field($field)
- ->when($status, function ($query) use ($aliasName, $status) {
- if ($status == 1) {
- $query->where($aliasName . 'status', 'in', [CommonConstant::STATUS_1, CommonConstant::STATUS_2]);
- }
- if ($status == 2) {
- $query->where($aliasName . 'status', 'in', [CommonConstant::STATUS_3, CommonConstant::STATUS_4]);
- }
- })
- ->where($aliasName . 'is_deleted', CommonConstant::IS_DELETED_0)
- ->where($aliasName . 'approve_user', $userid)
- ->where($jobName . 'is_deleted', CommonConstant::IS_DELETED_0)
- ->when($module, function ($query) use ($jobName, $module) {
- $query->where($jobName . 'module', 'in', $module);
- })
- ->when(!empty($start_time) && !empty($end_time), function ($query) use ($aliasName, $start_time, $end_time) {
- $query->where($aliasName . 'create_at', 'BETWEEN', [$start_time, $end_time]);
- })
- ->when($userids, function ($query) use ($jobName, $userids) {
- $query->where($jobName . 'user_id', 'in', $userids);
- })
- ->when($search, function ($query) use ($search, $userids) {
- if (!$userids) {
- if (!is_numeric($search)) {
- $query->where('order_no|reason', 'like', '%' . $search . '%');
- } else {
- $query->where('order_no', 'like', '%' . $search . '%');
- }
- }
- })
- ->join('__APPROVE_INFO__ approveInfo', 'approve.info_id = approveInfo.id', 'INNER');
- if ($status == 1) {
- // 待处理(待审批 审批中) 才关联 审批人信息
- $list->with(['approveInfoUser' => function ($query) {
- $query->field('userid,name');
- }, 'approveOne' => function ($query) {
- $query->field('id,info_id,status,approve_user')
- ->where('status', CommonConstant::STATUS_2)
- ->with(['user' => function ($query) {
- $query->field('userid,name');
- }]);
- }]);
- } else {
- $list->with(['approveInfoUser' => function ($query) {
- $query->field('userid,name');
- }]);
- }
- $list = $list->limit($offset, $length)
- ->order('approve_id desc')
- ->select();
- // $info_ids = [];
- // foreach ($list as $value){
- // if($value['status'] == CommonConstant::STATUS_1) {
- // $info_ids[] = $value['info_id'];
- // }
- // }
- // if($info_ids){
- // $approve_one_list = Approve::field('id,info_id,status,approve_user')
- // ->where('info_id','in',$info_ids)
- // ->where('status',CommonConstant::STATUS_2)
- // ->where('is_deleted', CommonConstant::IS_DELETED_0)
- // ->with(['user' => function ($query) {
- // $query->field('userid,name');
- // }])
- // ->select();
- // $approve_one_data = array_column($approve_one_list, null, 'info_id');
- // foreach ($list as $val){
- // if(array_key_exists($val['info_id'],$approve_one_data)){
- // $val['approve_one'] = $approve_one_data[$val['info_id']];
- // }
- // }
- // }
- return $list;
- }
- /**
- * 详情
- *
- * @param integer $id 审批ID
- * @param mixed $user 用户信息
- * @param string $type 类型:detail=详情,info=信息
- **/
- public static function get_detail($id, $user, $type)
- {
- $userid = $user['userid'];
- $info = Approve::field('id,info_id,status')
- ->where('approve_user', $userid)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->find($id);
- $data = [];
- if ($info) {
- $data = ApproveInfoService::get_detail($info['info_id'], '', $type);
- $data['approve_id'] = $id;
- $data['approve_status'] = $info['status'];
- }
- return $data;
- }
- /**
- * 操作方法
- *
- * @param integer $id 审批ID
- * @param array $params 数组
- * @param mixed $user 用户信息
- * @param string $type 类型:audit=审批,edit=修改
- **/
- public static function make($id, $params, $user, $type)
- {
- $userid = $user['userid'];
- $info = Approve::field('id,info_id,status,approve_flow,approve_time')
- ->where('approve_user', $userid)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->with([
- 'approveInfo' => function ($query) {
- $query->field('user_id,is_deleted', true);
- }
- ])
- ->find($id);
- if (!$info) {
- throw new Exception('审批记录不存在或已删除');
- }
- if ($info->status != CommonConstant::STATUS_2) {
- throw new Exception('非待处理状态无法操作');
- }
- $approve_info = $info->approve_info;
- if (!$approve_info) {
- throw new Exception(CommonConstant::get_module_list()[$approve_info->module] . '记录不存在或已删除');
- }
- if ($approve_info->status != CommonConstant::STATUS_2) {
- throw new Exception('非待处理状态无法操作!');
- }
- switch ($type) {
- case 'audit':
- self::audit($info, $params['status'], $params['remark']);
- break;
- case 'edit':
- self::edit($info, $params['module'], $params);
- break;
- }
- }
- /**
- * 审批
- *
- * @param mixed $info 审批信息
- * @param string $status 审批状态
- * @param string $remark 审批意见
- **/
- public static function audit($info, $status, $remark)
- {
- $approve_info = $info->approve_info;
- $approve_time = date('Y-m-d H:i:s');
- $duration = time() - strtotime($info['approve_time']);
- // 更新审批
- $info->status = $status;
- $info->approve_time = $approve_time;
- $info->remark = $remark;
- $info->time = $duration;
- $info->time_text = get_stay_time($duration);
- $info->save();
- // 更新审批申请
- if ($status == CommonConstant::STATUS_3) {
- if ($info['approve_flow'] < $approve_info['approve_num']) {
- // 更新下一级审批状态
- Approve::where(['info_id' => $info['info_id'], 'approve_flow' => $info['approve_flow'] + 1])->update(['status' => CommonConstant::STATUS_2, 'approve_time' => $approve_time]);
- } else {
- $approve_info->status = $status;
- }
- } else {
- $approve_info->status = $status;
- }
- $approve_info->cur_num = $approve_info['cur_num'] + 1;
- $approve_info->save();
- }
- /**
- * 修改
- *
- * @param mixed $info 审批信息
- * @param integer $module 模块类型
- * @param array $params 数组
- **/
- public static function edit($info, $module, $params)
- {
- $approve_info = $info->approve_info;
- $approve_info->module_info;
- $data = [
- 'reason' => $params['reason'],
- 'type' => $params['type'],
- 'desc' => $params['desc'],
- 'start_time' => $params['start_time'],
- 'end_time' => $params['end_time'],
- ];
- // 编辑出差申请
- $approve_info->save($data);
- // 编辑对应模块
- ApproveInfoService::create_module($approve_info->id, $module, $params, $approve_info, 'update');
- }
- /**
- * 拼接查询字段
- */
- public static function getAliasField($field, $aliasName)
- {
- $field = explode(',', $field);
- foreach ($field as &$value) {
- $value = $aliasName . $value;
- }
- return $field;
- }
- }
|