|
@@ -0,0 +1,257 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\common\service;
|
|
|
+
|
|
|
+use app\common\constant\CommonConstant;
|
|
|
+use app\common\model\Department;
|
|
|
+use app\common\model\EvectionApprove;
|
|
|
+use app\common\model\EvectionInfo;
|
|
|
+use app\common\model\EvectionPeerUser;
|
|
|
+use app\common\model\User;
|
|
|
+use think\Exception;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 审批申请服务类
|
|
|
+ */
|
|
|
+class ApproveInfoService
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 申请/重新发起
|
|
|
+ *
|
|
|
+ * @param $module
|
|
|
+ * @param $id
|
|
|
+ * @param $params
|
|
|
+ * @param $user
|
|
|
+ **/
|
|
|
+ public static function create($module,$id,$params, $user)
|
|
|
+ {
|
|
|
+ $userid = $user['userid'];
|
|
|
+ // 编辑
|
|
|
+ if ($id > 0) {
|
|
|
+ $info = EvectionInfo::field('user_id,is_deleted', true)
|
|
|
+ ->where('user_id', $userid)
|
|
|
+ ->where('is_deleted', CommonConstant::IS_DELETED_0)
|
|
|
+ ->find($id);
|
|
|
+ if (!$info) {
|
|
|
+ throw new Exception('出差记录不存在或已删除');
|
|
|
+ }
|
|
|
+ if($info->status != CommonConstant::STATUS_3){
|
|
|
+ throw new Exception('非审批驳回状态无法操作');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $order_no = get_order_sn($user->id);
|
|
|
+ $approve_user = explode(',', $params['approve_user']);
|
|
|
+ $copy_user = explode(',', $params['copy_user']);
|
|
|
+ $approve_num = count($approve_user);
|
|
|
+ $data = [
|
|
|
+ 'user_id' => $userid,
|
|
|
+ 'order_no' => $order_no,
|
|
|
+ 'apply_date' => date("Y-m-d"),
|
|
|
+ 'reason' => $params['reason'],
|
|
|
+ 'start_time' => $params['start_time'],
|
|
|
+ 'end_time' => $params['end_time'],
|
|
|
+ 'document' => $params['document'],
|
|
|
+ 'images' => $params['images'],
|
|
|
+ 'type' => $params['type'],
|
|
|
+ 'is_who' => $params['is_who'],
|
|
|
+ 'remark' => $params['remark'],
|
|
|
+ 'approve_num' => $approve_num,
|
|
|
+ 'cur_num' => 0,
|
|
|
+ ];
|
|
|
+ if ($id > 0) {
|
|
|
+ // 编辑出差申请
|
|
|
+ $save_data = [
|
|
|
+ 'status' => CommonConstant::STATUS_1,
|
|
|
+ 'create_at' => date('Y-m-d H:i:s'),
|
|
|
+ ];
|
|
|
+ $data = array_merge($data, $save_data);
|
|
|
+ $info->save($data);
|
|
|
+
|
|
|
+ // 编辑同行人员
|
|
|
+ self::create_peer_user($id, $params['peer_user'],'update');
|
|
|
+
|
|
|
+ // 编辑出差审批
|
|
|
+ self::create_evection_approve($id, $approve_user, $copy_user,'create');
|
|
|
+ } else {
|
|
|
+ // 添加出差申请
|
|
|
+ $info = EvectionInfo::create($data);
|
|
|
+
|
|
|
+ // 添加同行人员
|
|
|
+ self::create_peer_user($info->id, $params['peer_user']);
|
|
|
+
|
|
|
+ // 添加出差审批
|
|
|
+ self::create_evection_approve($info->id, $approve_user, $copy_user);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected static function create_peer_user($info_id, $params,$type = 'create')
|
|
|
+ {
|
|
|
+ if (!$params) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($type == 'update'){
|
|
|
+ EvectionPeerUser::where('info_id', $info_id)->delete();
|
|
|
+ }
|
|
|
+
|
|
|
+ $data = [];
|
|
|
+ foreach ($params as $value) {
|
|
|
+ $data[] = [
|
|
|
+ 'info_id' => $info_id,
|
|
|
+ 'is_who' => $value['is_who'],
|
|
|
+ 'user_id' => isset($value['user_id']) ? $value['user_id'] : '',
|
|
|
+ 'name' => $value['name'],
|
|
|
+ 'desc' => isset($value['desc']) ? $value['desc'] : '',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ EvectionPeerUser::insertAll($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected static function create_evection_approve($info_id, $approve_user, $copy_user,$type = 'create')
|
|
|
+ {
|
|
|
+ if($type == 'update'){
|
|
|
+ EvectionApprove::where('info_id', $info_id)->delete();
|
|
|
+ }
|
|
|
+
|
|
|
+ $data = [];
|
|
|
+ $flow_num = 0;
|
|
|
+ $create_at = date('Y-m-d H:i:s');
|
|
|
+ // 审批人
|
|
|
+ foreach ($approve_user as $key => $value) {
|
|
|
+ $flow_num++;
|
|
|
+ $data[] = [
|
|
|
+ 'info_id' => $info_id,
|
|
|
+ 'approve_flow' => $flow_num,
|
|
|
+ 'approve_type' => CommonConstant::TYPE_1,
|
|
|
+ 'approve_user' => $value,
|
|
|
+ 'approve_time' => $create_at,
|
|
|
+ 'create_at' => $create_at,
|
|
|
+ 'status' => $flow_num == 1 ? CommonConstant::STATUS_1 : CommonConstant::STATUS_0,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ // 抄送人
|
|
|
+ foreach ($copy_user as $key => $value) {
|
|
|
+ $data[] = [
|
|
|
+ 'info_id' => $info_id,
|
|
|
+ 'approve_flow' => 0,
|
|
|
+ 'approve_type' => CommonConstant::TYPE_2,
|
|
|
+ 'approve_user' => $value,
|
|
|
+ 'approve_time' => $create_at,
|
|
|
+ 'create_at' => $create_at,
|
|
|
+ 'status' => CommonConstant::STATUS_0,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ EvectionApprove::insertAll($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 列表
|
|
|
+ *
|
|
|
+ * @param $status
|
|
|
+ * @param $search
|
|
|
+ * @param $offset
|
|
|
+ * @param $length
|
|
|
+ * @param $user
|
|
|
+ **/
|
|
|
+ public static function get_list($status, $search, $offset, $length, $user)
|
|
|
+ {
|
|
|
+ $userid = $user['userid'];
|
|
|
+ $list = EvectionInfo::field('id,status,create_at,order_no,reason,start_time,end_time')
|
|
|
+ ->where('user_id', $userid)
|
|
|
+ ->where('is_deleted', CommonConstant::IS_DELETED_0)
|
|
|
+ ->when(array_key_exists($status, CommonConstant::get_status_list()), function ($query) use ($status) {
|
|
|
+ $query->where('status', $status);
|
|
|
+ })
|
|
|
+ ->when($search, function ($query) use ($search) {
|
|
|
+ $query->where('order_no|reason', 'like', '%' . $search . '%');
|
|
|
+ })
|
|
|
+ ->with(['peer_user' => function ($query) {
|
|
|
+ $query->field('id,info_id,name');
|
|
|
+ }, 'approve_info' => function ($query) use ($status) {
|
|
|
+ $query->field('id,info_id,status,approve_user')
|
|
|
+ ->where('status', $status)
|
|
|
+ ->with(['user' => function ($query) {
|
|
|
+ $query->field('userid,name');
|
|
|
+ }]);
|
|
|
+ }])
|
|
|
+ ->limit($offset, $length)
|
|
|
+ ->order('id desc')
|
|
|
+ ->select();
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 详情/信息
|
|
|
+ *
|
|
|
+ * @param $id
|
|
|
+ * @param $user
|
|
|
+ * @param $type
|
|
|
+ **/
|
|
|
+ public static function get_detail($id, $user,$type)
|
|
|
+ {
|
|
|
+ $userid = $user['userid'];
|
|
|
+ if($type == 'detail'){
|
|
|
+ $info = EvectionInfo::field('user_id,is_deleted', true)
|
|
|
+ ->where('user_id', $userid)
|
|
|
+ ->where('is_deleted', CommonConstant::IS_DELETED_0)
|
|
|
+ ->with(['peer_user' => function ($query) {
|
|
|
+ $query->field('id,info_id,name');
|
|
|
+ }, 'approve' => function ($query) {
|
|
|
+ $query->field('is_deleted,create_at', true)
|
|
|
+ ->with(['user' => function ($query) {
|
|
|
+ $query->field('userid,headimg,name');
|
|
|
+ }]);
|
|
|
+ }])
|
|
|
+ ->find($id);
|
|
|
+ if ($info) {
|
|
|
+ $info['department'] = Department::where('dept_id', 'in', $user['department'])->field('dept_id,name')->select();
|
|
|
+ }
|
|
|
+ } else{
|
|
|
+ $info = EvectionInfo::field('user_id,is_deleted', true)
|
|
|
+ ->where('user_id', $userid)
|
|
|
+ ->where('is_deleted', CommonConstant::IS_DELETED_0)
|
|
|
+ ->with(['peer_user'])
|
|
|
+ ->find($id);
|
|
|
+ }
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 操作方法
|
|
|
+ *
|
|
|
+ * @param $id
|
|
|
+ * @param $user
|
|
|
+ * @param $type
|
|
|
+ */
|
|
|
+ public static function make($id, $user,$type)
|
|
|
+ {
|
|
|
+ $userid = $user['userid'];
|
|
|
+ $info = EvectionInfo::field('id,status')
|
|
|
+ ->where('user_id', $userid)
|
|
|
+ ->where('is_deleted', CommonConstant::IS_DELETED_0)
|
|
|
+ ->find($id);
|
|
|
+ if(!$info) {
|
|
|
+ throw new Exception('出差记录不存在或已删除');
|
|
|
+ }
|
|
|
+ switch ($type){
|
|
|
+ case 'urging':
|
|
|
+ if($info->status != CommonConstant::STATUS_1){
|
|
|
+ throw new Exception('其他人已操作');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 钉钉接口 TODO
|
|
|
+ break;
|
|
|
+ case 'cancel':
|
|
|
+ if($info->status != CommonConstant::STATUS_3){
|
|
|
+ throw new Exception('非审批驳回状态无法操作');
|
|
|
+ }
|
|
|
+
|
|
|
+ $info->status = CommonConstant::STATUS_4;
|
|
|
+ $info->save();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|