123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?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 EvectionInfoService
- {
- /**
- * 申请出差/重新发起
- *
- * @param $id
- * @param $params
- * @param $user
- **/
- public static function create($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;
- }
- }
- }
|